Skip to content
Open
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
31 changes: 31 additions & 0 deletions device/cheetah/MODEM-CLIENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ M4 runbook in MODEM-PLAN.md.

---

## Transport deadlines (0.3.34; offline validation only)

SIT transport waits use Linux `CLOCK_BOOTTIME`, which counts time spent
suspended. A request whose reply is observed at or after its deadline
raises `TimeoutError`, including when the reply was read before a scheduling
pause and processed afterwards. Indications encountered during a request
remain queued, and unread buffered packets remain available. A timeout does
not cancel a modem operation or prove that it did not execute; the transport
does not retransmit it. Token matching continues to reject old responses.

The socket tests hold `CLOCK_MONOTONIC` still and advance BOOTTIME across
real socket reads. They cover replies before, at and after the deadline,
a late modem error, indication retention and a pause after packet receipt.
These are host tests with a simulated clock jump, not a real suspend test.

`select()` still uses a relative wait: this change rechecks deadlines when
execution returns, but does not arrange a wake alarm or guarantee immediate
expiry on resume without socket activity. The modem sleep inhibitor stays
in place. CPIF/PCIe suspend, incoming-call/SMS wake, IMS continuity and
battery consumption still need attended hardware qualification under
[MODEM-PLAN](MODEM-PLAN.md) M6 and section 6.3.

Before promoting this package change, a device owner must run the existing
awake modem smoke/telephony checks on a qualified build and attach
`tools/phone-smoke` with its kernel release. Stop on lost registration,
missing indications or unexpected timeouts; retain logs with identifiers
removed and restore the previous package through the normal update path
with an approved reboot (never restart the modem in place). Risk: changed
request timeout classification. Do not bypass the sleep inhibitor to test
this patch; modem-on suspend remains a separate hardware gate.

## 1. What is built

Two packages:
Expand Down
22 changes: 14 additions & 8 deletions packages/omamobile-modem-sit/lib/sit.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def close(self):
os.close(self.lock)

def read_packet(self, timeout):
deadline = time.monotonic() + timeout
# Linux BOOTTIME includes suspend; MONOTONIC excludes it. This is
# deadline accounting, not a wake alarm or permission to suspend.
deadline = time.clock_gettime(time.CLOCK_BOOTTIME) + timeout
while True:
if len(self.buffer) >= 6:
kind, _, _, length = struct.unpack_from('<BBHH', self.buffer)
Expand All @@ -116,7 +118,7 @@ def read_packet(self, timeout):
packet = bytes(self.buffer[:length])
del self.buffer[:length]
return packet
remaining = deadline - time.monotonic()
remaining = deadline - time.clock_gettime(time.CLOCK_BOOTTIME)
if remaining <= 0:
return None
ready, _, _ = select.select([self.fd], [], [], remaining)
Expand All @@ -141,9 +143,9 @@ def request(self, ident, payload=b'', timeout=8, *, manifest_reply=False):
data = struct.pack('<BBHHIBB', 0, 0, ident, 12 + len(payload), self.token, 0, 0) + payload
if os.write(self.fd, data) != len(data):
raise RuntimeError('short SIT write')
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
packet = self.read_packet(deadline - time.monotonic())
deadline = time.clock_gettime(time.CLOCK_BOOTTIME) + timeout
while time.clock_gettime(time.CLOCK_BOOTTIME) < deadline:
packet = self.read_packet(deadline - time.clock_gettime(time.CLOCK_BOOTTIME))
if packet is None:
break
kind, _, message, length = struct.unpack_from('<BBHH', packet)
Expand All @@ -161,6 +163,10 @@ def request(self, ident, payload=b'', timeout=8, *, manifest_reply=False):
raise RuntimeError('unprocessed indication limit exceeded')
self.indications.append(packet)
elif message == ident and struct.unpack_from('<I', packet, 6)[0] == self.token:
# A read (or scheduling delay) can cross the deadline. A late
# response cannot turn an expired transaction into success.
if time.clock_gettime(time.CLOCK_BOOTTIME) >= deadline:
break
print(f'SIT {name(ident)}: error={packet[10]} length={length}', flush=True)
if packet[10]:
raise ModemError(packet[10], packet[12:])
Expand All @@ -170,9 +176,9 @@ def request(self, ident, payload=b'', timeout=8, *, manifest_reply=False):
def next_indication(self, timeout=1):
if self.indications:
return self.indications.popleft()
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
packet = self.read_packet(deadline - time.monotonic())
deadline = time.clock_gettime(time.CLOCK_BOOTTIME) + timeout
while time.clock_gettime(time.CLOCK_BOOTTIME) < deadline:
packet = self.read_packet(deadline - time.clock_gettime(time.CLOCK_BOOTTIME))
if packet is None or packet[0] == 2:
return packet
return None
59 changes: 59 additions & 0 deletions packages/omamobile-modem-sit/test/test_sit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import tempfile
import threading
import unittest
from unittest.mock import patch
import sit
from sit import Sit

