-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqlight.c
More file actions
281 lines (251 loc) · 5.46 KB
/
qlight.c
File metadata and controls
281 lines (251 loc) · 5.46 KB
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
/* Q-Light USB driver v0.0.4
Written by Kenneth Wilke */
#include "qlight.h"
int main(int argc, char *argv[])
{
// show usage and exit if no args given
if(argc < 2)
{
usage(argv[0]);
exit(1);
}
handle_arguments(argc, argv);
// initialize libusb and detect qlight
init();
// do the needful
set_state(red, yellow, green, blue, white, sound);
// cleanup
usb_release();
quit(0);
return 0;
}
void usage()
{
fprintf(stderr, "Note: Sounds and lights vary by model\n - Light states: off, on, blink\n\
-r <state> | red light\n\
-y <state> | yellow light\n\
-b <state> | blue light\n\
-g <state> | green light\n\
-w <state> | white light\n\
\n\
- Sound states: off, use 1-5 to play sound\n\
-s <state> | speaker\n\n");
}
void parse_option(int *target, char *option, int lightopt)
{
int soundopt;
if(lightopt)
{
// handle light state options
if(strcmp(option, "off") == 0)
{
*target = light_off;
}
else if(strcmp(option, "on") == 0)
{
*target = light_on;
}
else if(strcmp(option, "blink") == 0)
{
*target = light_blink;
}
else
{
fprintf(stderr, "Invalid state selection\n");
usage();
exit(1);
}
}
else
{
// handle sound state options
if(strcmp(option, "off") == 0)
{
*target = sound_off;
}
else
{
soundopt = atoi(option);
if(soundopt >= 1 && soundopt <= 5)
{
*target = soundopt;
}
else
{
fprintf(stderr, "Sound selection invalid or out of range\n");
usage();
exit(1);
}
}
}
}
void handle_arguments(int argc, char *argv[])
{
int opt;
red = light_nochange;
yellow = light_nochange;
blue = light_nochange;
green = light_nochange;
white = light_nochange;
sound = sound_nochange;
opterr = 0;
while((opt = getopt(argc, argv, "r:y:b:g:w:s:")) != -1)
{
switch(opt)
{
case '?':
// unhandled switches and unfilled arguments
if(optopt == 'r' ||
optopt == 'y' ||
optopt == 'b' ||
optopt == 'g' ||
optopt == 'w' ||
optopt == 's')
{
fprintf(stderr, "Option '%c' requires an argument\n", optopt);
}
else
{
fprintf(stderr, "Unhandled option '%c' given\n", optopt);
}
usage(argv[0]);
exit(1);
case 'r':
parse_option(&red, optarg, 1);
break;
case 'y':
parse_option(&yellow, optarg, 1);
break;
case 'b':
parse_option(&blue, optarg, 1);
break;
case 'g':
parse_option(&green, optarg, 1);
break;
case 'w':
parse_option(&white, optarg, 1);
break;
case 's':
parse_option(&sound, optarg, 0);
break;
}
}
}
void init()
{
libusb_device **devices;
ssize_t deviceCount;
struct libusb_device_descriptor descriptor;
libusb_device *qlight = NULL;
int i, errNo;
// initialize libusb
if(libusb_init(&context) < 0)
{
fprintf(stderr, "Failed to initialize libusb context\n");
quit(1);
}
// full libusb verbosity
libusb_set_debug(context, 3);
// obtain list of devices
deviceCount = libusb_get_device_list(context, &devices);
if(deviceCount < 0)
{
fprintf(stderr, "Failed to obtain device list\n");
libusb_free_device_list(devices, 1);
quit(1);
}
// search for qlight in list of devices
for(i = 0; !qlight && i < deviceCount; i++)
{
if(libusb_get_device_descriptor(devices[i], &descriptor) < 0)
{
fprintf(stderr, "Failed to obtain a device descriptor\n");
libusb_free_device_list(devices, 1);
quit(1);
}
if(descriptor.idVendor == 0x04d8 && descriptor.idProduct == 0xe73c)
{
printf("Q-light detected\n");
qlight = devices[i];
}
}
if(!qlight)
{
fprintf(stderr, "Q-light not detected\n");
libusb_free_device_list(devices, 1);
quit(1);
}
// open device handle
if(libusb_open(qlight, &qlight_handle) < 0)
{
fprintf(stderr, "Failed to open device\n");
libusb_free_device_list(devices, 1);
quit(1);
}
// cleanup device list and poke kernel if necessary
libusb_free_device_list(devices, 1);
if(libusb_kernel_driver_active(qlight_handle, 0))
{
printf("Kernel driver is active\n");
if(!libusb_detach_kernel_driver(qlight_handle, 0))
{
printf("Kernel driver detached\n");
}
}
// mark your territory, this device is mine!
if((errNo = libusb_claim_interface(qlight_handle, 0)) < 0)
{
fprintf(stderr, "Could not claim interface\n");
switch(errNo)
{
case LIBUSB_ERROR_NOT_FOUND:
fprintf(stderr, " Could not find interface\n");
break;
case LIBUSB_ERROR_BUSY:
fprintf(stderr, " Device has been claimed by another program\n");
break;
case LIBUSB_ERROR_NO_DEVICE:
fprintf(stderr, " Device not found\n");
break;
}
libusb_close(qlight_handle);
quit(1);
}
printf("Claimed interface\n");
}
void usb_release(void)
{
if(libusb_release_interface(qlight_handle, 0) != 0)
{
fprintf(stderr, "Failed to release interface\n");
libusb_close(qlight_handle);
quit(1);
}
printf("Released interface\n");
libusb_close(qlight_handle);
}
void quit(int exitCode)
{
libusb_exit(context);
exit(exitCode);
}
int set_state(int red, int yellow, int green, int blue, int white, int sound)
{
int bytesWritten;
unsigned char data[16];
// copy template packet to local variable
memcpy(data, data_template, 16);
// fill desired state changes
data[qlight_red] = red;
data[qlight_yellow] = yellow;
data[qlight_green] = green;
data[qlight_blue] = blue;
data[qlight_white] = white;
data[qlight_sound] = sound;
// send the packet
if(libusb_bulk_transfer(qlight_handle, 0x1, data, 16, &bytesWritten, 0) == 0 && bytesWritten == 16)
{
return 1;
}
return 0;
}