linkdb.h 7.4 KB

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