udev-rules: Fix typo
[pub/pl2303-ft232-gpio.git] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 /* According to POSIX.1-2001 */
4 #include <sys/select.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <termios.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <usb.h>
13 #include <getopt.h>
14
15
16 #define _GNU_SOURCE
17 #include <getopt.h>
18
19
20 void check_handle(usb_dev_handle **h, const char* manuf, const char* product, const char* serial);
21 int gpio_read(usb_dev_handle *h, int gpio);
22 void gpio_in(usb_dev_handle *h, int gpio, int pullup);
23 void gpio_out(usb_dev_handle *h, int gpio, int value);
24
25
26 void handle_error(int ret)
27 {
28 if (ret<0) {
29 perror("Failed to write to PL2303 device");
30 fprintf(stderr, "Have you installed the correct udev rules?\n");
31 exit(1);
32 }
33 }
34
35 static struct option long_options[] =
36 {
37 /* These options set a flag. */
38 {"help", no_argument, 0, 'h'},
39 {"gpio", required_argument, 0, 'g'},
40 {"in", optional_argument, 0, 'i'},
41 {"out", required_argument, 0, 'o'},
42 {"sleep", required_argument, 0, 's'},
43 {"read", no_argument, 0, 'r'},
44 {"product", required_argument, 0, 'p'},
45 {"manuf", required_argument, 0, 'm'},
46 {"serial", required_argument, 0, 'n'},
47 {0, 0, 0, 0}
48 };
49
50 void usage(const char *self)
51 {
52 printf("PL2303HXA/CP2103 userspace GPIO control tool\n"
53 "(c) Andrew 'Necromant' Andrianov 2014, License: GPLv3\n"
54 "Usage: %s [action1] [action2] ...\n"
55 "Options are: \n"
56 "\t --product=blah - Use device with this 'product' string\n"
57 "\t --serial=blah - Use device with this 'serial' string\n"
58 "\t --manuf=blah - Use device with this 'manufacturer' string\n"
59 "\t -g/--gpio n - select GPIO, n=0, 1\n"
60 "\t -i/--in - configure GPIO as input\n"
61 "\t -o/--out v - configure GPIO as output with value v\n"
62 "\t -r/--read v - Read current GPIO value\n\n"
63 "\t -s/--sleep v - Delay for v ms\n\n"
64 "Examples: \n"
65 "\t%s --gpio=1 --out 1\n"
66 "\t%s --gpio=0 --out 0 --gpio=1 --in\n\n"
67 "All arguments are executed from left to right, you can add \n"
68 "delays using --sleep v option. e.g. \n"
69 "\t%s --gpio=0 --out 0 --sleep 1000 --gpio=0 --out 1\n"
70 "\n", self, self, self, self);
71 }
72
73 int main(int argc, char* argv[])
74 {
75 char c;
76 usb_dev_handle *h = NULL;
77 int gpio=0;
78 const char *product = NULL;
79 const char *manuf = NULL;
80 const char *serial = NULL;
81 if (argc == 1)
82 {
83 usage(argv[0]);
84 exit(1);
85 }
86 while(1) {
87 int option_index = 0;
88
89 c = getopt_long (argc, argv, "hg:i:o:r:s:",
90 long_options, &option_index);
91
92 /* Detect the end of the options. */
93 if (c == -1)
94 break;
95
96 switch (c)
97 {
98 case 'p':
99 product = optarg;
100 break;
101 case 'm':
102 manuf = optarg;
103 break;
104 case 'n':
105 serial = optarg;
106 break;
107 case 'h':
108 usage(argv[0]);
109 exit(1);
110 break;
111 case 'g':
112 gpio = atoi(optarg);
113 break;
114 case 'i':
115 {
116 int v=0;
117 check_handle(&h, manuf, product, serial);
118 if (optarg)
119 v = atoi(optarg);
120 gpio_in(h, gpio, v);
121 break;
122 }
123 case 'o':
124 {
125 int v=0;
126 check_handle(&h, manuf, product, serial);
127 if (optarg)
128 v = atoi(optarg);
129 gpio_out(h, gpio, v);
130 break;
131 }
132 case 's':
133 {
134 unsigned long n = atoi(optarg);
135 usleep(n*1000);
136 break;
137 }
138 case 'r':
139 {
140 check_handle(&h, manuf, product, serial);
141 printf("%d\n", gpio_read(h, gpio) ? 1 : 0);
142 break;
143 }
144
145 }
146 }
147 return 0;
148 }