@@ -220,6 +220,7 @@ static int prepare_boot_script(struct swupdate_cfg *cfg, const char *script)
LIST_FOREACH(bootvar, &cfg->bootloader, next) {
char *key = dict_entry_get_key(bootvar);
char *value = dict_entry_get_value(bootvar);
+
if (!key || !value)
continue;
snprintf(buf, sizeof(buf), "%s %s\n", key, value);
@@ -74,6 +74,21 @@ void *get_child(parsertype p, void *e, const char *name)
return NULL;
}
+void iterate_field(parsertype p, void *e, iterate_callback cb, void *data)
+{
+ switch (p) {
+ case LIBCFG_PARSER:
+ iterate_field_libconfig(e, cb, data);
+ break;
+ case JSON_PARSER:
+ iterate_field_json(e, cb, data);
+ break;
+ default:
+ (void)e;
+ (void)data;
+ }
+}
+
void *get_elem_from_idx(parsertype p, void *node, int idx)
{
switch (p) {
@@ -59,6 +59,30 @@ void *get_child_libconfig(void *e, const char *name)
return config_setting_get_member(e, name);
}
+void iterate_field_libconfig(config_setting_t *e, iterate_callback cb, void *data)
+{
+ config_setting_t *entry, *elem;
+ const char *str;
+ int i, j;
+
+ if (!cb)
+ return;
+
+ for (i = 0; i < config_setting_length(e); i++) {
+ entry = config_setting_get_elem(e, i);
+ if (!config_setting_length(entry)) {
+ str = config_setting_get_string(entry);
+ cb(entry->name, str, data);
+ } else {
+ for (j = 0; j < config_setting_length(entry); j++) {
+ elem = config_setting_get_elem(entry, j);
+ str = config_setting_get_string(elem);
+ cb(entry->name, str, data);
+ }
+ }
+ }
+}
+
void get_field_cfg(config_setting_t *e, const char *path, void *dest)
{
config_setting_t *elem;
@@ -63,6 +63,37 @@ void *get_child_json(json_object *e, const char *path)
return node;
}
+void iterate_field_json(json_object *e, iterate_callback cb, void *data)
+{
+ json_object *subnode;
+ const char *str;
+ int i;
+
+ if (!cb || json_object_get_type(e) != json_type_object)
+ return;
+
+ json_object_object_foreach(e, key, node) {
+ switch (json_object_get_type(node)) {
+ case json_type_string:
+ str = json_object_get_string(node);
+ cb(key, str, data);
+ break;
+ case json_type_array:
+ for (i = 0; i < json_object_array_length(node); i++) {
+ subnode = json_object_array_get_idx(node, i);
+ if (json_object_get_type(subnode) != json_type_string)
+ continue;
+
+ str = json_object_get_string(subnode);
+ cb(key, str, data);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+}
+
const char *get_field_string_json(json_object *e, const char *path)
{
const char *str;
@@ -340,9 +340,7 @@ the SWU forwarder:
filename = "image.swu";
type = "swuforward";
- properties: (
- {
- name = "url";
- value = "http://192.168.178.41:8080";
- });
+ properties: {
+ url = "http://192.168.178.41:8080";
+ };
});
@@ -27,6 +27,9 @@ typedef enum {
JSON_PARSER
} parsertype;
+typedef void (*iterate_callback)(const char *name, const char *value,
+ void *data);
+
#ifdef CONFIG_LIBCONFIG
#include <libconfig.h>
#define LIBCONFIG_VERSION ((LIBCONFIG_VER_MAJOR << 16) | \
@@ -38,6 +41,8 @@ typedef enum {
void get_value_libconfig(const config_setting_t *e, void *dest);
void get_field_cfg(config_setting_t *e, const char *path, void *dest);
void *get_child_libconfig(void *e, const char *name);
+void iterate_field_libconfig(config_setting_t *e, iterate_callback cb,
+ void *data);
const char *get_field_string_libconfig(config_setting_t *e, const char *path);
#else
@@ -47,6 +52,7 @@ const char *get_field_string_libconfig(config_setting_t *e, const char *path);
#define find_node_libconfig(cfg, field, swcfg) (NULL)
#define get_field_string_libconfig(e, path) (NULL)
#define get_child_libconfig(e, name) (NULL)
+#define iterate_field_libconfig(e, cb, data) (0)
#define get_field_cfg(e, path, dest)
#endif
@@ -57,6 +63,7 @@ const char *get_field_string_json(json_object *e, const char *path);
void get_value_json(json_object *e, void *dest);
void get_field_json(json_object *e, const char *path, void *dest);
void *get_child_json(json_object *e, const char *name);
+void iterate_field_json(json_object *e, iterate_callback cb, void *data);
json_object *find_json_recursive_node(json_object *root, const char **names);
json_object *json_get_key(json_object *json_root, const char *key);
const char *json_get_value(struct json_object *json_root,
@@ -68,6 +75,7 @@ char *json_get_data_url(json_object *json_root, const char *key);
#define find_node_json(a, b, c) (NULL)
#define get_field_string_json(e, path) (NULL)
#define get_child_json(e, name) (NULL)
+#define iterate_field_json(e, cb, data) (0)
#define get_field_json(e, path, dest)
#define json_object_object_get_ex(a,b,c) (0)
#define json_object_array_get_idx(a, b) (0)
@@ -82,6 +90,7 @@ void get_field_string_with_size(parsertype p, void *e, const char *path,
int get_array_length(parsertype p, void *root);
void *get_elem_from_idx(parsertype p, void *node, int idx);
void *get_child(parsertype p, void *node, const char *name);
+void iterate_field(parsertype p, void *e, iterate_callback cb, void *data);
void get_field(parsertype p, void *e, const char *path, void *dest);
int exist_field_string(parsertype p, void *e, const char *path);
void get_hash_value(parsertype p, void *elem, unsigned char *hash);
@@ -152,32 +152,27 @@ static void *find_node(parsertype p, void *root, const char *node,
return NULL;
}
-static void add_properties(parsertype p, void *node, struct img_type *image)
+static void add_properties_cb(const char *name, const char *value, void *data)
{
+ struct img_type *image = (struct img_type *)data;
- void *properties, *prop;
- int count, i;
+ if (!name || !value)
+ return;
+
+ TRACE("\t\tProperty %s: %s", name, value);
+ if (dict_insert_value(&image->properties, (char *)name, (char *)value))
+ ERROR("Property not stored, skipping...");
+}
+
+static void add_properties(parsertype p, void *node, struct img_type *image)
+{
+ void *properties;
properties = get_child(p, node, "properties");
if (properties) {
- count = get_array_length(p, properties);
-
- TRACE("Found %d properties for %s:", count, image->fname);
-
- for (i = 0; i < count; i++) {
- char key[255];
- char value[255];
- prop = get_elem_from_idx(p, properties, i);
- GET_FIELD_STRING(p, prop, "name", key);
- GET_FIELD_STRING(p, prop, "value", value);
- TRACE("\t\tProperty %d: name=%s val=%s ", i,
- key,
- value
- );
- if (dict_insert_value(&image->properties, key, value))
- ERROR("Property not stored, skipping...");
+ TRACE("Found properties for %s:", image->fname);
- }
+ iterate_field(p, properties, add_properties_cb, image);
}
}