def push(self, target_path: str, buffer: 'ReadableBuffer', mode=0o100755, mtime: int = None): """push data to device""" # Python has no type hint for buffer protocol, why? self.service('sync:') request = b'%s,%d' % (target_path.encode(), mode) self.sock.send(b'SEND' + struct.pack("<I", len(request)) + request) sendbuf = np.empty(65536 + 8, dtype=np.uint8) sendbuf[0:4] = np.frombuffer(b'DATA', dtype=np.uint8) input_arr = np.frombuffer(buffer, dtype=np.uint8) for arr in np.array_split(input_arr, np.arange(65536, input_arr.size, 65536)): sendbuf[4:8].view('<I')[0] = len(arr) sendbuf[8:8 + len(arr)] = arr self.sock.sendall(sendbuf[0:8 + len(arr)]) if mtime is None: mtime = int(time.time()) self.sock.sendall(b'DONE' + struct.pack("<I", mtime)) response = recvexactly(self.sock, 8) self.sock.close() if response[:4] != b'OKAY': raise RuntimeError('push failed')
def screencap(self): t0 = time.perf_counter() if self.compress == 0: s = self.device_session_factory().exec_stream(self.command) header = recvexactly(s, 12) w, h, f = struct.unpack_from('III', header, 0) datalen = w * h * 4 data = recvall(s, datalen + 4, True) s.close() # data = zlib.decompress(data, zlib.MAX_WBITS | 16, 8388608) assert (f == 1) result = _screencap_to_image((w, h, data[:datalen + 4]), self.screenshot_rotate) else: s = self.device_session_factory().exec_stream(self.command + self.compress_suffix) data = recvall(s, 8388608, True) data = self.decompress(data) w, h, f = struct.unpack_from('III', data, 0) datalen = w * h * 4 s.close() # data = zlib.decompress(data, zlib.MAX_WBITS | 16, 8388608) assert (f == 1) result = _screencap_to_image((w, h, data[12:datalen + 12 + 4]), self.screenshot_rotate) t = time.perf_counter() - t0 logger.debug('screencap: %.3fms', t * 1000) return result
def check(self): future = self.rch.register_cookie() with future: control_sock = self.device_session_factory().exec_stream( '(echo -n %s; screencap) | nc %s %d' % (future.cookie.decode(), self.loopback, self.rch.port)) with control_sock, future.get() as conn: data = recvexactly(conn, 12) w, h, f = struct.unpack_from('III', data, 0) assert (f == 1) return w, h
def check(self): A_gz = self.device_session_factory().exec('echo A | gzip -1') if zlib.decompress(A_gz, zlib.MAX_WBITS | 16, 32) != b'A\n': raise RuntimeError( "gzip -1 in device cannot produce desired output") s = self.device_session_factory().exec_stream('screencap') data = recvexactly(s, 12) s.close() w, h, f = struct.unpack_from('III', data, 0) assert (f == 1) return w, h
def check(self): A_gz = self.device_session_factory().exec('echo A' + self.compress_suffix) if self.decompress(A_gz) != b'A\n': raise RuntimeError( "gzip -1 in device cannot produce desired output") s = self.device_session_factory().exec_stream(self.command) data = recvexactly(s, 12) s.close() w, h, f = struct.unpack_from('III', data, 0) assert (f == 1) return w, h
def _read_hexlen(sock): textlen = int(recvexactly(sock, 4), 16) if textlen == 0: return b'' buf = recvexactly(sock, textlen) return buf
def _check_okay(sock): result = recvexactly(sock, 4) if result != b'OKAY': raise RuntimeError(_read_hexlen(sock))