-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinfrastructure.c
More file actions
175 lines (147 loc) · 4.99 KB
/
Copy pathinfrastructure.c
File metadata and controls
175 lines (147 loc) · 4.99 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
/** Bluetooth infrastructure for Zephyr-based devices.
* This file manages Bluetooth functionality, including advertising and connection handling.
*
* @license: GNU v3
* @maintainer: electricalgorithm @ github
*/
#include "userinterface/screens/blepairing/blepairing.h"
#include "zephyr/bluetooth/conn.h"
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>
LOG_MODULE_REGISTER(ZephyrWatch_BLE, LOG_LEVEL_INF);
static const struct bt_data m_ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_CTS_VAL)),
};
static const struct bt_data m_sd[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
};
static void start_advertisement()
{
const int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, m_ad, ARRAY_SIZE(m_ad), m_sd, ARRAY_SIZE(m_sd));
if (err) {
LOG_DBG("Advertising failed to start (err %d).", err);
return;
}
LOG_DBG("Advertising successfully started.");
}
static void process_connection(struct bt_conn* conn, uint8_t err)
{
if (err)
LOG_ERR("Connection failed (err %u).", err);
else {
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Connection established to %s.", addr);
}
}
static void process_disconnection(struct bt_conn* conn, uint8_t reason)
{
LOG_INF("Disconnected (reason 0x%02x).", reason);
}
BT_CONN_CB_DEFINE(connection_callbacks) = {
.connected = process_connection,
.disconnected = process_disconnection,
.recycled = start_advertisement,
};
char* passkey_to_string(const unsigned int passkey)
{
static char passkey_str[7];
snprintf(passkey_str, sizeof(passkey_str), "%06u", passkey);
return passkey_str;
}
static void process_passkey_display(struct bt_conn* conn, unsigned int passkey)
{
char addr[BT_ADDR_LE_STR_LEN] = { 0 };
// Write PIN to the screen.
blepairing_screen_init();
blepairing_screen_set_pin(passkey_to_string(passkey));
blepairing_screen_load();
LOG_DBG("Displaying passkey on the screen.");
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_DBG("Passkey for %s: %06u", addr, passkey);
}
static void process_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_DBG("Pairing cancelled: %s", addr);
blepairing_screen_unload();
}
static void process_pairing_complete(struct bt_conn* conn, bool bonded)
{
LOG_DBG("Pairing complete. Bonded: %s", bonded ? "OK" : "FAILURE");
blepairing_screen_unload();
}
static void process_pairing_failed(struct bt_conn* conn, enum bt_security_err reason)
{
LOG_DBG("Pairing failed. Reason: 0x%02x", reason);
bt_conn_disconnect(conn, BT_HCI_ERR_AUTH_FAIL);
blepairing_screen_unload();
}
static struct bt_conn_auth_info_cb auth_info_callbacks = {
.pairing_complete = process_pairing_complete,
.pairing_failed = process_pairing_failed,
};
static struct bt_conn_auth_cb auth_callbacks = {
.passkey_display = process_passkey_display,
.passkey_entry = NULL,
.cancel = process_auth_cancel,
};
/* The API function to enable Bluetooth and start advertisement. */
int enable_bluetooth_subsystem()
{
int err;
err = bt_enable(NULL);
if (err) {
LOG_ERR("Bluetooth init failed (err %d).", err);
return err;
}
LOG_DBG("Bluetooth initialized.");
err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY);
if (err) {
LOG_ERR("Unpairing failed (err %d).", err);
}
LOG_DBG("Unpairing successful.");
if (IS_ENABLED(CONFIG_SETTINGS)) {
settings_load();
}
err = bt_conn_auth_cb_register(&auth_callbacks);
if (err) {
LOG_ERR("Failed to register authentication callbacks (err %d).", err);
return err;
}
LOG_DBG("Authentication callback registered successfully.");
err = bt_conn_auth_info_cb_register(&auth_info_callbacks);
if (err) {
LOG_ERR("Failed to register authentication information callbacks (err %d).", err);
return err;
}
LOG_DBG("Authentication information callback registered successfully.");
start_advertisement();
return 0;
}
int disable_bluetooth_subsystem()
{
int err;
err = bt_le_adv_stop();
if (err) {
LOG_ERR("Failed to stop advertising (err %d).", err);
return err;
}
LOG_DBG("Advertising stopped successfully.");
err = bt_conn_auth_info_cb_unregister(&auth_info_callbacks);
if (err) {
LOG_ERR("Failed to unregister authentication information callbacks (err %d).", err);
return err;
}
LOG_DBG("Authentication information callback unregistered successfully.");
err = bt_disable();
if (err) {
LOG_ERR("Failed to disable Bluetooth subsystem (err %d).", err);
return err;
}
LOG_DBG("Bluetooth subsystem disabled successfully.");
return 0;
}