diff mbox series

[6/7] tools: enhance swupdate-hawkbitcfg

Message ID 20200202133646.23835-6-sbabic@denx.de
State Accepted
Headers show
Series None | expand

Commit Message

Stefano Babic Feb. 2, 2020, 1:36 p.m. UTC
Add example for enabling / disabling suricatta daemon via IPC.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 doc/source/swupdate-hawkbitcfg.rst | 18 +++++--
 tools/swupdate-hawkbitcfg.c        | 87 ++++++++++++++++++++++++------
 2 files changed, 87 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/doc/source/swupdate-hawkbitcfg.rst b/doc/source/swupdate-hawkbitcfg.rst
index 70b8ea5..2c13382 100644
--- a/doc/source/swupdate-hawkbitcfg.rst
+++ b/doc/source/swupdate-hawkbitcfg.rst
@@ -2,10 +2,22 @@  hawkbitcfg
 =============
 
 sendtohawkbit is a small tool that tries to connect to a running instance
-of SWUpdate and configures Hawkbit's parameter. Currently, only the the polling time
-is supported.
+of SWUpdate and configures Hawkbit's parameter.
+
+SYNOPYS
+-------
+
+swupdate-hawkbitcfg [option]
+
 
 DESCRIPTION
 -----------
 
-swupdate-hawkbitcfg <polling time>
+-h
+        help
+-p
+        allows to set the polling time (in seconds)
+-e
+        enable suricatta mode
+-d
+        disable suricatta mode
diff --git a/tools/swupdate-hawkbitcfg.c b/tools/swupdate-hawkbitcfg.c
index 6f44a53..a8ea37f 100644
--- a/tools/swupdate-hawkbitcfg.c
+++ b/tools/swupdate-hawkbitcfg.c
@@ -19,20 +19,55 @@ 
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <getopt.h>
 #include "network_ipc.h"
 
-static void usage(char *program) {
-	printf("%s <polling interval 0=from server> ..\n", program);
+static struct option long_options[] = {
+	{"help", no_argument, NULL, 'h'},
+	{"polling-time", required_argument, NULL, 'p'},
+	{"enable", no_argument, NULL, 'e'},
+	{"disable", no_argument, NULL, 'd'},
+	{NULL, 0, NULL, 0}
+};
+
+static void usage(char *programname)
+{
+	fprintf(stdout, "%s (compiled %s)\n", programname, __DATE__);
+	fprintf(stdout, "Usage %s [OPTION]\n",
+			programname);
+	fprintf(stdout,
+		" -p, --polling-time      : Set polling time (0=from server) to ask the backend server\n"
+		" -e, --enable            : Enable polling of backend server\n"
+		" -d, --disable           : Disable polling of backend server\n"
+		" -h, --help              : print this help and exit\n"
+		);
+}
+
+static void send_msg(ipc_message *msg)
+{
+	int rc;
+
+	fprintf(stdout, "Sending: '%s'", msg->data.instmsg.buf);
+	rc = ipc_send_cmd(msg);
+
+	fprintf(stdout, " returned %d\n", rc);
+	if (!rc)
+		fprintf(stdout, "Server returns %s\n",
+				(msg->type == ACK) ? "ACK" : "NACK");
 }
 
 /*
  * Simple example, it does nothing but calling the library
  */
 int main(int argc, char *argv[]) {
-	int rc;
 	ipc_message msg;
 	size_t size;
 	char *buf;
+	int c;
+	unsigned long polling_time;
+	bool enable = false;
+	int opt_e = 0;
+	int opt_p = 0;
 
 	if (argc < 2) {
 		usage(argv[0]);
@@ -41,11 +76,36 @@  int main(int argc, char *argv[]) {
 
 	memset(&msg, 0, sizeof(msg));
 	msg.data.instmsg.source = SOURCE_SURICATTA;
-	msg.data.instmsg.cmd = CMD_CONFIG;
 
 	size = sizeof(msg.data.instmsg.buf);
 	buf = msg.data.instmsg.buf;
 
+	/* Process options with getopt */
+	while ((c = getopt_long(argc, argv, "p:edh",
+				long_options, NULL)) != EOF) {
+		switch (c) {
+		case 'p':
+			opt_p = 1;
+			msg.data.instmsg.cmd = CMD_CONFIG;
+			polling_time = strtoul(optarg, NULL, 10);
+			break;
+		case 'e':
+		case 'd':
+			msg.data.instmsg.cmd = CMD_ENABLE;
+			opt_e = 1;
+			enable = (c == 'e');
+			break;
+		case 'h':
+			usage(argv[0]);
+			exit(0);
+			break;
+		default:
+			usage(argv[0]);
+			exit(1);
+			break;
+		}
+	}
+
 	/*
 	 * Build a json string with the command line parameters
 	 * do not check anything, let SWUpdate
@@ -53,17 +113,14 @@  int main(int argc, char *argv[]) {
 	 * An error or a NACK is returned in
 	 * case of failure
 	 */
-
-	snprintf(buf, size, "{ \"polling\" : \"%lu\"}", strtoul(argv[1], NULL, 10));
-
-	fprintf(stdout, "Sending: '%s'", msg.data.instmsg.buf);
-
-	rc = ipc_send_cmd(&msg);
-
-	fprintf(stdout, " returned %d\n", rc);
-	if (!rc)
-		fprintf(stdout, "Server returns %s\n",
-				(msg.type == ACK) ? "ACK" : "NACK");
+	if (opt_p) {
+		snprintf(buf, size, "{ \"polling\" : \"%lu\"}", polling_time);
+		send_msg(&msg);
+	}
+	if (opt_e) {
+		snprintf(buf, size, "{ \"enable\" : %s}", enable ? "true" : "false");
+		send_msg(&msg);
+	}
 
 	exit(0);
 }