test_gpio.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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,
  38. match=r"^Failed to access GPIO line {!r}\.\n".format(re.escape(name)),
  39. ):
  40. cc1101._gpio.GPIOLine.find(name.encode())
  41. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  42. def test_line_find_non_existing(libgpiod_mock, name):
  43. libgpiod_mock.gpiod_line_find.return_value = 0
  44. ctypes.set_errno(errno.ENOENT)
  45. with pytest.raises(
  46. FileNotFoundError,
  47. match=r"^GPIO line {!r} does not exist\.\n".format(re.escape(name)),
  48. ):
  49. cc1101._gpio.GPIOLine.find(name.encode())
  50. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  51. def test_line_find_unknown_error(libgpiod_mock, name):
  52. libgpiod_mock.gpiod_line_find.return_value = 0
  53. ctypes.set_errno(errno.ENOANO)
  54. with pytest.raises(
  55. OSError,
  56. match=r"^Failed to open GPIO line {!r}: ENOANO$".format(re.escape(name)),
  57. ):
  58. cc1101._gpio.GPIOLine.find(name.encode())
  59. def test_line_find(libgpiod_mock):
  60. libgpiod_mock.gpiod_line_find.return_value = 21
  61. line = cc1101._gpio.GPIOLine.find(b"GPIO24")
  62. libgpiod_mock.gpiod_line_find.assert_called_once_with(b"GPIO24")
  63. assert isinstance(line, cc1101._gpio.GPIOLine)
  64. assert line._pointer.value == 21
  65. def test_line_release(libgpiod_mock):
  66. line = cc1101._gpio.GPIOLine(42)
  67. del line
  68. libgpiod_mock.gpiod_line_close_chip.assert_called_once_with(42)
  69. @pytest.mark.parametrize("consumer", (b"CC1101 GDO0", b"test"))
  70. @pytest.mark.parametrize("timeout_seconds", (21.42, 0.1234))
  71. @pytest.mark.parametrize("reached_timeout", (False, True))
  72. def test_line_wait_for_rising_edge(
  73. libgpiod_mock, consumer: bytes, timeout_seconds: float, reached_timeout: bool
  74. ):
  75. pointer = ctypes.c_void_p(1234)
  76. line = cc1101._gpio.GPIOLine(pointer=pointer)
  77. libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = 0
  78. libgpiod_mock.gpiod_line_event_wait.return_value = 0 if reached_timeout else 1
  79. event_occured = line.wait_for_rising_edge(
  80. consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
  81. )
  82. assert event_occured is not reached_timeout
  83. libgpiod_mock.gpiod_line_request_rising_edge_events.assert_called_once_with(
  84. pointer, consumer
  85. )
  86. assert libgpiod_mock.gpiod_line_event_wait.call_count == 1
  87. wait_args, wait_kwargs = libgpiod_mock.gpiod_line_event_wait.call_args
  88. assert wait_args[0] == pointer
  89. assert (
  90. wait_args[1].contents.tv_sec + wait_args[1].contents.tv_nsec / 10 ** 9
  91. ) == pytest.approx(timeout_seconds, abs=1e-10)
  92. assert not wait_args[2:]
  93. assert not wait_kwargs
  94. libgpiod_mock.gpiod_line_release.assert_called_once_with(pointer)
  95. @pytest.mark.parametrize("consumer", (b"CC1101 GDO0",))
  96. @pytest.mark.parametrize("timeout_seconds", (21.42,))
  97. def test_line_wait_for_rising_edge_busy(
  98. libgpiod_mock, consumer: bytes, timeout_seconds: float
  99. ):
  100. pointer = ctypes.c_void_p(1234)
  101. line = cc1101._gpio.GPIOLine(pointer=pointer)
  102. libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = -1
  103. ctypes.set_errno(errno.EBUSY)
  104. with pytest.raises(
  105. OSError,
  106. match=r"^Request for rising edge event notifications failed \(EBUSY\)."
  107. r"\nBlocked by another process\?$",
  108. ):
  109. line.wait_for_rising_edge(
  110. consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
  111. )