test_gpio.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2021 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 ctypes
  18. import ctypes.util
  19. import datetime
  20. import errno
  21. import re
  22. import unittest.mock
  23. import pytest
  24. import cc1101._gpio
  25. # pylint: disable=protected-access
  26. def test__load_libgpiod():
  27. with unittest.mock.patch(
  28. "ctypes.util.find_library", return_value=ctypes.util.find_library("c")
  29. ) as find_library_mock:
  30. assert isinstance(cc1101._gpio._load_libgpiod(), ctypes.CDLL)
  31. find_library_mock.assert_called_once_with("gpiod")
  32. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  33. def test_line_find_permission_denied(libgpiod_mock, name):
  34. libgpiod_mock.gpiod_line_find.return_value = 0
  35. ctypes.set_errno(errno.EACCES)
  36. with pytest.raises(
  37. PermissionError, match=f"^Failed to access GPIO line {re.escape(name)!r}\\.\\n"
  38. ):
  39. cc1101._gpio.GPIOLine.find(name.encode())
  40. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  41. def test_line_find_non_existing(libgpiod_mock, name):
  42. libgpiod_mock.gpiod_line_find.return_value = 0
  43. ctypes.set_errno(errno.ENOENT)
  44. with pytest.raises(
  45. FileNotFoundError, match=f"^GPIO line {re.escape(name)!r} does not exist\\.\\n"
  46. ):
  47. cc1101._gpio.GPIOLine.find(name.encode())
  48. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  49. def test_line_find_unknown_error(libgpiod_mock, name):
  50. libgpiod_mock.gpiod_line_find.return_value = 0
  51. ctypes.set_errno(errno.ENOANO)
  52. with pytest.raises(
  53. OSError, match=f"^Failed to open GPIO line {re.escape(name)!r}: ENOANO$"
  54. ):
  55. cc1101._gpio.GPIOLine.find(name.encode())
  56. def test_line_find(libgpiod_mock):
  57. libgpiod_mock.gpiod_line_find.return_value = 21
  58. line = cc1101._gpio.GPIOLine.find(b"GPIO24")
  59. libgpiod_mock.gpiod_line_find.assert_called_once_with(b"GPIO24")
  60. assert isinstance(line, cc1101._gpio.GPIOLine)
  61. assert line._pointer.value == 21
  62. def test_line_release(libgpiod_mock):
  63. line = cc1101._gpio.GPIOLine(42)
  64. del line
  65. libgpiod_mock.gpiod_line_close_chip.assert_called_once_with(42)
  66. @pytest.mark.parametrize("consumer", (b"CC1101 GDO0", b"test"))
  67. @pytest.mark.parametrize("timeout_seconds", (21.42, 0.1234))
  68. @pytest.mark.parametrize("reached_timeout", (False, True))
  69. def test_line_wait_for_rising_edge(
  70. libgpiod_mock, consumer: bytes, timeout_seconds: float, reached_timeout: bool
  71. ):
  72. pointer = ctypes.c_void_p(1234)
  73. line = cc1101._gpio.GPIOLine(pointer=pointer)
  74. libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = 0
  75. libgpiod_mock.gpiod_line_event_wait.return_value = 0 if reached_timeout else 1
  76. event_occured = line.wait_for_rising_edge(
  77. consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
  78. )
  79. assert event_occured is not reached_timeout
  80. libgpiod_mock.gpiod_line_request_rising_edge_events.assert_called_once_with(
  81. pointer, consumer
  82. )
  83. libgpiod_mock.gpiod_line_event_wait.assert_called_once()
  84. wait_args, wait_kwargs = libgpiod_mock.gpiod_line_event_wait.call_args
  85. assert wait_args[0] == pointer
  86. assert (
  87. wait_args[1].contents.tv_sec + wait_args[1].contents.tv_nsec / 10 ** 9
  88. ) == pytest.approx(timeout_seconds, abs=1e-10)
  89. assert not wait_args[2:]
  90. assert not wait_kwargs
  91. libgpiod_mock.gpiod_line_release.assert_called_once_with(pointer)
  92. @pytest.mark.parametrize("consumer", (b"CC1101 GDO0",))
  93. @pytest.mark.parametrize("timeout_seconds", (21.42,))
  94. def test_line_wait_for_rising_edge_busy(
  95. libgpiod_mock, consumer: bytes, timeout_seconds: float
  96. ):
  97. pointer = ctypes.c_void_p(1234)
  98. line = cc1101._gpio.GPIOLine(pointer=pointer)
  99. libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = -1
  100. ctypes.set_errno(errno.EBUSY)
  101. with pytest.raises(
  102. OSError,
  103. match=r"^Request for rising edge event notifications failed \(EBUSY\)."
  104. r"\nBlocked by another process\?$",
  105. ):
  106. line.wait_for_rising_edge(
  107. consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
  108. )