-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.c
152 lines (141 loc) · 3.87 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
/* According to POSIX.1-2001 */
#include <sys/select.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <libusb.h>
#include <getopt.h>
#define _GNU_SOURCE
#include <getopt.h>
int get_device_vid();
int get_device_pid();
void check_handle(libusb_device_handle **h, int vid, int pid, const char* manuf, const char* product, const char* serial, int port);
int gpio_read(libusb_device_handle *h, int gpio);
void gpio_in(libusb_device_handle *h, int gpio, int pullup);
void gpio_out(libusb_device_handle *h, int gpio, int value);
void ncusb_device_list(struct libusb_context *ctx,
int vendor, int product);
void handle_error(int ret)
{
if (ret<0) {
perror("Failed to write to PL2303 device");
fprintf(stderr, "Have you installed the correct udev rules?\n");
exit(1);
}
}
static struct option long_options[] =
{
/* These options set a flag. */
{"help", no_argument, 0, 'h'},
{"gpio", required_argument, 0, 'g'},
{"in", no_argument, 0, 'i'},
{"out", required_argument, 0, 'o'},
{"sleep", required_argument, 0, 's'},
{"product", required_argument, 0, 'p'},
{"port", required_argument, 0, 'P'},
{"manuf", required_argument, 0, 'm'},
{"serial", required_argument, 0, 'n'},
{"list", no_argument, 0, 'l'},
{0, 0, 0, 0}
};
void usage(const char *self)
{
printf("PL2303HXA/CP2103 userspace GPIO control tool\n"
"(c) Andrew 'Necromant' Andrianov 2014, License: GPLv3\n"
"Usage: %s [action1] [action2] ...\n"
"Options are: \n"
"\t --product=blah - Use device with this 'product' string\n"
"\t --serial=blah - Use device with this 'serial' string\n"
"\t --manuf=blah - Use device with this 'manufacturer' string\n"
"\t -P/--port N - Use only device in physical port N\n"
"\t -g/--gpio n - select GPIO, n=0, 1\n"
"\t -i/--in - configure GPIO as input and read value\n"
"\t -o/--out v - configure GPIO as output with value v\n"
"\t -s/--sleep v - Delay for v ms\n\n"
"\t -l/--list - List candidates\n\n"
"Examples: \n"
"\t%s --gpio=1 --out 1\n"
"\t%s --gpio=0 --out 0 --gpio=1 --in\n\n"
"All arguments are executed from left to right, you can add \n"
"delays using --sleep v option. e.g. \n"
"\t%s --gpio=0 --out 0 --sleep 1000 --gpio=0 --out 1\n"
"\n", self, self, self, self);
}
int main(int argc, char* argv[])
{
int c;
libusb_device_handle *h = NULL;
int gpio=0;
const char *product = NULL;
const char *manuf = NULL;
const char *serial = NULL;
int port = -1;
if (argc == 1)
{
usage(argv[0]);
exit(1);
}
while(1) {
int option_index = 0;
c = getopt_long (argc, argv, "hg:io:s:lP:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 'p':
product = optarg;
break;
case 'm':
manuf = optarg;
break;
case 'n':
serial = optarg;
break;
case 'h':
usage(argv[0]);
exit(1);
break;
case 'l':
ncusb_device_list(NULL, get_device_vid(), get_device_pid());
break;
case 'g':
gpio = atoi(optarg);
break;
case 'P':
port = atoi(optarg);
break;
case 'i':
{
int v=0;
check_handle(&h, get_device_vid(), get_device_pid(), manuf, product, serial, port);
gpio_in(h, gpio, v);
printf("%d\n", gpio_read(h, gpio) ? 1 : 0);
break;
}
case 'o':
{
int v=0;
check_handle(&h, get_device_vid(), get_device_pid(), manuf, product, serial, port);
if (optarg)
v = atoi(optarg);
gpio_out(h, gpio, v);
break;
}
case 's':
{
unsigned long n = atoi(optarg);
usleep(n*1000);
break;
}
}
}
return 0;
}