diff --git a/packages/omamobile-modem-sit/lib/cp.py b/packages/omamobile-modem-sit/lib/cp.py index f8725bba8..65108b2ea 100644 --- a/packages/omamobile-modem-sit/lib/cp.py +++ b/packages/omamobile-modem-sit/lib/cp.py @@ -22,17 +22,29 @@ MODEM_UNIT = 'omamobile-modem.service' +# One open file for every status request. On the Pixel 9a each open and +# close of umts_boot0 wakes the CP's PCIe link for about 100 ms, while the +# status ioctl on a held file doesn't wake it at all (omamobile-modem 0.1.9, +# measured 2026-10-29), so the service's 2 s check would otherwise cycle the +# link thirty times a minute for as long as it runs. +_fd = None + + def state(): + global _fd if BOOT == 'none': return ONLINE try: - fd = os.open(BOOT, os.O_RDWR | os.O_CLOEXEC) + if _fd is None: + _fd = os.open(BOOT, os.O_RDWR | os.O_CLOEXEC) + return fcntl.ioctl(_fd, IOCTL_GET_CP_STATUS, 0) except FileNotFoundError: return 0 - try: - return fcntl.ioctl(fd, IOCTL_GET_CP_STATUS, 0) - finally: - os.close(fd) + except OSError: + if _fd is not None: + os.close(_fd) + _fd = None + raise def online(): diff --git a/packages/omamobile-modem-sit/test/test_cp.py b/packages/omamobile-modem-sit/test/test_cp.py new file mode 100644 index 000000000..28828b74b --- /dev/null +++ b/packages/omamobile-modem-sit/test/test_cp.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: MIT +"""cp.state() reads the CP state on one open file: it opens umts_boot0 once +and reuses it, reopens after an error, and reads a missing device as +OFFLINE (the suite itself runs with the CP check off).""" +import env # noqa: F401 (paths and sys.path first) +import unittest +from unittest.mock import patch + +import cp + + +class HeldFile(unittest.TestCase): + def setUp(self): + cp._fd = None + self.addCleanup(setattr, cp, '_fd', None) + + def test_opens_once_and_reuses_the_file(self): + with patch.object(cp, 'BOOT', '/dev/umts_boot0'), \ + patch.object(cp.os, 'open', return_value=7) as opened, \ + patch.object(cp.os, 'close') as closed, \ + patch.object(cp.fcntl, 'ioctl', return_value=cp.ONLINE) as ioctl: + self.assertEqual(cp.state(), cp.ONLINE) + self.assertEqual(cp.state(), cp.ONLINE) + self.assertTrue(cp.online()) + self.assertEqual(opened.call_count, 1) + self.assertEqual(ioctl.call_count, 3) + closed.assert_not_called() + self.assertEqual(ioctl.call_args[0][0], 7) + + def test_reopens_after_an_error(self): + with patch.object(cp, 'BOOT', '/dev/umts_boot0'), \ + patch.object(cp.os, 'open', side_effect=[7, 8]) as opened, \ + patch.object(cp.os, 'close') as closed, \ + patch.object(cp.fcntl, 'ioctl', + side_effect=[OSError(5, 'I/O error'), 3]): + with self.assertRaises(OSError): + cp.state() + closed.assert_called_once_with(7) + self.assertIsNone(cp._fd) + self.assertEqual(cp.state(), 3) + self.assertEqual(opened.call_count, 2) + self.assertEqual(cp._fd, 8) + + def test_missing_device_reads_offline(self): + with patch.object(cp, 'BOOT', '/dev/umts_boot0'), \ + patch.object(cp.os, 'open', side_effect=FileNotFoundError): + self.assertEqual(cp.state(), 0) + self.assertFalse(cp.online()) + self.assertIsNone(cp._fd) + + def test_no_device_configured_is_online(self): + with patch.object(cp, 'BOOT', 'none'), \ + patch.object(cp.os, 'open') as opened: + self.assertTrue(cp.online()) + opened.assert_not_called() + + +if __name__ == '__main__': + unittest.main() diff --git a/packages/omamobile-modem-sit/version b/packages/omamobile-modem-sit/version index 1c09c74e2..42045acae 100644 --- a/packages/omamobile-modem-sit/version +++ b/packages/omamobile-modem-sit/version @@ -1 +1 @@ -0.3.3 +0.3.4