test_receive.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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:
  59. get_line_mock.return_value = line_mock
  60. get_received_packet_mock.return_value = "packet-dummy"
  61. packet = transceiver._wait_for_packet(
  62. timeout=timeout,
  63. gdo0_chip=gdo0_chip_selector,
  64. gdo0_line_name=gdo0_line_name,
  65. )
  66. get_line_mock.assert_called_once_with(
  67. chip_selector=gdo0_chip_selector, line_name=gdo0_line_name
  68. )
  69. assert line_mock.request.call_count == 1
  70. (line_request,) = line_mock.request.call_args[0]
  71. assert vars(line_request) == {
  72. "consumer": "CC1101:GDO0",
  73. "flags": 0,
  74. "request_type": gpiod.line_request.EVENT_RISING_EDGE,
  75. }
  76. assert not line_mock.request.call_args[1]
  77. enable_receive_mode_mock.assert_called_once_with()
  78. line_mock.event_wait.assert_called_once_with(timeout=timeout)
  79. if reached_timeout:
  80. assert packet is None
  81. get_received_packet_mock.assert_not_called()
  82. else:
  83. get_received_packet_mock.assert_called_once_with()
  84. assert packet == "packet-dummy"