This repository was archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathread_conf_file.c
More file actions
executable file
·402 lines (352 loc) · 12 KB
/
read_conf_file.c
File metadata and controls
executable file
·402 lines (352 loc) · 12 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
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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <linux/input.h>
#include "read_conf_file.h"
#include <assert.h>
/****************************************************************
* Defines
****************************************************************/
#define die(str, args...) do { \
perror(str); \
return(EXIT_FAILURE); \
} while(0)
#define DEBUG_READ_CONF_FILE_PRINTF (0)
#define ERROR_READ_CONF_FILE_PRINTF (1)
#if (DEBUG_READ_CONF_FILE_PRINTF)
#define READ_CONF_FILE_PRINTF(...) printf(__VA_ARGS__);
#else
#define READ_CONF_FILE_PRINTF(...)
#endif
#if (ERROR_READ_CONF_FILE_PRINTF)
#define ERROR_CONF_FILE_PRINTF(...) printf(__VA_ARGS__);
#else
#define ERROR_CONF_FILE_PRINTF(...)
#endif
/****************************************************************
* Extern variables
****************************************************************/
extern key_names_s key_names[];
/****************************************************************
* Static variables
****************************************************************/
static STRUCT_MAPPED_GPIO * head_chained_list_mapping_gpio = NULL;
static int count_valid_gpios = 0;
static int gpio_pins[MAX_NUM_GPIO];
static bool gpio_active_high[MAX_NUM_GPIO];
/****************************************************************
* Static functions
****************************************************************/
/* Return linux Key code from corresponding str*/
static int find_key(const char *name)
{
int i=0;
while(key_names[i].code >= 0){
if(!strncmp(key_names[i].name, name, 32))break;
i++;
}
if(key_names[i].code < 0){
i = 0;
}
return i;
}
/* Return pin idx in the array of instanciated pins */
static int get_idx_pin(int* ptr_list_gpio_pins, int size_list_gpio_pins, int pin_number_to_find){
int idx;
for (idx=0; idx<size_list_gpio_pins; idx++){
if(ptr_list_gpio_pins[idx] == pin_number_to_find){
return idx;
}
}
// if we get here, then we did not find the pin:
idx=-1;
return idx;
}
/* function to swap data of two nodes a and b in chained list */
static void swap(STRUCT_MAPPED_GPIO *a, STRUCT_MAPPED_GPIO *b)
{
STRUCT_MAPPED_GPIO temp;
memcpy(&temp, a, sizeof(STRUCT_MAPPED_GPIO));
memcpy(a, b, sizeof(STRUCT_MAPPED_GPIO));
memcpy(b, &temp, sizeof(STRUCT_MAPPED_GPIO));
b->next_mapped_gpio = a->next_mapped_gpio;
a->next_mapped_gpio = b;
}
/* Bubble sort the given linked list */
static void bubbleSort(STRUCT_MAPPED_GPIO *head)
{
int swapped, i;
STRUCT_MAPPED_GPIO *ptr1 = head;
STRUCT_MAPPED_GPIO *lptr = NULL;
/* Checking for empty list */
if (ptr1 == NULL)
return;
do
{
swapped = 0;
ptr1 = head;
while (ptr1->next_mapped_gpio != lptr)
{
if (ptr1->nb_simultaneous_pins < ptr1->next_mapped_gpio->nb_simultaneous_pins)
{
swap(ptr1, ptr1->next_mapped_gpio);
swapped = 1;
}
ptr1 = ptr1->next_mapped_gpio;
}
lptr = ptr1;
}
while (swapped);
}
/* Push new node in chained list */
static void push(STRUCT_MAPPED_GPIO ** head, STRUCT_MAPPED_GPIO * new_mapping) {
STRUCT_MAPPED_GPIO ** current = head;
while (*current != NULL) {
current = &(*current)->next_mapped_gpio;
}
/* now we can add a new variable */
*current = malloc(sizeof(STRUCT_MAPPED_GPIO));
memcpy(*current, new_mapping, sizeof(STRUCT_MAPPED_GPIO));
(*current)->next_mapped_gpio = NULL;
}
/****************************************************************
* Public functions
****************************************************************/
void get_mapping_from_conf_file(STRUCT_MAPPED_GPIO ** chained_list_mapping,
int* nb_valid_gpios, int ** valid_gpio_pins, bool ** gpio_pins_active_high){
/* Variables */
READ_CONF_FILE_PRINTF("Reading the config file:\n");
FILE *fp;
char ln[MAX_LN_LENGTH];
int lnno = 0;
char conffile[80];
bool pins_declaration_line = true;
/* search for the conf file in ~/.funkey_gpio_mapping.conf and /etc/funkey_gpio_mapping.conf */
sprintf(conffile, "%s/.funkey_gpio_mapping.conf", getenv("HOME"));
fp = fopen(conffile, "r");
if(!fp){
fp = fopen("/etc/funkey_gpio_mapping.conf", "r");
if(!fp){
fp = fopen("./funkey_gpio_mapping.conf", "r");
if(!fp){
perror(conffile);
perror("/etc/funkey_gpio_mapping.conf");
perror("./funkey_gpio_mapping.conf");
}
sprintf(conffile, "./funkey_gpio_mapping.conf");
}
else{
sprintf(conffile, "/etc/funkey_gpio_mapping.conf");
}
}
if(fp){
printf("GPIO config file used: %s\n", conffile);
}
/* Main loop to read conf file (purposely exploded and not in multiple sub-functions) */
while(fp && !feof(fp)){
if(fgets(ln, MAX_LN_LENGTH, fp)){
lnno++;
if(strlen(ln) > 1){
if(ln[0]=='#')continue;
READ_CONF_FILE_PRINTF("\nCurrent line : %s\n", ln);
// ************ Pins declaration ************
if(pins_declaration_line){
char* token;
int token_int;
//count nb valid GPIOs and store them
token = strtok(ln, ",");
while(token != NULL){
// Search for char '*' which means active low
bool cur_active_high = true;
char *substr = strchr(token, '*');
if(substr != NULL){
// Save polarity and set this char to \0 to end string
cur_active_high = false;
substr[0] = 0;
}
// Read pin idx
token_int = atoi(token);
if(token_int || !strcmp(token, "0")){
gpio_pins[count_valid_gpios] = token_int;
gpio_active_high[count_valid_gpios] = cur_active_high;
count_valid_gpios++;
READ_CONF_FILE_PRINTF("GPIO %d declared - active %s\n", token_int, cur_active_high?"high":"low");
}
else{
ERROR_CONF_FILE_PRINTF("Could not declare GPIO: %s\n", token);
}
token = strtok(NULL, ",");
}
pins_declaration_line = false;
}
// ************ Pins mapping ************
else{
STRUCT_MAPPED_GPIO cur_gpio_mapping;
char* end_ln;
char* token_comma;
char * current_arg;
int cur_arg_idx = MAPPING_ARG_PINS;
int idx_pin = 0;
int cur_pin;
bool add_current_mapping = true;
token_comma = strtok_r(ln, ",", &end_ln);
while(token_comma != NULL && cur_arg_idx < NB_MAPPING_ARG_NAMES && add_current_mapping){
current_arg= token_comma;
//remove_initial_spaces:
while(*current_arg==' ' && *current_arg!=0){current_arg++;}
READ_CONF_FILE_PRINTF(" Current arg: %s\n", current_arg);
//Handle current mapping arg
switch(cur_arg_idx){
case MAPPING_ARG_PINS:
//count nb valid GPIOs and store them
;char* end_arg;
char * token_plus = strtok_r(current_arg, "+", &end_arg);
while(token_plus != NULL){
cur_pin = atoi(token_plus);
if(cur_pin || !strcmp(token_plus, "0")){
int idx_cur_pin = get_idx_pin(gpio_pins, count_valid_gpios, cur_pin);
if(idx_cur_pin == -1){
ERROR_CONF_FILE_PRINTF(" Could not find GPIO: %s in previously instanciated GPIOs\n", token_plus);
add_current_mapping = false;
break;
}
cur_gpio_mapping.pins_idx[idx_pin] = idx_cur_pin;
idx_pin++;
READ_CONF_FILE_PRINTF(" GPIO %d in current mapping\n", cur_pin);
}
else{
ERROR_CONF_FILE_PRINTF(" Could not find GPIO: %s\n", token_plus);
add_current_mapping = false;
break;
}
token_plus = strtok_r(NULL, "+", &end_arg);//strtok_r(NULL, ",");
}
cur_gpio_mapping.nb_simultaneous_pins = idx_pin;
break;
case MAPPING_ARG_TYPE:
if(!strcmp(current_arg, "KEYBOARD")){
cur_gpio_mapping.type_mapping = TYPE_MAPPING_KEYBOARD;
}
else if(!strcmp(current_arg, "SHELL_COMMAND")){
cur_gpio_mapping.type_mapping = TYPE_MAPPING_SHELL_COMMAND;
}
else{
ERROR_CONF_FILE_PRINTF(" %s is not a valid mapping type\n", current_arg);
add_current_mapping = false;
break;
}
break;
case MAPPING_ARG_VALUE:
switch (cur_gpio_mapping.type_mapping){
case TYPE_MAPPING_KEYBOARD:
cur_gpio_mapping.key_value = find_key(current_arg);
if(!cur_gpio_mapping.key_value){
ERROR_CONF_FILE_PRINTF(" Could not find Key: %s\n", current_arg);
add_current_mapping = false;
}
break;
case TYPE_MAPPING_SHELL_COMMAND:
cur_gpio_mapping.shell_command = malloc(strlen(current_arg)*sizeof(char));
strcpy(cur_gpio_mapping.shell_command, current_arg);
break;
default:
ERROR_CONF_FILE_PRINTF(" %d is not a valid mapping type\n", cur_gpio_mapping.type_mapping);
add_current_mapping = false;
break;
}
break;
case MAPPING_ARG_STR_HELP_PIN_NAME:
if(!strlen(current_arg)) continue;
cur_gpio_mapping.pin_help_str = malloc(strlen(current_arg)*sizeof(char));
strcpy(cur_gpio_mapping.pin_help_str, current_arg);
break;
case MAPPING_ARG_STR_HELP_PIN_FCT:
if(!strlen(current_arg)) continue;
cur_gpio_mapping.fct_help_str = malloc(strlen(current_arg)*sizeof(char));
strcpy(cur_gpio_mapping.fct_help_str, current_arg);
break;
default:
break;
}
//next arg
token_comma = strtok_r(NULL, ",", &end_ln);
cur_arg_idx++;
}
if(add_current_mapping){
push(&head_chained_list_mapping_gpio, &cur_gpio_mapping);
READ_CONF_FILE_PRINTF("Current Mapping added successfully\n\n");
}
else{
ERROR_CONF_FILE_PRINTF("Current Mapping not added\n\n");
}
}
}
}
}
READ_CONF_FILE_PRINTF("Sorting Chained list...\n");
bubbleSort(head_chained_list_mapping_gpio);
print_all_chained_list(head_chained_list_mapping_gpio);
print_gpios(gpio_pins, count_valid_gpios);
if(fp){
fclose(fp);
}
/* Return parameters */
*chained_list_mapping = head_chained_list_mapping_gpio;
*nb_valid_gpios = count_valid_gpios;
*valid_gpio_pins = gpio_pins;
*gpio_pins_active_high = gpio_active_high;
}
/* Print all chained list content */
void print_all_chained_list(STRUCT_MAPPED_GPIO * head){
STRUCT_MAPPED_GPIO * current = head;
int idx = 0;
READ_CONF_FILE_PRINTF("Printing Chained list:\n ");
while (current != NULL) {
READ_CONF_FILE_PRINTF("Chained list Mapping GPIOs, idx: %d\n", idx);
print_chained_list_node(current);
idx++;
current = current->next_mapped_gpio;
}
}
/* Print one chained list node */
void print_chained_list_node(STRUCT_MAPPED_GPIO * node){
// Print Pins:
if(node->nb_simultaneous_pins > 1){
READ_CONF_FILE_PRINTF(" %d Simultaneous pins at following idx: [", node->nb_simultaneous_pins);
int i;
for (i=0; i < node->nb_simultaneous_pins; i++){
READ_CONF_FILE_PRINTF("%d%s", node->pins_idx[i], (i==node->nb_simultaneous_pins-1)?"]\n":",");
}
}
else{
READ_CONF_FILE_PRINTF(" idx pin: %d\n", node->pins_idx[0]);
}
// Type mapping and Value
if (node->type_mapping == TYPE_MAPPING_KEYBOARD){
READ_CONF_FILE_PRINTF(" Type mapping: TYPE_MAPPING_KEYBOARD\n");
READ_CONF_FILE_PRINTF(" Key: %d\n", node->key_value);
}
else if (node->type_mapping == TYPE_MAPPING_SHELL_COMMAND){
READ_CONF_FILE_PRINTF(" Type mapping: TYPE_MAPPING_SHELL_COMMAND\n");
READ_CONF_FILE_PRINTF(" Shell command: %s\n", node->shell_command);
}
// Pin help String
if(node->pin_help_str[0]){
READ_CONF_FILE_PRINTF(" Pins: %s\n", node->pin_help_str);
}
// Function help String
if(node->fct_help_str[0]){
READ_CONF_FILE_PRINTF(" Function: %s\n", node->fct_help_str);
}
}
void print_gpios(int * valid_gpio_pins, int nb_valid_gpios){
READ_CONF_FILE_PRINTF("GPIO pins:\n[");
for(int i = 0; i< nb_valid_gpios; i++) {
READ_CONF_FILE_PRINTF("%d%s", valid_gpio_pins[i], (i==nb_valid_gpios-1)?"]\n":",");
}
}