Expand Down Expand Up @@ -59,6 +60,64 @@ def reply():
self.assertEqual(struct.unpack_from('<I', self.client.next_indication(), 8)[0], 399)
self.assertFalse(self.client.indications)

def test_request_deadline_counts_suspend_and_preserves_indications(self):
# Real socket I/O, with only the clocks advanced to model suspend.
# A completed write does not prove that the modem did not execute it.
for elapsed, error in ((7, 0), (8, 0), (9, 0), (9, 1)):
with self.subTest(elapsed=elapsed, error=error):
now = [100.0]
token = self.client.token + 1
indication = struct.pack('<BBHHH', 2, 0, 0x0009, 8, 0)
response = struct.pack('<BBHHIBB', 1, 0, 0x0701, 13,
token, error, 0) + b'x'
self.peer.sendall(indication + response)
real_read = os.read

def resumed_read(fd, size):
data = real_read(fd, size)
now[0] += elapsed
return data

def boottime(clock):
self.assertEqual(clock, sit.time.CLOCK_BOOTTIME)
return now[0]

with patch.object(sit.time, 'monotonic', return_value=100.0), \
patch.object(sit.time, 'clock_gettime', side_effect=boottime), \
patch.object(sit.os, 'read', side_effect=resumed_read):
if elapsed < 8:
self.assertEqual(self.client.request(0x0701), b'x')
else:
with self.assertRaises(TimeoutError):
self.client.request(0x0701)
self.assertEqual(self.client.next_indication(), indication)
# Drain any late response without treating it as an indication.
self.assertIsNone(self.client.next_indication(0.001))
sent = self.peer.recv(4096)
self.assertEqual(len(sent), 12) # exactly one request, no retry
self.assertEqual(struct.unpack_from('<I', sent, 6)[0], token)

def test_reply_crossing_deadline_after_read_is_not_success(self):
for error in (0, 1):
with self.subTest(error=error):
token = self.client.token + 1
response = struct.pack('<BBHHIBB', 1, 0, 0x0701, 13,
token, error, 0) + b'x'
self.peer.sendall(response)
now = [100.0]
real_read_packet = self.client.read_packet

def delayed_packet(timeout):
packet = real_read_packet(timeout)
now[0] += 8
return packet

with patch.object(sit.time, 'monotonic', side_effect=lambda: now[0]), \
patch.object(sit.time, 'clock_gettime', side_effect=lambda _: now[0]), \
patch.object(self.client, 'read_packet', side_effect=delayed_packet):
with self.assertRaises(TimeoutError):
self.client.request(0x0701)

def test_disallowed_commands_never_write(self):
for command in (0x0001, 0x0100, 0x0d00, 0x0d02, 0x0d03, 0x0d0a, 0x0d1b, 0x0201, 0x0208, 0x020c, 0x0249):
with self.assertRaises(ValueError):
Expand Down
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.33
0.3.34