diff mbox series

[v1,2/8] suricatta: add basic status request

Message ID 20211015082457.6804-3-roland.gaudig-oss@weidmueller.com
State Changes Requested
Headers show
Series suricatta: ipc: add request to get hawkBit server status | expand

Commit Message

Roland Gaudig Oct. 15, 2021, 8:24 a.m. UTC
From: Roland Gaudig <roland.gaudig@weidmueller.com>

Extend the SWupdate API for external programs with a new IPC protocol
for retrieving the hawkBit server status from Suricatta.
Therefore, a new command CMD_GET_STATUS is added.

Signed-off-by: Roland Gaudig <roland.gaudig@weidmueller.com>
---

 include/network_ipc.h      |  3 ++-
 suricatta/server_hawkbit.c | 26 +++++++++++++++++++++++++-
 suricatta/server_hawkbit.h |  2 ++
 3 files changed, 29 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/include/network_ipc.h b/include/network_ipc.h
index f370ccf..6dc2521 100644
--- a/include/network_ipc.h
+++ b/include/network_ipc.h
@@ -48,7 +48,8 @@  typedef enum {
 enum {
 	CMD_ACTIVATION,	/* this returns the answer if a SW can be activated */
 	CMD_CONFIG,
-	CMD_ENABLE	/* Enable or disable suricatta mode */
+	CMD_ENABLE,	/* Enable or disable suricatta mode */
+	CMD_GET_STATUS
 };
 
 enum run_type {
diff --git a/suricatta/server_hawkbit.c b/suricatta/server_hawkbit.c
index f8f560e..6dd1f8c 100644
--- a/suricatta/server_hawkbit.c
+++ b/suricatta/server_hawkbit.c
@@ -548,7 +548,12 @@  static server_op_res_t server_get_device_info(channel_t *channel, channel_data_t
 		result = SERVER_EINIT;
 		goto cleanup;
 	}
-	if ((result = map_channel_retcode(channel->get(channel, (void *)channel_data))) !=
+
+	channel_op_res_t ch_response = channel->get(channel, (void *)channel_data);
+
+	server_hawkbit.server_status = ch_response;
+	server_hawkbit.server_status_time = time(NULL);
+	if ((result = map_channel_retcode(ch_response)) !=
 	    SERVER_OK) {
 		goto cleanup;
 	}
@@ -2054,6 +2059,22 @@  static server_op_res_t server_configuration_ipc(ipc_message *msg)
 	return SERVER_OK;
 }
 
+static server_op_res_t server_status_ipc(ipc_message *msg)
+{
+	struct timeval tv = {
+		.tv_sec = server_hawkbit.server_status_time,
+		.tv_usec = 0
+	};
+
+	sprintf(msg->data.procmsg.buf,
+		"{\"server\":{\"status\":%d,\"time\":\"%s\"}}",
+		server_hawkbit.server_status,
+		swupdate_time_iso8601(&tv));
+	msg->data.procmsg.len = strlen(msg->data.procmsg.buf);
+
+	return SERVER_OK;
+}
+
 server_op_res_t server_ipc(ipc_message *msg)
 {
 	server_op_res_t result = SERVER_OK;
@@ -2065,6 +2086,9 @@  server_op_res_t server_ipc(ipc_message *msg)
 	case CMD_CONFIG:
 		result = server_configuration_ipc(msg);
 		break;
+	case CMD_GET_STATUS:
+		result = server_status_ipc(msg);
+		break;
 	default:
 		result = SERVER_EERR;
 		break;
diff --git a/suricatta/server_hawkbit.h b/suricatta/server_hawkbit.h
index 67f1c31..86627ee 100644
--- a/suricatta/server_hawkbit.h
+++ b/suricatta/server_hawkbit.h
@@ -45,6 +45,8 @@  typedef struct {
 	char *cached_file;
 	bool usetokentodwl;
 	unsigned int initial_report_resend_period;
+	int server_status;
+	time_t server_status_time;
 } server_hawkbit_t;
 
 extern server_hawkbit_t server_hawkbit;