-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_handling.c
382 lines (312 loc) · 11.2 KB
/
client_handling.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is confidential property of Nordic Semiconductor. The use,
* copying, transfer or disclosure of such information is prohibited except by express written
* agreement with Nordic Semiconductor.
*
*/
#include "client_handling.h"
#include <string.h>
#include <stdbool.h>
#include "nrf.h"
#include "app_error.h"
#include "nrf_gpio.h"
#include "app_trace.h"
#include "ble_db_discovery.h"
#include "ble_srv_common.h"
#define MULTILINK_PERIPHERAL_BASE_UUID {{0xB3, 0x58, 0x55, 0x40, 0x50, 0x60, 0x11, 0xe3,\
0x8f, 0x96, 0x08, 0x00, 0x00, 0x00, 0x9a, 0x66}} /**< Peripheral base UUID. */
#define MULTILINK_PERIPHERAL_SERVICE_UUID 0x9001 /**< Peripheral service UUID. */
#define MULTILINK_PERIPHERAL_CHAR_UUID 0x900A /**< Peripheral characterisctics UUID. */
/**@brief Client states. */
typedef enum
{
IDLE, /**< Idle state. */
STATE_SERVICE_DISC, /**< Service discovery state. */
STATE_NOTIF_ENABLE, /**< State where the request to enable notifications is sent to the peer. . */
STATE_RUNNING, /**< Running state. */
STATE_ERROR /**< Error state. */
} client_state_t;
/**@brief Client context information. */
typedef struct
{
ble_db_discovery_t srv_db; /**< The DB Discovery module instance associated with this client. */
dm_handle_t handle; /**< Device manager identifier for the device. */
uint8_t char_index; /**< Client characteristics index in discovered service information. */
uint8_t state; /**< Client state. */
} client_t;
static client_t m_client[MAX_CLIENTS]; /**< Client context information list. */
static uint8_t m_client_count; /**< Number of clients. */
static uint8_t m_base_uuid_type; /**< UUID type. */
/**@brief Function for finding client context information based on handle.
*
* @param[in] conn_handle Connection handle.
*
* @return client context information or NULL upon failure.
*/
static uint32_t client_find(uint16_t conn_handle)
{
uint32_t i;
for (i = 0; i < MAX_CLIENTS; i++)
{
if (m_client[i].srv_db.conn_handle == conn_handle)
{
return i;
}
}
return MAX_CLIENTS;
}
/**@brief Function for service discovery.
*
* @param[in] p_client Client context information.
*/
static void service_discover(client_t * p_client)
{
uint32_t err_code;
p_client->state = STATE_SERVICE_DISC;
err_code = ble_db_discovery_start(&(p_client->srv_db),
p_client->srv_db.conn_handle);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling enabling notifications.
*
* @param[in] p_client Client context information.
*/
static void notif_enable(client_t * p_client)
{
uint32_t err_code;
ble_gattc_write_params_t write_params;
uint8_t buf[BLE_CCCD_VALUE_LEN];
p_client->state = STATE_NOTIF_ENABLE;
buf[0] = BLE_GATT_HVX_NOTIFICATION;
buf[1] = 0;
write_params.write_op = BLE_GATT_OP_WRITE_REQ;
write_params.handle = p_client->srv_db.services[0].charateristics[p_client->char_index].cccd_handle;
write_params.offset = 0;
write_params.len = sizeof(buf);
write_params.p_value = buf;
err_code = sd_ble_gattc_write(p_client->srv_db.conn_handle, &write_params);
APP_ERROR_CHECK(err_code);
}
static void db_discovery_evt_handler(ble_db_discovery_evt_t * p_evt)
{
// Find the client using the connection handle.
client_t * p_client;
uint32_t index;
bool is_valid_srv_found = false;
index = client_find(p_evt->conn_handle);
p_client = &m_client[index];
if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE)
{
uint8_t i;
for (i = 0; i < p_evt->params.discovered_db.char_count; i++)
{
ble_db_discovery_char_t * p_characteristic;
p_characteristic = &(p_evt->params.discovered_db.charateristics[i]);
if ((p_characteristic->characteristic.uuid.uuid == MULTILINK_PERIPHERAL_CHAR_UUID)
&&
(p_characteristic->characteristic.uuid.type == m_base_uuid_type))
{
// Characteristic found. Store the information needed and break.
p_client->char_index = i;
is_valid_srv_found = true;
break;
}
}
}
if (is_valid_srv_found)
{
// Enable notification.
notif_enable(p_client);
}
else
{
p_client->state = STATE_ERROR;
}
}
/**@brief Function for setting client to the running state once write response is received.
*
* @param[in] p_ble_evt Event to handle.
*/
static void on_evt_write_rsp(ble_evt_t * p_ble_evt, client_t * p_client)
{
if ((p_client != NULL) && (p_client->state == STATE_NOTIF_ENABLE))
{
if (p_ble_evt->evt.gattc_evt.params.write_rsp.handle !=
p_client->srv_db.services[0].charateristics[p_client->char_index].cccd_handle)
{
// Got response from unexpected handle.
p_client->state = STATE_ERROR;
}
else
{
p_client->state = STATE_RUNNING;
}
}
}
/**@brief Function for toggling LEDS based on received notifications.
*
* @param[in] p_ble_evt Event to handle.
*/
static void on_evt_hvx(ble_evt_t * p_ble_evt, client_t * p_client, uint32_t index)
{
if ((p_client != NULL) && (p_client->state == STATE_RUNNING))
{
if (
(
p_ble_evt->evt.gattc_evt.params.hvx.handle
==
p_client->srv_db.services[0].charateristics[p_client->char_index].characteristic.handle_value
)
&&
(p_ble_evt->evt.gattc_evt.params.hvx.len == 1)
)
{
//TODO: This is where we should set the LEDs to light up when another device is found.
// if(index < LEDS_NUMBER)
// {
// uint8_t leds[] = LEDS_LIST;
// if (p_ble_evt->evt.gattc_evt.params.hvx.data[0] == 0)
// {
// LEDS_OFF(1<<leds[index]);
// }
// else
// {
// LEDS_ON(1<<leds[index]);
// }
// }
}
}
}
/**@brief Function for handling timeout events.
*/
static void on_evt_timeout(ble_evt_t * p_ble_evt, client_t * p_client)
{
APP_ERROR_CHECK_BOOL(p_ble_evt->evt.gattc_evt.params.timeout.src
== BLE_GATT_TIMEOUT_SRC_PROTOCOL);
if (p_client != NULL)
{
p_client->state = STATE_ERROR;
}
}
ret_code_t client_handling_dm_event_handler(const dm_handle_t * p_handle,
const dm_event_t * p_event,
const ret_code_t event_result)
{
client_t * p_client = &m_client[p_handle->connection_id];
switch (p_event->event_id)
{
case DM_EVT_LINK_SECURED:
// Attempt configuring CCCD now that bonding is established.
if (event_result == NRF_SUCCESS)
{
notif_enable(p_client);
}
break;
default:
break;
}
return NRF_SUCCESS;
}
void client_handling_ble_evt_handler(ble_evt_t * p_ble_evt)
{
client_t * p_client = NULL;
uint32_t index = client_find(p_ble_evt->evt.gattc_evt.conn_handle);
if (index != MAX_CLIENTS)
{
p_client = &m_client[index];
}
switch (p_ble_evt->header.evt_id)
{
case BLE_GATTC_EVT_WRITE_RSP:
if ((p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION) ||
(p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION))
{
uint32_t err_code = dm_security_setup_req(&p_client->handle);
APP_ERROR_CHECK(err_code);
}
on_evt_write_rsp(p_ble_evt, p_client);
break;
case BLE_GATTC_EVT_HVX:
on_evt_hvx(p_ble_evt, p_client, index);
break;
case BLE_GATTC_EVT_TIMEOUT:
on_evt_timeout(p_ble_evt, p_client);
break;
default:
break;
}
if (p_client != NULL)
{
ble_db_discovery_on_ble_evt(&(p_client->srv_db), p_ble_evt);
}
}
/**@brief Database discovery module initialization.
*/
static void db_discovery_init(void)
{
uint32_t err_code = ble_db_discovery_init();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the client handling.
*/
void client_handling_init(void)
{
uint32_t err_code;
uint32_t i;
ble_uuid128_t base_uuid = MULTILINK_PERIPHERAL_BASE_UUID;
err_code = sd_ble_uuid_vs_add(&base_uuid, &m_base_uuid_type);
APP_ERROR_CHECK(err_code);
for (i = 0; i < MAX_CLIENTS; i++)
{
m_client[i].state = IDLE;
}
m_client_count = 0;
db_discovery_init();
// Register with discovery module for the discovery of the service.
ble_uuid_t uuid;
uuid.type = m_base_uuid_type;
uuid.uuid = MULTILINK_PERIPHERAL_SERVICE_UUID;
err_code = ble_db_discovery_evt_register(&uuid,
db_discovery_evt_handler);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for returning the current number of clients.
*/
uint8_t client_handling_count(void)
{
return m_client_count;
}
/**@brief Function for creating a new client.
*/
uint32_t client_handling_create(const dm_handle_t * p_handle, uint16_t conn_handle)
{
m_client[p_handle->connection_id].state = STATE_SERVICE_DISC;
m_client[p_handle->connection_id].srv_db.conn_handle = conn_handle;
m_client_count++;
m_client[p_handle->connection_id].handle = (*p_handle);
service_discover(&m_client[p_handle->connection_id]);
return NRF_SUCCESS;
}
/**@brief Function for freeing up a client by setting its state to idle.
*/
uint32_t client_handling_destroy(const dm_handle_t * p_handle)
{
uint32_t err_code = NRF_SUCCESS;
client_t * p_client = &m_client[p_handle->connection_id];
//uint32_t leds[] = LEDS_LIST;
if (p_client->state != IDLE)
{
m_client_count--;
p_client->state = IDLE;
//TODO: This is we turn off the LED after
// if(p_handle->connection_id < LEDS_NUMBER)
// LEDS_OFF( 1 << leds[p_handle->connection_id] );
}
else
{
err_code = NRF_ERROR_INVALID_STATE;
}
return err_code;
}