diff mbox series

[2/2] Allow hawkbit identify option to be repeated

Message ID 20221017114950.1296055-1-mirza.kapetanovic@gmail.com
State Changes Requested
Delegated to: Stefano Babic
Headers show
Series [1/1] Add update hawkbit identify option to ipc tools | expand

Commit Message

Mirza Kapetanovic Oct. 17, 2022, 11:49 a.m. UTC
Hi,

Additional changes which now adds support for multiple -i options at once.

Usage: swupdate-ipc hawkbitcfg -i Name1=Value1 -i Name2=Value2 -i NameN=ValueN

Signed-off-by: Mirza Kapetanovic <mirza.kapetanovic@gmail.com>
---
 tools/swupdate-ipc.c | 49 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 42 insertions(+), 7 deletions(-)

Comments

Stefano Babic Feb. 5, 2023, 11 a.m. UTC | #1
Hi Mirza,

sorry for very late answer:

On 17.10.22 13:49, Mirza Kapetanovic wrote:
> Hi,

This is added to the commit message, and it will be integrated when 
apply. If you want to add some comments, just put the, after the "---" 
line - they are ignored by git-am.


> 
> Additional changes which now adds support for multiple -i options at once.


> 
> Usage: swupdate-ipc hawkbitcfg -i Name1=Value1 -i Name2=Value2 -i NameN=ValueN
> 
> Signed-off-by: Mirza Kapetanovic <mirza.kapetanovic@gmail.com>
> ---
>   tools/swupdate-ipc.c | 49 +++++++++++++++++++++++++++++++++++++-------
>   1 file changed, 42 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/swupdate-ipc.c b/tools/swupdate-ipc.c
> index eb1f33a..778bc99 100644
> --- a/tools/swupdate-ipc.c
> +++ b/tools/swupdate-ipc.c
> @@ -45,6 +45,11 @@ typedef struct cmd_t {
>   	help usage;
>   } cmd_t;
>   
> +typedef struct {
> +	const char *name;
> +	const char *value;
> +} identify_entry_t;
> +
>   /*
>    * usage functions for each command
>    */
> @@ -79,7 +84,7 @@ static void usage_hawkbitcfg(const char *program) {
>   	fprintf(stdout,"\t %s \n", program);
>   	fprintf(stdout,
>   		"\t\t-p, --polling-time                 : Set polling time (0=from server) to ask the backend server\n"
> -		"\t\t-i, --identify <name>=<value>      : Set custom device attribute sent to backend server\n"
> +		"\t\t-i, --identify <name>=<value>      : Set custom device attribute sent to backend server (may occur more than once)\n"

Patch does not apply at all on top of current development, it looks like 
you had some other own patches in your tree. Could you check, rework and 
post a new version ? Thanks !

Best regards,
Stefano Babic

>   		"\t\t-e, --enable                       : Enable polling of backend server\n"
>   		"\t\t-d, --disable                      : Disable polling of backend server\n"
>   		"\t\t-t, --trigger                      : Enable and check for update\n"
> @@ -150,8 +155,8 @@ static int hawkbitcfg(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *a
>   	char *buf;
>   	int c;
>   	unsigned long polling_time;
> -	char *identify_name;
> -	char *identify_value;
> +	const int MAX_IDENTIFY_ENTRIES = 32;
> +	identify_entry_t identify_entries[MAX_IDENTIFY_ENTRIES];
>   	bool enable = false;
>   	bool trigger = false;
>   	int opt_e = 0;
> @@ -175,15 +180,21 @@ static int hawkbitcfg(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *a
>   			polling_time = strtoul(optarg, NULL, 10);
>   			break;
>   		case 'i':
> -			opt_i = 1;
>   			msg.data.procmsg.cmd = CMD_CONFIG;
>   
>   			char *eq = strchr(optarg, '=');
>   
>   			if (eq) {
>   				*eq = '\0';
> -				identify_name = strdup(optarg);
> -				identify_value = strdup(eq + 1);
> +
> +				if (opt_i < MAX_IDENTIFY_ENTRIES) {
> +					identify_entries[opt_i].name = strdup(optarg);
> +					identify_entries[opt_i].value  = strdup(eq + 1);
> +					opt_i++;
> +				} else {
> +					fprintf(stderr, "Error: too many entries only %d allowed.\n", MAX_IDENTIFY_ENTRIES);
> +					return 1;
> +				}
>   			} else {
>   				fprintf(stderr, "Error: expected <name>=<value> pair.\n");
>   				return 1;
> @@ -214,7 +225,31 @@ static int hawkbitcfg(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *a
>   		send_msg(&msg);
>   	}
>   	if (opt_i) {
> -		snprintf(buf, size, "{ \"identify\" : [{ \"name\": \"%s\", \"value\": \"%s\" }] }", identify_name, identify_value);
> +		int offset = snprintf(buf, size - 2, "{ \"identify\" : ["); /* Reserve 2 bytes for terminators */
> +		int result = 0;
> +
> +		for (int i = 0; i < opt_i; i++) {
> +			result = snprintf(buf + offset,
> +				size - offset - 2,
> +				"%s{ \"name\": \"%s\", \"value\": \"%s\" }",
> +				i ? "," : "",
> +				identify_entries[i].name,
> +				identify_entries[i].value);
> +
> +			if (result < 0) {
> +				fprintf(stderr, "Error: failed to format name value pair %s %s.\n",
> +					identify_entries[i].name,
> +					identify_entries[i].value);
> +				return 1;
> +			} else if (result >= size - offset - 2) {
> +				fprintf(stderr, "Error: total size of name value entries too big.\n");
> +				return 1;
> +			}
> +
> +			offset += result;
> +		}
> +
> +		snprintf(buf + offset, size - offset, "]}");
>   		msg.data.procmsg.len = strnlen(buf, size);
>   		send_msg(&msg);
>   	}
diff mbox series

Patch

diff --git a/tools/swupdate-ipc.c b/tools/swupdate-ipc.c
index eb1f33a..778bc99 100644
--- a/tools/swupdate-ipc.c
+++ b/tools/swupdate-ipc.c
@@ -45,6 +45,11 @@  typedef struct cmd_t {
 	help usage;
 } cmd_t;
 
+typedef struct {
+	const char *name;
+	const char *value;
+} identify_entry_t;
+
 /*
  * usage functions for each command
  */
@@ -79,7 +84,7 @@  static void usage_hawkbitcfg(const char *program) {
 	fprintf(stdout,"\t %s \n", program);
 	fprintf(stdout,
 		"\t\t-p, --polling-time                 : Set polling time (0=from server) to ask the backend server\n"
-		"\t\t-i, --identify <name>=<value>      : Set custom device attribute sent to backend server\n"
+		"\t\t-i, --identify <name>=<value>      : Set custom device attribute sent to backend server (may occur more than once)\n"
 		"\t\t-e, --enable                       : Enable polling of backend server\n"
 		"\t\t-d, --disable                      : Disable polling of backend server\n"
 		"\t\t-t, --trigger                      : Enable and check for update\n"
@@ -150,8 +155,8 @@  static int hawkbitcfg(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *a
 	char *buf;
 	int c;
 	unsigned long polling_time;
-	char *identify_name;
-	char *identify_value;
+	const int MAX_IDENTIFY_ENTRIES = 32;
+	identify_entry_t identify_entries[MAX_IDENTIFY_ENTRIES];
 	bool enable = false;
 	bool trigger = false;
 	int opt_e = 0;
@@ -175,15 +180,21 @@  static int hawkbitcfg(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *a
 			polling_time = strtoul(optarg, NULL, 10);
 			break;
 		case 'i':
-			opt_i = 1;
 			msg.data.procmsg.cmd = CMD_CONFIG;
 
 			char *eq = strchr(optarg, '=');
 
 			if (eq) {
 				*eq = '\0';
-				identify_name = strdup(optarg);
-				identify_value = strdup(eq + 1);
+
+				if (opt_i < MAX_IDENTIFY_ENTRIES) {
+					identify_entries[opt_i].name = strdup(optarg);
+					identify_entries[opt_i].value  = strdup(eq + 1);
+					opt_i++;
+				} else {
+					fprintf(stderr, "Error: too many entries only %d allowed.\n", MAX_IDENTIFY_ENTRIES);
+					return 1;
+				}
 			} else {
 				fprintf(stderr, "Error: expected <name>=<value> pair.\n");
 				return 1;
@@ -214,7 +225,31 @@  static int hawkbitcfg(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *a
 		send_msg(&msg);
 	}
 	if (opt_i) {
-		snprintf(buf, size, "{ \"identify\" : [{ \"name\": \"%s\", \"value\": \"%s\" }] }", identify_name, identify_value);
+		int offset = snprintf(buf, size - 2, "{ \"identify\" : ["); /* Reserve 2 bytes for terminators */
+		int result = 0;
+
+		for (int i = 0; i < opt_i; i++) {
+			result = snprintf(buf + offset,
+				size - offset - 2,
+				"%s{ \"name\": \"%s\", \"value\": \"%s\" }",
+				i ? "," : "",
+				identify_entries[i].name,
+				identify_entries[i].value);
+
+			if (result < 0) {
+				fprintf(stderr, "Error: failed to format name value pair %s %s.\n",
+					identify_entries[i].name,
+					identify_entries[i].value);
+				return 1;
+			} else if (result >= size - offset - 2) {
+				fprintf(stderr, "Error: total size of name value entries too big.\n");
+				return 1;
+			}
+
+			offset += result;
+		}
+
+		snprintf(buf + offset, size - offset, "]}");
 		msg.data.procmsg.len = strnlen(buf, size);
 		send_msg(&msg);
 	}