test_spi.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 re
  18. import unittest.mock
  19. import pytest
  20. import cc1101
  21. import cc1101.addresses
  22. import cc1101.options
  23. # pylint: disable=protected-access
  24. @pytest.mark.parametrize("bus", [0, 1])
  25. @pytest.mark.parametrize("chip_select", [0, 2])
  26. def test___init__select_device(bus, chip_select):
  27. with unittest.mock.patch("spidev.SpiDev"):
  28. transceiver = cc1101.CC1101(spi_bus=bus, spi_chip_select=chip_select)
  29. assert transceiver._spi_bus == bus
  30. assert transceiver._spi_chip_select == chip_select
  31. assert transceiver._spi_device_path == "/dev/spidev{}.{}".format(bus, chip_select)
  32. transceiver._spi.open.side_effect = SystemExit
  33. with pytest.raises(SystemExit):
  34. with transceiver:
  35. pass
  36. transceiver._spi.open.assert_called_once_with(bus, chip_select)
  37. def test__read_status_register(transceiver):
  38. transceiver._spi.xfer.return_value = [0, 20]
  39. transceiver._read_status_register(cc1101.addresses.StatusRegisterAddress.VERSION)
  40. transceiver._spi.xfer.assert_called_once_with([0x31 | 0xC0, 0])
  41. def test__command_strobe(transceiver):
  42. transceiver._spi.xfer.return_value = [15]
  43. transceiver._command_strobe(cc1101.addresses.StrobeAddress.STX)
  44. transceiver._spi.xfer.assert_called_once_with([0x35 | 0x00])
  45. def test__reset(transceiver):
  46. transceiver._spi.xfer.return_value = [15]
  47. transceiver._reset()
  48. transceiver._spi.xfer.assert_called_once_with([0x30 | 0x00])
  49. @pytest.mark.parametrize("chip_version", [0x14, 0x04])
  50. def test___enter__(transceiver, chip_version):
  51. with unittest.mock.patch.object(
  52. transceiver, "_read_status_register"
  53. ) as read_status_register_mock, unittest.mock.patch.object(
  54. transceiver, "_reset"
  55. ) as reset_mock, unittest.mock.patch.object(
  56. transceiver, "_set_modulation_format"
  57. ) as set_modulation_format_mock, unittest.mock.patch.object(
  58. transceiver, "_set_power_amplifier_setting_index"
  59. ) as set_pa_setting_mock, unittest.mock.patch.object(
  60. transceiver, "_disable_data_whitening"
  61. ) as disable_whitening_mock, unittest.mock.patch.object(
  62. transceiver, "_write_burst"
  63. ) as write_burst_mock, unittest.mock.patch.object(
  64. transceiver,
  65. "get_main_radio_control_state_machine_state",
  66. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  67. ):
  68. read_status_register_mock.side_effect = lambda r: {
  69. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  70. cc1101.addresses.StatusRegisterAddress.VERSION: chip_version,
  71. }[r]
  72. with transceiver as transceiver_context:
  73. assert transceiver == transceiver_context
  74. transceiver._spi.open.assert_called_once_with(0, 0)
  75. assert transceiver._spi.max_speed_hz == 55700
  76. reset_mock.assert_called_once_with()
  77. set_modulation_format_mock.assert_called_once_with(
  78. cc1101.options.ModulationFormat.ASK_OOK
  79. )
  80. set_pa_setting_mock.assert_called_once_with(1)
  81. disable_whitening_mock.assert_called_once_with()
  82. write_burst_mock.assert_called_once_with(0x18, [0b010100])
  83. def test___enter___unsupported_chip_version(transceiver):
  84. with unittest.mock.patch.object(
  85. transceiver, "_read_status_register"
  86. ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
  87. read_status_register_mock.side_effect = lambda r: {
  88. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  89. cc1101.addresses.StatusRegisterAddress.VERSION: 0x15,
  90. }[r]
  91. with pytest.raises(
  92. ValueError,
  93. match=r"^{}$".format(
  94. re.escape(
  95. "unsupported chip version 0x15 (expected one of [0x04, 0x14])"
  96. )
  97. ),
  98. ):
  99. with transceiver:
  100. pass
  101. @pytest.mark.parametrize("bus", [0, 1])
  102. @pytest.mark.parametrize("chip_select", [0, 2])
  103. def test___enter__permission_error(transceiver, bus, chip_select):
  104. transceiver._spi.open.side_effect = PermissionError("[Errno 13] Permission denied")
  105. transceiver._spi_bus = bus
  106. transceiver._spi_chip_select = chip_select
  107. with pytest.raises(
  108. PermissionError, match=r"\s/dev/spidev{}.{}\s".format(bus, chip_select)
  109. ):
  110. with transceiver:
  111. pass