observer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. * INCLUDES
  12. */
  13. #include "bcomdef.h"
  14. #include "OSAL.h"
  15. #include "osal_cbTimer.h"
  16. #include "osal_snv.h"
  17. #include "hci_tl.h"
  18. #include "gap.h"
  19. #include "observer.h"
  20. /*********************************************************************
  21. * MACROS
  22. */
  23. /*********************************************************************
  24. * CONSTANTS
  25. */
  26. /*********************************************************************
  27. * TYPEDEFS
  28. */
  29. /*********************************************************************
  30. * GLOBAL VARIABLES
  31. */
  32. /*********************************************************************
  33. * EXTERNAL VARIABLES
  34. */
  35. /*********************************************************************
  36. * EXTERNAL FUNCTIONS
  37. */
  38. /*********************************************************************
  39. * LOCAL VARIABLES
  40. */
  41. // Task ID
  42. static uint8 gapObserverRoleTaskId;
  43. // App callbacks
  44. static gapObserverRoleCB_t *pGapObserverRoleCB;
  45. /*********************************************************************
  46. * Profile Parameters - reference GAPCENTRALROLE_PROFILE_PARAMETERS for
  47. * descriptions
  48. */
  49. static uint8 gapObserverRoleBdAddr[B_ADDR_LEN];
  50. static uint8 gapObserverRoleMaxScanRes = 0;
  51. /*********************************************************************
  52. * LOCAL FUNCTIONS
  53. */
  54. static void gapObserverRole_ProcessOSALMsg( osal_event_hdr_t *pMsg );
  55. static void gapObserverRole_ProcessGAPMsg( gapEventHdr_t *pMsg );
  56. /*********************************************************************
  57. * PUBLIC FUNCTIONS
  58. */
  59. /**
  60. * @brief Start the device in Observer role. This function is typically
  61. * called once during system startup.
  62. *
  63. * Public function defined in observer.h.
  64. */
  65. bStatus_t GAPObserverRole_StartDevice( gapObserverRoleCB_t *pAppCallbacks )
  66. {
  67. if ( pAppCallbacks )
  68. {
  69. pGapObserverRoleCB = pAppCallbacks;
  70. }
  71. return GAP_DeviceInit( gapObserverRoleTaskId, GAP_PROFILE_OBSERVER,
  72. gapObserverRoleMaxScanRes, NULL, NULL, NULL );
  73. }
  74. /**
  75. * @brief Set a parameter in the Observer Profile.
  76. *
  77. * Public function defined in observer.h.
  78. */
  79. bStatus_t GAPObserverRole_SetParameter( uint16 param, uint8 len, void *pValue )
  80. {
  81. bStatus_t ret = SUCCESS;
  82. switch ( param )
  83. {
  84. case GAPOBSERVERROLE_MAX_SCAN_RES:
  85. if ( len == sizeof ( uint8 ) )
  86. {
  87. gapObserverRoleMaxScanRes = *((uint8*)pValue);
  88. }
  89. else
  90. {
  91. ret = bleInvalidRange;
  92. }
  93. break;
  94. default:
  95. ret = INVALIDPARAMETER;
  96. break;
  97. }
  98. return ret;
  99. }
  100. /**
  101. * @brief Get a parameter in the Observer Profile.
  102. *
  103. * Public function defined in observer.h.
  104. */
  105. bStatus_t GAPObserverRole_GetParameter( uint16 param, void *pValue )
  106. {
  107. bStatus_t ret = SUCCESS;
  108. switch ( param )
  109. {
  110. case GAPOBSERVERROLE_BD_ADDR:
  111. VOID osal_memcpy( pValue, gapObserverRoleBdAddr, B_ADDR_LEN ) ;
  112. break;
  113. case GAPOBSERVERROLE_MAX_SCAN_RES:
  114. *((uint8*)pValue) = gapObserverRoleMaxScanRes;
  115. break;
  116. default:
  117. ret = INVALIDPARAMETER;
  118. break;
  119. }
  120. return ret;
  121. }
  122. /**
  123. * @brief Start a device discovery scan.
  124. *
  125. * Public function defined in observer.h.
  126. */
  127. bStatus_t GAPObserverRole_StartDiscovery( uint8 mode, uint8 activeScan, uint8 whiteList )
  128. {
  129. gapDevDiscReq_t params;
  130. params.taskID = gapObserverRoleTaskId;
  131. params.mode = mode;
  132. params.activeScan = activeScan;
  133. params.whiteList = whiteList;
  134. return GAP_DeviceDiscoveryRequest( &params );
  135. }
  136. /**
  137. * @brief Cancel a device discovery scan.
  138. *
  139. * Public function defined in observer.h.
  140. */
  141. bStatus_t GAPObserverRole_CancelDiscovery( void )
  142. {
  143. return GAP_DeviceDiscoveryCancel( gapObserverRoleTaskId );
  144. }
  145. /**
  146. * @brief Observer Profile Task initialization function.
  147. *
  148. * @param taskId - Task ID.
  149. *
  150. * @return void
  151. */
  152. void GAPObserverRole_Init( uint8 taskId )
  153. {
  154. gapObserverRoleTaskId = taskId;
  155. // Register for HCI messages (for RSSI)
  156. GAP_RegisterForHCIMsgs( taskId );
  157. }
  158. /**
  159. * @brief Observer Profile Task event processing function.
  160. *
  161. * @param taskId - Task ID
  162. * @param events - Events.
  163. *
  164. * @return events not processed
  165. */
  166. uint16 GAPObserverRole_ProcessEvent( uint8 taskId, uint16 events )
  167. {
  168. if ( events & SYS_EVENT_MSG )
  169. {
  170. uint8 *pMsg;
  171. if ( (pMsg = osal_msg_receive( gapObserverRoleTaskId )) != NULL )
  172. {
  173. gapObserverRole_ProcessOSALMsg( (osal_event_hdr_t *) pMsg );
  174. // Release the OSAL message
  175. VOID osal_msg_deallocate( pMsg );
  176. }
  177. // return unprocessed events
  178. return (events ^ SYS_EVENT_MSG);
  179. }
  180. // Discard unknown events
  181. return 0;
  182. }
  183. /*********************************************************************
  184. * @fn gapObserverRole_ProcessOSALMsg
  185. *
  186. * @brief Process an incoming task message.
  187. *
  188. * @param pMsg - message to process
  189. *
  190. * @return none
  191. */
  192. static void gapObserverRole_ProcessOSALMsg( osal_event_hdr_t *pMsg )
  193. {
  194. switch ( pMsg->event )
  195. {
  196. case HCI_GAP_EVENT_EVENT:
  197. if ( pMsg->status == HCI_COMMAND_COMPLETE_EVENT_CODE )
  198. {
  199. //hciEvt_CmdComplete_t *pPkt = (hciEvt_CmdComplete_t *) pMsg;
  200. }
  201. break;
  202. case GAP_MSG_EVENT:
  203. gapObserverRole_ProcessGAPMsg( (gapEventHdr_t *) pMsg );
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. /*********************************************************************
  210. * @fn gapObserverRole_ProcessGAPMsg
  211. *
  212. * @brief Process an incoming task message from GAP.
  213. *
  214. * @param pMsg - message to process
  215. *
  216. * @return none
  217. */
  218. static void gapObserverRole_ProcessGAPMsg( gapEventHdr_t *pMsg )
  219. {
  220. switch ( pMsg->opcode )
  221. {
  222. case GAP_DEVICE_INIT_DONE_EVENT:
  223. {
  224. gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *) pMsg;
  225. if ( pPkt->hdr.status == SUCCESS )
  226. {
  227. // Save off the information
  228. VOID osal_memcpy( gapObserverRoleBdAddr, pPkt->devAddr, B_ADDR_LEN );
  229. }
  230. }
  231. break;
  232. default:
  233. break;
  234. }
  235. // Pass event to app
  236. if ( pGapObserverRoleCB && pGapObserverRoleCB->eventCB )
  237. {
  238. pGapObserverRoleCB->eventCB( (gapObserverRoleEvent_t *) pMsg );
  239. }
  240. }
  241. /*********************************************************************
  242. *********************************************************************/