1 /*
2  * Copyright (c) 2020 Linumiz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include <zephyr/shell/shell.h>
9 #include <zephyr/drivers/flash.h>
10 #include <zephyr/dfu/mcuboot.h>
11 #include <zephyr/dfu/flash_img.h>
12 #include <zephyr/mgmt/hawkbit/hawkbit.h>
13 #include <zephyr/mgmt/hawkbit/config.h>
14 #include <zephyr/mgmt/hawkbit/autohandler.h>
15 #include "hawkbit_firmware.h"
16 #include "hawkbit_device.h"
17 
18 LOG_MODULE_DECLARE(hawkbit, CONFIG_HAWKBIT_LOG_LEVEL);
19 
cmd_run(const struct shell * sh,size_t argc,char ** argv)20 static void cmd_run(const struct shell *sh, size_t argc, char **argv)
21 {
22 	ARG_UNUSED(argc);
23 	ARG_UNUSED(argv);
24 
25 	LOG_INF("Run started from %s", sh->name);
26 	shell_info(sh, "Starting hawkBit run...");
27 
28 	hawkbit_autohandler(false);
29 
30 	switch (hawkbit_autohandler_wait(UINT32_MAX, K_FOREVER)) {
31 	case HAWKBIT_UNCONFIRMED_IMAGE:
32 		shell_error(sh, "Image is unconfirmed."
33 				"Rebooting to revert back to previous confirmed image");
34 		break;
35 
36 	case HAWKBIT_NO_UPDATE:
37 		shell_info(sh, "No update found");
38 		break;
39 
40 	case HAWKBIT_UPDATE_INSTALLED:
41 		shell_info(sh, "Update installed");
42 		break;
43 
44 	case HAWKBIT_DOWNLOAD_ERROR:
45 		shell_error(sh, "Download error");
46 		break;
47 
48 	case HAWKBIT_NETWORKING_ERROR:
49 		shell_error(sh, "Networking error");
50 		break;
51 
52 	case HAWKBIT_METADATA_ERROR:
53 		shell_error(sh, "Metadata error");
54 		break;
55 
56 	case HAWKBIT_NOT_INITIALIZED:
57 		shell_error(sh, "hawkBit not initialized");
58 		break;
59 
60 	default:
61 		shell_error(sh, "Invalid response");
62 		break;
63 	}
64 	k_sleep(K_MSEC(1));
65 }
66 
cmd_info(const struct shell * sh,size_t argc,char * argv)67 static int cmd_info(const struct shell *sh, size_t argc, char *argv)
68 {
69 	ARG_UNUSED(argc);
70 	ARG_UNUSED(argv);
71 
72 	char device_id[DEVICE_ID_HEX_MAX_SIZE] = {0},
73 	     firmware_version[BOOT_IMG_VER_STRLEN_MAX] = {0};
74 
75 	hawkbit_get_firmware_version(firmware_version, BOOT_IMG_VER_STRLEN_MAX);
76 	hawkbit_get_device_identity(device_id, DEVICE_ID_HEX_MAX_SIZE);
77 
78 	shell_print(sh, "Action id: %d", hawkbit_get_action_id());
79 	shell_print(sh, "Unique device id: %s", device_id);
80 	shell_print(sh, "Firmware Version: %s", firmware_version);
81 	shell_print(sh, "Server address: %s", hawkbit_get_server_addr());
82 	shell_print(sh, "Server port: %d", hawkbit_get_server_port());
83 	shell_print(sh, "DDI security token: %s",
84 		    (IS_ENABLED(CONFIG_HAWKBIT_DDI_NO_SECURITY)
85 			     ? "<disabled>"
86 			     : hawkbit_get_ddi_security_token()));
87 
88 	return 0;
89 }
90 
cmd_init(const struct shell * sh,size_t argc,char * argv)91 static int cmd_init(const struct shell *sh, size_t argc, char *argv)
92 {
93 	ARG_UNUSED(argc);
94 	ARG_UNUSED(argv);
95 
96 	shell_info(sh, "Init hawkBit ...");
97 
98 	hawkbit_init();
99 
100 	return 0;
101 }
102 
cmd_reset(const struct shell * sh,size_t argc,char * argv)103 static int cmd_reset(const struct shell *sh, size_t argc, char *argv)
104 {
105 	ARG_UNUSED(argc);
106 	ARG_UNUSED(argv);
107 
108 	int ret = hawkbit_reset_action_id();
109 
110 	shell_print(sh, "Reset action id %s", (ret == 0) ? "success" : "failed");
111 
112 	return 0;
113 }
114 
115 #ifdef CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME
116 
cmd_set_addr(const struct shell * sh,size_t argc,char ** argv)117 static int cmd_set_addr(const struct shell *sh, size_t argc, char **argv)
118 {
119 	if (argc < 2) {
120 		shell_error(sh, "Invalid number of arguments");
121 		return -EINVAL;
122 	}
123 
124 	hawkbit_set_server_addr(argv[1]);
125 
126 	return 0;
127 }
128 
cmd_set_port(const struct shell * sh,size_t argc,char ** argv)129 static int cmd_set_port(const struct shell *sh, size_t argc, char **argv)
130 {
131 	if (argc < 2) {
132 		shell_error(sh, "Invalid number of arguments");
133 		return -EINVAL;
134 	}
135 
136 	hawkbit_set_server_port(atoi(argv[1]));
137 
138 	return 0;
139 }
140 
141 #ifndef CONFIG_HAWKBIT_DDI_NO_SECURITY
cmd_set_token(const struct shell * sh,size_t argc,char ** argv)142 static int cmd_set_token(const struct shell *sh, size_t argc, char **argv)
143 {
144 	if (argc < 2) {
145 		shell_error(sh, "Invalid number of arguments");
146 		return -EINVAL;
147 	}
148 
149 	hawkbit_set_ddi_security_token(argv[1]);
150 
151 	return 0;
152 }
153 #endif /* CONFIG_HAWKBIT_DDI_NO_SECURITY */
154 
155 SHELL_STATIC_SUBCMD_SET_CREATE(
156 	sub_hawkbit_set,
157 	SHELL_CMD(addr, NULL, "Set hawkBit server address", cmd_set_addr),
158 	SHELL_CMD(port, NULL, "Set hawkBit server port", cmd_set_port),
159 #ifndef CONFIG_HAWKBIT_DDI_NO_SECURITY
160 	SHELL_CMD(ddi_token, NULL, "Set hawkBit DDI Security token", cmd_set_token),
161 #endif
162 	SHELL_SUBCMD_SET_END);
163 #endif /* CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME */
164 
165 SHELL_STATIC_SUBCMD_SET_CREATE(
166 	sub_hawkbit,
167 	SHELL_CMD(info, NULL, "Dump hawkBit information", cmd_info),
168 	SHELL_CMD(init, NULL, "Initialize hawkBit", cmd_init),
169 	SHELL_CMD(run, NULL, "Trigger an hawkBit update run", cmd_run),
170 	SHELL_CMD(reset, NULL, "Reset the hawkBit action id", cmd_reset),
171 #ifdef CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME
172 	SHELL_CMD(set, &sub_hawkbit_set, "Set hawkBit settings", NULL),
173 #endif
174 	SHELL_SUBCMD_SET_END);
175 
176 SHELL_CMD_REGISTER(hawkbit, &sub_hawkbit, "hawkBit commands", NULL);
177