test_config.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # python-cc1101 - Python Library to Transmit RF Signals via C1101 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. import cc1101
  20. import cc1101.options
  21. # pylint: disable=protected-access
  22. @pytest.mark.parametrize(
  23. ("xfer_return_value", "sync_word"),
  24. [([64, 211, 145], b"\xd3\x91"), ([64, 0, 0], b"\0\0")],
  25. )
  26. def test_get_sync_word(transceiver, xfer_return_value, sync_word):
  27. transceiver._spi.xfer.return_value = xfer_return_value
  28. assert transceiver.get_sync_word() == sync_word
  29. transceiver._spi.xfer.assert_called_once_with([0x04 | 0xC0, 0, 0])
  30. def test_set_sync_word(transceiver):
  31. transceiver._spi.xfer.return_value = [15, 15, 15]
  32. transceiver.set_sync_word(b"\x12\x34")
  33. transceiver._spi.xfer.assert_called_once_with([0x04 | 0x40, 0x12, 0x34])
  34. @pytest.mark.parametrize("sync_word", [b"", b"\0", "\x12\x34\x56"])
  35. def test_set_sync_word_invalid_length(transceiver, sync_word):
  36. with pytest.raises(ValueError, match=r"\bexpected two bytes\b"):
  37. transceiver.set_sync_word(sync_word)
  38. _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
  39. ([0x10, 0xA7, 0x62], 433000000),
  40. ([0x10, 0xAB, 0x85], 433420000),
  41. ([0x10, 0xB1, 0x3B], 434000000),
  42. ([0x21, 0x62, 0x76], 868000000),
  43. ]
  44. @pytest.mark.parametrize(
  45. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  46. )
  47. def test__frequency_control_word_to_hertz(control_word, hertz):
  48. assert cc1101.CC1101._frequency_control_word_to_hertz(
  49. control_word
  50. ) == pytest.approx(hertz, abs=200)
  51. @pytest.mark.parametrize(
  52. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  53. )
  54. def test__hertz_to_frequency_control_word(control_word, hertz):
  55. assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
  56. _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS = [
  57. # > The default values give 203 kHz channel filter bandwidth,
  58. # > assuming a 26.0 MHz crystal.
  59. (0, 2, 203e3),
  60. # "Table 26: Channel Filter Bandwidths [kHz] (assuming a 26 MHz crystal)"
  61. (0, 0, 812e3),
  62. (0, 1, 406e3),
  63. (0, 2, 203e3),
  64. (1, 0, 650e3),
  65. (1, 1, 325e3),
  66. (3, 0, 464e3),
  67. (3, 1, 232e3),
  68. (3, 2, 116e3),
  69. (3, 3, 58e3),
  70. ]
  71. @pytest.mark.parametrize(
  72. ("mantissa", "exponent", "real"), _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS
  73. )
  74. def test__filter_bandwidth_floating_point_to_real(mantissa, exponent, real):
  75. assert cc1101.CC1101._filter_bandwidth_floating_point_to_real(
  76. mantissa=mantissa, exponent=exponent
  77. ) == pytest.approx(real, rel=1e-3)
  78. @pytest.mark.parametrize(
  79. ("mdmcfg4", "real"),
  80. [
  81. (0b10001100, 203e3),
  82. (0b10001010, 203e3),
  83. (0b10001110, 203e3),
  84. (0b11111100, 58e3),
  85. (0b01011100, 325e3),
  86. ],
  87. )
  88. def test__get_filter_bandwidth_hertz(transceiver, mdmcfg4, real):
  89. transceiver._spi.xfer.return_value = [15, mdmcfg4]
  90. assert transceiver._get_filter_bandwidth_hertz() == pytest.approx(real, rel=1e-3)
  91. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
  92. @pytest.mark.parametrize(
  93. ("mdmcfg4", "symbol_rate_exponent"), [(0b1001100, 12), (0b10011001, 9)]
  94. )
  95. def test__get_symbol_rate_exponent(transceiver, mdmcfg4, symbol_rate_exponent):
  96. transceiver._spi.xfer.return_value = [15, mdmcfg4]
  97. assert transceiver._get_symbol_rate_exponent() == symbol_rate_exponent
  98. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
  99. @pytest.mark.parametrize(
  100. ("mdmcfg3", "symbol_rate_mantissa"), [(0b00100010, 34), (0b10101010, 170)]
  101. )
  102. def test__get_symbol_rate_mantissa(transceiver, mdmcfg3, symbol_rate_mantissa):
  103. transceiver._spi.xfer.return_value = [15, mdmcfg3]
  104. assert transceiver._get_symbol_rate_mantissa() == symbol_rate_mantissa
  105. transceiver._spi.xfer.assert_called_once_with([0x11 | 0x80, 0])
  106. _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
  107. # > The default values give a data rate of 115.051 kBaud
  108. # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
  109. (34, 12, 115051),
  110. (34, 12 + 1, 115051 * 2),
  111. (34, 12 - 1, 115051 / 2),
  112. ]
  113. @pytest.mark.parametrize(
  114. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  115. )
  116. def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
  117. assert cc1101.CC1101._symbol_rate_floating_point_to_real(
  118. mantissa=mantissa, exponent=exponent
  119. ) == pytest.approx(real, rel=1e-5)
  120. @pytest.mark.parametrize(
  121. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  122. )
  123. def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
  124. assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
  125. mantissa,
  126. exponent,
  127. )
  128. @pytest.mark.parametrize(
  129. ("mdmcfg1", "length"),
  130. [
  131. (0b00000010, 2),
  132. (0b00010010, 3),
  133. (0b00100010, 4),
  134. (0b00110010, 6),
  135. (0b01000010, 8),
  136. (0b01010010, 12),
  137. (0b01100010, 16),
  138. (0b01110010, 24),
  139. ],
  140. )
  141. def test_get_preamble_length_bytes(transceiver, mdmcfg1, length):
  142. transceiver._spi.xfer.return_value = [0, mdmcfg1]
  143. assert transceiver.get_preamble_length_bytes() == length
  144. transceiver._spi.xfer.assert_called_once_with([0x13 | 0x80, 0])
  145. @pytest.mark.parametrize(
  146. ("mdmcfg1_before", "mdmcfg1_after", "length"),
  147. [
  148. (0b00000010, 0b00000010, 2),
  149. (0b00000010, 0b00010010, 3),
  150. (0b00000010, 0b00100010, 4),
  151. (0b00000010, 0b00110010, 6),
  152. (0b00000010, 0b01000010, 8),
  153. (0b00000010, 0b01010010, 12),
  154. (0b00000010, 0b01100010, 16),
  155. (0b00000010, 0b01110010, 24),
  156. (0b01010010, 0b01100010, 16),
  157. (0b01110010, 0b00000010, 2),
  158. (0b01110010, 0b01000010, 8),
  159. (0b11011010, 0b11101010, 16),
  160. (0b11110111, 0b11000111, 8),
  161. (0b11111110, 0b10001110, 2),
  162. ],
  163. )
  164. def test_set_preamble_length_bytes(transceiver, mdmcfg1_before, mdmcfg1_after, length):
  165. transceiver._spi.xfer.return_value = [15, 15]
  166. with unittest.mock.patch.object(
  167. transceiver, "_read_single_byte", return_value=mdmcfg1_before
  168. ):
  169. transceiver.set_preamble_length_bytes(length)
  170. transceiver._spi.xfer.assert_called_once_with([0x13 | 0x40, mdmcfg1_after])
  171. @pytest.mark.parametrize(
  172. "length", [-21, 0, 1, 5, 7, 9, 10, 11, 13, 14, 15, 17, 20, 23, 25, 32, 42]
  173. )
  174. def test_set_preamble_length_bytes_invalid(transceiver, length):
  175. with pytest.raises(ValueError, match=r"\bpreamble length\b"):
  176. transceiver.set_preamble_length_bytes(length)
  177. def test_get_packet_length_bytes(transceiver):
  178. xfer_mock = transceiver._spi.xfer
  179. xfer_mock.return_value = [0, 8]
  180. assert transceiver.get_packet_length_bytes() == 8
  181. xfer_mock.assert_called_once_with([0x06 | 0x80, 0])
  182. @pytest.mark.parametrize("packet_length", [21])
  183. def test_set_packet_length_bytes(transceiver, packet_length):
  184. xfer_mock = transceiver._spi.xfer
  185. xfer_mock.return_value = [15, 15]
  186. transceiver.set_packet_length_bytes(packet_length)
  187. xfer_mock.assert_called_once_with([0x06 | 0x40, packet_length])
  188. @pytest.mark.parametrize("packet_length", [-21, 0, 256, 1024])
  189. def test_set_packet_length_bytes_fail(transceiver, packet_length):
  190. with pytest.raises(Exception):
  191. transceiver.set_packet_length_bytes(packet_length)
  192. transceiver._spi.xfer.assert_not_called()
  193. @pytest.mark.parametrize(
  194. ("pktctrl0_before", "pktctrl0_after"),
  195. (
  196. # unchanged
  197. (0b00000000, 0b00000000),
  198. (0b00010000, 0b00010000),
  199. (0b00010001, 0b00010001),
  200. (0b01000000, 0b01000000),
  201. (0b01000010, 0b01000010),
  202. (0b01110000, 0b01110000),
  203. (0b01110010, 0b01110010),
  204. # disabled
  205. (0b00010100, 0b00010000),
  206. (0b01000100, 0b01000000),
  207. (0b01000110, 0b01000010),
  208. (0b01110110, 0b01110010),
  209. ),
  210. )
  211. def test_disable_checksum(transceiver, pktctrl0_before, pktctrl0_after):
  212. xfer_mock = transceiver._spi.xfer
  213. xfer_mock.return_value = [15, 15]
  214. with unittest.mock.patch.object(
  215. transceiver, "_read_single_byte", return_value=pktctrl0_before
  216. ):
  217. transceiver.disable_checksum()
  218. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])
  219. @pytest.mark.parametrize(
  220. ("pktctrl0", "expected_mode"),
  221. (
  222. (0b00000000, cc1101.options.PacketLengthMode.FIXED),
  223. (0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  224. (0b01000100, cc1101.options.PacketLengthMode.FIXED),
  225. (0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  226. ),
  227. )
  228. def test_get_packet_length_mode(transceiver, pktctrl0, expected_mode):
  229. xfer_mock = transceiver._spi.xfer
  230. xfer_mock.return_value = [0, pktctrl0]
  231. assert transceiver.get_packet_length_mode() == expected_mode
  232. xfer_mock.assert_called_once_with([0x08 | 0x80, 0])
  233. @pytest.mark.parametrize(
  234. ("pktctrl0_before", "pktctrl0_after", "mode"),
  235. (
  236. (0b00000000, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  237. (0b00000001, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  238. (0b00000001, 0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  239. (0b00000010, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  240. (0b00000010, 0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  241. (0b01000100, 0b01000100, cc1101.options.PacketLengthMode.FIXED),
  242. (0b01000100, 0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  243. (0b01000101, 0b01000100, cc1101.options.PacketLengthMode.FIXED),
  244. (0b01000101, 0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  245. ),
  246. )
  247. def test_set_packet_length_mode(transceiver, pktctrl0_before, pktctrl0_after, mode):
  248. xfer_mock = transceiver._spi.xfer
  249. xfer_mock.return_value = [15, 15]
  250. with unittest.mock.patch.object(
  251. transceiver, "_read_single_byte", return_value=pktctrl0_before
  252. ):
  253. transceiver.set_packet_length_mode(mode)
  254. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])