forked from nakj/powerusb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerusb.c
380 lines (324 loc) · 6.91 KB
/
powerusb.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
#include <libusb-1.0/libusb.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define USB_VENDOR_ID 0x04d8
#define USB_PRODUCT_ID 0x003f
#define ENDPOINT_IN 0x81
#define ENDPOINT_OUT 0x01
#define CMD_GET_MODEL 0xaa
#define CMD_GET_FIRM_VER 0xa7
#define CMD_GET_STATE1 0xa1
#define CMD_GET_STATE2 0xa2
#define CMD_GET_STATE3 0xac
#define CMD_GET_POWER 0xb1
#define CMD_PING 0xb2
#define CMD_OUTLET1_ON 0x41
#define CMD_OUTLET1_OFF 0x42
#define CMD_OUTLET2_ON 0x43
#define CMD_OUTLET2_OFF 0x44
#define CMD_OUTLET3_ON 0x45
#define CMD_OUTLET3_OFF 0x50
//#define DEBUG
#ifdef DEBUG
#define Dprintf printf
#else
#define Dprintf(fmt, ...) while(0){}
#endif
libusb_context *ctx = NULL;
libusb_device *dev;
libusb_device **devs;
struct libusb_device_handle *devh = NULL;
int state[4];
void usage(void)
{
const char usage_string[] =
"command usage\n"
"\tpowerusb get power [sec]\n"
"\tpowerusb get state [1-3]\n"
"\tpowerusb set [1-3] [on|off]\n";
printf("%s",usage_string);
exit(1);
}
int send_cmd(struct libusb_device_handle *devh,int cmd, uint8_t ret[2])
{
int i;
uint8_t buf[64];
int size=0;
Dprintf("send_cmd:%x\n",cmd);
memset(buf, 0xff, sizeof(buf));
buf[0] = cmd;
/* packet send*/
if((libusb_interrupt_transfer(devh,ENDPOINT_OUT,buf,
sizeof(buf),&size, 1000)) < 0 ) {
perror("libusb_interrupt_transfer");
exit(1);
}
memset(buf, 0x00, sizeof(buf));
/* packet recieve if needed */
if (cmd > 0x50) {
Dprintf("recieve\n");
if((libusb_interrupt_transfer(devh,ENDPOINT_IN,buf,
sizeof(buf),&size, 1000)) < 0 ) {
perror("libusb_interrupt_transfer");
exit(1);
}
}
Dprintf("send_cmd:read:");
for(i=0;i<2;i++){
ret[i] = buf[i];
Dprintf("%02x",buf[i]);
}
Dprintf("\n");
return 0;
}
void finalize(void){
libusb_close(devh);
libusb_exit(ctx);
}
int initialize(void){
int r=1;
int i=0;
int cnt=0;
/* libusb initialize*/
if ((r = libusb_init(&ctx)) < 0) {
perror("libusb_init\n");
exit(1);
} else {
libusb_set_debug(ctx,3);
Dprintf("init done\n");
}
/* confirm powerusb device */
/* list up all usb devices */
if((libusb_get_device_list(ctx,&devs)) < 0) {
perror("no usb device found");
exit(1);
}
/* check every usb devices */
while((dev =devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
if (libusb_get_device_descriptor(dev,&desc) < 0) {
perror("failed to get device descriptor\n");
return 1;
}
/* count how many PowerUSB device connected */
if (desc.idVendor == USB_VENDOR_ID &&
desc.idProduct == USB_PRODUCT_ID) {
cnt++;
Dprintf("PowerUSB device found\n");
}
}
/* no PowerUSB found*/
if (cnt == 0) {
fprintf(stderr, "Power USB device not connected\n");
exit(1);
}
/* multi-PowerUSB device found: return error*/
if (cnt > 1) {
/* FIXME */
fprintf(stderr, "multi PowerUSB is not implemented yet\n");
exit(1);
}
/* open powerusb device */
if ((devh = libusb_open_device_with_vid_pid(ctx,USB_VENDOR_ID,
USB_PRODUCT_ID)) < 0 ) {
perror("can't find PowerUSB device\n");
finalize();
exit(1);
} else {
Dprintf("PowerUSB device opened\n");
}
/* detach kernel driver if attached. */
/* is kernel driver active?*/
r = libusb_kernel_driver_active(devh,0);
if (r == 1) {
/*detaching kernel driver*/
r = libusb_detach_kernel_driver(devh,0);
if (r != 0) {
perror("detaching kernel driver failed");
exit(1);
}
}
return 0;
}
int get_status(int a)
{
uint8_t ret[2];
if (( a < 0) | (a > 3))
usage();
switch(a){
case 1:
send_cmd(devh,CMD_GET_STATE1,ret);
break;
case 2:
send_cmd(devh,CMD_GET_STATE2,ret);
break;
case 3:
send_cmd(devh,CMD_GET_STATE3,ret);
break;
default:
usage();
}
printf("Outlet%d status:",a);
if (ret[0] == 0){
printf("OFF\n");
}else {
printf("ON\n");
}
return 0;
}
void get_power(int psec)
{
uint8_t ret[2];
char buf[5],*e;
while (psec > 0){
ret[0]=ret[1]=0;
send_cmd(devh,CMD_GET_POWER,ret);
sprintf(buf,"0x%02x%02x",ret[0],ret[1]);
Dprintf("ret:%s\n",buf);
printf("Current %ldmA\n",strtol(buf,&e,16));
send_cmd(devh,CMD_PING,ret);
sleep(1);
psec--;
}
}
void cmd_get(int argc,char **argv)
{
int psec;
if (argc < 3)
usage();
if (!strcmp(argv[2],"power")) {
Dprintf("power\n");
if (argc < 4 ) {
psec =10;
} else {
psec = atoi(argv[3]);
}
get_power(psec);
} else if (!strcmp(argv[2],"state")){
Dprintf("state");
if (argc < 4)
usage();
if (!strcmp(argv[3],"all")) {
get_status(1);
get_status(2);
get_status(3);
}else if (!(!strcmp(argv[3],"1") | !strcmp(argv[3],"2") | !strcmp(argv[3],"3" ))){
usage();
}else {
get_status(atoi(argv[3]));
}
}else{
usage();
}
}
void cmd_set2(int s, char* cmd)
{
uint8_t ret[2];
int cond;
printf("to swich condition Outlet%d:%s\n",s, cmd);
if (!strcmp(cmd,"on")) {
cond = 1;
}else if (!strcmp(cmd,"off")) {
cond = 0;
}
switch(s){
case 1:
if (cond == 0){
send_cmd(devh,CMD_OUTLET1_OFF,ret);
}else if (cond == 1) {
send_cmd(devh,CMD_OUTLET1_ON,ret);
}
break;
case 2:
if (cond == 0){
send_cmd(devh,CMD_OUTLET2_OFF,ret);
}else if (cond == 1) {
send_cmd(devh,CMD_OUTLET2_ON,ret);
}
break;
case 3:
if (cond == 0){
send_cmd(devh,CMD_OUTLET3_OFF,ret);
}else if (cond == 1) {
send_cmd(devh,CMD_OUTLET3_ON,ret);
}
break;
default:
usage();
exit(1);
}
}
void cmd_set(int argc,char **argv)
{
int s;
if (argc < 4)
usage();
s = atoi(argv[2]);
if ((s ==1) | (s ==2) | (s ==3)) {
/* set routine */
if (!strcmp(argv[3],"on")|!strcmp(argv[3],"off"))
cmd_set2(s,argv[3]);
else
usage();
} else {
usage();
}
}
int main(int argc, char **argv)
{
uint8_t ret[2];
int i;
initialize();
send_cmd(devh,CMD_GET_MODEL,ret);
printf("Model:");
switch (ret[0]){
case 1:
printf("Basic\n");
break;
case 2:
printf("digIO\n");
break;
case 3:
printf("watchdog\n");
break;
case 4:
printf("smart\n");
break;
}
send_cmd(devh,CMD_GET_FIRM_VER,ret);
printf("Firmware version: %d.%d\n",ret[0],ret[1]);
send_cmd(devh,CMD_GET_STATE1,ret);
state[1] = ret[0];
send_cmd(devh,CMD_GET_STATE2,ret);
state[2] = ret[0];
send_cmd(devh,CMD_GET_STATE3,ret);
state[3] = ret[0];
for (i=1;i<4;i++){
printf("Outlet%d:",i);
switch(state[i]){
case 0:
printf("off\n");
break;
case 1:
printf("on\n");
break;
}
}
if (argc < 2) {
usage();
exit(1);
}
if (!strcmp(argv[1],"get")) {
cmd_get(argc,argv);
}else if (!strcmp(argv[1],"set")) {
Dprintf("set\n");
cmd_set(argc,argv);
}else {
usage();
exit(1);
}
finalize();
exit(0);
}