This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
/
aws_ble_gatt_server_demo.c
453 lines (384 loc) · 13.8 KB
/
aws_ble_gatt_server_demo.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* FreeRTOS V202007.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/**
* @file aws_ble_gatt_server_demo.c
* @brief Sample demo for a BLE GATT server
*/
#include "FreeRTOSConfig.h"
#include "iot_demo_logging.h"
#include "iot_ble_config.h"
#include "aws_ble_gatt_server_demo.h"
#include "iot_ble.h"
#include "task.h"
#include "semphr.h"
#include "platform/iot_network.h"
static BaseType_t vGattDemoSvcHook( void );
#define xServiceUUID_TYPE \
{ \
.uu.uu128 = gattDemoSVC_UUID, \
.ucType = eBTuuidType128 \
}
#define xCharCounterUUID_TYPE \
{ \
.uu.uu128 = gattDemoCHAR_COUNTER_UUID, \
.ucType = eBTuuidType128 \
}
#define xCharControlUUID_TYPE \
{ \
.uu.uu128 = gattDemoCHAR_CONTROL_UUID, \
.ucType = eBTuuidType128 \
}
#define xClientCharCfgUUID_TYPE \
{ \
.uu.uu16 = gattDemoCLIENT_CHAR_CFG_UUID, \
.ucType = eBTuuidType16 \
}
static uint16_t usHandlesBuffer[ egattDemoNbAttributes ];
static const BTAttribute_t pxAttributeTable[] =
{
{
.xServiceUUID = xServiceUUID_TYPE
},
{
.xAttributeType = eBTDbCharacteristic,
.xCharacteristic =
{
.xUuid = xCharCounterUUID_TYPE,
.xPermissions = ( IOT_BLE_CHAR_READ_PERM ),
.xProperties = ( eBTPropRead | eBTPropNotify )
}
},
{
.xAttributeType = eBTDbDescriptor,
.xCharacteristicDescr =
{
.xUuid = xClientCharCfgUUID_TYPE,
.xPermissions = ( IOT_BLE_CHAR_READ_PERM | IOT_BLE_CHAR_WRITE_PERM )
}
},
{
.xAttributeType = eBTDbCharacteristic,
.xCharacteristic =
{
.xUuid = xCharControlUUID_TYPE,
.xPermissions = ( IOT_BLE_CHAR_READ_PERM | IOT_BLE_CHAR_WRITE_PERM ),
.xProperties = ( eBTPropRead | eBTPropWrite )
}
}
};
static const BTService_t xGattDemoService =
{
.xNumberOfAttributes = egattDemoNbAttributes,
.ucInstId = 0,
.xType = eBTServiceTypePrimary,
.pusHandlesBuffer = usHandlesBuffer,
.pxBLEAttributes = ( BTAttribute_t * ) pxAttributeTable
};
/**
* @brief Task used to update the counter periodically.
*/
TaskHandle_t xCounterUpdateTask = NULL;
/**
* @brief Counter value.
*/
uint32_t ulCounter = 0;
/**
* @brief Interval in Milliseconds to update the counter.
*/
TickType_t xCounterUpdateInterval = pdMS_TO_TICKS( 1000 );
/**
* @brief Should send the counter update as a notification.
*/
BaseType_t xNotifyCounterUpdate = pdFALSE;
/**
* @brief BLE connection ID to send the notification.
*/
uint16_t usBLEConnectionID;
#define CHAR_HANDLE( svc, ch_idx ) ( ( svc )->pusHandlesBuffer[ ch_idx ] )
#define CHAR_UUID( svc, ch_idx ) ( ( svc )->pxBLEAttributes[ ch_idx ].xCharacteristic.xUuid )
/**
* @brief Callback to read request from a GATT client on the Counter value characteristic.
* Returns the current counter value.
* @param[in] pxAttribute Attribute structure for the characteristic
* @param pEventParam Event param for the read Request.
*/
void vReadCounter( IotBleAttributeEvent_t * pEventParam );
/**
* @brief Callback to receive write request from a GATT client to set the counter status.
*
* Sets the event to start/stop/reset the counter update operation and sends back a response.
*
* @param pxAttribute
* @param pEventParam
*/
void vWriteCommand( IotBleAttributeEvent_t * pEventParam );
/**
* @brief Callback to enable notification when GATT client writes a value to the Client Characteristic
* Configuration descriptor.
*
* @param pxAttribute Attribute structure for the Client Characteristic Configuration Descriptor
* @param pEventParam Write/Read event parametes
*/
void vEnableNotification( IotBleAttributeEvent_t * pEventParam );
/**
* @brief Task used to update the counter value periodically
*
* Sends a notification of counter update to GATT client if it has enabled notification via Client Characteristic
* Configuration Descriptor. Also receives events from GATT client and starts/stops/resets counter update.
*
* @param pvParams NULL
*/
static void vCounterUpdateTaskFunction( void * pvParams );
/**
* @brief Callback for BLE connect/disconnect.
*
* Stops the Counter Update task on disconnection.
*
* @param[in] xStatus Status indicating result of connect/disconnect operation
* @param[in] connId Connection Id for the connection
* @param[in] bConnected true if connection, false if disconnection
* @param[in] pxRemoteBdAddr Remote address of the BLE device which connected or disconnected
*/
static void _connectionCallback( BTStatus_t xStatus,
uint16_t connId,
bool bConnected,
BTBdaddr_t * pxRemoteBdAddr );
/* ---------------------------------------------------------------------------------------*/
#if ( IOT_BLE_ADD_CUSTOM_SERVICES == 1 )
void IotBle_AddCustomServicesCb( void )
{
vGattDemoSvcHook();
}
#endif
static const IotBleAttributeEventCallback_t pxCallBackArray[ egattDemoNbAttributes ] =
{
NULL,
vReadCounter,
vEnableNotification,
vWriteCommand
};
/*-----------------------------------------------------------*/
void vCounterUpdateTaskFunction( void * pvParams )
{
uint32_t xBitsToWait = EVENT_BIT( eGattDemoStart ) | EVENT_BIT( eGattDemoStop ) | EVENT_BIT( eGattDemoReset );
BaseType_t xStarted = pdFALSE;
IotBleAttributeData_t xAttrData = { 0 };
IotBleEventResponse_t xResp;
uint32_t ulSetBits = 0;
xAttrData.handle = CHAR_HANDLE( &xGattDemoService, egattDemoCharCounter );
xAttrData.uuid = CHAR_UUID( &xGattDemoService, egattDemoCharCounter );
xResp.pAttrData = &xAttrData;
xResp.attrDataOffset = 0;
xResp.eventStatus = eBTStatusSuccess;
xResp.rspErrorStatus = eBTRspErrorNone;
( void ) pvParams;
while( 1 )
{
( void ) xTaskNotifyWait( xBitsToWait, 0, &ulSetBits, xCounterUpdateInterval );
if( ulSetBits & EVENT_BIT( eGattDemoReset ) )
{
ulCounter = 0;
}
else if( ulSetBits & EVENT_BIT( eGattDemoStart ) )
{
xStarted = pdTRUE;
}
else if( ulSetBits & EVENT_BIT( eGattDemoStop ) )
{
xStarted = pdFALSE;
}
if( xStarted == pdTRUE )
{
ulCounter++;
if( xNotifyCounterUpdate == pdTRUE )
{
xAttrData.pData = ( uint8_t * ) &ulCounter;
xAttrData.size = sizeof( ulCounter );
( void ) IotBle_SendIndication( &xResp, usBLEConnectionID, false );
}
}
}
vTaskDelete( NULL );
}
/*-----------------------------------------------------------*/
int vGattDemoSvcInit( bool awsIotMqttMode,
const char * pIdentifier,
void * pNetworkServerInfo,
void * pNetworkCredentialInfo,
const IotNetworkInterface_t * pNetworkInterface )
{
int status = EXIT_SUCCESS;
while( 1 )
{
vTaskDelay( 1000 );
}
return status;
}
static BaseType_t vGattDemoSvcHook( void )
{
BaseType_t xRet = pdFALSE;
BTStatus_t xStatus;
IotBleEventsCallbacks_t xCallback;
/* Select the handle buffer. */
xStatus = IotBle_CreateService( ( BTService_t * ) &xGattDemoService, ( IotBleAttributeEventCallback_t * ) pxCallBackArray );
if( xStatus == eBTStatusSuccess )
{
xRet = pdTRUE;
}
if( xRet == pdTRUE )
{
xCallback.pConnectionCb = _connectionCallback;
if( IotBle_RegisterEventCb( eBLEConnection, xCallback ) != eBTStatusSuccess )
{
xRet = pdFAIL;
}
}
if( xRet == pdTRUE )
{
xRet = xTaskCreate(
vCounterUpdateTaskFunction,
"GattDemoCounterTask",
configMINIMAL_STACK_SIZE * 4,
NULL,
tskIDLE_PRIORITY,
&xCounterUpdateTask );
}
return xRet;
}
/*-----------------------------------------------------------*/
void vGattDemoSvcStart( void )
{
( void ) xTaskNotify( xCounterUpdateTask, EVENT_BIT( eGattDemoStart ), eSetBits );
}
/*-----------------------------------------------------------*/
void vGattDemoSvcStop( void )
{
( void ) xTaskNotify( xCounterUpdateTask, EVENT_BIT( eGattDemoStop ), eSetBits );
}
/*-----------------------------------------------------------*/
void vReadCounter( IotBleAttributeEvent_t * pEventParam )
{
IotBleReadEventParams_t * pxReadParam;
IotBleAttributeData_t xAttrData = { 0 };
IotBleEventResponse_t xResp;
xResp.pAttrData = &xAttrData;
xResp.rspErrorStatus = eBTRspErrorNone;
xResp.eventStatus = eBTStatusFail;
xResp.attrDataOffset = 0;
if( pEventParam->xEventType == eBLERead )
{
pxReadParam = pEventParam->pParamRead;
xResp.pAttrData->handle = pxReadParam->attrHandle;
xResp.pAttrData->pData = ( uint8_t * ) &ulCounter;
xResp.pAttrData->size = sizeof( ulCounter );
xResp.attrDataOffset = 0;
xResp.eventStatus = eBTStatusSuccess;
IotBle_SendResponse( &xResp, pxReadParam->connId, pxReadParam->transId );
}
}
/*-----------------------------------------------------------*/
void vWriteCommand( IotBleAttributeEvent_t * pEventParam )
{
IotBleWriteEventParams_t * pxWriteParam;
IotBleAttributeData_t xAttrData = { 0 };
IotBleEventResponse_t xResp;
uint8_t ucEvent;
xResp.pAttrData = &xAttrData;
xResp.rspErrorStatus = eBTRspErrorNone;
xResp.eventStatus = eBTStatusFail;
xResp.attrDataOffset = 0;
if( ( pEventParam->xEventType == eBLEWrite ) || ( pEventParam->xEventType == eBLEWriteNoResponse ) )
{
pxWriteParam = pEventParam->pParamWrite;
xResp.pAttrData->handle = pxWriteParam->attrHandle;
if( pxWriteParam->length == 1 )
{
ucEvent = pxWriteParam->pValue[ 0 ];
xTaskNotify( xCounterUpdateTask, EVENT_BIT( ucEvent ), eSetBits );
xResp.eventStatus = eBTStatusSuccess;
}
if( pEventParam->xEventType == eBLEWrite )
{
xResp.pAttrData->pData = pxWriteParam->pValue;
xResp.attrDataOffset = pxWriteParam->offset;
xResp.pAttrData->size = pxWriteParam->length;
IotBle_SendResponse( &xResp, pxWriteParam->connId, pxWriteParam->transId );
}
}
}
/*-----------------------------------------------------------*/
void vEnableNotification( IotBleAttributeEvent_t * pEventParam )
{
IotBleWriteEventParams_t * pxWriteParam;
IotBleAttributeData_t xAttrData = { 0 };
IotBleEventResponse_t xResp;
uint16_t ucCCFGValue;
xResp.pAttrData = &xAttrData;
xResp.rspErrorStatus = eBTRspErrorNone;
xResp.eventStatus = eBTStatusFail;
xResp.attrDataOffset = 0;
if( ( pEventParam->xEventType == eBLEWrite ) || ( pEventParam->xEventType == eBLEWriteNoResponse ) )
{
pxWriteParam = pEventParam->pParamWrite;
xResp.pAttrData->handle = pxWriteParam->attrHandle;
if( pxWriteParam->length == 2 )
{
ucCCFGValue = ( pxWriteParam->pValue[ 1 ] << 8 ) | pxWriteParam->pValue[ 0 ];
if( ucCCFGValue == ( uint16_t ) ENABLE_NOTIFICATION )
{
IotLogInfo( "Enabled Notification for Read Characteristic\n" );
xNotifyCounterUpdate = pdTRUE;
usBLEConnectionID = pxWriteParam->connId;
}
else if( ucCCFGValue == 0 )
{
xNotifyCounterUpdate = pdFALSE;
}
xResp.eventStatus = eBTStatusSuccess;
}
if( pEventParam->xEventType == eBLEWrite )
{
xResp.pAttrData->pData = pxWriteParam->pValue;
xResp.pAttrData->size = pxWriteParam->length;
xResp.attrDataOffset = pxWriteParam->offset;
IotBle_SendResponse( &xResp, pxWriteParam->connId, pxWriteParam->transId );
}
}
}
/*-----------------------------------------------------------*/
static void _connectionCallback( BTStatus_t xStatus,
uint16_t connId,
bool bConnected,
BTBdaddr_t * pxRemoteBdAddr )
{
if( ( xStatus == eBTStatusSuccess ) && ( bConnected == false ) )
{
if( connId == usBLEConnectionID )
{
IotLogInfo( " Disconnected from BLE device. Stopping the counter update \n" );
vGattDemoSvcStop();
}
}
}