Talustus | 3019a91 | 2013-04-06 11:50:07 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import socket |
| 4 | import sys |
| 5 | import struct |
| 6 | import time |
| 7 | |
| 8 | # debug |
| 9 | VERBOSE = True |
| 10 | |
| 11 | def D(msg): |
| 12 | if VERBOSE: print(msg) |
| 13 | |
| 14 | # "struct fbinfo" is defined in $T/system/core/adb/framebuffer_service.c |
| 15 | def fbinfo_unpack(data): |
| 16 | keys = ("version", |
| 17 | "bpp", |
| 18 | "size", |
| 19 | "width", |
| 20 | "height", |
| 21 | "red_offset", |
| 22 | "red_length", |
| 23 | "blue_offset", |
| 24 | "blue_length", |
| 25 | "green_offset", |
| 26 | "green_length", |
| 27 | "alpha_offset", |
| 28 | "alpha_length" |
| 29 | ) |
| 30 | # the data is little-endian |
| 31 | values = struct.unpack("<IIIIIIIIIIIII",data) |
| 32 | |
| 33 | D("dump struct fbinfo") |
| 34 | i = 0 |
| 35 | for key in keys: |
| 36 | D("%14s: %-12d" % (key, values[i])) |
| 37 | i = i + 1 |
| 38 | |
| 39 | |
| 40 | |
| 41 | def save(): |
| 42 | f = open('dump', 'w') |
| 43 | while True: |
| 44 | data = s.recv(4096 * 16) |
| 45 | if data == "": |
| 46 | break |
| 47 | f.write(data) |
| 48 | f.close() |
| 49 | |
| 50 | |
| 51 | def communicate(cmd=None): |
| 52 | if cmd != None: |
| 53 | buf = "%04x%s" % (len(cmd), cmd) |
| 54 | D("<< " + buf) |
| 55 | s.send(buf) |
| 56 | data = s.recv(4096) |
| 57 | |
| 58 | D(">> [%s]" % len(data)) |
| 59 | D(data) |
| 60 | |
| 61 | if data[0:4] == 'FAIL': |
| 62 | return False |
| 63 | else: |
| 64 | return True |
| 65 | |
| 66 | |
| 67 | target = '' |
| 68 | # use getopt module in future |
| 69 | for arg in sys.argv: |
| 70 | if arg == '-q': |
| 71 | VERBOSE = False |
| 72 | if target != 'any': |
| 73 | # compatiable with "adb -d", redirect commands to usb |
| 74 | if arg == '-d': |
| 75 | target = 'usb' |
| 76 | # compatiable with "adb -e", redirect commands to emulator |
| 77 | elif arg == '-e': |
| 78 | target = 'local' |
| 79 | |
| 80 | if target == '': target ='any' |
| 81 | |
| 82 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 83 | |
| 84 | D("connecting") |
| 85 | try: |
| 86 | s.connect(("localhost", 5037)) |
| 87 | except socket.error: |
| 88 | print 'Cannot connect to localhost:5037' |
| 89 | print socket.error |
| 90 | sys.exit(0) |
| 91 | |
| 92 | D("connected") |
| 93 | |
| 94 | if not communicate("host:transport-%s" % target): |
| 95 | sys.exit(1) |
| 96 | #communicate("host:transport-usb:shell:ls /data") |
| 97 | communicate("framebuffer:") |
| 98 | |
| 99 | data = s.recv(52) |
| 100 | fbinfo_unpack(data) |
| 101 | |
| 102 | t0 = float(time.time()) |
| 103 | save() |
| 104 | t1 = float(time.time()) |
| 105 | print t1 - t0 |
| 106 | |