test_receive.py 3.5 KB

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