gapbondmgr.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /**
  29. @headerfile: gapbondmgr.h
  30. $Date:
  31. $Revision:
  32. @mainpage BLE GAP Bond Manager
  33. This GAP profile manages bonded connections between devices.<BR><BR>
  34. */
  35. #ifndef GAPBONDMGR_H
  36. #define GAPBONDMGR_H
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. /*-------------------------------------------------------------------
  42. * INCLUDES
  43. */
  44. #include "gap.h"
  45. /*-------------------------------------------------------------------
  46. * CONSTANTS
  47. */
  48. #if !defined ( GAP_BONDINGS_MAX )
  49. #define GAP_BONDINGS_MAX 10 //!< Maximum number of bonds that can be saved in NV.
  50. #endif
  51. #if !defined ( GAP_CHAR_CFG_MAX )
  52. #define GAP_CHAR_CFG_MAX 4 //!< Maximum number of characteristic configuration that can be saved in NV.
  53. #endif
  54. /** @defgroup GAPBOND_CONSTANTS_NAME GAP Bond Manager Constants
  55. * @{
  56. */
  57. /** @} End GAPBOND_CONSTANTS_NAME */
  58. /** @defgroup GAPBOND_PROFILE_PARAMETERS GAP Bond Manager Parameters
  59. * @{
  60. */
  61. #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.
  62. #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).
  63. #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).
  64. #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.
  65. #define GAPBOND_OOB_ENABLED 0x404 //!< OOB data available for pairing algorithm. Read/Write. Size is uint8. Default is 0(disabled).
  66. #define GAPBOND_OOB_DATA 0x405 //!< OOB Data. Read/Write. size uint8[16]. Default is all 0's.
  67. #define GAPBOND_BONDING_ENABLED 0x406 //!< Request Bonding during the pairing process if enabled. Read/Write. Size is uint8. Default is 0(disabled).
  68. #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.
  69. #define GAPBOND_DEFAULT_PASSCODE 0x408 //!< The default passcode for MITM protection. size is uint32. Range is 0 - 999,999. Default is 0.
  70. #define GAPBOND_ERASE_ALLBONDS 0x409 //!< Erase all of the bonded devices. Write Only. No Size.
  71. #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).
  72. #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).
  73. #define GAPBOND_KEYSIZE 0x40C //!< Key Size used in pairing. Read/Write. size is uint8. Default is 16.
  74. #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.
  75. #define GAPBOND_BOND_COUNT 0x40E //!< Gets the total number of bonds stored in NV. Read Only. Size is uint8. Default is 0 (no bonds).
  76. #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).
  77. #define GAPBOND_ERASE_SINGLEBOND 0x410 //!< Erase a single bonded device. Write only. Must provide address type followed by device address.
  78. /** @} End GAPBOND_PROFILE_PARAMETERS */
  79. /** @defgroup GAPBOND_PAIRING_MODE_DEFINES GAP Bond Manager Pairing Modes
  80. * @{
  81. */
  82. #define GAPBOND_PAIRING_MODE_NO_PAIRING 0x00 //!< Pairing is not allowed
  83. #define GAPBOND_PAIRING_MODE_WAIT_FOR_REQ 0x01 //!< Wait for a pairing request or slave security request
  84. #define GAPBOND_PAIRING_MODE_INITIATE 0x02 //!< Don't wait, initiate a pairing request or slave security request
  85. /** @} End GAPBOND_PAIRING_MODE_DEFINES */
  86. /** @defgroup GAPBOND_IO_CAP_DEFINES GAP Bond Manager I/O Capabilities
  87. * @{
  88. */
  89. #define GAPBOND_IO_CAP_DISPLAY_ONLY 0x00 //!< Display Only Device
  90. #define GAPBOND_IO_CAP_DISPLAY_YES_NO 0x01 //!< Display and Yes and No Capable
  91. #define GAPBOND_IO_CAP_KEYBOARD_ONLY 0x02 //!< Keyboard Only
  92. #define GAPBOND_IO_CAP_NO_INPUT_NO_OUTPUT 0x03 //!< No Display or Input Device
  93. #define GAPBOND_IO_CAP_KEYBOARD_DISPLAY 0x04 //!< Both Keyboard and Display Capable
  94. /** @} End GAPBOND_IO_CAP_DEFINES */
  95. /** @defgroup GAPBOND_KEY_DIST_DEFINES GAP Bond Manager Key Distribution
  96. * @{
  97. */
  98. #define GAPBOND_KEYDIST_SENCKEY 0x01 //!< Slave Encryption Key
  99. #define GAPBOND_KEYDIST_SIDKEY 0x02 //!< Slave IRK and ID information
  100. #define GAPBOND_KEYDIST_SSIGN 0x04 //!< Slave CSRK
  101. #define GAPBOND_KEYDIST_MENCKEY 0x08 //!< Master Encrypton Key
  102. #define GAPBOND_KEYDIST_MIDKEY 0x10 //!< Master IRK and ID information
  103. #define GAPBOND_KEYDIST_MSIGN 0x20 //!< Master CSRK
  104. /** @} End GAPBOND_IO_CAP_DEFINES */
  105. /** @defgroup GAPBOND_PAIRING_STATE_DEFINES GAP Bond Manager Pairing States
  106. * @{
  107. */
  108. #define GAPBOND_PAIRING_STATE_STARTED 0x00 //!< Pairing started
  109. #define GAPBOND_PAIRING_STATE_COMPLETE 0x01 //!< Pairing complete
  110. #define GAPBOND_PAIRING_STATE_BONDED 0x02 //!< Devices bonded
  111. /** @} End GAPBOND_PAIRING_STATE_DEFINES */
  112. /** @defgroup SMP_PAIRING_FAILED_DEFINES Pairing failure status values
  113. * @{
  114. */
  115. #define SMP_PAIRING_FAILED_PASSKEY_ENTRY_FAILED 0x01 //!< The user input of the passkey failed, for example, the user cancelled the operation.
  116. #define SMP_PAIRING_FAILED_OOB_NOT_AVAIL 0x02 //!< The OOB data is not available
  117. #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
  118. #define SMP_PAIRING_FAILED_CONFIRM_VALUE 0x04 //!< The confirm value doesn't match the calculated compare value
  119. #define SMP_PAIRING_FAILED_NOT_SUPPORTED 0x05 //!< Pairing isn't supported by the device
  120. #define SMP_PAIRING_FAILED_ENC_KEY_SIZE 0x06 //!< The resultant encryption key size is insufficient for the security requirements of this device.
  121. #define SMP_PAIRING_FAILED_CMD_NOT_SUPPORTED 0x07 //!< The SMP command received is not supported on this device.
  122. #define SMP_PAIRING_FAILED_UNSPECIFIED 0x08 //!< Pairing failed due to an unspecified reason
  123. #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.
  124. /** @} End SMP_PAIRING_FAILED_DEFINES */
  125. /** @defgroup GAPBOND_BONDING_FAILURE_DEFINES Bonding Failure Actions
  126. * @{
  127. */
  128. #define GAPBOND_FAIL_NO_ACTION 0x00 //!< Take no action upon unsuccessful bonding
  129. #define GAPBOND_FAIL_INITIATE_PAIRING 0x01 //!< Initiate pairing upon unsuccessful bonding
  130. #define GAPBOND_FAIL_TERMINATE_LINK 0x02 //!< Terminate link upon unsuccessful bonding
  131. #define GAPBOND_FAIL_TERMINATE_ERASE_BONDS 0x03 //!< Terminate link and erase all existing bonds on device upon unsuccessful bonding
  132. /** @} End GAPBOND_BONDING_FAILURE_DEFINES */
  133. /*-------------------------------------------------------------------
  134. * TYPEDEFS
  135. */
  136. /**
  137. * Passcode Callback Function
  138. */
  139. typedef void (*pfnPasscodeCB_t)
  140. (
  141. uint8 *deviceAddr, //!< address of device to pair with, and could be either public or random.
  142. uint16 connectionHandle, //!< Connection handle
  143. uint8 uiInputs, //!< Pairing User Interface Inputs - Ask user to input passcode
  144. uint8 uiOutputs //!< Pairing User Interface Outputs - Display passcode
  145. );
  146. /**
  147. * Pairing State Callback Function
  148. */
  149. typedef void (*pfnPairStateCB_t)
  150. (
  151. uint16 connectionHandle, //!< Connection handle
  152. uint8 state, //!< Pairing state @ref GAPBOND_PAIRING_STATE_DEFINES
  153. uint8 status //!< Pairing status
  154. );
  155. /**
  156. * Callback Registration Structure
  157. */
  158. typedef struct
  159. {
  160. pfnPasscodeCB_t passcodeCB; //!< Passcode callback
  161. pfnPairStateCB_t pairStateCB; //!< Pairing state callback
  162. } gapBondCBs_t;
  163. /*-------------------------------------------------------------------
  164. * MACROS
  165. */
  166. /*-------------------------------------------------------------------
  167. * API FUNCTIONS
  168. */
  169. /**
  170. * @defgroup GAPROLES_BONDMGR_API GAP Bond Manager API Functions
  171. *
  172. * @{
  173. */
  174. /**
  175. * @brief Set a GAP Bond Manager parameter.
  176. *
  177. * NOTE: You can call this function with a GAP Parameter ID and it will set the
  178. * GAP Parameter. GAP Parameters are defined in (gap.h). Also,
  179. * the "len" field must be set to the size of a "uint16" and the
  180. * "pValue" field must point to a "uint16".
  181. *
  182. * @param param - Profile parameter ID: @ref GAPBOND_PROFILE_PARAMETERS
  183. * @param len - length of data to write
  184. * @param pValue - pointer to data to write. This is dependent on
  185. * the parameter ID and WILL be cast to the appropriate
  186. * data type (example: data type of uint16 will be cast to
  187. * uint16 pointer).
  188. *
  189. * @return SUCCESS or INVALIDPARAMETER (invalid paramID)
  190. */
  191. extern bStatus_t GAPBondMgr_SetParameter( uint16 param, uint8 len, void *pValue );
  192. /**
  193. * @brief Get a GAP Bond Manager parameter.
  194. *
  195. * NOTE: You can call this function with a GAP Parameter ID and it will get a
  196. * GAP Parameter. GAP Parameters are defined in (gap.h). Also, the
  197. * "pValue" field must point to a "uint16".
  198. *
  199. * @param param - Profile parameter ID: @ref GAPBOND_PROFILE_PARAMETERS
  200. * @param pValue - pointer to location to get the value. This is dependent on
  201. * the parameter ID and WILL be cast to the appropriate
  202. * data type (example: data type of uint16 will be cast to
  203. * uint16 pointer).
  204. *
  205. * @return SUCCESS or INVALIDPARAMETER (invalid paramID)
  206. */
  207. extern bStatus_t GAPBondMgr_GetParameter( uint16 param, void *pValue );
  208. /**
  209. * @brief Notify the Bond Manager that a connection has been made.
  210. *
  211. * NOTE: The GAP Peripheral/Central Role profile will
  212. * call this function, if they are included in the project.
  213. *
  214. * @param addrType - device's address type. Reference GAP_ADDR_TYPE_DEFINES in gap.h
  215. * @param pDevAddr - device's address
  216. * @param connHandle - connection handle
  217. * @param role - master or slave role. Reference GAP_PROFILE_ROLE_DEFINES in gap.h
  218. *
  219. * @return SUCCESS, otherwise failure
  220. */
  221. extern bStatus_t GAPBondMgr_LinkEst( uint8 addrType, uint8 *pDevAddr, uint16 connHandle, uint8 role );
  222. /**
  223. * @brief Resolve an address from bonding information.
  224. *
  225. * @param addrType - device's address type. Reference GAP_ADDR_TYPE_DEFINES in gap.h
  226. * @param pDevAddr - device's address
  227. * @param pResolvedAddr - pointer to buffer to put the resolved address
  228. *
  229. * @return bonding index (0 - (GAP_BONDINGS_MAX-1) if found,
  230. * GAP_BONDINGS_MAX if not found
  231. */
  232. extern uint8 GAPBondMgr_ResolveAddr( uint8 addrType, uint8 *pDevAddr, uint8 *pResolvedAddr );
  233. /**
  234. * @brief Set/clear the service change indication in a bond record.
  235. *
  236. * @param connectionHandle - connection handle of the connected device or 0xFFFF
  237. * if all devices in database.
  238. * @param setParam - TRUE to set the service change indication, FALSE to clear it.
  239. *
  240. * @return SUCCESS - bond record found and changed,<BR>
  241. * bleNoResources - bond record not found (for 0xFFFF connectionHandle),<BR>
  242. * bleNotConnected - connection not found - connectionHandle is invalid (for non-0xFFFF connectionHandle).
  243. */
  244. extern bStatus_t GAPBondMgr_ServiceChangeInd( uint16 connectionHandle, uint8 setParam );
  245. /**
  246. * @brief Update the Characteristic Configuration in a bond record.
  247. *
  248. * @param connectionHandle - connection handle of the connected device or 0xFFFF
  249. * if all devices in database.
  250. * @param attrHandle - attribute handle.
  251. * @param value - characteristic configuration value.
  252. *
  253. * @return SUCCESS - bond record found and changed,<BR>
  254. * bleNoResources - bond record not found (for 0xFFFF connectionHandle),<BR>
  255. * bleNotConnected - connection not found - connectionHandle is invalid (for non-0xFFFF connectionHandle).
  256. */
  257. extern bStatus_t GAPBondMgr_UpdateCharCfg( uint16 connectionHandle, uint16 attrHandle, uint16 value );
  258. /**
  259. * @brief Register callback functions with the bond manager.
  260. *
  261. * NOTE: There is no need to register a passcode callback function
  262. * if the passcode will be handled with the GAPBOND_DEFAULT_PASSCODE parameter.
  263. *
  264. * @param pCB - pointer to callback function structure.
  265. *
  266. * @return none
  267. */
  268. extern void GAPBondMgr_Register( gapBondCBs_t *pCB );
  269. /**
  270. * @brief Respond to a passcode request.
  271. *
  272. * @param connectionHandle - connection handle of the connected device or 0xFFFF
  273. * if all devices in database.
  274. * @param status - SUCCESS if passcode is available, otherwise see @ref SMP_PAIRING_FAILED_DEFINES.
  275. * @param passcode - integer value containing the passcode.
  276. *
  277. * @return SUCCESS - bond record found and changed,<BR>
  278. * bleIncorrectMode - Link not found.
  279. */
  280. extern bStatus_t GAPBondMgr_PasscodeRsp( uint16 connectionHandle, uint8 status, uint32 passcode );
  281. /**
  282. * @brief This is a bypass mechanism to allow the bond manager to process
  283. * GAP messages.
  284. *
  285. * NOTE: This is an advanced feature and shouldn't be called unless
  286. * the normal GAP Bond Manager task ID registration is overridden.
  287. *
  288. * @param pMsg - GAP event message
  289. *
  290. * @return TRUE if safe to deallocate incoming GAP message, FALSE otherwise.
  291. */
  292. extern uint8 GAPBondMgr_ProcessGAPMsg( gapEventHdr_t *pMsg );
  293. /**
  294. * @brief This function will check the length of a Bond Manager NV Item.
  295. *
  296. * @param id - NV ID.
  297. * @param len - lengths in bytes of item.
  298. *
  299. * @return SUCCESS or FAILURE
  300. */
  301. extern uint8 GAPBondMgr_CheckNVLen( uint8 id, uint8 len );
  302. /**
  303. * @} End GAPROLES_BONDMGR_API
  304. */
  305. /*-------------------------------------------------------------------
  306. * TASK FUNCTIONS - Don't call these. These are system functions.
  307. */
  308. /**
  309. * @internal
  310. *
  311. * @brief Initialization function for the GAP Bond Manager Task.
  312. * This is called during initialization and should contain
  313. * any application specific initialization (ie. hardware
  314. * initialization/setup, table initialization, power up
  315. * notificaiton ... ).
  316. *
  317. * @param the ID assigned by OSAL. This ID should be
  318. * used to send messages and set timers.
  319. *
  320. * @return void
  321. */
  322. extern void GAPBondMgr_Init( uint8 task_id );
  323. /**
  324. * @internal
  325. *
  326. * @brief GAP Bond Manager Task event processor.
  327. * This function is called to process all events for the task.
  328. * Events include timers, messages and any other user defined
  329. * events.
  330. *
  331. * @param task_id - The OSAL assigned task ID.
  332. * @param events - events to process. This is a bit map and can
  333. * contain more than one event.
  334. *
  335. * @return events not processed
  336. */
  337. extern uint16 GAPBondMgr_ProcessEvent( uint8 task_id, uint16 events );
  338. /*-------------------------------------------------------------------
  339. -------------------------------------------------------------------*/
  340. #ifdef __cplusplus
  341. }
  342. #endif
  343. #endif /* GAPBONDMGR_H */