Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/omamobile-modem-sit/lib/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
59 changes: 59 additions & 0 deletions packages/omamobile-modem-sit/test/test_cp.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion packages/omamobile-modem-sit/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.3
0.3.4