1 /*
2  * Copyright (c) 2020 Linumiz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/shell/shell.h>
8 #include <zephyr/drivers/flash.h>
9 #include <zephyr/dfu/mcuboot.h>
10 #include <zephyr/dfu/flash_img.h>
11 #include <zephyr/sys/reboot.h>
12 #include <zephyr/mgmt/hawkbit.h>
13 #include "hawkbit_firmware.h"
14 #include "hawkbit_device.h"
15 
cmd_run(const struct shell * sh,size_t argc,char ** argv)16 static void cmd_run(const struct shell *sh, size_t argc, char **argv)
17 {
18 	ARG_UNUSED(argc);
19 	ARG_UNUSED(argv);
20 
21 	shell_fprintf(sh, SHELL_INFO, "Starting Hawkbit run...\n");
22 	switch (hawkbit_probe()) {
23 	case HAWKBIT_UNCONFIRMED_IMAGE:
24 		shell_fprintf(
25 			sh, SHELL_ERROR,
26 			"Image is unconfirmed."
27 			"Rebooting to revert back to previous confirmed image\n");
28 		sys_reboot(SYS_REBOOT_WARM);
29 		break;
30 
31 	case HAWKBIT_CANCEL_UPDATE:
32 		shell_fprintf(sh, SHELL_INFO,
33 			      "Hawkbit update Cancelled from server\n");
34 		break;
35 
36 	case HAWKBIT_NO_UPDATE:
37 		shell_fprintf(sh, SHELL_INFO, "No update found\n");
38 		break;
39 
40 	case HAWKBIT_OK:
41 		shell_fprintf(sh, SHELL_INFO, "Image Already updated\n");
42 		break;
43 
44 	case HAWKBIT_UPDATE_INSTALLED:
45 		shell_fprintf(sh, SHELL_INFO, "Update Installed\n");
46 		break;
47 
48 	case HAWKBIT_DOWNLOAD_ERROR:
49 		shell_fprintf(sh, SHELL_INFO, "Download Error\n");
50 		break;
51 
52 	case HAWKBIT_NETWORKING_ERROR:
53 		shell_fprintf(sh, SHELL_INFO, "Networking Error\n");
54 		break;
55 
56 	case HAWKBIT_METADATA_ERROR:
57 		shell_fprintf(sh, SHELL_INFO, "Metadata Error\n");
58 		break;
59 
60 	default:
61 		shell_fprintf(sh, SHELL_ERROR, "Invalid response\n");
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_fprintf(sh, SHELL_NORMAL, "Unique device id: %s\n", device_id);
79 	shell_fprintf(sh, SHELL_NORMAL, "Firmware Version: %s\n",
80 		      firmware_version);
81 
82 	return 0;
83 }
84 
85 SHELL_STATIC_SUBCMD_SET_CREATE(
86 	sub_hawkbit,
87 	SHELL_CMD(info, NULL, "Dump Hawkbit information", cmd_info),
88 	SHELL_CMD(run, NULL, "Trigger an Hawkbit update run", cmd_run),
89 	SHELL_SUBCMD_SET_END);
90 
91 SHELL_CMD_REGISTER(hawkbit, &sub_hawkbit, "Hawkbit commands", NULL);
92