blebrr.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /**
  2. * \file blebrr.c
  3. *
  4. * This File contains the BLE Bearer interface for the
  5. * Mindtree Mesh stack.
  6. */
  7. /*
  8. * Copyright (C) 2016. Mindtree Ltd.
  9. * All rights reserved.
  10. */
  11. /* ------------------------------- Header File Inclusion */
  12. #include "MS_brr_api.h"
  13. #include "MS_prov_api.h"
  14. #include "MS_access_api.h"
  15. #include "blebrr.h"
  16. #include "ll.h"
  17. #include "MS_trn_api.h"
  18. #include "pwrmgr.h"
  19. #include "ll_sleep.h"
  20. #include "led_light.h"
  21. /* --------------------------------------------- External Global Variables */
  22. /* --------------------------------------------- Global Definitions */
  23. #define BLEBRR_MAX_ADV_FILTER_LIST_COUNT 100
  24. #define BLEBRR_MAX_ADV_DATA_SIZE 31
  25. #define BLEBRR_BCON_ELEMENTS 2
  26. #define BLEBRR_ACTIVEADV_TIMEOUT 1 /* Second */
  27. #define BLEBRR_ADV_TIMEOUT 6 /* Millisecond */
  28. #define BLEBRR_SCAN_TIMEOUT (EM_TIMEOUT_MILLISEC | 30) /* Millisecond */
  29. #define BLEBRR_NCON_ADVTYPE_OFFSET 2
  30. #define BLEBRR_ADVREPEAT_PRO_COUNT 2
  31. #define BLEBRR_ADVREPEAT_NET_COUNT 2
  32. #define BLEBRR_SCAN_ADJ_STEP 30
  33. #define BLEBRR_SCAN_ADJ_THD_MAX 45//90
  34. #define BLEBRR_SCAN_ADJ_THD_MIN 15//90
  35. #define BLEBRR_TURNOFF_RELAY_THD 32
  36. #define BLEBRR_SKIP_BEACON_QUEUE_DEPTH 0
  37. #define BLEBRR_BCON_READY_TIME 10
  38. /** Bearer Queue defines */
  39. #define BLEBRR_QTYPE_DATA 0x00
  40. #define BLEBRR_QTYPE_BEACON 0x01
  41. #define BLEBRR_NUM_QUEUES 0x02
  42. /** Beacon type defines */
  43. #define BLEBRR_UPROV_ADV_BCON 0x00
  44. #define BLEBRR_UPROV_ADV_URI 0x01
  45. #define BLEBRR_UPROV_GATT_BCON 0x02
  46. #define BLEBRR_UPROV_GATT_URI 0x03
  47. #define BLEBRR_SECNET_BCON 0x04
  48. #define BLEBRR_NUM_BCONS 0x05
  49. /** GATT Mode GAP Connectable Advertising Service data offset */
  50. #define BLEBRR_GATT_ADV_SERV_DATA_OFFSET 11
  51. #define BLEBRR_GATT_ADV_SERV_DATALEN_OFFSET 7
  52. /** Advertising data maximum length */
  53. #define BLEBRR_GAP_ADVDATA_LEN 31
  54. /** Advertising data sets MAX */
  55. #define BLEBRR_GAP_MAX_ADVDATA_SETS 2
  56. #ifdef BLEBRR_LP_SUPPORT
  57. UCHAR blebrr_lp_flag = MS_FALSE;
  58. #define BLEBRR_LP_UNPROVISION_TIMEOUT 10*60 //unprovison timeout 10min
  59. #define BLEBRR_LP_PROVISIONED_TIMEOUT 1200 //
  60. #define BLEBRR_LP_PROVISIONED_WKP_TIMEOUT 60 //
  61. #define BLEBRR_LP_PROVISIONED_SLP_TIMEOUT (BLEBRR_LP_PROVISIONED_TIMEOUT-BLEBRR_LP_PROVISIONED_WKP_TIMEOUT)
  62. #endif
  63. /* --------------------------------------------- Macros */
  64. #define BLEBRR_MUTEX_INIT() MS_MUTEX_INIT(blebrr_mutex, BRR);
  65. #define BLEBRR_MUTEX_INIT_VOID() MS_MUTEX_INIT_VOID(blebrr_mutex, BRR);
  66. #define BLEBRR_LOCK() MS_MUTEX_LOCK(blebrr_mutex, BRR);
  67. #define BLEBRR_LOCK_VOID() MS_MUTEX_LOCK_VOID(blebrr_mutex, BRR);
  68. #define BLEBRR_UNLOCK() MS_MUTEX_UNLOCK(blebrr_mutex, BRR);
  69. #define BLEBRR_UNLOCK_VOID() MS_MUTEX_UNLOCK_VOID(blebrr_mutex, BRR);
  70. /* --------------------------------------------- Structures/Data Types */
  71. /** BLEBRR Data Queue Element */
  72. typedef struct _BLEBRR_Q_ELEMENT
  73. {
  74. /* "Allocated" Data Pointer */
  75. UCHAR *pdata;
  76. /*
  77. * Data Length. If data length is zero, the element is considered
  78. * invalid.
  79. */
  80. UINT16 pdatalen;
  81. /* Type of data element */
  82. UCHAR type;
  83. } BLEBRR_Q_ELEMENT;
  84. /** BLEBRR Data Queue */
  85. typedef struct _BLEBRR_Q
  86. {
  87. /* List of Bearer Queue elements */
  88. BLEBRR_Q_ELEMENT element[BLEBRR_QUEUE_SIZE];
  89. /* Queue start index */
  90. UINT16 start;
  91. /* Queue end index */
  92. UINT16 end;
  93. } BLEBRR_Q;
  94. /** Advertising Data type */
  95. typedef struct _BLEBRR_GAP_ADV_DATA
  96. {
  97. /** Data */
  98. UCHAR data[BLEBRR_GAP_ADVDATA_LEN];
  99. /** Data Length */
  100. UCHAR datalen;
  101. } BLEBRR_GAP_ADV_DATA;
  102. /* --------------------------------------------- Global Variables */
  103. #ifdef BLEBRR_LP_SUPPORT
  104. EM_timer_handle blebrr_lp_thandle;
  105. #endif
  106. #ifdef BLEBRR_FILTER_DUPLICATE_PACKETS
  107. DECL_STATIC UCHAR blebrr_adv_list[BLEBRR_MAX_ADV_FILTER_LIST_COUNT][BLEBRR_MAX_ADV_DATA_SIZE];
  108. DECL_STATIC UCHAR blebrr_adv_list_inser_index = 0;
  109. #endif /* BLEBRR_FILTER_DUPLICATE_PACKETS */
  110. BRR_BEARER_INFO blebrr_adv; //HZF
  111. DECL_STATIC BRR_HANDLE blebrr_advhandle;
  112. DECL_STATIC UCHAR blebrr_bconidx;
  113. #ifdef BLEBRR_LP_SUPPORT
  114. DECL_STATIC UCHAR blebrr_beacon;
  115. #endif
  116. DECL_STATIC UCHAR blebrr_update_advcount = BLEBRR_BCON_READY_TIME;
  117. DECL_STATIC BLEBRR_Q_ELEMENT blebrr_bcon[BRR_BCON_COUNT];
  118. DECL_STATIC BLEBRR_Q blebrr_queue;
  119. MS_DEFINE_MUTEX_TYPE(static, blebrr_mutex)
  120. DECL_STATIC EM_timer_handle blebrr_timer_handle;
  121. UCHAR blebrr_state; // HZF
  122. //DECL_STATIC UCHAR blebrr_state;
  123. /* Set provision started */
  124. UCHAR blebrr_prov_started;
  125. UCHAR blebrr_adv_restart;
  126. UINT32 blebrr_scanTimeOut = 100;
  127. DECL_STATIC UCHAR blebrr_datacount;
  128. /* DECL_STATIC UCHAR blebrr_scan_type; */
  129. DECL_STATIC UCHAR blebrr_advrepeat_count;
  130. DECL_STATIC UCHAR blebrr_scan_interleave;
  131. BLEBRR_GAP_ADV_DATA blebrr_gap_adv_data[BLEBRR_GAP_MAX_ADVDATA_SETS] =
  132. {
  133. /* Index 0x00: Mesh Provisioning Service ADV Data */
  134. {
  135. {
  136. /**
  137. * Flags:
  138. * 0x01: LE Limited Discoverable Mode
  139. * 0x02: LE General Discoverable Mode
  140. * 0x04: BR/EDR Not Supported
  141. * 0x08: Simultaneous LE and BR/EDR to Same Device
  142. * Capable (Controller)
  143. * 0x10: Simultaneous LE and BR/EDR to Same Device
  144. * Capable (Host)
  145. */
  146. 0x02, 0x01, 0x06,
  147. /**
  148. * Service UUID List:
  149. * Mesh Provisioning Service (0x1827)
  150. */
  151. 0x03, 0x03, 0x27, 0x18,
  152. /**
  153. * Service Data List:
  154. * Mesh Provisioning Service (0x1827)
  155. * Mesh UUID (16 Bytes)
  156. * Mesh OOB Info (2 Bytes)
  157. */
  158. 0x15, 0x16,
  159. 0x27, 0x18,
  160. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  161. 0x00, 0x00
  162. },
  163. /** Advertising Data length */
  164. 29
  165. },
  166. /* Index 0x01: Mesh Proxy Service ADV Data */
  167. {
  168. {
  169. /**
  170. * Flags:
  171. * 0x01: LE Limited Discoverable Mode
  172. * 0x02: LE General Discoverable Mode
  173. * 0x04: BR/EDR Not Supported
  174. * 0x08: Simultaneous LE and BR/EDR to Same Device
  175. * Capable (Controller)
  176. * 0x10: Simultaneous LE and BR/EDR to Same Device
  177. * Capable (Host)
  178. */
  179. 0x02, 0x01, 0x06,
  180. /**
  181. * Service UUID List:
  182. * Mesh Proxy Service (0x1828)
  183. */
  184. 0x03, 0x03, 0x28, 0x18,
  185. /**
  186. * Service Data List:
  187. * Mesh Provisioning Service (0x1828)
  188. * Type (1 Byte) "0x00 - Network ID; 0x01 - Node Identity"
  189. * NetWork ID (8 Bytes)
  190. */
  191. 0x0C, 0x16,
  192. 0x28, 0x18,
  193. 0x00,
  194. 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88
  195. },
  196. /** Advertising Data length */
  197. 20
  198. }
  199. };
  200. DECL_STATIC UCHAR pl_advdata_offset;
  201. UCHAR blebrr_sleep;
  202. // ------------ add by HZF
  203. uint32 blebrr_advscan_timeout_count = 0;
  204. extern uint32_t osal_sys_tick;
  205. /* ------------------------------- Functions */
  206. void blebrr_handle_evt_adv_complete (UINT8 enable);
  207. DECL_STATIC void blebrr_timer_start (UINT32 timeout);
  208. DECL_STATIC void blebrr_timer_stop (void);
  209. API_RESULT blebrr_queue_depth_check(void);
  210. /**
  211. * \brief
  212. *
  213. * \par Description
  214. *
  215. *
  216. * \return void
  217. */
  218. void blebrr_scan_enable(void)
  219. {
  220. BLEBRR_LOCK_VOID();
  221. if ((BLEBRR_STATE_IDLE == BLEBRR_GET_STATE()) &&
  222. (MS_TRUE != blebrr_sleep))
  223. {
  224. blebrr_scan_pl(MS_TRUE);
  225. /* Update state */
  226. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
  227. }
  228. BLEBRR_UNLOCK_VOID();
  229. }
  230. /**
  231. * \brief
  232. *
  233. * \par Description
  234. *
  235. *
  236. * \param type
  237. * \param bcon
  238. *
  239. * \return void
  240. */
  241. DECL_STATIC UCHAR blebrr_get_beacon_type (UCHAR type, UCHAR bcon)
  242. {
  243. return (BRR_BCON_PASSIVE == type)?
  244. ((BRR_BCON_TYPE_UNPROV_DEVICE == bcon)? BLEBRR_UPROV_ADV_BCON: BLEBRR_SECNET_BCON):
  245. ((BRR_BCON_TYPE_UNPROV_DEVICE == bcon)? BLEBRR_UPROV_GATT_BCON: BLEBRR_NUM_BCONS);
  246. }
  247. /**
  248. * \brief
  249. *
  250. * \par Description
  251. *
  252. *
  253. * \param type
  254. *
  255. * \return void
  256. */
  257. DECL_STATIC BLEBRR_Q_ELEMENT * blebrr_enqueue_alloc (void)
  258. {
  259. BLEBRR_Q_ELEMENT * elt;
  260. UINT16 ei;
  261. /* Get reference to the requested Queue block members */
  262. elt = blebrr_queue.element;
  263. ei = blebrr_queue.end;
  264. /* Check if queue end element is free */
  265. if (0 != (elt + ei)->pdatalen)
  266. {
  267. /* Not free */
  268. elt = NULL;
  269. }
  270. else
  271. {
  272. /* Set the element to be returned */
  273. elt = (elt + ei);
  274. /* Update the data availability */
  275. // blebrr_datacount++;
  276. /* EM_debug_trace (0, "[BLEBRR] Enqueue at Q Index: %d\n", ei); */
  277. /* Update queue end */
  278. if(ei == BLEBRR_QUEUE_SIZE - 1)
  279. ei = 0;
  280. else
  281. ei++;
  282. // ei++;
  283. // ei &= (BLEBRR_QUEUE_SIZE - 1);
  284. blebrr_queue.end = ei;
  285. }
  286. return elt;
  287. }
  288. DECL_STATIC void blebrr_dequeue_manual (void)
  289. {
  290. UINT16 ei;
  291. ei = blebrr_queue.end;
  292. /* Update the data availability */
  293. // blebrr_datacount--;
  294. /* EM_debug_trace (0, "[BLEBRR] Enqueue at Q Index: %d\n", ei); */
  295. // printf ("[BLEBRR] Dequeue at Q Index: __%d\n", ei);
  296. /* Update queue end */
  297. if(ei == 0)
  298. ei = BLEBRR_QUEUE_SIZE - 1;
  299. else
  300. ei--;
  301. blebrr_queue.end = ei;
  302. }
  303. /**
  304. * \brief
  305. *
  306. * \par Description
  307. *
  308. *
  309. * \param type
  310. *
  311. * \return void
  312. */
  313. DECL_STATIC BLEBRR_Q_ELEMENT * blebrr_dequeue (void)
  314. {
  315. BLEBRR_Q_ELEMENT * elt;
  316. UINT16 si;
  317. /* Get reference to the requested Queue block members */
  318. elt = blebrr_queue.element;
  319. si = blebrr_queue.start;
  320. /* Check if queue start element is valid */
  321. if (0 == (elt + si)->pdatalen)
  322. {
  323. /* Not valid */
  324. elt = NULL;
  325. }
  326. else
  327. {
  328. /* Set the element to be returned */
  329. elt = (elt + si);
  330. /* EM_debug_trace (0, "[BLEBRR] Dequeue at Q Index: %d\n", si); */
  331. /* Is Adv data type in element? */
  332. if (BRR_BCON_COUNT == elt->type)
  333. {
  334. /* Update the data availability */
  335. blebrr_datacount--;
  336. }
  337. /* Update the data availability */
  338. // blebrr_datacount--;
  339. /* Update queue start */
  340. if(si == BLEBRR_QUEUE_SIZE - 1)
  341. si = 0;
  342. else
  343. si++;
  344. // si++;
  345. // si &= (BLEBRR_QUEUE_SIZE - 1);
  346. blebrr_queue.start = si;
  347. }
  348. return elt;
  349. }
  350. /**
  351. * \brief
  352. *
  353. * \par Description
  354. *
  355. *
  356. * \param bcon
  357. *
  358. * \return void
  359. */
  360. DECL_STATIC void blebrr_clear_bcon (UCHAR bconidx)
  361. {
  362. BLEBRR_Q_ELEMENT * elt;
  363. /* Get reference to the beacon queue element */
  364. elt = &blebrr_bcon[bconidx];
  365. /* Clear the element and the next one for the given type of beacon */
  366. if (NULL != elt->pdata)
  367. {
  368. EM_free_mem (elt->pdata);
  369. elt->pdata = NULL;
  370. elt->pdatalen = 0;
  371. elt->type = BRR_BCON_COUNT;
  372. if ((BRR_BCON_TYPE_UNPROV_DEVICE == bconidx) &&
  373. (NULL != (elt + 1)->pdata) &&
  374. (0 != (elt + 1)->pdatalen))
  375. {
  376. EM_free_mem((elt + 1)->pdata);
  377. (elt + 1)->pdata = NULL;
  378. (elt + 1)->pdatalen = 0;
  379. (elt + 1)->type = BRR_BCON_COUNT;
  380. }
  381. blebrr_datacount--;
  382. }
  383. }
  384. UCHAR blebrr_get_queue_depth(void)
  385. {
  386. UCHAR depth;
  387. if(blebrr_queue.end>=blebrr_queue.start)
  388. {
  389. depth = blebrr_queue.end-blebrr_queue.start;
  390. }
  391. else
  392. {
  393. depth = BLEBRR_QUEUE_SIZE-(blebrr_queue.start-blebrr_queue.end);
  394. }
  395. return depth;
  396. }
  397. API_RESULT blebrr_queue_depth_check(void)
  398. {
  399. API_RESULT retval =API_SUCCESS;
  400. uint8_t randData;
  401. UCHAR depth =blebrr_get_queue_depth();
  402. if(depth>BLEBRR_TURNOFF_RELAY_THD)
  403. {
  404. LL_Rand(&randData, 1);
  405. randData=randData>>1;
  406. if( depth > randData)
  407. {
  408. retval= API_FAILURE;
  409. }
  410. BLEBRR_LOG("[Queue DATA CNT] = %d %d %4X\n", depth,randData,retval);
  411. }
  412. return retval;
  413. }
  414. extern uint8 llState, llSecondaryState;
  415. /**
  416. * \brief
  417. *
  418. * \par Description
  419. *
  420. * *
  421. * * \param void
  422. *
  423. * \return void
  424. */
  425. DECL_STATIC API_RESULT blebrr_update_advdata(void)
  426. {
  427. BLEBRR_Q_ELEMENT * elt;
  428. UCHAR type;
  429. elt = NULL;
  430. UCHAR is_proxy_beacon;
  431. is_proxy_beacon = 1;
  432. //ZQY skip bcon adv when queue is not empty
  433. // printf("blebrr_get_queue_depth:%d\n",blebrr_get_queue_depth());
  434. if(blebrr_update_advcount < BLEBRR_BCON_READY_TIME)
  435. {
  436. is_proxy_beacon = 0;
  437. blebrr_update_advcount++;
  438. }
  439. else
  440. {
  441. blebrr_update_advcount = 0;
  442. }
  443. if(blebrr_get_queue_depth()>BLEBRR_SKIP_BEACON_QUEUE_DEPTH)
  444. {
  445. is_proxy_beacon = 0;
  446. }
  447. if (is_proxy_beacon)
  448. {
  449. UCHAR bconidx;
  450. bconidx = blebrr_bconidx;
  451. do
  452. {
  453. if (0 != blebrr_bcon[blebrr_bconidx].pdatalen)
  454. {
  455. elt = &blebrr_bcon[blebrr_bconidx];
  456. is_proxy_beacon = 0;
  457. }
  458. if (BRR_BCON_COUNT == ++blebrr_bconidx)
  459. {
  460. blebrr_bconidx = 0;
  461. }
  462. } while ((blebrr_bconidx != bconidx) && (NULL == elt));
  463. }
  464. if (!is_proxy_beacon && NULL == elt)
  465. {
  466. elt = blebrr_dequeue();
  467. is_proxy_beacon = 1;
  468. }
  469. if (NULL == elt)
  470. {
  471. return API_FAILURE;
  472. }
  473. /* Set the type */
  474. type = (BRR_BCON_COUNT == elt->type) ? BRR_BCON_PASSIVE : elt->type;
  475. /* Set the advertising data */
  476. blebrr_advrepeat_count = 1;
  477. blebrr_advertise_data_pl(type, elt->pdata, elt->pdatalen);
  478. /* Is Adv data type in element? */
  479. if (BRR_BCON_COUNT == elt->type)
  480. {
  481. #ifdef BLEBRR_LP_SUPPORT
  482. blebrr_beacon = 0;
  483. #endif
  484. /* Yes, Free the element */
  485. EM_free_mem(elt->pdata);
  486. elt->pdatalen = 0;
  487. }
  488. #ifdef BLEBRR_LP_SUPPORT
  489. else
  490. {
  491. blebrr_beacon = 1;
  492. }
  493. #endif
  494. return API_SUCCESS;
  495. }
  496. #if 0
  497. DECL_STATIC void blebrr_timer_restart (UINT32 timeout);
  498. #endif
  499. /**
  500. * \brief
  501. *
  502. * \par Description
  503. *
  504. *
  505. * \param type
  506. * \param pdata
  507. * \param datalen
  508. * \param elt
  509. *
  510. * \return void
  511. */
  512. DECL_STATIC API_RESULT blebrr_send
  513. (
  514. UCHAR type,
  515. void * pdata,
  516. UINT16 datalen,
  517. BLEBRR_Q_ELEMENT * elt
  518. )
  519. {
  520. API_RESULT retval;
  521. UCHAR * data;
  522. UINT16 packet_len;
  523. UCHAR offset;
  524. data = (UCHAR *)pdata;
  525. /* BLEBRR_LOG("[ADV-Tx >]: ");
  526. BLEBRR_dump_bytes(data, datalen); */
  527. /* Get the offset based on the type */
  528. offset = (0 != type)? BLEBRR_NCON_ADVTYPE_OFFSET: 0;
  529. /* Calculate the total length, including Adv Data Type headers */
  530. packet_len = datalen + offset;
  531. /* Allocate and save the data */
  532. elt->pdata = EM_alloc_mem(packet_len);
  533. if (NULL == elt->pdata)
  534. {
  535. // BLEBRR_LOG("Failed to allocate memory!\n");
  536. return API_FAILURE;
  537. }
  538. if (offset >= 1)
  539. {
  540. /* Add the Length and Adv type headers */
  541. elt->pdata[0] = (UCHAR)(datalen + (offset - 1));
  542. if (offset - 1)
  543. {
  544. elt->pdata[1] = type;
  545. }
  546. }
  547. /* Update the data and datalen */
  548. EM_mem_copy((elt->pdata + offset), data, datalen);
  549. elt->pdatalen = packet_len;
  550. /* Is the Adv/Scan timer running? */
  551. if (EM_TIMER_HANDLE_INIT_VAL != blebrr_timer_handle)
  552. {
  553. /* Yes. Do nothing */
  554. if (BLEBRR_STATE_SCAN_ENABLED == BLEBRR_GET_STATE())
  555. {
  556. retval = EM_stop_timer(blebrr_timer_handle);
  557. blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
  558. if(retval == EM_SUCCESS)
  559. blebrr_scan_pl (MS_FALSE);
  560. }
  561. }
  562. else
  563. {
  564. /*
  565. * No. Scan must be enabled. Disable it to trigger alternating
  566. * Adv/Scan Procedure
  567. */
  568. if (BLEBRR_STATE_SCAN_ENABLED == BLEBRR_GET_STATE())
  569. {
  570. // printf ("Trigger Tx...\r\n");
  571. blebrr_scan_pl (MS_FALSE);
  572. /* Update state */
  573. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_DISABLE);
  574. }
  575. else if (BLEBRR_STATE_ADV_ENABLED == BLEBRR_GET_STATE())
  576. {
  577. blebrr_adv_restart = MS_TRUE;
  578. blebrr_timer_stop();
  579. /* Disable Advertising */
  580. blebrr_advertise_pl(MS_FALSE);
  581. /* Update state */
  582. BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_DISABLE);
  583. }
  584. else if (BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
  585. {
  586. /* No, Enable Advertising with Data */
  587. retval = blebrr_update_advdata();
  588. if (API_SUCCESS != retval)
  589. {
  590. /* Enable Scan */
  591. blebrr_scan_pl(MS_TRUE);
  592. /* Update state */
  593. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
  594. }
  595. else
  596. {
  597. /* Update state */
  598. BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
  599. }
  600. }
  601. }
  602. return API_SUCCESS;
  603. }
  604. /**
  605. * \brief
  606. *
  607. * \par Description
  608. *
  609. *
  610. * \param handle
  611. * \param pdata
  612. * \param datalen
  613. *
  614. * \return void
  615. */
  616. DECL_STATIC API_RESULT blebrr_bcon_send(BRR_HANDLE * handle, void * pdata, UINT16 datalen)
  617. {
  618. BRR_BEACON_INFO * info;
  619. BLEBRR_Q_ELEMENT * elt;
  620. UCHAR op, action, type, bcon, bcontype;
  621. UCHAR bconidx;
  622. /* Get the beacon information */
  623. info = (BRR_BEACON_INFO *)pdata;
  624. /* Get the Operation and Action */
  625. op = (info->action & 0x0F);
  626. action = ((info->action & 0xF0) >> 4);
  627. /* Get the Broadcast/Observe type and Beacon type */
  628. type = (info->type & 0x0F);
  629. bcon = ((info->type & 0xF0) >> 4);
  630. /* Lock */
  631. BLEBRR_LOCK();
  632. /* Check the operations */
  633. switch (op)
  634. {
  635. case BRR_OBSERVE:
  636. /* blebrr_scan_type = type; */
  637. break;
  638. case BRR_BROADCAST:
  639. /* Get the Beacon mapping at the BLEBRR */
  640. bcontype = blebrr_get_beacon_type (type, bcon);
  641. /* Set the bcon index */
  642. bconidx = bcon;
  643. if (BRR_ENABLE == action)
  644. {
  645. /* Update the connectable beacon packet */
  646. if ((BRR_BCON_ACTIVE == type) && ((NULL != info->bcon_data)))
  647. {
  648. /* Active Beacon (advdata) Source Index */
  649. UCHAR abs_index;
  650. abs_index = blebrr_gatt_mode_get();
  651. if (BLEBRR_GATT_PROV_MODE == abs_index)
  652. {
  653. /* Copy the incoming UUID and OOB info to global connectable ADV data for PB GATT */
  654. /* TODO have a state to decide about provisioned and unprovisioned state */
  655. EM_mem_copy
  656. (
  657. blebrr_gap_adv_data[abs_index].data + BLEBRR_GATT_ADV_SERV_DATA_OFFSET,
  658. info->bcon_data + 1,
  659. 16 + 2
  660. );
  661. /**
  662. * NOTE: It is not need to calculate assign the Service Data Length as
  663. * Service Data length is Fixed for Connectable Provisioning ADV.
  664. * This data length is : 1 + 2 + 16 + 2 = 0x15 Bytes, already updated
  665. * in the global data strucutre blebrr_gap_adv_data[0].
  666. */
  667. /* Disable Interleaving */
  668. blebrr_scan_interleave = MS_FALSE;
  669. }
  670. /* Assuming that this Active Beacon is for GATT Proxy*/
  671. else
  672. {
  673. /* Copy the incoming UUID and OOB info to global connectable ADV data for PB GATT */
  674. /* TODO have a state to decide about provisioned and unprovisioned state */
  675. abs_index = BLEBRR_GATT_PROXY_MODE;
  676. /* Copy the incoming Proxy ADV data */
  677. EM_mem_copy
  678. (
  679. blebrr_gap_adv_data[abs_index].data + BLEBRR_GATT_ADV_SERV_DATA_OFFSET,
  680. info->bcon_data,
  681. info->bcon_datalen
  682. );
  683. /* Copy the incoming Proxy ADV datalen + the BLEBRR_GATT_ADV_SERV_DATA_OFFSET */
  684. blebrr_gap_adv_data[abs_index].datalen = BLEBRR_GATT_ADV_SERV_DATA_OFFSET + info->bcon_datalen;
  685. /**
  686. * Assign the service data length correctly for Proxy ADVs
  687. * Total incoming data + 1 Byte of AD Flags + 2 Bytes of Service UUID
  688. */
  689. blebrr_gap_adv_data[abs_index].data[BLEBRR_GATT_ADV_SERV_DATALEN_OFFSET] =
  690. info->bcon_datalen + 1 + 2;
  691. }
  692. /* Re-assign updated ADV data to Info Structure */
  693. info->bcon_data = blebrr_gap_adv_data[abs_index].data + pl_advdata_offset;
  694. info->bcon_datalen = blebrr_gap_adv_data[abs_index].datalen - pl_advdata_offset;
  695. }
  696. else
  697. {
  698. /* Enable Interleaving */
  699. blebrr_scan_interleave = MS_TRUE;
  700. }
  701. /* Check if beacon element is free */
  702. if (0 != blebrr_bcon[bconidx].pdatalen)
  703. {
  704. /* Unlock */
  705. BLEBRR_UNLOCK();
  706. BLEBRR_LOG("Beacon Not Free!\n");
  707. return API_FAILURE;
  708. }
  709. elt = &blebrr_bcon[bconidx];
  710. blebrr_datacount++;
  711. /* Update element type */
  712. elt->type = type;
  713. /* Schedule to send */
  714. blebrr_send
  715. (
  716. ((BRR_BCON_TYPE_UNPROV_DEVICE == bcon) &&
  717. (BRR_BCON_ACTIVE != type))? MESH_AD_TYPE_BCON : 0,
  718. info->bcon_data,
  719. info->bcon_datalen,
  720. elt
  721. );
  722. /* Check if URI data is present for Unprovisioned device */
  723. if ((BRR_BCON_TYPE_UNPROV_DEVICE == bconidx) &&
  724. (NULL != info->uri) &&
  725. (NULL != info->uri->payload) &&
  726. (0 != info->uri->length))
  727. {
  728. elt = &blebrr_bcon[bconidx + 1];
  729. /* Update element type */
  730. elt->type = bcontype + 1;
  731. /* Schedule to send */
  732. blebrr_send
  733. (
  734. 0,
  735. info->uri->payload,
  736. info->uri->length,
  737. elt
  738. );
  739. }
  740. }
  741. else
  742. {
  743. /* Remove the beacon with type from the queue */
  744. blebrr_clear_bcon (bconidx);
  745. }
  746. break;
  747. default:
  748. break;
  749. }
  750. /* Unlock */
  751. BLEBRR_UNLOCK();
  752. return API_SUCCESS;
  753. }
  754. /**
  755. * \brief
  756. *
  757. * \par Description
  758. *
  759. *
  760. * \param handle
  761. * \param pdata
  762. * \param datalen
  763. *
  764. * \return void
  765. */
  766. DECL_STATIC API_RESULT blebrr_adv_send(BRR_HANDLE * handle, UCHAR type, void * pdata, UINT16 datalen)
  767. {
  768. API_RESULT retval;
  769. BLEBRR_Q_ELEMENT * elt;
  770. /* Validate handle */
  771. if (*handle != blebrr_advhandle)
  772. {
  773. return API_FAILURE;
  774. }
  775. if ((NULL == pdata) ||
  776. (0 == datalen))
  777. {
  778. return API_FAILURE;
  779. }
  780. /* Enable interleaving */
  781. blebrr_scan_interleave = MS_TRUE;
  782. blebrr_update_advcount = BLEBRR_BCON_READY_TIME; //send beacon immediately
  783. /* If beacon type, pass to the handler */
  784. if (MESH_AD_TYPE_BCON == type)
  785. {
  786. BRR_BEACON_INFO * info;
  787. /* Get reference to the beacon info */
  788. info = (BRR_BEACON_INFO *)pdata;
  789. if (BRR_BCON_TYPE_SECURE_NET != (info->type >> 4))
  790. {
  791. return blebrr_bcon_send(handle, pdata, datalen);
  792. }
  793. else
  794. {
  795. /* Update the data and length reference */
  796. pdata = info->bcon_data;
  797. datalen = info->bcon_datalen;
  798. }
  799. }
  800. /* Lock */
  801. BLEBRR_LOCK();
  802. /* Allocate the next free element in the data queue */
  803. elt = blebrr_enqueue_alloc();
  804. /* Is any element free? */
  805. if (NULL == elt)
  806. {
  807. /* Unlock */
  808. BLEBRR_UNLOCK();
  809. BLEBRR_LOG("Queue Full! blebrr_advscan_timeout_count = %d, ble state = %d\r\n", blebrr_advscan_timeout_count, BLEBRR_GET_STATE());
  810. blebrr_scan_pl(FALSE); // HZF
  811. return API_FAILURE;
  812. }
  813. /* Update element type */
  814. elt->type = BRR_BCON_COUNT;
  815. /* Schedule to send */
  816. retval = blebrr_send
  817. (
  818. type,
  819. pdata,
  820. datalen,
  821. elt
  822. );
  823. if(retval == API_FAILURE)
  824. blebrr_dequeue_manual();
  825. else
  826. blebrr_datacount++;
  827. /* Unlock */
  828. BLEBRR_UNLOCK();
  829. return API_SUCCESS;
  830. }
  831. #ifdef BLEBRR_LP_SUPPORT
  832. DECL_STATIC void blebrr_adv_sleep(BRR_HANDLE * handle)
  833. {
  834. BLEBRR_LOCK_VOID();
  835. /* Set bearer sleep state */
  836. blebrr_sleep = MS_TRUE;
  837. MS_prov_stop_interleave_timer();
  838. MS_proxy_server_stop_timer();
  839. if (BLEBRR_STATE_SCAN_ENABLED == BLEBRR_GET_STATE() ||
  840. BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
  841. {
  842. /* Disable Scan */
  843. blebrr_scan_pl(MS_FALSE);
  844. /* Update state */
  845. BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
  846. }
  847. else if(BLEBRR_STATE_ADV_ENABLED == BLEBRR_GET_STATE())
  848. {
  849. /* Disable Advertising */
  850. blebrr_advertise_pl(MS_FALSE);
  851. }
  852. /* Enter platform sleep */
  853. EM_enter_sleep_pl();
  854. BLEBRR_UNLOCK_VOID();
  855. }
  856. DECL_STATIC void blebrr_adv_wakeup(BRR_HANDLE * handle, UINT8 mode)
  857. {
  858. BLEBRR_LOCK_VOID();
  859. /* Exit platform sleep */
  860. EM_exit_sleep_pl();
  861. /* Reset bearer sleep state */
  862. blebrr_sleep = MS_FALSE;
  863. if (BRR_TX & mode)
  864. {
  865. if (BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
  866. {
  867. blebrr_update_advcount = BLEBRR_BCON_READY_TIME; //send beacon immediately
  868. /* Enable Advertise */
  869. blebrr_lp_flag = MS_TRUE;
  870. blebrr_update_advdata();
  871. /* Update state */
  872. BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
  873. }
  874. }
  875. else if (BRR_RX & mode)
  876. {
  877. if (BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
  878. {
  879. /* Enable Scan */
  880. blebrr_scan_pl(MS_TRUE);
  881. /* Update state */
  882. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
  883. }
  884. }
  885. BLEBRR_UNLOCK_VOID();
  886. }
  887. API_RESULT blebrr_sleep_handler(void)
  888. {
  889. API_RESULT retval;
  890. retval=MS_brr_sleep();
  891. return retval;
  892. }
  893. API_RESULT blebrr_wakeup_handler(void)
  894. {
  895. API_RESULT retval;
  896. UCHAR state;
  897. /* Fetch PROXY feature state */
  898. MS_access_cm_get_features_field(&state, MS_FEATURE_PROXY);
  899. if(state == MS_TRUE)
  900. {
  901. retval = MS_brr_wakeup(BRR_TX|BRR_RX);
  902. }
  903. else
  904. {
  905. retval = MS_brr_wakeup(BRR_RX);
  906. }
  907. return retval;
  908. }
  909. static void enter_lp_sleep_mode (void)
  910. {
  911. light_timeout_handle();
  912. hal_pwrmgr_unlock(MOD_USR1);
  913. blebrr_sleep_handler();
  914. printf("sleep mode:%d\n", isSleepAllow());
  915. }
  916. void blebrr_lp_mode (void * args, UINT16 size)
  917. {
  918. UCHAR mode;
  919. UCHAR glp_mode;
  920. blebrr_lp_thandle = EM_TIMER_HANDLE_INIT_VAL;
  921. UCHAR state;
  922. /* Fetch PROXY feature state */
  923. MS_access_cm_get_features_field(&state, MS_FEATURE_PROXY);
  924. MS_IGNORE_UNUSED_PARAM(size);
  925. mode = (*((UCHAR *)args));
  926. if(mode == BLEBRR_LP_OFF)
  927. {
  928. pwroff_cfg_t pwr_wkp_cfg[]={{P14,NEGEDGE}};
  929. hal_pwrmgr_poweroff( pwr_wkp_cfg, sizeof(pwr_wkp_cfg)/sizeof(pwr_wkp_cfg[0]) );
  930. }
  931. else if(mode == BLEBRR_LP_SLEEP)
  932. {
  933. blebrr_wakeup_handler();
  934. if(state != MS_TRUE)
  935. {
  936. glp_mode = BLEBRR_LP_WKP;
  937. EM_start_timer
  938. (
  939. &blebrr_lp_thandle,
  940. EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_WKP_TIMEOUT,
  941. blebrr_lp_mode,
  942. (void *)&glp_mode,
  943. sizeof(glp_mode)
  944. );
  945. }
  946. }
  947. else
  948. {
  949. glp_mode = BLEBRR_LP_SLEEP;
  950. EM_start_timer
  951. (
  952. &blebrr_lp_thandle,
  953. EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_SLP_TIMEOUT,
  954. blebrr_lp_mode,
  955. (void *)&glp_mode,
  956. sizeof(glp_mode)
  957. );
  958. enter_lp_sleep_mode();
  959. }
  960. }
  961. API_RESULT blebrr_lp_start(UCHAR mode)
  962. {
  963. API_RESULT retval;
  964. UCHAR state;
  965. UINT32 timeout;
  966. /* Fetch PROXY feature state */
  967. MS_access_cm_get_features_field(&state, MS_FEATURE_PROXY);
  968. switch(mode)
  969. {
  970. case BLEBRR_LP_OFF:
  971. {
  972. timeout = BLEBRR_LP_UNPROVISION_TIMEOUT;
  973. }
  974. break;
  975. case BLEBRR_LP_SLEEP:
  976. {
  977. timeout = EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_SLP_TIMEOUT;
  978. }
  979. break;
  980. case BLEBRR_LP_WKP:
  981. {
  982. timeout = EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_WKP_TIMEOUT;
  983. }
  984. break;
  985. default:
  986. break;
  987. }
  988. retval = EM_start_timer
  989. (
  990. &blebrr_lp_thandle,
  991. timeout,
  992. blebrr_lp_mode,
  993. (void *)&mode,
  994. sizeof(mode)
  995. );
  996. if(mode == BLEBRR_LP_SLEEP)
  997. enter_lp_sleep_mode();
  998. return retval;
  999. }
  1000. void blebrr_lp_stop(void)
  1001. {
  1002. EM_stop_timer (blebrr_lp_thandle);
  1003. blebrr_lp_thandle = EM_TIMER_HANDLE_INIT_VAL;
  1004. }
  1005. #endif /* BLEBRR_LP_SUPPORT */
  1006. /**
  1007. * \brief
  1008. *
  1009. * \par Description
  1010. *
  1011. *
  1012. * \param args
  1013. * \param size
  1014. *
  1015. * \return void
  1016. */
  1017. DECL_STATIC void blebrr_advscan_timeout_handler (void * args, UINT16 size)
  1018. {
  1019. MS_IGNORE_UNUSED_PARAM(args);
  1020. MS_IGNORE_UNUSED_PARAM(size);
  1021. BLEBRR_LOCK_VOID();
  1022. blebrr_advscan_timeout_count ++;
  1023. /* Reset Timer Handler */
  1024. blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
  1025. /* Check the state of AdvScan procedure */
  1026. switch (BLEBRR_GET_STATE())
  1027. {
  1028. case BLEBRR_STATE_ADV_ENABLED:
  1029. /* Disable Adv */
  1030. if (!blebrr_advertise_pl (MS_FALSE)) // HZF
  1031. /* Update state */
  1032. BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_DISABLE);
  1033. break;
  1034. case BLEBRR_STATE_SCAN_ENABLED:
  1035. /* Disable Scan */
  1036. blebrr_scan_pl (MS_FALSE);
  1037. /* Update state */
  1038. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_DISABLE);
  1039. break;
  1040. default:
  1041. /* Should not reach here */
  1042. //BLEBRR_LOG("=======blebrr_advscan_timeout_handler error: state = %2X, state will not change\r\n", BLEBRR_GET_STATE());
  1043. break;
  1044. }
  1045. BLEBRR_UNLOCK_VOID();
  1046. }
  1047. /**
  1048. * \brief
  1049. *
  1050. * \par Description
  1051. *
  1052. *
  1053. * \param timeout
  1054. *
  1055. * \return void
  1056. */
  1057. DECL_STATIC void blebrr_timer_start (UINT32 timeout)
  1058. {
  1059. EM_RESULT retval;
  1060. blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
  1061. retval = EM_start_timer
  1062. (
  1063. &blebrr_timer_handle,
  1064. timeout,
  1065. blebrr_advscan_timeout_handler,
  1066. NULL,
  1067. 0
  1068. );
  1069. if (EM_SUCCESS != retval)
  1070. {
  1071. /* TODO: Log */
  1072. }
  1073. }
  1074. DECL_STATIC void blebrr_timer_stop (void)
  1075. {
  1076. EM_stop_timer(blebrr_timer_handle);
  1077. blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
  1078. }
  1079. #if 0
  1080. /**
  1081. * \brief
  1082. *
  1083. * \par Description
  1084. *
  1085. *
  1086. * \param timeout
  1087. *
  1088. * \return void
  1089. */
  1090. DECL_STATIC void blebrr_timer_restart (UINT32 timeout)
  1091. {
  1092. EM_RESULT retval;
  1093. // printf("before blebrr_timer_handle:%d\n",blebrr_timer_handle);
  1094. retval = EM_restart_timer
  1095. (
  1096. blebrr_timer_handle,
  1097. timeout
  1098. );
  1099. // printf("after blebrr_timer_handle:%d\n",blebrr_timer_handle);
  1100. if (EM_SUCCESS != retval)
  1101. {
  1102. /* TODO: Log */
  1103. }
  1104. }
  1105. #endif
  1106. /**
  1107. * \brief
  1108. *
  1109. * \par Description
  1110. *
  1111. *
  1112. * \param enable
  1113. *
  1114. * \return void
  1115. */
  1116. void blebrr_pl_scan_setup (UCHAR enable)
  1117. {
  1118. API_RESULT retval;
  1119. BLEBRR_LOCK_VOID();
  1120. #ifdef BLEBRR_ENABLE_SCAN_TRACE
  1121. BLEBRR_LOG ("Scan Setup - %d", enable);
  1122. #endif /* BLEBRR_ENABLE_SCAN_TRACE */
  1123. /* Is scan enabled? */
  1124. if (MS_TRUE == enable)
  1125. {
  1126. /* Yes, Update state */
  1127. BLEBRR_SET_STATE(BLEBRR_STATE_SCAN_ENABLED);
  1128. /* Is data available in queue to be sent? */
  1129. if (0 != blebrr_datacount)
  1130. {
  1131. UCHAR depth=blebrr_get_queue_depth();
  1132. if(depth>0)
  1133. blebrr_scanTimeOut = BLEBRR_SCAN_ADJ_THD_MIN;
  1134. else
  1135. blebrr_scanTimeOut = BLEBRR_SCAN_ADJ_THD_MAX;
  1136. /* Yes, Start bearer timer for Scan Timeout */
  1137. blebrr_timer_start ((EM_TIMEOUT_MILLISEC | blebrr_scanTimeOut));
  1138. }
  1139. }
  1140. else
  1141. {
  1142. /* No, Enable Advertising with Data */
  1143. retval = blebrr_update_advdata();
  1144. if (API_SUCCESS != retval)
  1145. {
  1146. if (MS_TRUE != blebrr_sleep)
  1147. {
  1148. /* Enale Scan */
  1149. blebrr_scan_pl(MS_TRUE);
  1150. /* Update state */
  1151. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
  1152. }
  1153. else
  1154. {
  1155. /* Update state */
  1156. BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
  1157. }
  1158. }
  1159. else
  1160. {
  1161. /* Update state */
  1162. BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
  1163. }
  1164. }
  1165. BLEBRR_UNLOCK_VOID();
  1166. }
  1167. /**
  1168. * \brief
  1169. *
  1170. * \par Description
  1171. *
  1172. *
  1173. * \param type
  1174. * \param enable
  1175. *
  1176. * \return void
  1177. */
  1178. void blebrr_pl_advertise_setup (UCHAR enable)
  1179. {
  1180. BLEBRR_LOCK_VOID();
  1181. UCHAR adv_repeat_count;
  1182. API_RESULT retval;
  1183. #ifdef BLEBRR_ENABLE_ADV_TRACE
  1184. BLEBRR_LOG ("Adv Setup - %d", enable);
  1185. #endif /* BLEBRR_ENABLE_ADV_TRACE */
  1186. /* Is advertise enabled? */
  1187. if (MS_TRUE == enable)
  1188. {
  1189. /* Yes, Update state */
  1190. BLEBRR_SET_STATE(BLEBRR_STATE_ADV_ENABLED);
  1191. /* Start bearer timer for Adv Timeout */
  1192. if (blebrr_scan_interleave == MS_TRUE)
  1193. {
  1194. blebrr_timer_start (EM_TIMEOUT_MILLISEC | BLEBRR_ADV_TIMEOUT);
  1195. }
  1196. }
  1197. else
  1198. {
  1199. if (MS_TRUE == blebrr_sleep)
  1200. {
  1201. /* Update state */
  1202. blebrr_adv_restart = MS_FALSE;
  1203. BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
  1204. return;
  1205. }
  1206. if(blebrr_adv_restart == MS_TRUE)
  1207. {
  1208. blebrr_adv_restart = MS_FALSE;
  1209. blebrr_update_advcount = BLEBRR_BCON_READY_TIME;
  1210. retval = blebrr_update_advdata();
  1211. if (API_SUCCESS != retval)
  1212. {
  1213. /* Enale Scan */
  1214. blebrr_scan_pl(MS_TRUE);
  1215. /* Update state */
  1216. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
  1217. }
  1218. else
  1219. {
  1220. /* Update state */
  1221. BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
  1222. }
  1223. }
  1224. else
  1225. {
  1226. adv_repeat_count = (blebrr_prov_started == MS_TRUE)?BLEBRR_ADVREPEAT_PRO_COUNT:BLEBRR_ADVREPEAT_NET_COUNT;
  1227. if (/*blebrr_beacon && */(adv_repeat_count > blebrr_advrepeat_count))
  1228. {
  1229. blebrr_advrepeat_count++;
  1230. blebrr_advertise_pl(MS_TRUE);
  1231. /* Update state */
  1232. BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
  1233. }
  1234. else
  1235. {
  1236. /* No, Enable Scanning */
  1237. blebrr_scan_pl(MS_TRUE);
  1238. #ifdef BLEBRR_LP_SUPPORT
  1239. UCHAR glp_mode;
  1240. if((blebrr_lp_flag == MS_TRUE) &&(blebrr_beacon == 1))
  1241. {
  1242. blebrr_beacon =0;
  1243. blebrr_lp_flag = MS_FALSE;
  1244. glp_mode = BLEBRR_LP_WKP;
  1245. blebrr_lp_start(glp_mode);
  1246. }
  1247. #endif
  1248. /* Update state */
  1249. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
  1250. }
  1251. }
  1252. }
  1253. BLEBRR_UNLOCK_VOID();
  1254. }
  1255. /**
  1256. * \brief
  1257. *
  1258. * \par Description
  1259. *
  1260. *
  1261. * \param None
  1262. *
  1263. * \return void
  1264. */
  1265. void blebrr_pl_advertise_end (void)
  1266. {
  1267. BLEBRR_LOCK_VOID();
  1268. BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
  1269. blebrr_advrepeat_count = 0;
  1270. BLEBRR_UNLOCK_VOID();
  1271. }
  1272. #ifdef BLEBRR_FILTER_DUPLICATE_PACKETS
  1273. /**
  1274. * \brief
  1275. *
  1276. * \par Description
  1277. *
  1278. *
  1279. * \param p_adv_data_with_bd_addr
  1280. *
  1281. * \return void
  1282. */
  1283. DECL_STATIC API_RESULT blebrr_adv_duplicate_filter(/* IN */ UCHAR * p_adv_data_with_bd_addr)
  1284. {
  1285. UCHAR length, index;
  1286. /* Get the ADV data packet length */
  1287. length = p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE];
  1288. for (index = 0; index < BLEBRR_MAX_ADV_FILTER_LIST_COUNT; index++)
  1289. {
  1290. /* First Match BD Addr */
  1291. if (0 == EM_mem_cmp
  1292. (
  1293. &blebrr_adv_list[index][0],
  1294. p_adv_data_with_bd_addr,
  1295. 1 + BT_BD_ADDR_SIZE
  1296. ))
  1297. {
  1298. /* Check Data Length */
  1299. if (blebrr_adv_list[index][1 + BT_BD_ADDR_SIZE] == p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE])
  1300. {
  1301. if (0 == EM_mem_cmp
  1302. (
  1303. &blebrr_adv_list[index][1 + BT_BD_ADDR_SIZE + 1],
  1304. &p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE + 1],
  1305. length
  1306. ))
  1307. {
  1308. return API_SUCCESS;
  1309. }
  1310. }
  1311. /* Update Adv data */
  1312. EM_mem_copy
  1313. (
  1314. &blebrr_adv_list[index][1 + BT_BD_ADDR_SIZE],
  1315. &p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE],
  1316. length + 1
  1317. );
  1318. return API_FAILURE;
  1319. }
  1320. }
  1321. /* Find out the suitable location to save the most recent ADV packet */
  1322. /* New peer device. Add */
  1323. EM_mem_copy
  1324. (
  1325. &blebrr_adv_list[blebrr_adv_list_inser_index][0],
  1326. p_adv_data_with_bd_addr,
  1327. length + 1 + BT_BD_ADDR_SIZE + 1
  1328. );
  1329. /* Increment and Wrap (if required) */
  1330. blebrr_adv_list_inser_index++;
  1331. if (BLEBRR_MAX_ADV_FILTER_LIST_COUNT <= blebrr_adv_list_inser_index)
  1332. {
  1333. blebrr_adv_list_inser_index = 0;
  1334. }
  1335. return API_FAILURE;
  1336. }
  1337. #endif /* BLEBRR_FILTER_DUPLICATE_PACKETS */
  1338. extern uint8 osal_memory_audit(void *ptr);
  1339. /**
  1340. * \brief
  1341. *
  1342. * \par Description
  1343. *
  1344. *
  1345. * \param type
  1346. * \param pdata
  1347. * \param pdatalen
  1348. * \param rssi
  1349. *
  1350. * \return void
  1351. */
  1352. void blebrr_pl_recv_advpacket (UCHAR type, UCHAR * pdata, UINT16 pdatalen, UCHAR rssi)
  1353. {
  1354. MS_BUFFER info;
  1355. #ifdef BLEBRR_FILTER_DUPLICATE_PACKETS
  1356. /* Duplicate Filtering */
  1357. retval = blebrr_adv_duplicate_filter(p_adv_data_with_bd_addr);
  1358. /* If found the ADV packet as duplicate, drop the ADV packet */
  1359. if (API_SUCCESS == retval)
  1360. {
  1361. return API_FAILURE;
  1362. }
  1363. #endif /* BLEBRR_FILTER_DUPLICATE_PACKETS */
  1364. /* Handle only if Non-Connectable (Passive) Advertising */
  1365. if (BRR_BCON_PASSIVE != type)
  1366. {
  1367. return;
  1368. }
  1369. /* Pack the RSSI as metadata */
  1370. info.payload = &rssi;
  1371. info.length = sizeof(UCHAR);
  1372. /* Deliver the packet to the bearer */
  1373. if (NULL != blebrr_adv.bearer_recv)
  1374. {
  1375. /* BLEBRR_LOG("[ADV-Rx <]: ");
  1376. BLEBRR_dump_bytes(pdata, pdatalen); */
  1377. blebrr_adv.bearer_recv(&blebrr_advhandle, pdata, pdatalen, &info);
  1378. }
  1379. else
  1380. {
  1381. BLEBRR_LOG("BEARER RECV Callback Currently NULL !!\n");
  1382. }
  1383. }
  1384. /**
  1385. * \brief
  1386. *
  1387. * \par Description
  1388. *
  1389. * *
  1390. * * \param void
  1391. *
  1392. * \return void
  1393. */
  1394. void blebrr_register(void)
  1395. {
  1396. /* Initialize locals */
  1397. BLEBRR_MUTEX_INIT_VOID();
  1398. /* Initialize Timer Handler */
  1399. blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
  1400. /* Initialize the transport */
  1401. blebrr_init_pl();
  1402. /* Get the platform Advdata initial offset if any */
  1403. pl_advdata_offset = blebrr_get_advdata_offset_pl ();
  1404. /* Reset the bearer sleep */
  1405. blebrr_sleep = MS_FALSE;
  1406. /* Add the Adv Bearer */
  1407. blebrr_adv.bearer_send = blebrr_adv_send;
  1408. #ifdef BLEBRR_LP_SUPPORT
  1409. blebrr_adv.bearer_sleep = blebrr_adv_sleep;
  1410. blebrr_adv.bearer_wakeup = blebrr_adv_wakeup;
  1411. blebrr_lp_thandle = EM_TIMER_HANDLE_INIT_VAL;
  1412. #endif /* BLEBRR_LP_SUPPORT */
  1413. MS_brr_add_bearer(BRR_TYPE_ADV, &blebrr_adv, &blebrr_advhandle);
  1414. /* Enable all Mesh related Module Debugging */
  1415. EM_enable_module_debug_flag
  1416. (
  1417. MS_MODULE_ID_APP |
  1418. MS_MODULE_ID_ACCESS |
  1419. MS_MODULE_ID_TRN |
  1420. MS_MODULE_ID_LTRN |
  1421. MS_MODULE_ID_NET |
  1422. MS_MODULE_ID_COMMON |
  1423. MS_MODULE_ID_BRR |
  1424. MS_MODULE_ID_STBX |
  1425. MS_MODULE_ID_PROV
  1426. );
  1427. /* Set Debug Level Trace as default. Enable Information only if required */
  1428. EM_set_debug_level(EM_DEBUG_LEVEL_TRC);
  1429. /* Allow the tasks to start and be ready */
  1430. EM_sleep (1);
  1431. #if 0
  1432. /* Start Observing */
  1433. BLEBRR_LOG ("Start Observing...\n");
  1434. blebrr_scan_pl (MS_TRUE);
  1435. /* Update state */
  1436. BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
  1437. #else /* 0 */
  1438. BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
  1439. #endif /* 0 */
  1440. }