-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigfile.c
339 lines (312 loc) · 6.84 KB
/
configfile.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
#include "configfile.h"
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "zigbee.h"
char* config_scriptName;
char* config_ttydevice;
char* config_gpio_reset;
char* config_device;
uint8_t* config_panID;
uint32_t config_nbPanID;
double config_altitude;
char* config_fifo_name;
uint16_t config_scan_channel;
char* config_gpio_ctrl_name;
uint32_t config_gpio_line;
static int32_t configfile_doRead(FILE* f);
static int32_t configfile_decodeLine(char line[]);
static int32_t configfile_createConfig(char key[], char value[]);
int32_t configfile_read(const char filename[])
{
FILE* f;
int32_t rc;
rc = -1;
f = fopen(filename, "r");
if (f == NULL)
{
fprintf(stderr, "unable to open %s\n", filename);
}
else
{
rc = configfile_doRead(f);
fclose(f);
}
return rc;
}
#define SIZE_CURRENT_LINE (256)
static int32_t configfile_doRead(FILE* f)
{
char currentLine[SIZE_CURRENT_LINE];
char* endRead;
int32_t rc;
uint32_t lineNo;
lineNo = 0;
do
{
endRead = fgets(currentLine, SIZE_CURRENT_LINE, f);
if (endRead != NULL)
{
rc = configfile_decodeLine(currentLine);
if (rc != 0)
{
fprintf(stderr, "syntax error near line %d\n", lineNo);
}
}
lineNo++;
}
while (!feof(f) && (rc == 0));
return rc;
}
enum
{
IDLE,
DECODE_KEY,
WAIT_EQUAL,
VALUE_WAIT_VALUE,
VALUE,
VALUE_STRING
};
static int32_t configfile_decodeLine(char line[])
{
int32_t rc;
uint32_t index;
char* key;
char* value = NULL;
int64_t state;
bool bEndDecoding;
char currentChar;
rc = 0;
state = IDLE;
bEndDecoding = false;
index = 0;
while (bEndDecoding == false)
{
currentChar = line[index];
if (currentChar == '#')
{
bEndDecoding = true;
line[index] = '\0';
}
else
{
switch (state)
{
case IDLE:
if ((currentChar == '\0') || (currentChar == '\n'))
{
bEndDecoding = true;
}
else if (!isspace(currentChar))
{
state = DECODE_KEY;
key = &line[index];
}
break;
case DECODE_KEY:
if ((currentChar == '\0') || (currentChar == '\n'))
{
bEndDecoding = true;
}
else if (!isalnum(currentChar) && (currentChar != '_') && (currentChar != '=') )
{
state = WAIT_EQUAL;
line[index] = '\0';
}
else if (currentChar == '=')
{
state = VALUE_WAIT_VALUE;
line[index] = '\0';
}
break;
case WAIT_EQUAL:
if ((currentChar == '\0') || (currentChar == '\n'))
{
bEndDecoding = true;
}
else if (currentChar == '=')
{
state = VALUE_WAIT_VALUE;
}
else if (!isspace(currentChar))
{
bEndDecoding = true;
}
break;
case VALUE_WAIT_VALUE:
if ((currentChar == '\0') || (currentChar == '\n'))
{
bEndDecoding = true;
}
else if (!isspace(currentChar) && (currentChar != '"'))
{
state = VALUE;
value = &line[index];
}
else if (currentChar == '"')
{
state = VALUE_STRING;
value = &line[index + 1];
}
break;
case VALUE_STRING:
if (currentChar == '"')
{
bEndDecoding = true;
line[index] = '\0';
}
break;
case VALUE:
if ((currentChar == '\0') || (currentChar == '\n'))
{
line[index] = '\0';
bEndDecoding = true;
}
else if (isspace(currentChar))
{
line[index] = '\0';
bEndDecoding = true;
}
break;
default:
assert(false);
break;
}
}
index++;
}
if ((state == VALUE) || (state == VALUE_STRING))
{
// fprintf(stdout, "key = '%s', value = '%s'\n", key, value);
rc = configfile_createConfig( key, value);
}
else if (state == IDLE)
{
//not in error, ignore
}
else
{
rc = -1;
}
return rc;
}
static int32_t configfile_createConfig(char key[], char value[])
{
int32_t rc;
char* token;
char* endConversion;
uint32_t v;
uint32_t i;
uint32_t max;
i = 0;
max = 8;
rc = 0;
if (strcmp(key, "panID") == 0)
{
if (config_panID == NULL)
{
config_panID = malloc(sizeof(uint8_t) * max);
assert(config_panID != NULL);
}
token = strtok(value, ", ");
while (token != NULL)
{
// fprintf(stdout, "token = %s\n", token);
v = strtoul(token, &endConversion, 0);
if (*endConversion == '\0')
{
config_panID[i] = v;
if (i == max)
{
uint8_t* n;
n = realloc(config_panID, max * 2);
assert(n != NULL);
config_panID = n;
max = max * 2;
}
i++;
}
token = strtok(NULL, ", ");
}
config_nbPanID = i;
}
else if (strcmp(key, "script") == 0)
{
config_scriptName = malloc(strlen(value) + 1);
assert(config_scriptName != NULL);
strcpy(config_scriptName, value);
}
else if (strcmp(key, "ttydevice") == 0)
{
config_ttydevice = malloc(strlen(value) + 1);
assert(config_ttydevice != NULL);
strcpy(config_ttydevice, value);
}
else if (strcmp(key, "gpio_reset") == 0)
{
config_gpio_reset = malloc(strlen(value) + 1);
assert(config_gpio_reset != NULL);
strcpy(config_gpio_reset, value);
}
else if (strcmp(key, "gpio_ctrl_name") == 0)
{
config_gpio_ctrl_name = malloc(strlen(value) + 1);
assert(config_gpio_ctrl_name != NULL);
strcpy(config_gpio_ctrl_name, value);
}
else if (strcmp(key, "gpio_line") == 0)
{
uint32_t v;
v = strtoul(value, &endConversion, 0);
if (*endConversion == '\0')
{
config_gpio_line = v;
}
else
{
config_gpio_line = -1;
}
}
else if (strcmp(key, "device") == 0)
{
config_device = malloc(strlen(value) + 1);
assert(config_device != NULL);
strcpy(config_device, value);
}
else if (strcmp(key, "altitude") == 0)
{
double v;
v = strtod(value, &endConversion);
if (*endConversion == '\0')
{
config_altitude = v;
}
}
else if (strcmp(key, "fifo") == 0)
{
config_fifo_name = malloc(strlen(value) + 1);
assert(config_fifo_name != NULL);
strcpy(config_fifo_name, value);
}
else if (strcmp(key, "scan_channel") == 0)
{
uint32_t v;
v = strtoul(value, &endConversion, 0);
if (*endConversion == '\0')
{
config_scan_channel = (uint16_t) v;
}
else
{
config_scan_channel = ZIGGEE_DEFAULT_BITMASK;
}
}
else
{
rc = -1;
}
return rc;
}