FreeRTOS: BLE
BLE
Return to main page ↑
iot_ble.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS BLE V2.2.0
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25 
31 #ifndef IOT_BLE_H
32 #define IOT_BLE_H
33 
34 #include <stddef.h>
36 #include "bt_hal_manager.h"
37 #include "bt_hal_gatt_server.h"
38 #include "bt_hal_gatt_types.h"
39 #include "iot_ble_config.h"
41 
50 typedef struct
51 {
52  uint32_t appearance;
53  uint32_t minInterval;
54  uint32_t maxInterval;
56  char * pServiceData;
59  uint16_t manufacturerLen;
60  uint16_t serviceDataLen;
63  bool setScanRsp;
65 
70 typedef struct
71 {
72  uint32_t minInterval;
73  uint32_t maxInterval;
74  uint32_t latency;
75  uint32_t timeout;
77 
78 
83 typedef struct
84 {
88  BTSecurityLevel_t securityLevel;
90  uint16_t connId;
91  bool isBonded;
93 
98 typedef enum
99 {
107 
112 typedef struct IotBleAttributeEvent IotBleAttributeEvent_t;
113 
114 
122 typedef void (* IotBleAttributeEventCallback_t)( IotBleAttributeEvent_t * pEventParam );
123 
124 /************************************************************/
125 
130 typedef struct
131 {
132  uint16_t connId;
133  uint32_t transId;
135  uint16_t attrHandle;
136  uint16_t offset;
138 
143 typedef struct
144 {
145  uint32_t transId;
147  uint8_t * pValue;
148  uint32_t length;
149  uint16_t connId;
150  uint16_t attrHandle;
151  uint16_t offset;
152  bool needRsp;
153  bool isPrep;
155 
160 typedef struct
161 {
162  uint32_t transId;
164  uint16_t connId;
165  bool execWrite;
167 
172 typedef struct
173 {
175  uint16_t handle;
177 
182 typedef struct
183 {
185  uint16_t connId;
188 
194 {
195  union
196  {
202  };
204 };
205 
211 typedef struct
212 {
213  size_t size;
214  uint8_t * pData;
216  size_t handle;
218 
223 typedef struct
224 {
226  size_t attrDataOffset;
230 
238 typedef void (* IotBle_MtuChangedCallback_t)( uint16_t connId,
239  uint16_t mtu );
240 
249 typedef void (* IotBle_ConnectionCallback_t)( BTStatus_t status,
250  uint16_t connId,
251  bool connected,
252  BTBdaddr_t * pRemoteBdAddr );
253 
260 typedef void (* IotBle_StartAdvCallback_t)( BTStatus_t status );
261 
268 typedef void (* IotBle_StopAdvCallback_t)( BTStatus_t status );
269 
280  const BTBdaddr_t * pRemoteBdAddr,
281  IotBleConnectionParam_t * pConnectionParam,
282  uint32_t connInterval );
283 
294 typedef void (* IotBle_PairingStateChanged_t)( BTStatus_t status,
295  BTBdaddr_t * pRemoteBdAddr,
296  BTBondState_t bondstate,
297  BTSecurityLevel_t securityLevel,
298  BTAuthFailureReason_t reason );
299 
308 typedef void ( * IotBle_NumericComparisonCallback_t )( BTBdaddr_t * pRemoteBdAddr,
309  uint32_t passKey );
310 
315 typedef enum
316 {
324 
330 typedef union
331 {
337  void * pvPtr;
339 
340 
429 #if ( IOT_BLE_ADD_CUSTOM_SERVICES == 1 )
430 
436  void IotBle_AddCustomServicesCb( void );
438 #endif
439 
440 #if ( IOT_BLE_SET_CUSTOM_ADVERTISEMENT_MSG == 1 )
441 
451  void IotBle_SetCustomAdvCb( IotBleAdvertisementParams_t * pAdvParams,
452  IotBleAdvertisementParams_t * pScanParams );
454 #endif
455 
461 /* @[declare_iotble_init] */
462 BTStatus_t IotBle_Init( void );
463 /* @[declare_iotble_init] */
464 
470 /* @[declare_iotble_on] */
471 BTStatus_t IotBle_On( void );
472 /* @[declare_iotble_on] */
473 
479 /* @[declare_iotble_off] */
480 BTStatus_t IotBle_Off( void );
481 /* @[declare_iotble_off] */
482 
488 /* @[declare_iotble_startadv] */
490 /* @[declare_iotble_startadv] */
491 
499 /* @[declare_iotble_setstopadvcallback] */
501 /* @[declare_iotble_setstopadvcallback] */
502 
508 /* @[declare_iotble_stopadv] */
510 /* @[declare_iotble_stopadv] */
511 
519 /* @[declare_iotble_connparameterupdaterequest] */
521  IotBleConnectionParam_t * pConnectionParam );
522 /* @[declare_iotble_connparameterupdaterequest] */
523 
524 
537 /* @[declare_iotble_registereventcb] */
539  IotBleEventsCallbacks_t bleEventsCallbacks );
540 /* @[declare_iotble_registereventcb] */
541 
550 /* @[declare_iotble_unregistereventcb] */
552  IotBleEventsCallbacks_t bleEventsCallbacks );
553 /* @[declare_iotble_unregistereventcb] */
554 
561 /* @[declare_iotble_removebond] */
562 BTStatus_t IotBle_RemoveBond( const BTBdaddr_t * pRemoteBdAddr );
563 /* @[declare_iotble_removebond] */
564 
574 /* @[declare_iotble_createservice] */
575 BTStatus_t IotBle_CreateService( BTService_t * pService,
576  IotBleAttributeEventCallback_t pEventsCallbacks[] );
577 /* @[declare_iotble_createservice] */
578 
585 /* @[declare_iotble_deleteservice] */
586 BTStatus_t IotBle_DeleteService( BTService_t * pService );
587 /* @[declare_iotble_deleteservice] */
588 
597 /* @[declare_iotble_sendindication] */
599  uint16_t connId,
600  bool confirm );
601 /* @[declare_iotble_sendindication] */
602 
611 /* @[declare_iotble_sendresponse] */
613  uint16_t connId,
614  uint32_t transId );
615 /* @[declare_iotble_sendresponse] */
616 
627 /* @[declare_iotble_getconnectioninfolist] */
628 BTStatus_t IotBle_GetConnectionInfoList( IotLink_t ** pConnectionInfoList );
629 /* @[declare_iotble_getconnectioninfolist] */
630 
640 /* @[declare_iotble_getconnectioninfo] */
641 BTStatus_t IotBle_GetConnectionInfo( uint16_t connId,
642  IotBleConnectionInfoListElement_t ** pConnectionInfo );
643 /* @[declare_iotble_getconnectioninfo] */
644 
652 /* @[declare_iotble_confirmnumericcomparisonkeys] */
654  bool keyAccepted );
655 /* @[declare_iotble_confirmnumericcomparisonkeys] */
656 
657 
670 /* @[declare_iotble_setdevicename] */
671 BTStatus_t IotBle_SetDeviceName( const char * pName,
672  size_t length );
673 /* @[declare_iotble_setdevicename] */
674 
675 #endif /* IOT_BLE_H*/
Definition: iot_ble.h:105
void(* IotBle_PairingStateChanged_t)(BTStatus_t status, BTBdaddr_t *pRemoteBdAddr, BTBondState_t bondstate, BTSecurityLevel_t securityLevel, BTAuthFailureReason_t reason)
Callback invoked when pairing state is changed.
Definition: iot_ble.h:294
IotBle_ConnParameterUpdateRequestCallback_t pConnParameterUpdateRequestCb
Definition: iot_ble.h:335
uint16_t handle
Definition: iot_ble.h:175
BTBondState_t
Definition: bt_hal_manager.h:308
BLE Attribute event.
Definition: iot_ble.h:193
void(* IotBle_NumericComparisonCallback_t)(BTBdaddr_t *pRemoteBdAddr, uint32_t passKey)
Callback invoked on pairing request from remote device.
Definition: iot_ble.h:308
size_t size
Definition: iot_ble.h:213
IotBleExecWriteEventParams_t * pParamExecWrite
Definition: iot_ble.h:199
Parameters for indication sent event.
Definition: iot_ble.h:182
BTStatus_t status
Definition: iot_ble.h:174
BTUuid_t * pUUID2
Definition: iot_ble.h:58
Definition: iot_ble.h:319
Definition: iot_ble.h:317
uint16_t offset
Definition: iot_ble.h:136
uint16_t connId
Definition: iot_ble.h:90
IotBleAttributeEventType_t
Attribute event type.
Definition: iot_ble.h:98
Parameters for write executed event.
Definition: iot_ble.h:160
BTBdaddr_t remoteBdAddr
Definition: iot_ble.h:87
uint16_t attrHandle
Definition: iot_ble.h:150
uint32_t maxInterval
Definition: iot_ble.h:54
char * pManufacturerData
Definition: iot_ble.h:55
Declares and implements doubly-linked lists and queues.
uint32_t appearance
Definition: iot_ble.h:52
char * pServiceData
Definition: iot_ble.h:56
uint32_t length
Definition: iot_ble.h:148
uint16_t connId
Definition: iot_ble.h:132
Definition: iot_ble.h:322
uint32_t transId
Definition: iot_ble.h:133
bool needRsp
Definition: iot_ble.h:152
void(* IotBle_MtuChangedCallback_t)(uint16_t connId, uint16_t mtu)
Callback invoked when the MTU for a given connection changes.
Definition: iot_ble.h:238
BTRspErrorStatus_t
Error status sent inside response to a read or write from the client.
Definition: bt_hal_gatt_server.h:53
uint16_t connId
Definition: iot_ble.h:149
BTStatus_t IotBle_RemoveBond(const BTBdaddr_t *pRemoteBdAddr)
Remove a bonded device. Will trigger pBondedCb.
BTStatus_t IotBle_DeleteService(BTService_t *pService)
Delete a local service.
Definition: iot_ble.h:101
IotBleRespConfirmEventParams_t * pParamRespConfirm
Definition: iot_ble.h:200
Definition: iot_ble.h:100
bool execWrite
Definition: iot_ble.h:165
void(* IotBleAttributeEventCallback_t)(IotBleAttributeEvent_t *pEventParam)
Callback called when a request on a attribute is made by the remote device.
Definition: iot_ble.h:122
void(* IotBle_ConnParameterUpdateRequestCallback_t)(BTStatus_t status, const BTBdaddr_t *pRemoteBdAddr, IotBleConnectionParam_t *pConnectionParam, uint32_t connInterval)
Callback invoked on BLE_ConnParameterUpdateRequest from remote device.
Definition: iot_ble.h:279
Definition: iot_ble.h:104
BTStatus_t IotBle_SetStopAdvCallback(IotBle_StopAdvCallback_t pStopAdvCb)
Sets an application defined callback invoked when an advertisement duration has ended or advertisemen...
BTStatus_t IotBle_Init(void)
Starting point. Initialize the BLE stack and its services. It is up to the application to decide when...
BTStatus_t IotBle_ConfirmNumericComparisonKeys(BTBdaddr_t *pBdAddr, bool keyAccepted)
Confirm key for numeric comparison.
Basic info contained in an attribute. This is common to all attributes.
Definition: iot_ble.h:211
uint32_t minInterval
Definition: iot_ble.h:72
BTBdaddr_t * pRemoteBdAddr
Definition: iot_ble.h:163
BTAuthFailureReason_t
Authentication failure reasons.
Definition: bt_hal_manager.h:134
bool isPrep
Definition: iot_ble.h:153
bool isBonded
Definition: iot_ble.h:91
BTStatus_t IotBle_SetDeviceName(const char *pName, size_t length)
Set device name for BLE.
BTStatus_t IotBle_GetConnectionInfo(uint16_t connId, IotBleConnectionInfoListElement_t **pConnectionInfo)
Get connection info for a specific connection ID.
uint16_t attrHandle
Definition: iot_ble.h:135
BTBdaddr_t * pRemoteBdAddr
Definition: iot_ble.h:146
bool includeTxPower
Definition: iot_ble.h:61
uint32_t latency
Definition: iot_ble.h:74
Contains the connection info. Return when requested by IotBleGetConnectionInfoList.
Definition: iot_ble.h:83
uint16_t offset
Definition: iot_ble.h:151
BTStatus_t eventStatus
Definition: iot_ble.h:227
IotBleWriteEventParams_t * pParamWrite
Definition: iot_ble.h:198
IotLink_t clientCharDescrListHead
Definition: iot_ble.h:89
void(* IotBle_StopAdvCallback_t)(BTStatus_t status)
Callback indicating the status of stop advertisement operation. Invoked on BLE_StopAdv.
Definition: iot_ble.h:268
BTStatus_t IotBle_On(void)
Turns on the BLE radio.
IotBleAttributeEventType_t xEventType
Definition: iot_ble.h:203
BTStatus_t IotBle_CreateService(BTService_t *pService, IotBleAttributeEventCallback_t pEventsCallbacks[])
Create a new service.
IotBle_NumericComparisonCallback_t pNumericComparisonCb
Definition: iot_ble.h:336
BTStatus_t IotBle_SendIndication(IotBleEventResponse_t *pResp, uint16_t connId, bool confirm)
Send value indication to a remote device.
void * pvPtr
Definition: iot_ble.h:337
Structure containing all advertisement parameters.
Definition: iot_ble.h:50
BTSecurityLevel_t securityLevel
Definition: iot_ble.h:88
size_t attrDataOffset
Definition: iot_ble.h:226
BLE Event response.
Definition: iot_ble.h:223
BTStatus_t IotBle_ConnParameterUpdateRequest(const BTBdaddr_t *pRemoteBdAddr, IotBleConnectionParam_t *pConnectionParam)
Request an update of the connection parameters.
uint32_t timeout
Definition: iot_ble.h:75
uint32_t minInterval
Definition: iot_ble.h:53
uint16_t serviceDataLen
Definition: iot_ble.h:60
void(* IotBle_StartAdvCallback_t)(BTStatus_t status)
Callback indicating the status of start advertisement operation. Invoked on BLE_StartAdv.
Definition: iot_ble.h:260
UUID.
Definition: bt_hal_manager_types.h:83
BTStatus_t IotBle_UnRegisterEventCb(IotBleEvents_t event, IotBleEventsCallbacks_t bleEventsCallbacks)
Remove a subscription to an event. Note: API should not be invoked from within the event callback...
T HAL provides the standard defintions used by BT GATT interfaces.
BTBdaddr_t * pRemoteBdAddr
Definition: iot_ble.h:134
Advertising Name Format.
Definition: bt_hal_manager_adapter_ble.h:100
IotLink_t connectionList
Definition: iot_ble.h:85
uint16_t connId
Definition: iot_ble.h:185
uint8_t * pValue
Definition: iot_ble.h:147
IotBleIndicationSentEventParams_t * pParamIndicationSent
Definition: iot_ble.h:201
BTRspErrorStatus_t rspErrorStatus
Definition: iot_ble.h:228
uint8_t * pData
Definition: iot_ble.h:214
size_t handle
Definition: iot_ble.h:216
IotBleEvents_t
enum listing all the BLE events (not directly triggered by a function call)
Definition: iot_ble.h:315
BTUuid_t uuid
Definition: iot_ble.h:215
Definition: iot_ble.h:318
IotBleAttributeData_t * pAttrData
Definition: iot_ble.h:225
uint32_t transId
Definition: iot_ble.h:162
BTGattAdvName_t name
Definition: iot_ble.h:62
Parameters for read event.
Definition: iot_ble.h:130
BTStatus_t
Bluetooth Error Status .
Definition: bt_hal_manager_types.h:97
uint32_t transId
Definition: iot_ble.h:145
BTAttribute_t * pAttribute
Definition: iot_ble.h:184
IotBle_MtuChangedCallback_t pMtuChangedCb
Definition: iot_ble.h:332
Connection parameters.
Definition: iot_ble.h:70
Parameters for write event.
Definition: iot_ble.h:143
BTStatus_t status
Definition: iot_ble.h:186
BTStatus_t IotBle_Off(void)
Turns off the BLE radio.
uint32_t maxInterval
Definition: iot_ble.h:73
BTStatus_t IotBle_StartAdv(IotBle_StartAdvCallback_t pStartAdvCb)
Start advertisements to listen for incoming connections. Triggers IotBle_StartAdvCallback_t.
IotBle_PairingStateChanged_t pGAPPairingStateChangedCb
Definition: iot_ble.h:334
IotBleReadEventParams_t * pParamRead
Definition: iot_ble.h:197
BLE events not directly triggered by a function call. Most of them are triggered by a remote device m...
Definition: iot_ble.h:330
Definition: iot_ble.h:102
BT/BLE address.
Definition: bt_hal_manager_types.h:66
Definition: iot_ble.h:103
uint16_t connId
Definition: iot_ble.h:164
void(* IotBle_ConnectionCallback_t)(BTStatus_t status, uint16_t connId, bool connected, BTBdaddr_t *pRemoteBdAddr)
Callback indicating that a remote device has connected or been disconnected.
Definition: iot_ble.h:249
bool setScanRsp
Definition: iot_ble.h:63
uint16_t manufacturerLen
Definition: iot_ble.h:59
BTStatus_t IotBle_SendResponse(IotBleEventResponse_t *pResp, uint16_t connId, uint32_t transId)
Send response to the remote device.
Definition: iot_ble.h:321
Parameters for confirmation response event.
Definition: iot_ble.h:172
IotBleConnectionParam_t connectionParams
Definition: iot_ble.h:86
BT GATT Server provides the interfaces to use Bluetooth GATT server feature.
Generic BLE attribute.
Definition: bt_hal_gatt_types.h:233
IotBle_ConnectionCallback_t pConnectionCb
Definition: iot_ble.h:333
BTUuid_t * pUUID1
Definition: iot_ble.h:57
BTStatus_t IotBle_RegisterEventCb(IotBleEvents_t event, IotBleEventsCallbacks_t bleEventsCallbacks)
Used to route event to whatever service requests it.
BTStatus_t IotBle_GetConnectionInfoList(IotLink_t **pConnectionInfoList)
Send response to the remote device.
BTStatus_t IotBle_StopAdv(IotBle_StopAdvCallback_t pStopAdvCb)
Stop advertisements to listen for incoming connections. Triggers IotBle_StopAdvCallback_t.