-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
245 lines (197 loc) · 5.67 KB
/
main.c
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
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
/** @file
* @brief Nordic UART Bridge Service (NUS) sample
*/
#include <stdio.h>
#include <zephyr/types.h>
#include <zephyr.h>
#include <drivers/uart.h>
#include <device.h>
#include <soc.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/hci.h>
#include <bluetooth/services/nus.h>
#include <settings/settings.h>
#include <logging/log.h>
#include <timeslot.h>
#include <proprietary_rf.h>
#define LOG_MODULE_NAME peripheral_uart
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define DESIRED_CONN_INTERVAL 28
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
static struct bt_conn *current_conn;
static struct bt_conn *auth_conn;
static bool timeslot_running;
static struct timeslot_config timeslot_config = TS_DEFAULT_CONFIG;
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};
static const struct bt_data sd[] = {
BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
};
void error(void)
{
LOG_ERR("Error handler");
while (true) {
/* Spin for ever */
k_sleep(K_MSEC(1000));
}
}
static void connected(struct bt_conn *conn, uint8_t err)
{
char addr[BT_ADDR_LE_STR_LEN];
if (err) {
LOG_ERR("Connection failed (err %u)", err);
return;
}
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Connected %s", log_strdup(addr));
current_conn = bt_conn_ref(conn);
}
static void 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 %u)", log_strdup(addr), reason);
if (auth_conn) {
bt_conn_unref(auth_conn);
auth_conn = NULL;
}
if (current_conn) {
bt_conn_unref(current_conn);
current_conn = NULL;
}
int err = timeslot_stop();
if (err) {
LOG_ERR("timeslot_stop failed (err=%d)", err);
error();
}
}
static void conn_param_updated(struct bt_conn *conn, uint16_t interval,
uint16_t latency, uint16_t timeout)
{
/* NOTE: This may be called multiple times at the beginning of the connection. */
LOG_INF("Connection params updated: (interval=%d, SL=%d, timeout=%d)",
interval, latency, timeout);
int err;
if (DESIRED_CONN_INTERVAL != interval) {
LOG_INF("Requesting new Connection Interval");
struct bt_le_conn_param param = {
.interval_min = DESIRED_CONN_INTERVAL,
.interval_max = DESIRED_CONN_INTERVAL,
.latency = latency,
.timeout = timeout,
};
err = bt_conn_le_param_update(conn, ¶m);
if (err == -EALREADY) {
/* Connection parameters are already set. */
err = 0;
}
if (err) {
LOG_ERR("bt_conn_le_param_update failed (err=%d)", err);
}
} else {
if (!timeslot_running) {
err = timeslot_start(TS_LEN_US);
if (err) {
LOG_ERR("timeslot_start failed (err=%d)", err);
} else {
timeslot_running = true;
}
}
}
}
static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
.le_param_updated = conn_param_updated,
};
static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data,
uint16_t len)
{
char addr[BT_ADDR_LE_STR_LEN] = {0};
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, ARRAY_SIZE(addr));
LOG_INF("Received data from: %s", log_strdup(addr));
}
static void bt_nus_enabled_cb(enum bt_nus_send_status status)
{
switch (status) {
case BT_NUS_SEND_STATUS_ENABLED:
LOG_INF("NUS TX CCCD enabled");
break;
case BT_NUS_SEND_STATUS_DISABLED:
LOG_INF("NUX TX CCCD disabled");
break;
default:
break;
}
}
static struct bt_nus_cb nus_cb = {
.received = bt_receive_cb,
.send_enabled = bt_nus_enabled_cb,
};
static void timeslot_err_cb(int err)
{
LOG_ERR("Timeslot session error: %d", err);
}
static void timeslot_stopped_cb(void)
{
LOG_INF("Timeslot stopped");
timeslot_running = false;
}
#if !TIMESLOT_CALLS_RADIO_IRQHANDLER
static void radio_irq_cb(void)
{
LOG_DBG("Radio_IRQHandler");
}
#endif
static struct timeslot_cb timeslot_callbacks = {
.error = timeslot_err_cb,
.start = proprietary_rf_start,
.end = proprietary_rf_end,
.skipped = proprietary_rf_skipped,
.stopped = timeslot_stopped_cb,
#if !TIMESLOT_CALLS_RADIO_IRQHANDLER
.radio_irq = radio_irq_cb
#endif
};
void main(void)
{
int err = 0;
bt_conn_cb_register(&conn_callbacks);
err = timeslot_open(×lot_config, ×lot_callbacks);
if (err) {
LOG_ERR("timeslot_open failed (err: %d)", err);
error();
}
err = bt_enable(NULL);
if (err) {
LOG_ERR("bt_enable failed (err: %d)", err);
error();
}
LOG_INF("Bluetooth initialized");
if (IS_ENABLED(CONFIG_SETTINGS)) {
settings_load();
}
err = bt_nus_init(&nus_cb);
if (err) {
LOG_ERR("Failed to initialize UART service (err: %d)", err);
error();
}
err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (err) {
LOG_ERR("Advertising failed to start (err %d)", err);
error();
}
for (;;) {
k_sleep(K_MSEC(500));
}
}