From 475922ce345bb0443cf4fdef00efa86676a1bf62 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 1 Sep 2015 00:41:49 +0100 Subject: fish --- manifest.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 manifest.c (limited to 'manifest.c') diff --git a/manifest.c b/manifest.c new file mode 100644 index 0000000..795e833 --- /dev/null +++ b/manifest.c @@ -0,0 +1,73 @@ +#include "project.h" + + +json_object * +_json_object_object_get (json_object * obj, const char *name) +{ + json_object *sub; + return json_object_object_get_ex (obj, name, &sub) ? sub : NULL; +} + + +static void +dump_manifest (struct manifest *m) +{ + printf ("Manifest:\n"); + printf (" type : %s\n", m->type); + printf (" dfu_version: %s\n", m->dfu_version); + printf (" dat_file : %s\n", m->dat_file); + printf (" bin_file : %s\n", m->bin_file); +} + + + +struct manifest * +parse_manifest (const char *str) +{ + json_object *json, *manifest; + enum json_type type; + enum json_tokener_error jerr = json_tokener_success; + struct manifest *m; + + + m = xmalloc (sizeof (*m)); + memset (m, 0, sizeof (*m)); + + json = json_tokener_parse_verbose (str, &jerr); + + manifest = _json_object_object_get (json, "manifest"); + + m->dfu_version = + json_object_get_string (_json_object_object_get + (manifest, "dfu_version")); + + json_object_object_foreach (manifest, key, val) + { + if (!strcmp (key, "dfu_version")) + continue; + + m->type = key; + + m->bin_file = + json_object_get_string (_json_object_object_get (val, "bin_file")); + m->dat_file = + json_object_get_string (_json_object_object_get (val, "dat_file")); + break; + } + + + dump_manifest (m); + + + if (!m->dfu_version || !m->type || !m->bin_file || !m->dat_file) + { + fprintf (stderr, "Failed to process manifest\n"); + exit (EXIT_FAILURE); + } + + + + + + return m; +} -- cgit v1.2.3