diff --git a/common/android.py b/common/android.py index e88080884c2401..a4b8b836e648f1 100644 --- a/common/android.py +++ b/common/android.py @@ -4,6 +4,7 @@ import re import struct import subprocess +import random ANDROID = os.path.isfile('/EON') @@ -17,9 +18,10 @@ def get_imei(slot): if slot not in ("0", "1"): raise ValueError("SIM slot must be 0 or 1") - ret = parse_service_call_string(["iphonesubinfo", "3" ,"i32", str(slot)]) + ret = parse_service_call_string(service_call(["iphonesubinfo", "3" ,"i32", str(slot)])) if not ret: - ret = "000000000000000" + # allow non android to be identified differently + ret = "%015d" % random.randint(0, 1<<32) return ret def get_serial(): @@ -29,7 +31,7 @@ def get_serial(): return ret def get_subscriber_info(): - ret = parse_service_call_string(["iphonesubinfo", "7"]) + ret = parse_service_call_string(service_call(["iphonesubinfo", "7"])) if ret is None or len(ret) < 8: return "" return ret @@ -47,15 +49,23 @@ def reboot(reason=None): "i32", "1" # wait ]) -def parse_service_call_unpack(call, fmt): - r = parse_service_call_bytes(call) +def service_call(call): + if not ANDROID: + return None + + ret = subprocess.check_output(["service", "call", *call], encoding='utf8').strip() + if 'Parcel' not in ret: + return None + + return parse_service_call_bytes(ret) + +def parse_service_call_unpack(r, fmt): try: return struct.unpack(fmt, r)[0] except Exception: return None -def parse_service_call_string(call): - r = parse_service_call_bytes(call) +def parse_service_call_string(r): try: r = r[8:] # Cut off length field r = r.decode('utf_16_be') @@ -71,13 +81,7 @@ def parse_service_call_string(call): except Exception: return None -def parse_service_call_bytes(call): - if not ANDROID: - return None - ret = subprocess.check_output(["service", "call", *call], encoding='utf8').strip() - if 'Parcel' not in ret: - return None - +def parse_service_call_bytes(ret): try: r = b"" for hex_part in re.findall(r'[ (]([0-9a-f]{8})', ret): diff --git a/selfdrive/athena/athenad.py b/selfdrive/athena/athenad.py index 76d4a9ed38cda0..bf3836d07f3173 100755 --- a/selfdrive/athena/athenad.py +++ b/selfdrive/athena/athenad.py @@ -204,8 +204,8 @@ def getSimInfo(): network_type = android.getprop("gsm.network.type").split(',') mcc_mnc = android.getprop("gsm.sim.operator.numeric") or None - sim_id = android.parse_service_call_string(['iphonesubinfo', '11']) - cell_data_state = android.parse_service_call_unpack(['phone', '46'], ">q") + sim_id = android.parse_service_call_string(android.service_call(['iphonesubinfo', '11'])) + cell_data_state = android.parse_service_call_unpack(android.service_call(['phone', '46']), ">q") cell_data_connected = (cell_data_state == 2) return {