Skip to content

Commit

Permalink
Set json support as mandatory
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
sbabic committed Nov 20, 2023
1 parent 64ff18b commit 82e7532
Show file tree
Hide file tree
Showing 13 changed files with 10 additions and 110 deletions.
4 changes: 0 additions & 4 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions Makefile.deps
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions Makefile.flags
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions corelib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
5 changes: 1 addition & 4 deletions corelib/channel_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions handlers/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions include/channel_curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 0 additions & 15 deletions include/parselib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
12 changes: 0 additions & 12 deletions parser/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
17 changes: 1 addition & 16 deletions suricatta/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -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

Expand Down
18 changes: 1 addition & 17 deletions suricatta/server_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ||
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
11 changes: 1 addition & 10 deletions tools/swupdate-ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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[]) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 82e7532

Please sign in to comment.