diff mbox series

[v3,3/8] tools: add swupdate-gethawkbitstatus

Message ID 20211027134952.73063-4-roland.gaudig-oss@weidmueller.com
State Accepted
Headers show
Series suricatta: ipc: add request to get hawkBit server status | expand

Commit Message

Roland Gaudig Oct. 27, 2021, 1:49 p.m. UTC
From: Roland Gaudig <roland.gaudig@weidmueller.com>

Add example on how to retrieve the hawkBit sserver status from
Surricatta with the CMD_GET_STATUS IPC protocol.

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

(no changes since v1)

 tools/Makefile                    |  1 +
 tools/swupdate-gethawkbitstatus.c | 70 +++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 tools/swupdate-gethawkbitstatus.c
diff mbox series

Patch

diff --git a/tools/Makefile b/tools/Makefile
index 013022b..8c495c9 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -10,6 +10,7 @@ 
 
 lib-y += \
 	 swupdate-client.o \
+	 swupdate-gethawkbitstatus.o \
 	 swupdate-progress.o \
 	 swupdate-hawkbitcfg.o \
 	 swupdate-sendtohawkbit.o \
diff --git a/tools/swupdate-gethawkbitstatus.c b/tools/swupdate-gethawkbitstatus.c
new file mode 100644
index 0000000..cd480ed
--- /dev/null
+++ b/tools/swupdate-gethawkbitstatus.c
@@ -0,0 +1,70 @@ 
+/*
+ * Copyright (C) 2021 Weidmueller Interface GmbH & Co. KG
+ * Roland Gaudig <roland.gaudig@weidmueller.com>
+ *
+ * SPDX-License-Identifier:     GPL-2.0-only
+ */
+
+/*
+ * This is a small example how to retrieve the hawkBit server status
+ * from surricata.
+ */
+
+#include <stdio.h>
+
+#if defined(CONFIG_JSON)
+#include <json-c/json.h>
+
+#include <network_ipc.h>
+
+int main(int __attribute__ ((__unused__)) argc,
+	 char __attribute__ ((__unused__)) *argv[])
+{
+	ipc_message msg;
+	struct json_object *parsed_json;
+	struct json_object *server;
+	struct json_object *status;
+	struct json_object *time;
+
+	msg.type = SWUPDATE_SUBPROCESS;
+	msg.data.procmsg.source = SOURCE_SURICATTA;
+	msg.data.procmsg.cmd = CMD_GET_STATUS;
+
+	msg.data.procmsg.buf[0] = '\0';
+	msg.data.procmsg.len = 0;
+	msg.data.procmsg.timeout = 10; /* Wait 10 s for Suricatta response */
+
+	int rc = ipc_send_cmd(&msg);
+
+	if (rc) {
+		fprintf(stderr, "Error: ipc_send_cmd failed\n");
+		exit(1);
+	}
+
+	if (msg.type == ACK) {
+		parsed_json = json_tokener_parse(msg.data.procmsg.buf);
+		json_object_object_get_ex(parsed_json, "server", &server);
+		json_object_object_get_ex(server, "status", &status);
+		json_object_object_get_ex(server, "time", &time);
+
+		printf("status: %d, time: %s\n",
+		       json_object_get_int(status),
+		       json_object_get_string(time));
+		exit(0);
+	} else {
+		printf("Error: suricatta did respond with NACK.\n");
+		exit(1);
+	}
+}
+#else
+#include <stdlib.h>
+
+#warning "swupdate-gethawkbitstatus needs json-c, replaced with dummy"
+
+int main(int __attribute__((__unused__)) argc,
+	 char __attribute__((__unused__)) **argv)
+{
+	fprintf(stderr, "json-c not available, exiting..\n");
+	exit(1);
+}
+#endif