test_receive.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 datetime
  18. import unittest.mock
  19. import gpiod
  20. import pytest
  21. # pylint: disable=protected-access
  22. def test__enable_receive_mode(transceiver):
  23. transceiver._spi.xfer.return_value = [15]
  24. transceiver._enable_receive_mode()
  25. transceiver._spi.xfer.assert_called_once_with([0x34 | 0x00])
  26. @pytest.mark.parametrize("payload", [b"\0", b"\x12\x45\x56"])
  27. def test__get_received_packet(transceiver, payload):
  28. fifo_buffer = list(payload) + [128, (1 << 7) | 42]
  29. with unittest.mock.patch.object(
  30. transceiver, "_read_status_register", return_value=len(fifo_buffer)
  31. ) as read_status_register, unittest.mock.patch.object(
  32. transceiver, "_read_burst", return_value=fifo_buffer
  33. ) as read_burst_mock:
  34. received_packet = transceiver._get_received_packet()
  35. read_status_register.assert_called_once_with(0x3B)
  36. read_burst_mock.assert_called_once_with(
  37. start_register=0x3F, length=len(fifo_buffer)
  38. )
  39. assert received_packet.payload == payload
  40. assert received_packet._rssi_index == 128
  41. assert received_packet.checksum_valid
  42. assert received_packet.link_quality_indicator == 42
  43. @pytest.mark.parametrize("gdo0_chip_selector", (0, "/dev/gpiochip1"))
  44. @pytest.mark.parametrize("gdo0_line_name", ("GPIO24", "GPIO25"))
  45. @pytest.mark.parametrize("reached_timeout", (True, False))
  46. @pytest.mark.parametrize("timeout", (datetime.timedelta(seconds=1),))
  47. def test__wait_for_packet(
  48. transceiver, gdo0_chip_selector, gdo0_line_name, timeout, reached_timeout
  49. ):
  50. line_mock = unittest.mock.MagicMock()
  51. line_mock.event_wait.return_value = not reached_timeout
  52. with unittest.mock.patch(
  53. "cc1101._gpio.get_line"
  54. ) as get_line_mock, unittest.mock.patch.object(
  55. transceiver, "_get_received_packet"
  56. ) as get_received_packet_mock, unittest.mock.patch.object(
  57. transceiver, "_enable_receive_mode"
  58. ) as enable_receive_mode_mock, unittest.mock.patch.object(
  59. transceiver, "_command_strobe"
  60. ) as command_strobe_mock:
  61. get_line_mock.return_value = line_mock
  62. get_received_packet_mock.return_value = "packet-dummy"
  63. packet = transceiver._wait_for_packet(
  64. timeout=timeout,
  65. gdo0_chip=gdo0_chip_selector,
  66. gdo0_line_name=gdo0_line_name,
  67. )
  68. get_line_mock.assert_called_once_with(
  69. chip_selector=gdo0_chip_selector, line_name=gdo0_line_name
  70. )
  71. assert line_mock.request.call_count == 1
  72. (line_request,) = line_mock.request.call_args[0]
  73. assert vars(line_request) == {
  74. "consumer": "CC1101:GDO0",
  75. "flags": 0,
  76. "request_type": gpiod.line_request.EVENT_RISING_EDGE,
  77. }
  78. assert not line_mock.request.call_args[1]
  79. enable_receive_mode_mock.assert_called_once_with()
  80. line_mock.event_wait.assert_called_once_with(timeout=timeout)
  81. if reached_timeout:
  82. assert packet is None
  83. command_strobe_mock.assert_called_once_with(0x36) # SIDLE
  84. get_received_packet_mock.assert_not_called()
  85. else:
  86. command_strobe_mock.assert_not_called()
  87. get_received_packet_mock.assert_called_once_with()
  88. assert packet == "packet-dummy"