gapbondmgr.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @file
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /**
  11. @headerfile: gapbondmgr.h
  12. $Date:
  13. $Revision:
  14. @mainpage BLE GAP Bond Manager
  15. This GAP profile manages bonded connections between devices.<BR><BR>
  16. */
  17. #ifndef GAPBONDMGR_H
  18. #define GAPBONDMGR_H
  19. #ifdef __cplusplus
  20. extern "C"
  21. {
  22. #endif
  23. /*-------------------------------------------------------------------
  24. * INCLUDES
  25. */
  26. #include "gap.h"
  27. /*-------------------------------------------------------------------
  28. * CONSTANTS
  29. */
  30. #if !defined ( GAP_BONDINGS_MAX )
  31. #define GAP_BONDINGS_MAX 4 //!< Maximum number of bonds that can be saved in NV.
  32. #endif
  33. #if !defined ( GAP_BOND_MGR_INDEX_REPLACE )
  34. #define GAP_BOND_MGR_INDEX_REPLACE TRUE //!< Maximum number of bonds that can be saved in NV.
  35. #endif
  36. #if !defined ( GAP_CHAR_CFG_MAX )
  37. #define GAP_CHAR_CFG_MAX 4 //!< Maximum number of characteristic configuration that can be saved in NV.
  38. #endif
  39. /** @defgroup GAPBOND_CONSTANTS_NAME GAP Bond Manager Constants
  40. * @{
  41. */
  42. /** @} End GAPBOND_CONSTANTS_NAME */
  43. /** @defgroup GAPBOND_PROFILE_PARAMETERS GAP Bond Manager Parameters
  44. * @{
  45. */
  46. #define GAPBOND_PAIRING_MODE 0x400 //!< Pairing Mode: @ref GAPBOND_PAIRING_MODE_DEFINES. Read/Write. Size is uint8. Default is GAPBOND_PAIRING_MODE_WAIT_FOR_REQ.
  47. #define GAPBOND_INITIATE_WAIT 0x401 //!< Pairing Mode Initiate wait timeout. This is the time it will wait for a Pairing Request before sending the Slave Initiate Request. Read/Write. Size is uint16. Default is 1000(in milliseconds).
  48. #define GAPBOND_MITM_PROTECTION 0x402 //!< Man-In-The-Middle (MITM) basically turns on Passkey protection in the pairing algorithm. Read/Write. Size is uint8. Default is 0(disabled).
  49. #define GAPBOND_IO_CAPABILITIES 0x403 //!< I/O capabilities. Read/Write. Size is uint8. Default is GAPBOND_IO_CAP_DISPLAY_ONLY @ref GAPBOND_IO_CAP_DEFINES.
  50. #define GAPBOND_OOB_ENABLED 0x404 //!< OOB data available for pairing algorithm. Read/Write. Size is uint8. Default is 0(disabled).
  51. #define GAPBOND_OOB_DATA 0x405 //!< OOB Data. Read/Write. size uint8[16]. Default is all 0's.
  52. #define GAPBOND_BONDING_ENABLED 0x406 //!< Request Bonding during the pairing process if enabled. Read/Write. Size is uint8. Default is 0(disabled).
  53. #define GAPBOND_KEY_DIST_LIST 0x407 //!< The key distribution list for bonding. size is uint8. @ref GAPBOND_KEY_DIST_DEFINES. Default is sEncKey, sIdKey, mIdKey, mSign enabled.
  54. #define GAPBOND_DEFAULT_PASSCODE 0x408 //!< The default passcode for MITM protection. size is uint32. Range is 0 - 999,999. Default is 0.
  55. #define GAPBOND_ERASE_ALLBONDS 0x409 //!< Erase all of the bonded devices. Write Only. No Size.
  56. #define GAPBOND_AUTO_FAIL_PAIRING 0x40A //!< TEST MODE (DO NOT USE) to automatically send a Pairing Fail when a Pairing Request is received. Read/Write. size is uint8. Default is 0 (disabled).
  57. #define GAPBOND_AUTO_FAIL_REASON 0x40B //!< TEST MODE (DO NOT USE) Pairing Fail reason when auto failing. Read/Write. size is uint8. Default is 0x05 (SMP_PAIRING_FAILED_NOT_SUPPORTED).
  58. #define GAPBOND_KEYSIZE 0x40C //!< Key Size used in pairing. Read/Write. size is uint8. Default is 16.
  59. #define GAPBOND_AUTO_SYNC_WL 0x40D //!< Clears the White List adds to it each unique address stored by bonds in NV. Read/Write. Size is uint8. Default is FALSE.
  60. #define GAPBOND_BOND_COUNT 0x40E //!< Gets the total number of bonds stored in NV. Read Only. Size is uint8. Default is 0 (no bonds).
  61. #define GAPBOND_BOND_FAIL_ACTION 0x40F //!< Possible actions Central may take upon an unsuccessful bonding. Write Only. Size is uint8. Default is 0x02 (Terminate link upon unsuccessful bonding).
  62. #define GAPBOND_ERASE_SINGLEBOND 0x410 //!< Erase a single bonded device. Write only. Must provide address type followed by device address.
  63. /** @} End GAPBOND_PROFILE_PARAMETERS */
  64. /** @defgroup GAPBOND_PAIRING_MODE_DEFINES GAP Bond Manager Pairing Modes
  65. * @{
  66. */
  67. #define GAPBOND_PAIRING_MODE_NO_PAIRING 0x00 //!< Pairing is not allowed
  68. #define GAPBOND_PAIRING_MODE_WAIT_FOR_REQ 0x01 //!< Wait for a pairing request or slave security request
  69. #define GAPBOND_PAIRING_MODE_INITIATE 0x02 //!< Don't wait, initiate a pairing request or slave security request
  70. /** @} End GAPBOND_PAIRING_MODE_DEFINES */
  71. /** @defgroup GAPBOND_IO_CAP_DEFINES GAP Bond Manager I/O Capabilities
  72. * @{
  73. */
  74. #define GAPBOND_IO_CAP_DISPLAY_ONLY 0x00 //!< Display Only Device
  75. #define GAPBOND_IO_CAP_DISPLAY_YES_NO 0x01 //!< Display and Yes and No Capable
  76. #define GAPBOND_IO_CAP_KEYBOARD_ONLY 0x02 //!< Keyboard Only
  77. #define GAPBOND_IO_CAP_NO_INPUT_NO_OUTPUT 0x03 //!< No Display or Input Device
  78. #define GAPBOND_IO_CAP_KEYBOARD_DISPLAY 0x04 //!< Both Keyboard and Display Capable
  79. /** @} End GAPBOND_IO_CAP_DEFINES */
  80. /** @defgroup GAPBOND_KEY_DIST_DEFINES GAP Bond Manager Key Distribution
  81. * @{
  82. */
  83. #define GAPBOND_KEYDIST_SENCKEY 0x01 //!< Slave Encryption Key
  84. #define GAPBOND_KEYDIST_SIDKEY 0x02 //!< Slave IRK and ID information
  85. #define GAPBOND_KEYDIST_SSIGN 0x04 //!< Slave CSRK
  86. #define GAPBOND_KEYDIST_MENCKEY 0x08 //!< Master Encrypton Key
  87. #define GAPBOND_KEYDIST_MIDKEY 0x10 //!< Master IRK and ID information
  88. #define GAPBOND_KEYDIST_MSIGN 0x20 //!< Master CSRK
  89. /** @} End GAPBOND_IO_CAP_DEFINES */
  90. /** @defgroup GAPBOND_PAIRING_STATE_DEFINES GAP Bond Manager Pairing States
  91. * @{
  92. */
  93. #define GAPBOND_PAIRING_STATE_STARTED 0x00 //!< Pairing started
  94. #define GAPBOND_PAIRING_STATE_COMPLETE 0x01 //!< Pairing complete
  95. #define GAPBOND_PAIRING_STATE_BONDED 0x02 //!< Devices bonded
  96. /** @} End GAPBOND_PAIRING_STATE_DEFINES */
  97. /** @defgroup SMP_PAIRING_FAILED_DEFINES Pairing failure status values
  98. * @{
  99. */
  100. #define SMP_PAIRING_FAILED_PASSKEY_ENTRY_FAILED 0x01 //!< The user input of the passkey failed, for example, the user cancelled the operation.
  101. #define SMP_PAIRING_FAILED_OOB_NOT_AVAIL 0x02 //!< The OOB data is not available
  102. #define SMP_PAIRING_FAILED_AUTH_REQ 0x03 //!< The pairing procedure can't be performed as authentication requirements can't be met due to IO capabilities of one or both devices
  103. #define SMP_PAIRING_FAILED_CONFIRM_VALUE 0x04 //!< The confirm value doesn't match the calculated compare value
  104. #define SMP_PAIRING_FAILED_NOT_SUPPORTED 0x05 //!< Pairing isn't supported by the device
  105. #define SMP_PAIRING_FAILED_ENC_KEY_SIZE 0x06 //!< The resultant encryption key size is insufficient for the security requirements of this device.
  106. #define SMP_PAIRING_FAILED_CMD_NOT_SUPPORTED 0x07 //!< The SMP command received is not supported on this device.
  107. #define SMP_PAIRING_FAILED_UNSPECIFIED 0x08 //!< Pairing failed due to an unspecified reason
  108. #define SMP_PAIRING_FAILED_REPEATED_ATTEMPTS 0x09 //!< Pairing or authenication procedure is disallowed because too little time has elapsed since the last pairing request or security request.
  109. /** @} End SMP_PAIRING_FAILED_DEFINES */
  110. /** @defgroup GAPBOND_BONDING_FAILURE_DEFINES Bonding Failure Actions
  111. * @{
  112. */
  113. #define GAPBOND_FAIL_NO_ACTION 0x00 //!< Take no action upon unsuccessful bonding
  114. #define GAPBOND_FAIL_INITIATE_PAIRING 0x01 //!< Initiate pairing upon unsuccessful bonding
  115. #define GAPBOND_FAIL_TERMINATE_LINK 0x02 //!< Terminate link upon unsuccessful bonding
  116. #define GAPBOND_FAIL_TERMINATE_ERASE_BONDS 0x03 //!< Terminate link and erase all existing bonds on device upon unsuccessful bonding
  117. /** @} End GAPBOND_BONDING_FAILURE_DEFINES */
  118. /*-------------------------------------------------------------------
  119. * TYPEDEFS
  120. */
  121. /**
  122. * Passcode Callback Function
  123. */
  124. typedef void (*pfnPasscodeCB_t)
  125. (
  126. uint8 *deviceAddr, //!< address of device to pair with, and could be either public or random.
  127. uint16 connectionHandle, //!< Connection handle
  128. uint8 uiInputs, //!< Pairing User Interface Inputs - Ask user to input passcode
  129. uint8 uiOutputs //!< Pairing User Interface Outputs - Display passcode
  130. );
  131. /**
  132. * Pairing State Callback Function
  133. */
  134. typedef void (*pfnPairStateCB_t)
  135. (
  136. uint16 connectionHandle, //!< Connection handle
  137. uint8 state, //!< Pairing state @ref GAPBOND_PAIRING_STATE_DEFINES
  138. uint8 status //!< Pairing status
  139. );
  140. /**
  141. * Callback Registration Structure
  142. */
  143. typedef struct
  144. {
  145. pfnPasscodeCB_t passcodeCB; //!< Passcode callback
  146. pfnPairStateCB_t pairStateCB; //!< Pairing state callback
  147. } gapBondCBs_t;
  148. /*-------------------------------------------------------------------
  149. * MACROS
  150. */
  151. /*-------------------------------------------------------------------
  152. * API FUNCTIONS
  153. */
  154. /**
  155. * @defgroup GAPROLES_BONDMGR_API GAP Bond Manager API Functions
  156. *
  157. * @{
  158. */
  159. /**
  160. * @brief Set a GAP Bond Manager parameter.
  161. *
  162. * NOTE: You can call this function with a GAP Parameter ID and it will set the
  163. * GAP Parameter. GAP Parameters are defined in (gap.h). Also,
  164. * the "len" field must be set to the size of a "uint16" and the
  165. * "pValue" field must point to a "uint16".
  166. *
  167. * @param param - Profile parameter ID: @ref GAPBOND_PROFILE_PARAMETERS
  168. * @param len - length of data to write
  169. * @param pValue - pointer to data to write. This is dependent on
  170. * the parameter ID and WILL be cast to the appropriate
  171. * data type (example: data type of uint16 will be cast to
  172. * uint16 pointer).
  173. *
  174. * @return SUCCESS or INVALIDPARAMETER (invalid paramID)
  175. */
  176. extern bStatus_t GAPBondMgr_SetParameter( uint16 param, uint8 len, void *pValue );
  177. /**
  178. * @brief Get a GAP Bond Manager parameter.
  179. *
  180. * NOTE: You can call this function with a GAP Parameter ID and it will get a
  181. * GAP Parameter. GAP Parameters are defined in (gap.h). Also, the
  182. * "pValue" field must point to a "uint16".
  183. *
  184. * @param param - Profile parameter ID: @ref GAPBOND_PROFILE_PARAMETERS
  185. * @param pValue - pointer to location to get the value. This is dependent on
  186. * the parameter ID and WILL be cast to the appropriate
  187. * data type (example: data type of uint16 will be cast to
  188. * uint16 pointer).
  189. *
  190. * @return SUCCESS or INVALIDPARAMETER (invalid paramID)
  191. */
  192. extern bStatus_t GAPBondMgr_GetParameter( uint16 param, void *pValue );
  193. /**
  194. * @brief Notify the Bond Manager that a connection has been made.
  195. *
  196. * NOTE: The GAP Peripheral/Central Role profile will
  197. * call this function, if they are included in the project.
  198. *
  199. * @param addrType - device's address type. Reference GAP_ADDR_TYPE_DEFINES in gap.h
  200. * @param pDevAddr - device's address
  201. * @param connHandle - connection handle
  202. * @param role - master or slave role. Reference GAP_PROFILE_ROLE_DEFINES in gap.h
  203. *
  204. * @return SUCCESS, otherwise failure
  205. */
  206. extern bStatus_t GAPBondMgr_LinkEst( uint8 addrType, uint8 *pDevAddr, uint16 connHandle, uint8 role );
  207. /**
  208. * @brief Resolve an address from bonding information.
  209. *
  210. * @param addrType - device's address type. Reference GAP_ADDR_TYPE_DEFINES in gap.h
  211. * @param pDevAddr - device's address
  212. * @param pResolvedAddr - pointer to buffer to put the resolved address
  213. *
  214. * @return bonding index (0 - (GAP_BONDINGS_MAX-1) if found,
  215. * GAP_BONDINGS_MAX if not found
  216. */
  217. extern uint8 GAPBondMgr_ResolveAddr( uint8 addrType, uint8 *pDevAddr, uint8 *pResolvedAddr );
  218. /**
  219. * @brief Set/clear the service change indication in a bond record.
  220. *
  221. * @param connectionHandle - connection handle of the connected device or 0xFFFF
  222. * if all devices in database.
  223. * @param setParam - TRUE to set the service change indication, FALSE to clear it.
  224. *
  225. * @return SUCCESS - bond record found and changed,<BR>
  226. * bleNoResources - bond record not found (for 0xFFFF connectionHandle),<BR>
  227. * bleNotConnected - connection not found - connectionHandle is invalid (for non-0xFFFF connectionHandle).
  228. */
  229. extern bStatus_t GAPBondMgr_ServiceChangeInd( uint16 connectionHandle, uint8 setParam );
  230. /**
  231. * @brief Update the Characteristic Configuration in a bond record.
  232. *
  233. * @param connectionHandle - connection handle of the connected device or 0xFFFF
  234. * if all devices in database.
  235. * @param attrHandle - attribute handle.
  236. * @param value - characteristic configuration value.
  237. *
  238. * @return SUCCESS - bond record found and changed,<BR>
  239. * bleNoResources - bond record not found (for 0xFFFF connectionHandle),<BR>
  240. * bleNotConnected - connection not found - connectionHandle is invalid (for non-0xFFFF connectionHandle).
  241. */
  242. extern bStatus_t GAPBondMgr_UpdateCharCfg( uint16 connectionHandle, uint16 attrHandle, uint16 value );
  243. /**
  244. * @brief Register callback functions with the bond manager.
  245. *
  246. * NOTE: There is no need to register a passcode callback function
  247. * if the passcode will be handled with the GAPBOND_DEFAULT_PASSCODE parameter.
  248. *
  249. * @param pCB - pointer to callback function structure.
  250. *
  251. * @return none
  252. */
  253. extern void GAPBondMgr_Register( gapBondCBs_t *pCB );
  254. /**
  255. * @brief Respond to a passcode request.
  256. *
  257. * @param connectionHandle - connection handle of the connected device or 0xFFFF
  258. * if all devices in database.
  259. * @param status - SUCCESS if passcode is available, otherwise see @ref SMP_PAIRING_FAILED_DEFINES.
  260. * @param passcode - integer value containing the passcode.
  261. *
  262. * @return SUCCESS - bond record found and changed,<BR>
  263. * bleIncorrectMode - Link not found.
  264. */
  265. extern bStatus_t GAPBondMgr_PasscodeRsp( uint16 connectionHandle, uint8 status, uint32 passcode );
  266. /**
  267. * @brief This is a bypass mechanism to allow the bond manager to process
  268. * GAP messages.
  269. *
  270. * NOTE: This is an advanced feature and shouldn't be called unless
  271. * the normal GAP Bond Manager task ID registration is overridden.
  272. *
  273. * @param pMsg - GAP event message
  274. *
  275. * @return TRUE if safe to deallocate incoming GAP message, FALSE otherwise.
  276. */
  277. extern uint8 GAPBondMgr_ProcessGAPMsg( gapEventHdr_t *pMsg );
  278. /**
  279. * @brief This function will check the length of a Bond Manager NV Item.
  280. *
  281. * @param id - NV ID.
  282. * @param len - lengths in bytes of item.
  283. *
  284. * @return SUCCESS or FAILURE
  285. */
  286. extern uint8 GAPBondMgr_CheckNVLen( uint8 id, uint8 len );
  287. /**
  288. * @} End GAPROLES_BONDMGR_API
  289. */
  290. /*-------------------------------------------------------------------
  291. * TASK FUNCTIONS - Don't call these. These are system functions.
  292. */
  293. /**
  294. * @internal
  295. *
  296. * @brief Initialization function for the GAP Bond Manager Task.
  297. * This is called during initialization and should contain
  298. * any application specific initialization (ie. hardware
  299. * initialization/setup, table initialization, power up
  300. * notificaiton ... ).
  301. *
  302. * @param the ID assigned by OSAL. This ID should be
  303. * used to send messages and set timers.
  304. *
  305. * @return void
  306. */
  307. extern void GAPBondMgr_Init( uint8 task_id );
  308. /**
  309. * @internal
  310. *
  311. * @brief GAP Bond Manager Task event processor.
  312. * This function is called to process all events for the task.
  313. * Events include timers, messages and any other user defined
  314. * events.
  315. *
  316. * @param task_id - The OSAL assigned task ID.
  317. * @param events - events to process. This is a bit map and can
  318. * contain more than one event.
  319. *
  320. * @return events not processed
  321. */
  322. extern uint16 GAPBondMgr_ProcessEvent( uint8 task_id, uint16 events );
  323. /*-------------------------------------------------------------------
  324. -------------------------------------------------------------------*/
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. #endif /* GAPBONDMGR_H */