diff mbox series

[15/17] Factorize some json function for reuse

Message ID 1511176210-28928-15-git-send-email-sbabic@denx.de
State Accepted
Headers show
Series [01/17] parser: added function to get net child in tree | expand

Commit Message

Stefano Babic Nov. 20, 2017, 11:10 a.m. UTC
Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 corelib/parsing_library_libjson.c | 47 ++++++++++++++++++++++++++++++++++++
 include/parselib.h                |  5 ++++
 suricatta/server_hawkbit.c        | 50 +--------------------------------------
 3 files changed, 53 insertions(+), 49 deletions(-)
diff mbox series

Patch

diff --git a/corelib/parsing_library_libjson.c b/corelib/parsing_library_libjson.c
index 2f64d41..67cbc2c 100644
--- a/corelib/parsing_library_libjson.c
+++ b/corelib/parsing_library_libjson.c
@@ -31,6 +31,8 @@ 
 #include "swupdate.h"
 #include "parselib.h"
 
+#define MAX_URL_LENGTH 2048
+
 json_object *find_json_recursive_node(json_object *root, const char **names)
 {
 	json_object *node = root;
@@ -114,3 +116,48 @@  void get_field_json(json_object *e, const char *path, void *dest)
 		get_value_json(e, dest);
 	}
 }
+
+json_object *json_get_key(json_object *json_root, const char *key)
+{
+	json_object *json_child;
+	if (json_object_object_get_ex(json_root, key, &json_child)) {
+		return json_child;
+	}
+	return NULL;
+}
+
+const char *json_get_value(struct json_object *json_root,
+			   const char *key)
+{
+	json_object *json_data = json_get_key(json_root, key);
+
+	if (json_data == NULL)
+		return "";
+
+	return json_object_get_string(json_data);
+}
+
+json_object *json_get_path_key(json_object *json_root, const char **json_path)
+{
+	json_object *json_data = json_root;
+	while (*json_path) {
+		const char *key = *json_path;
+		json_data = json_get_key(json_data, key);
+		if (json_data == NULL) {
+			return NULL;
+		}
+		json_path++;
+	}
+	return json_data;
+}
+
+char *json_get_data_url(json_object *json_root, const char *key)
+{
+	json_object *json_data = json_get_path_key(
+	    json_root, (const char *[]){"_links", key, "href", NULL});
+	return json_data == NULL
+		   ? NULL
+		   : strndup(json_object_get_string(json_data), MAX_URL_LENGTH);
+}
+
+
diff --git a/include/parselib.h b/include/parselib.h
index c32920a..7c44a5e 100644
--- a/include/parselib.h
+++ b/include/parselib.h
@@ -58,6 +58,11 @@  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);
 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,
+			   const char *key);
+json_object *json_get_path_key(json_object *json_root, const char **json_path);
+char *json_get_data_url(json_object *json_root, const char *key);
 
 #else
 #define find_node_json(a, b, c)		(NULL)
diff --git a/suricatta/server_hawkbit.c b/suricatta/server_hawkbit.c
index 9649d27..175396c 100644
--- a/suricatta/server_hawkbit.c
+++ b/suricatta/server_hawkbit.c
@@ -32,6 +32,7 @@ 
 #include <sys/time.h>
 #include <swupdate_status.h>
 #include "suricatta/suricatta.h"
+#include "parselib.h"
 #include "channel.h"
 #include "channel_curl.h"
 #include "suricatta/state.h"
@@ -45,7 +46,6 @@ 
 #define DEFAULT_RESUME_DELAY 5
 #define INITIAL_STATUS_REPORT_WAIT_DELAY 10
 
-#define MAX_URL_LENGTH 2048
 #define STRINGIFY(...) #__VA_ARGS__
 #define JSON_OBJECT_FREED 1
 #define ENOMEM_ASPRINTF -1
@@ -109,11 +109,6 @@  static hawkbit_enums_t hawkbit_enums[] = {
 extern channel_op_res_t channel_curl_init(void);
 /* Prototypes for "internal" functions */
 /* Note that they're not `static` so that they're callable from unit tests. */
-json_object *json_get_key(json_object *json_root, const char *key);
-const char *json_get_value(struct json_object *json_root,
-			   const char *key);
-json_object *json_get_path_key(json_object *json_root, const char **json_path);
-char *json_get_data_url(json_object *json_root, const char *key);
 server_op_res_t map_channel_retcode(channel_op_res_t response);
 server_op_res_t server_handle_initial_state(update_state_t stateovrrd);
 static int server_update_status_callback(ipc_message *msg);
@@ -208,49 +203,6 @@  static bool hawkbit_enum_check(const char *key, const char *value)
 	return false;
 }
 
-json_object *json_get_key(json_object *json_root, const char *key)
-{
-	json_object *json_child;
-	if (json_object_object_get_ex(json_root, key, &json_child)) {
-		return json_child;
-	}
-	return NULL;
-}
-
-const char *json_get_value(struct json_object *json_root,
-			   const char *key)
-{
-	json_object *json_data = json_get_key(json_root, key);
-
-	if (json_data == NULL)
-		return "";
-
-	return json_object_get_string(json_data);
-}
-
-json_object *json_get_path_key(json_object *json_root, const char **json_path)
-{
-	json_object *json_data = json_root;
-	while (*json_path) {
-		const char *key = *json_path;
-		json_data = json_get_key(json_data, key);
-		if (json_data == NULL) {
-			return NULL;
-		}
-		json_path++;
-	}
-	return json_data;
-}
-
-char *json_get_data_url(json_object *json_root, const char *key)
-{
-	json_object *json_data = json_get_path_key(
-	    json_root, (const char *[]){"_links", key, "href", NULL});
-	return json_data == NULL
-		   ? NULL
-		   : strndup(json_object_get_string(json_data), MAX_URL_LENGTH);
-}
-
 static const char *json_get_deployment_update_action(json_object *json_reply)
 {
 	if (!json_reply)