30 #include <sys/socket.h> 33 #include <sys/types.h> 37 #ifdef HAVE_KERNEL_LIRC_H 38 #include <linux/lirc.h> 40 #include "include/media/lirc.h" 43 #include "lirc/lirc_log.h" 44 #include "lirc/lirc_options.h" 45 #include "lirc/ir_remote.h" 46 #include "lirc/config_file.h" 47 #include "lirc/transmit.h" 48 #include "lirc/config_flags.h" 53 enum directive { ID_none, ID_remote, ID_codes, ID_raw_codes, ID_raw_name };
70 typedef void* (*array_guest_func)(
void* item,
void* arg);
74 #define MAX_INCLUDES 10 76 const char* whitespace =
" \t";
79 static int parse_error;
81 static struct ir_remote* read_config_recursive(FILE* f,
const char*
name,
int depth);
82 static void calculate_signal_lengths(
struct ir_remote* remote);
84 void** init_void_array(
struct void_array* ar,
size_t chunk_size,
size_t item_size)
86 ar->chunk_size = chunk_size;
87 ar->item_size = item_size;
89 ar->ptr = calloc(chunk_size, ar->item_size);
112 {
"REVERSE", REVERSE },
127 if ((ar->nr_items % ar->chunk_size) == (ar->chunk_size) - 1) {
130 ptr = realloc(ar->ptr,
132 (ar->nr_items + ar->chunk_size + 1));
140 memcpy((ar->ptr) + (ar->item_size * ar->nr_items), dataptr, ar->item_size);
141 ar->nr_items = (ar->nr_items) + 1;
142 memset((ar->ptr) + (ar->item_size * ar->nr_items), 0, ar->item_size);
164 for (i = 0; i < ar->nr_items; i += 1) {
165 r = func(ar->ptr + (i * ar->item_size), arg);
176 if (node1 == NULL || node2 == NULL)
177 return node1 == node2;
178 return node1->code == node2->code;
186 static void* array_guest_code_equals(
void* arg1,
void* arg2)
194 if (code1 == NULL || code2 == NULL)
195 return code1 == code2 ? arg1 : NULL;
200 while (code1->
next != NULL) {
201 if (!ir_code_node_equals(next1, next2))
214 static void* array_guest_ncode_cmp(
void* item,
void* arg)
220 if (strcmp(code1->
name, code2->
name) == 0)
226 void* s_malloc(
size_t size)
236 memset(ptr, 0, size);
240 char* s_strdup(
char*
string)
244 ptr = strdup(
string);
253 ir_code s_strtocode(
const char* val)
259 code = strtoull(val, &endptr, 0);
260 if ((code == (__u64) -1 && errno == ERANGE) || strlen(endptr) != 0 || strlen(val) == 0) {
261 log_error(
"error in configfile line %d:", line);
262 log_error(
"\"%s\": must be a valid (__u64) number", val);
269 __u32 s_strtou32(
char* val)
274 n = strtoul(val, &endptr, 0);
275 if (!*val || *endptr) {
276 log_error(
"error in configfile line %d:", line);
277 log_error(
"\"%s\": must be a valid (__u32) number", val);
284 int s_strtoi(
char* val)
290 n = strtol(val, &endptr, 0);
292 if (!*val || *endptr || n != ((
long)h)) {
293 log_error(
"error in configfile line %d:", line);
294 log_error(
"\"%s\": must be a valid (int) number", val);
301 unsigned int s_strtoui(
char* val)
307 n = strtoul(val, &endptr, 0);
309 if (!*val || *endptr || n != ((__u32)h)) {
310 log_error(
"error in configfile line %d:", line);
311 log_error(
"\"%s\": must be a valid (unsigned int) number", val);
318 lirc_t s_strtolirc_t(
char* val)
324 n = strtoul(val, &endptr, 0);
326 if (!*val || *endptr || n != ((__u32)h)) {
327 log_error(
"error in configfile line %d:", line);
328 log_error(
"\"%s\": must be a valid (lirc_t) number", val);
333 log_warn(
"error in configfile line %d:", line);
334 log_warn(
"\"%s\" is out of range", val);
339 int checkMode(
int is_mode,
int c_mode,
char* error)
341 if (is_mode != c_mode) {
342 log_error(
"fatal error in configfile line %d:", line);
343 log_error(
"\"%s\" isn't valid at this position", error);
350 int addSignal(
struct void_array*
signals,
char* val)
365 memset(code, 0,
sizeof(*code));
366 code->
name = s_strdup(key);
367 code->
code = s_strtocode(val);
376 node = s_malloc(
sizeof(*node));
380 node->code = s_strtocode(val);
395 int parseFlags(
char* val)
403 while (flag != NULL) {
404 while (*help !=
'|' && *help != 0)
414 while (flaglptr->
name != NULL) {
415 if (strcasecmp(flaglptr->
name, flag) == 0) {
416 if (flaglptr->
flag & IR_PROTOCOL_MASK && flags & IR_PROTOCOL_MASK) {
417 log_error(
"error in configfile line %d:", line);
418 log_error(
"multiple protocols given in flags: \"%s\"", flag);
422 flags = flags | flaglptr->
flag;
428 if (flaglptr->
name == NULL) {
429 log_error(
"error in configfile line %d:", line);
441 int defineRemote(
char* key,
char* val,
char* val2,
struct ir_remote* rem)
443 if ((strcasecmp(
"name", key)) == 0) {
444 if (rem->
name != NULL)
445 free((
void*)(rem->
name));
446 rem->
name = s_strdup(val);
450 if (options_getboolean(
"lircd:dynamic-codes")) {
451 if ((strcasecmp(
"dyncodes_name", key)) == 0) {
457 }
else if (strcasecmp(
"driver", key) == 0) {
459 free((
void*)(rem->
driver));
460 rem->
driver = s_strdup(val);
462 }
else if ((strcasecmp(
"bits", key)) == 0) {
463 rem->
bits = s_strtoi(val);
465 }
else if (strcasecmp(
"flags", key) == 0) {
466 rem->
flags |= parseFlags(val);
468 }
else if (strcasecmp(
"eps", key) == 0) {
469 rem->
eps = s_strtoi(val);
471 }
else if (strcasecmp(
"aeps", key) == 0) {
472 rem->
aeps = s_strtoi(val);
474 }
else if (strcasecmp(
"plead", key) == 0) {
475 rem->
plead = s_strtolirc_t(val);
477 }
else if (strcasecmp(
"ptrail", key) == 0) {
478 rem->
ptrail = s_strtolirc_t(val);
480 }
else if (strcasecmp(
"pre_data_bits", key) == 0) {
483 }
else if (strcasecmp(
"pre_data", key) == 0) {
486 }
else if (strcasecmp(
"post_data_bits", key) == 0) {
489 }
else if (strcasecmp(
"post_data", key) == 0) {
492 }
else if (strcasecmp(
"gap", key) == 0) {
494 rem->
gap2 = s_strtou32(val2);
495 rem->
gap = s_strtou32(val);
496 return val2 != NULL ? 2 : 1;
497 }
else if (strcasecmp(
"repeat_gap", key) == 0) {
500 }
else if (strcasecmp(
"repeat_mask", key) == 0) {
505 else if (strcasecmp(
"toggle_bit", key) == 0) {
508 }
else if (strcasecmp(
"toggle_bit_mask", key) == 0) {
511 }
else if (strcasecmp(
"toggle_mask", key) == 0) {
514 }
else if (strcasecmp(
"rc6_mask", key) == 0) {
517 }
else if (strcasecmp(
"ignore_mask", key) == 0) {
520 }
else if (strcasecmp(
"manual_sort", key) == 0) {
525 else if (strcasecmp(
"repeat_bit", key) == 0) {
528 }
else if (strcasecmp(
"suppress_repeat", key) == 0) {
531 }
else if (strcasecmp(
"min_repeat", key) == 0) {
534 }
else if (strcasecmp(
"min_code_repeat", key) == 0) {
537 }
else if (strcasecmp(
"frequency", key) == 0) {
538 rem->
freq = s_strtoui(val);
540 }
else if (strcasecmp(
"duty_cycle", key) == 0) {
543 }
else if (strcasecmp(
"baud", key) == 0) {
544 rem->
baud = s_strtoui(val);
546 }
else if (strcasecmp(
"serial_mode", key) == 0) {
547 if (val[0] <
'5' || val[0] >
'9') {
548 log_error(
"error in configfile line %d:", line);
554 switch (toupper(val[1])) {
556 rem->
parity = IR_PARITY_NONE;
559 rem->
parity = IR_PARITY_EVEN;
562 rem->
parity = IR_PARITY_ODD;
565 log_error(
"error in configfile line %d:", line);
570 if (strcmp(val + 2,
"1.5") == 0)
575 }
else if (val2 != NULL) {
576 if (strcasecmp(
"header", key) == 0) {
577 rem->phead = s_strtolirc_t(val);
578 rem->
shead = s_strtolirc_t(val2);
580 }
else if (strcasecmp(
"three", key) == 0) {
581 rem->pthree = s_strtolirc_t(val);
582 rem->
sthree = s_strtolirc_t(val2);
584 }
else if (strcasecmp(
"two", key) == 0) {
585 rem->ptwo = s_strtolirc_t(val);
586 rem->
stwo = s_strtolirc_t(val2);
588 }
else if (strcasecmp(
"one", key) == 0) {
589 rem->pone = s_strtolirc_t(val);
590 rem->
sone = s_strtolirc_t(val2);
592 }
else if (strcasecmp(
"zero", key) == 0) {
593 rem->pzero = s_strtolirc_t(val);
594 rem->
szero = s_strtolirc_t(val2);
596 }
else if (strcasecmp(
"foot", key) == 0) {
597 rem->pfoot = s_strtolirc_t(val);
598 rem->
sfoot = s_strtolirc_t(val2);
600 }
else if (strcasecmp(
"repeat", key) == 0) {
601 rem->prepeat = s_strtolirc_t(val);
602 rem->
srepeat = s_strtolirc_t(val2);
604 }
else if (strcasecmp(
"pre", key) == 0) {
605 rem->pre_p = s_strtolirc_t(val);
606 rem->
pre_s = s_strtolirc_t(val2);
608 }
else if (strcasecmp(
"post", key) == 0) {
609 rem->post_p = s_strtolirc_t(val);
610 rem->
post_s = s_strtolirc_t(val2);
615 log_error(
"error in configfile line %d:", line);
616 log_error(
"unknown definiton: \"%s %s %s\"", key, val, val2);
618 log_error(
"error in configfile line %d:", line);
619 log_error(
"unknown definiton or too few arguments: \"%s %s\"", key, val);
625 static int sanityChecks(
struct ir_remote* rem,
const char* path)
630 path = path != NULL ? path :
"unknown file";
633 log_error(
"%s: %s: Missing remote name", path, rem);
637 log_warn(
"%s: %s: Gap value missing or invalid",
640 if (has_repeat_gap(rem) && is_const(rem)) {
641 log_warn(
"%s: %s: Repeat_gap ignored (CONST_LENGTH is set)",
650 "%s: %s: Invalid pre_data", path, rem->
name);
654 log_warn(
"%s: %s: Invalid post_data",
658 for (codes = rem->codes; codes->
name != NULL; codes++) {
659 if ((codes->
code & gen_mask(rem->
bits)) != codes->
code) {
660 log_warn(
"%s: %s: Invalid code : %s",
664 for (node = codes->
next; node != NULL; node = node->next) {
665 if ((node->code & gen_mask(rem->
bits)) != node->code) {
666 log_warn(
"%s: %s: Invalid code %s: %s",
668 node->code &= gen_mask(rem->
bits);
687 int r1_is_raw = is_raw(r1);
688 int r2_is_raw = is_raw(r2);
690 if (!r1_is_raw && r2_is_raw)
692 if (r1_is_raw && !r2_is_raw)
695 if (r1_is_raw && r2_is_raw) {
696 for (c = r1->codes, r1_size = 0; c->
name != NULL; c++)
698 for (c = r2->codes, r2_size = 0; c->
name != NULL; c++)
701 r1_size = bit_count(r1);
702 r2_size = bit_count(r2);
704 if (r1_size == r2_size)
706 return r1_size < r2_size ? -1 : 1;
723 for (r = remotes; r != NULL && r != (
void*)-1; r = r->next)
728 while (rem != NULL && rem != (
void*)-1) {
733 while (scan && remote_bits_cmp(scan, rem) <= 0) {
752 static const char* lirc_parse_include(
char* s)
761 while (last > s && strchr(whitespace, *last) != NULL)
765 if (*s !=
'"' && *s !=
'<')
767 if (*s ==
'"' && *last !=
'"')
769 else if (*s ==
'<' && *last !=
'>')
772 memmove(s, s + 1, len - 2 + 1);
779 static const char* lirc_parse_relative(
char* dst,
794 if (strlen(current) >= dst_size)
796 strcpy(dst, current);
798 dirlen = strlen(dir);
800 memmove(dst, dir, dirlen + 1);
802 if (dirlen + 1 + strlen(child) + 1 > dst_size)
821 if (root == NULL && what != NULL)
825 for (r = root; r->next != NULL; r = r->next)
836 head = read_config_recursive(f, name, 0);
837 head = sort_by_bit_count(head);
853 read_included(
const char*
name,
int depth,
char* val,
struct ir_remote* top_rem)
856 const char* childName;
859 if (depth > MAX_INCLUDES) {
860 log_error(
"error opening child file defined at %s:%d", name, line);
864 childName = lirc_parse_include(val);
866 log_error(
"error parsing child file value defined at line %d:", line);
870 childFile = fopen(childName,
"r");
871 if (childFile == NULL) {
872 log_error(
"error opening child file '%s' defined at line %d:",
874 log_error(
"ignoring this child file for now.");
877 rem = read_config_recursive(childFile, childName, depth + 1);
878 top_rem = ir_remotes_append(top_rem, rem);
894 static struct ir_remote* read_all_included(
const char* name,
901 char buff[256] = {
'\0' };
903 memset(&globbuf, 0,
sizeof(globbuf));
905 val[strlen(val) - 1] =
'\0';
906 lirc_parse_relative(buff,
sizeof(buff), val, name);
907 glob(buff, 0, NULL, &globbuf);
908 for (i = 0; i < globbuf.gl_pathc; i += 1) {
909 snprintf(buff,
sizeof(buff),
"\"%s\"", globbuf.gl_pathv[i]);
910 top_rem = read_included(name, depth, buff, top_rem);
917 static void check_ncode_dups(
const char* path,
919 struct void_array* ar,
922 if (foreach_void_array(ar, array_guest_ncode_cmp, code) != NULL) {
923 log_notice(
"%s: %s: Multiple definitions of: %s",
924 path, name, code->
name);
926 if (foreach_void_array(ar, array_guest_code_equals, code) != NULL) {
927 log_notice(
"%s: %s: Multiple values for same code: %s",
928 path, name, code->
name);
934 read_config_recursive(FILE* f,
const char* name,
int depth)
936 char buf[LINE_LEN + 1];
943 struct void_array codes_list, raw_codes, signals;
944 struct ir_ncode raw_code = { NULL, 0, 0, NULL };
945 struct ir_ncode name_code = { NULL, 0, 0, NULL };
953 while (fgets(buf, LINE_LEN, f) != NULL) {
956 if (len == LINE_LEN && buf[len - 1] !=
'\n') {
957 log_error(
"line %d too long in config file", line);
964 if (buf[len] ==
'\n')
969 if (buf[len] ==
'\r')
975 key = strtok(buf, whitespace);
979 val = strtok(NULL, whitespace);
981 val2 = strtok(NULL, whitespace);
982 log_trace2(
"Tokens: \"%s\" \"%s\" \"%s\"", key, val, (val2 == NULL ?
"(null)" : val));
983 if (strcasecmp(
"include", key) == 0) {
984 int save_line = line;
986 top_rem = read_all_included(name,
991 }
else if (strcasecmp(
"begin", key) == 0) {
992 if (strcasecmp(
"codes", val) == 0) {
995 if (!checkMode(mode, ID_remote,
"begin codes"))
998 log_error(
"error in configfile line %d:", line);
1004 init_void_array(&codes_list, 30,
sizeof(
struct ir_ncode));
1006 }
else if (strcasecmp(
"raw_codes", val) == 0) {
1009 if (!checkMode(mode, ID_remote,
"begin raw_codes"))
1012 log_error(
"error in configfile line %d:", line);
1019 init_void_array(&raw_codes, 30,
sizeof(
struct ir_ncode));
1020 mode = ID_raw_codes;
1021 }
else if (strcasecmp(
"remote", val) == 0) {
1024 if (!checkMode(mode, ID_none,
"begin remote"))
1030 rem = top_rem = s_malloc(
sizeof(
struct ir_remote));
1031 rem->
freq = DEFAULT_FREQ;
1035 rem = s_malloc(
sizeof(
struct ir_remote));
1036 rem->
freq = DEFAULT_FREQ;
1037 ir_remotes_append(top_rem, rem);
1039 }
else if (mode == ID_codes) {
1040 code = defineCode(key, val, &name_code);
1041 while (!parse_error && val2 != NULL) {
1044 defineNode(code, val2);
1045 val2 = strtok(NULL, whitespace);
1048 check_ncode_dups(name, rem->
name, &codes_list, code);
1051 log_error(
"error in configfile line %d:", line);
1052 log_error(
"unknown section \"%s\"", val);
1055 if (!parse_error && val2 != NULL) {
1056 log_warn(
"%s: garbage after '%s' token " 1057 "in line %d ignored",
1058 rem->
name, val, line);
1060 }
else if (strcasecmp(
"end", key) == 0) {
1061 if (strcasecmp(
"codes", val) == 0) {
1064 if (!checkMode(mode, ID_codes,
"end codes"))
1068 }
else if (strcasecmp(
"raw_codes", val) == 0) {
1072 if (mode == ID_raw_name) {
1074 raw_code.
length = signals.nr_items;
1075 if (raw_code.
length % 2 == 0) {
1076 log_error(
"error in configfile line %d:", line);
1082 mode = ID_raw_codes;
1084 if (!checkMode(mode, ID_raw_codes,
"end raw_codes"))
1088 }
else if (strcasecmp(
"remote", val) == 0) {
1092 if (!checkMode(mode, ID_remote,
"end remote"))
1094 if (!sanityChecks(rem, name)) {
1098 if (options_getboolean(
"lircd:dynamic-codes")) {
1109 }
else if (mode == ID_codes) {
1110 code = defineCode(key, val, &name_code);
1111 while (!parse_error && val2 != NULL) {
1114 defineNode(code, val2);
1115 val2 = strtok(NULL, whitespace);
1120 log_error(
"error in configfile line %d:", line);
1124 if (!parse_error && val2 != NULL) {
1126 "%s: garbage after '%s'" 1127 " token in line %d ignored",
1128 rem->
name, val, line);
1133 argc = defineRemote(key, val, val2, rem);
1135 && ((argc == 1 && val2 != NULL)
1136 || (argc == 2 && val2 != NULL && strtok(NULL, whitespace) != NULL))) {
1138 " token in line %d ignored",
1139 rem->
name, key, line);
1143 code = defineCode(key, val, &name_code);
1144 while (!parse_error && val2 != NULL) {
1147 defineNode(code, val2);
1148 val2 = strtok(NULL, whitespace);
1151 check_ncode_dups(name,
1159 if (strcasecmp(
"name", key) == 0) {
1161 if (mode == ID_raw_name) {
1163 raw_code.
length = signals.nr_items;
1164 if (raw_code.
length % 2 == 0) {
1165 log_error(
"error in configfile line %d:",
1173 raw_code.
name = s_strdup(val);
1177 init_void_array(&signals, 50,
sizeof(lirc_t));
1179 if (!parse_error && val2 != NULL) {
1181 " token in line %d ignored",
1182 rem->
name, key, line);
1185 if (mode == ID_raw_codes) {
1186 log_error(
"no name for signal defined at line %d",
1191 if (!addSignal(&signals, key))
1193 if (!addSignal(&signals, val))
1196 if (!addSignal(&signals, val2))
1198 while ((val = strtok(NULL, whitespace)))
1199 if (!addSignal(&signals, val))
1205 }
else if (mode == ID_raw_name) {
1206 if (!addSignal(&signals, key))
1209 log_error(
"error in configfile line %d", line);
1216 if (mode != ID_none) {
1219 if (raw_code.
name != NULL) {
1220 free(raw_code.
name);
1237 static int print_error = 1;
1240 log_error(
"reading of file '%s' failed", name);
1252 while (rem != NULL) {
1253 if ((!is_raw(rem)) && rem->
flags & REVERSE) {
1261 while (codes->
name != NULL) {
1272 int all_bits = bit_count(rem);
1277 int all_bits = bit_count(rem);
1279 if (has_toggle_bit_mask(rem)) {
1280 log_warn(
"%s uses both toggle_bit and toggle_bit_mask", rem->
name);
1286 if (has_toggle_bit_mask(rem)) {
1287 if (!is_raw(rem) && rem->codes) {
1289 if (rem->toggle_bit_mask_state)
1294 if (is_serial(rem)) {
1297 if (rem->
baud > 0) {
1298 base = 1000000 / rem->
baud;
1299 if (rem->pzero == 0 && rem->
szero == 0)
1301 if (rem->pone == 0 && rem->
sone == 0)
1309 log_warn(
"invalid min_code_repeat value");
1313 calculate_signal_lengths(rem);
1320 void calculate_signal_lengths(
struct ir_remote* remote)
1322 if (is_const(remote)) {
1330 lirc_t min_signal_length = 0, max_signal_length = 0;
1331 lirc_t max_pulse = 0, max_space = 0;
1333 struct ir_ncode* c = remote->codes;
1346 code.
code = next->code;
1349 for (repeat = 0; repeat < 2; repeat++) {
1350 if (init_sim(remote, &code, repeat)) {
1354 if (first_sum || sum < min_signal_length)
1355 min_signal_length = sum;
1356 if (first_sum || sum > max_signal_length)
1357 max_signal_length = sum;
1381 }
else if (is_const(remote)) {
1385 log_warn(
"min_gap_length is 0 for '%s' remote",
1392 log_warn(
"max_gap_length is 0 for '%s' remote", remote->
name);
1408 while (remotes != NULL) {
1409 next = remotes->next;
1413 if (remotes->
name != NULL)
1414 free((
void*)(remotes->
name));
1415 if (remotes->codes != NULL) {
1416 codes = remotes->codes;
1417 while (codes->
name != NULL) {
1426 next_node = node->next;
1432 free(remotes->codes);
const lirc_t * send_buffer_data(void)
lirc_t min_total_signal_length
void *(* array_guest_func)(void *item, void *arg)
lirc_t max_total_signal_length
struct ir_code_node * next
void free_config(struct ir_remote *remotes)
struct ir_ncode * last_code
#define log_warn(fmt,...)
#define log_trace2(fmt,...)
struct ir_code_node * current
#define log_trace1(fmt,...)
#define log_error(fmt,...)
lirc_t send_buffer_sum(void)
#define log_trace(fmt,...)
void * get_void_array(struct void_array *ar)
unsigned int min_code_repeat
const struct flaglist all_flags[]
struct ir_ncode dyncodes[2]
int send_buffer_length(void)
#define log_info(fmt,...)
struct ir_remote * read_config(FILE *f, const char *name)
#define log_notice(fmt,...)
unsigned int bits_in_byte
int add_void_array(struct void_array *ar, void *dataptr)