linkdb.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. Filename: linkdb.h
  30. Revised:
  31. Revision:
  32. Description: This file contains the linkDB interface.
  33. **************************************************************************************************/
  34. #ifndef LINKDB_H
  35. #define LINKDB_H
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40. /*********************************************************************
  41. * INCLUDES
  42. */
  43. /*********************************************************************
  44. * MACROS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. // Special case connection handles
  50. #define INVALID_CONNHANDLE 0xFFFF // Invalid connection handle, used for no connection handle
  51. #define LOOPBACK_CONNHANDLE 0xFFFE // Loopback connection handle, used to loopback a message
  52. // Link state flags
  53. #define LINK_NOT_CONNECTED 0x00 // Link isn't connected
  54. #define LINK_CONNECTED 0x01 // Link is connected
  55. #define LINK_AUTHENTICATED 0x02 // Link is authenticated
  56. #define LINK_BOUND 0x04 // Link is bonded
  57. #define LINK_ENCRYPTED 0x10 // Link is encrypted
  58. // Link Database Status callback changeTypes
  59. #define LINKDB_STATUS_UPDATE_NEW 0 // New connection created
  60. #define LINKDB_STATUS_UPDATE_REMOVED 1 // Connection was removed
  61. #define LINKDB_STATUS_UPDATE_STATEFLAGS 2 // Connection state flag changed
  62. // Link Authentication Errors
  63. #define LINKDB_ERR_INSUFFICIENT_AUTHEN 0x05 // Link isn't even encrypted
  64. #define LINBDB_ERR_INSUFFICIENT_KEYSIZE 0x0c // Link is encrypted but the key size is too small
  65. #define LINKDB_ERR_INSUFFICIENT_ENCRYPTION 0x0f // Link is encrypted but it's not authenticated
  66. /*********************************************************************
  67. * TYPEDEFS
  68. */
  69. typedef struct
  70. {
  71. uint8 srk[KEYLEN]; // Signature Resolving Key
  72. uint32 signCounter; // Sign Counter
  73. } linkSec_t;
  74. typedef struct
  75. {
  76. uint8 ltk[KEYLEN]; // Long Term Key
  77. uint16 div; // Diversifier
  78. uint8 rand[B_RANDOM_NUM_SIZE]; // random number
  79. uint8 keySize; // LTK Key Size
  80. } encParams_t;
  81. typedef struct
  82. {
  83. uint8 taskID; // Application that controls the link
  84. uint16 connectionHandle; // Controller connection handle
  85. uint8 stateFlags; // LINK_CONNECTED, LINK_AUTHENTICATED...
  86. uint8 addrType; // Address type of connected device
  87. uint8 addr[B_ADDR_LEN]; // Other Device's address
  88. uint16 connInterval; // The connection's interval (n * 1.23 ms)
  89. linkSec_t sec; // Connection Security related items
  90. encParams_t *pEncParams; // pointer to LTK, ediv, rand. if needed.
  91. } linkDBItem_t;
  92. // function pointer used to register for a status callback
  93. typedef void (*pfnLinkDBCB_t)( uint16 connectionHandle, uint8 changeType );
  94. // function pointer used to perform specialized link database searches
  95. typedef void (*pfnPerformFuncCB_t)( linkDBItem_t *pLinkItem );
  96. /*********************************************************************
  97. * GLOBAL VARIABLES
  98. */
  99. /*********************************************************************
  100. * PUBLIC FUNCTIONS
  101. */
  102. /*
  103. * linkDB_Init - Initialize the Link Database.
  104. */
  105. extern void linkDB_Init( void );
  106. /*
  107. * linkDB_Register - Register with this function to receive a callback when
  108. * status changes on a connection.
  109. */
  110. extern uint8 linkDB_Register( pfnLinkDBCB_t pFunc );
  111. /*
  112. * linkDB_Add - Adds a record to the link database.
  113. */
  114. extern uint8 linkDB_Add( uint8 taskID, uint16 connectionHandle, uint8 stateFlags,
  115. uint8 addrType, uint8 *pAddr, uint16 connInterval );
  116. /*
  117. * linkDB_Remove - Removes a record from the link database.
  118. */
  119. extern uint8 linkDB_Remove( uint16 connectionHandle );
  120. /*
  121. * linkDB_Update - This function is used to update the stateFlags of
  122. * a link record.
  123. */
  124. extern uint8 linkDB_Update( uint16 connectionHandle, uint8 newState );
  125. /*
  126. * linkDB_NumActive - returns the number of active connections.
  127. */
  128. extern uint8 linkDB_NumActive( void );
  129. /*
  130. * linkDB_Find - Find link database item (link information)
  131. *
  132. * returns a pointer to the link item, NULL if not found
  133. */
  134. extern linkDBItem_t *linkDB_Find( uint16 connectionHandle );
  135. /*
  136. * linkDB_FindFirst - Find the first link that matches the taskID.
  137. *
  138. * returns a pointer to the link item, NULL if not found
  139. */
  140. extern linkDBItem_t *linkDB_FindFirst( uint8 taskID );
  141. /*
  142. * linkDB_State - Check to see if a physical link is in a specific state.
  143. *
  144. * returns TRUE is the link is in state. FALSE, otherwise.
  145. */
  146. extern uint8 linkDB_State( uint16 connectionHandle, uint8 state );
  147. /*
  148. * linkDB_Authen - Check to see if the physical link is encrypted and authenticated.
  149. * returns SUCCESS if the link is authenticated or
  150. * bleNotConnected - connection handle is invalid,
  151. * LINKDB_ERR_INSUFFICIENT_AUTHEN - link is not encrypted,
  152. * LINBDB_ERR_INSUFFICIENT_KEYSIZE - key size encrypted is not large enough,
  153. * LINKDB_ERR_INSUFFICIENT_ENCRYPTION - link is encrypted, but not authenticated
  154. */
  155. extern uint8 linkDB_Authen( uint16 connectionHandle, uint8 keySize, uint8 mitmRequired );
  156. /*
  157. * linkDB_PerformFunc - Perform a function of each connection in the link database.
  158. */
  159. extern void linkDB_PerformFunc( pfnPerformFuncCB_t cb );
  160. /*
  161. * linkDB_Up - Check to see if a physical link is up (connected).
  162. * Use like: uint8 linkDB_Up( uint16 connectionHandle );
  163. * connectionHandle - controller link connection handle.
  164. * returns TRUE if the link is up. FALSE, otherwise.
  165. */
  166. #define linkDB_Up( connectionHandle ) linkDB_State( (connectionHandle), LINK_CONNECTED )
  167. /*
  168. * linkDB_Encrypted - Check to see if the physical link is encrypted.
  169. * Use like: linkDB_Encrypted( uint16 connectionHandle );
  170. * connectionHandle - controller link connection handle.
  171. * returns TRUE if the link is encrypted. FALSE, otherwise.
  172. */
  173. #define linkDB_Encrypted( connectionHandle ) linkDB_State( (connectionHandle), LINK_ENCRYPTED )
  174. /*
  175. * linkDB_Authenticated - Check to see if the physical link is authenticated.
  176. * Use like: linkDB_Authenticated( uint16 connectionHandle );
  177. * connectionHandle - controller link connection handle.
  178. * returns TRUE if the link is authenticated. FALSE, otherwise.
  179. */
  180. #define linkDB_Authenticated( connectionHandle ) linkDB_State( (connectionHandle), LINK_AUTHENTICATED )
  181. /*
  182. * linkDB_Bonded - Check to see if the physical link is bonded.
  183. * Use like: linkDB_Bonded( uint16 connectionHandle );
  184. * connectionHandle - controller link connection handle.
  185. * returns TRUE if the link is bonded. FALSE, otherwise.
  186. */
  187. #define linkDB_Bonded( connectionHandle ) linkDB_State( (connectionHandle), LINK_BOUND )
  188. /*********************************************************************
  189. *********************************************************************/
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. #endif /* LINKDB_H */