observer.c 8.1 KB

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