1 /*
2 * Copyright (c) 2018-2023 O.S.Systems
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/mgmt/updatehub.h>
9 #include <zephyr/net/net_mgmt.h>
10 #include <zephyr/net/net_event.h>
11 #include <zephyr/net/conn_mgr_monitor.h>
12
13 #ifdef CONFIG_NET_L2_WIFI_MGMT
14 #include <zephyr/net/wifi_mgmt.h>
15 #endif /* CONFIG_NET_L2_WIFI_MGMT */
16
17 #if defined(CONFIG_UPDATEHUB_DTLS)
18 #include <zephyr/net/tls_credentials.h>
19 #include "c_certificates.h"
20 #endif
21
22 #include <zephyr/logging/log.h>
23 LOG_MODULE_REGISTER(main);
24
25 #define EVENT_MASK (NET_EVENT_L4_CONNECTED | \
26 NET_EVENT_L4_DISCONNECTED)
27
28 static struct net_mgmt_event_callback mgmt_cb;
29
start_updatehub(void)30 void start_updatehub(void)
31 {
32 #if defined(CONFIG_UPDATEHUB_SAMPLE_POLLING)
33 LOG_INF("Starting UpdateHub polling mode");
34 updatehub_autohandler();
35 #endif
36
37 #if defined(CONFIG_UPDATEHUB_SAMPLE_MANUAL)
38 LOG_INF("Starting UpdateHub manual mode");
39
40 switch (updatehub_probe()) {
41 case UPDATEHUB_HAS_UPDATE:
42 switch (updatehub_update()) {
43 case UPDATEHUB_OK:
44 ret = 0;
45 updatehub_reboot();
46 break;
47
48 default:
49 LOG_ERR("Error installing update.");
50 break;
51 }
52
53 case UPDATEHUB_NO_UPDATE:
54 LOG_INF("No update found");
55 ret = 0;
56 break;
57
58 default:
59 LOG_ERR("Invalid response");
60 break;
61 }
62 #endif
63 }
64
event_handler(struct net_mgmt_event_callback * cb,uint32_t mgmt_event,struct net_if * iface)65 static void event_handler(struct net_mgmt_event_callback *cb,
66 uint32_t mgmt_event, struct net_if *iface)
67 {
68 if ((mgmt_event & EVENT_MASK) != mgmt_event) {
69 return;
70 }
71
72 if (mgmt_event == NET_EVENT_L4_CONNECTED) {
73 LOG_INF("Network connected");
74 start_updatehub();
75 return;
76 }
77
78 if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
79 LOG_INF("Network disconnected");
80 return;
81 }
82 }
83
main(void)84 int main(void)
85 {
86 int ret;
87
88 LOG_INF("UpdateHub sample app started");
89
90 #if defined(CONFIG_UPDATEHUB_DTLS)
91 if (tls_credential_add(CA_CERTIFICATE_TAG,
92 TLS_CREDENTIAL_SERVER_CERTIFICATE,
93 server_certificate,
94 sizeof(server_certificate)) < 0) {
95 LOG_ERR("Failed to register server certificate");
96 return 0;
97 }
98
99 if (tls_credential_add(CA_CERTIFICATE_TAG,
100 TLS_CREDENTIAL_PRIVATE_KEY,
101 private_key,
102 sizeof(private_key)) < 0) {
103 LOG_ERR("Failed to register private key");
104 return 0;
105 }
106 #endif
107
108 /* The image of application needed be confirmed */
109 LOG_INF("Confirming the boot image");
110 ret = updatehub_confirm();
111 if (ret < 0) {
112 LOG_ERR("Error to confirm the image");
113 }
114
115 #if defined(CONFIG_WIFI)
116 int nr_tries = 10;
117 struct net_if *iface = net_if_get_default();
118 static struct wifi_connect_req_params cnx_params = {
119 .ssid = CONFIG_UPDATEHUB_SAMPLE_WIFI_SSID,
120 .ssid_length = 0,
121 .psk = CONFIG_UPDATEHUB_SAMPLE_WIFI_PSK,
122 .psk_length = 0,
123 .channel = 0,
124 .security = WIFI_SECURITY_TYPE_PSK,
125 };
126
127 cnx_params.ssid_length = strlen(CONFIG_UPDATEHUB_SAMPLE_WIFI_SSID);
128 cnx_params.psk_length = strlen(CONFIG_UPDATEHUB_SAMPLE_WIFI_PSK);
129
130 /* Let's wait few seconds to allow wifi device be on-line */
131 while (nr_tries-- > 0) {
132 ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &cnx_params,
133 sizeof(struct wifi_connect_req_params));
134 if (ret == 0) {
135 break;
136 }
137
138 LOG_INF("Connect request failed %d. Waiting iface be up...", ret);
139 k_msleep(500);
140 }
141 #endif
142
143 net_mgmt_init_event_callback(&mgmt_cb, event_handler, EVENT_MASK);
144 net_mgmt_add_event_callback(&mgmt_cb);
145 conn_mgr_mon_resend_status();
146 return 0;
147 }
148