This source file includes following definitions.
- gpiotools_request_linehandle
- gpiotools_set_values
- gpiotools_get_values
- gpiotools_release_linehandle
- gpiotools_get
- gpiotools_gets
- gpiotools_set
- gpiotools_sets
1
2
3
4
5
6
7
8
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <getopt.h>
16 #include <sys/ioctl.h>
17 #include <linux/gpio.h>
18 #include "gpio-utils.h"
19
20 #define COMSUMER "gpio-utils"
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 int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
59 unsigned int nlines, unsigned int flag,
60 struct gpiohandle_data *data,
61 const char *consumer_label)
62 {
63 struct gpiohandle_request req;
64 char *chrdev_name;
65 int fd;
66 int i;
67 int ret;
68
69 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
70 if (ret < 0)
71 return -ENOMEM;
72
73 fd = open(chrdev_name, 0);
74 if (fd == -1) {
75 ret = -errno;
76 fprintf(stderr, "Failed to open %s, %s\n",
77 chrdev_name, strerror(errno));
78 goto exit_close_error;
79 }
80
81 for (i = 0; i < nlines; i++)
82 req.lineoffsets[i] = lines[i];
83
84 req.flags = flag;
85 strcpy(req.consumer_label, consumer_label);
86 req.lines = nlines;
87 if (flag & GPIOHANDLE_REQUEST_OUTPUT)
88 memcpy(req.default_values, data, sizeof(req.default_values));
89
90 ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
91 if (ret == -1) {
92 ret = -errno;
93 fprintf(stderr, "Failed to issue %s (%d), %s\n",
94 "GPIO_GET_LINEHANDLE_IOCTL", ret, strerror(errno));
95 }
96
97 exit_close_error:
98 if (close(fd) == -1)
99 perror("Failed to close GPIO character device file");
100 free(chrdev_name);
101 return ret < 0 ? ret : req.fd;
102 }
103
104
105
106
107
108
109
110
111
112 int gpiotools_set_values(const int fd, struct gpiohandle_data *data)
113 {
114 int ret;
115
116 ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data);
117 if (ret == -1) {
118 ret = -errno;
119 fprintf(stderr, "Failed to issue %s (%d), %s\n",
120 "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret,
121 strerror(errno));
122 }
123
124 return ret;
125 }
126
127
128
129
130
131
132
133
134
135
136 int gpiotools_get_values(const int fd, struct gpiohandle_data *data)
137 {
138 int ret;
139
140 ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data);
141 if (ret == -1) {
142 ret = -errno;
143 fprintf(stderr, "Failed to issue %s (%d), %s\n",
144 "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret,
145 strerror(errno));
146 }
147
148 return ret;
149 }
150
151
152
153
154
155
156
157
158
159 int gpiotools_release_linehandle(const int fd)
160 {
161 int ret;
162
163 ret = close(fd);
164 if (ret == -1) {
165 perror("Failed to close GPIO LINEHANDLE device file");
166 ret = -errno;
167 }
168
169 return ret;
170 }
171
172
173
174
175
176
177
178
179
180
181 int gpiotools_get(const char *device_name, unsigned int line)
182 {
183 struct gpiohandle_data data;
184 unsigned int lines[] = {line};
185
186 gpiotools_gets(device_name, lines, 1, &data);
187 return data.values[0];
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 int gpiotools_gets(const char *device_name, unsigned int *lines,
204 unsigned int nlines, struct gpiohandle_data *data)
205 {
206 int fd;
207 int ret;
208 int ret_close;
209
210 ret = gpiotools_request_linehandle(device_name, lines, nlines,
211 GPIOHANDLE_REQUEST_INPUT, data,
212 COMSUMER);
213 if (ret < 0)
214 return ret;
215
216 fd = ret;
217 ret = gpiotools_get_values(fd, data);
218 ret_close = gpiotools_release_linehandle(fd);
219 return ret < 0 ? ret : ret_close;
220 }
221
222
223
224
225
226
227
228
229
230
231
232 int gpiotools_set(const char *device_name, unsigned int line,
233 unsigned int value)
234 {
235 struct gpiohandle_data data;
236 unsigned int lines[] = {line};
237
238 data.values[0] = value;
239 return gpiotools_sets(device_name, lines, 1, &data);
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 int gpiotools_sets(const char *device_name, unsigned int *lines,
256 unsigned int nlines, struct gpiohandle_data *data)
257 {
258 int ret;
259
260 ret = gpiotools_request_linehandle(device_name, lines, nlines,
261 GPIOHANDLE_REQUEST_OUTPUT, data,
262 COMSUMER);
263 if (ret < 0)
264 return ret;
265
266 return gpiotools_release_linehandle(ret);
267 }