# Read adb response, if 'OKAY' turn true
def readAdbResponse(s):
if s != None:
resp = s.recv(4)
if DEBUG:
print 'resp: %s' % repr(resp)
if len(resp) != 4:
print 'protocol fault (no status)'
return False
if resp == 'OKAY':
return True
elif resp == 'FAIL':
resp = s.recv(4)
if len(resp) < 4:
print 'protocol fault (status len)'
return False
else:
length = int(resp, 16)
resp = s.recv(length)
if len(resp) != length:
print 'protocol fault (status read)'
return False
else:
print resp
return False
else:
print "protocol fault (status %02x %02x %02x %02x?!)", (resp[0], resp[1], resp[2], resp[3])
return False
return False
# Send adb shell command
def adbshellcommand(cmd):
reply = None
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# waiting adb server start
while True:
try:
s.connect(('127.0.0.1', 5037))
except:
os.system('adb start-server')
time.sleep(2)
continue
else:
break
req_msg = 'host:transport-any'
s.sendall('%04x' % len(req_msg))
s.sendall(req_msg)
if not readAdbResponse(s):
return None
req_msg = 'shell:%s' % cmd
if DEBUG:
print '%s' % req_msg
s.sendall('%04x' % len(req_msg))
s.sendall(req_msg)
if readAdbResponse(s):
reply = s.recv(4096)
if DEBUG:
hexdump(bytearray(reply))
s.close()
return reply
# Convert buffer to Int
def getInt(tbuf = None):
if (tbuf != None):
if DEBUG:
hexdump(bytearray(tbuf))
if len(tbuf) < 4:
print 'buff len < 4'
return 0
else:
if DEBUG:
print 'parse: %02x %02x %02x %02x' % (tbuf[0],tbuf[1],tbuf[2],tbuf[3])
intnum = tbuf[0]
intnum = intnum + tbuf[1]*0x100
intnum = intnum + tbuf[2]*0x10000
intnum = intnum + tbuf[3]*0x1000000
if DEBUG:
print 'INT: %08x' % intnum
return intnum
else:
return 0
# Parse fb header from buffer
def readHeader(tfb, ver, buf):
if DEBUG:
print 'readHeader: ver = %d' % ver
if ver == 16:
tfb.fb_bpp = 16
tfb.fb_size = getInt(buf[0:4])
tfb.fb_width = getInt(buf[4:8])
tfb.fb_height = getInt(buf[8:12])
tfb.red_offset = 11
tfb.red_length = 5
tfb.blue_offset = 5
tfb.blue_length = 6
tfb.green_offset = 0
tfb.green_length = 5
tfb.alpha_offset = 0
tfb.alpha_length = 0
elif ver == 1:
tfb.fb_bpp = getInt(bytearray(buf[0:4]))
tfb.fb_size = getInt(bytearray(buf[4:8]))
tfb.fb_width = getInt(bytearray(buf[8:12]))
tfb.fb_height = getInt(bytearray(buf[12:16]))
tfb.red_offset = getInt(bytearray(buf[16:20]))
tfb.red_length = getInt(bytearray(buf[20:24]))
tfb.blue_offset = getInt(bytearray(buf[24:28]))
tfb.blue_length = getInt(bytearray(buf[28:32]))
tfb.green_offset = getInt(bytearray(buf[32:36]))
tfb.green_length = getInt(bytearray(buf[36:40]))
tfb.alpha_offset = getInt(bytearray(buf[40:44]))
tfb.alpha_length = getInt(bytearray(buf[44:48]))
else:
return False
return True