test_gpio.py 4.6 KB

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