cleanup eon hw abstraction layer (#21396)

* cleanup eon hw

* handle none from getprop
pull/69/head
Adeeb Shihadeh 4 years ago committed by GitHub
parent 1da3ab42dc
commit 8ac7ee6bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      selfdrive/hardware/eon/hardware.py

@ -5,6 +5,7 @@ import re
import serial import serial
import struct import struct
import subprocess import subprocess
from typing import List, Union
from cereal import log from cereal import log
from selfdrive.hardware.base import HardwareBase, ThermalConfig from selfdrive.hardware.base import HardwareBase, ThermalConfig
@ -14,7 +15,7 @@ NetworkStrength = log.DeviceState.NetworkStrength
MODEM_PATH = "/dev/smd11" MODEM_PATH = "/dev/smd11"
def service_call(call): def service_call(call: List[str]) -> Union[bytes, None]:
try: try:
ret = subprocess.check_output(["service", "call", *call], encoding='utf8').strip() ret = subprocess.check_output(["service", "call", *call], encoding='utf8').strip()
if 'Parcel' not in ret: if 'Parcel' not in ret:
@ -24,31 +25,29 @@ def service_call(call):
return None return None
def parse_service_call_unpack(r, fmt): def parse_service_call_unpack(r, fmt) -> Union[bytes, None]:
try: try:
return struct.unpack(fmt, r)[0] return struct.unpack(fmt, r)[0]
except Exception: except Exception:
return None return None
def parse_service_call_string(r): def parse_service_call_string(r: bytes) -> Union[str, None]:
try: try:
r = r[8:] # Cut off length field r = r[8:] # Cut off length field
r = r.decode('utf_16_be') r_str = r.decode('utf_16_be')
# All pairs of two characters seem to be swapped. Not sure why # All pairs of two characters seem to be swapped. Not sure why
result = "" result = ""
for a, b, in itertools.zip_longest(r[::2], r[1::2], fillvalue='\x00'): for a, b, in itertools.zip_longest(r_str[::2], r_str[1::2], fillvalue='\x00'):
result += b + a result += b + a
result = result.replace('\x00', '') return result.replace('\x00', '')
return result
except Exception: except Exception:
return None return None
def parse_service_call_bytes(ret): def parse_service_call_bytes(ret: str) -> Union[bytes, None]:
try: try:
r = b"" r = b""
for hex_part in re.findall(r'[ (]([0-9a-f]{8})', ret): for hex_part in re.findall(r'[ (]([0-9a-f]{8})', ret):
@ -58,8 +57,11 @@ def parse_service_call_bytes(ret):
return None return None
def getprop(key): def getprop(key: str) -> Union[str, None]:
return subprocess.check_output(["getprop", key], encoding='utf8').strip() try:
return subprocess.check_output(["getprop", key], encoding='utf8').strip()
except subprocess.CalledProcessError:
return None
class Android(HardwareBase): class Android(HardwareBase):
@ -134,9 +136,9 @@ class Android(HardwareBase):
def get_network_info(self): def get_network_info(self):
msg = log.DeviceState.NetworkInfo.new_message() msg = log.DeviceState.NetworkInfo.new_message()
msg.state = getprop("gsm.sim.state") msg.state = getprop("gsm.sim.state") or ""
msg.technology = getprop("gsm.network.type") msg.technology = getprop("gsm.network.type") or ""
msg.operator = getprop("gsm.sim.operator.numeric") msg.operator = getprop("gsm.sim.operator.numeric") or ""
try: try:
modem = serial.Serial(MODEM_PATH, 115200, timeout=0.1) modem = serial.Serial(MODEM_PATH, 115200, timeout=0.1)

Loading…
Cancel
Save