broadcaster.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 "hci_tl.h"
  34. #include "gap.h"
  35. #include "broadcaster.h"
  36. /*********************************************************************
  37. * MACROS
  38. */
  39. /*********************************************************************
  40. * CONSTANTS
  41. */
  42. // Profile Events
  43. #define START_ADVERTISING_EVT 0x0001
  44. #define DEFAULT_ADVERT_OFF_TIME 30000 // 30 seconds
  45. /*********************************************************************
  46. * TYPEDEFS
  47. */
  48. /*********************************************************************
  49. * GLOBAL VARIABLES
  50. */
  51. /*********************************************************************
  52. * EXTERNAL VARIABLES
  53. */
  54. /*********************************************************************
  55. * EXTERNAL FUNCTIONS
  56. */
  57. /*********************************************************************
  58. * LOCAL VARIABLES
  59. */
  60. static uint8 gapRole_TaskID; // Task ID for internal task/event processing
  61. static gaprole_States_t gapRole_state;
  62. /*********************************************************************
  63. * Profile Parameters - reference GAPROLE_PROFILE_PARAMETERS for
  64. * descriptions
  65. */
  66. static uint8 gapRole_profileRole;
  67. static uint8 gapRole_bdAddr[B_ADDR_LEN];
  68. static uint8 gapRole_AdvEnabled = TRUE;
  69. static uint16 gapRole_AdvertOffTime = DEFAULT_ADVERT_OFF_TIME;
  70. static uint8 gapRole_AdvertDataLen = 3;
  71. static uint8 gapRole_AdvertData[B_MAX_ADV_LEN] =
  72. {
  73. 0x02, // length of this data
  74. GAP_ADTYPE_FLAGS, // AD Type = Flags
  75. // BR/EDR not supported
  76. GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
  77. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  78. };
  79. static uint8 gapRole_ScanRspDataLen = 0;
  80. static uint8 gapRole_ScanRspData[B_MAX_ADV_LEN] = {0};
  81. static uint8 gapRole_AdvEventType;
  82. static uint8 gapRole_AdvDirectType;
  83. static uint8 gapRole_AdvDirectAddr[B_ADDR_LEN] = {0};
  84. static uint8 gapRole_AdvChanMap;
  85. static uint8 gapRole_AdvFilterPolicy;
  86. // Application callbacks
  87. static gapRolesCBs_t *pGapRoles_AppCGs = NULL;
  88. /*********************************************************************
  89. * Profile Attributes - variables
  90. */
  91. /*********************************************************************
  92. * Profile Attributes - Table
  93. */
  94. /*********************************************************************
  95. * LOCAL FUNCTIONS
  96. */
  97. static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg );
  98. static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg );
  99. static void gapRole_SetupGAP( void );
  100. /*********************************************************************
  101. * NETWORK LAYER CALLBACKS
  102. */
  103. /*********************************************************************
  104. * PUBLIC FUNCTIONS
  105. */
  106. /*********************************************************************
  107. * @brief Set a GAP Role parameter.
  108. *
  109. * Public function defined in broadcaster.h.
  110. */
  111. bStatus_t GAPRole_SetParameter( uint16 param, uint8 len, void *pValue )
  112. {
  113. bStatus_t ret = SUCCESS;
  114. switch ( param )
  115. {
  116. case GAPROLE_ADVERT_ENABLED:
  117. if ( len == sizeof( uint8 ) )
  118. {
  119. uint8 oldAdvEnabled = gapRole_AdvEnabled;
  120. gapRole_AdvEnabled = *((uint8*)pValue);
  121. if ( (oldAdvEnabled) && (gapRole_AdvEnabled == FALSE) )
  122. {
  123. // Turn off Advertising
  124. if ( gapRole_state == GAPROLE_ADVERTISING )
  125. {
  126. VOID GAP_EndDiscoverable( gapRole_TaskID );
  127. }
  128. }
  129. else if ( (oldAdvEnabled == FALSE) && (gapRole_AdvEnabled) )
  130. {
  131. // Turn on Advertising
  132. if ( (gapRole_state == GAPROLE_STARTED)
  133. || (gapRole_state == GAPROLE_WAITING) )
  134. {
  135. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  136. }
  137. }
  138. }
  139. else
  140. {
  141. ret = bleInvalidRange;
  142. }
  143. break;
  144. case GAPROLE_ADVERT_OFF_TIME:
  145. if ( len == sizeof ( uint16 ) )
  146. {
  147. gapRole_AdvertOffTime = *((uint16*)pValue);
  148. }
  149. else
  150. {
  151. ret = bleInvalidRange;
  152. }
  153. break;
  154. case GAPROLE_ADVERT_DATA:
  155. if ( len <= B_MAX_ADV_LEN )
  156. {
  157. VOID osal_memset( gapRole_AdvertData, 0, B_MAX_ADV_LEN );
  158. VOID osal_memcpy( gapRole_AdvertData, pValue, len );
  159. gapRole_AdvertDataLen = len;
  160. }
  161. else
  162. {
  163. ret = bleInvalidRange;
  164. }
  165. break;
  166. case GAPROLE_SCAN_RSP_DATA:
  167. if ( len <= B_MAX_ADV_LEN )
  168. {
  169. VOID osal_memset( gapRole_ScanRspData, 0, B_MAX_ADV_LEN );
  170. VOID osal_memcpy( gapRole_ScanRspData, pValue, len );
  171. gapRole_ScanRspDataLen = len;
  172. }
  173. else
  174. {
  175. ret = bleInvalidRange;
  176. }
  177. break;
  178. case GAPROLE_ADV_EVENT_TYPE:
  179. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_ADTYPE_ADV_LDC_DIRECT_IND) )
  180. {
  181. gapRole_AdvEventType = *((uint8*)pValue);
  182. }
  183. else
  184. {
  185. ret = bleInvalidRange;
  186. }
  187. break;
  188. case GAPROLE_ADV_DIRECT_TYPE:
  189. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= ADDRTYPE_PRIVATE_RESOLVE) )
  190. {
  191. gapRole_AdvDirectType = *((uint8*)pValue);
  192. }
  193. else
  194. {
  195. ret = bleInvalidRange;
  196. }
  197. break;
  198. case GAPROLE_ADV_DIRECT_ADDR:
  199. if ( len == B_ADDR_LEN )
  200. {
  201. VOID osal_memcpy( gapRole_AdvDirectAddr, pValue, B_ADDR_LEN ) ;
  202. }
  203. else
  204. {
  205. ret = bleInvalidRange;
  206. }
  207. break;
  208. case GAPROLE_ADV_CHANNEL_MAP:
  209. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= 0x07) )
  210. {
  211. gapRole_AdvChanMap = *((uint8*)pValue);
  212. }
  213. else
  214. {
  215. ret = bleInvalidRange;
  216. }
  217. break;
  218. case GAPROLE_ADV_FILTER_POLICY:
  219. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_FILTER_POLICY_WHITE) )
  220. {
  221. gapRole_AdvFilterPolicy = *((uint8*)pValue);
  222. }
  223. else
  224. {
  225. ret = bleInvalidRange;
  226. }
  227. break;
  228. default:
  229. // The param value isn't part of this profile, try the GAP.
  230. if ( (param < TGAP_PARAMID_MAX) && (len == sizeof ( uint16 )) )
  231. {
  232. ret = GAP_SetParamValue( param, *((uint16*)pValue) );
  233. }
  234. else
  235. {
  236. ret = INVALIDPARAMETER;
  237. }
  238. break;
  239. }
  240. return ( ret );
  241. }
  242. /*********************************************************************
  243. * @brief Get a GAP Role parameter.
  244. *
  245. * Public function defined in broadcaster.h.
  246. */
  247. bStatus_t GAPRole_GetParameter( uint16 param, void *pValue )
  248. {
  249. bStatus_t ret = SUCCESS;
  250. switch ( param )
  251. {
  252. case GAPROLE_PROFILEROLE:
  253. *((uint8*)pValue) = gapRole_profileRole;
  254. break;
  255. case GAPROLE_BD_ADDR:
  256. VOID osal_memcpy( pValue, gapRole_bdAddr, B_ADDR_LEN ) ;
  257. break;
  258. case GAPROLE_ADVERT_ENABLED:
  259. *((uint8*)pValue) = gapRole_AdvEnabled;
  260. break;
  261. case GAPROLE_ADVERT_OFF_TIME:
  262. *((uint16*)pValue) = gapRole_AdvertOffTime;
  263. break;
  264. case GAPROLE_ADVERT_DATA:
  265. VOID osal_memcpy( pValue , gapRole_AdvertData, gapRole_AdvertDataLen );
  266. break;
  267. case GAPROLE_SCAN_RSP_DATA:
  268. VOID osal_memcpy( pValue, gapRole_ScanRspData, gapRole_ScanRspDataLen ) ;
  269. break;
  270. case GAPROLE_ADV_EVENT_TYPE:
  271. *((uint8*)pValue) = gapRole_AdvEventType;
  272. break;
  273. case GAPROLE_ADV_DIRECT_TYPE:
  274. *((uint8*)pValue) = gapRole_AdvDirectType;
  275. break;
  276. case GAPROLE_ADV_DIRECT_ADDR:
  277. VOID osal_memcpy( pValue, gapRole_AdvDirectAddr, B_ADDR_LEN ) ;
  278. break;
  279. case GAPROLE_ADV_CHANNEL_MAP:
  280. *((uint8*)pValue) = gapRole_AdvChanMap;
  281. break;
  282. case GAPROLE_ADV_FILTER_POLICY:
  283. *((uint8*)pValue) = gapRole_AdvFilterPolicy;
  284. break;
  285. default:
  286. // The param value isn't part of this profile, try the GAP.
  287. if ( param < TGAP_PARAMID_MAX )
  288. {
  289. *((uint16*)pValue) = GAP_GetParamValue( param );
  290. }
  291. else
  292. {
  293. ret = INVALIDPARAMETER;
  294. }
  295. break;
  296. }
  297. return ( ret );
  298. }
  299. /*********************************************************************
  300. * @brief Does the device initialization.
  301. *
  302. * Public function defined in broadcaster.h.
  303. */
  304. bStatus_t GAPRole_StartDevice( gapRolesCBs_t *pAppCallbacks )
  305. {
  306. if ( gapRole_state == GAPROLE_INIT )
  307. {
  308. // Clear all of the Application callbacks
  309. if ( pAppCallbacks )
  310. {
  311. pGapRoles_AppCGs = pAppCallbacks;
  312. }
  313. // Start the GAP
  314. gapRole_SetupGAP();
  315. return ( SUCCESS );
  316. }
  317. else
  318. {
  319. return ( bleAlreadyInRequestedMode );
  320. }
  321. }
  322. /*********************************************************************
  323. * LOCAL FUNCTION PROTOTYPES
  324. */
  325. /*********************************************************************
  326. * @brief Task Initialization function.
  327. *
  328. * Internal function defined in broadcaster.h.
  329. */
  330. void GAPRole_Init( uint8 task_id )
  331. {
  332. gapRole_TaskID = task_id;
  333. gapRole_state = GAPROLE_INIT;
  334. GAP_RegisterForHCIMsgs( gapRole_TaskID );
  335. // Initialize the Profile Advertising and Connection Parameters
  336. gapRole_profileRole = GAP_PROFILE_BROADCASTER;
  337. gapRole_AdvEventType = GAP_ADTYPE_ADV_NONCONN_IND;
  338. gapRole_AdvDirectType = ADDRTYPE_PUBLIC;
  339. gapRole_AdvChanMap = GAP_ADVCHAN_ALL;
  340. gapRole_AdvFilterPolicy = GAP_FILTER_POLICY_ALL;
  341. }
  342. /*********************************************************************
  343. * @brief Task Event Processor function.
  344. *
  345. * Internal function defined in broadcaster.h.
  346. */
  347. uint16 GAPRole_ProcessEvent( uint8 task_id, uint16 events )
  348. {
  349. VOID task_id; // OSAL required parameter that isn't used in this function
  350. if ( events & SYS_EVENT_MSG )
  351. {
  352. uint8 *pMsg;
  353. if ( (pMsg = osal_msg_receive( gapRole_TaskID )) != NULL )
  354. {
  355. gapRole_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
  356. // Release the OSAL message
  357. VOID osal_msg_deallocate( pMsg );
  358. }
  359. // return unprocessed events
  360. return (events ^ SYS_EVENT_MSG);
  361. }
  362. if ( events & START_ADVERTISING_EVT )
  363. {
  364. if ( gapRole_AdvEnabled )
  365. {
  366. gapAdvertisingParams_t params;
  367. // Setup advertisement parameters
  368. params.eventType = gapRole_AdvEventType;
  369. params.initiatorAddrType = gapRole_AdvDirectType;
  370. VOID osal_memcpy( params.initiatorAddr, gapRole_AdvDirectAddr, B_ADDR_LEN );
  371. params.channelMap = gapRole_AdvChanMap;
  372. params.filterPolicy = gapRole_AdvFilterPolicy;
  373. if ( GAP_MakeDiscoverable( gapRole_TaskID, &params ) != SUCCESS )
  374. {
  375. gapRole_state = GAPROLE_ERROR;
  376. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
  377. {
  378. pGapRoles_AppCGs->pfnStateChange( gapRole_state );
  379. }
  380. }
  381. }
  382. return ( events ^ START_ADVERTISING_EVT );
  383. }
  384. // Discard unknown events
  385. return 0;
  386. }
  387. /*********************************************************************
  388. * @fn gapRole_ProcessOSALMsg
  389. *
  390. * @brief Process an incoming task message.
  391. *
  392. * @param pMsg - message to process
  393. *
  394. * @return none
  395. */
  396. static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg )
  397. {
  398. switch ( pMsg->event )
  399. {
  400. case HCI_GAP_EVENT_EVENT:
  401. if ( pMsg->status == HCI_COMMAND_COMPLETE_EVENT_CODE )
  402. {
  403. //hciEvt_CmdComplete_t *pPkt = (hciEvt_CmdComplete_t *)pMsg;
  404. }
  405. break;
  406. case GAP_MSG_EVENT:
  407. gapRole_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
  408. break;
  409. default:
  410. break;
  411. }
  412. }
  413. /*********************************************************************
  414. * @fn gapRole_ProcessGAPMsg
  415. *
  416. * @brief Process an incoming task message.
  417. *
  418. * @param pMsg - message to process
  419. *
  420. * @return none
  421. */
  422. static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg )
  423. {
  424. uint8 notify = FALSE; // State changed notify the app? (default no)
  425. switch ( pMsg->opcode )
  426. {
  427. case GAP_DEVICE_INIT_DONE_EVENT:
  428. {
  429. gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *)pMsg;
  430. bStatus_t stat = pPkt->hdr.status;
  431. if ( stat == SUCCESS )
  432. {
  433. // Save off the information
  434. VOID osal_memcpy( gapRole_bdAddr, pPkt->devAddr, B_ADDR_LEN );
  435. gapRole_state = GAPROLE_STARTED;
  436. // Update the advertising data
  437. stat = GAP_UpdateAdvertisingData( gapRole_TaskID, TRUE,
  438. gapRole_AdvertDataLen,
  439. gapRole_AdvertData );
  440. }
  441. if ( stat != SUCCESS )
  442. {
  443. gapRole_state = GAPROLE_ERROR;
  444. }
  445. notify = TRUE;
  446. }
  447. break;
  448. case GAP_ADV_DATA_UPDATE_DONE_EVENT:
  449. {
  450. gapAdvDataUpdateEvent_t *pPkt = (gapAdvDataUpdateEvent_t *)pMsg;
  451. if ( pPkt->hdr.status == SUCCESS )
  452. {
  453. if ( pPkt->adType )
  454. {
  455. // Setup the Response Data
  456. pPkt->hdr.status = GAP_UpdateAdvertisingData( gapRole_TaskID,
  457. FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
  458. }
  459. else
  460. {
  461. // Start advertising
  462. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  463. }
  464. }
  465. if ( pPkt->hdr.status != SUCCESS )
  466. {
  467. // Set into Error state
  468. gapRole_state = GAPROLE_ERROR;
  469. notify = TRUE;
  470. }
  471. }
  472. break;
  473. case GAP_MAKE_DISCOVERABLE_DONE_EVENT:
  474. case GAP_END_DISCOVERABLE_DONE_EVENT:
  475. {
  476. gapMakeDiscoverableRspEvent_t *pPkt = (gapMakeDiscoverableRspEvent_t *)pMsg;
  477. if ( pPkt->hdr.status == SUCCESS )
  478. {
  479. if ( pMsg->opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT )
  480. {
  481. gapRole_state = GAPROLE_ADVERTISING;
  482. }
  483. else // GAP_END_DISCOVERABLE_DONE_EVENT
  484. {
  485. if ( gapRole_AdvertOffTime != 0 )
  486. {
  487. if ( ( gapRole_AdvEnabled ) )
  488. {
  489. VOID osal_start_timerEx( gapRole_TaskID, START_ADVERTISING_EVT, gapRole_AdvertOffTime );
  490. }
  491. }
  492. else
  493. {
  494. // Since gapRole_AdvertOffTime is set to 0, the device should not
  495. // automatically become discoverable again after a period of time.
  496. // Set enabler to FALSE; device will become discoverable again when
  497. // this value gets set to TRUE
  498. gapRole_AdvEnabled = FALSE;
  499. }
  500. // In the Advertising Off period
  501. gapRole_state = GAPROLE_WAITING;
  502. }
  503. }
  504. else
  505. {
  506. gapRole_state = GAPROLE_ERROR;
  507. }
  508. notify = TRUE;
  509. }
  510. break;
  511. default:
  512. break;
  513. }
  514. if ( notify == TRUE )
  515. {
  516. // Notify the application
  517. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
  518. {
  519. pGapRoles_AppCGs->pfnStateChange( gapRole_state );
  520. }
  521. }
  522. }
  523. /*********************************************************************
  524. * @fn gapRole_SetupGAP
  525. *
  526. * @brief Call the GAP Device Initialization function using the
  527. * Profile Parameters.
  528. *
  529. * @param none
  530. *
  531. * @return none
  532. */
  533. static void gapRole_SetupGAP( void )
  534. {
  535. VOID GAP_DeviceInit( gapRole_TaskID,
  536. gapRole_profileRole, 0,
  537. NULL, NULL, NULL );
  538. }
  539. /*********************************************************************
  540. *********************************************************************/