forked from bitbegin/RedAdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb-windows.reds
523 lines (471 loc) · 13.8 KB
/
adb-windows.reds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
Red/System []
#define handle! int-ptr!
#define FORMAT_MESSAGE_FROM_SYSTEM 00001000h
#define FORMAT_MESSAGE_IGNORE_INSERTS 00000200h
#define DIGCF_DEFAULT 00000001h
#define DIGCF_PRESENT 00000002h
#define DIGCF_ALLCLASSES 00000004h
#define DIGCF_PROFILE 00000008h
#define DIGCF_DEVICEINTERFACE 00000010h
#define GENERIC_READ 80000000h
#define GENERIC_WRITE 40000000h
#define GENERIC_EXECUTE 20000000h
#define GENERIC_ALL 10000000h
#define FILE_SHARE_READ 00000001h
#define FILE_SHARE_WRITE 00000002h
#define FILE_FLAG_OVERLAPPED 40000000h
#define PIPE_TRANSFER_TIMEOUT 03h
#define USB_ENDPOINT_DIRECTION_MASK 80h
#define CREATE_NEW 1
#define CREATE_ALWAYS 2
#define OPEN_EXISTING 3
pipe-info-struct: alias struct! [
pipeType [integer!]
pipeID [byte!]
maxPackSize [integer!]
;interval [byte!]
]
dev-info-data: alias struct! [
cbSize [integer!]
ClassGuid [integer!]
pad1 [integer!]
pad2 [integer!]
pad3 [integer!]
DevInst [integer!]
reserved [integer!]
]
dev-interface-data: alias struct! [
cbSize [integer!]
ClassGuid [integer!]
pad1 [integer!]
pad2 [integer!]
pad3 [integer!]
Flags [integer!]
reserved [integer!]
]
dev-interface-detail: alias struct! [
cbSize [integer!]
DevicePath [c-string!]
]
overlapped-struct: alias struct! [
Internal [integer!]
InternalHigh [integer!]
Offset [integer!]
OffsetHight [integer!]
hEvent [integer!]
]
guid-struct: alias struct! [
data1 [integer!]
data2 [integer!]
data3 [integer!]
data4 [integer!]
]
SECURITY_ATTRIBUTES: declare struct! [
nLength [integer!]
lpSecurityDescriptor [integer!]
bInheritHandle [integer!]
]
#import [
"kernel32.dll" stdcall [
CreateEvent: "CreateEventA" [
lpEventAttributes [integer!]
bManualReset [integer!]
bInitialState [integer!]
lpName [integer!]
return: [integer!]
]
CloseHandle: "CloseHandle" [
hObject [integer!]
return: [integer!]
]
CreateFile: "CreateFileA" [
lpFileName [c-string!]
dwDesiredAccess [integer!]
dwShareMode [integer!]
lpSecurityAttributes [integer!]
dwCreationDisposition [integer!]
dwFlagsAndAttributes [integer!]
hTemplateFile [integer!]
return: [integer!]
]
GetLastError: "GetLastError" [
return: [integer!]
]
FormatMessage: "FormatMessageW" [
dwFlags [integer!]
lpSource [integer!]
dwMessageId [integer!]
dwLanguageId [integer!]
lpBuffer [c-string!]
nSize [integer!]
Arguments [integer!]
return: [integer!]
]
]
"setupapi.dll" stdcall [
SetupDiGetClassDevs: "SetupDiGetClassDevsA" [
ClassGuid [guid-struct]
Enumerator [integer!]
hwndParent [integer!]
Flags [integer!]
return: [integer!]
]
SetupDiDestroyDeviceInfoList: "SetupDiDestroyDeviceInfoList" [
handle [integer!]
return: [logic!]
]
SetupDiEnumDeviceInterfaces: "SetupDiEnumDeviceInterfaces" [
DeviceInfoSet [integer!]
DeviceInfoData [integer!]
InterfaceClassGuid [guid-struct]
MemberIndex [integer!]
DeviceInterfaceData [dev-interface-data]
return: [logic!]
]
SetupDiGetDeviceInterfaceDetail: "SetupDiGetDeviceInterfaceDetailA" [
DeviceInfoSet [integer!]
DeviceInterfaceData [dev-interface-data]
DeviceInterfaceDetailData [c-string!]
DeviceInterfaceDetailDataSize [integer!]
RequiredSize [int-ptr!]
DeviceInfoData [integer!]
return: [logic!]
]
]
"winusb.dll" stdcall [
WinUsb_Initialize: "WinUsb_Initialize" [
DeviceHandle [integer!]
InterfaceHandle [handle!]
return: [logic!]
]
WinUsb_Free: "WinUsb_Free" [
InterfaceHandle [integer!]
return: [logic!]
]
WinUsb_QueryPipe: "WinUsb_QueryPipe" [
InterfaceHandle [integer!]
AlternateInterfaceNumber [byte!]
PipeIndex [byte!]
PipeInformation [pipe-info-struct]
return: [logic!]
]
WinUsb_GetCurrentAlternateSetting: "WinUsb_GetCurrentAlternateSetting" [
DeviceHandle [integer!]
AltSetting [int-ptr!]
return: [logic!]
]
WinUsb_WritePipe: "WinUsb_WritePipe" [
handle [integer!]
pipeID [byte!]
buffer [c-string!]
buf-len [integer!]
trans-len [int-ptr!]
overlapped [overlapped-struct]
return: [logic!]
]
WinUsb_ReadPipe: "WinUsb_ReadPipe" [
handle [integer!]
pipeID [byte!]
buffer [c-string!]
buf-len [integer!]
trans-len [int-ptr!]
overlapped [overlapped-struct]
return: [logic!]
]
WinUsb_GetOverlappedResult: "WinUsb_GetOverlappedResult" [
handle [integer!]
overlapped [overlapped-struct]
trans-len [int-ptr!]
wait? [byte!]
return: [logic!]
]
WinUsb_SetPipePolicy: "WinUsb_SetPipePolicy" [
handle [integer!]
pipeID [byte!]
policy [integer!]
value-len [integer!]
value [int-ptr!]
return: [logic!]
]
]
]
;high level interface
#define UsbdPipeTypeControl 0
#define UsbdPipeTypeIsochronous 1
#define UsbdPipeTypeBulk 2
#define UsbdPipeTypeInterrupt 3
#define ERROR_NO_MORE_ITEMS 259
last-error: 0
last-adb-err: 0
#enum adb-err![
ADB-GET-DEV-INFO-SET: 1
ADB-NO-DEVS: 2
ADB-OPEN-FAILED: 3
ADB-INIT-FAILED: 4
ADB-BUFF-OVER: 5
ADB-WRITE-PIPO: 6
]
GUID: declare guid-struct
ANDROID_USB_CLASS_ID: declare guid-struct
ANDROID_USB_CLASS_ID: as guid-struct [
F72FE0D4h
407DCBCBh
D69E1488h
6BDDD073h
]
fmt-msg-flags: FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS
#define MAX-ADBS-ALLOWED 10
adbs: 0
adb-info-struct: alias struct! [
device-set [integer!]
device [integer!]
interface [integer!]
read-id [byte!]
write-id [byte!]
local-id [integer!]
remote-id [integer!]
zero-mask [integer!]
]
adb-array: allocate MAX-ADBS-ALLOWED * size? adb-info-struct
adb-ptr: as adb-info-struct adb-array
get-adb-handle: func [
iadb [integer!]
return: [adb-info-struct]
][
adb-ptr + iadb
]
get-local-id*: func [
iadb [integer!]
return: [integer!]
/local
adb [adb-info-struct]
][
adb: adb-ptr + iadb
adb/local-id
]
get-remote-id*: func [
iadb [integer!]
return: [integer!]
/local
adb [adb-info-struct]
][
adb: adb-ptr + iadb
adb/remote-id
]
set-local-id*: func [
iadb [integer!]
local-id [integer!]
/local
adb [adb-info-struct]
][
adb: adb-ptr + iadb
adb/local-id: local-id
]
set-remote-id*: func [
iadb [integer!]
remote-id [integer!]
/local
adb [adb-info-struct]
][
adb: adb-ptr + iadb
adb/remote-id: remote-id
]
out: make-c-string 256 ;to save memory
get-error-msg*: func [return: [c-string!]][
set-memory as byte-ptr! out as byte! 0 256
FormatMessage fmt-msg-flags 0 last-error 0 out 256 0
out
]
parse-adb-result*: func [return: [c-string!]][
set-memory as byte-ptr! out as byte! 0 256
switch last-adb-err [
0 [copy-memory as byte-ptr! out as byte-ptr! "**ADB**: Init Success!" 100]
ADB-GET-DEV-INFO-SET [copy-memory as byte-ptr! out as byte-ptr! "**ADB**: Error: Can not get device information set" 100]
ADB-NO-DEVS [copy-memory as byte-ptr! out as byte-ptr! "**ADB**: Error: No android devices" 100]
ADB-OPEN-FAILED [copy-memory as byte-ptr! out as byte-ptr! "**ADB**: Error: Can not open android device" 100]
ADB-INIT-FAILED [copy-memory as byte-ptr! out as byte-ptr! "**ADB**: Error: Can not initialize interface" 100]
ADB-BUFF-OVER [copy-memory as byte-ptr! out as byte-ptr! "**ADB**: Error: Buffer overflow" 100]
ADB-WRITE-PIPO [copy-memory as byte-ptr! out as byte-ptr! "**ADB**: Error: Write data failed" 100]
]
out
]
set-timeout: func [adb [adb-info-struct] seconds [integer!] /local time [int-ptr!] sec [integer!]][
sec: seconds * 1000
time: :sec
WinUsb_SetPipePolicy adb/interface adb/read-id PIPE_TRANSFER_TIMEOUT 4 time
WinUsb_SetPipePolicy adb/interface adb/write-id PIPE_TRANSFER_TIMEOUT 4 time
]
#define ADB-NAME-BUFFER-RAW-SIZE 510
#define ADB-NAME-BUFFER-SIZE 500
buffer: make-c-string ADB-NAME-BUFFER-RAW-SIZE ;to save memory
interface-name: make-c-string ADB-NAME-BUFFER-SIZE ;to save memory
init-adb*: func [return: [integer!]
/local
dev-info [integer!]
interface-data [dev-interface-data]
size-value [integer!]
required-size [int-ptr!]
dev-handle [integer!]
if-handle-value [integer!]
interface-handle [int-ptr!]
if-number-value [integer!]
interface-number [int-ptr!]
pipe-read [byte!]
pipe-write [byte!]
index [integer!]
if-num [integer!]
pipe-info [pipe-info-struct]
max-packet-size [integer!]
byte-p [pointer! [byte!]]
adb [adb-info-struct]
][
dev-info: SetupDiGetClassDevs ANDROID_USB_CLASS_ID 0 0 DIGCF_DEVICEINTERFACE or DIGCF_PRESENT
if -1 = dev-info [
last-error: GetLastError
last-adb-err: ADB-GET-DEV-INFO-SET
return last-adb-err
]
if-num: 0
interface-data: declare dev-interface-data
size-value: 0
required-size: :size-value
if-handle-value: 0
interface-handle: :if-handle-value
if-number-value: 0
interface-number: :if-number-value
pipe-info: declare pipe-info-struct
until [
interface-data/cbSize: size? dev-interface-data
if false = SetupDiEnumDeviceInterfaces dev-info 0 ANDROID_USB_CLASS_ID if-num interface-data [
last-error: GetLastError
if ERROR_NO_MORE_ITEMS = last-error [
adbs: if-num
last-adb-err: 0
return last-adb-err
]
SetupDiDestroyDeviceInfoList dev-info
last-adb-err: ADB-NO-DEVS
return last-adb-err
]
set-memory as byte-ptr! buffer as byte! 0 ADB-NAME-BUFFER-RAW-SIZE
buffer/1: as byte! 5 ;-- sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA)
SetupDiGetDeviceInterfaceDetail dev-info interface-data as c-string! 0 0 required-size 0
if size-value > ADB-NAME-BUFFER-SIZE [
last-error: GetLastError
last-adb-err: ADB-BUFF-OVER
return last-adb-err
]
SetupDiGetDeviceInterfaceDetail dev-info interface-data buffer ADB-NAME-BUFFER-RAW-SIZE required-size 0
copy-memory as byte-ptr! interface-name as byte-ptr! (buffer + 4) ADB-NAME-BUFFER-SIZE ;length? as c-string! (buffer + 4)
dev-handle: CreateFile interface-name
GENERIC_READ or GENERIC_WRITE
FILE_SHARE_READ or FILE_SHARE_WRITE
0 OPEN_EXISTING
FILE_FLAG_OVERLAPPED 0
if -1 = dev-handle [
last-error: GetLastError
SetupDiDestroyDeviceInfoList dev-info
last-adb-err: ADB-OPEN-FAILED
return last-adb-err
]
;;-- get interface handle
if false = WinUsb_Initialize dev-handle interface-handle [
last-error: GetLastError
CloseHandle dev-handle
SetupDiDestroyDeviceInfoList dev-info
last-adb-err: ADB-INIT-FAILED
return last-adb-err
]
;;-- get write pipe id & read pipe id
WinUsb_GetCurrentAlternateSetting interface-handle/value interface-number
index: 0
while [index < 3][
if false = WinUsb_QueryPipe
interface-handle/value
as byte! interface-number/value
as byte! index
pipe-info [break]
if pipe-info/pipeType = UsbdPipeTypeBulk [
either (as byte! 0) = (pipe-info/pipeID and as byte! USB_ENDPOINT_DIRECTION_MASK) [
pipe-write: pipe-info/pipeID
byte-p: declare pointer! [byte!]
byte-p: as pointer! [byte!] pipe-info
max-packet-size: ((as integer! byte-p/6) << 8) + (as integer! byte-p/7)
][
pipe-read: pipe-info/pipeID
]
]
index: index + 1
]
adb: get-adb-handle if-num
adb/device-set: dev-info
adb/device: dev-handle
adb/interface: interface-handle/value
adb/read-id: pipe-read
adb/write-id: pipe-write
adb/zero-mask: max-packet-size - 1
set-timeout adb 1
if-num: if-num + 1
if-num >= MAX-ADBS-ALLOWED
]
adbs: if-num
last-adb-err: 0
return last-adb-err
]
close-adb*: func [iadb [integer!]
/local
adb [adb-info-struct]
][
adb: get-adb-handle iadb
if adb/interface = 0 [WinUsb_Free adb/interface]
if adb/device = 0 [CloseHandle adb/device]
if adb/device-set = 0 [SetupDiDestroyDeviceInfoList adb/device-set]
]
pipe*: func [
iadb [integer!]
data [red-binary!]
write [logic!]
return: [integer!]
/local
adb [adb-info-struct]
s [series!]
ovlap [overlapped-struct]
interface [integer!]
transferred [int-ptr!]
len [integer!]
num [integer!]
p [c-string!]
][
adb: get-adb-handle iadb
s: GET_BUFFER(data)
p: as c-string! s/offset
len: as integer! (s/tail - s/offset)
interface: adb/interface
ovlap: declare overlapped-struct
ovlap/hEvent: CreateEvent 0 1 0 0
num: 0
transferred: :num
either write [
WinUsb_WritePipe interface adb/write-id p len transferred ovlap
if all [
positive? adb/zero-mask
zero? (adb/zero-mask and len)
][
WinUsb_WritePipe interface adb/write-id "" 0 transferred ovlap
]
][
WinUsb_ReadPipe interface adb/read-id p len transferred ovlap
]
WinUsb_GetOverlappedResult interface ovlap transferred as byte! 1
if write [
if transferred/1 <> len [
unless zero? ovlap/hEvent [CloseHandle ovlap/hEvent]
close-adb* iadb
last-adb-err: ADB-WRITE-PIPO
return last-adb-err
]
]
unless zero? ovlap/hEvent [CloseHandle ovlap/hEvent]
last-adb-err: 0
return last-adb-err
]