diff mbox series

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

Message ID 20211026084138.20087-4-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. 26, 2021, 8:41 a.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 | 56 +++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 tools/swupdate-gethawkbitstatus.c

Comments

Stefano Babic Oct. 27, 2021, 8:14 a.m. UTC | #1
Hi Roland,

On 26.10.21 10:41, Roland Gaudig wrote:
> 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 | 56 +++++++++++++++++++++++++++++++
>   2 files changed, 57 insertions(+)
>   create mode 100644 tools/swupdate-gethawkbitstatus.c
> 
> diff --git a/tools/Makefile b/tools/Makefile
> index 013022b..e2cac44 100644
> --- a/tools/Makefile
> +++ b/tools/Makefile
> @@ -14,6 +14,7 @@ lib-y += \
>   	 swupdate-hawkbitcfg.o \
>   	 swupdate-sendtohawkbit.o \
>   	 swupdate-sysrestart.o
> +lib-$(CONFIG_JSON) += swupdate-gethawkbitstatus.o

I was optimistic, this does not work and build fails.

We can fix as already done in sysrestart:

 From 4811a601280a108395aac14caf741396d4e402be Mon Sep 17 00:00:00 2001
From: Stefano Babic <sbabic@denx.de>
Date: Tue, 26 Oct 2021 22:43:33 +0200
Subject: [PATCH] Fix build swupdate-gethawkbitstatus without json-c

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
  tools/Makefile                    |  4 ++--
  tools/swupdate-gethawkbitstatus.c | 13 ++++++++++++-
  2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/tools/Makefile b/tools/Makefile
index e2cac440..38414203 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -13,8 +13,8 @@ lib-y += \
  	 swupdate-progress.o \
  	 swupdate-hawkbitcfg.o \
  	 swupdate-sendtohawkbit.o \
-	 swupdate-sysrestart.o
-lib-$(CONFIG_JSON) += swupdate-gethawkbitstatus.o
+	 swupdate-sysrestart.o \
+	 swupdate-gethawkbitstatus.o

  # # Uncomment the next lines to integrate the compiling/linking of
  # # any .c files placed alongside the above "official" tools in the
diff --git a/tools/swupdate-gethawkbitstatus.c 
b/tools/swupdate-gethawkbitstatus.c
index fb02b927..bcd594b9 100644
--- a/tools/swupdate-gethawkbitstatus.c
+++ b/tools/swupdate-gethawkbitstatus.c
@@ -11,8 +11,10 @@
   */

  #include <stdio.h>
-#include <json-c/json.h>
+#include <stdlib.h>

+#if defined(CONFIG_JSON)
+#include <json-c/json.h>
  #include <network_ipc.h>

  int main(int __attribute__ ((__unused__)) argc,
@@ -54,3 +56,12 @@ int main(int __attribute__ ((__unused__)) argc,
  		exit(1);
  	}
  }
+#else
+#warning "swupdate-gethawkbitstatus needs json-c, replaced with dummy"
+
+int main(int __attribute__((__unused__)) argc, char 
__attribute__((__unused__)) **argv)
+{
+	fprintf(stderr, "Curl not available, exiting..\n");
+	exit(1);
+}
+#endif
diff mbox series

Patch

diff --git a/tools/Makefile b/tools/Makefile
index 013022b..e2cac44 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -14,6 +14,7 @@  lib-y += \
 	 swupdate-hawkbitcfg.o \
 	 swupdate-sendtohawkbit.o \
 	 swupdate-sysrestart.o
+lib-$(CONFIG_JSON) += swupdate-gethawkbitstatus.o
 
 # # Uncomment the next lines to integrate the compiling/linking of
 # # any .c files placed alongside the above "official" tools in the
diff --git a/tools/swupdate-gethawkbitstatus.c b/tools/swupdate-gethawkbitstatus.c
new file mode 100644
index 0000000..fb02b92
--- /dev/null
+++ b/tools/swupdate-gethawkbitstatus.c
@@ -0,0 +1,56 @@ 
+/*
+ * 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>
+#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);
+	}
+}