peripheralBroadcaster.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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.h"
  34. #include "l2cap.h"
  35. #include "gap.h"
  36. #include "linkdb.h"
  37. #include "att.h"
  38. #include "gatt.h"
  39. #include "osal_snv.h"
  40. #include "hci_tl.h"
  41. #include "peripheralBroadcaster.h"
  42. #include "gapbondmgr.h"
  43. /*********************************************************************
  44. * MACROS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. // Profile Events
  50. #define START_ADVERTISING_EVT 0x0001
  51. #define RSSI_READ_EVT 0x0002
  52. #define UPDATE_PARAMS_TIMEOUT_EVT 0x0004
  53. #define DEFAULT_ADVERT_OFF_TIME 30000 // 30 seconds
  54. #define RSSI_NOT_AVAILABLE 127
  55. #define DEFAULT_MIN_CONN_INTERVAL 0x0006 // 100 milliseconds
  56. #define DEFAULT_MAX_CONN_INTERVAL 0x0C80 // 4 seconds
  57. #define MIN_CONN_INTERVAL 0x0006
  58. #define MAX_CONN_INTERVAL 0x0C80
  59. #define DEFAULT_SLAVE_LATENCY 0
  60. #define DEFAULT_TIMEOUT_MULTIPLIER 1000
  61. #define CONN_INTERVAL_MULTIPLIER 6
  62. #define MAX_SLAVE_LATENCY 500
  63. #define MIN_TIMEOUT_MULTIPLIER 0x000a
  64. #define MAX_TIMEOUT_MULTIPLIER 0x0c80
  65. #define MAX_TIMEOUT_VALUE 0xFFFF
  66. /*********************************************************************
  67. * TYPEDEFS
  68. */
  69. /*********************************************************************
  70. * GLOBAL VARIABLES
  71. */
  72. /*********************************************************************
  73. * EXTERNAL VARIABLES
  74. */
  75. /*********************************************************************
  76. * EXTERNAL FUNCTIONS
  77. */
  78. /*********************************************************************
  79. * LOCAL VARIABLES
  80. */
  81. static uint8 gapRole_TaskID; // Task ID for internal task/event processing
  82. static gaprole_States_t gapRole_state;
  83. /*********************************************************************
  84. * Profile Parameters - reference GAPROLE_PROFILE_PARAMETERS for
  85. * descriptions
  86. */
  87. static uint8 gapRole_profileRole;
  88. static uint8 gapRole_IRK[KEYLEN];
  89. static uint8 gapRole_SRK[KEYLEN];
  90. static uint32 gapRole_signCounter;
  91. static uint8 gapRole_bdAddr[B_ADDR_LEN];
  92. static uint8 gapRole_AdvEnabled = TRUE;
  93. static uint16 gapRole_AdvertOffTime = DEFAULT_ADVERT_OFF_TIME;
  94. static uint8 gapRole_AdvertDataLen = 3;
  95. static uint8 gapRole_AdvertData[B_MAX_ADV_LEN] =
  96. {
  97. 0x02, // length of this data
  98. GAP_ADTYPE_FLAGS, // AD Type = Flags
  99. // Limited Discoverable & BR/EDR not supported
  100. (GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED),
  101. 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
  102. };
  103. static uint8 gapRole_ScanRspDataLen = 0;
  104. static uint8 gapRole_ScanRspData[B_MAX_ADV_LEN] = {0};
  105. static uint8 gapRole_AdvEventType;
  106. static uint8 gapRole_AdvDirectType;
  107. static uint8 gapRole_AdvDirectAddr[B_ADDR_LEN] = {0};
  108. static uint8 gapRole_AdvChanMap;
  109. static uint8 gapRole_AdvFilterPolicy;
  110. static uint16 gapRole_ConnectionHandle = INVALID_CONNHANDLE;
  111. static uint16 gapRole_RSSIReadRate = 0;
  112. static gapRolesCBs_t *pGapRoles_AppCGs = NULL;
  113. static uint8 gapRole_ConnectedDevAddr[B_ADDR_LEN] = {0};
  114. static uint8 gapRole_ParamUpdateEnable = FALSE;
  115. static uint16 gapRole_MinConnInterval = DEFAULT_MIN_CONN_INTERVAL;
  116. static uint16 gapRole_MaxConnInterval = DEFAULT_MAX_CONN_INTERVAL;
  117. static uint16 gapRole_SlaveLatency = DEFAULT_SLAVE_LATENCY;
  118. static uint16 gapRole_TimeoutMultiplier = DEFAULT_TIMEOUT_MULTIPLIER;
  119. /*********************************************************************
  120. * Profile Attributes - variables
  121. */
  122. /*********************************************************************
  123. * Profile Attributes - Table
  124. */
  125. /*********************************************************************
  126. * LOCAL FUNCTIONS
  127. */
  128. static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg );
  129. static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg );
  130. static void gapRole_SetupGAP( void );
  131. static void gapRole_SendUpdateParam( uint16 connInterval, uint16 connLatency );
  132. /*********************************************************************
  133. * NETWORK LAYER CALLBACKS
  134. */
  135. /*********************************************************************
  136. * PUBLIC FUNCTIONS
  137. */
  138. /*********************************************************************
  139. * @brief Set a GAP Role parameter.
  140. *
  141. * Public function defined in peripheral.h.
  142. */
  143. bStatus_t GAPRole_SetParameter( uint16 param, uint8 len, void *pValue )
  144. {
  145. bStatus_t ret = SUCCESS;
  146. switch ( param )
  147. {
  148. case GAPROLE_IRK:
  149. if ( len == KEYLEN )
  150. {
  151. VOID osal_memcpy( gapRole_IRK, pValue, KEYLEN ) ;
  152. }
  153. else
  154. {
  155. ret = bleInvalidRange;
  156. }
  157. break;
  158. case GAPROLE_SRK:
  159. if ( len == KEYLEN )
  160. {
  161. VOID osal_memcpy( gapRole_SRK, pValue, KEYLEN ) ;
  162. }
  163. else
  164. {
  165. ret = bleInvalidRange;
  166. }
  167. break;
  168. case GAPROLE_SIGNCOUNTER:
  169. if ( len == sizeof ( uint32 ) )
  170. {
  171. gapRole_signCounter = *((uint32*)pValue);
  172. }
  173. else
  174. {
  175. ret = bleInvalidRange;
  176. }
  177. break;
  178. case GAPROLE_ADVERT_ENABLED:
  179. if ( len == sizeof( uint8 ) )
  180. {
  181. if ( (gapRole_state == GAPROLE_CONNECTED) || (gapRole_state == GAPROLE_CONNECTED_ADV) )
  182. {
  183. uint8 advEnabled = *((uint8*)pValue);
  184. if ( (gapRole_state == GAPROLE_CONNECTED) && (advEnabled == TRUE) )
  185. {
  186. // Turn on advertising
  187. osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  188. }
  189. else if ( (gapRole_state == GAPROLE_CONNECTED_ADV) && (advEnabled == FALSE) )
  190. {
  191. // Turn off Advertising
  192. GAP_EndDiscoverable( gapRole_TaskID );
  193. }
  194. }
  195. else
  196. {
  197. uint8 oldAdvEnabled = gapRole_AdvEnabled;
  198. gapRole_AdvEnabled = *((uint8*)pValue);
  199. if ( (oldAdvEnabled) && (gapRole_AdvEnabled == FALSE) )
  200. {
  201. // Turn off Advertising
  202. VOID GAP_EndDiscoverable( gapRole_TaskID );
  203. }
  204. else if ( (oldAdvEnabled == FALSE) && (gapRole_AdvEnabled) )
  205. {
  206. // Turn on Advertising
  207. if ( (gapRole_state == GAPROLE_STARTED)
  208. || (gapRole_state == GAPROLE_WAITING )
  209. || (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT) )
  210. {
  211. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  212. }
  213. }
  214. }
  215. }
  216. else
  217. {
  218. ret = bleInvalidRange;
  219. }
  220. break;
  221. case GAPROLE_ADVERT_OFF_TIME:
  222. if ( len == sizeof ( uint16 ) )
  223. {
  224. gapRole_AdvertOffTime = *((uint16*)pValue);
  225. }
  226. else
  227. {
  228. ret = bleInvalidRange;
  229. }
  230. break;
  231. case GAPROLE_ADVERT_DATA:
  232. if ( len <= B_MAX_ADV_LEN )
  233. {
  234. VOID osal_memset( gapRole_AdvertData, 0, B_MAX_ADV_LEN );
  235. VOID osal_memcpy( gapRole_AdvertData, pValue, len );
  236. gapRole_AdvertDataLen = len;
  237. }
  238. else
  239. {
  240. ret = bleInvalidRange;
  241. }
  242. break;
  243. case GAPROLE_SCAN_RSP_DATA:
  244. if ( len <= B_MAX_ADV_LEN )
  245. {
  246. VOID osal_memset( gapRole_ScanRspData, 0, B_MAX_ADV_LEN );
  247. VOID osal_memcpy( gapRole_ScanRspData, pValue, len );
  248. gapRole_ScanRspDataLen = len;
  249. }
  250. else
  251. {
  252. ret = bleInvalidRange;
  253. }
  254. break;
  255. case GAPROLE_ADV_EVENT_TYPE:
  256. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_ADTYPE_ADV_LDC_DIRECT_IND) )
  257. {
  258. gapRole_AdvEventType = *((uint8*)pValue);
  259. }
  260. else
  261. {
  262. ret = bleInvalidRange;
  263. }
  264. break;
  265. case GAPROLE_ADV_DIRECT_TYPE:
  266. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= ADDRTYPE_PRIVATE_RESOLVE) )
  267. {
  268. gapRole_AdvDirectType = *((uint8*)pValue);
  269. }
  270. else
  271. {
  272. ret = bleInvalidRange;
  273. }
  274. break;
  275. case GAPROLE_ADV_DIRECT_ADDR:
  276. if ( len == B_ADDR_LEN )
  277. {
  278. VOID osal_memcpy( gapRole_AdvDirectAddr, pValue, B_ADDR_LEN ) ;
  279. }
  280. else
  281. {
  282. ret = bleInvalidRange;
  283. }
  284. break;
  285. case GAPROLE_ADV_CHANNEL_MAP:
  286. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= 0x07) )
  287. {
  288. gapRole_AdvChanMap = *((uint8*)pValue);
  289. }
  290. else
  291. {
  292. ret = bleInvalidRange;
  293. }
  294. break;
  295. case GAPROLE_ADV_FILTER_POLICY:
  296. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_FILTER_POLICY_WHITE) )
  297. {
  298. gapRole_AdvFilterPolicy = *((uint8*)pValue);
  299. }
  300. else
  301. {
  302. ret = bleInvalidRange;
  303. }
  304. break;
  305. case GAPROLE_RSSI_READ_RATE:
  306. if ( len == sizeof ( uint16 ) )
  307. {
  308. gapRole_RSSIReadRate = *((uint16*)pValue);
  309. if ( (gapRole_RSSIReadRate) && (gapRole_state == GAPROLE_CONNECTED) )
  310. {
  311. // Start the RSSI Reads
  312. VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
  313. }
  314. }
  315. else
  316. {
  317. ret = bleInvalidRange;
  318. }
  319. break;
  320. case GAPROLE_PARAM_UPDATE_ENABLE:
  321. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
  322. {
  323. gapRole_ParamUpdateEnable = *((uint8*)pValue);
  324. }
  325. else
  326. {
  327. ret = bleInvalidRange;
  328. }
  329. break;
  330. case GAPROLE_MIN_CONN_INTERVAL:
  331. {
  332. uint16 newInterval = *((uint16*)pValue);
  333. if ( len == sizeof ( uint16 ) &&
  334. ( newInterval >= MIN_CONN_INTERVAL ) &&
  335. ( newInterval <= MAX_CONN_INTERVAL ) )
  336. {
  337. gapRole_MinConnInterval = newInterval;
  338. }
  339. else
  340. {
  341. ret = bleInvalidRange;
  342. }
  343. }
  344. break;
  345. case GAPROLE_MAX_CONN_INTERVAL:
  346. {
  347. uint16 newInterval = *((uint16*)pValue);
  348. if ( len == sizeof ( uint16 ) &&
  349. ( newInterval >= MIN_CONN_INTERVAL) &&
  350. ( newInterval <= MAX_CONN_INTERVAL) )
  351. {
  352. gapRole_MaxConnInterval = newInterval;
  353. }
  354. else
  355. {
  356. ret = bleInvalidRange;
  357. }
  358. }
  359. break;
  360. case GAPROLE_SLAVE_LATENCY:
  361. {
  362. uint16 latency = *((uint16*)pValue);
  363. if ( len == sizeof ( uint16 ) && (latency < MAX_SLAVE_LATENCY) )
  364. {
  365. gapRole_SlaveLatency = latency;
  366. }
  367. else
  368. {
  369. ret = bleInvalidRange;
  370. }
  371. }
  372. break;
  373. case GAPROLE_TIMEOUT_MULTIPLIER:
  374. {
  375. uint16 newTimeout = *((uint16*)pValue);
  376. if ( len == sizeof ( uint16 )
  377. && (newTimeout >= MIN_TIMEOUT_MULTIPLIER) && (newTimeout <= MAX_TIMEOUT_MULTIPLIER) )
  378. {
  379. gapRole_TimeoutMultiplier = newTimeout;
  380. }
  381. else
  382. {
  383. ret = bleInvalidRange;
  384. }
  385. }
  386. break;
  387. default:
  388. // The param value isn't part of this profile, try the GAP.
  389. if ( (param < TGAP_PARAMID_MAX) && (len == sizeof ( uint16 )) )
  390. {
  391. ret = GAP_SetParamValue( param, *((uint16*)pValue) );
  392. }
  393. else
  394. {
  395. ret = INVALIDPARAMETER;
  396. }
  397. break;
  398. }
  399. return ( ret );
  400. }
  401. /*********************************************************************
  402. * @brief Get a GAP Role parameter.
  403. *
  404. * Public function defined in peripheral.h.
  405. */
  406. bStatus_t GAPRole_GetParameter( uint16 param, void *pValue )
  407. {
  408. bStatus_t ret = SUCCESS;
  409. switch ( param )
  410. {
  411. case GAPROLE_PROFILEROLE:
  412. *((uint8*)pValue) = gapRole_profileRole;
  413. break;
  414. case GAPROLE_IRK:
  415. VOID osal_memcpy( pValue, gapRole_IRK, KEYLEN ) ;
  416. break;
  417. case GAPROLE_SRK:
  418. VOID osal_memcpy( pValue, gapRole_SRK, KEYLEN ) ;
  419. break;
  420. case GAPROLE_SIGNCOUNTER:
  421. *((uint32*)pValue) = gapRole_signCounter;
  422. break;
  423. case GAPROLE_BD_ADDR:
  424. VOID osal_memcpy( pValue, gapRole_bdAddr, B_ADDR_LEN ) ;
  425. break;
  426. case GAPROLE_ADVERT_ENABLED:
  427. *((uint8*)pValue) = gapRole_AdvEnabled;
  428. break;
  429. case GAPROLE_ADVERT_OFF_TIME:
  430. *((uint16*)pValue) = gapRole_AdvertOffTime;
  431. break;
  432. case GAPROLE_ADVERT_DATA:
  433. VOID osal_memcpy( pValue , gapRole_AdvertData, gapRole_AdvertDataLen );
  434. break;
  435. case GAPROLE_SCAN_RSP_DATA:
  436. VOID osal_memcpy( pValue, gapRole_ScanRspData, gapRole_ScanRspDataLen ) ;
  437. break;
  438. case GAPROLE_ADV_EVENT_TYPE:
  439. *((uint8*)pValue) = gapRole_AdvEventType;
  440. break;
  441. case GAPROLE_ADV_DIRECT_TYPE:
  442. *((uint8*)pValue) = gapRole_AdvDirectType;
  443. break;
  444. case GAPROLE_ADV_DIRECT_ADDR:
  445. VOID osal_memcpy( pValue, gapRole_AdvDirectAddr, B_ADDR_LEN ) ;
  446. break;
  447. case GAPROLE_ADV_CHANNEL_MAP:
  448. *((uint8*)pValue) = gapRole_AdvChanMap;
  449. break;
  450. case GAPROLE_ADV_FILTER_POLICY:
  451. *((uint8*)pValue) = gapRole_AdvFilterPolicy;
  452. break;
  453. case GAPROLE_CONNHANDLE:
  454. *((uint16*)pValue) = gapRole_ConnectionHandle;
  455. break;
  456. case GAPROLE_RSSI_READ_RATE:
  457. *((uint16*)pValue) = gapRole_RSSIReadRate;
  458. break;
  459. case GAPROLE_PARAM_UPDATE_ENABLE:
  460. *((uint16*)pValue) = gapRole_ParamUpdateEnable;
  461. break;
  462. case GAPROLE_MIN_CONN_INTERVAL:
  463. *((uint16*)pValue) = gapRole_MinConnInterval;
  464. break;
  465. case GAPROLE_MAX_CONN_INTERVAL:
  466. *((uint16*)pValue) = gapRole_MaxConnInterval;
  467. break;
  468. case GAPROLE_SLAVE_LATENCY:
  469. *((uint16*)pValue) = gapRole_SlaveLatency;
  470. break;
  471. case GAPROLE_TIMEOUT_MULTIPLIER:
  472. *((uint16*)pValue) = gapRole_TimeoutMultiplier;
  473. break;
  474. case GAPROLE_CONN_BD_ADDR:
  475. VOID osal_memcpy( pValue, gapRole_ConnectedDevAddr, B_ADDR_LEN ) ;
  476. break;
  477. default:
  478. // The param value isn't part of this profile, try the GAP.
  479. if ( param < TGAP_PARAMID_MAX )
  480. {
  481. *((uint16*)pValue) = GAP_GetParamValue( param );
  482. }
  483. else
  484. {
  485. ret = INVALIDPARAMETER;
  486. }
  487. break;
  488. }
  489. return ( ret );
  490. }
  491. /*********************************************************************
  492. * @brief Does the device initialization.
  493. *
  494. * Public function defined in peripheral.h.
  495. */
  496. bStatus_t GAPRole_StartDevice( gapRolesCBs_t *pAppCallbacks )
  497. {
  498. if ( gapRole_state == GAPROLE_INIT )
  499. {
  500. // Clear all of the Application callbacks
  501. if ( pAppCallbacks )
  502. {
  503. pGapRoles_AppCGs = pAppCallbacks;
  504. }
  505. // Start the GAP
  506. gapRole_SetupGAP();
  507. return ( SUCCESS );
  508. }
  509. else
  510. {
  511. return ( bleAlreadyInRequestedMode );
  512. }
  513. }
  514. /*********************************************************************
  515. * @brief Terminates the existing connection.
  516. *
  517. * Public function defined in peripheral.h.
  518. */
  519. bStatus_t GAPRole_TerminateConnection( void )
  520. {
  521. if ( (gapRole_state == GAPROLE_CONNECTED)
  522. || (gapRole_state == GAPROLE_CONNECTED_ADV) )
  523. {
  524. return ( GAP_TerminateLinkReq( gapRole_TaskID, gapRole_ConnectionHandle,
  525. HCI_DISCONNECT_REMOTE_USER_TERM ) );
  526. }
  527. else
  528. {
  529. return ( bleIncorrectMode );
  530. }
  531. }
  532. /*********************************************************************
  533. * LOCAL FUNCTION PROTOTYPES
  534. */
  535. /*********************************************************************
  536. * @brief Task Initialization function.
  537. *
  538. * Internal function defined in peripheral.h.
  539. */
  540. void GAPRole_Init( uint8 task_id )
  541. {
  542. gapRole_TaskID = task_id;
  543. gapRole_state = GAPROLE_INIT;
  544. gapRole_ConnectionHandle = INVALID_CONNHANDLE;
  545. GAP_RegisterForHCIMsgs( gapRole_TaskID );
  546. // Initialize the Profile Advertising and Connection Parameters
  547. gapRole_profileRole = (GAP_PROFILE_PERIPHERAL | GAP_PROFILE_BROADCASTER);
  548. VOID osal_memset( gapRole_IRK, 0, KEYLEN );
  549. VOID osal_memset( gapRole_SRK, 0, KEYLEN );
  550. gapRole_signCounter = 0;
  551. gapRole_AdvEventType = GAP_ADTYPE_ADV_IND;
  552. gapRole_AdvDirectType = ADDRTYPE_PUBLIC;
  553. gapRole_AdvChanMap = GAP_ADVCHAN_ALL;
  554. gapRole_AdvFilterPolicy = GAP_FILTER_POLICY_ALL;
  555. // Restore Items from NV
  556. VOID osal_snv_read( BLE_NVID_IRK, KEYLEN, gapRole_IRK );
  557. VOID osal_snv_read( BLE_NVID_CSRK, KEYLEN, gapRole_SRK );
  558. VOID osal_snv_read( BLE_NVID_SIGNCOUNTER, sizeof( uint32 ), &gapRole_signCounter );
  559. }
  560. /*********************************************************************
  561. * @brief Task Event Processor function.
  562. *
  563. * Internal function defined in peripheral.h.
  564. */
  565. uint16 GAPRole_ProcessEvent( uint8 task_id, uint16 events )
  566. {
  567. VOID task_id; // OSAL required parameter that isn't used in this function
  568. if ( events & SYS_EVENT_MSG )
  569. {
  570. uint8 *pMsg;
  571. if ( (pMsg = osal_msg_receive( gapRole_TaskID )) != NULL )
  572. {
  573. gapRole_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
  574. // Release the OSAL message
  575. VOID osal_msg_deallocate( pMsg );
  576. }
  577. // return unprocessed events
  578. return (events ^ SYS_EVENT_MSG);
  579. }
  580. if ( events & GAP_EVENT_SIGN_COUNTER_CHANGED )
  581. {
  582. // Sign counter changed, save it to NV
  583. VOID osal_snv_write( BLE_NVID_SIGNCOUNTER, sizeof( uint32 ), &gapRole_signCounter );
  584. return ( events ^ GAP_EVENT_SIGN_COUNTER_CHANGED );
  585. }
  586. if ( events & START_ADVERTISING_EVT )
  587. {
  588. if ( gapRole_AdvEnabled )
  589. {
  590. gapAdvertisingParams_t params;
  591. // Setup advertisement parameters
  592. if ( gapRole_state == GAPROLE_CONNECTED )
  593. {
  594. // While in a connection, we can only advertise non-connectable undirected.
  595. params.eventType = GAP_ADTYPE_ADV_NONCONN_IND;
  596. }
  597. else
  598. {
  599. params.eventType = gapRole_AdvEventType;
  600. params.initiatorAddrType = gapRole_AdvDirectType;
  601. VOID osal_memcpy( params.initiatorAddr, gapRole_AdvDirectAddr, B_ADDR_LEN );
  602. }
  603. params.channelMap = gapRole_AdvChanMap;
  604. params.filterPolicy = gapRole_AdvFilterPolicy;
  605. if ( GAP_MakeDiscoverable( gapRole_TaskID, &params ) != SUCCESS )
  606. {
  607. gapRole_state = GAPROLE_ERROR;
  608. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
  609. {
  610. pGapRoles_AppCGs->pfnStateChange( gapRole_state );
  611. }
  612. }
  613. }
  614. return ( events ^ START_ADVERTISING_EVT );
  615. }
  616. if ( events & RSSI_READ_EVT )
  617. {
  618. // Only get RSSI when in a connection
  619. if ( (gapRole_state == GAPROLE_CONNECTED)
  620. || (gapRole_state == GAPROLE_CONNECTED_ADV) )
  621. {
  622. // Ask for RSSI
  623. VOID HCI_ReadRssiCmd( gapRole_ConnectionHandle );
  624. // Setup next event
  625. if ( gapRole_RSSIReadRate )
  626. {
  627. VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
  628. }
  629. }
  630. return ( events ^ RSSI_READ_EVT );
  631. }
  632. if ( events & UPDATE_PARAMS_TIMEOUT_EVT )
  633. {
  634. // Clear an existing timeout
  635. if ( osal_get_timeoutEx( gapRole_TaskID, UPDATE_PARAMS_TIMEOUT_EVT ) )
  636. {
  637. VOID osal_stop_timerEx( gapRole_TaskID, UPDATE_PARAMS_TIMEOUT_EVT );
  638. }
  639. // The Update Parameters Timeout occurred - Terminate connection
  640. VOID GAP_TerminateLinkReq( gapRole_TaskID, gapRole_ConnectionHandle,
  641. HCI_DISCONNECT_REMOTE_USER_TERM );
  642. return ( events ^ UPDATE_PARAMS_TIMEOUT_EVT );
  643. }
  644. // Discard unknown events
  645. return 0;
  646. }
  647. /*********************************************************************
  648. * @fn gapRole_ProcessOSALMsg
  649. *
  650. * @brief Process an incoming task message.
  651. *
  652. * @param pMsg - message to process
  653. *
  654. * @return none
  655. */
  656. static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg )
  657. {
  658. switch ( pMsg->event )
  659. {
  660. case HCI_GAP_EVENT_EVENT:
  661. if ( pMsg->status == HCI_COMMAND_COMPLETE_EVENT_CODE )
  662. {
  663. hciEvt_CmdComplete_t *pPkt = (hciEvt_CmdComplete_t *)pMsg;
  664. if ( pPkt->cmdOpcode == HCI_READ_RSSI )
  665. {
  666. int8 rssi = (int8)pPkt->pReturnParam[3];
  667. if ( ((gapRole_state == GAPROLE_CONNECTED)
  668. || (gapRole_state == GAPROLE_CONNECTED_ADV))
  669. && (rssi != RSSI_NOT_AVAILABLE) )
  670. {
  671. // Report RSSI to app
  672. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnRssiRead )
  673. {
  674. pGapRoles_AppCGs->pfnRssiRead( rssi );
  675. }
  676. }
  677. }
  678. }
  679. break;
  680. case GAP_MSG_EVENT:
  681. gapRole_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
  682. break;
  683. case L2CAP_SIGNAL_EVENT:
  684. {
  685. l2capSignalEvent_t *pPkt = (l2capSignalEvent_t *)pMsg;
  686. // Process the Parameter Update Response
  687. if ( pPkt->opcode == L2CAP_PARAM_UPDATE_RSP )
  688. {
  689. l2capParamUpdateRsp_t *pRsp = (l2capParamUpdateRsp_t *)&(pPkt->cmd.updateRsp);
  690. if ( pRsp->result == SUCCESS )
  691. {
  692. // All is good stop Update Parameters timeout
  693. VOID osal_stop_timerEx( gapRole_TaskID, UPDATE_PARAMS_TIMEOUT_EVT );
  694. }
  695. }
  696. }
  697. break;
  698. default:
  699. break;
  700. }
  701. }
  702. /*********************************************************************
  703. * @fn gapRole_ProcessGAPMsg
  704. *
  705. * @brief Process an incoming task message.
  706. *
  707. * @param pMsg - message to process
  708. *
  709. * @return none
  710. */
  711. static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg )
  712. {
  713. uint8 notify = FALSE; // State changed notify the app? (default no)
  714. switch ( pMsg->opcode )
  715. {
  716. case GAP_DEVICE_INIT_DONE_EVENT:
  717. {
  718. gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *)pMsg;
  719. bStatus_t stat = pPkt->hdr.status;
  720. if ( stat == SUCCESS )
  721. {
  722. // Save off the generated keys
  723. VOID osal_snv_write( BLE_NVID_IRK, KEYLEN, gapRole_IRK );
  724. VOID osal_snv_write( BLE_NVID_CSRK, KEYLEN, gapRole_SRK );
  725. // Save off the information
  726. VOID osal_memcpy( gapRole_bdAddr, pPkt->devAddr, B_ADDR_LEN );
  727. gapRole_state = GAPROLE_STARTED;
  728. // Update the advertising data
  729. stat = GAP_UpdateAdvertisingData( gapRole_TaskID,
  730. TRUE, gapRole_AdvertDataLen, gapRole_AdvertData );
  731. }
  732. if ( stat != SUCCESS )
  733. {
  734. gapRole_state = GAPROLE_ERROR;
  735. }
  736. notify = TRUE;
  737. }
  738. break;
  739. case GAP_ADV_DATA_UPDATE_DONE_EVENT:
  740. {
  741. gapAdvDataUpdateEvent_t *pPkt = (gapAdvDataUpdateEvent_t *)pMsg;
  742. if ( pPkt->hdr.status == SUCCESS )
  743. {
  744. if ( pPkt->adType )
  745. {
  746. // Setup the Response Data
  747. pPkt->hdr.status = GAP_UpdateAdvertisingData( gapRole_TaskID,
  748. FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
  749. }
  750. else
  751. {
  752. // Start advertising
  753. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  754. }
  755. }
  756. if ( pPkt->hdr.status != SUCCESS )
  757. {
  758. // Set into Error state
  759. gapRole_state = GAPROLE_ERROR;
  760. notify = TRUE;
  761. }
  762. }
  763. break;
  764. case GAP_MAKE_DISCOVERABLE_DONE_EVENT:
  765. case GAP_END_DISCOVERABLE_DONE_EVENT:
  766. {
  767. gapMakeDiscoverableRspEvent_t *pPkt = (gapMakeDiscoverableRspEvent_t *)pMsg;
  768. if ( pPkt->hdr.status == SUCCESS )
  769. {
  770. if ( pMsg->opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT )
  771. {
  772. if ( gapRole_state == GAPROLE_CONNECTED )
  773. {
  774. gapRole_state = GAPROLE_CONNECTED_ADV;
  775. }
  776. else
  777. {
  778. gapRole_state = GAPROLE_ADVERTISING;
  779. }
  780. }
  781. else // GAP_END_DISCOVERABLE_DONE_EVENT
  782. {
  783. if ( gapRole_AdvertOffTime != 0 )
  784. {
  785. if ( ( gapRole_AdvEnabled ) )
  786. {
  787. VOID osal_start_timerEx( gapRole_TaskID, START_ADVERTISING_EVT, gapRole_AdvertOffTime );
  788. }
  789. }
  790. else
  791. {
  792. // Since gapRole_AdvertOffTime is set to 0, the device should not
  793. // automatically become discoverable again after a period of time.
  794. // Set enabler to FALSE; device will become discoverable again when
  795. // this value gets set to TRUE
  796. gapRole_AdvEnabled = FALSE;
  797. }
  798. // In the Advertising Off period
  799. gapRole_state = GAPROLE_WAITING;
  800. }
  801. }
  802. else
  803. {
  804. gapRole_state = GAPROLE_ERROR;
  805. }
  806. notify = TRUE;
  807. }
  808. break;
  809. case GAP_LINK_ESTABLISHED_EVENT:
  810. {
  811. gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;
  812. if ( pPkt->hdr.status == SUCCESS )
  813. {
  814. VOID osal_memcpy( gapRole_ConnectedDevAddr, pPkt->devAddr, B_ADDR_LEN );
  815. gapRole_ConnectionHandle = pPkt->connectionHandle;
  816. gapRole_state = GAPROLE_CONNECTED;
  817. if ( gapRole_RSSIReadRate )
  818. {
  819. // Start the RSSI Reads
  820. VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
  821. }
  822. // Check whether update parameter request is enabled, and check the connection parameters
  823. if ( ( gapRole_ParamUpdateEnable == TRUE ) &&
  824. ( (pPkt->connInterval < gapRole_MinConnInterval) ||
  825. (pPkt->connInterval > gapRole_MaxConnInterval) ||
  826. (pPkt->connLatency != gapRole_SlaveLatency) ||
  827. (pPkt->connTimeout != gapRole_TimeoutMultiplier) ))
  828. {
  829. gapRole_SendUpdateParam( pPkt->connInterval, pPkt->connLatency );
  830. }
  831. // Notify the Bond Manager to the connection
  832. VOID GAPBondMgr_LinkEst( pPkt->devAddrType, pPkt->devAddr, pPkt->connectionHandle, GAP_PROFILE_PERIPHERAL );
  833. }
  834. else
  835. {
  836. gapRole_state = GAPROLE_ERROR;
  837. }
  838. notify = TRUE;
  839. }
  840. break;
  841. case GAP_LINK_TERMINATED_EVENT:
  842. {
  843. VOID GAPBondMgr_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
  844. osal_memset( gapRole_ConnectedDevAddr, 0, B_ADDR_LEN );
  845. if ( gapRole_state == GAPROLE_CONNECTED_ADV )
  846. {
  847. // End the non-connectable advertising
  848. GAP_EndDiscoverable( gapRole_TaskID );
  849. gapRole_state = GAPROLE_CONNECTED;
  850. }
  851. else
  852. {
  853. gapTerminateLinkEvent_t *pPkt = (gapTerminateLinkEvent_t *)pMsg;
  854. if( pPkt->reason == LL_SUPERVISION_TIMEOUT_TERM )
  855. {
  856. gapRole_state = GAPROLE_WAITING_AFTER_TIMEOUT;
  857. }
  858. else
  859. {
  860. gapRole_state = GAPROLE_WAITING;
  861. }
  862. notify = TRUE;
  863. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  864. }
  865. gapRole_ConnectionHandle = INVALID_CONNHANDLE;
  866. }
  867. break;
  868. case GAP_LINK_PARAM_UPDATE_EVENT:
  869. {
  870. gapLinkUpdateEvent_t *pPkt = (gapLinkUpdateEvent_t *)pMsg;
  871. if ( (pPkt->hdr.status != SUCCESS)
  872. || (pPkt->connInterval < gapRole_MinConnInterval)
  873. || (pPkt->connInterval > gapRole_MaxConnInterval) )
  874. {
  875. // Ask to change the interval
  876. gapRole_SendUpdateParam( pPkt->connInterval, pPkt->connLatency );
  877. }
  878. else
  879. {
  880. // All is good stop Update Parameters timeout
  881. VOID osal_stop_timerEx( gapRole_TaskID, UPDATE_PARAMS_TIMEOUT_EVT );
  882. }
  883. }
  884. break;
  885. default:
  886. break;
  887. }
  888. if ( notify == TRUE )
  889. {
  890. // Notify the application
  891. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
  892. {
  893. pGapRoles_AppCGs->pfnStateChange( gapRole_state );
  894. }
  895. }
  896. }
  897. /*********************************************************************
  898. * @fn gapRole_SetupGAP
  899. *
  900. * @brief Call the GAP Device Initialization function using the
  901. * Profile Parameters.
  902. *
  903. * @param none
  904. *
  905. * @return none
  906. */
  907. static void gapRole_SetupGAP( void )
  908. {
  909. VOID GAP_DeviceInit( gapRole_TaskID,
  910. gapRole_profileRole, 0,
  911. gapRole_IRK, gapRole_SRK,
  912. &gapRole_signCounter );
  913. }
  914. /*********************************************************************
  915. * @fn gapRole_SendUpdateParam
  916. *
  917. * @brief Send an Update Connection Parameters.
  918. *
  919. * @param connInterval - current connection interval
  920. * @param connLatency - current connection latency
  921. *
  922. * @return none
  923. */
  924. static void gapRole_SendUpdateParam( uint16 connInterval, uint16 connLatency )
  925. {
  926. l2capParamUpdateReq_t updateReq; // Space for Conn Update parameters
  927. uint32 timeout; // Calculated response timeout
  928. // Calculate the current interval
  929. uint16 effectiveOldInterval = (connInterval * (connLatency + 1));
  930. // Calculate the interval we want
  931. uint16 effectiveNewMaxInterval = (gapRole_MaxConnInterval * (gapRole_SlaveLatency + 1));
  932. // Fill in the wanted parameters
  933. updateReq.intervalMin = gapRole_MinConnInterval;
  934. updateReq.intervalMax = gapRole_MaxConnInterval;
  935. updateReq.slaveLatency = gapRole_SlaveLatency;
  936. updateReq.timeoutMultiplier = gapRole_TimeoutMultiplier;
  937. VOID L2CAP_ConnParamUpdateReq( gapRole_ConnectionHandle, &updateReq, gapRole_TaskID );
  938. // Set up the timeout for expected response
  939. if( effectiveOldInterval > effectiveNewMaxInterval )
  940. {
  941. timeout = (uint32)(effectiveOldInterval) * CONN_INTERVAL_MULTIPLIER;
  942. }
  943. else
  944. {
  945. timeout = (uint32)(effectiveNewMaxInterval) * CONN_INTERVAL_MULTIPLIER;
  946. }
  947. if( timeout > MAX_TIMEOUT_VALUE )
  948. {
  949. timeout = MAX_TIMEOUT_VALUE;
  950. }
  951. VOID osal_start_timerEx( gapRole_TaskID, UPDATE_PARAMS_TIMEOUT_EVT, (uint16)(timeout) );
  952. }
  953. /*********************************************************************
  954. *********************************************************************/