peripheral.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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. Filename: peripheral.c
  30. Revised:
  31. Revision:
  32. Description: GAP Peripheral Role
  33. **************************************************************************************************/
  34. /*********************************************************************
  35. * INCLUDES
  36. */
  37. #include "bcomdef.h"
  38. #include "OSAL.h"
  39. #include "hci_tl.h"
  40. #include "l2cap.h"
  41. #include "gap.h"
  42. #include "linkdb.h"
  43. #include "att.h"
  44. #include "gatt.h"
  45. #include "osal_snv.h"
  46. #include "log.h"
  47. #include "peripheral.h"
  48. #include "gapbondmgr.h"
  49. #ifdef EXT_ADV_ENABLE
  50. #include "rf_phy_driver.h"
  51. #endif
  52. /*********************************************************************
  53. * MACROS
  54. */
  55. /*********************************************************************
  56. * CONSTANTS
  57. */
  58. // Profile Events
  59. #define START_ADVERTISING_EVT 0x0001 // Start Advertising
  60. #define RSSI_READ_EVT 0x0002 // Read RSSI
  61. #define START_CONN_UPDATE_EVT 0x0004 // Start Connection Update Procedure
  62. #define CONN_PARAM_TIMEOUT_EVT 0x0008 // Connection Parameters Update Timeout
  63. #ifdef EXT_ADV_ENABLE
  64. #define START_SECOND_ADVERTISING_EVENT 0x0010
  65. #endif
  66. #define DEFAULT_ADVERT_OFF_TIME 30000 // 30 seconds
  67. #define RSSI_NOT_AVAILABLE 127
  68. #define DEFAULT_MIN_CONN_INTERVAL 0x0006 // 100 milliseconds
  69. #define DEFAULT_MAX_CONN_INTERVAL 0x0C80 // 4 seconds
  70. #define MIN_CONN_INTERVAL 0x0006
  71. #define MAX_CONN_INTERVAL 0x0C80
  72. #define DEFAULT_TIMEOUT_MULTIPLIER 1000
  73. #define CONN_INTERVAL_MULTIPLIER 6
  74. #define MIN_SLAVE_LATENCY 0
  75. #define MAX_SLAVE_LATENCY 500
  76. #define MIN_TIMEOUT_MULTIPLIER 0x000a
  77. #define MAX_TIMEOUT_MULTIPLIER 0x0c80
  78. #define MAX_TIMEOUT_VALUE 0xFFFF
  79. /*********************************************************************
  80. * TYPEDEFS
  81. */
  82. /*********************************************************************
  83. * GLOBAL VARIABLES
  84. */
  85. /*********************************************************************
  86. * EXTERNAL VARIABLES
  87. */
  88. /*********************************************************************
  89. * EXTERNAL FUNCTIONS
  90. */
  91. /*********************************************************************
  92. * LOCAL VARIABLES
  93. */
  94. static uint8 gapRole_TaskID; // Task ID for internal task/event processing
  95. static gaprole_States_t gapRole_state;
  96. /*********************************************************************
  97. * Profile Parameters - reference GAPROLE_PROFILE_PARAMETERS for
  98. * descriptions
  99. */
  100. static uint8 gapRole_profileRole;
  101. static uint8 gapRole_IRK[KEYLEN];
  102. static uint8 gapRole_SRK[KEYLEN];
  103. static uint32 gapRole_signCounter;
  104. static uint8 gapRole_bdAddr[B_ADDR_LEN];
  105. uint8 gapRole_AdvEnabled = FALSE;
  106. #ifdef EXT_ADV_ENABLE
  107. static uint8 gapRole_ExtAdvEnabled = FALSE;
  108. #endif
  109. static uint16 gapRole_AdvertOffTime = DEFAULT_ADVERT_OFF_TIME;
  110. static uint8 gapRole_AdvertDataLen = 3;
  111. static uint8 gapRole_AdvertData[B_MAX_ADV_LEN] =
  112. {
  113. 0x02, // length of this data
  114. GAP_ADTYPE_FLAGS, // AD Type = Flags
  115. // Limited Discoverable & BR/EDR not supported
  116. (GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED),
  117. 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
  118. };
  119. static uint8 gapRole_ScanRspDataLen = 0;
  120. static uint8 gapRole_ScanRspData[B_MAX_ADV_LEN] = {0};
  121. uint8 gapRole_AdvEventType;
  122. static uint8 gapRole_AdvDirectType;
  123. static uint8 gapRole_AdvDirectAddr[B_ADDR_LEN] = {0};
  124. static uint8 gapRole_AdvChanMap;
  125. static uint8 gapRole_AdvFilterPolicy;
  126. static uint16 gapRole_ConnectionHandle = INVALID_CONNHANDLE;
  127. static uint16 gapRole_ConnectionInterval = 0;
  128. static uint16 gapRole_ConnectionLatency = 0;
  129. static uint16 gapRole_RSSIReadRate = 0;
  130. static uint8 gapRole_ConnectedDevAddr[B_ADDR_LEN] = {0};
  131. static uint8 gapRole_ParamUpdateEnable = FALSE;
  132. static uint16 gapRole_MinConnInterval = DEFAULT_MIN_CONN_INTERVAL;
  133. static uint16 gapRole_MaxConnInterval = DEFAULT_MAX_CONN_INTERVAL;
  134. static uint16 gapRole_SlaveLatency = MIN_SLAVE_LATENCY;
  135. static uint16 gapRole_TimeoutMultiplier = DEFAULT_TIMEOUT_MULTIPLIER;
  136. static uint16 gapRole_ConnInterval = 0;
  137. static uint16 gapRole_ConnSlaveLatency = 0;
  138. static uint16 gapRole_ConnTimeout = 0;
  139. static uint8 paramUpdateNoSuccessOption = GAPROLE_NO_ACTION;
  140. // Application callbacks
  141. static gapRolesCBs_t *pGapRoles_AppCGs = NULL;
  142. static gapRolesParamUpdateCB_t *pGapRoles_ParamUpdateCB = NULL;
  143. /*********************************************************************
  144. * Profile Attributes - variables
  145. */
  146. /*********************************************************************
  147. * Profile Attributes - Table
  148. */
  149. /*********************************************************************
  150. * LOCAL FUNCTIONS
  151. */
  152. static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg );
  153. static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg );
  154. static void gapRole_SetupGAP( void );
  155. static void gapRole_HandleParamUpdateNoSuccess( void );
  156. static void gapRole_startConnUpdate( uint8 handleFailure );
  157. #ifdef EXT_ADV_ENABLE
  158. extern volatile uint8_t g_rfPhyPktFmt;
  159. #endif
  160. /*********************************************************************
  161. * NETWORK LAYER CALLBACKS
  162. */
  163. /*********************************************************************
  164. * PUBLIC FUNCTIONS
  165. */
  166. /*********************************************************************
  167. * @brief Set a GAP Role parameter.
  168. *
  169. * Public function defined in peripheral.h.
  170. */
  171. bStatus_t GAPRole_SetParameter( uint16 param, uint8 len, void *pValue )
  172. {
  173. bStatus_t ret = SUCCESS;
  174. switch ( param )
  175. {
  176. case GAPROLE_IRK:
  177. if ( len == KEYLEN )
  178. {
  179. VOID osal_memcpy( gapRole_IRK, pValue, KEYLEN ) ;
  180. }
  181. else
  182. {
  183. ret = bleInvalidRange;
  184. }
  185. break;
  186. case GAPROLE_SRK:
  187. if ( len == KEYLEN )
  188. {
  189. VOID osal_memcpy( gapRole_SRK, pValue, KEYLEN ) ;
  190. }
  191. else
  192. {
  193. ret = bleInvalidRange;
  194. }
  195. break;
  196. case GAPROLE_SIGNCOUNTER:
  197. if ( len == sizeof ( uint32 ) )
  198. {
  199. gapRole_signCounter = *((uint32*)pValue);
  200. }
  201. else
  202. {
  203. ret = bleInvalidRange;
  204. }
  205. break;
  206. case GAPROLE_ADVERT_ENABLED:
  207. if ( len == sizeof( uint8 ) )
  208. {
  209. uint8 oldAdvEnabled = gapRole_AdvEnabled;
  210. gapRole_AdvEnabled = *((uint8*)pValue);
  211. if ( (oldAdvEnabled) && (gapRole_AdvEnabled == FALSE) )
  212. {
  213. // Turn off Advertising
  214. if ( ( gapRole_state == GAPROLE_ADVERTISING )
  215. || ( gapRole_state == GAPROLE_CONNECTED_ADV )
  216. || ( gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT ) )
  217. {
  218. VOID GAP_EndDiscoverable( gapRole_TaskID );
  219. }
  220. }
  221. else if ( (oldAdvEnabled == FALSE) && (gapRole_AdvEnabled) )
  222. {
  223. // Turn on Advertising
  224. if ( (gapRole_state == GAPROLE_STARTED)
  225. || (gapRole_state == GAPROLE_WAITING)
  226. || (gapRole_state == GAPROLE_CONNECTED)
  227. || (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT) )
  228. {
  229. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  230. }
  231. }
  232. }
  233. else
  234. {
  235. ret = bleInvalidRange;
  236. }
  237. break;
  238. case GAPROLE_ADVERT_OFF_TIME:
  239. if ( len == sizeof ( uint16 ) )
  240. {
  241. gapRole_AdvertOffTime = *((uint16*)pValue);
  242. }
  243. else
  244. {
  245. ret = bleInvalidRange;
  246. }
  247. break;
  248. case GAPROLE_ADVERT_DATA:
  249. if ( len <= B_MAX_ADV_LEN )
  250. {
  251. VOID osal_memset( gapRole_AdvertData, 0, B_MAX_ADV_LEN );
  252. VOID osal_memcpy( gapRole_AdvertData, pValue, len );
  253. gapRole_AdvertDataLen = len;
  254. // Update the advertising data
  255. ret = GAP_UpdateAdvertisingData( gapRole_TaskID,
  256. TRUE, len, gapRole_AdvertData );
  257. }
  258. else
  259. {
  260. ret = bleInvalidRange;
  261. }
  262. break;
  263. case GAPROLE_SCAN_RSP_DATA:
  264. if ( len <= B_MAX_ADV_LEN )
  265. {
  266. VOID osal_memset( gapRole_ScanRspData, 0, B_MAX_ADV_LEN );
  267. VOID osal_memcpy( gapRole_ScanRspData, pValue, len );
  268. gapRole_ScanRspDataLen = len;
  269. // Update the Response Data
  270. ret = GAP_UpdateAdvertisingData( gapRole_TaskID,
  271. FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
  272. }
  273. else
  274. {
  275. ret = bleInvalidRange;
  276. }
  277. break;
  278. case GAPROLE_ADV_EVENT_TYPE:
  279. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_ADTYPE_ADV_LDC_DIRECT_IND) )
  280. {
  281. gapRole_AdvEventType = *((uint8*)pValue);
  282. }
  283. else
  284. {
  285. ret = bleInvalidRange;
  286. }
  287. break;
  288. case GAPROLE_ADV_DIRECT_TYPE:
  289. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= ADDRTYPE_PRIVATE_RESOLVE) )
  290. {
  291. gapRole_AdvDirectType = *((uint8*)pValue);
  292. }
  293. else
  294. {
  295. ret = bleInvalidRange;
  296. }
  297. break;
  298. case GAPROLE_ADV_DIRECT_ADDR:
  299. if ( len == B_ADDR_LEN )
  300. {
  301. VOID osal_memcpy( gapRole_AdvDirectAddr, pValue, B_ADDR_LEN ) ;
  302. }
  303. else
  304. {
  305. ret = bleInvalidRange;
  306. }
  307. break;
  308. case GAPROLE_ADV_CHANNEL_MAP:
  309. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= 0x07) )
  310. {
  311. gapRole_AdvChanMap = *((uint8*)pValue);
  312. }
  313. else
  314. {
  315. ret = bleInvalidRange;
  316. }
  317. break;
  318. case GAPROLE_ADV_FILTER_POLICY:
  319. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_FILTER_POLICY_WHITE) )
  320. {
  321. gapRole_AdvFilterPolicy = *((uint8*)pValue);
  322. }
  323. else
  324. {
  325. ret = bleInvalidRange;
  326. }
  327. break;
  328. case GAPROLE_RSSI_READ_RATE:
  329. if ( len == sizeof ( uint16 ) )
  330. {
  331. gapRole_RSSIReadRate = *((uint16*)pValue);
  332. if ( (gapRole_RSSIReadRate) && (gapRole_state == GAPROLE_CONNECTED) )
  333. {
  334. // Start the RSSI Reads
  335. VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
  336. }
  337. }
  338. else
  339. {
  340. ret = bleInvalidRange;
  341. }
  342. break;
  343. case GAPROLE_PARAM_UPDATE_ENABLE:
  344. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
  345. {
  346. gapRole_ParamUpdateEnable = *((uint8*)pValue);
  347. }
  348. else
  349. {
  350. ret = bleInvalidRange;
  351. }
  352. break;
  353. case GAPROLE_MIN_CONN_INTERVAL:
  354. {
  355. uint16 newInterval = *((uint16*)pValue);
  356. if ( len == sizeof ( uint16 ) &&
  357. ( newInterval >= MIN_CONN_INTERVAL ) &&
  358. ( newInterval <= MAX_CONN_INTERVAL ) )
  359. {
  360. gapRole_MinConnInterval = newInterval;
  361. }
  362. else
  363. {
  364. ret = bleInvalidRange;
  365. }
  366. }
  367. break;
  368. case GAPROLE_MAX_CONN_INTERVAL:
  369. {
  370. uint16 newInterval = *((uint16*)pValue);
  371. if ( len == sizeof ( uint16 ) &&
  372. ( newInterval >= MIN_CONN_INTERVAL) &&
  373. ( newInterval <= MAX_CONN_INTERVAL) )
  374. {
  375. gapRole_MaxConnInterval = newInterval;
  376. }
  377. else
  378. {
  379. ret = bleInvalidRange;
  380. }
  381. }
  382. break;
  383. case GAPROLE_SLAVE_LATENCY:
  384. {
  385. uint16 latency = *((uint16*)pValue);
  386. if ( len == sizeof ( uint16 ) && (latency < MAX_SLAVE_LATENCY) )
  387. {
  388. gapRole_SlaveLatency = latency;
  389. }
  390. else
  391. {
  392. ret = bleInvalidRange;
  393. }
  394. }
  395. break;
  396. case GAPROLE_TIMEOUT_MULTIPLIER:
  397. {
  398. uint16 newTimeout = *((uint16*)pValue);
  399. if ( len == sizeof ( uint16 )
  400. && (newTimeout >= MIN_TIMEOUT_MULTIPLIER) && (newTimeout <= MAX_TIMEOUT_MULTIPLIER) )
  401. {
  402. gapRole_TimeoutMultiplier = newTimeout;
  403. }
  404. else
  405. {
  406. ret = bleInvalidRange;
  407. }
  408. }
  409. break;
  410. case GAPROLE_PARAM_UPDATE_REQ:
  411. {
  412. uint8 req = *((uint8*)pValue);
  413. if ( len == sizeof ( uint8 ) && (req == TRUE) )
  414. {
  415. // Make sure we don't send an L2CAP Connection Parameter Update Request
  416. // command within TGAP(conn_param_timeout) of an L2CAP Connection Parameter
  417. // Update Response being received.
  418. if ( osal_get_timeoutEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT ) == 0 )
  419. {
  420. // Start connection update procedure
  421. gapRole_startConnUpdate( GAPROLE_NO_ACTION );
  422. // Connection update requested by app, cancel such pending procedure (if active)
  423. VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
  424. }
  425. else
  426. {
  427. ret = blePending;
  428. }
  429. }
  430. else
  431. {
  432. ret = bleInvalidRange;
  433. }
  434. }
  435. break;
  436. #ifdef EXT_ADV_ENABLE
  437. case GAPROLE_EXT_ADVERT_ENABLED:
  438. if ( len == sizeof( uint8 ) )
  439. {
  440. gapRole_ExtAdvEnabled = *((uint8*)pValue);
  441. }
  442. else
  443. {
  444. ret = bleInvalidRange;
  445. }
  446. break;
  447. #endif
  448. default:
  449. // The param value isn't part of this profile, try the GAP.
  450. if ( (param < TGAP_PARAMID_MAX) && (len == sizeof ( uint16 )) )
  451. {
  452. ret = GAP_SetParamValue( param, *((uint16*)pValue) );
  453. }
  454. else
  455. {
  456. ret = INVALIDPARAMETER;
  457. }
  458. break;
  459. }
  460. return ( ret );
  461. }
  462. /*********************************************************************
  463. * @brief Get a GAP Role parameter.
  464. *
  465. * Public function defined in peripheral.h.
  466. */
  467. bStatus_t GAPRole_GetParameter( uint16 param, void *pValue )
  468. {
  469. bStatus_t ret = SUCCESS;
  470. switch ( param )
  471. {
  472. case GAPROLE_PROFILEROLE:
  473. *((uint8*)pValue) = gapRole_profileRole;
  474. break;
  475. case GAPROLE_IRK:
  476. VOID osal_memcpy( pValue, gapRole_IRK, KEYLEN ) ;
  477. break;
  478. case GAPROLE_SRK:
  479. VOID osal_memcpy( pValue, gapRole_SRK, KEYLEN ) ;
  480. break;
  481. case GAPROLE_SIGNCOUNTER:
  482. *((uint32*)pValue) = gapRole_signCounter;
  483. break;
  484. case GAPROLE_BD_ADDR:
  485. VOID osal_memcpy( pValue, gapRole_bdAddr, B_ADDR_LEN ) ;
  486. break;
  487. case GAPROLE_ADVERT_ENABLED:
  488. *((uint8*)pValue) = gapRole_AdvEnabled;
  489. break;
  490. case GAPROLE_ADVERT_OFF_TIME:
  491. *((uint16*)pValue) = gapRole_AdvertOffTime;
  492. break;
  493. case GAPROLE_ADVERT_DATA:
  494. VOID osal_memcpy( pValue , gapRole_AdvertData, gapRole_AdvertDataLen );
  495. break;
  496. case GAPROLE_SCAN_RSP_DATA:
  497. VOID osal_memcpy( pValue, gapRole_ScanRspData, gapRole_ScanRspDataLen ) ;
  498. break;
  499. case GAPROLE_ADV_EVENT_TYPE:
  500. *((uint8*)pValue) = gapRole_AdvEventType;
  501. break;
  502. case GAPROLE_ADV_DIRECT_TYPE:
  503. *((uint8*)pValue) = gapRole_AdvDirectType;
  504. break;
  505. case GAPROLE_ADV_DIRECT_ADDR:
  506. VOID osal_memcpy( pValue, gapRole_AdvDirectAddr, B_ADDR_LEN ) ;
  507. break;
  508. case GAPROLE_ADV_CHANNEL_MAP:
  509. *((uint8*)pValue) = gapRole_AdvChanMap;
  510. break;
  511. case GAPROLE_ADV_FILTER_POLICY:
  512. *((uint8*)pValue) = gapRole_AdvFilterPolicy;
  513. break;
  514. case GAPROLE_CONNHANDLE:
  515. *((uint16*)pValue) = gapRole_ConnectionHandle;
  516. break;
  517. case GAPROLE_RSSI_READ_RATE:
  518. *((uint16*)pValue) = gapRole_RSSIReadRate;
  519. break;
  520. case GAPROLE_PARAM_UPDATE_ENABLE:
  521. *((uint16*)pValue) = gapRole_ParamUpdateEnable;
  522. break;
  523. case GAPROLE_MIN_CONN_INTERVAL:
  524. *((uint16*)pValue) = gapRole_MinConnInterval;
  525. break;
  526. case GAPROLE_MAX_CONN_INTERVAL:
  527. *((uint16*)pValue) = gapRole_MaxConnInterval;
  528. break;
  529. case GAPROLE_SLAVE_LATENCY:
  530. *((uint16*)pValue) = gapRole_SlaveLatency;
  531. break;
  532. case GAPROLE_TIMEOUT_MULTIPLIER:
  533. *((uint16*)pValue) = gapRole_TimeoutMultiplier;
  534. break;
  535. case GAPROLE_CONN_BD_ADDR:
  536. VOID osal_memcpy( pValue, gapRole_ConnectedDevAddr, B_ADDR_LEN ) ;
  537. break;
  538. case GAPROLE_CONN_INTERVAL:
  539. *((uint16*)pValue) = gapRole_ConnInterval;
  540. break;
  541. case GAPROLE_CONN_LATENCY:
  542. *((uint16*)pValue) = gapRole_ConnSlaveLatency;
  543. break;
  544. case GAPROLE_CONN_TIMEOUT:
  545. *((uint16*)pValue) = gapRole_ConnTimeout;
  546. break;
  547. case GAPROLE_STATE:
  548. *((uint8*)pValue) = gapRole_state;
  549. break;
  550. case GAPROLE_CONNECTION_INTERVAL:
  551. *((uint16*)pValue) = gapRole_ConnectionInterval;
  552. break;
  553. case GAPROLE_CONNECTION_LATENCY:
  554. *((uint16*)pValue) = gapRole_ConnectionLatency;
  555. break;
  556. #ifdef EXT_ADV_ENABLE
  557. case GAPROLE_EXT_ADVERT_ENABLED:
  558. *((uint8*)pValue) = gapRole_ExtAdvEnabled;
  559. break;
  560. #endif
  561. default:
  562. // The param value isn't part of this profile, try the GAP.
  563. if ( param < TGAP_PARAMID_MAX )
  564. {
  565. *((uint16*)pValue) = GAP_GetParamValue( param );
  566. }
  567. else
  568. {
  569. ret = INVALIDPARAMETER;
  570. }
  571. break;
  572. }
  573. return ( ret );
  574. }
  575. /*********************************************************************
  576. * @brief Does the device initialization.
  577. *
  578. * Public function defined in peripheral.h.
  579. */
  580. bStatus_t GAPRole_StartDevice( gapRolesCBs_t *pAppCallbacks )
  581. {
  582. if ( gapRole_state == GAPROLE_INIT )
  583. {
  584. // Clear all of the Application callbacks
  585. if ( pAppCallbacks )
  586. {
  587. pGapRoles_AppCGs = pAppCallbacks;
  588. }
  589. // Start the GAP
  590. gapRole_SetupGAP();
  591. return ( SUCCESS );
  592. }
  593. else
  594. {
  595. return ( bleAlreadyInRequestedMode );
  596. }
  597. }
  598. /*********************************************************************
  599. * @brief Register application's callbacks.
  600. *
  601. * Public function defined in peripheral.h.
  602. */
  603. void GAPRole_RegisterAppCBs( gapRolesParamUpdateCB_t *pParamUpdateCB )
  604. {
  605. if ( pParamUpdateCB != NULL )
  606. {
  607. pGapRoles_ParamUpdateCB = pParamUpdateCB;
  608. }
  609. }
  610. /*********************************************************************
  611. * @brief Terminates the existing connection.
  612. *
  613. * Public function defined in peripheral.h.
  614. */
  615. bStatus_t GAPRole_TerminateConnection( void )
  616. {
  617. if ( gapRole_state == GAPROLE_CONNECTED )
  618. {
  619. return ( GAP_TerminateLinkReq( gapRole_TaskID, gapRole_ConnectionHandle,
  620. HCI_DISCONNECT_REMOTE_USER_TERM ) );
  621. }
  622. else
  623. {
  624. return ( bleIncorrectMode );
  625. }
  626. }
  627. /*********************************************************************
  628. * LOCAL FUNCTION PROTOTYPES
  629. */
  630. /*********************************************************************
  631. * @brief Task Initialization function.
  632. *
  633. * Internal function defined in peripheral.h.
  634. */
  635. void GAPRole_Init( uint8 task_id )
  636. {
  637. gapRole_TaskID = task_id;
  638. gapRole_state = GAPROLE_INIT;
  639. gapRole_ConnectionHandle = INVALID_CONNHANDLE;
  640. GAP_RegisterForHCIMsgs( gapRole_TaskID );
  641. // Initialize the Profile Advertising and Connection Parameters
  642. gapRole_profileRole = GAP_PROFILE_PERIPHERAL;
  643. VOID osal_memset( gapRole_IRK, 0, KEYLEN );
  644. VOID osal_memset( gapRole_SRK, 0, KEYLEN );
  645. gapRole_signCounter = 0;
  646. gapRole_AdvEventType = GAP_ADTYPE_ADV_IND;
  647. gapRole_AdvDirectType = ADDRTYPE_PUBLIC;
  648. gapRole_AdvChanMap = GAP_ADVCHAN_37;// GAP_ADVCHAN_ALL;
  649. gapRole_AdvFilterPolicy = GAP_FILTER_POLICY_ALL;
  650. // Restore Items from NV
  651. // VOID osal_snv_read( BLE_NVID_IRK, KEYLEN, gapRole_IRK );
  652. // VOID osal_snv_read( BLE_NVID_CSRK, KEYLEN, gapRole_SRK );
  653. // VOID osal_snv_read( BLE_NVID_SIGNCOUNTER, sizeof( uint32 ), &gapRole_signCounter );
  654. }
  655. /*********************************************************************
  656. * @brief Task Event Processor function.
  657. *
  658. * Internal function defined in peripheral.h.
  659. */
  660. uint16 GAPRole_ProcessEvent( uint8 task_id, uint16 events )
  661. {
  662. VOID task_id; // OSAL required parameter that isn't used in this function
  663. if ( events & SYS_EVENT_MSG )
  664. {
  665. uint8 *pMsg;
  666. if ( (pMsg = osal_msg_receive( gapRole_TaskID )) != NULL )
  667. {
  668. gapRole_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
  669. // Release the OSAL message
  670. VOID osal_msg_deallocate( pMsg );
  671. }
  672. // return unprocessed events
  673. return (events ^ SYS_EVENT_MSG);
  674. }
  675. if ( events & GAP_EVENT_SIGN_COUNTER_CHANGED )
  676. {
  677. // Sign counter changed, save it to NV
  678. // VOID osal_snv_write( BLE_NVID_SIGNCOUNTER, sizeof( uint32 ), &gapRole_signCounter );
  679. return ( events ^ GAP_EVENT_SIGN_COUNTER_CHANGED );
  680. }
  681. if ( events & START_ADVERTISING_EVT )
  682. {
  683. if ( gapRole_AdvEnabled )
  684. {
  685. gapAdvertisingParams_t params;
  686. // Setup advertisement parameters
  687. if ( gapRole_state == GAPROLE_CONNECTED )
  688. {
  689. // While in a connection, we can only advertise non-connectable undirected.
  690. params.eventType = GAP_ADTYPE_ADV_NONCONN_IND;
  691. }
  692. else
  693. {
  694. params.eventType = gapRole_AdvEventType;
  695. params.initiatorAddrType = gapRole_AdvDirectType;
  696. VOID osal_memcpy( params.initiatorAddr, gapRole_AdvDirectAddr, B_ADDR_LEN );
  697. }
  698. params.channelMap = gapRole_AdvChanMap;
  699. params.filterPolicy = gapRole_AdvFilterPolicy;
  700. uint8 ret = GAP_MakeDiscoverable( gapRole_TaskID, &params );
  701. if ( ret != SUCCESS )
  702. {
  703. gapRole_state = GAPROLE_ERROR;
  704. // Notify the application with the new state change
  705. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
  706. {
  707. pGapRoles_AppCGs->pfnStateChange( gapRole_state );
  708. }
  709. }
  710. }
  711. return ( events ^ START_ADVERTISING_EVT );
  712. }
  713. #ifdef EXT_ADV_ENABLE
  714. if( events & START_SECOND_ADVERTISING_EVENT)
  715. {
  716. if( gapRole_ExtAdvEnabled )
  717. {
  718. if( GAP_EXTADV_MAKEDiscoverable(gapRole_TaskID, TRUE) != SUCCESS )
  719. {
  720. gapRole_state = GAPROLE_ERROR;
  721. // Notify the application with the new state change
  722. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
  723. {
  724. pGapRoles_AppCGs->pfnStateChange( gapRole_state );
  725. }
  726. }
  727. }
  728. return ( events ^ START_SECOND_ADVERTISING_EVENT );
  729. }
  730. #endif
  731. if ( events & RSSI_READ_EVT )
  732. {
  733. // Only get RSSI when in a connection
  734. if ( gapRole_state == GAPROLE_CONNECTED )
  735. {
  736. // Ask for RSSI
  737. VOID HCI_ReadRssiCmd( gapRole_ConnectionHandle );
  738. // Setup next event
  739. if ( gapRole_RSSIReadRate )
  740. {
  741. VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
  742. }
  743. }
  744. return ( events ^ RSSI_READ_EVT );
  745. }
  746. if ( events & START_CONN_UPDATE_EVT )
  747. {
  748. // Start connection update procedure
  749. gapRole_startConnUpdate( GAPROLE_NO_ACTION );
  750. return ( events ^ START_CONN_UPDATE_EVT );
  751. }
  752. if ( events & CONN_PARAM_TIMEOUT_EVT )
  753. {
  754. // Unsuccessful in updating connection parameters
  755. gapRole_HandleParamUpdateNoSuccess();
  756. return ( events ^ CONN_PARAM_TIMEOUT_EVT );
  757. }
  758. // Discard unknown events
  759. return 0;
  760. }
  761. /*********************************************************************
  762. * @fn gapRole_ProcessOSALMsg
  763. *
  764. * @brief Process an incoming task message.
  765. *
  766. * @param pMsg - message to process
  767. *
  768. * @return none
  769. */
  770. static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg )
  771. {
  772. switch ( pMsg->event )
  773. {
  774. case HCI_GAP_EVENT_EVENT:
  775. //LOG("GAP EVT[%d]\n", pMsg->status);
  776. if ( pMsg->status == HCI_COMMAND_COMPLETE_EVENT_CODE )
  777. {
  778. hciEvt_CmdComplete_t *pPkt = (hciEvt_CmdComplete_t *)pMsg;
  779. if ( pPkt->cmdOpcode == HCI_READ_RSSI )
  780. {
  781. int8 rssi = (int8)pPkt->pReturnParam[3];
  782. if ( (gapRole_state == GAPROLE_CONNECTED) && (rssi != RSSI_NOT_AVAILABLE) )
  783. {
  784. // Report RSSI to app
  785. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnRssiRead )
  786. {
  787. pGapRoles_AppCGs->pfnRssiRead( rssi );
  788. }
  789. }
  790. }
  791. else if(pPkt->cmdOpcode == HCI_LE_SET_DATA_LENGTH)
  792. {
  793. LOG("[HCI DLE]%d %d %d\n",pPkt->pReturnParam[0],pPkt->pReturnParam[1],pPkt->pReturnParam[2]);
  794. }
  795. }
  796. else if(pMsg->status==HCI_COMMAND_STATUS_EVENT_CODE)
  797. {
  798. hciEvt_CommandStatus_t *pPkt = (hciEvt_CommandStatus_t *)pMsg;
  799. if ( pPkt->cmdOpcode == HCI_LE_SET_PHY )
  800. {
  801. AT_LOG("[HCI PHY] %2x \n",pPkt->cmdStatus);
  802. }
  803. }
  804. else if(pMsg->status==HCI_LE_EVENT_CODE)
  805. {
  806. hciEvt_BLEEvent_Hdr_t *pPkt = (hciEvt_BLEEvent_Hdr_t *)pMsg;
  807. if ( pPkt->BLEEventCode == HCI_BLE_DATA_LENGTH_CHANGE_EVENT )
  808. {
  809. // hciEvt_BLEDataLenChange_t * pkt = (hciEvt_BLEDataLenChange_t *)pMsg;
  810. // AT_LOG("[HCI DLE EVT]%d %d %d %d\n",pkt->MaxRxOctets,pkt->MaxRxTime,
  811. // pkt->MaxTxOctets,pkt->MaxTxTime);
  812. }
  813. else if(pPkt->BLEEventCode == HCI_BLE_PHY_UPDATE_COMPLETE_EVENT )
  814. {
  815. // hciEvt_BLEPhyUpdateComplete_t * pkt = (hciEvt_BLEPhyUpdateComplete_t *)pMsg;
  816. // AT_LOG("[HCI PHY EVT]s%d r%d t%d\n",pkt->status, pkt->rxPhy,pkt->txPhy);
  817. }
  818. }
  819. break;
  820. case GAP_MSG_EVENT:
  821. gapRole_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
  822. break;
  823. case L2CAP_SIGNAL_EVENT:
  824. {
  825. l2capSignalEvent_t *pPkt = (l2capSignalEvent_t *)pMsg;
  826. // Process the Parameter Update Response
  827. if ( pPkt->opcode == L2CAP_PARAM_UPDATE_RSP )
  828. {
  829. l2capParamUpdateRsp_t *pRsp = (l2capParamUpdateRsp_t *)&(pPkt->cmd.updateRsp);
  830. if ( ( pRsp->result == L2CAP_CONN_PARAMS_REJECTED ) &&
  831. ( paramUpdateNoSuccessOption == GAPROLE_TERMINATE_LINK ) )
  832. {
  833. // Cancel connection param update timeout timer
  834. VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
  835. // Terminate connection immediately
  836. GAPRole_TerminateConnection();
  837. }
  838. else
  839. {
  840. uint16 timeout = GAP_GetParamValue( TGAP_CONN_PARAM_TIMEOUT );
  841. // Let's wait for Controller to update connection parameters if they're
  842. // accepted. Otherwise, decide what to do based on no success option.
  843. VOID osal_start_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT, timeout );
  844. }
  845. }
  846. }
  847. break;
  848. default:
  849. break;
  850. }
  851. }
  852. /*********************************************************************
  853. * @fn gapRole_ProcessGAPMsg
  854. *
  855. * @brief Process an incoming task message.
  856. *
  857. * @param pMsg - message to process
  858. *
  859. * @return none
  860. */
  861. static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg )
  862. {
  863. uint8 notify = FALSE; // State changed notify the app? (default no)
  864. #ifdef EXT_ADV_ENABLE
  865. uint8 LegacyAdvEnable = FALSE;
  866. uint8 SecondAdvEnable = FALSE;
  867. gapDeviceInitDoneEvent_t *pPktsec = (gapDeviceInitDoneEvent_t *)pMsg;
  868. bStatus_t statsec = pPktsec->hdr.status;
  869. GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED,&LegacyAdvEnable);
  870. GAPRole_GetParameter(GAPROLE_EXT_ADVERT_ENABLED , &SecondAdvEnable);
  871. if( LegacyAdvEnable )
  872. {
  873. #endif
  874. switch ( pMsg->opcode )
  875. {
  876. case GAP_DEVICE_INIT_DONE_EVENT:
  877. {
  878. gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *)pMsg;
  879. bStatus_t stat = pPkt->hdr.status;
  880. if ( stat == SUCCESS )
  881. {
  882. // Save off the generated keys
  883. // VOID osal_snv_write( BLE_NVID_IRK, KEYLEN, gapRole_IRK );
  884. // VOID osal_snv_write( BLE_NVID_CSRK, KEYLEN, gapRole_SRK );
  885. // Save off the information
  886. VOID osal_memcpy( gapRole_bdAddr, pPkt->devAddr, B_ADDR_LEN );
  887. gapRole_state = GAPROLE_STARTED;
  888. // Update the advertising data
  889. stat = GAP_UpdateAdvertisingData( gapRole_TaskID,
  890. TRUE, gapRole_AdvertDataLen, gapRole_AdvertData );
  891. }
  892. if ( stat != SUCCESS )
  893. {
  894. gapRole_state = GAPROLE_ERROR;
  895. }
  896. notify = TRUE;
  897. }
  898. break;
  899. case GAP_ADV_DATA_UPDATE_DONE_EVENT:
  900. {
  901. gapAdvDataUpdateEvent_t *pPkt = (gapAdvDataUpdateEvent_t *)pMsg;
  902. if ( pPkt->hdr.status == SUCCESS )
  903. {
  904. if ( pPkt->adType )
  905. {
  906. // Setup the Response Data
  907. pPkt->hdr.status = GAP_UpdateAdvertisingData( gapRole_TaskID,
  908. FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
  909. }
  910. else if ( ( gapRole_state != GAPROLE_ADVERTISING ) &&
  911. ( osal_get_timeoutEx( gapRole_TaskID, START_ADVERTISING_EVT ) == 0 ) )
  912. {
  913. // Start advertising
  914. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  915. }
  916. }
  917. if ( pPkt->hdr.status != SUCCESS )
  918. {
  919. // Set into Error state
  920. gapRole_state = GAPROLE_ERROR;
  921. notify = TRUE;
  922. }
  923. }
  924. break;
  925. case GAP_MAKE_DISCOVERABLE_DONE_EVENT:
  926. case GAP_END_DISCOVERABLE_DONE_EVENT:
  927. {
  928. gapMakeDiscoverableRspEvent_t *pPkt = (gapMakeDiscoverableRspEvent_t *)pMsg;
  929. if ( pPkt->hdr.status == SUCCESS )
  930. {
  931. if ( pMsg->opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT )
  932. {
  933. if (gapRole_state == GAPROLE_CONNECTED)
  934. {
  935. gapRole_state = GAPROLE_CONNECTED_ADV;
  936. }
  937. else
  938. {
  939. gapRole_state = GAPROLE_ADVERTISING;
  940. }
  941. }
  942. else // GAP_END_DISCOVERABLE_DONE_EVENT
  943. {
  944. if ( gapRole_AdvertOffTime != 0 )
  945. {
  946. if ( ( gapRole_AdvEnabled ) )
  947. {
  948. VOID osal_start_timerEx( gapRole_TaskID, START_ADVERTISING_EVT, gapRole_AdvertOffTime );
  949. }
  950. }
  951. else
  952. {
  953. // Since gapRole_AdvertOffTime is set to 0, the device should not
  954. // automatically become discoverable again after a period of time.
  955. // Set enabler to FALSE; device will become discoverable again when
  956. // this value gets set to TRUE
  957. gapRole_AdvEnabled = FALSE;
  958. }
  959. if (gapRole_state == GAPROLE_CONNECTED_ADV)
  960. {
  961. // In the Advertising Off period
  962. gapRole_state = GAPROLE_CONNECTED;
  963. }
  964. else if (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT)
  965. {
  966. // Advertising was just turned off after the link disconnected so begin
  967. // advertising again.
  968. gapRole_AdvEnabled = TRUE;
  969. // Turn advertising back on.
  970. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
  971. }
  972. else
  973. {
  974. // In the Advertising Off period
  975. gapRole_state = GAPROLE_WAITING;
  976. }
  977. }
  978. }
  979. else
  980. {
  981. gapRole_state = GAPROLE_ERROR;
  982. }
  983. notify = TRUE;
  984. }
  985. break;
  986. case GAP_LINK_ESTABLISHED_EVENT:
  987. {
  988. gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;
  989. if ( pPkt->hdr.status == SUCCESS )
  990. {
  991. VOID osal_memcpy( gapRole_ConnectedDevAddr, pPkt->devAddr, B_ADDR_LEN );
  992. gapRole_ConnectionHandle = pPkt->connectionHandle;
  993. gapRole_ConnectionInterval = pPkt->connInterval;
  994. gapRole_ConnectionLatency = pPkt->connLatency;
  995. gapRole_state = GAPROLE_CONNECTED;
  996. //LOG("connect by[%02x%02x%02x%02x%02x%02x] handle[%d] interval[%d] latency[%d] timeout[%d]\n",
  997. // gapRole_ConnectedDevAddr[0], gapRole_ConnectedDevAddr[1], gapRole_ConnectedDevAddr[2],
  998. // gapRole_ConnectedDevAddr[3],gapRole_ConnectedDevAddr[4], gapRole_ConnectedDevAddr[5],
  999. // pPkt->connectionHandle, pPkt->connInterval,pPkt->connLatency, pPkt->connTimeout);
  1000. if ( gapRole_RSSIReadRate )
  1001. {
  1002. // Start the RSSI Reads
  1003. VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
  1004. }
  1005. // Store connection information
  1006. gapRole_ConnInterval = pPkt->connInterval;
  1007. gapRole_ConnSlaveLatency = pPkt->connLatency;
  1008. gapRole_ConnTimeout = pPkt->connTimeout;
  1009. // Check whether update parameter request is enabled
  1010. if ( gapRole_ParamUpdateEnable == TRUE )
  1011. {
  1012. // Get the minimum time upon connection establishment before the
  1013. // peripheral can start a connection update procedure.
  1014. uint16 timeout = GAP_GetParamValue( TGAP_CONN_PAUSE_PERIPHERAL );
  1015. osal_start_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT, timeout*1000 );
  1016. }
  1017. // Notify the Bond Manager to the connection
  1018. VOID GAPBondMgr_LinkEst( pPkt->devAddrType, pPkt->devAddr, pPkt->connectionHandle, GAP_PROFILE_PERIPHERAL );
  1019. // Set enabler to FALSE; device will become discoverable again when
  1020. // this value gets set to TRUE
  1021. gapRole_AdvEnabled = FALSE;
  1022. }
  1023. else if ( pPkt->hdr.status == bleGAPConnNotAcceptable )
  1024. {
  1025. // Set enabler to FALSE; device will become discoverable again when
  1026. // this value gets set to TRUE
  1027. gapRole_AdvEnabled = FALSE;
  1028. // Go to WAITING state, and then start advertising
  1029. gapRole_state = GAPROLE_WAITING;
  1030. }
  1031. else
  1032. {
  1033. gapRole_state = GAPROLE_ERROR;
  1034. }
  1035. notify = TRUE;
  1036. }
  1037. break;
  1038. case GAP_LINK_TERMINATED_EVENT:
  1039. {
  1040. gapTerminateLinkEvent_t *pPkt = (gapTerminateLinkEvent_t *)pMsg;
  1041. // VOID GAPBondMgr_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
  1042. osal_memset( gapRole_ConnectedDevAddr, 0, B_ADDR_LEN );
  1043. // Erase connection information
  1044. gapRole_ConnInterval = 0;
  1045. gapRole_ConnSlaveLatency = 0;
  1046. gapRole_ConnTimeout = 0;
  1047. // Cancel all connection parameter update timers (if any active)
  1048. VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
  1049. VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
  1050. // Go to WAITING state, and then start advertising
  1051. if( pPkt->reason == LL_SUPERVISION_TIMEOUT_TERM )
  1052. {
  1053. gapRole_state = GAPROLE_WAITING_AFTER_TIMEOUT;
  1054. }
  1055. else
  1056. {
  1057. gapRole_state = GAPROLE_WAITING;
  1058. }
  1059. AT_LOG("[DISC].reason %02x\n",pPkt->reason);
  1060. notify = TRUE;
  1061. //LOG("disconnected reason[%d]!\n", pPkt->reason);
  1062. // Check if still advertising from within last connection.
  1063. if ( gapRole_AdvEnabled)
  1064. {
  1065. // End advertising so we can restart advertising in order
  1066. // to change to connectable advertising from nonconnectable.
  1067. VOID GAP_EndDiscoverable( gapRole_TaskID );
  1068. }
  1069. else // Turn advertising back on.
  1070. {
  1071. gapRole_AdvEnabled = TRUE;
  1072. VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT);
  1073. }
  1074. gapRole_ConnectionHandle = INVALID_CONNHANDLE;
  1075. }
  1076. break;
  1077. case GAP_LINK_PARAM_UPDATE_EVENT:
  1078. {
  1079. gapLinkUpdateEvent_t *pPkt = (gapLinkUpdateEvent_t *)pMsg;
  1080. // Cancel connection param update timeout timer (if active)
  1081. VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
  1082. if ( pPkt->hdr.status == SUCCESS )
  1083. {
  1084. // Store new connection parameters
  1085. gapRole_ConnInterval = pPkt->connInterval;
  1086. gapRole_ConnSlaveLatency = pPkt->connLatency;
  1087. gapRole_ConnTimeout = pPkt->connTimeout;
  1088. // Make sure there's no pending connection update procedure
  1089. if ( osal_get_timeoutEx( gapRole_TaskID, START_CONN_UPDATE_EVT ) == 0 )
  1090. {
  1091. // Notify the application with the new connection parameters
  1092. if ( pGapRoles_ParamUpdateCB != NULL )
  1093. {
  1094. (*pGapRoles_ParamUpdateCB)( gapRole_ConnInterval,
  1095. gapRole_ConnSlaveLatency,
  1096. gapRole_ConnTimeout );
  1097. }
  1098. }
  1099. }
  1100. }
  1101. break;
  1102. default:
  1103. break;
  1104. }
  1105. #ifdef EXT_ADV_ENABLE
  1106. }
  1107. else if( SecondAdvEnable )
  1108. {
  1109. switch ( pMsg->opcode )
  1110. {
  1111. case GAP_DEVICE_INIT_DONE_EVENT:
  1112. if ( statsec == SUCCESS )
  1113. {
  1114. // Save off the information
  1115. VOID osal_memcpy( gapRole_bdAddr, pPktsec->devAddr, B_ADDR_LEN );
  1116. gapRole_state = GAPROLE_STARTED;
  1117. GapAdv_UpdateParameter(gapRole_bdAddr);
  1118. VOID osal_set_event( gapRole_TaskID, START_SECOND_ADVERTISING_EVENT );
  1119. }
  1120. if ( statsec != SUCCESS )
  1121. {
  1122. gapRole_state = GAPROLE_ERROR;
  1123. }
  1124. notify = TRUE;
  1125. break;
  1126. case GAP_LINK_ESTABLISHED_EVENT:
  1127. gapRole_state = GAPROLE_CONNECTED;
  1128. g_rfPhyPktFmt = PKT_FMT_BLR125K;
  1129. notify = TRUE;
  1130. break;
  1131. case GAP_LINK_TERMINATED_EVENT:
  1132. {
  1133. gapTerminateLinkEvent_t *pPkt = (gapTerminateLinkEvent_t *)pMsg;
  1134. g_rfPhyPktFmt = PKT_FMT_BLR125K;
  1135. // VOID GAPBondMgr_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
  1136. osal_memset( gapRole_ConnectedDevAddr, 0, B_ADDR_LEN );
  1137. // Erase connection information
  1138. gapRole_ConnInterval = 0;
  1139. gapRole_ConnSlaveLatency = 0;
  1140. gapRole_ConnTimeout = 0;
  1141. // Cancel all connection parameter update timers (if any active)
  1142. VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
  1143. VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
  1144. // Go to WAITING state, and then start advertising
  1145. if( pPkt->reason == LL_SUPERVISION_TIMEOUT_TERM )
  1146. {
  1147. gapRole_state = GAPROLE_WAITING_AFTER_TIMEOUT;
  1148. }
  1149. else
  1150. {
  1151. gapRole_state = GAPROLE_WAITING;
  1152. }
  1153. AT_LOG("[DISC].reason %02x\n",pPkt->reason);
  1154. notify = TRUE;
  1155. //LOG("disconnected reason[%d]!\n", pPkt->reason);
  1156. // Check if still advertising from within last connection.
  1157. // = FALSE , should set in GAP_END_DISCOVERABLE_DONE_EVENT , follow-up
  1158. gapRole_ExtAdvEnabled = FALSE;
  1159. if ( gapRole_ExtAdvEnabled)
  1160. {
  1161. // End advertising so we can restart advertising in order
  1162. // to change to connectable advertising from nonconnectable.
  1163. VOID GAP_EndDiscoverable( gapRole_TaskID );
  1164. }
  1165. else // Turn advertising back on.
  1166. {
  1167. gapRole_ExtAdvEnabled = TRUE;
  1168. VOID osal_set_event( gapRole_TaskID, START_SECOND_ADVERTISING_EVENT);
  1169. }
  1170. gapRole_ConnectionHandle = INVALID_CONNHANDLE;
  1171. }
  1172. break;
  1173. case GAP_LINK_PARAM_UPDATE_EVENT:
  1174. {
  1175. gapLinkUpdateEvent_t *pPkt = (gapLinkUpdateEvent_t *)pMsg;
  1176. // Cancel connection param update timeout timer (if active)
  1177. VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
  1178. if ( pPkt->hdr.status == SUCCESS )
  1179. {
  1180. // Store new connection parameters
  1181. gapRole_ConnInterval = pPkt->connInterval;
  1182. gapRole_ConnSlaveLatency = pPkt->connLatency;
  1183. gapRole_ConnTimeout = pPkt->connTimeout;
  1184. // Make sure there's no pending connection update procedure
  1185. if ( osal_get_timeoutEx( gapRole_TaskID, START_CONN_UPDATE_EVT ) == 0 )
  1186. {
  1187. // Notify the application with the new connection parameters
  1188. if ( pGapRoles_ParamUpdateCB != NULL )
  1189. {
  1190. (*pGapRoles_ParamUpdateCB)( gapRole_ConnInterval,
  1191. gapRole_ConnSlaveLatency,
  1192. gapRole_ConnTimeout );
  1193. }
  1194. }
  1195. }
  1196. }
  1197. break;
  1198. default:
  1199. break;
  1200. }
  1201. }
  1202. //AT_LOG(" SecondAdvEnable notify %d,pMsg->opcode %d,gapRole_state %d\n ",notify,pMsg->opcode,gapRole_state);
  1203. #endif
  1204. if ( notify == TRUE )
  1205. {
  1206. // Notify the application with the new state change
  1207. if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
  1208. {
  1209. pGapRoles_AppCGs->pfnStateChange( gapRole_state );
  1210. }
  1211. }
  1212. }
  1213. /*********************************************************************
  1214. * @fn gapRole_SetupGAP
  1215. *
  1216. * @brief Call the GAP Device Initialization function using the
  1217. * Profile Parameters.
  1218. *
  1219. * @param none
  1220. *
  1221. * @return none
  1222. */
  1223. static void gapRole_SetupGAP( void )
  1224. {
  1225. VOID GAP_DeviceInit( gapRole_TaskID,
  1226. gapRole_profileRole, 0,
  1227. gapRole_IRK, gapRole_SRK,
  1228. &gapRole_signCounter );
  1229. }
  1230. /*********************************************************************
  1231. * @fn gapRole_HandleParamUpdateNoSuccess
  1232. *
  1233. * @brief Handle unsuccessful connection parameters update.
  1234. *
  1235. * @param none
  1236. *
  1237. * @return none
  1238. */
  1239. static void gapRole_HandleParamUpdateNoSuccess( void )
  1240. {
  1241. // See which option was choosen for unsuccessful updates
  1242. switch ( paramUpdateNoSuccessOption )
  1243. {
  1244. case GAPROLE_RESEND_PARAM_UPDATE:
  1245. GAPRole_SendUpdateParam( gapRole_MinConnInterval, gapRole_MaxConnInterval,
  1246. gapRole_SlaveLatency, gapRole_TimeoutMultiplier,
  1247. GAPROLE_RESEND_PARAM_UPDATE );
  1248. break;
  1249. case GAPROLE_TERMINATE_LINK:
  1250. GAPRole_TerminateConnection();
  1251. break;
  1252. case GAPROLE_NO_ACTION:
  1253. // fall through
  1254. default:
  1255. //do nothing
  1256. break;
  1257. }
  1258. }
  1259. /********************************************************************
  1260. * @fn gapRole_startConnUpdate
  1261. *
  1262. * @brief Start the connection update procedure
  1263. *
  1264. * @param handleFailure - what to do if the update does not occur.
  1265. * Method may choose to terminate connection, try again, or take no action
  1266. *
  1267. * @return none
  1268. */
  1269. static void gapRole_startConnUpdate( uint8 handleFailure )
  1270. {
  1271. // First check the current connection parameters versus the configured parameters
  1272. if ( (gapRole_ConnInterval < gapRole_MinConnInterval) ||
  1273. (gapRole_ConnInterval > gapRole_MaxConnInterval) ||
  1274. (gapRole_ConnSlaveLatency != gapRole_SlaveLatency) ||
  1275. (gapRole_ConnTimeout != gapRole_TimeoutMultiplier) )
  1276. {
  1277. l2capParamUpdateReq_t updateReq;
  1278. uint16 timeout = GAP_GetParamValue( TGAP_CONN_PARAM_TIMEOUT );
  1279. updateReq.intervalMin = gapRole_MinConnInterval;
  1280. updateReq.intervalMax = gapRole_MaxConnInterval;
  1281. updateReq.slaveLatency = gapRole_SlaveLatency;
  1282. updateReq.timeoutMultiplier = gapRole_TimeoutMultiplier;
  1283. L2CAP_ConnParamUpdateReq( gapRole_ConnectionHandle, &updateReq, gapRole_TaskID );
  1284. paramUpdateNoSuccessOption = handleFailure;
  1285. // Let's wait for L2CAP Connection Parameters Update Response
  1286. VOID osal_start_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT, timeout );
  1287. }
  1288. }
  1289. /********************************************************************
  1290. * @fn GAPRole_SendUpdateParam
  1291. *
  1292. * @brief Update the parameters of an existing connection
  1293. *
  1294. * @param minConnInterval - the new min connection interval
  1295. * @param maxConnInterval - the new max connection interval
  1296. * @param latency - the new slave latency
  1297. * @param connTimeout - the new timeout value
  1298. * @param handleFailure - what to do if the update does not occur.
  1299. * Method may choose to terminate connection, try again, or take no action
  1300. *
  1301. * @return SUCCESS, bleNotConnected, or bleInvalidRange
  1302. */
  1303. bStatus_t GAPRole_SendUpdateParam( uint16 minConnInterval, uint16 maxConnInterval,
  1304. uint16 latency, uint16 connTimeout, uint8 handleFailure )
  1305. {
  1306. // If there is no existing connection no update need be sent
  1307. if ( gapRole_state != GAPROLE_CONNECTED )
  1308. {
  1309. return ( bleNotConnected );
  1310. }
  1311. // Check that all parameters are in range before sending request
  1312. if ( ( minConnInterval >= DEFAULT_MIN_CONN_INTERVAL ) &&
  1313. ( minConnInterval < DEFAULT_MAX_CONN_INTERVAL ) &&
  1314. ( maxConnInterval >= DEFAULT_MIN_CONN_INTERVAL ) &&
  1315. ( maxConnInterval < DEFAULT_MAX_CONN_INTERVAL ) &&
  1316. ( latency < MAX_SLAVE_LATENCY ) &&
  1317. ( connTimeout >= MIN_TIMEOUT_MULTIPLIER ) &&
  1318. ( connTimeout < MAX_TIMEOUT_MULTIPLIER ) )
  1319. {
  1320. gapRole_MinConnInterval = minConnInterval;
  1321. gapRole_MaxConnInterval = maxConnInterval;
  1322. gapRole_SlaveLatency = latency;
  1323. gapRole_TimeoutMultiplier = connTimeout;
  1324. // Start connection update procedure
  1325. gapRole_startConnUpdate( handleFailure );
  1326. // Connection update requested by app, cancel such pending procedure (if active)
  1327. VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
  1328. return ( SUCCESS );
  1329. }
  1330. return ( bleInvalidRange );
  1331. }
  1332. #ifdef EXT_ADV_ENABLE
  1333. // exdended advertising
  1334. bStatus_t GapAdv_create(pfnGapCB_t *cb, Gap_ExtAdv_Param *advParam,uint8 *advHandle)
  1335. {
  1336. return(HCI_LE_SetExtAdvParamCmd(advParam));
  1337. }
  1338. bStatus_t GapAdv_loadByHandle(uint8 handle, GapAdv_dataTypes_t dataType,uint16 len, uint8 *pBuf)
  1339. {
  1340. switch (dataType)
  1341. {
  1342. case GAP_ADV_DATA_TYPE_ADV:
  1343. HCI_LE_SetExtAdvDataCmd(handle,len,pBuf);
  1344. break;
  1345. case GAP_ADV_DATA_TYPE_SCAN_RSP:
  1346. HCI_LE_SetExtScanRspDataCmd(handle,len,pBuf);
  1347. break;
  1348. default:
  1349. break;
  1350. }
  1351. return HCI_SUCCESS;
  1352. }
  1353. bStatus_t GapAdv_setEventMask(uint8 handle, GapAdv_eventMaskFlags_t mask)
  1354. {
  1355. return HCI_SUCCESS;
  1356. }
  1357. bStatus_t GapAdv_enable(uint8 handle,
  1358. GapAdv_enableOptions_t enableOptions,
  1359. uint16 durationOrMaxEvents)
  1360. {
  1361. Gap_ExtAdv_EnableParam param;
  1362. param.Enable = TRUE;
  1363. param.AdvHandle = handle;
  1364. switch (enableOptions)
  1365. {
  1366. case GAP_ADV_ENABLE_OPTIONS_USE_MAX:
  1367. param.Duration = enableOptions;
  1368. param.MaxExtAdv_Events = durationOrMaxEvents;
  1369. break;
  1370. case GAP_ADV_ENABLE_OPTIONS_USE_DURATION:
  1371. break;
  1372. case GAP_ADV_ENABLE_OPTIONS_USE_MAX_EVENTS:
  1373. break;
  1374. default:
  1375. break;
  1376. }
  1377. HCI_LE_SetExtAdvEnableCmd(&param);
  1378. return HCI_SUCCESS;
  1379. }
  1380. #endif
  1381. /*********************************************************************
  1382. *********************************************************************/