-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightSensor-daemon.c
423 lines (359 loc) · 9.75 KB
/
lightSensor-daemon.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* lightSensor-daemon for DCS-932L by Andreas Boehler <andreas _AT_ aboehler.at>
* Copyright (c) 2014, Andreas Boehler
* based on GPIO example,
* Copyright (c) 2011, RidgeRun
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the RidgeRun.
* 4. Neither the name of the RidgeRun nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <syslog.h>
#include <signal.h>
// Declare some constants
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64
#define GPIO_LIGHT 11
#define GPIO_IR_PIN1 12
#define GPIO_IR_PIN2 14
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
syslog(LOG_ERR, "gpio/export failed: %i", gpio);
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) {
syslog(LOG_ERR, "gpio/unexport failed: %i", gpio);
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
syslog(LOG_ERR, "gpio/direction failed: %i", gpio);
perror("gpio/direction");
return fd;
}
if (out_flag)
write(fd, "out", 4);
else
write(fd, "in", 3);
close(fd);
return 0;
}
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
syslog(LOG_ERR, "gpio/set-value failed: %i", gpio);
perror("gpio/set-value");
return fd;
}
if (value)
write(fd, "1", 2);
else
write(fd, "0", 2);
close(fd);
return 0;
}
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF];
char ch;
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
syslog(LOG_ERR, "gpio/get-value failed: %i", gpio);
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1);
if (ch != '0') {
*value = 1;
} else {
*value = 0;
}
close(fd);
return 0;
}
int gpio_set_edge(unsigned int gpio, char *edge)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
syslog(LOG_ERR, "gpio/set-edge failed: %i", gpio);
perror("gpio/set-edge");
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
int gpio_fd_open(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0) {
syslog(LOG_ERR, "gpio/fd_open failed: %i", gpio);
perror("gpio/fd_open");
}
return fd;
}
int gpio_fd_close(int fd)
{
return close(fd);
}
static void sighandler(int sig)
{
syslog(LOG_DEBUG, "Signal Handler called\n");
switch(sig)
{
case SIGINT:
case SIGTERM:
gpio_set_value(GPIO_IR_PIN1, 1);
gpio_set_value(GPIO_IR_PIN2, 0);
exit(EXIT_SUCCESS);
break;
}
}
/*
* Main
* There are two GPIOs for the IR LED/Filter control.
* They are coded, meaning:
* 0 0 .. IR LEDs ON, filter OUT
* 0 1 .. IR LEDs ON, no active filter positioning
* 1 0 .. IR LEDs OFF, no active filter positioning
* 1 1 .. IR LEDs OFF, filter IN
*
* In order to reduce heat, the ICR positioning should be
* stopped after some time. Periodic repositioning of the filter
* is scheduled.
*/
int main(int argc, char **argv, char **envp)
{
struct pollfd fdset[1];
int nfds = 1;
int gpio_fd, timeout, rc, count, len, i;
char ch;
int dayMode = 0;
unsigned int gpio;
int releaseICR = 0;
char buf[MAX_BUF];
int goDaemon = 1;
int debug = 0;
pid_t pid;
pid_t sid;
// Parse command line arguments
while((i = getopt(argc, argv, "fc:d")) != -1)
{
switch(i)
{
case 'f':
goDaemon = 0;
break;
case 'd':
debug = 1;
goDaemon = 0;
break;
case '?':
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
fprintf(stderr, "Usage: %s [-f] [-d]\n", argv[0]);
fprintf(stderr, " where\n");
fprintf(stderr, " -f don't detach\n");
fprintf(stderr, " -d debug (implies -f)\n");
return EXIT_FAILURE;
}
}
// Setup syslog
if(debug)
setlogmask(LOG_UPTO(LOG_DEBUG));
else
setlogmask(LOG_UPTO(LOG_INFO));
if(goDaemon)
openlog("lightSensor-daemon", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
else
openlog("lightSensor-daemon", LOG_CONS | LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_LOCAL1);
if(goDaemon)
{
pid = fork();
if(pid < 0)
{
syslog(LOG_ERR, "Forking failed.\n");
return EXIT_FAILURE;
}
if(pid > 0)
{
return EXIT_SUCCESS;
}
// From here on we are the child process...
umask(0);
sid = setsid();
if(sid < 0)
{
syslog(LOG_ERR, "Could not create process group\n");
return EXIT_FAILURE;
}
if((chdir("/")) < 0)
{
syslog(LOG_ERR, "Could not chdir(\"/\")\n");
return EXIT_FAILURE;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
// Register some signal handlers
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
gpio_export(GPIO_LIGHT);
gpio_export(GPIO_IR_PIN1);
gpio_export(GPIO_IR_PIN2);
gpio_set_dir(GPIO_LIGHT, 0);
gpio_set_dir(GPIO_IR_PIN1, 1);
gpio_set_dir(GPIO_IR_PIN2, 1);
gpio_set_edge(GPIO_LIGHT, "both");
gpio_fd = gpio_fd_open(GPIO_LIGHT);
gpio_get_value(GPIO_LIGHT, &dayMode);
timeout = POLL_TIMEOUT;
count = 0; // Set to 0 for initial setup based on initial read of light sensor
syslog(LOG_INFO, "lightSensor-daemon starting up...");
while (1) {
memset((void*)fdset, 0, sizeof(fdset));
if(count == 0) // This is the periodic re-positioning of the filter
{
syslog(LOG_DEBUG, "Periodic set called");
count = (300 * 1000) / timeout; // 5 minutes
if(dayMode)
{
gpio_set_value(GPIO_IR_PIN1, 1);
gpio_set_value(GPIO_IR_PIN2, 1);
}
else
{
gpio_set_value(GPIO_IR_PIN1, 0);
gpio_set_value(GPIO_IR_PIN2, 0);
}
releaseICR = 1;
}
fdset[0].fd = gpio_fd;
fdset[0].events = POLLPRI;
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
syslog(LOG_ERR, "poll() failed");
return -1;
}
if (rc == 0) { // timeout
count--;
if(releaseICR)
{
syslog(LOG_DEBUG, "Release ICR called");
if(dayMode)
{
gpio_set_value(GPIO_IR_PIN1, 1);
gpio_set_value(GPIO_IR_PIN2, 0);
}
else
{
gpio_set_value(GPIO_IR_PIN1, 0);
gpio_set_value(GPIO_IR_PIN2, 1);
}
releaseICR = 0;
}
}
// POLLPRI executed, an interrupt occured, something changed
if (fdset[0].revents & POLLPRI) {
lseek(fdset[0].fd, 0, SEEK_SET);
len = read(fdset[0].fd, buf, MAX_BUF);
if((len == 2) && (buf[0] != '0'))
{
syslog(LOG_DEBUG, "Light sensor reported day");
// Light sensor went high -> day
if(!dayMode)
{
gpio_set_value(GPIO_IR_PIN1, 1);
gpio_set_value(GPIO_IR_PIN2, 1);
dayMode = 1;
}
}
else
{
syslog(LOG_DEBUG, "Light sensor reported night");
// Light sensor went low -> night
if(dayMode)
{
gpio_set_value(GPIO_IR_PIN1, 0);
gpio_set_value(GPIO_IR_PIN2, 0);
dayMode = 0;
}
}
releaseICR = 1;
}
}
gpio_fd_close(gpio_fd);
return 0;
}