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
|
#include "config-process.h"
#include "device.h"
#include "util.h"
#include <libubox/avl-cmp.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
typedef struct _process_ctx {
struct avl_tree *ret;
} process_ctx_t;
typedef struct _config_subtype {
struct avl_node node;
} config_subtype_t;
static device_t * config_process_device(const char *name, struct json_object *obj) {
const char *typename = NULL;
struct json_object *device = neco_json_get_value(obj, "device", json_type_object);
if (device)
typename = neco_json_get_string(device, "type");
const device_type_t *type = get_device_type(typename ?: "interface");
if (!type)
return NULL;
return type->process_config(name, obj);
}
struct avl_tree * config_process(struct json_object *config) {
if (!json_object_is_type(config, json_type_object))
return NULL;
struct json_object *devices = neco_json_get_value(config, "devices", json_type_object);
if (!devices)
return NULL;
process_ctx_t ctx;
ctx.ret = calloc(1, sizeof(*ctx.ret));
if (!ctx.ret)
return NULL;
avl_init(ctx.ret, avl_strcmp, false, NULL);
json_object_object_foreach(devices, name, obj) {
device_t *device = config_process_device(name, obj);
if (!device)
continue;
avl_insert(ctx.ret, &device->node);
}
return ctx.ret;
}
|