-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
274 lines (214 loc) · 6.97 KB
/
Copy pathmain.c
File metadata and controls
274 lines (214 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (c) 2025 Golioth, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/dhcpv4.h>
#include <golioth/client.h>
#include <golioth/gateway.h>
#include <samples/common/sample_credentials.h>
#include <pouch/transport/gatt/common/types.h>
#include <pouch_gateway/bt/connect.h>
#include <pouch_gateway/bt/scan.h>
#include <pouch_gateway/cert.h>
#include <pouch_gateway/downlink.h>
#include <pouch_gateway/uplink.h>
#include <git_describe.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(main);
struct golioth_client *client;
#ifdef CONFIG_POUCH_GATEWAY_CLOUD
static K_SEM_DEFINE(connected, 0, 1);
static void on_client_event(struct golioth_client *client,
enum golioth_client_event event,
void *arg)
{
bool is_connected = (event == GOLIOTH_CLIENT_EVENT_CONNECTED);
if (is_connected)
{
k_sem_give(&connected);
}
LOG_INF("Golioth client %s", is_connected ? "connected" : "disconnected");
}
static void connect_golioth_client(void)
{
const struct golioth_client_config *client_config = golioth_sample_credentials_get();
if (client_config == NULL || client_config->credentials.psk.psk_id_len == 0
|| client_config->credentials.psk.psk_len == 0)
{
LOG_ERR("No credentials found.");
LOG_ERR(
"Please store your credentials with the following commands, then reboot the device.");
LOG_ERR("\tsettings set golioth/psk-id <your-psk-id>");
LOG_ERR("\tsettings set golioth/psk <your-psk>");
return;
}
client = golioth_client_create(client_config);
golioth_client_register_event_callback(client, on_client_event, NULL);
}
#ifdef CONFIG_NRF_MODEM
#include <modem/lte_lc.h>
static void lte_handler(const struct lte_lc_evt *const evt)
{
if (evt->type == LTE_LC_EVT_NW_REG_STATUS)
{
if ((evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME)
|| (evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_ROAMING))
{
if (!client)
{
connect_golioth_client();
}
}
}
}
#endif /* CONFIG_NRF_MODEM */
#ifdef CONFIG_MODEM_HL7800
#define NET_MGMT_MASK (NET_EVENT_DNS_SERVER_ADD | NET_EVENT_L4_DISCONNECTED)
#include <zephyr/net/conn_mgr_monitor.h>
#include <zephyr/net/conn_mgr_connectivity.h>
#include <zephyr/net/net_mgmt.h>
static K_SEM_DEFINE(network_connected, 0, 1);
static struct net_mgmt_event_callback cb;
static void net_mgmt_cb(struct net_mgmt_event_callback *cb, uint64_t event, struct net_if *iface)
{
switch (event)
{
case NET_EVENT_DNS_SERVER_ADD:
LOG_INF("Network connectivity established and IP address assigned");
k_sem_give(&network_connected);
break;
case NET_EVENT_L4_DISCONNECTED:
break;
default:
break;
}
}
void wait_for_network(void)
{
net_mgmt_init_event_callback(&cb, net_mgmt_cb, NET_MGMT_MASK);
net_mgmt_add_event_callback(&cb);
conn_mgr_mon_resend_status();
LOG_INF("Waiting for network connection...");
k_sem_take(&network_connected, K_FOREVER);
}
#endif /* CONFIG_MODEM_HL7800 */
static void connect_to_cloud(void)
{
#if defined(CONFIG_NRF_MODEM)
LOG_INF("Connecting to LTE, this may take some time...");
lte_lc_connect_async(lte_handler);
#else
#if defined(CONFIG_NET_L2_ETHERNET) && defined(CONFIG_NET_DHCPV4)
net_dhcpv4_start(net_if_get_default());
#elif defined(CONFIG_MODEM_HL7800)
wait_for_network();
#endif
connect_golioth_client();
#endif
k_sem_take(&connected, K_FOREVER);
}
#else /* CONFIG_POUCH_GATEWAY_CLOUD */
static inline void connect_to_cloud(void) {}
#endif /* CONFIG_POUCH_GATEWAY_CLOUD */
static void bt_connected(struct bt_conn *conn, uint8_t err)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (err)
{
LOG_ERR("Failed to connect to %s %u %s", addr, err, bt_hci_err_to_str(err));
bt_conn_unref(conn);
pouch_gateway_scan_start();
return;
}
LOG_INF("Connected: %s", addr);
err = bt_conn_set_security(conn, BT_SECURITY_L2);
if (err)
{
LOG_ERR("Failed to set security (%d).", err);
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
return;
}
pouch_gateway_bt_start(conn);
}
static void bt_disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Disconnected: %s, reason 0x%02x %s", addr, reason, bt_hci_err_to_str(reason));
pouch_gateway_bt_stop(conn);
bt_conn_unref(conn);
pouch_gateway_scan_start();
}
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
{
LOG_INF("BT security changed to level %u, err %s(%u)", level, bt_security_err_to_str(err), err);
}
BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = bt_connected,
.disconnected = bt_disconnected,
.security_changed = security_changed,
};
static void auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Pairing cancelled: %s", addr);
}
static struct bt_conn_auth_cb auth_cb = {
.cancel = auth_cancel,
};
static void pairing_complete(struct bt_conn *conn, bool bonded)
{
LOG_INF("Pairing Complete");
}
static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason)
{
LOG_WRN("Pairing Failed (%d). Disconnecting.", reason);
bt_conn_disconnect(conn,
(reason == BT_SECURITY_ERR_PAIR_NOT_ALLOWED) ? BT_HCI_ERR_PAIRING_NOT_ALLOWED
: BT_HCI_ERR_AUTH_FAIL);
}
static struct bt_conn_auth_info_cb auth_info_cb = {
.pairing_complete = pairing_complete,
.pairing_failed = pairing_failed,
};
void pouch_gateway_bt_finished(struct bt_conn *conn)
{
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
}
int main(void)
{
LOG_INF("Gateway Version: " STRINGIFY(GIT_DESCRIBE));
LOG_INF("Pouch BLE Transport Protocol Version: %d", POUCH_GATT_VERSION);
connect_to_cloud();
pouch_gateway_cert_module_on_connected(client);
pouch_gateway_uplink_module_init(client);
pouch_gateway_downlink_module_init(client);
int err = bt_enable(NULL);
if (err)
{
LOG_ERR("Bluetooth init failed (err %d)", err);
return err;
}
if (IS_ENABLED(CONFIG_BT_SMP))
{
bt_conn_auth_cb_register(&auth_cb);
bt_conn_auth_info_cb_register(&auth_info_cb);
}
LOG_INF("Bluetooth initialized");
pouch_gateway_scan_start();
#ifdef CONFIG_POUCH_GATEWAY_CLOUD
while (true)
{
k_sem_take(&connected, K_FOREVER);
pouch_gateway_cert_module_on_connected(client);
}
#endif
return 0;
}