-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathirmplircd.c
403 lines (325 loc) · 9.42 KB
/
irmplircd.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
/*
irmplircd -- zeroconf LIRC daemon that reads IRMP events from the USB IR Remote Receiver
http://www.mikrocontroller.net/articles/USB_IR_Remote_Receiver
Copyright (C) 2011-2014 Dirk E. Wagner
based on:
inputlircd -- zeroconf LIRC daemon that reads from /dev/input/event devices
Copyright (C) 2006 Guus Sliepen <[email protected]>
This program is free software; you can redistribute it and/or modify it
under the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sysexits.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <pwd.h>
#include <ctype.h>
/* Input subsystem interface */
#include <linux/input.h>
#include "debug.h"
#include "hashmap.h"
#include "mapping.h"
typedef struct __attribute__ ((__packed__)) {
uint8_t report_id; // report id
uint8_t protocol; // protocol, i.e. NEC_PROTOCOL
uint16_t address; // address
uint16_t command; // command
uint8_t flags; // flags, e.g. repetition
} IRMP_DATA;
typedef struct evdev {
char *name;
int fd;
struct evdev *next;
} evdev_t;
static evdev_t *evdevs = NULL;
typedef struct client {
int fd;
struct client *next;
} client_t;
static client_t *clients = NULL;
static int sockfd = -1;
static bool grab = false;
static char *device = "/var/run/lirc/lircd";
static long repeat_delay = 0L;
static long repeat_period = 0L;
static int repeat = 0;
static map_t mymap;
/* returns time since 01.01.1970 */
static double getTime_ms(void) {
struct timeval sTime;
struct timezone tz;
double dTime_ms;
gettimeofday(&sTime, &tz);
dTime_ms=((double) sTime.tv_sec * (double)1000);
dTime_ms+=(sTime.tv_usec/1000);
return dTime_ms;
}
static void *xalloc(size_t size) {
void *buf = malloc(size);
if(!buf) {
fprintf(stderr, "Could not allocate %zd bytes with malloc(): %s\n", size, strerror(errno));
exit(EX_OSERR);
}
memset(buf, 0, size);
return buf;
}
static void add_evdevs(int argc, char *argv[]) {
int i;
evdev_t *newdev;
for(i = 0; i < argc; i++) {
newdev = xalloc(sizeof *newdev);
newdev->fd = open(argv[i], O_RDONLY);
if(newdev->fd < 0) {
free(newdev);
fprintf(stderr, "Could not open %s: %s\n", argv[i], strerror(errno));
continue;
}
if(grab) {
if(ioctl(newdev->fd, EVIOCGRAB, 1) < 0) {
close(newdev->fd);
free(newdev);
fprintf(stderr, "Failed to grab %s: %s\n", argv[i], strerror(errno));
continue;
}
}
newdev->name = basename(strdup(argv[i]));
newdev->next = evdevs;
evdevs = newdev;
}
}
static bool add_unixsocket(void) {
struct sockaddr_un sa = {0};
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(sockfd < 0) {
fprintf(stderr, "Unable to create an AF_UNIX socket: %s\n", strerror(errno));
return false;
}
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, device, sizeof sa.sun_path - 1);
unlink(device);
if(bind(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
fprintf(stderr, "Unable to bind AF_UNIX socket to %s: %s\n", device, strerror(errno));
return false;
}
chmod(device, 0666);
if(listen(sockfd, 3) < 0) {
fprintf(stderr, "Unable to listen on AF_UNIX socket: %s\n", strerror(errno));
return false;
}
return true;
}
static void processnewclient(void) {
client_t *newclient = xalloc(sizeof *newclient);
newclient->fd = accept(sockfd, NULL, NULL);
if(newclient->fd < 0) {
free(newclient);
if(errno == ECONNABORTED || errno == EINTR)
return;
syslog(LOG_ERR, "Error during accept(): %s\n", strerror(errno));
exit(EX_OSERR);
}
int flags = fcntl(newclient->fd, F_GETFL);
fcntl(newclient->fd, F_SETFL, flags | O_NONBLOCK);
newclient->next = clients;
clients = newclient;
}
static void processevent(evdev_t *evdev) {
IRMP_DATA event;
char hash_key[100];
char irmp_fulldata[100];
char message[100];
int len;
static double first_time = 0;
static double last_time = 0;
client_t *client, *prev, *next;
message[0]=0;
if((len=read(evdev->fd, &event, sizeof event)) <= 0) {
syslog(LOG_ERR, "Error processing event from %s: %s\n", evdev->name, strerror(errno));
exit(EX_OSERR);
}
DBG ("report_id = 0x%02d, p = %02d, a = 0x%04x, c = 0x%04x, f = 0x%02x\n", event.report_id, event.protocol, event.address, event.command, event.flags);
if(event.report_id != 0x01)
return;
if(event.flags == 0) {
first_time = getTime_ms();
repeat = 0;
} else {
if(((getTime_ms()-first_time) < repeat_delay) || (getTime_ms()-last_time) < repeat_period) {
return;
} else {
last_time=getTime_ms();
repeat++;
}
}
snprintf (irmp_fulldata, sizeof irmp_fulldata, "%02x%04x%04x%02x", event.protocol, event.address, event.command, 0);
snprintf (hash_key, sizeof hash_key, "%02x%04x%04x%02x", event.protocol, event.address, event.command, 0);
map_entry_t *map_entry;
if(hashmap_get(mymap, hash_key, (void**)(&map_entry))==MAP_OK) {
DBG ("MAP_OK irmpd_fulldata=%s lirc=%s\n", irmp_fulldata, map_entry->value);
len = snprintf(message, sizeof message, "%s %x %s %s\n", irmp_fulldata, repeat, map_entry->value, "IRMP");
} else {
DBG ("MAP_ERROR irmpd_fulldata=%s|\n", irmp_fulldata);
len = snprintf(message, sizeof message, "%s %x %s %s\n", irmp_fulldata, repeat, irmp_fulldata, "IRMP");
}
DBG ("LIRC message=%s\n", message);
for(client = clients; client; client = client->next) {
if(write(client->fd, message, len) != len) {
close(client->fd);
client->fd = -1;
}
}
for(prev = NULL, client = clients; client; client = next) {
next = client->next;
if(client->fd < 0) {
if(prev)
prev->next = client->next;
else
clients = client->next;
free(client);
} else {
prev = client;
}
}
}
static void print_help() {
printf("irmplircd [-d socket] [-f] [-c] [-r repeat-delay] [-s repeat-period] [-m keycode] -u username] device [device ...]\n\n");
printf("Options: \n");
printf("\t-d <socket> UNIX socket. The default is /var/run/lirc/lircd.\n");
printf("\t-f Run in the foreground.\n");
printf("\t-r <delay> Repeat delay in ms (delay for first repeat\n");
printf("\t-s <period> Repeat period in ms (delay for further repeats\n");
printf("\t-g Grab the input device(s).\n");
printf("\t-u <user> User name.\n");
printf("\t-t <path> Path to translation table.\n");
printf("\tdevice The input device e.g. /dev/hidraw0\n");
}
static void main_loop(void) {
fd_set permset;
fd_set fdset;
evdev_t *evdev;
int maxfd = 0;
FD_ZERO(&permset);
for(evdev = evdevs; evdev; evdev = evdev->next) {
FD_SET(evdev->fd, &permset);
if(evdev->fd > maxfd)
maxfd = evdev->fd;
}
FD_SET(sockfd, &permset);
if(sockfd > maxfd)
maxfd = sockfd;
maxfd++;
while(true) {
fdset = permset;
if(select(maxfd, &fdset, NULL, NULL, NULL) < 0) {
if(errno == EINTR)
continue;
syslog(LOG_ERR, "Error during select(): %s\n", strerror(errno));
exit(EX_OSERR);
}
for(evdev = evdevs; evdev; evdev = evdev->next)
if(FD_ISSET(evdev->fd, &fdset))
processevent(evdev);
if(FD_ISSET(sockfd, &fdset))
processnewclient();
}
}
int main(int argc, char *argv[]) {
char *user = "nobody";
char *translation_path = NULL;
int opt;
bool foreground = false;
bool use_translationtable = false;
while((opt = getopt(argc, argv, "d:gm:fu:r:s:t:")) != -1) {
switch(opt) {
case 'd':
device = strdup(optarg);
break;
case 'g':
grab = true;
break;
case 'u':
user = strdup(optarg);
break;
case 'f':
foreground = true;
break;
case 'r':
repeat_delay = atoi(optarg);
break;
case 's':
repeat_period = atoi(optarg);
break;
case 't':
use_translationtable = true;
translation_path = strdup(optarg);
break;
default:
print_help();
return EX_USAGE;
}
}
if(argc <= optind) {
fprintf(stderr, "Not enough arguments.\n");
return EX_USAGE;
}
add_evdevs(argc - optind, argv + optind);
if(!evdevs) {
fprintf(stderr, "Unable to open any event device!\n");
return EX_OSERR;
}
mymap = hashmap_new();
if(use_translationtable && !parse_translation_table(translation_path, mymap)) {
hashmap_free(mymap);
return EX_OSERR;
}
if (!add_unixsocket()) {
hashmap_free(mymap);
if (sockfd >= 0) close (sockfd);
return EX_OSERR;
}
struct passwd *pwd = getpwnam(user);
if(!pwd) {
fprintf(stderr, "Unable to resolve user %s!\n", user);
hashmap_free(mymap);
if (sockfd >= 0) close (sockfd);
return EX_OSERR;
}
if(setgid(pwd->pw_gid) || setuid(pwd->pw_uid)) {
fprintf(stderr, "Unable to setuid/setguid to %s!\n", user);
hashmap_free(mymap);
if (sockfd >= 0) close (sockfd);
return EX_OSERR;
}
if(!foreground)
daemon(0, 0);
syslog(LOG_INFO, "Started");
signal(SIGPIPE, SIG_IGN);
main_loop();
/* Now, destroy the map */
hashmap_free(mymap);
if (sockfd >= 0) close (sockfd);
return 0;
}