test_receive.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. @pytest.mark.parametrize("gdo0_gpio_line_name", (b"GPIO24", b"GPIO25"))
  43. @pytest.mark.parametrize("reached_timeout", (True, False))
  44. @pytest.mark.parametrize("timeout", (datetime.timedelta(seconds=4),))
  45. def test__wait_for_packet(transceiver, gdo0_gpio_line_name, timeout, reached_timeout):
  46. line_mock = unittest.mock.MagicMock()
  47. line_mock.wait_for_rising_edge.return_value = not reached_timeout
  48. with unittest.mock.patch(
  49. "cc1101._gpio.GPIOLine.find", return_value=line_mock
  50. ) as find_line_mock, unittest.mock.patch.object(
  51. transceiver, "_get_received_packet", return_value="packet-dummy"
  52. ) as get_received_packet_mock, unittest.mock.patch.object(
  53. transceiver, "_enable_receive_mode"
  54. ) as enable_receive_mode_mock, unittest.mock.patch.object(
  55. transceiver, "_command_strobe"
  56. ) as command_strobe_mock:
  57. packet = transceiver._wait_for_packet(
  58. timeout=timeout,
  59. gdo0_gpio_line_name=gdo0_gpio_line_name,
  60. )
  61. find_line_mock.assert_called_once_with(name=gdo0_gpio_line_name)
  62. enable_receive_mode_mock.assert_called_once_with()
  63. line_mock.wait_for_rising_edge.assert_called_once_with(
  64. consumer=b"CC1101:GDO0", timeout=timeout
  65. )
  66. if reached_timeout:
  67. assert packet is None
  68. command_strobe_mock.assert_called_once_with(0x36) # SIDLE
  69. get_received_packet_mock.assert_not_called()
  70. else:
  71. command_strobe_mock.assert_not_called()
  72. get_received_packet_mock.assert_called_once_with()
  73. assert packet == "packet-dummy"