1
0

test_0x08_pktctrl0.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import unittest.mock
  18. import pytest
  19. # pylint: disable=import-private-name
  20. import cc1101
  21. from cc1101.options import PacketLengthMode, _TransceiveMode
  22. # pylint: disable=protected-access
  23. @pytest.mark.parametrize(
  24. ("pktctrl0_before", "pktctrl0_after"),
  25. [
  26. (0b01000101, 0b00000101),
  27. (0b00000101, 0b00000101),
  28. (0b11111111, 0b10111111),
  29. ],
  30. )
  31. def test_disable_data_whitening(
  32. transceiver: cc1101.CC1101, pktctrl0_before: int, pktctrl0_after: int
  33. ) -> None:
  34. xfer_mock = transceiver._spi.xfer
  35. xfer_mock.return_value = [0xFF] * 2 # chip status byte
  36. with unittest.mock.patch.object(
  37. transceiver, "_read_single_byte", return_value=pktctrl0_before
  38. ):
  39. transceiver._disable_data_whitening()
  40. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])
  41. def test__get_transceive_mode(transceiver: cc1101.CC1101) -> None:
  42. xfer_mock = transceiver._spi.xfer
  43. xfer_mock.return_value = [0x00, 0b01000101]
  44. assert transceiver._get_transceive_mode() == _TransceiveMode.FIFO
  45. xfer_mock.assert_called_once_with([0x08 | 0x80, 0])
  46. xfer_mock.return_value = [0x00, 0b00110101]
  47. assert transceiver._get_transceive_mode() == _TransceiveMode.ASYNCHRONOUS_SERIAL
  48. @pytest.mark.parametrize(
  49. ("pktctrl0_before", "mode", "pktctrl0_after"),
  50. [
  51. (0b01000101, _TransceiveMode.FIFO, 0b01000101),
  52. (0b01000101, _TransceiveMode.SYNCHRONOUS_SERIAL, 0b01010101),
  53. (0b01000101, _TransceiveMode.ASYNCHRONOUS_SERIAL, 0b01110101),
  54. (0b11111111, _TransceiveMode.FIFO, 0b11001111),
  55. ],
  56. )
  57. def test__set_transceive_mode(
  58. transceiver: cc1101.CC1101,
  59. pktctrl0_before: int,
  60. mode: _TransceiveMode,
  61. pktctrl0_after: int,
  62. ) -> None:
  63. xfer_mock = transceiver._spi.xfer
  64. xfer_mock.return_value = [0xFF] * 2 # chip status byte
  65. with unittest.mock.patch.object(
  66. transceiver, "_read_single_byte", return_value=pktctrl0_before
  67. ):
  68. transceiver._set_transceive_mode(mode)
  69. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])
  70. @pytest.mark.parametrize(
  71. ("pktctrl0_before", "pktctrl0_after"),
  72. (
  73. # unchanged
  74. (0b00000000, 0b00000000),
  75. (0b00010000, 0b00010000),
  76. (0b00010001, 0b00010001),
  77. (0b01000000, 0b01000000),
  78. (0b01000010, 0b01000010),
  79. (0b01110000, 0b01110000),
  80. (0b01110010, 0b01110010),
  81. # disabled
  82. (0b00010100, 0b00010000),
  83. (0b01000100, 0b01000000),
  84. (0b01000110, 0b01000010),
  85. (0b01110110, 0b01110010),
  86. ),
  87. )
  88. def test_disable_checksum(transceiver, pktctrl0_before, pktctrl0_after):
  89. xfer_mock = transceiver._spi.xfer
  90. xfer_mock.return_value = [15, 15]
  91. with unittest.mock.patch.object(
  92. transceiver, "_read_single_byte", return_value=pktctrl0_before
  93. ):
  94. transceiver.disable_checksum()
  95. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])
  96. @pytest.mark.parametrize(
  97. ("pktctrl0", "expected_mode"),
  98. (
  99. (0b00000000, PacketLengthMode.FIXED),
  100. (0b00000001, PacketLengthMode.VARIABLE),
  101. (0b01000100, PacketLengthMode.FIXED),
  102. (0b01000101, PacketLengthMode.VARIABLE),
  103. ),
  104. )
  105. def test_get_packet_length_mode(transceiver, pktctrl0, expected_mode):
  106. xfer_mock = transceiver._spi.xfer
  107. xfer_mock.return_value = [0, pktctrl0]
  108. assert transceiver.get_packet_length_mode() == expected_mode
  109. xfer_mock.assert_called_once_with([0x08 | 0x80, 0])
  110. @pytest.mark.parametrize(
  111. ("pktctrl0_before", "pktctrl0_after", "mode"),
  112. (
  113. (0b00000000, 0b00000000, PacketLengthMode.FIXED),
  114. (0b00000001, 0b00000000, PacketLengthMode.FIXED),
  115. (0b00000001, 0b00000001, PacketLengthMode.VARIABLE),
  116. (0b00000010, 0b00000000, PacketLengthMode.FIXED),
  117. (0b00000010, 0b00000001, PacketLengthMode.VARIABLE),
  118. (0b01000100, 0b01000100, PacketLengthMode.FIXED),
  119. (0b01000100, 0b01000101, PacketLengthMode.VARIABLE),
  120. (0b01000101, 0b01000100, PacketLengthMode.FIXED),
  121. (0b01000101, 0b01000101, PacketLengthMode.VARIABLE),
  122. ),
  123. )
  124. def test_set_packet_length_mode(transceiver, pktctrl0_before, pktctrl0_after, mode):
  125. xfer_mock = transceiver._spi.xfer
  126. xfer_mock.return_value = [15, 15]
  127. with unittest.mock.patch.object(
  128. transceiver, "_read_single_byte", return_value=pktctrl0_before
  129. ):
  130. transceiver.set_packet_length_mode(mode)
  131. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])