1 /*
2  * Copyright (c) 2020 Linumiz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/mgmt/hawkbit.h>
9 #include <zephyr/dfu/mcuboot.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/sys/reboot.h>
12 #include <zephyr/logging/log.h>
13 
14 #include "dhcp.h"
15 
16 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
17 #include <zephyr/net/tls_credentials.h>
18 #include "ca_certificate.h"
19 #endif
20 
21 LOG_MODULE_REGISTER(main);
22 
main(void)23 int main(void)
24 {
25 	int ret = -1;
26 
27 	LOG_INF("Hawkbit sample app started");
28 	LOG_INF("Image build time: " __DATE__ " " __TIME__);
29 
30 	app_dhcpv4_startup();
31 
32 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
33 	tls_credential_add(CA_CERTIFICATE_TAG, TLS_CREDENTIAL_CA_CERTIFICATE,
34 			   ca_certificate, sizeof(ca_certificate));
35 #endif
36 
37 	ret = hawkbit_init();
38 
39 	if (ret < 0) {
40 		LOG_ERR("Failed to init hawkbit");
41 	}
42 
43 #if defined(CONFIG_HAWKBIT_POLLING)
44 	LOG_INF("Starting hawkbit polling mode");
45 	hawkbit_autohandler();
46 #endif
47 
48 #if defined(CONFIG_HAWKBIT_MANUAL)
49 	LOG_INF("Starting hawkbit manual mode");
50 
51 	switch (hawkbit_probe()) {
52 	case HAWKBIT_UNCONFIRMED_IMAGE:
53 		LOG_ERR("Image is unconfirmed");
54 		LOG_ERR("Rebooting to previous confirmed image");
55 		LOG_ERR("If this image is flashed using a hardware tool");
56 		LOG_ERR("Make sure that it is a confirmed image");
57 		k_sleep(K_SECONDS(1));
58 		sys_reboot(SYS_REBOOT_WARM);
59 		break;
60 
61 	case HAWKBIT_NO_UPDATE:
62 		LOG_INF("No update found");
63 		break;
64 
65 	case HAWKBIT_CANCEL_UPDATE:
66 		LOG_INF("Hawkbit update cancelled from server");
67 		break;
68 
69 	case HAWKBIT_OK:
70 		LOG_INF("Image is already updated");
71 		break;
72 
73 	case HAWKBIT_UPDATE_INSTALLED:
74 		LOG_INF("Update installed");
75 		break;
76 
77 	case HAWKBIT_PROBE_IN_PROGRESS:
78 		LOG_INF("Hawkbit is already running");
79 		break;
80 
81 	default:
82 		break;
83 	}
84 
85 #endif
86 	return 0;
87 }
88