feat(cellular, power): enable SDM660 IPA v2.6L 4G LTE, power-key status screen, and DVFS standby
Offline project checks / check (push) Canceled after 0s
Offline project checks / check (push) Canceled after 0s
This commit is contained in:
@@ -27,6 +27,71 @@ import time
|
||||
import tty
|
||||
|
||||
|
||||
class PPPFrameStats:
|
||||
"""Count validated async PPP frames without retaining packet contents."""
|
||||
|
||||
def __init__(self):
|
||||
self.frame = None
|
||||
self.escaped = False
|
||||
self.protocols = {}
|
||||
self.invalid_fcs = 0
|
||||
self.oversized = 0
|
||||
self.ipv4_bytes = 0
|
||||
|
||||
@staticmethod
|
||||
def fcs(data):
|
||||
value = 0xffff
|
||||
for byte in data:
|
||||
value ^= byte
|
||||
for _ in range(8):
|
||||
value = (value >> 1) ^ (0x8408 if value & 1 else 0)
|
||||
return value
|
||||
|
||||
def feed(self, data):
|
||||
for byte in data:
|
||||
if byte == 0x7e:
|
||||
if self.frame and not self.escaped:
|
||||
self.record(bytes(self.frame))
|
||||
self.frame = bytearray()
|
||||
self.escaped = False
|
||||
elif self.frame is not None:
|
||||
if self.escaped:
|
||||
self.frame.append(byte ^ 0x20)
|
||||
self.escaped = False
|
||||
elif byte == 0x7d:
|
||||
self.escaped = True
|
||||
else:
|
||||
self.frame.append(byte)
|
||||
if len(self.frame) > 65536:
|
||||
self.oversized += 1
|
||||
self.frame = None
|
||||
self.escaped = False
|
||||
|
||||
def record(self, frame):
|
||||
if len(frame) < 3 or self.fcs(frame) != 0xf0b8:
|
||||
self.invalid_fcs += 1
|
||||
return
|
||||
payload = frame[:-2]
|
||||
if payload.startswith(b'\xff\x03'):
|
||||
payload = payload[2:]
|
||||
if not payload:
|
||||
return
|
||||
size = 1 if payload[0] & 1 else 2
|
||||
if len(payload) < size:
|
||||
return
|
||||
protocol = int.from_bytes(payload[:size], 'big')
|
||||
name = {0x21: 'ipv4', 0x57: 'ipv6', 0xc021: 'lcp',
|
||||
0x8021: 'ipcp', 0xc023: 'pap', 0xc223: 'chap'}.get(
|
||||
protocol, f'0x{protocol:04x}')
|
||||
self.protocols[name] = self.protocols.get(name, 0) + 1
|
||||
if protocol == 0x21:
|
||||
self.ipv4_bytes += len(payload) - size
|
||||
|
||||
def summary(self):
|
||||
return {'protocol_frames': dict(self.protocols), 'ipv4_bytes': self.ipv4_bytes,
|
||||
'invalid_fcs': self.invalid_fcs, 'oversized_frames': self.oversized}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def exclusive_session(path='/run/lock/platina-cellular-rpmsg.lock'):
|
||||
fd = os.open(path, os.O_RDWR | os.O_CREAT | os.O_NOFOLLOW, 0o600)
|
||||
@@ -142,6 +207,7 @@ def query(fd, timeout=5):
|
||||
results = []
|
||||
for command in ('AT', 'AT+CFUN?', 'AT+CPIN?', 'AT+CSQ', 'AT+COPS?',
|
||||
'AT+CEREG?', 'AT+CGREG?', 'AT+CGATT?', 'AT+CGDCONT?',
|
||||
'AT+CGACT?', 'AT+CGPADDR', 'AT+CGCONTRDP',
|
||||
'AT+CGDATA=?', 'AT+CEER'):
|
||||
result = at_command(fd, command, timeout)
|
||||
result.pop('trailing')
|
||||
@@ -170,6 +236,52 @@ def interface_ipv4(interface):
|
||||
return None
|
||||
|
||||
|
||||
def stop_ppp(proc, fd, master, timeout=5):
|
||||
"""Keep the transport alive while pppd sends its termination handshake."""
|
||||
if proc.poll() is not None:
|
||||
return
|
||||
proc.terminate()
|
||||
deadline = time.monotonic() + timeout
|
||||
pending = bytearray()
|
||||
try:
|
||||
while proc.poll() is None and time.monotonic() < deadline:
|
||||
readable, writable, _ = select.select(
|
||||
[fd, master], [master] if pending else [], [],
|
||||
min(0.1, max(0, deadline - time.monotonic())))
|
||||
if master in writable:
|
||||
try:
|
||||
count = os.write(master, pending)
|
||||
del pending[:count]
|
||||
except BlockingIOError:
|
||||
pass
|
||||
if fd in readable:
|
||||
try:
|
||||
data = os.read(fd, 65536)
|
||||
except BlockingIOError:
|
||||
data = None
|
||||
if data == b'':
|
||||
break
|
||||
if data:
|
||||
pending.extend(data)
|
||||
if len(pending) > 1024 * 1024:
|
||||
break
|
||||
if master in readable:
|
||||
try:
|
||||
data = os.read(master, 1024)
|
||||
except BlockingIOError:
|
||||
continue
|
||||
if data:
|
||||
send_message(fd, data)
|
||||
except (OSError, RuntimeError):
|
||||
# Remote modem teardown can remove the endpoint before pppd exits.
|
||||
# Process cleanup must still finish, and must not hide the main error.
|
||||
pass
|
||||
finally:
|
||||
if proc.poll() is None:
|
||||
proc.kill()
|
||||
proc.wait(timeout=5)
|
||||
|
||||
|
||||
def probe_dns(interface, target):
|
||||
"""Query a public resolver over a socket explicitly bound to the PPP device."""
|
||||
transaction = os.urandom(2)
|
||||
@@ -278,6 +390,8 @@ def connect(fd, apn, interface='ppp0', duration=120, timeout=30, dial_method='at
|
||||
local_ip = None
|
||||
verification = None
|
||||
tx_bytes = rx_bytes = 0
|
||||
tx_frames, rx_frames = PPPFrameStats(), PPPFrameStats()
|
||||
rx_frames.feed(trailing)
|
||||
try:
|
||||
tty.setraw(slave)
|
||||
os.set_blocking(master, False)
|
||||
@@ -326,6 +440,7 @@ def connect(fd, apn, interface='ppp0', duration=120, timeout=30, dial_method='at
|
||||
if data:
|
||||
to_pty.extend(data)
|
||||
rx_bytes += len(data)
|
||||
rx_frames.feed(data)
|
||||
if len(to_pty) > 1024 * 1024:
|
||||
raise RuntimeError('PPP receive queue exceeded 1 MiB')
|
||||
if master in readable:
|
||||
@@ -340,9 +455,11 @@ def connect(fd, apn, interface='ppp0', duration=120, timeout=30, dial_method='at
|
||||
if data:
|
||||
send_message(fd, data)
|
||||
tx_bytes += len(data)
|
||||
tx_frames.feed(data)
|
||||
return {'interface': interface, 'local_ip': local_ip,
|
||||
'bridge_tx_bytes': tx_bytes,
|
||||
'bridge_rx_bytes': rx_bytes, 'pppd_exit': proc.poll(),
|
||||
'ppp_frames': {'tx': tx_frames.summary(), 'rx': rx_frames.summary()},
|
||||
'verification': verification,
|
||||
'ip_connectivity': bool(verification and verification['exit_code'] == 0)}
|
||||
finally:
|
||||
@@ -358,12 +475,7 @@ def connect(fd, apn, interface='ppp0', duration=120, timeout=30, dial_method='at
|
||||
if dns_pool is not None:
|
||||
dns_pool.shutdown(wait=True, cancel_futures=True)
|
||||
if proc is not None and proc.poll() is None:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
proc.wait()
|
||||
stop_ppp(proc, fd, master)
|
||||
os.close(master)
|
||||
os.close(slave)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user