-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlusb.c
2384 lines (2266 loc) · 62.8 KB
/
lusb.c
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <poll.h>
#include <libusb.h>
#include "lua.h"
#include "lauxlib.h"
#if LUA_VERSION_NUM<502
typedef unsigned int lua_Unsigned;
#define lua_rawlen(L,i) lua_objlen(L,(i))
#define luaL_len(L,i) lua_objlen(L,(i))
#define lua_tounsigned(L,i) (lua_Unsigned)lua_tointeger(L,(i))
#define luaL_checkunsigned(L,n) (lua_Unsigned)luaL_checkinteger(L,(n))
#define luaL_optunsigned(L,n,d) (lua_Unsigned)luaL_optinteger(L,(n),(d))
#define luaL_setfuncs(L,l,n) luaI_openlib(L,NULL,(l),(n))
#define luaL_newlibtable(L,l) lua_createtable(L,0,sizeof(l)/sizeof((l)[0])-1)
#define luaL_newlib(L,l) (luaL_newlibtable(L,l),luaL_setfuncs(L,l,0))
static int lua_absindex(lua_State *L, int index)
{
return (LUA_REGISTRYINDEX <= index && index < 0)
? lua_gettop(L) + index + 1
: index;
}
#endif
static lua_Unsigned l_checkunsigned(lua_State *L, int narg, int nval)
{
#if LUA_VERSION_NUM>=502
int isnum;
lua_Unsigned d = lua_tounsignedx(L, nval, &isnum);
if (!isnum)
{
const char *msg = lua_pushfstring(L, "%s expected, got %s",
lua_typename(L, LUA_TNUMBER),
luaL_typename(L, nval));
luaL_argerror(L, narg, msg);
}
return d;
#else
lua_Unsigned d;
if (!lua_isnumber(L, nval))
{
const char *msg = lua_pushfstring(L, "%s expected, got %s",
lua_typename(L, LUA_TNUMBER),
luaL_typename(L, nval));
luaL_argerror(L, narg, msg);
}
return lua_tointeger(L, nval);
#endif
}
#if LUA_VERSION_NUM < 502
char* luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
{
return (char*)lua_newuserdata(B->L, sz);
}
void luaL_addbuffsize(luaL_Buffer *B, size_t sz)
{
if (sz > 0)
{
char *str = (char*)lua_touserdata(B->L, -1);
lua_pushlstring(B->L, str, sz);
lua_remove(B->L, -2);
luaL_addvalue(B);
}
else
lua_pop(B->L, 1);
}
#else
#define luaL_addbuffsize(B,s) luaL_addsize(B,(s))
#endif
/* not included in libusb */
struct usb_descriptor
{
uint8_t bLength;
uint8_t bDescriptorType;
};
struct usb_string_descriptor
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bString[127];
};
struct usb_hub_descriptor
{
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bNumberOfPorts;
uint16_t wHubCharacteristics;
uint8_t bPowerOnToPowerGood;
uint8_t bHubControlCurrent;
uint8_t bRemoveAndPowerMask[64];
};
struct usb_hid_descriptor_list
{
uint8_t bReportType;
uint16_t wReportLength;
} ;
struct usb_hid_descriptor
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
struct usb_hid_descriptor_list DescriptorList[1];
};
#define CONTEXT_MT "libusb1_context"
#define DEVICE_MT "libusb1_device"
#define HANDLE_MT "libusb1_device_handle"
#define TRANSFER_MT "libusb1_transfer"
#define DEFAULT_CTX "libusb1 default context"
#define DEVICES_REG "libusb1 devices"
#define DEVPTR_REG "libusb1 pointers"
#define HANDLES_REG "libusb1 handles"
#define TRANSFER_REG "libusb1 active transfers"
#define BUFFER_REG "libusb1 transfer buffers"
#define POLLFD_REG "libusb1 pollfds"
/* NULL is a valid context */
#define INVALID_CONTEXT ((libusb_context*)1)
#define INVALID_DEVICE ((libusb_device*)0)
#define INVALID_HANDLE ((libusb_device_handle*)0)
#define INVALID_TRANSFER ((struct libusb_transfer*)0)
static int _err(lua_State *L, int err)
{
if (err == LIBUSB_SUCCESS)
{
lua_pushboolean(L, 1);
return 1;
}
lua_pushnil(L);
switch (err)
{
case LIBUSB_ERROR_IO:
lua_pushliteral(L, "input/output error"); break;
case LIBUSB_ERROR_INVALID_PARAM:
lua_pushliteral(L, "invalid parameter"); break;
case LIBUSB_ERROR_ACCESS:
lua_pushliteral(L, "access denied (insufficient permissions)"); break;
case LIBUSB_ERROR_NO_DEVICE:
lua_pushliteral(L, "no such device (it may have been disconnected)"); break;
case LIBUSB_ERROR_NOT_FOUND:
lua_pushliteral(L, "entity not found"); break;
case LIBUSB_ERROR_BUSY:
lua_pushliteral(L, "resource busy"); break;
case LIBUSB_ERROR_TIMEOUT:
lua_pushliteral(L, "operation timed out"); break;
case LIBUSB_ERROR_OVERFLOW:
lua_pushliteral(L, "overflow"); break;
case LIBUSB_ERROR_PIPE:
lua_pushliteral(L, "pipe error"); break;
case LIBUSB_ERROR_INTERRUPTED:
lua_pushliteral(L, "system call interrupted (perhaps due to signal)"); break;
case LIBUSB_ERROR_NO_MEM:
lua_pushliteral(L, "insufficient memory"); break;
case LIBUSB_ERROR_NOT_SUPPORTED:
lua_pushliteral(L, "operation not supported or unimplemented on this platform"); break;
case LIBUSB_ERROR_OTHER:
lua_pushliteral(L, "other error"); break;
default:
lua_pushfstring(L, "unknown error (0x%x)", err); break;
}
lua_pushinteger(L, err);
return 3;
}
static libusb_context** newctx(lua_State *L)
{
libusb_context **ctx;
ctx = (libusb_context**)lua_newuserdata(L, sizeof(libusb_context*));
*ctx = INVALID_CONTEXT;
luaL_getmetatable(L, CONTEXT_MT);
lua_setmetatable(L, -2);
return ctx;
}
static void ctxptr(lua_State *L, int ctx, libusb_context *ptr)
{
/* Uses the same table for device pointers. */
/* Should be safe since they should never overlap. */
lua_pushvalue(L, ctx);
lua_pushlightuserdata(L, ptr);
lua_getfield(L, LUA_REGISTRYINDEX, DEVPTR_REG);
lua_insert(L, -3);
lua_rawset(L, -3);
lua_pop(L, 1);
}
static libusb_context* defctx(lua_State *L)
{
libusb_context **ctx;
int err;
lua_getfield(L, LUA_REGISTRYINDEX, DEFAULT_CTX);
if (lua_isnil(L, -1))
{
lua_pop(L, 1);
ctx = newctx(L);
*ctx = NULL;
if ((err = libusb_init(NULL)) != 0)
{
luaL_where(L, 1);
_err(L, err);
lua_pop(L, 1);
lua_concat(L, 2);
lua_error(L);
return NULL;
}
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, DEFAULT_CTX);
}
else
ctx = (libusb_context**)lua_touserdata(L, -1);
return *ctx;
}
static void devptr(lua_State *L, int dev, libusb_device *ptr)
{
lua_pushvalue(L, dev);
lua_pushlightuserdata(L, ptr);
lua_getfield(L, LUA_REGISTRYINDEX, DEVPTR_REG);
lua_insert(L, -3);
lua_rawset(L, -3);
lua_pop(L, 1);
}
static libusb_device** newdev(lua_State *L, int object, libusb_device *dev)
{
libusb_device **udev;
object = lua_absindex(L, object);
if (dev != INVALID_DEVICE)
{
lua_getfield(L, LUA_REGISTRYINDEX, DEVPTR_REG);
lua_pushlightuserdata(L, dev);
lua_rawget(L, -2);
if (!lua_isnil(L, -1))
{
/* use existing object for this device */
lua_remove(L, -2);
udev = (libusb_device**)lua_touserdata(L, -1);
return udev;
}
lua_pop(L, 2);
}
udev = (libusb_device**)lua_newuserdata(L, sizeof(libusb_device*));
*udev = dev;
luaL_getmetatable(L, DEVICE_MT);
lua_setmetatable(L, -2);
if (dev != INVALID_DEVICE)
{
libusb_ref_device(dev);
devptr(L, -1, dev);
}
/* associate device with context */
lua_pushvalue(L, -1);
lua_getmetatable(L, object);
luaL_getmetatable(L, HANDLE_MT);
if (lua_rawequal(L, -1, -2))
{
/* find context from handle */
lua_pop(L, 2);
lua_getfield(L, LUA_REGISTRYINDEX, HANDLES_REG);
lua_pushvalue(L, object);
lua_rawget(L, -2);
}
else
{
lua_pop(L, 2);
lua_pushvalue(L, object);
}
lua_getmetatable(L, -1);
luaL_getmetatable(L, CONTEXT_MT);
if (lua_rawequal(L, -1, -2))
{
lua_pop(L, 2);
lua_getfield(L, LUA_REGISTRYINDEX, DEVICES_REG);
lua_insert(L, -3);
lua_rawset(L, -3);
lua_pop(L, 1);
}
else
{
/* not a context object, ignore it */
lua_pop(L, 4);
}
return udev;
}
static libusb_device_handle** newhandle(lua_State *L, int object)
{
libusb_device_handle **handle;
object = lua_absindex(L, object);
handle = (libusb_device_handle**)lua_newuserdata(L, sizeof(libusb_device_handle*));
*handle = INVALID_HANDLE;
luaL_getmetatable(L, HANDLE_MT);
lua_setmetatable(L, -2);
/* associate handle with context */
lua_pushvalue(L, -1);
lua_getmetatable(L, object);
luaL_getmetatable(L, DEVICE_MT);
if (lua_rawequal(L, -1, -2))
{
/* find context from device */
lua_pop(L, 2);
lua_getfield(L, LUA_REGISTRYINDEX, DEVICES_REG);
lua_pushvalue(L, object);
lua_rawget(L, -2);
}
else
{
lua_pop(L, 2);
lua_pushvalue(L, object);
}
lua_getmetatable(L, -1);
luaL_getmetatable(L, CONTEXT_MT);
if (lua_rawequal(L, -1, -2))
{
lua_pop(L, 2);
lua_getfield(L, LUA_REGISTRYINDEX, HANDLES_REG);
lua_insert(L, -3);
lua_rawset(L, -3);
lua_pop(L, 1);
}
else
{
/* not a context object, ignore it */
lua_pop(L, 4);
}
return handle;
}
static int exitctx(lua_State *L)
{
libusb_context **ud;
ud = (libusb_context**)luaL_checkudata(L, 1, CONTEXT_MT);
if (*ud != INVALID_CONTEXT)
{
libusb_exit(*ud);
*ud = INVALID_CONTEXT;
}
return 0;
}
static int unrefdev(lua_State *L)
{
libusb_device **ud;
ud = (libusb_device**)luaL_checkudata(L, 1, DEVICE_MT);
if (*ud != INVALID_DEVICE)
{
libusb_unref_device(*ud);
*ud = INVALID_DEVICE;
}
return 0;
}
static int closehandle(lua_State *L)
{
libusb_device_handle **ud;
ud = (libusb_device_handle**)luaL_checkudata(L, 1, HANDLE_MT);
if (*ud != INVALID_HANDLE)
{
libusb_close(*ud);
*ud = INVALID_HANDLE;
}
return 0;
}
static int freetransfer(lua_State *L)
{
struct libusb_transfer **ud;
ud = (struct libusb_transfer**)luaL_checkudata(L, 1, TRANSFER_MT);
if (*ud != INVALID_TRANSFER)
{
libusb_free_transfer(*ud);
*ud = INVALID_TRANSFER;
}
return 0;
}
static int pushdevicedesc(lua_State *L, const struct libusb_device_descriptor *desc)
{
lua_createtable(L, 0, 14);
lua_pushliteral(L, "bLength");
lua_pushinteger(L, desc->bLength);
lua_rawset(L, -3);
lua_pushliteral(L, "bDescriptorType");
lua_pushinteger(L, desc->bDescriptorType);
lua_rawset(L, -3);
lua_pushliteral(L, "bcdUSB");
lua_pushinteger(L, desc->bcdUSB);
lua_rawset(L, -3);
lua_pushliteral(L, "bDeviceClass");
lua_pushinteger(L, desc->bDeviceClass);
lua_rawset(L, -3);
lua_pushliteral(L, "bDeviceSubClass");
lua_pushinteger(L, desc->bDeviceSubClass);
lua_rawset(L, -3);
lua_pushliteral(L, "bDeviceProtocol");
lua_pushinteger(L, desc->bDeviceProtocol);
lua_rawset(L, -3);
lua_pushliteral(L, "bMaxPacketSize0");
lua_pushinteger(L, desc->bMaxPacketSize0);
lua_rawset(L, -3);
lua_pushliteral(L, "idVendor");
lua_pushinteger(L, desc->idVendor);
lua_rawset(L, -3);
lua_pushliteral(L, "idProduct");
lua_pushinteger(L, desc->idProduct);
lua_rawset(L, -3);
lua_pushliteral(L, "bcdDevice");
lua_pushinteger(L, desc->bcdDevice);
lua_rawset(L, -3);
lua_pushliteral(L, "iManufacturer");
lua_pushinteger(L, desc->iManufacturer);
lua_rawset(L, -3);
lua_pushliteral(L, "iProduct");
lua_pushinteger(L, desc->iProduct);
lua_rawset(L, -3);
lua_pushliteral(L, "iSerialNumber");
lua_pushinteger(L, desc->iSerialNumber);
lua_rawset(L, -3);
lua_pushliteral(L, "bNumConfigurations");
lua_pushinteger(L, desc->bNumConfigurations);
lua_rawset(L, -3);
return 1;
}
static int pushendpointdesc(lua_State *L, const struct libusb_endpoint_descriptor *desc)
{
lua_createtable(L, 0, 8);
lua_pushliteral(L, "bLength");
lua_pushinteger(L, desc->bLength);
lua_rawset(L, -3);
lua_pushliteral(L, "bDescriptorType");
lua_pushinteger(L, desc->bDescriptorType);
lua_rawset(L, -3);
lua_pushliteral(L, "bEndpointAddress");
lua_pushinteger(L, desc->bEndpointAddress);
lua_rawset(L, -3);
lua_pushliteral(L, "bmAttributes");
lua_pushinteger(L, desc->bmAttributes);
lua_rawset(L, -3);
lua_pushliteral(L, "wMaxPacketSize");
lua_pushinteger(L, desc->wMaxPacketSize);
lua_rawset(L, -3);
lua_pushliteral(L, "bInterval");
lua_pushinteger(L, desc->bInterval);
lua_rawset(L, -3);
if (desc->bLength >= LIBUSB_DT_ENDPOINT_AUDIO_SIZE)
{
lua_pushliteral(L, "bRefresh");
lua_pushinteger(L, desc->bRefresh);
lua_rawset(L, -3);
lua_pushliteral(L, "bSynchAddress");
lua_pushinteger(L, desc->bSynchAddress);
lua_rawset(L, -3);
}
return 1;
}
static int pushinterfacedesc(lua_State *L, const struct libusb_interface_descriptor *desc, int includeendpoints)
{
lua_createtable(L, 0, 9+includeendpoints);
lua_pushliteral(L, "bLength");
lua_pushinteger(L, desc->bLength);
lua_rawset(L, -3);
lua_pushliteral(L, "bDescriptorType");
lua_pushinteger(L, desc->bDescriptorType);
lua_rawset(L, -3);
lua_pushliteral(L, "bInterfaceNumber");
lua_pushinteger(L, desc->bInterfaceNumber);
lua_rawset(L, -3);
lua_pushliteral(L, "bAlternateSetting");
lua_pushinteger(L, desc->bAlternateSetting);
lua_rawset(L, -3);
lua_pushliteral(L, "bNumEndpoints");
lua_pushinteger(L, desc->bNumEndpoints);
lua_rawset(L, -3);
lua_pushliteral(L, "bInterfaceClass");
lua_pushinteger(L, desc->bInterfaceClass);
lua_rawset(L, -3);
lua_pushliteral(L, "bInterfaceSubClass");
lua_pushinteger(L, desc->bInterfaceSubClass);
lua_rawset(L, -3);
lua_pushliteral(L, "bInterfaceProtocol");
lua_pushinteger(L, desc->bInterfaceProtocol);
lua_rawset(L, -3);
lua_pushliteral(L, "iInterface");
lua_pushinteger(L, desc->iInterface);
lua_rawset(L, -3);
if (includeendpoints)
{
unsigned int i;
lua_pushliteral(L, "endpoint");
lua_createtable(L, desc->bNumEndpoints, 0);
for (i = 0; i < desc->bNumEndpoints; ++i)
{
pushendpointdesc(L, &desc->endpoint[i]);
lua_rawseti(L, -2, i+1);
}
lua_rawset(L, -3);
}
return 1;
}
static int pushconfigdesc(lua_State *L, const struct libusb_config_descriptor *desc)
{
unsigned int i,j;
lua_createtable(L, 0, 9);
lua_pushliteral(L, "bLength");
lua_pushinteger(L, desc->bLength);
lua_rawset(L, -3);
lua_pushliteral(L, "bDescriptorType");
lua_pushinteger(L, desc->bDescriptorType);
lua_rawset(L, -3);
lua_pushliteral(L, "wTotalLength");
lua_pushinteger(L, desc->wTotalLength);
lua_rawset(L, -3);
lua_pushliteral(L, "bNumInterfaces");
lua_pushinteger(L, desc->bNumInterfaces);
lua_rawset(L, -3);
lua_pushliteral(L, "bConfigurationValue");
lua_pushinteger(L, desc->bConfigurationValue);
lua_rawset(L, -3);
lua_pushliteral(L, "iConfiguration");
lua_pushinteger(L, desc->iConfiguration);
lua_rawset(L, -3);
lua_pushliteral(L, "bmAttributes");
lua_pushinteger(L, desc->bmAttributes);
lua_rawset(L, -3);
lua_pushliteral(L, "MaxPower");
lua_pushinteger(L, desc->MaxPower);
lua_rawset(L, -3);
lua_pushliteral(L, "interface");
lua_createtable(L, desc->bNumInterfaces, 0);
for (i = 0; i < desc->bNumInterfaces; ++i)
{
lua_createtable(L, desc->interface[i].num_altsetting, 0);
for (j = 0; j < desc->interface[i].num_altsetting; ++j)
{
pushinterfacedesc(L, desc->interface[i].altsetting+j, 1);
lua_rawseti(L, -2, j+1);
}
lua_rawseti(L, -2, i+1);
}
lua_rawset(L, -3);
return 1;
}
static libusb_context* getctx(lua_State *L, int ix)
{
libusb_context **ctx;
ctx = (libusb_context**)luaL_checkudata(L, ix, CONTEXT_MT);
if (ctx == NULL || *ctx == INVALID_CONTEXT)
luaL_error(L, "attempt to use an invalid context");
return *ctx;
}
static libusb_device* getdev(lua_State *L, int ix)
{
libusb_device **dev;
dev = (libusb_device**)luaL_checkudata(L, ix, DEVICE_MT);
if (dev == NULL || *dev == INVALID_DEVICE)
luaL_error(L, "attempt to use an invalid device");
return *dev;
}
static libusb_device_handle* gethandle(lua_State *L, int ix)
{
libusb_device_handle **handle;
handle = (libusb_device_handle**)luaL_checkudata(L, ix, HANDLE_MT);
if (handle == NULL || *handle == INVALID_HANDLE)
luaL_error(L, "attempt to use a closed device");
return *handle;
}
static struct libusb_transfer* gettransfer(lua_State *L, int ix)
{
struct libusb_transfer **transfer;
transfer = (struct libusb_transfer**)luaL_checkudata(L, ix, TRANSFER_MT);
if (*transfer == INVALID_TRANSFER)
luaL_error(L, "attempt to use an invalid transfer");
return *transfer;
}
static int lusb_init(lua_State *L)
{
int err;
libusb_context **ctx = newctx(L);
if ((err = libusb_init(ctx)) != 0)
return _err(L, err);
return 1;
}
static int lusb_set_debug(lua_State *L)
{
libusb_context *ctx;
int level;
lua_settop(L, 2);
if (lua_isuserdata(L, 1))
{
ctx = getctx(L, 1);
level = luaL_checkinteger(L, 2);
}
else
{
ctx = defctx(L);
level = luaL_checkinteger(L, 1);
}
libusb_set_debug(ctx, level);
return 0;
}
static int lusb_get_device_list(lua_State *L)
{
libusb_context *ctx;
libusb_device **devlist;
ssize_t numdevices, n;
if (!lua_isnoneornil(L, 1))
{
ctx = getctx(L, 1);
}
else
{
lua_settop(L, 0);
ctx = defctx(L);
}
numdevices = libusb_get_device_list(ctx, &devlist);
if (numdevices < 0)
return _err(L, numdevices);
lua_createtable(L, numdevices, 0);
for (n = 0; n < numdevices; ++n)
{
newdev(L, 1, devlist[n]);
lua_rawseti(L, -2, n+1);
}
libusb_free_device_list(devlist, 1);
return 1;
}
static int lusb_get_bus_number(lua_State *L)
{
libusb_device *dev = getdev(L, 1);
lua_pushinteger(L, libusb_get_bus_number(dev));
return 1;
}
static int lusb_get_device_address(lua_State *L)
{
libusb_device *dev = getdev(L, 1);
lua_pushinteger(L, libusb_get_device_address(dev));
return 1;
}
static int lusb_get_max_packet_size(lua_State *L)
{
libusb_device *dev = getdev(L, 1);
int endp = luaL_checkinteger(L, 2);
int num = libusb_get_max_packet_size(dev, endp);
if (num < 0)
return _err(L, num);
lua_pushinteger(L, num);
return 1;
}
static int lusb_get_max_iso_packet_size(lua_State *L)
{
libusb_device *dev = getdev(L, 1);
int endp = luaL_checkinteger(L, 2);
int num = libusb_get_max_iso_packet_size(dev, endp);
if (num < 0)
return _err(L, num);
lua_pushinteger(L, num);
return 1;
}
static int lusb_get_device_descriptor(lua_State *L)
{
struct libusb_device_descriptor desc;
libusb_device *dev;
int err;
dev = getdev(L, 1);
if ((err = libusb_get_device_descriptor(dev, &desc)) != 0)
return _err(L, err);
pushdevicedesc(L, &desc);
return 1;
}
static int lusb_get_active_config_descriptor(lua_State *L)
{
struct libusb_config_descriptor *desc;
libusb_device *dev;
int err;
dev = getdev(L, 1);
if ((err = libusb_get_active_config_descriptor(dev, &desc)) != 0)
return _err(L, err);
pushconfigdesc(L, desc);
libusb_free_config_descriptor(desc);
return 1;
}
static int lusb_get_config_descriptor(lua_State *L)
{
struct libusb_config_descriptor *desc;
libusb_device *dev;
int idx, err;
dev = getdev(L, 1);
idx = luaL_checkinteger(L, 2);
if ((err = libusb_get_config_descriptor(dev, idx, &desc)) != 0)
return _err(L, err);
pushconfigdesc(L, desc);
libusb_free_config_descriptor(desc);
return 1;
}
static int lusb_get_config_descriptor_by_value(lua_State *L)
{
struct libusb_config_descriptor *desc;
libusb_device *dev;
int val, err;
dev = getdev(L, 1);
val = luaL_checkinteger(L, 2);
if ((err = libusb_get_config_descriptor_by_value(dev, val, &desc)) != 0)
return _err(L, err);
pushconfigdesc(L, desc);
libusb_free_config_descriptor(desc);
return 1;
}
static int lusb_get_descriptor(lua_State *L)
{
libusb_device_handle *handle;
unsigned char buf[255];
struct usb_descriptor *desc;
int dtype, idx;
int err;
handle = gethandle(L, 1);
dtype = luaL_checkinteger(L, 2);
idx = luaL_checkinteger(L, 3);
err = libusb_get_descriptor(handle, dtype, idx, buf, sizeof(buf));
if (err < 0)
return _err(L, err);
desc = (struct usb_descriptor*)buf;
switch (desc->bDescriptorType)
{
case LIBUSB_DT_DEVICE:
pushdevicedesc(L, (struct libusb_device_descriptor*)desc);
break;
case LIBUSB_DT_INTERFACE:
pushinterfacedesc(L, (struct libusb_interface_descriptor*)desc, 0);
break;
case LIBUSB_DT_ENDPOINT:
pushendpointdesc(L, (struct libusb_endpoint_descriptor*)desc);
break;
case LIBUSB_DT_STRING:
lua_pushlstring(L, (char*)((struct usb_string_descriptor*)desc)->bString, desc->bLength-2);
break;
default:
lua_pushlstring(L, (char*)buf, desc->bLength);
break;
}
return 1;
}
static int lusb_get_string_descriptor(lua_State *L)
{
struct usb_string_descriptor desc;
libusb_device_handle *handle;
int idx, langid;
size_t len;
int err;
handle = gethandle(L, 1);
idx = luaL_checkinteger(L, 2);
langid = luaL_optinteger(L, 3, 0);
len = sizeof(struct usb_string_descriptor);
err = libusb_get_string_descriptor(handle, idx, langid, (unsigned char*)&desc, len);
if (err < 0)
return _err(L, err);
len = desc.bLength - 2;
if (idx == 0)
{
/* descriptor is array of langids */
int i;
len /= 2;
lua_createtable(L, len, 0);
for (i = 0; i < len; ++i)
{
lua_pushinteger(L, desc.bString[i]);
lua_rawseti(L, -2, i+1);
}
return 1;
}
lua_pushlstring(L, (const char*)desc.bString, len);
return 1;
}
static int lusb_get_string_descriptor_ascii(lua_State *L)
{
libusb_device_handle *handle;
unsigned char str[255];
int idx;
int err;
handle = gethandle(L, 1);
idx = luaL_checkinteger(L, 2);
if (idx == 0)
return _err(L, LIBUSB_ERROR_INVALID_PARAM);
err = libusb_get_string_descriptor_ascii(handle, idx, str, sizeof(str));
if (err < 0)
return _err(L, err);
lua_pushstring(L, (const char*)str);
return 1;
}
static int lusb_get_string_descriptor_utf8(lua_State *L)
{
struct usb_string_descriptor desc;
libusb_device_handle *handle;
int idx, langid;
size_t len, i;
int err;
luaL_Buffer buffer;
handle = gethandle(L, 1);
idx = luaL_checkinteger(L, 2);
if (idx == 0)
return _err(L, LIBUSB_ERROR_INVALID_PARAM);
len = sizeof(struct usb_string_descriptor);
err = libusb_get_string_descriptor(handle, 0, 0, (unsigned char*)&desc, len);
if (err < 0)
return _err(L, err);
if (err < 4)
return _err(L, LIBUSB_ERROR_IO);
langid = desc.bString[0];
err = libusb_get_string_descriptor(handle, idx, langid, (unsigned char*)&desc, len);
if (err < 0)
return _err(L, err);
len = (err > 0 && desc.bLength > 1) ? (desc.bLength - 2)/2 : 0;
luaL_buffinit(L, &buffer);
for (i = 0; i < len; ++i)
{
unsigned int ch = desc.bString[i];
if (ch < 0x80)
{
luaL_addchar(&buffer, ch);
}
else if (ch < 0x800)
{
luaL_addchar(&buffer, (ch>>6)|0xC0);
luaL_addchar(&buffer, (ch&0x3F)|0x80);
}
else if ((ch&0xF800) == 0xD800)
{
unsigned int ch2 = desc.bString[i+1];
if ((ch2&0xFC00) == 0xDC00)
{
ch = (((ch&0x3FF)<<10)|(ch2&0x3FF)) + 0x10000;
luaL_addchar(&buffer, (ch>>18)|0xF0);
luaL_addchar(&buffer, ((ch>>12)&0x3F)|0x80);
luaL_addchar(&buffer, ((ch>>6)&0x3F)|0x80);
luaL_addchar(&buffer, (ch&0x3F)|0x80);
}
else
{
luaL_addchar(&buffer, 0xEF);
luaL_addchar(&buffer, 0xBF);
luaL_addchar(&buffer, 0xBD);
}
}
else
{
luaL_addchar(&buffer, (ch>>12)|0xE0);
luaL_addchar(&buffer, ((ch>>6)&0x3F)|0x80);
luaL_addchar(&buffer, (ch&0x3F)|0x80);
}
}
luaL_pushresult(&buffer);
return 1;
}
static int lusb_open_device_with_vid_pid(lua_State *L)
{
libusb_context *ctx;
libusb_device_handle **ud, *handle;
uint16_t vid, pid;
lua_settop(L, 3);
if (lua_isuserdata(L, 1))
{
ctx = getctx(L, 1);
}
else
{
ctx = defctx(L);
lua_insert(L, 1);
}
vid = luaL_checkinteger(L, 2);
pid = luaL_checkinteger(L, 3);
ud = newhandle(L, 1);
handle = libusb_open_device_with_vid_pid(ctx, vid, pid);
/* the real error code is lost, just return NO_DEVICE every time */
if (handle == NULL)
return _err(L, LIBUSB_ERROR_NO_DEVICE);
*ud = handle;
return 1;
}
static int lusb_open(lua_State *L)
{
libusb_device *dev;
libusb_device_handle **handle;
int err;
dev = getdev(L, 1);
handle = newhandle(L, 1);
if ((err = libusb_open(dev, handle)) != 0)
return _err(L, err);
return 1;
}
static int lusb_get_device(lua_State *L)
{
libusb_device_handle *handle = gethandle(L, 1);
libusb_device *dev = libusb_get_device(handle);
newdev(L, 1, dev);
return 1;
}
static int lusb_get_configuration(lua_State *L)
{
libusb_device_handle *handle = gethandle(L, 1);
int cfg, err;
if ((err = libusb_get_configuration(handle, &cfg)) != 0)
return _err(L, err);
lua_pushinteger(L, cfg);
return 1;
}
static int lusb_set_configuration(lua_State *L)
{
libusb_device_handle *handle;
int cfg, err;
handle = gethandle(L, 1);
cfg = luaL_checkinteger(L, 2);
if ((err = libusb_set_configuration(handle, cfg)) != 0)
return _err(L, err);
lua_settop(L, 1);
return 1;
}
static int lusb_claim_interface(lua_State *L)
{
libusb_device_handle *handle;
int iface, err;
handle = gethandle(L, 1);
iface = luaL_checkinteger(L, 2);
if ((err = libusb_claim_interface(handle, iface)) != 0)
return _err(L, err);
lua_settop(L, 1);
return 1;
}
static int lusb_release_interface(lua_State *L)
{
libusb_device_handle *handle;
int iface, err;
handle = gethandle(L, 1);
iface = luaL_checkinteger(L, 2);
if ((err = libusb_release_interface(handle, iface)) != 0)
return _err(L, err);
lua_settop(L, 1);
return 1;
}
static int lusb_set_interface_alt_setting(lua_State *L)
{
libusb_device_handle *handle;
int iface, alt, err;
handle = gethandle(L, 1);
iface = luaL_checkinteger(L, 2);
alt = luaL_checkinteger(L, 3);
if ((err = libusb_set_interface_alt_setting(handle, iface, alt)) != 0)
return _err(L, err);
lua_settop(L, 1);
return 1;
}
static int lusb_clear_halt(lua_State *L)
{