diff mbox series

Set json support as mandatory

Message ID 20231107133714.120001-1-stefano.babic@swupdate.org
State Accepted
Headers show
Series Set json support as mandatory | expand

Commit Message

Stefano Babic Nov. 7, 2023, 1:37 p.m. UTC
JSON is used in a lto of part of code, and it is quite always set. Make
it mandatory and always link SWUpdate to libjson. Cleanup code to remove
superseded CONFIG_JSON.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 Kconfig                |  4 ----
 Makefile.deps          |  4 ----
 Makefile.flags         |  7 +------
 corelib/Makefile       |  5 +++--
 corelib/channel_curl.c |  5 +----
 handlers/Config.in     |  6 ++----
 include/channel_curl.h |  4 ----
 include/parselib.h     | 15 ---------------
 parser/Config.in       | 12 ------------
 parser/parser.c        | 12 ------------
 suricatta/Config.in    | 17 +----------------
 suricatta/server_lua.c | 18 +-----------------
 tools/swupdate-ipc.c   | 11 +----------
 13 files changed, 10 insertions(+), 110 deletions(-)
diff mbox series

Patch

diff --git a/Kconfig b/Kconfig
index bd4ce049..2ae2e4bc 100644
--- a/Kconfig
+++ b/Kconfig
@@ -109,10 +109,6 @@  config HAVE_P11KIT
 	bool
 	option env="HAVE_P11KIT"
 
-config HAVE_JSON_C
-	bool
-	option env="HAVE_JSON_C"
-
 config HAVE_LIBWEBSOCKETS
 	bool
 	option env="HAVE_LIBWEBSOCKETS"
diff --git a/Makefile.deps b/Makefile.deps
index 397e08c4..30e4d92a 100644
--- a/Makefile.deps
+++ b/Makefile.deps
@@ -102,10 +102,6 @@  ifeq ($(HAVE_P11KIT),)
 export HAVE_P11KIT = y
 endif
 
-ifeq ($(HAVE_JSON_C),)
-export HAVE_JSON_C = y
-endif
-
 ifeq ($(HAVE_LIBWEBSOCKETS),)
 export HAVE_LIBWEBSOCKETS = y
 endif
diff --git a/Makefile.flags b/Makefile.flags
index accdd6a2..a2bb2e7f 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -106,7 +106,7 @@  export SYSROOT=$(CONFIG_SYSROOT)
 endif
 
 # Links always pthread
-LDLIBS += pthread ubootenv
+LDLIBS += pthread ubootenv json-c
 # lua
 ifneq ($(CONFIG_LUA),)
 LDFLAGS_swupdate += -Wl,-E
@@ -144,11 +144,6 @@  ifeq ($(CONFIG_ARCHIVE),y)
 LDLIBS += archive
 endif
 
-# json-c
-ifeq ($(CONFIG_JSON),y)
-LDLIBS += json-c
-endif
-
 ifeq ($(CONFIG_SSL_IMPL_OPENSSL),y)
 LDLIBS += crypto ssl
 endif
diff --git a/corelib/Makefile b/corelib/Makefile
index 0566f888..c9ca4aa6 100644
--- a/corelib/Makefile
+++ b/corelib/Makefile
@@ -2,7 +2,9 @@ 
 #
 # SPDX-License-Identifier:     GPL-2.0-only
 
-lib-y				+= multipart_parser.o
+lib-y				+= multipart_parser.o \
+				   parsing_library_libjson.o \
+				   server_utils.o
 lib-$(CONFIG_DOWNLOAD)		+= downloader.o
 lib-$(CONFIG_MTD)		+= mtd-interface.o
 lib-$(CONFIG_LUA)		+= lua_interface.o lua_compat.o
@@ -35,5 +37,4 @@  endif
 lib-$(CONFIG_SIGALG_GPG)	+= swupdate_gpg_verify.o
 lib-$(CONFIG_LIBCONFIG)		+= swupdate_settings.o \
 				   parsing_library_libconfig.o
-lib-$(CONFIG_JSON)		+= parsing_library_libjson.o server_utils.o
 lib-$(CONFIG_CHANNEL_CURL)	+= channel_curl.o
diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index 90ab3ea5..c411aa8a 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -27,9 +27,7 @@ 
 #include "channel.h"
 #include "channel_curl.h"
 #include "progress.h"
-#ifdef CONFIG_JSON
 #include <json-c/json.h>
-#endif
 
 #define SPEED_LOW_BYTES_SEC 8
 #define SPEED_LOW_TIME_SEC 300
@@ -945,7 +943,6 @@  static channel_op_res_t parse_reply(channel_data_t *channel_data, output_data_t
 		return CHANNEL_ENOMEM;
 	}
 
