0% found this document useful (0 votes)
48 views

Layui Code

The document describes how to implement an ADB client protocol using sockets in code. It defines functions for connecting to the ADB server socket, sending commands to host and devices, and transmitting data like files between the client and devices. These functions allow operations like getting connected devices, executing shell commands, port forwarding, and pushing/pulling files.

Uploaded by

jet htc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Layui Code

The document describes how to implement an ADB client protocol using sockets in code. It defines functions for connecting to the ADB server socket, sending commands to host and devices, and transmitting data like files between the client and devices. These functions allow operations like getting connected devices, executing shell commands, port forwarding, and pushing/pulling files.

Uploaded by

jet htc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

socket 实现 adb 客户端协议

codelayui.code
1. import wsock.tcp.client;
2. class adb{
3. ctor(){
4. var string = ..string;
5. var encode = function(data){
6. var len = #data;
7. return string.format("%04X%s",len,data);
8. }
9. var int_encode = function(n){
10. var s = string.format("%08X",n);
11. s = string.unhex(s,"");
12. return string.reverse(s);
13. }
14. var int_decode = function(s){
15. s = string.reverse(s);
16. s = string.hex(s,"");
17. return tonumber(s,16);
18. }
19. var soketConnect = function(){
20. var client = ..wsock.tcp.client();
21. client.onClosed = function(){
22. ..console.log("套接字关闭!")
23. }
24. var re,err = client.connect("127.0.0.1",5037);
25. if(re)
26. return client;
27.
28. return null,err;
29. }
30. };
31. sendHostCommand = function(command) {
32. var client,err = soketConnect();
33. if(!client) return null,err;
34.
35. var data = encode(command)
36. var re,err = client.send(data);
37. if(!re)
38. return null,err;
39.
40. var ok = client.read(4);
41. if(ok == "OKAY" ){
42. var ret = "";
43. var len = client.read(4);
44. len = tonumber(len,16) : 0;
45. if(len > 0 ){
46. ret = client.read(len);
47. }
48. client.close();
49. return ret;
50. }
51. var err = client.readAll();
52. client.close();
53. ..console.debug("error in response to adb host command", command, err)
54. return null,err;
55. }
56.
57. devices = function(){
58. var ret,err = this.sendHostCommand("host:devices-l");
59. if(!ret) return null,err;
60. var devices = {};
61. for line in string.lines(ret){
62. var serialno,status = string.match(line,"^(\S+)\s+(<device>|<offline>|<unknown>)");
63. if(serialno){
64. var dev = {serialno=serialno;status=status};
65. var name = string.match(line,"model\:(\w+)");
66. dev.name = name ? string.replace(name,"_"," ") : serialno;
67. dev.properties = line;
68. ..table.push(devices,dev);
69. }
70. }
71. return devices;
72. }
73.
74. killServer = function(){
75. this.sendHostCommand("host:kill");
76. }
77.
78. sendClientCommand = function(serialno,command){
79. var client,err = soketConnect();
80. if(!client ) return null,err;
81.
82. var transport = "host:transport:" + serialno;
83. var re,err = client.send(encode(transport));
84. if(!re) return null,err;
85.
86. var ok = client.read(4);
87. if(ok == "OKAY"){
88. ok,err = client.send(encode(command));
89. if(ok){
90. ok = client.read(4);
91. ..console.log("clientCommand return:",command,ok)
92. if(ok == "OKAY")
93. return client;
94. }
95. }
96. client.close();
97. }
98.
99. shell = function(serialno,command){
100. var client,err = this.sendClientCommand(serialno,"shell:" ++ command);
101. if(!client ) return null,err;
102. var out = {};
103. for(str,readSize,remainSize in client.eachRead() ){
104. ..table.push(out,str);
105. if( remainSize ==0 )
106. break ;
107. }
108. client.close();
109. return ..string.join(out);
110. }
111.
112. forward = function(serialno,from,to){
113. var command = string.format("host-serial:%s:forward:%s;%s",serialno,from,to);
114. return this.sendHostCommand(command);
115. }
116.
117. push = function(serialno,remotePath,localPath){
118. var client,err = this.sendClientCommand(serialno,"sync:");
119. if(!client) return null,"打开同步模式失败:"++tostring(err);
120.
121. var path = remotePath ++ ",0644";
122. var re,err = client.write("SEND",int_encode(#path),path);
123. if(!re) return null,err;
124.
125. var file = ..io.open(localPath,"rb");
126. var size,errMsg = file.size();
127. while(size > 0 ){
128. var n = ..math.min(size,65536);
129. var buffer = ..raw.buffer(n);
130. var len,err = file.readBuffer(buffer,n);
131. if(!len){
132. errMsg = "读取本地文件失败:"++tostring(err);
133. break ;
134. }
135. size = size - len;
136.
137. var re,err = client.write("DATA",int_encode(len),buffer);
138. if(!re){
139. errMsg = "发送数据失败:"++tostring(err);
140. break ;
141. }
142. }
143. file.close();
144. if(size == 0){//发送完毕
145. client.write("DONE"++string.repeat(4));
146. var ok,err = client.read(4);
147. if(ok=="OKAY")
148. return remotePath;
149. errMsg = err;
150. }
151. return null,errMsg;
152. }
153.
154. pull = function(serialno,remotePath,localPath){
155. var client,err= this.sendClientCommand(serialno,"sync:");
156. if(!client) return null,"打开同步模式失败:"++tostring(err);
157.
158. var re,err = client.write("RECV",int_encode(#remotePath),remotePath);
159. if(!re) return null,err;
160. var file = ..io.open(localPath,"wb");
161. var eventId,errMsg = client.read(4);
162. while(eventId == "DATA"){
163. var len,err = client.read(4);
164. if(!len){
165. errMsg = err;
166. break ;
167. }
168.
169. var n = int_decode(len);
170. var buffer,err = client.read(n);
171. if(!buffer){
172. errMsg = err;
173. break ;
174. }
175. file.write(buffer);
176. eventId,errMsg = client.read(4);
177. }
178. file.close();
179. if( eventId == "DONE"){
180. client.close();
181. return localPath;
182. }
183. return null,errMsg;
184. }
185. }
186. /**intellisense()
187. adb() = !adb.
188. !adb.devices() = 获取当前设备
189. !adb.killServer() = 关闭 adb 服务
190. !adb.shell(.(设备序列号,命令) = 执行 shell 命令
191. !adb.forward(.(设备序列号,本地端口,远程端口) = tcp 端口映射
192. !adb.push(.(设备序列号,远程存储路径,本地文件路径) = 发送文件
193. !adb.pull(.(设备序列号,远程文件路径,本地存储路径) = 接收文件
194. end intellisense**/

You might also like