gap_internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. **************************************************************************************************/
  30. #ifndef GAP_INTERNAL_H
  31. #define GAP_INTERNAL_H
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. /*********************************************************************
  37. * INCLUDES
  38. */
  39. #include "bcomdef.h"
  40. #include "hci.h"
  41. #include "l2cap.h"
  42. #include "gap.h"
  43. /*********************************************************************
  44. * MACROS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. // GAP OSAL Events
  50. #define GAP_OSAL_TIMER_SCAN_DURATION_EVT 0x0001
  51. #define GAP_END_ADVERTISING_EVT 0x0002
  52. #define GAP_CHANGE_RESOLVABLE_PRIVATE_ADDR_EVT 0x0004
  53. #define ADDRTYPE_RANDOM 1 // Not public
  54. #define GAP_PRIVATE_ADDR_CHANGE_RESOLUTION 0xEA60 // Timer resolution is 1 minute
  55. #define ADV_TOKEN_HDR 2
  56. // Address header bits
  57. #define RANDOM_ADDR_HDR 0xC0 // Used for LL RANDOM Address
  58. #define STATIC_ADDR_HDR 0xC0 // Host Static Address, same as RANDOM address
  59. #define PRIVATE_RESOLVE_ADDR_HDR 0x40
  60. #if defined ( TESTMODES )
  61. // GAP TestModes
  62. #define GAP_TESTMODE_OFF 0 // No Test mode
  63. #define GAP_TESTMODE_NO_RESPONSE 1 // Don't respond to any GAP message
  64. #endif // TESTMODES
  65. // L2CAP Connection Parameters Update Request event
  66. #define L2CAP_PARAM_UPDATE 0xFFFF
  67. /*********************************************************************
  68. * TYPEDEFS
  69. */
  70. typedef struct gapAdvToken
  71. {
  72. struct gapAdvToken *pNext; // Pointer to next item in link list
  73. gapAdvDataToken_t *pToken; // Pointer to data token
  74. } gapAdvToken_t;
  75. /** Advertising and Scan Response Data **/
  76. typedef struct
  77. {
  78. uint8 dataLen; // Number of bytes used in "dataField"
  79. uint8 dataField[B_MAX_ADV_LEN]; // Data field of the advertisement or SCAN_RSP
  80. } gapAdvertisingData_t;
  81. typedef struct
  82. {
  83. uint8 dataLen; // length (in bytes) of "dataField"
  84. uint8 dataField[1]; // This is just a place holder size
  85. // The dataField will be allocated bigger
  86. } gapAdvertRecData_t;
  87. // Temporary advertising record
  88. typedef struct
  89. {
  90. uint8 eventType; // Avertisement or SCAN_RSP
  91. uint8 addrType; // Advertiser's address type
  92. uint8 addr[B_ADDR_LEN]; // Advertiser's address
  93. gapAdvertRecData_t *pAdData; // Advertising data field. This space is allocated.
  94. gapAdvertRecData_t *pScanData; // SCAN_RSP data field. This space is allocated.
  95. } gapAdvertRec_t;
  96. typedef enum
  97. {
  98. GAP_ADSTATE_SET_PARAMS, // Setting the advertisement parameters
  99. GAP_ADSTATE_SET_MODE, // Turning on advertising
  100. GAP_ADSTATE_ADVERTISING, // Currently Advertising
  101. GAP_ADSTATE_ENDING // Turning off advertising
  102. } gapAdvertStatesIDs_t;
  103. // Advertising State Information
  104. typedef struct
  105. {
  106. uint8 taskID; // App that started an advertising period
  107. gapAdvertStatesIDs_t state; // Make Discoverable state
  108. gapAdvertisingParams_t params; // Advertisement parameters
  109. } gapAdvertState_t;
  110. typedef struct
  111. {
  112. uint8 state; // Authentication states
  113. uint16 connectionHandle; // Connection Handle from controller,
  114. smLinkSecurityReq_t secReqs; // Pairing Control info
  115. // The following are only used if secReqs.bondable == BOUND, which means that
  116. // the device is already bound and we should use the security information and
  117. // keys
  118. smSecurityInfo_t *pSecurityInfo; // BOUND - security information
  119. smIdentityInfo_t *pIdentityInfo; // BOUND - identity information
  120. smSigningInfo_t *pSigningInfo; // Signing information
  121. } gapAuthStateParams_t;
  122. // Callback when an HCI Command Event has been received on the Central.
  123. typedef uint8 (*gapProcessHCICmdEvt_t)( uint16 cmdOpcode, hciEvt_CmdComplete_t *pMsg );
  124. // Callback when an Scanning Report has been received on the Central.
  125. typedef void (*gapProcessScanningEvt_t)( hciEvt_BLEAdvPktReport_t *pMsg );
  126. // Callback to cancel a connection initiation on the Central.
  127. typedef bStatus_t (*gapCancelLinkReq_t)( uint8 taskID, uint16 connectionHandle );
  128. // Callback when a connection-related event has been received on the Central.
  129. typedef uint8(*gapProcessConnEvt_t)( uint16 cmdOpcode, hciEvt_CommandStatus_t *pMsg );
  130. // Callback when an HCI Command Command Event on the Peripheral.
  131. typedef uint8 (*gapProcessHCICmdCompleteEvt_t)( hciEvt_CmdComplete_t *pMsg );
  132. // Callback when an Advertising Event has been received on the Peripheral.
  133. typedef void (*gapProcessAdvertisingEvt_t)( uint8 timeout );
  134. // Callback when a Set Advertising Params has been received on the Peripheral.
  135. typedef bStatus_t (*gapSetAdvParams_t)( void );
  136. // Central callback structure - must be setup by the Central.
  137. typedef struct
  138. {
  139. gapProcessHCICmdEvt_t pfnProcessHCICmdEvt; // When HCI Command Event received
  140. gapProcessScanningEvt_t pfnProcessScanningEvt; // When Scanning Report received
  141. } gapCentralCBs_t;
  142. // Central connection-related callback structure - must be setup by the Central.
  143. typedef struct
  144. {
  145. gapCancelLinkReq_t pfnCancelLinkReq; // When cancel connection initiation requested
  146. gapProcessConnEvt_t pfnProcessConnEvt; // When connection-related event received
  147. } gapCentralConnCBs_t;
  148. // Peripheral callback structure - must be setup by the Peripheral.
  149. typedef struct
  150. {
  151. gapProcessHCICmdCompleteEvt_t pfnProcessHCICmdCompleteEvt; // When HCI Command Complete Event received
  152. gapProcessAdvertisingEvt_t pfnProcessAdvertisingEvt; // When Advertising Event received
  153. gapSetAdvParams_t pfnSetAdvParams; // When Set Advertising Params received
  154. } gapPeripheralCBs_t;
  155. /*********************************************************************
  156. * GLOBAL VARIABLES
  157. */
  158. extern uint8 gapTaskID;
  159. extern uint8 gapUnwantedTaskID;
  160. extern uint8 gapAppTaskID; // default task ID to send events
  161. extern uint8 gapProfileRole; // device GAP Profile Role(s)
  162. extern uint8 gapDeviceAddrMode; // ADDRTYPE_PUBLIC, ADDRTYPE_STATIC,
  163. // ADDRTYPE_PRIVATE_NONRESOLVE
  164. // or ADDRTYPE_PRIVATE_RESOLVE
  165. // Central Peripheral variables
  166. extern gapDevDiscReq_t *pGapDiscReq;
  167. extern gapEstLinkReq_t *pEstLink;
  168. extern gapCentralConnCBs_t *pfnCentralConnCBs;
  169. // Peripheral variables
  170. extern gapAdvertState_t *pGapAdvertState;
  171. extern gapPeripheralCBs_t *pfnPeripheralCBs;
  172. // Common variables
  173. extern gapAuthStateParams_t *pAuthLink;
  174. extern uint16 gapPrivateAddrChangeTimeout;
  175. extern uint8 gapAutoAdvPrivateAddrChange;
  176. /*********************************************************************
  177. * FUNCTIONS - API
  178. */
  179. /*********************************************************************
  180. * Application Level Functions
  181. */
  182. /*
  183. * gapSetScanParamStatus - Process HCI Command Complete Event status for
  184. * the call to HCI_BLESetScanParamCmd().
  185. */
  186. extern uint8 gapSetScanParamStatus( uint8 status );
  187. /*
  188. * gapSetAdvParamsStatus - Process HCI Command Complete Event status for
  189. * the call to HCI_BLESetAdvParamCmd().
  190. */
  191. extern uint8 gapSetAdvParamsStatus( uint8 status );
  192. /*
  193. * gapWriteAdvEnableStatus - Process HCI Command Complete Event status for
  194. * the call to HCI_BLEWriteAdvEnableCmd().
  195. */
  196. extern uint8 gapWriteAdvEnableStatus( uint8 status, uint16 interval );
  197. /*
  198. * gapWriteAdvDataStatus - Process HCI Command Complete Event status for
  199. * the call to HCI_BLEWriteAdvDataCmd() or
  200. * HCI_BLEWriteScanRspDataCmd().
  201. */
  202. extern void gapWriteAdvDataStatus( uint8 adType, uint8 status );
  203. /*
  204. * gapReadBD_ADDRStatus - Process the HCI Command Complete Event for the
  205. * call to HCI_ReadBDADDRCmd().
  206. */
  207. extern uint8 gapReadBD_ADDRStatus( uint8 status, uint8 *pBdAddr );
  208. /*
  209. * gapReadBufSizeCmdStatus - Process the HCI Command Complete Event for the
  210. * call to HCI_BLEReadBufSizeCmd().
  211. */
  212. extern uint8 gapReadBufSizeCmdStatus( hciRetParam_LeReadBufSize_t *pCmdStat );
  213. /*
  214. * gapProcessConnectionCompleteEvt - Process the HCI Connection Complete
  215. * event for the call to HCI_BLECreateLLConnCmd().
  216. */
  217. extern void gapProcessConnectionCompleteEvt( hciEvt_BLEConnComplete_t *pPkt );
  218. /*
  219. * gapProcessConnUpdateCompleteEvt - Process the HCI Connection Parameters
  220. * Update Complete event for the call to HCI_BLEUpdateLLConnCmd().
  221. */
  222. extern void gapProcessConnUpdateCompleteEvt( hciEvt_BLEConnUpdateComplete_t *pPkt );
  223. /*
  224. * gapProcessDisconnectCompleteEvt - Process the LL Disconnection Complete Event
  225. * for the call to HCI_DisconnectCmd().
  226. */
  227. extern void gapProcessDisconnectCompleteEvt( hciEvt_DisconnComplete_t *pPkt );
  228. /*
  229. * gapProcessCreateLLConnCmdStatus - Process the status for the HCI_BLECreateLLConnCmd().
  230. */
  231. extern void gapProcessCreateLLConnCmdStatus( uint8 status );
  232. /*
  233. * gapProcessConnUpdateCmdStatus - Process the status for the HCI_LE_ConnUpdateCmd().
  234. */
  235. extern void gapProcessConnUpdateCmdStatus( uint8 status );
  236. /*
  237. * gapProcessNewAddr - Process message SM
  238. */
  239. extern bStatus_t gapProcessNewAddr( uint8 *pNewAddr );
  240. /*
  241. * gapAddAddrAdj - Add the top two bits based on the address type.
  242. */
  243. extern uint8 gapAddAddrAdj( uint8 addrType, uint8 *pAddr );
  244. /*
  245. * gapDetermineAddrType - Convert from LL address type to host address type.
  246. */
  247. extern uint8 gapDetermineAddrType( uint8 addrType, uint8 *pAddr );
  248. /*
  249. * gapProcessRandomAddrComplete - Process message HCI
  250. */
  251. extern void gapProcessRandomAddrComplete( uint8 status );
  252. /*
  253. * gapGetSRK - Get pointer to the SRK
  254. */
  255. extern uint8 *gapGetSRK( void );
  256. /*
  257. * gapGetSignCounter - Get the signature counter
  258. */
  259. extern uint32 gapGetSignCounter( void );
  260. /*
  261. * gapIncSignCounter - Increment the signature counter
  262. */
  263. extern void gapIncSignCounter( void );
  264. /*
  265. * gapUpdateConnSignCounter - Update a connection's signature's counter
  266. */
  267. extern void gapUpdateConnSignCounter( uint16 connHandle, uint32 newSignCounter );
  268. /*
  269. * gapLinkCheck - linkDB callback function
  270. */
  271. extern void gapLinkCheck( uint16 connectionHandle, uint8 changeType );
  272. /*
  273. * gapGetDevAddressMode - Get the device address mode.
  274. */
  275. extern uint8 gapGetDevAddressMode( void );
  276. /*
  277. * gapGetDevAddress - Get the device address.
  278. * real - TRUE if you always want BD_ADDR, FALSE will allow random addresses.
  279. */
  280. extern uint8 *gapGetDevAddress( uint8 real );
  281. /*
  282. * gapGetIRK - Get the device's IRK.
  283. */
  284. extern uint8 *gapGetIRK( void );
  285. /*
  286. * gapPasskeyNeededCB - Callback function to ask for passkey
  287. */
  288. extern void gapPasskeyNeededCB( uint16 connectionHandle, uint8 type );
  289. /*
  290. * gapPairingCompleteCB - Callback function to inform pairing process complete.
  291. */
  292. extern void gapPairingCompleteCB( uint8 status, uint8 initiatorRole,
  293. uint16 connectionHandle,
  294. uint8 authState,
  295. smSecurityInfo_t *pEncParams,
  296. smSecurityInfo_t *pDevEncParams,
  297. smIdentityInfo_t *pIdInfo,
  298. smSigningInfo_t *pSigningInfo );
  299. /*
  300. * gapTerminateConnComplete - Process command complete for HCI_BLECreateLLConnCancelCmd.
  301. */
  302. extern void gapTerminateConnComplete( void );
  303. /*
  304. * gapSendSlaveSecurityReqEvent - Generate a Slave Security Request event to the app.
  305. */
  306. extern void gapSendSlaveSecurityReqEvent( uint8 taskID, uint16 connHandle, uint8 *pDevAddr, uint8 authReq );
  307. /*
  308. * gapSetAdvParams - Send the advertisement parameters to the LL.
  309. */
  310. extern bStatus_t gapSetAdvParams( void );
  311. /*
  312. * gapAddAdvToken - Add token to the end of the list.
  313. */
  314. extern bStatus_t gapAddAdvToken( gapAdvDataToken_t *pToken );
  315. /*
  316. * gapDeleteAdvToken - Remove a token from the list.
  317. */
  318. extern gapAdvDataToken_t *gapDeleteAdvToken( uint8 ADType );
  319. /*
  320. * gapFindAdvToken - Find a Advertisement data token from the advertisement type.
  321. */
  322. extern gapAdvToken_t *gapFindAdvToken( uint8 ADType );
  323. /*
  324. * gapCalcAdvTokenDataLen - Find a Advertisement data token from the advertisement type.
  325. */
  326. extern void gapCalcAdvTokenDataLen( uint8 *pAdLen, uint8 *pSrLen );
  327. /*
  328. * gapValidADType - Is a Advertisement Data Type valid.
  329. */
  330. extern uint8 gapValidADType( uint8 adType );
  331. /*
  332. * gapBuildADTokens - Is a Advertisement Data Type valid.
  333. */
  334. extern bStatus_t gapBuildADTokens( void );
  335. /*
  336. * gapSendBondCompleteEvent - Indicate that a bond has occurred.
  337. */
  338. extern void gapSendBondCompleteEvent( uint8 status, uint16 connectionHandle );
  339. /*
  340. * gapSendPairingReqEvent - Indicate that an unexpected Pairing Request was received.
  341. */
  342. extern void gapSendPairingReqEvent( uint8 status, uint16 connectionHandle,
  343. uint8 ioCap,
  344. uint8 oobDataFlag,
  345. uint8 authReq,
  346. uint8 maxEncKeySize,
  347. keyDist_t keyDist );
  348. /*
  349. * gapFindADType - Find Advertisement Data Type field in advertising data
  350. * field.
  351. */
  352. extern uint8 *gapFindADType( uint8 adType, uint8 *pAdLen,
  353. uint8 dataLen, uint8 *pDataField );
  354. /*
  355. * gapRegisterCentral - Register Central's processing function with GAP task
  356. */
  357. extern void gapRegisterCentral( gapCentralCBs_t *pfnCBs );
  358. /*
  359. * gapRegisterCentralConn - Register Central's connection-related processing function with GAP task
  360. */
  361. extern void gapRegisterCentralConn( gapCentralConnCBs_t *pfnCBs);
  362. /*
  363. * gapRegisterPeripheral - Register Peripheral's processing function with GAP task
  364. */
  365. extern void gapRegisterPeripheral( gapPeripheralCBs_t *pfnCBs );
  366. /*
  367. * gapIsAdvertising - Check if we are currently advertising.
  368. */
  369. extern uint8 gapIsAdvertising( void );
  370. /*
  371. * gapIsScanning - Check if we are currently scanning.
  372. */
  373. extern uint8 gapIsScanning( void );
  374. /*
  375. * gapCancelLinkReq - Cancel a connection create request.
  376. */
  377. extern bStatus_t gapCancelLinkReq( uint8 taskID, uint16 connectionHandle );
  378. /*
  379. * gapFreeEstLink - Free the establish link memory.
  380. */
  381. extern void gapFreeEstLink( void );
  382. /*
  383. * sendEstLinkEvent - Build and send the GAP_LINK_ESTABLISHED_EVENT to the app.
  384. */
  385. extern void sendEstLinkEvent( uint8 status, uint8 taskID, uint8 devAddrType,
  386. uint8 *pDevAddr, uint16 connectionHandle,
  387. uint16 connInterval, uint16 connLatency,
  388. uint16 connTimeout, uint16 clockAccuracy );
  389. /*
  390. * gapSendLinkUpdateEvent - Build and send the GAP_LINK_PARAM_UPDATE_EVENT to the app.
  391. *
  392. */
  393. extern void gapSendLinkUpdateEvent( uint8 status, uint16 connectionHandle,
  394. uint16 connInterval, uint16 connLatency,
  395. uint16 connTimeout );
  396. /*
  397. * gapProcessL2CAPSignalEvt - Process L2CAP Signaling messages.
  398. */
  399. extern void gapProcessL2CAPSignalEvt( l2capSignalEvent_t *pCmd );
  400. /*********************************************************************
  401. *********************************************************************/
  402. #ifdef __cplusplus
  403. }
  404. #endif
  405. #endif /* GAP_INTERNAL_H */