-#ifdef CONFIG_JSON
 	if (channel_data->format == CHANNEL_PARSE_JSON) {
 		assert(channel_data->json_reply == NULL);
 		enum json_tokener_error json_res;
@@ -963,7 +960,7 @@  static channel_op_res_t parse_reply(channel_data_t *channel_data, output_data_t
 			return CHANNEL_EBADMSG;
 		}
 	}
-#endif
+
 	if (channel_data->format == CHANNEL_PARSE_RAW) {
 		/* strndup is strnlen + malloc + memcpy, seems more appropriate than just malloc + memcpy. */
 		if ((channel_data->raw_reply = strndup(chunk->memory, chunk->size)) == NULL) {
diff --git a/handlers/Config.in b/handlers/Config.in
index 107ffebf..1029e582 100644
--- a/handlers/Config.in
+++ b/handlers/Config.in
@@ -204,11 +204,9 @@  config SHELLSCRIPTHANDLER
 config SWUFORWARDER_HANDLER
 	bool "SWU forwarder"
 	depends on HAVE_LIBCURL
-	depends on HAVE_JSON_C
 	depends on HAVE_LIBWEBSOCKETS
 	depends on HAVE_URIPARSER
 	select CHANNEL_CURL
-	select JSON
 	default n
 	help
 	  This allows one to build a chain of updater. A
@@ -217,8 +215,8 @@  config SWUFORWARDER_HANDLER
 	  embedded SWU to the other devices using the
 	  webserver REST API.
 
-comment "swuforward handler needs json-c and libcurl"
-	depends on !HAVE_JSON_C || !HAVE_LIBCURL
+comment "swuforward handler needs libcurl"
+	depends on !HAVE_LIBCURL
 
 comment "swuforward handler needs websockets and uriparser"
 	depends on !HAVE_LIBWEBSOCKETS || !HAVE_URIPARSER
diff --git a/include/channel_curl.h b/include/channel_curl.h
index 4409dca8..643082b1 100644
--- a/include/channel_curl.h
+++ b/include/channel_curl.h
@@ -7,9 +7,7 @@ 
 
 #pragma once
 #include "util.h"
-#ifdef CONFIG_JSON
 #include <json-c/json.h>
-#endif
 #include <stdio.h>
 #include <stdbool.h>
 #include "swupdate_status.h"
@@ -42,9 +40,7 @@  typedef struct {
 	char *auth;
 	char *request_body;
 	char *iface;
-#ifdef CONFIG_JSON
 	json_object *json_reply;
-#endif
 	char *raw_reply;
 	bool dry_run;
 	char *cafile;
diff --git a/include/parselib.h b/include/parselib.h
index 7baf555d..b1674709 100644
--- a/include/parselib.h
+++ b/include/parselib.h
@@ -56,7 +56,6 @@  void *get_node_libconfig(config_t *cfg, const char **nodes);
 #define is_field_numeric_cfg(e, path)	(false)
 #endif
 
-#ifdef CONFIG_JSON
 #include <json-c/json.h>
 
 bool is_field_numeric_json(json_object *e, const char *path);
@@ -74,20 +73,6 @@  char *json_get_data_url(json_object *json_root, const char *key);
 void *find_root_json(json_object *root, const char **nodes, unsigned int depth);
 void *get_node_json(json_object *root, const char **nodes);
 
-#else
-#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)	{ }
-#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)
-#define json_object_array_length(a)	(0)
-#define find_root_json(root, nodes, depth)	(NULL)
-#define get_node_json(root, nodes)	(NULL)
-#define is_field_numeric_json(e, path)	(false)
-#endif
-
 bool is_field_numeric(parsertype p, void *e, const char *path);
 const char *get_field_string(parsertype p, void *e, const char *path);
 void get_field_string_with_size(parsertype p, void *e, const char *path,
diff --git a/parser/Config.in b/parser/Config.in
index 454e9cdb..e97734da 100644
--- a/parser/Config.in
+++ b/parser/Config.in
@@ -17,23 +17,11 @@  comment "Default config parser support needs libconfig"
 
 config PARSERROOT
 	string "Name of root node"
-	depends on LIBCONFIG || JSON
 	help
 	  For compatibility reasons, the root node
 	  can be set to something else as "software".
 	  In most case you do not need to set it.
 
-config JSON
-	bool "libjson"
-	default n
-	depends on HAVE_JSON_C
-	help
-	  Use json-c to parse the configuration file.
-	  Also required for suricatta server support.
-
-comment "JSON config parser support needs json-c"
-	depends on !HAVE_JSON_C
-
 config LUAEXTERNAL
 	bool "lua"
 	depends on LUA
diff --git a/parser/parser.c b/parser/parser.c
index 46642a4d..60f979af 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -31,8 +31,6 @@ 
 #define NODEROOT (!strlen(CONFIG_PARSERROOT) ? \
 			"software" : CONFIG_PARSERROOT)
 
-#if defined(CONFIG_LIBCONFIG) || defined(CONFIG_JSON)
-
 typedef int (*parse_element)(parsertype p,
 		void *cfg, void *start, const char **nodes,
 		struct swupdate_cfg *swcfg, lua_State *L);
@@ -1056,7 +1054,6 @@  static int parser(parsertype p, void *cfg, struct swupdate_cfg *swcfg)
 
 	return ret;
 }
-#endif
 
 #ifdef CONFIG_LIBCONFIG
 int parse_cfg (struct swupdate_cfg *swcfg, const char *filename)
@@ -1094,8 +1091,6 @@  int parse_cfg (struct swupdate_cfg __attribute__ ((__unused__)) *swcfg,
 }
 #endif
 
-#ifdef CONFIG_JSON
-
 #define JSON_OBJECT_FREED 1
 
 int parse_json(struct swupdate_cfg *swcfg, const char *filename)
@@ -1159,10 +1154,3 @@  int parse_json(struct swupdate_cfg *swcfg, const char *filename)
 
 	return ret;
 }
-#else
-int parse_json(struct swupdate_cfg __attribute__ ((__unused__)) *swcfg,
-		const char __attribute__ ((__unused__)) *filename)
-{
-	return -1;
-}
-#endif
diff --git a/suricatta/Config.in b/suricatta/Config.in
index 08362482..f6efa28a 100644
--- a/suricatta/Config.in
+++ b/suricatta/Config.in
@@ -38,26 +38,19 @@  menu "Server"
 config SURICATTA_HAWKBIT
 	bool "hawkBit support"
 	default y
-	depends on HAVE_JSON_C
-	select JSON
 	help
 	  Support for hawkBit server.
 	  https://projects.eclipse.org/projects/iot.hawkbit
 
-comment "hawkBit support needs json-c"
-	depends on !HAVE_JSON_C
-
 config SURICATTA_LUA
 	bool "Suricatta Lua module"
-	depends on HAVE_JSON_C
 	depends on HAVE_LUA
 	select CHANNEL_CURL
-	select JSON
 	select LUA
 	help
 	  Support for Suricatta modules in Lua.
 
-	  The optional JSON support for Suricatta Lua modules
+	  The JSON support for Suricatta Lua modules
 	  provides JSON as Lua Tables to the Lua realm and
 	  enables channel result parsing to JSON per default.
 	  To enable, select 'libjson' in 'Parser Features'.
@@ -96,21 +89,13 @@  config EMBEDDED_SURICATTA_LUA_SOURCE
 
 config SURICATTA_GENERAL
 	bool "General HTTP support"
-	depends on HAVE_JSON_C
-	select JSON
 	help
 	  Support for Simple HTTP coded server
 	  The server uses HTTP return codes to detect if an update
 	  is available. See documentation for more details.
 
-comment "General HTTP support needs json-c"
-	depends on !HAVE_JSON_C
-
 endmenu
 
-comment "hawkBit & Suricatta Lua module support needs json-c"
-	depends on !HAVE_JSON_C
-
 comment "Suricatta Lua module support needs Lua"
 	depends on !HAVE_LUA
 
diff --git a/suricatta/server_lua.c b/suricatta/server_lua.c
index 0244bf2e..62bc35be 100644
--- a/suricatta/server_lua.c
+++ b/suricatta/server_lua.c
@@ -18,9 +18,7 @@ 
 #include <lualib.h>
 #include <lua_util.h>
 
-#ifdef CONFIG_JSON
 #include <json-c/json_visit.h>
-#endif
 
 #include <util.h>
 #include <network_ipc.h>
@@ -62,11 +60,7 @@  static channel_data_t channel_data_defaults = {
 	.retry_sleep = CHANNEL_DEFAULT_RESUME_DELAY,
 	.retries = CHANNEL_DEFAULT_RESUME_TRIES,
 	.low_speed_timeout = 300,
-#ifdef CONFIG_JSON
-	.format = CHANNEL_PARSE_JSON,
-#else
-	.format = CHANNEL_PARSE_RAW,
-#endif
+	.format = CHANNEL_PARSE_JSON, /* just default, it can be overwritten */
 	.debug = false,
 #ifdef CONFIG_SURICATTA_SSL
 	.usessl = true,
@@ -273,8 +267,6 @@  typedef struct {
 					     get_from_table_3(__VA_ARGS__) \
 					     )
 
-
-#ifdef CONFIG_JSON
 /**
  * @brief Push the Lua equivalent of a JSON type to Table on stack top.
  *
@@ -378,8 +370,6 @@  static bool json_to_table(lua_State *L, json_object *json_root)
 	}
 	return true;
 }
-#endif
-
 
 /**
  * @brief Push true or nil to Lua stack according to result argument.
@@ -720,7 +710,6 @@  static int channel_do_operation(lua_State *L, channel_method_t op)
 	lua_newtable(L);
 	push_to_table(L, "http_response_code", channel_data.http_response_code);
 	push_to_table(L, "format",             channel_data.format);
-	#ifdef CONFIG_JSON
 	if (channel_data.format == CHANNEL_PARSE_JSON) {
 		lua_pushstring(L, "json_reply");
 		if (!channel_data.json_reply ||
@@ -734,7 +723,6 @@  static int channel_do_operation(lua_State *L, channel_method_t op)
 			ERROR("JSON object should be freed but was not.");
 		}
 	}
-	#endif
 	if (channel_data.format == CHANNEL_PARSE_RAW) {
 		lua_pushstring(L, "raw_reply");
 		if (!channel_data.raw_reply) {
@@ -913,7 +901,6 @@  static void *progress_offloader_thread(void *data)
 			push_to_table(thread_data->L, "hnd_name",    (char*)message->hnd_name);
 			push_to_table(thread_data->L, "source",      message->source);
 			push_to_table(thread_data->L, "info",        (char*)message->info);
-			#ifdef CONFIG_JSON
 			if (message->infolen > 0) {
 				lua_pushstring(thread_data->L, "jsoninfo");
 				struct json_object *json_root = json_tokener_parse(
@@ -927,7 +914,6 @@  static void *progress_offloader_thread(void *data)
 				}
 				lua_settable(thread_data->L, -3);
 			}
-			#endif
 			STAILQ_REMOVE_HEAD(&thread_data->progress_msgq, entries);
 			free(qitem);
 			(void)pthread_mutex_unlock(thread_data->progress_msgq_lock);
@@ -1985,7 +1971,6 @@  static server_op_res_t server_ipc(ipc_message *msg)
 	lua_pushstring(gL, "msg");
 	lua_pushlstring(gL, (char *)msg->data.procmsg.buf, msg->data.procmsg.len);
 	lua_settable(gL, -3);
-	#ifdef CONFIG_JSON
 	if (msg->data.procmsg.len > 0) {
 		lua_pushstring(gL, "json");
 		struct json_object *json_root = json_tokener_parse(msg->data.procmsg.buf);
@@ -1998,7 +1983,6 @@  static server_op_res_t server_ipc(ipc_message *msg)
 			ERROR("JSON object should be freed but was not.");
 		}
 	}
-	#endif
 	server_op_res_t result = map_lua_result(call_lua_func(gL, SURICATTA_FUNC_IPC, 1));
 	msg->type = result == SERVER_OK ? ACK : NACK;
 	msg->data.procmsg.len = 0;
diff --git a/tools/swupdate-ipc.c b/tools/swupdate-ipc.c
index d070b18d..2fd759c5 100644
--- a/tools/swupdate-ipc.c
+++ b/tools/swupdate-ipc.c
@@ -31,6 +31,7 @@ 
 #include <netdb.h>
 #include <pthread.h>
 #include <getopt.h>
+#include <json-c/json.h>
 
 #include "network_ipc.h"
 #include <progress_ipc.h>
@@ -351,8 +352,6 @@  static int sendtohawkbit(cmd_t *cmd, int argc, char *argv[]) {
 	return 0;
 }
 
-#if defined(CONFIG_JSON)
-#include <json-c/json.h>
 static int gethawkbitstatus(cmd_t  __attribute__((__unused__)) *cmd,
 			    int  __attribute__((__unused__)) argc,
 			    char  __attribute__((__unused__)) *argv[]) {
@@ -388,14 +387,6 @@  static int gethawkbitstatus(cmd_t  __attribute__((__unused__)) *cmd,
 	}
 
 }
-#else
-static int gethawkbitstatus(cmd_t __attribute__((__unused__)) *cmd,
-			    int __attribute__((__unused__)) argc,
-			    char **argv) {
-	fprintf(stderr, "%s: JSON not available, exiting..\n", argv[1]);
-	return 1;
-}
-#endif
 
 static int sendaes(cmd_t *cmd, int argc, char *argv[]) {
 	char *key, *ivt;