forked from darrylb123/usbrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbrelay.c
169 lines (150 loc) · 5.46 KB
/
usbrelay.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
/*
usbrelay: Control USB HID connected electrical relay modules
Copyright (C) 2014 Darryl Bond
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <hidapi/hidapi.h>
#include "usbrelay.h"
int main( int argc, char *argv[]) {
struct relay *relays = 0;
unsigned char buf[9];// 1 extra byte for the report ID
char arg_t[20] = {'\0'};
int debug = 0;
char *token;
const char delimiters[] = "_=";
int i;
struct hid_device_info *devs, *cur_dev;
hid_device *handle;
int size = sizeof(struct relay);
unsigned short vendor_id = 0x16c0;
unsigned short product_id = 0x05df;
char *vendor, *product;
/* allocate the memeory for all the relays */
if (argc > 1) {
relays = malloc(size*(argc+1)); /* Yeah, I know. Not using the first member */
relays[0].this_serial[0] = '\0';
} else debug = 1;
/* loop through the command line and grab the relay details */
for (i = 1 ; i < argc; i++ ) {
/* copy the arg and bounds check */
strncpy(arg_t,argv[i],19);
arg_t[19] = '\0';
token = strtok(arg_t, delimiters);
if (token != NULL) {
strcpy(relays[i].this_serial,token);
}
token = strtok((char *)NULL, delimiters);
if (token != NULL) {
relays[i].relay_num = atoi(token);
}
token = strtok(NULL, delimiters);
if (token != NULL) {
if (atoi(token)) {
relays[i].state = ON;
} else {
relays[i].state = OFF;
}
}
fprintf(stderr,"Orig: %s, Serial: %s, Relay: %d State: %x\n",arg_t,relays[i].this_serial,relays[i].relay_num,relays[i].state);
relays[i].found = 0;
}
product = getenv("USBID");
if (product != NULL) {
vendor = strsep(&product, ":");
if (vendor && *vendor) {
vendor_id = strtol(vendor, NULL, 16);
}
if (product && *product) {
product_id = strtol(product, NULL, 16);
}
}
devs = hid_enumerate(vendor_id, product_id);
cur_dev = devs;
while (cur_dev) {
fprintf(stderr,"Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
fprintf(stderr,"\n");
fprintf(stderr," Manufacturer: %ls\n", cur_dev->manufacturer_string);
fprintf(stderr," Product: %ls\n", cur_dev->product_string);
fprintf(stderr," Release: %hx\n", cur_dev->release_number);
fprintf(stderr," Interface: %d\n", cur_dev->interface_number);
handle = hid_open_path(cur_dev->path);
if (!handle) {
fprintf(stderr,"unable to open device\n");
return 1;
}
buf[0] = 0x01;
int ret = hid_get_feature_report(handle,buf,sizeof(buf));
if (ret == -1) {
perror("hid_get_feature_report");
exit(1);
}
if (debug) {
if (buf[7] == 0) printf("%s_1=0\n%s_2=0\n",buf,buf );
else if (buf[7] == 1) printf("%s_1=1\n%s_2=0\n",buf,buf);
else if (buf[7] == 2) printf("%s_1=0\n%s_2=1\n",buf,buf);
else if (buf[7] == 3) printf("%s_1=1\n%s_2=1\n",buf,buf);
/* fprintf(stderr,"Usage: %s %s_1=0 %s\n",argv[0],buf); */
}
/* loop through the supplied command line and try to match the serial */
for (i=1;i<argc;i++) {
fprintf(stderr,"Serial: %s, Relay: %d State: %x \n",relays[i].this_serial,relays[i].relay_num,relays[i].state);
if (!strcmp(relays[i].this_serial, (const char *) buf)) {
fprintf(stderr,"%d HID Serial: %s ", i, buf);
fprintf(stderr,"Serial: %s, Relay: %d State: %x\n",relays[i].this_serial,relays[i].relay_num,relays[i].state);
operate_relay(handle,relays[i].relay_num,relays[i].state);
relays[i].found = 1;
}
}
hid_close(handle);
fprintf(stderr,"\n");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
/* Free static HIDAPI objects. */
hid_exit();
for (i=1;i<argc;i++){
fprintf(stderr,"Serial: %s, Relay: %d State: %x ",relays[i].this_serial,relays[i].relay_num,relays[i].state);
if (relays[i].found )
fprintf(stderr,"--- Found\n");
else
fprintf(stderr,"--- Not Found\n");
}
if (relays)
free(relays);
exit(0);
}
int operate_relay(hid_device *handle,unsigned char relay, unsigned char state){
unsigned char buf[9];// 1 extra byte for the report ID
int res;
buf[0] = 0x0; //report number
buf[1] = state;
buf[2] = relay;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x00;
buf[7] = 0x00;
buf[8] = 0x00;
res = hid_write(handle, buf, sizeof(buf));
if (res < 0) {
fprintf(stderr,"Unable to write()\n");
fprintf(stderr,"Error: %ls\n", hid_error(handle));
}
return(res);
}