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 #if defined(CONFIG_MODEM_GSM_PPP)
23 #define GSM_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_gsm_ppp)
24 #define UART_NODE DT_BUS(GSM_NODE)
25 #endif
26
27 #include <zephyr/logging/log.h>
28 LOG_MODULE_REGISTER(main);
29
30 #define EVENT_MASK (NET_EVENT_L4_CONNECTED | \
31 NET_EVENT_L4_DISCONNECTED)
32
33 static struct net_mgmt_event_callback mgmt_cb;
34
start_updatehub(void)35 void start_updatehub(void)
36 {
37 #if defined(CONFIG_UPDATEHUB_SAMPLE_POLLING)
38 LOG_INF("Starting UpdateHub polling mode");
39 updatehub_autohandler();
40 #endif
41
42 #if defined(CONFIG_UPDATEHUB_SAMPLE_MANUAL)
43 LOG_INF("Starting UpdateHub manual mode");
44
45 switch (updatehub_probe()) {
46 case UPDATEHUB_HAS_UPDATE:
47 switch (updatehub_update()) {
48 case UPDATEHUB_OK:
49 ret = 0;
50 updatehub_reboot();
51 break;
52
53 default:
54 LOG_ERR("Error installing update.");
55 break;
56 }
57
58 case UPDATEHUB_NO_UPDATE:
59 LOG_INF("No update found");
60 ret = 0;
61 break;
62
63 default:
64 LOG_ERR("Invalid response");
65 break;
66 }
67 #endif
68 }
69
event_handler(struct net_mgmt_event_callback * cb,uint32_t mgmt_event,struct net_if * iface)70 static void event_handler(struct net_mgmt_event_callback *cb,
71 uint32_t mgmt_event, struct net_if *iface)
72 {
73 if ((mgmt_event & EVENT_MASK) != mgmt_event) {
74 return;
75 }
76
77 if (mgmt_event == NET_EVENT_L4_CONNECTED) {
78 LOG_INF("Network connected");
79 start_updatehub();
80 return;
81 }
82
83 if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
84 LOG_INF("Network disconnected");
85 return;
86 }
87 }
88
main(void)89 int main(void)
90 {
91 int ret;
92
93 LOG_INF("UpdateHub sample app started");
94
95 #if defined(CONFIG_UPDATEHUB_DTLS)
96 if (tls_credential_add(CA_CERTIFICATE_TAG,
97 TLS_CREDENTIAL_SERVER_CERTIFICATE,
98 server_certificate,
99 sizeof(server_certificate)) < 0) {
100 LOG_ERR("Failed to register server certificate");
101 return 0;
102 }
103
104 if (tls_credential_add(CA_CERTIFICATE_TAG,
105 TLS_CREDENTIAL_PRIVATE_KEY,
106 private_key,
107 sizeof(private_key)) < 0) {
108 LOG_ERR("Failed to register private key");
109 return 0;
110 }
111 #endif
112
113 /* The image of application needed be confirmed */
114 LOG_INF("Confirming the boot image");
115 ret = updatehub_confirm();
116 if (ret < 0) {
117 LOG_ERR("Error to confirm the image");
118 }
119
120 #if defined(CONFIG_WIFI)
121 int nr_tries = 10;
122 struct net_if *iface = net_if_get_default();
123 static struct wifi_connect_req_params cnx_params = {
124 .ssid = CONFIG_UPDATEHUB_SAMPLE_WIFI_SSID,
125 .ssid_length = 0,
126 .psk = CONFIG_UPDATEHUB_SAMPLE_WIFI_PSK,
127 .psk_length = 0,
128 .channel = 0,
129 .security = WIFI_SECURITY_TYPE_PSK,
130 };
131
132 cnx_params.ssid_length = strlen(CONFIG_UPDATEHUB_SAMPLE_WIFI_SSID);
133 cnx_params.psk_length = strlen(CONFIG_UPDATEHUB_SAMPLE_WIFI_PSK);
134
135 /* Let's wait few seconds to allow wifi device be on-line */
136 while (nr_tries-- > 0) {
137 ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &cnx_params,
138 sizeof(struct wifi_connect_req_params));
139 if (ret == 0) {
140 break;
141 }
142
143 LOG_INF("Connect request failed %d. Waiting iface be up...", ret);
144 k_msleep(500);
145 }
146
147 #elif defined(CONFIG_MODEM_GSM_PPP)
148 const struct device *const uart_dev = DEVICE_DT_GET(UART_NODE);
149
150 LOG_INF("APN '%s' UART '%s' device %p", CONFIG_MODEM_GSM_APN,
151 uart_dev->name, uart_dev);
152 #endif
153
154 net_mgmt_init_event_callback(&mgmt_cb, event_handler, EVENT_MASK);
155 net_mgmt_add_event_callback(&mgmt_cb);
156 conn_mgr_mon_resend_status();
157 return 0;
158 }
159