forked from edzius/tlvstore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
329 lines (283 loc) · 6.52 KB
/
main.c
File metadata and controls
329 lines (283 loc) · 6.52 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
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
#include "tlv.h"
#include "device.h"
#include "protocol.h"
#ifndef TLVS_DEFAULT_FILE
#define TLVS_DEFAULT_FILE NULL
#endif
#ifndef TLVS_DEFAULT_SIZE
#define TLVS_DEFAULT_SIZE 0
#endif
#ifndef TLVS_DEFAULT_OFFSET
#define TLVS_DEFAULT_OFFSET 0
#endif
#define OP_LIST 1
#define OP_GET 2
#define OP_SET 3
struct params_list {
struct params_list *next;
char *key;
char *val;
};
static struct params_list *pl;
static struct storage_device *dev;
static struct storage_protocol *proto;
static int op;
static int compat;
/* Global log level - default to WARNING */
int g_log_level = LOG_WARNING;
int tlvstore_parse_line(char *arg)
{
char *key, *val;
struct params_list *pe;
key = strdup(arg);
val = strchr(key, '=');
if (val) {
*val = 0;
val++;
}
ldebug("Parsed parameter: '%s' = '%s'", key, val);
if (eeprom_check(proto, key, op == OP_SET ? val : NULL)) {
lerror("Invalid EEPROM param '%s'", arg);
free(key);
return 1;
}
/* Find available param */
pe = pl;
while (pe) {
if (!strcmp(pe->key, key))
break;
pe = pe->next;
}
if (pe) {
free(pe->key);
pe->key = key;
pe->val = val;
} else {
pe = calloc(1, sizeof(*pe));
pe->key = key;
pe->val = val;
pe->next = pl;
pl = pe;
}
return 0;
}
int tlvstore_parse_config(char *arg)
{
FILE *fp;
char *fname;
char line[256];
int len, fail = 0;
fname = *arg == '@' ? arg + 1 : arg;
fp = fopen(fname, "r");
if (!fp) {
lerror("Invalid EEPROM config '%s'", arg);
return 1;
}
ldebug("Opened EEPROM config file: %s", fname);
while (fgets(line, sizeof(line) - 1, fp)) {
/* Trim the line */
len = strlen(line);
while (line[len - 1] <= 32 || line[len - 1] >= 127)
len--;
line[len] = 0;
fail += tlvstore_parse_line(line);
}
if (ferror(fp)) {
lerror("Cannot read EEPROM config file: '%s'", fname);
fail++;
}
fclose(fp);
return fail;
}
int tlvstore_parse_params(int argc, char *argv[])
{
int i, fail = 0;
char *arg;
ldebug("Parsing %d parameters", argc);
for (int i = 0; i < argc; i++) {
arg = argv[i];
if (arg[0] == '@')
fail += tlvstore_parse_config(arg);
else
fail += tlvstore_parse_line(arg);
}
return fail;
}
int tlvstore_export_params(void)
{
struct params_list *pe = pl;
int fail = 0;
int ret;
if (!pl) {
ldebug("Exporting all TLV properties");
fail = eeprom_export(proto, NULL, NULL);
} else {
ldebug("Starting parameters export");
}
while (pl) {
ret = eeprom_export(proto, pl->key, pl->val);
if (ret < 0 || (!compat && ret)) {
if (pl->val && pl->val[0] == '@')
lwarning("Failed to export '%s' to '%s'",
pl->key, pl->val);
else if (pl->val)
lwarning("Failed to export '%s' as '%s'",
pl->key, pl->val);
else
lwarning("Failed to export '%s'", pl->key);
fail++;
}
pl = pl->next;
}
if (fail)
ldebug("Failed TLV export, %i failures", fail);
return fail;
}
int tlvstore_import_params(void)
{
struct params_list *pe = pl;
int fail = 0;
ldebug("Starting parameters import");
while (pl) {
if (eeprom_import(proto, pl->key, pl->val) < 0) {
lwarning("Failed to import '%s' value '%s'", pl->key, pl->val);
fail++;
}
pl = pl->next;
}
if (fail)
lerror("Failed TLV import, %i failures", fail);
return fail;
}
void tlvstore_list_properties(void)
{
ldebug("Listing TLV properties");
eeprom_list(proto);
}
static void tlvstore_usage(void)
{
fprintf(stderr, "Usage: tlvstore [options] <key>[=@value>] ...\n"
" -F, --store-file <file-name> Storage file path\n"
" -S, --store-size <file-size> Preferred storage file size\n"
" -O, --store-offset <file-offset> Storage file data offset\n"
" -v, --verbose Increase verbosity\n"
" -f, --force Force initialise storage\n"
" -c, --compat Compatibility retrieve avilable params\n"
" -g, --get Get specified keys or all keys when no specified\n"
" -s, --set Set specified keys\n"
" -l, --list List available keys\n"
);
}
static struct option tlvstore_options[] =
{
{ "store-size", 1, 0, 'S' },
{ "store-file", 1, 0, 'F' },
{ "store-offset", 1, 0, 'O' },
{ "verbose", 0, 0, 'v' },
{ "force", 0, 0, 'f' },
{ "compat", 0, 0, 'c' },
{ "get", 0, 0, 'g' },
{ "set", 0, 0, 's' },
{ "list", 0, 0, 'l' },
{ 0, 0, 0, 0 }
};
int main(int argc, char *argv[])
{
int opt, index;
int ret = 1;
char *store_file = NULL;
int store_size = 0;
int store_offset = 0;
int user_size = 0;
int user_offset = 0;
int force = 0;
while ((opt = getopt_long(argc, argv, "F:S:O:hvfcgsl", tlvstore_options, &index)) != -1) {
switch (opt) {
case 'F':
store_file = strdup(optarg);
break;
case 'S':
store_size = atoi(optarg);
user_size = 1;
break;
case 'O':
store_offset = atoi(optarg);
user_offset = 1;
break;
case 'v':
if (g_log_level)
g_log_level--;
break;
case 'f':
force = 1;
break;
case 'c':
compat = 1;
break;
case 'g':
op = OP_GET;
break;
case 's':
op = OP_SET;
break;
case 'l':
op = OP_LIST;
break;
case 'h':
default:
tlvstore_usage();
exit(EXIT_FAILURE);
}
}
/* Initialise build-time defaults only when default
* storage file is used. Even in that case allow to
* override custom storage size and offset when set. */
if (!store_file) {
store_file = TLVS_DEFAULT_FILE;
if (!user_size)
store_size = TLVS_DEFAULT_SIZE;
if (!user_offset)
store_offset = TLVS_DEFAULT_OFFSET;
}
dev = storage_open(store_file, store_size, store_offset);
if (!dev) {
fprintf(stderr, "Failed to initialize '%s' storage file\n", store_file);
exit(EXIT_FAILURE);
}
linfo("Opened storage file '%s' (%zu bytes)", store_file, dev->size);
proto = eeprom_init(dev, force);
if (!proto) {
fprintf(stderr, "Unknown storage protocol for '%s'\n", store_file);
storage_close(dev);
exit(EXIT_FAILURE);
}
linfo("Initialized storage protocol '%s'", proto->name);
if (tlvstore_parse_params(argc - optind, &argv[optind])) {
tlvstore_usage();
eeprom_free(proto);
storage_close(dev);
exit(EXIT_FAILURE);
}
if (op == OP_LIST) {
tlvstore_list_properties();
ret = 0;
} else if (op == OP_GET) {
if (!tlvstore_export_params())
ret = 0;
} else if (op == OP_SET) {
if (!tlvstore_import_params())
ret = 0;
} else {
lwarning("No operation specified");
}
eeprom_free(proto);
storage_close(dev);
eeprom_unregister();
return ret;
}