[f41b88]: drivers / irman / lirmand.c Maximize Restore History

Download this file

lirmand.c    323 lines (253 with data), 6.2 kB

  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
/* $Id: lirmand.c,v 1.1 1999/04/29 21:16:53 columbus Exp $ */
/* lirmand.c v0.5.5 (c) 1998-1999 Tom Wheeley <tomw@tsys.demon.co.uk> */
/* This program is free software. See file COPYING for details */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <irman.h>
/* default bit order is MSB_TO_LSB */
#ifndef LSB_TO_MSB
# ifndef MSB_TO_LSB /* neither defined */
# define MSB_TO_LSB 1
# endif
#else
# ifdef MSB_TO_LSB /* both defined */
# error "both LSB_TO_MSB and MSB_TO_LSB are defined. cannot cope"
# endif
#endif
/* how lazy... */
#define STRERR strerror(errno)
unsigned long gap = 0x00010000;
unsigned long pulse = 0x01000400;
unsigned long one_space = 0x00000c00;
unsigned long zero_space = 0x00000800;
char *progname = "lirmand";
/* these should be set by config.h (via acconfig.h) */
#ifndef LIRC_DRIVER_DEVICE
# define LIRC_DRIVER_DEVICE "/tmp/lirc"
#endif
#ifndef LIRMAND_LOGFILE
# define LIRMAND_LOGFILE "/tmp/lirmand-log"
#endif
int lirc = 0; /* file descriptor */
int is_daemon = 0;
FILE *log = NULL;
FILE *open_log(void)
{
time_t t=time(NULL);
if (!log) {
log = fopen(LIRMAND_LOGFILE, "a");
if (log) {
fprintf(log, "%s started at %s\n", progname, ctime(&t));
}
}
return log;
}
void close_log(void)
{
time_t t = time(NULL);
if (log) {
fprintf(log, "%s stopped at %s\n", progname, ctime(&t));
fclose(log);
}
log = NULL;
}
int debug_printf(char *format, ...)
{
va_list va;
int ret = 0;
if (!open_log())
return 0;
va_start(va, format);
ret = vfprintf(log, format, va);
if (!is_daemon) {
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, format, va);
}
va_end(va);
return ret;
}
/* print errors */
int eprintf(char *format, ...)
{
va_list va;
int ret = 0;
va_start(va, format);
if (open_log()) {
ret = fprintf(log, "error: ");
ret += vfprintf(log, format, va);
}
if (!is_daemon) {
ret = fprintf(stderr, "%s: ", progname);
ret += vfprintf(stderr, format, va);
}
va_end(va);
return ret;
}
/* Here I have attempted to implement REC-80 encoding, but it is quite likely
* I've not done it right as I wrote this without the specs in front of me
*/
void write_encoded_bit(int value)
{
if (value != 0) {
write(lirc, &one_space, sizeof one_space);
} else {
write(lirc, &zero_space, sizeof zero_space);
}
write(lirc, &pulse, sizeof pulse);
}
/* brrrr */
#ifdef MSB_TO_LSB
# define ORDER(X) (bit)
#else
# define ORDER(x) (numbits - bit - 1)
#endif
void write_encoded_uchar(unsigned char value, int numbits)
{
int bit;
int max = 1 << (numbits - 1);
for (bit=0; bit<numbits; bit++) {
write_encoded_bit((value & (max >> ORDER(bit))) ? 1 : 0);
}
}
RETSIGTYPE sigterm(int sig)
{
ir_finish();
close(lirc);
close_log();
raise(sig);
}
#ifdef DAEMONIZE
void daemonize(void)
{
pid_t pid;
if ((pid = fork()) < 0) { /* error */
eprintf("fork() failed: `%s'\n", STRERR);
exit(1);
} else if (pid) { /* parent */
printf("%s installed (%d)\n", progname, (int)pid);
exit(0);
} else { /* child */
is_daemon = 1;
fclose(stdin);
fclose(stdout);
fclose(stderr);
setsid();
chdir("/");
umask(0);
}
}
#endif
void loop()
{
unsigned char *code;
char *text = NULL;
int i;
errno = 0;
for(;;) {
code = ir_get_code();
if (code) {
text = ir_code_to_text(code);
debug_printf("rx `%s'\n", text);
write(lirc, &gap, sizeof gap);
write(lirc, &pulse, sizeof pulse);
/* irrecord expects >20% of the bits to be '1'.
The percentage in the code the Irman sends is too low.
We'll fix that by sending a dummy 'pre_data' consisting of 0xffff */
write_encoded_uchar(0xff, 8);
write_encoded_uchar(0xff, 8);
for(i = 0; i < IR_CODE_LEN; i++) {
write_encoded_uchar(code[i], 8);
}
/* error handling dullness */
} else {
if (errno == IR_EDUPCODE) {
debug_printf("rx `%s' (dup - ignored)\n", text?text:"(null - bug)");
} else {
if (errno == IR_EDISABLED) {
eprintf("irman not initialised (this is a bug)\n");
} else {
eprintf("error reading code: `%s'\n", ir_strerror(errno));
}
return;
}
}
}
}
int main(int argc, char **argv)
{
char *filename;
if (ir_init_commands(NULL, 1) < 0) {
fprintf(stderr, "error initialising irman software: %s\n", strerror(errno));
exit(1);
}
filename = ir_default_portname();
if (argc == 2) {
filename = argv[1];
} else {
if (!filename) {
fprintf(stderr, "usage: lirmand device\n");
exit(0);
}
}
errno = 0;
if (ir_init(filename) < 0) {
eprintf("error initialising irman hardware: `%s'\n", STRERR);
exit(1);
}
/* try and create the fifo we write to lircd through. If it exists then
* we either get EEXIST or EACCES, so we can ignore these errors. We get
* EACCES when it exists and we are not root. open() will throw up an error
* if we don't have permission to write, so we can ignore EACCES for now.
*/
if (mkfifo(LIRC_DRIVER_DEVICE, 0644) == -1) {
if(errno != EEXIST && errno != EACCES) {
eprintf("could not create fifo: `%s'\n", STRERR);
exit(1);
}
}
lirc = open(LIRC_DRIVER_DEVICE, O_RDWR|O_NONBLOCK);
if (lirc == -1) {
eprintf("could not open fifo: `%s'\n", STRERR);
exit(1);
}
/* borrowed from lircmd */
signal(SIGPIPE,SIG_IGN);
signal(SIGTERM,sigterm);
signal(SIGINT,sigterm);
#ifdef DAEMONIZE
daemonize();
#else
fprintf(stderr, "%s running (non daemon)\n", progname);
#endif
loop();
ir_finish();
close(lirc);
return 0;
}
/* end of lirmand.c */