peripheral.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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: peripheral.h
  12. $Date:
  13. $Revision:
  14. This GAP profile advertises and allows connections.
  15. */
  16. #ifndef PERIPHERAL_H
  17. #define PERIPHERAL_H
  18. #ifdef EXT_ADV_ENABLE
  19. #include "rflib_LR.h"
  20. #endif
  21. #ifdef __cplusplus
  22. extern "C"
  23. {
  24. #endif
  25. /*-------------------------------------------------------------------
  26. * INCLUDES
  27. */
  28. #include "types.h"
  29. #include "bcomdef.h"
  30. /*-------------------------------------------------------------------
  31. * CONSTANTS
  32. */
  33. /** @defgroup GAPROLE_PROFILE_PARAMETERS GAP Role Parameters
  34. * @{
  35. */
  36. #define GAPROLE_PROFILEROLE 0x300 //!< Reading this parameter will return GAP Role type. Read Only. Size is uint8.
  37. #define GAPROLE_IRK 0x301 //!< Identity Resolving Key. Read/Write. Size is uint8[KEYLEN]. Default is all 0, which means that the IRK will be randomly generated.
  38. #define GAPROLE_SRK 0x302 //!< Signature Resolving Key. Read/Write. Size is uint8[KEYLEN]. Default is all 0, which means that the SRK will be randomly generated.
  39. #define GAPROLE_SIGNCOUNTER 0x303 //!< Sign Counter. Read/Write. Size is uint32. Default is 0.
  40. #define GAPROLE_BD_ADDR 0x304 //!< Device's Address. Read Only. Size is uint8[B_ADDR_LEN]. This item is read from the controller.
  41. #define GAPROLE_ADVERT_ENABLED 0x305 //!< Enable/Disable Advertising. Read/Write. Size is uint8. Default is TRUE=Enabled.
  42. #define GAPROLE_ADVERT_OFF_TIME 0x306 //!< Advertising Off Time for Limited advertisements (in milliseconds). Read/Write. Size is uint16. Default is 30 seconds.
  43. #define GAPROLE_ADVERT_DATA 0x307 //!< Advertisement Data. Read/Write. Max size is uint8[B_MAX_ADV_LEN]. Default is "02:01:01", which means that it is a Limited Discoverable Advertisement.
  44. #define GAPROLE_SCAN_RSP_DATA 0x308 //!< Scan Response Data. Read/Write. Max size is uint8[B_MAX_ADV_LEN]. Defaults to all 0.
  45. #define GAPROLE_ADV_EVENT_TYPE 0x309 //!< Advertisement Type. Read/Write. Size is uint8. Default is GAP_ADTYPE_ADV_IND (defined in GAP.h).
  46. #define GAPROLE_ADV_DIRECT_TYPE 0x30A //!< Direct Advertisement Address Type. Read/Write. Size is uint8. Default is ADDRTYPE_PUBLIC (defined in GAP.h).
  47. #define GAPROLE_ADV_DIRECT_ADDR 0x30B //!< Direct Advertisement Address. Read/Write. Size is uint8[B_ADDR_LEN]. Default is NULL.
  48. #define GAPROLE_ADV_CHANNEL_MAP 0x30C //!< Which channels to advertise on. Read/Write Size is uint8. Default is GAP_ADVCHAN_ALL (defined in GAP.h)
  49. #define GAPROLE_ADV_FILTER_POLICY 0x30D //!< Filter Policy. Ignored when directed advertising is used. Read/Write. Size is uint8. Default is GAP_FILTER_POLICY_ALL (defined in GAP.h).
  50. #define GAPROLE_CONNHANDLE 0x30E //!< Connection Handle. Read Only. Size is uint16.
  51. #define GAPROLE_RSSI_READ_RATE 0x30F //!< How often to read the RSSI during a connection. Read/Write. Size is uint16. The value is in milliseconds. Default is 0 = OFF.
  52. #define GAPROLE_PARAM_UPDATE_ENABLE 0x310 //!< Slave Connection Parameter Update Enable. Read/Write. Size is uint8. If TRUE then automatic connection parameter update request is sent. Default is FALSE.
  53. #define GAPROLE_MIN_CONN_INTERVAL 0x311 //!< Minimum Connection Interval to allow (n * 1.25ms). Range: 7.5 msec to 4 seconds (0x0006 to 0x0C80). Read/Write. Size is uint16. Default is 7.5 milliseconds (0x0006).
  54. #define GAPROLE_MAX_CONN_INTERVAL 0x312 //!< Maximum Connection Interval to allow (n * 1.25ms). Range: 7.5 msec to 4 seconds (0x0006 to 0x0C80). Read/Write. Size is uint16. Default is 4 seconds (0x0C80).
  55. #define GAPROLE_SLAVE_LATENCY 0x313 //!< Update Parameter Slave Latency. Range: 0 - 499. Read/Write. Size is uint16. Default is 0.
  56. #define GAPROLE_TIMEOUT_MULTIPLIER 0x314 //!< Update Parameter Timeout Multiplier (n * 10ms). Range: 100ms to 32 seconds (0x000a - 0x0c80). Read/Write. Size is uint16. Default is 1000.
  57. #define GAPROLE_CONN_BD_ADDR 0x315 //!< Address of connected device. Read only. Size is uint8[B_MAX_ADV_LEN]. Set to all zeros when not connected.
  58. #define GAPROLE_CONN_INTERVAL 0x316 //!< Current connection interval. Read only. Size is uint16. Range is 7.5ms to 4 seconds (0x0006 to 0x0C80). Default is 0 (no connection).
  59. #define GAPROLE_CONN_LATENCY 0x317 //!< Current slave latency. Read only. Size is uint16. Range is 0 to 499. Default is 0 (no slave latency or no connection).
  60. #define GAPROLE_CONN_TIMEOUT 0x318 //!< Current timeout value. Read only. size is uint16. Range is 100ms to 32 seconds. Default is 0 (no connection).
  61. #define GAPROLE_PARAM_UPDATE_REQ 0x319 //!< Slave Connection Parameter Update Request. Write. Size is uint8. If TRUE then connection parameter update request is sent.
  62. #define GAPROLE_STATE 0x31A //!< Reading this parameter will return GAP Peripheral Role State. Read Only. Size is uint8.
  63. #define GAPROLE_CONNECTION_INTERVAL 0x31B
  64. #define GAPROLE_CONNECTION_LATENCY 0x31C
  65. #define GAPROLE_CONN_BD_ADDR_TYPE 0x31D
  66. #ifdef EXT_ADV_ENABLE
  67. #define GAPROLE_EXT_ADVERT_ENABLED 0x0320
  68. #define GAPROLE_EXT_ADVERT_DATA 0x0321
  69. #define GAPROLE_EXT_SCAN_RSP_DATA 0x0322
  70. #define GAPROLE_EXT_ADV_EVENT_TYPE 0x0323
  71. #endif
  72. /** @} End GAPROLE_PROFILE_PARAMETERS */
  73. /*-------------------------------------------------------------------
  74. * TYPEDEFS
  75. */
  76. /**
  77. * GAP Peripheral Role States.
  78. */
  79. typedef enum
  80. {
  81. GAPROLE_INIT = 0, //!< Waiting to be started
  82. GAPROLE_STARTED, //!< Started but not advertising
  83. GAPROLE_ADVERTISING, //!< Currently Advertising
  84. GAPROLE_WAITING, //!< Device is started but not advertising, is in waiting period before advertising again
  85. GAPROLE_WAITING_AFTER_TIMEOUT, //!< Device just timed out from a connection but is not yet advertising, is in waiting period before advertising again
  86. GAPROLE_CONNECTED, //!< In a connection
  87. GAPROLE_CONNECTED_ADV, //!< In a connection + advertising
  88. GAPROLE_ERROR //!< Error occurred - invalid state
  89. } gaprole_States_t;
  90. /**
  91. * Possible actions the peripheral device may take if an unsuccessful parameter
  92. * update is received.
  93. *
  94. * Parameters for GAPRole_SendUpdateParam() only
  95. */
  96. #define GAPROLE_NO_ACTION 0 // Take no action upon unsuccessful parameter updates
  97. #define GAPROLE_RESEND_PARAM_UPDATE 1 // Continue to resend request until successful update
  98. #define GAPROLE_TERMINATE_LINK 2 // Terminate link upon unsuccessful parameter updates
  99. /*-------------------------------------------------------------------
  100. * MACROS
  101. */
  102. /*-------------------------------------------------------------------
  103. * Profile Callbacks
  104. */
  105. /**
  106. * Callback when the connection parameteres are updated.
  107. */
  108. typedef void (*gapRolesParamUpdateCB_t)( uint16 connInterval,
  109. uint16 connSlaveLatency,
  110. uint16 connTimeout );
  111. /**
  112. * Callback when the device has been started. Callback event to
  113. * the Notify of a state change.
  114. */
  115. typedef void (*gapRolesStateNotify_t)( gaprole_States_t newState );
  116. /**
  117. * Callback when the device has read an new RSSI value during a connection.
  118. */
  119. typedef void (*gapRolesRssiRead_t)( int8 newRSSI );
  120. /**
  121. * Callback structure - must be setup by the application and used when gapRoles_StartDevice() is called.
  122. */
  123. typedef struct
  124. {
  125. gapRolesStateNotify_t pfnStateChange; //!< Whenever the device changes state
  126. gapRolesRssiRead_t pfnRssiRead; //!< When a valid RSSI is read from controller
  127. } gapRolesCBs_t;
  128. /*-------------------------------------------------------------------
  129. * API FUNCTIONS
  130. */
  131. #ifdef EXT_ADV_ENABLE
  132. // extended advertising
  133. typedef unsigned int uintptr_t;
  134. typedef void (*pfnGapCB_t)
  135. (
  136. uint32_t event, //!< see @ref GapAdvScan_Event_IDs and GapAdvScan_Event_IDs
  137. void *pBuf, //!< data potentially accompanying event
  138. uintptr_t arg //!< custom application argument that can be return through this callback
  139. );
  140. typedef enum
  141. {
  142. GAP_ADV_DATA_TYPE_ADV, //!< Advertising data
  143. GAP_ADV_DATA_TYPE_SCAN_RSP //!< Scan response data
  144. } GapAdv_dataTypes_t;
  145. #endif
  146. /**
  147. * @defgroup GAPROLES_PERIPHERAL_API GAP Peripheral Role API Functions
  148. *
  149. * @{
  150. */
  151. bStatus_t GAPRole_EncodeAdvDeviceName(uint8_t * pAdvData, uint8_t * pOffset, uint8_t maxSize, uint8_t *devName, uint8_t devNameLen);
  152. bStatus_t GAPRole_EncodeAdvDeviceAddr(uint8_t * pAdvData, uint8_t * pOffset, uint8_t maxSize);
  153. /**
  154. * @brief Set a GAP Role parameter.
  155. *
  156. * NOTE: You can call this function with a GAP Parameter ID and it will set the
  157. * GAP Parameter. GAP Parameters are defined in (gap.h). Also,
  158. * the "len" field must be set to the size of a "uint16" and the
  159. * "pValue" field must point to a "uint16".
  160. *
  161. * @param param - Profile parameter ID: @ref GAPROLE_PROFILE_PARAMETERS
  162. * @param len - length of data to write
  163. * @param pValue - pointer to data to write. This is dependent on
  164. * the parameter ID and WILL be cast to the appropriate
  165. * data type (example: data type of uint16 will be cast to
  166. * uint16 pointer).
  167. *
  168. * @return SUCCESS or INVALIDPARAMETER (invalid paramID)
  169. */
  170. extern bStatus_t GAPRole_SetParameter( uint16 param, uint8 len, void *pValue );
  171. /**
  172. * @brief Get a GAP Role parameter.
  173. *
  174. * NOTE: You can call this function with a GAP Parameter ID and it will get a
  175. * GAP Parameter. GAP Parameters are defined in (gap.h). Also, the
  176. * "pValue" field must point to a "uint16".
  177. *
  178. * @param param - Profile parameter ID: @ref GAPROLE_PROFILE_PARAMETERS
  179. * @param pValue - pointer to location to get the value. This is dependent on
  180. * the parameter ID and WILL be cast to the appropriate
  181. * data type (example: data type of uint16 will be cast to
  182. * uint16 pointer).
  183. *
  184. * @return SUCCESS or INVALIDPARAMETER (invalid paramID)
  185. */
  186. extern bStatus_t GAPRole_GetParameter( uint16 param, void *pValue );
  187. #ifdef EXT_ADV_ENABLE
  188. extern bStatus_t GapRoleAdv_loadByHandle(uint8 handle, GapAdv_dataTypes_t dataType,
  189. uint16 len, uint8 *pBuf);
  190. #endif
  191. /**
  192. * @brief Does the device initialization. Only call this function once.
  193. *
  194. * @param pAppCallbacks - pointer to application callbacks.
  195. *
  196. * @return SUCCESS or bleAlreadyInRequestedMode
  197. */
  198. extern bStatus_t GAPRole_StartDevice( gapRolesCBs_t *pAppCallbacks );
  199. /**
  200. * @brief Terminates the existing connection.
  201. *
  202. * @return SUCCESS or bleIncorrectMode
  203. */
  204. extern bStatus_t GAPRole_TerminateConnection( void );
  205. /**
  206. * @brief Update the parameters of an existing connection
  207. *
  208. * @param connInterval - the new connection interval
  209. * @param latency - the new slave latency
  210. * @param connTimeout - the new timeout value
  211. * @param handleFailure - what to do if the update does not occur.
  212. * Method may choose to terminate connection, try again, or take no action
  213. *
  214. * @return SUCCESS, bleNotConnected or bleInvalidRange
  215. */
  216. extern bStatus_t GAPRole_SendUpdateParam( uint16 minConnInterval, uint16 maxConnInterval,
  217. uint16 latency, uint16 connTimeout, uint8 handleFailure );
  218. /**
  219. * @brief Register application's callbacks.
  220. *
  221. * @param pParamUpdateCB - pointer to param update callback.
  222. *
  223. * @return none
  224. */
  225. extern void GAPRole_RegisterAppCBs( gapRolesParamUpdateCB_t *pParamUpdateCB );
  226. /**
  227. * @} End GAPROLES_PERIPHERAL_API
  228. */
  229. /*-------------------------------------------------------------------
  230. * TASK FUNCTIONS - Don't call these. These are system functions.
  231. */
  232. /**
  233. * @internal
  234. *
  235. * @brief Initialization function for the GAP Role Task.
  236. * This is called during initialization and should contain
  237. * any application specific initialization (ie. hardware
  238. * initialization/setup, table initialization, power up
  239. * notificaiton ... ).
  240. *
  241. * @param the ID assigned by OSAL. This ID should be
  242. * used to send messages and set timers.
  243. *
  244. * @return void
  245. */
  246. extern void GAPRole_Init( uint8 task_id );
  247. /**
  248. * @internal
  249. *
  250. * @brief GAP Role Task event processor.
  251. * This function is called to process all events for the task.
  252. * Events include timers, messages and any other user defined
  253. * events.
  254. *
  255. * @param task_id - The OSAL assigned task ID.
  256. * @param events - events to process. This is a bit map and can
  257. * contain more than one event.
  258. *
  259. * @return events not processed
  260. */
  261. extern uint16 GAPRole_ProcessEvent( uint8 task_id, uint16 events );
  262. /*-------------------------------------------------------------------
  263. -------------------------------------------------------------------*/
  264. #ifdef EXT_ADV_ENABLE
  265. extern bStatus_t GAP_UpdateExtAdvertisingData( uint8 taskID, uint16_t adType,
  266. uint8 dataLen, uint8 *pAdvertData );
  267. /**
  268. * GAP Advertiser bitfields to enable / disable callback events
  269. *
  270. * These are used in @ref GapAdv_setEventMask
  271. * The events that that these flags control are defined in
  272. * @ref GapAdvScan_Event_IDs
  273. */
  274. // Advertising Scan Request Notification Flag
  275. #define AE_NOTIFY_DISABLE_SCAN_REQUEST ~BV(0)
  276. #define AE_NOTIFY_ENABLE_SCAN_REQUEST BV(0)
  277. #define AE_NOTIFY
  278. #define AE_NOTIFY_DISABLE_ADV_SET_START ~BV(4)
  279. #define AE_NOTIFY_ENABLE_ADV_SET_START BV(4)
  280. #define AE_NOTIFY_DISABLE_ADV_START ~BV(5)
  281. #define AE_NOTIFY_ENABLE_ADV_START BV(5)
  282. #define AE_NOTIFY_DISABLE_ADV_END ~BV(6)
  283. #define AE_NOTIFY_ENABLE_ADV_END BV(6)
  284. #define AE_NOTIFY_DISABLE_ADV_SET_END ~BV(7)
  285. #define AE_NOTIFY_ENABLE_ADV_SET_END BV(7)
  286. typedef enum
  287. {
  288. /**
  289. * Enables / disables the @ref GAP_EVT_SCAN_REQ_RECEIVED event
  290. */
  291. GAP_ADV_EVT_MASK_SCAN_REQ_NOTI = AE_NOTIFY_ENABLE_SCAN_REQUEST,
  292. /**
  293. * Enables / disables the @ref GAP_EVT_ADV_SET_TERMINATED event
  294. */
  295. GAP_ADV_EVT_MASK_SET_TERMINATED = BV(1),
  296. /**
  297. * Enables / disables the @ref GAP_EVT_ADV_START_AFTER_ENABLE event
  298. */
  299. GAP_ADV_EVT_MASK_START_AFTER_ENABLE = AE_NOTIFY_ENABLE_ADV_SET_START,
  300. /**
  301. * Enables / disables the @ref GAP_EVT_ADV_START event
  302. */
  303. GAP_ADV_EVT_MASK_START = AE_NOTIFY_ENABLE_ADV_START,
  304. /**
  305. * Enables / disables the @ref GAP_EVT_ADV_END event
  306. */
  307. GAP_ADV_EVT_MASK_END = AE_NOTIFY_ENABLE_ADV_END,
  308. /**
  309. * Enables / disables the @ref GAP_EVT_ADV_END_AFTER_DISABLE event
  310. */
  311. GAP_ADV_EVT_MASK_END_AFTER_DISABLE = AE_NOTIFY_ENABLE_ADV_SET_END,
  312. /**
  313. * Mask to enables / disable all advertising events
  314. */
  315. GAP_ADV_EVT_MASK_ALL = GAP_ADV_EVT_MASK_SCAN_REQ_NOTI |
  316. GAP_ADV_EVT_MASK_START_AFTER_ENABLE |
  317. GAP_ADV_EVT_MASK_START |
  318. GAP_ADV_EVT_MASK_END |
  319. GAP_ADV_EVT_MASK_END_AFTER_DISABLE |
  320. GAP_ADV_EVT_MASK_SET_TERMINATED,
  321. /// @cond NODOC
  322. /**
  323. * Used to set this to 16 bits for future events
  324. */
  325. GAP_ADV_EVT_MASK_RESERVED = BV(15)
  326. /// @endcond // NODOC
  327. } GapAdv_eventMaskFlags_t;
  328. /// Enable options for @ref GapAdv_enable
  329. typedef enum
  330. {
  331. /**
  332. * Use the maximum possible value. This is the spec-defined maximum for
  333. * directed advertising and infinite advertising for all other types
  334. */
  335. GAP_ADV_ENABLE_OPTIONS_USE_MAX,
  336. /**
  337. * Use the user-specified duration
  338. */
  339. GAP_ADV_ENABLE_OPTIONS_USE_DURATION,
  340. /**
  341. * Use the user-specified maximum number of events
  342. */
  343. GAP_ADV_ENABLE_OPTIONS_USE_MAX_EVENTS,
  344. } GapAdv_enableOptions_t;
  345. extern bStatus_t GapAdv_create(pfnGapCB_t *cb, Gap_ExtAdv_Param *advParam,
  346. uint8 *advHandle);
  347. bStatus_t GapAdv_loadByHandle(uint8 handle, GapAdv_dataTypes_t dataType,
  348. uint16 len, uint8 *pBuf);
  349. extern bStatus_t GapAdv_setEventMask(uint8 handle, GapAdv_eventMaskFlags_t mask);
  350. extern bStatus_t GapAdv_enable(uint8 handle,
  351. GapAdv_enableOptions_t enableOptions,
  352. uint16 durationOrMaxEvents);
  353. extern bStatus_t GapAdv_UpdateParameter(uint8 *pBuf);
  354. #endif
  355. #ifdef __cplusplus
  356. }
  357. #endif
  358. #endif /* PERIPHERAL_H */