-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_ble_service.c
413 lines (385 loc) · 14.9 KB
/
simple_ble_service.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// based on the ble_peripheral example in RTL8762C-sdk-gcc-v0.0.5.zip
// <https://www.realmcu.com/en/Home/Product/93cc0582-3a3f-4ea8-82ea-76c6504e478a>
// modified by wuwbobo2021 <https://github.com/wuwbobo2021>, <[email protected]>
#include <string.h>
#include <trace.h>
#include <gap.h>
#include "simple_ble_service.h"
T_SERVER_ID simp_service_id;
/**< Value of simple read characteristic. */
static uint8_t simple_char_read_value[SIMP_CONFIG_V1_MAX_LEN];
static uint16_t simple_char_read_len = 1;
char v1_user_descr[] = "UART Baudrate";
/**< Function pointer used to send event to application from simple profile. Initiated in simp_ble_service_add_service. */
static P_FUN_SERVER_GENERAL_CB pfn_simp_ble_service_cb = NULL;
/**< @brief profile/service definition. */
const T_ATTRIB_APPL simple_ble_service_tbl[] =
{
/* <<Primary Service>>, .. */
{
(ATTRIB_FLAG_VALUE_INCL | ATTRIB_FLAG_LE), /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_PRIMARY_SERVICE),
HI_WORD(GATT_UUID_PRIMARY_SERVICE),
LO_WORD(GATT_UUID_SIMPLE_PROFILE), /* service UUID */
HI_WORD(GATT_UUID_SIMPLE_PROFILE)
},
UUID_16BIT_SIZE, /* bValueLen */
NULL, /* p_value_context */
GATT_PERM_READ /* permissions */
},
/* <<Characteristic>> config */
{
ATTRIB_FLAG_VALUE_INCL, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHARACTERISTIC),
HI_WORD(GATT_UUID_CHARACTERISTIC),
GATT_CHAR_PROP_READ | GATT_CHAR_PROP_WRITE_NO_RSP /* characteristic properties */
/* characteristic UUID not needed here, is UUID of next attrib. */
},
1, /* bValueLen */
NULL,
GATT_PERM_READ /* permissions */
},
{
ATTRIB_FLAG_VALUE_APPL, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHAR_SIMPLE_V1_CONFIG),
HI_WORD(GATT_UUID_CHAR_SIMPLE_V1_CONFIG)
},
0, /* bValueLen */
NULL,
#if SIMP_SRV_AUTHEN_EN
GATT_PERM_READ_AUTHEN_REQ | GATT_PERM_WRITE_AUTHEN_REQ /* permissions */
#else
GATT_PERM_READ | GATT_PERM_WRITE /* permissions */
#endif
},
{
ATTRIB_FLAG_VOID | ATTRIB_FLAG_ASCII_Z, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHAR_USER_DESCR),
HI_WORD(GATT_UUID_CHAR_USER_DESCR),
},
(sizeof(v1_user_descr) - 1), /* bValueLen */
(void *)v1_user_descr,
GATT_PERM_READ /* permissions */
},
/* <<Characteristic>> write */
{
ATTRIB_FLAG_VALUE_INCL, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHARACTERISTIC),
HI_WORD(GATT_UUID_CHARACTERISTIC),
(GATT_CHAR_PROP_WRITE | GATT_CHAR_PROP_WRITE_NO_RSP) /* characteristic properties */
/* characteristic UUID not needed here, is UUID of next attrib. */
},
1, /* bValueLen */
NULL,
GATT_PERM_READ /* permissions */
},
{
ATTRIB_FLAG_VALUE_APPL, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHAR_SIMPLE_V2_WRITE),
HI_WORD(GATT_UUID_CHAR_SIMPLE_V2_WRITE)
},
0, /* bValueLen */
NULL,
#if SIMP_SRV_AUTHEN_EN
GATT_PERM_WRITE_AUTHEN_REQ /* permissions */
#else
GATT_PERM_WRITE /* permissions */
#endif
},
/* <<Characteristic>>, notify */
{
ATTRIB_FLAG_VALUE_INCL, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHARACTERISTIC),
HI_WORD(GATT_UUID_CHARACTERISTIC),
(GATT_CHAR_PROP_NOTIFY) /* characteristic properties */
/* characteristic UUID not needed here, is UUID of next attrib. */
},
1, /* bValueLen */
NULL,
GATT_PERM_READ /* permissions */
},
{
ATTRIB_FLAG_VALUE_APPL, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHAR_SIMPLE_V3_NOTIFY),
HI_WORD(GATT_UUID_CHAR_SIMPLE_V3_NOTIFY)
},
0, /* bValueLen */
NULL,
#if SIMP_SRV_AUTHEN_EN
GATT_PERM_NOTIF_IND_AUTHEN_REQ /* permissions */
#else
GATT_PERM_NOTIF_IND /* permissions */
#endif
},
/* client characteristic configuration */
{
ATTRIB_FLAG_VALUE_INCL | ATTRIB_FLAG_CCCD_APPL, /* flags */
{ /* type_value */
LO_WORD(GATT_UUID_CHAR_CLIENT_CONFIG),
HI_WORD(GATT_UUID_CHAR_CLIENT_CONFIG),
/* NOTE: this value has an instantiation for each client, a write to */
/* this attribute does not modify this default value: */
LO_WORD(GATT_CLIENT_CHAR_CONFIG_DEFAULT), /* client char. config. bit field */
HI_WORD(GATT_CLIENT_CHAR_CONFIG_DEFAULT)
},
2, /* bValueLen */
NULL,
#if SIMP_SRV_AUTHEN_EN
(GATT_PERM_READ_AUTHEN_REQ | GATT_PERM_WRITE_AUTHEN_REQ) /* permissions */
#else
(GATT_PERM_READ | GATT_PERM_WRITE) /* permissions */
#endif
},
};
/**
* @brief Set service related data from application.
*
* @param[in] param_type parameter type to set.
* @param[in] len value length to be set.
* @param[in] p_value value to set.
* @return parameter set result.
* @retval 0 false
* @retval 1 true
*/
bool simp_ble_service_set_parameter(T_SIMP_PARAM_TYPE param_type, uint16_t len, void *p_value)
{
bool ret = true;
switch (param_type)
{
default:
ret = false;
break;
case SIMPLE_BLE_SERVICE_PARAM_V1_READ_CHAR_VAL:
if (len <= SIMP_CONFIG_V1_MAX_LEN)
{
memcpy(simple_char_read_value, p_value, len);
simple_char_read_len = len;
}
else
{
ret = false;
}
break;
}
if (!ret)
{
APP_PRINT_ERROR0("simp_ble_service_set_parameter failed");
}
return ret;
}
/**
* @brief read characteristic data from service.
*
* @param service_id ServiceID of characteristic data.
* @param attrib_index Attribute index of getting characteristic data.
* @param offset Used for Blob Read.
* @param p_length length of getting characteristic data.
* @param pp_value data got from service.
* @return Profile procedure result
*/
T_APP_RESULT simp_ble_service_attr_read_cb(uint8_t conn_id, T_SERVER_ID service_id,
uint16_t attrib_index, uint16_t offset, uint16_t *p_length, uint8_t **pp_value)
{
T_APP_RESULT cause = APP_RESULT_SUCCESS;
switch (attrib_index)
{
default:
APP_PRINT_ERROR1("simp_ble_service_attr_read_cb, Attr not found, index %d", attrib_index);
cause = APP_RESULT_ATTR_NOT_FOUND;
break;
case SIMPLE_BLE_SERVICE_CHAR_V1_CONFIG_INDEX:
{
TSIMP_CALLBACK_DATA callback_data;
callback_data.msg_type = SERVICE_CALLBACK_TYPE_READ_CHAR_VALUE;
callback_data.msg_data.read_value_index = SIMP_CONFIG_V1;
callback_data.conn_id = conn_id;
if (pfn_simp_ble_service_cb)
{
pfn_simp_ble_service_cb(service_id, (void *)&callback_data);
}
*pp_value = simple_char_read_value;
*p_length = simple_char_read_len;
}
break;
}
return (cause);
}
void simple_write_post_callback(uint8_t conn_id, T_SERVER_ID service_id, uint16_t attrib_index,
uint16_t length, uint8_t *p_value)
{
APP_PRINT_INFO4("simple_write_post_callback: conn_id %d, service_id %d, attrib_index 0x%x, length %d",
conn_id, service_id, attrib_index, length);
}
/**
* @brief write characteristic data from service.
*
* @param conn_id
* @param service_id ServiceID to be written.
* @param attrib_index Attribute index of characteristic.
* @param length length of value to be written.
* @param p_value value to be written.
* @return Profile procedure result
*/
T_APP_RESULT simp_ble_service_attr_write_cb(uint8_t conn_id, T_SERVER_ID service_id,
uint16_t attrib_index, T_WRITE_TYPE write_type, uint16_t length, uint8_t *p_value,
P_FUN_WRITE_IND_POST_PROC *p_write_ind_post_proc)
{
TSIMP_CALLBACK_DATA callback_data;
T_APP_RESULT cause = APP_RESULT_SUCCESS;
APP_PRINT_INFO1("simp_ble_service_attr_write_cb write_type = 0x%x", write_type);
*p_write_ind_post_proc = simple_write_post_callback;
switch (attrib_index)
{
default:
APP_PRINT_ERROR2("simp_ble_service_attr_write_cb Error: attrib_index 0x%x, length %d",
attrib_index, length);
cause = APP_RESULT_ATTR_NOT_FOUND;
break;
case SIMPLE_BLE_SERVICE_CHAR_V1_CONFIG_INDEX:
{
/* Make sure written value size is valid. */
if (p_value == NULL)
{
cause = APP_RESULT_INVALID_VALUE_SIZE;
}
else
{
/* Notify Application. */
callback_data.msg_type = SERVICE_CALLBACK_TYPE_WRITE_CHAR_VALUE;
callback_data.conn_id = conn_id;
callback_data.msg_data.write.opcode = SIMP_CONFIG_V1;
callback_data.msg_data.write.write_type = write_type;
callback_data.msg_data.write.len = length;
callback_data.msg_data.write.p_value = p_value;
if (pfn_simp_ble_service_cb)
pfn_simp_ble_service_cb(service_id, (void *)&callback_data);
}
}
break;
case SIMPLE_BLE_SERVICE_CHAR_V2_WRITE_INDEX:
{
/* Make sure written value size is valid. */
if (p_value == NULL)
{
cause = APP_RESULT_INVALID_VALUE_SIZE;
}
else
{
/* Notify Application. */
callback_data.msg_type = SERVICE_CALLBACK_TYPE_WRITE_CHAR_VALUE;
callback_data.conn_id = conn_id;
callback_data.msg_data.write.opcode = SIMP_WRITE_V2;
callback_data.msg_data.write.write_type = write_type;
callback_data.msg_data.write.len = length;
callback_data.msg_data.write.p_value = p_value;
if (pfn_simp_ble_service_cb)
pfn_simp_ble_service_cb(service_id, (void *)&callback_data);
}
}
break;
}
return cause;
}
/**
* @brief send notification of simple notify characteristic value.
*
* @param[in] conn_id connection id
* @param[in] service_id service ID of service.
* @param[in] p_value characteristic value to notify
* @param[in] length characteristic value length to notify
* @return notification action result
* @retval 1 true
* @retval 0 false
*/
bool simp_ble_service_send_v3_notify(uint8_t conn_id, T_SERVER_ID service_id, void *p_value,
uint16_t length)
{
//APP_PRINT_INFO0("simp_ble_service_send_v3_notify");
// send notification to client
return server_send_data(conn_id, service_id, SIMPLE_BLE_SERVICE_CHAR_V3_NOTIFY_INDEX, p_value,
length,
GATT_PDU_TYPE_ANY);
}
/**
* @brief update CCCD bits from stack.
*
* @param conn_id connection id.
* @param service_id Service ID.
* @param index Attribute index of characteristic data.
* @param cccbits CCCD bits from stack.
* @return None
*/
void simp_ble_service_cccd_update_cb(uint8_t conn_id, T_SERVER_ID service_id, uint16_t index,
uint16_t cccbits)
{
TSIMP_CALLBACK_DATA callback_data;
bool is_handled = false;
callback_data.conn_id = conn_id;
callback_data.msg_type = SERVICE_CALLBACK_TYPE_INDIFICATION_NOTIFICATION;
APP_PRINT_INFO2("simp_ble_service_cccd_update_cb: index = %d, cccbits 0x%x", index, cccbits);
switch (index)
{
case SIMPLE_BLE_SERVICE_CHAR_NOTIFY_CCCD_INDEX:
{
if (cccbits & GATT_CLIENT_CHAR_CONFIG_NOTIFY)
{
// Enable Notification
callback_data.msg_data.notification_indification_index = SIMP_NOTIFY_V3_ENABLE;
}
else
{
// Disable Notification
callback_data.msg_data.notification_indification_index = SIMP_NOTIFY_V3_DISABLE;
}
is_handled = true;
}
break;
default:
break;
}
/* Notify Application. */
if (pfn_simp_ble_service_cb && (is_handled == true))
{
pfn_simp_ble_service_cb(service_id, (void *)&callback_data);
}
}
/**
* @brief Simple ble Service Callbacks.
*/
const T_FUN_GATT_SERVICE_CBS simp_ble_service_cbs =
{
simp_ble_service_attr_read_cb, // Read callback function pointer
simp_ble_service_attr_write_cb, // Write callback function pointer
simp_ble_service_cccd_update_cb // CCCD update callback function pointer
};
/**
* @brief Add simple BLE service to the BLE stack database.
*
* @param[in] p_func Callback when service attribute was read, write or cccd update.
* @return Service id generated by the BLE stack: @ref T_SERVER_ID.
* @retval 0xFF Operation failure.
* @retval others Service id assigned by stack.
*
*/
T_SERVER_ID simp_ble_service_add_service(void *p_func)
{
if (false == server_add_service(&simp_service_id,
(uint8_t *)simple_ble_service_tbl,
sizeof(simple_ble_service_tbl),
simp_ble_service_cbs))
{
APP_PRINT_ERROR0("simp_ble_service_add_service: fail");
simp_service_id = 0xff;
return simp_service_id;
}
pfn_simp_ble_service_cb = (P_FUN_SERVER_GENERAL_CB)p_func;
return simp_service_id;
}