update common/android.py

old-commit-hash: 67ab1c5b2e
commatwo_master
Willem Melching 5 years ago
parent 46403dff2a
commit 0f2056f352
  1. 32
      common/android.py

@ -4,6 +4,7 @@ import itertools
import re import re
import struct import struct
import subprocess import subprocess
import random
ANDROID = os.path.isfile('/EON') ANDROID = os.path.isfile('/EON')
@ -17,9 +18,10 @@ def get_imei(slot):
if slot not in ("0", "1"): if slot not in ("0", "1"):
raise ValueError("SIM slot must be 0 or 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: if not ret:
ret = "000000000000000" # allow non android to be identified differently
ret = "%015d" % random.randint(0, 1<<32)
return ret return ret
def get_serial(): def get_serial():
@ -29,7 +31,7 @@ def get_serial():
return ret return ret
def get_subscriber_info(): 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: if ret is None or len(ret) < 8:
return "" return ""
return ret return ret
@ -47,15 +49,23 @@ def reboot(reason=None):
"i32", "1" # wait "i32", "1" # wait
]) ])
def parse_service_call_unpack(call, fmt): def service_call(call):
r = 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
return parse_service_call_bytes(ret)
def parse_service_call_unpack(r, fmt):
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(call): def parse_service_call_string(r):
r = parse_service_call_bytes(call)
try: try:
r = r[8:] # Cut off length field r = r[8:] # Cut off length field
r = r.decode('utf_16_be') r = r.decode('utf_16_be')
@ -71,13 +81,7 @@ def parse_service_call_string(call):
except Exception: except Exception:
return None return None
def parse_service_call_bytes(call): def parse_service_call_bytes(ret):
if not ANDROID:
return None
ret = subprocess.check_output(["service", "call", *call], encoding='utf8').strip()
if 'Parcel' not in ret:
return 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):

Loading…
Cancel
Save