peripheral.h 18 KB

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