test_receive.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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=protected-access
  20. def test__enable_receive_mode(transceiver):
  21. transceiver._spi.xfer.return_value = [15]
  22. transceiver._enable_receive_mode()
  23. transceiver._spi.xfer.assert_called_once_with([0x34 | 0x00])
  24. @pytest.mark.parametrize("payload", [b"\0", b"\x12\x45\x56"])
  25. def test__get_received_packet(transceiver, payload):
  26. fifo_buffer = list(payload) + [128, (1 << 7) | 42]
  27. with unittest.mock.patch.object(
  28. transceiver, "_read_status_register", return_value=len(fifo_buffer)
  29. ) as read_status_register, unittest.mock.patch.object(
  30. transceiver, "_read_burst", return_value=fifo_buffer
  31. ) as read_burst_mock:
  32. received_packet = transceiver._get_received_packet()
  33. read_status_register.assert_called_once_with(0x3B)
  34. read_burst_mock.assert_called_once_with(
  35. start_register=0x3F, length=len(fifo_buffer)
  36. )
  37. assert received_packet.payload == payload
  38. assert received_packet._rssi_index == 128
  39. assert received_packet.checksum_valid
  40. assert received_packet.link_quality_indicator == 42
  41. @pytest.mark.parametrize("gdo0_gpio_line_name", (b"GPIO24", b"GPIO25"))
  42. @pytest.mark.parametrize("reached_timeout", (True, False))
  43. @pytest.mark.parametrize("timeout_seconds", (2,))
  44. def test__wait_for_packet(
  45. transceiver, gdo0_gpio_line_name, timeout_seconds, reached_timeout
  46. ):
  47. line_mock = unittest.mock.MagicMock()
  48. line_mock.wait_for_rising_edge.return_value = not reached_timeout
  49. with unittest.mock.patch(
  50. "cc1101._gpio.GPIOLine.find", return_value=line_mock
  51. ) as find_line_mock, unittest.mock.patch.object(
  52. transceiver, "_get_received_packet", return_value="packet-dummy"
  53. ) as get_received_packet_mock, unittest.mock.patch.object(
  54. transceiver, "_enable_receive_mode"
  55. ) as enable_receive_mode_mock, unittest.mock.patch.object(
  56. transceiver, "_command_strobe"
  57. ) as command_strobe_mock:
  58. packet = transceiver._wait_for_packet(
  59. timeout_seconds=timeout_seconds,
  60. gdo0_gpio_line_name=gdo0_gpio_line_name,
  61. )
  62. find_line_mock.assert_called_once_with(name=gdo0_gpio_line_name)
  63. enable_receive_mode_mock.assert_called_once_with()
  64. line_mock.wait_for_rising_edge.assert_called_once_with(
  65. consumer=b"CC1101:GDO0", timeout_seconds=timeout_seconds
  66. )
  67. if reached_timeout:
  68. assert packet is None
  69. command_strobe_mock.assert_called_once_with(0x36) # SIDLE
  70. get_received_packet_mock.assert_not_called()
  71. else:
  72. command_strobe_mock.assert_not_called()
  73. get_received_packet_mock.assert_called_once_with()
  74. assert packet == "packet-dummy"