blebrr_pl.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /**
  2. * \file blebrr_pl.c
  3. *
  4. *
  5. */
  6. /*
  7. * Copyright (C) 2018. Mindtree Limited.
  8. * All rights reserved.
  9. */
  10. /* --------------------------------------------- Header File Inclusion */
  11. /* Platform Stack Headers */
  12. #include <hci.h>
  13. #include <gap.h>
  14. #include <gatt.h>
  15. #define BLE_CLIENT_ROLE
  16. /* Povisioning API headers */
  17. #include "MS_prov_api.h"
  18. /* BLE Bearer related Headers */
  19. #include "blebrr.h"
  20. #include "mesh_services.h"
  21. #include "appl_main.h"
  22. extern uint8 llState, llSecondaryState;
  23. #ifdef BLE_CLIENT_ROLE
  24. #include "mesh_clients.h"
  25. #endif /* BLE_CLIENT_ROLE */
  26. /* Platform log to be mapped */
  27. #define BLEBRRPL_LOG printf
  28. #define BLEBRRPL_dump_bytes appl_dump_bytes
  29. /*********************************************************************
  30. * EXTERNAL FUNCTIONS
  31. */
  32. bStatus_t BLE_gap_set_scan_enable(uint8_t scan_enable);
  33. bStatus_t BLE_gap_connect(uint8_t whitelist, uint8_t * addr, uint8_t addr_type);
  34. bStatus_t BLE_gap_set_advscanrsp_data(uint8_t type, uint8_t * adv_data, uint16_t adv_datalen);
  35. bStatus_t BLE_gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scan_filterpolicy);
  36. bStatus_t BLE_gap_set_adv_params(uint8_t adv_type, uint16_t adv_intervalmin, uint16_t adv_intervalmax, uint8_t adv_filterpolicy);
  37. bStatus_t BLE_gap_disconnect(uint16_t conn_handle);
  38. bStatus_t BLE_gap_set_adv_enable(uint8_t adv_enable);
  39. API_RESULT blebrr_scan_cmd_handler_pl(UCHAR enable);
  40. void blebrr_enable_mesh_serv_pl (UCHAR serv_type);
  41. void blebrr_disable_mesh_serv_pl (UCHAR serv_type);
  42. API_RESULT blebrr_handle_le_connection_pl
  43. (
  44. uint16_t conn_idx,
  45. uint16_t conn_hndl,
  46. uint8_t peer_addr_type,
  47. uint8_t * peer_addr
  48. );
  49. API_RESULT blebrr_handle_le_disconnection_pl
  50. (
  51. uint16_t conn_idx,
  52. uint16_t conn_hndl,
  53. uint8_t reason
  54. );
  55. void blebrr_handle_evt_adv_complete (UINT8 enable);
  56. void blebrr_handle_evt_adv_report (gapDeviceInfoEvent_t * adv);
  57. void blebrr_handle_evt_scan_complete (UINT8 enable);
  58. /**
  59. * Value for invalid connection index
  60. *
  61. * Portable code should use this value wherever it's required to mark connection index as invalid.
  62. */
  63. #define BLEBRR_CONN_IDX_INVALID (0xFFFF)
  64. #define BLEBRR_CONN_HNDL_INVALID (0xFFFF)
  65. /* --------------------------------------------- Global Definitions */
  66. #define BLEBRR_ADVDATA_OFFSET 0 /* 3 */
  67. #define BLEBRR_NCON_ADVINTMIN 0xA0 /* ADV for NON_CONN_IND should be greater than 0x00A0 */
  68. #define BLEBRR_NCON_ADVINTMAX 0xA0 /* ADV for NON_CONN_IND should be greater than 0x00A0 */
  69. #define BLEBRR_NCON_ADVTYPE 0x03
  70. #define BLEBRR_NCON_DIRADDRTYPE 0x00
  71. #define BLEBRR_NCON_ADVCHMAP 0x07
  72. #define BLEBRR_NCON_ADVFILTERPOLICY 0x00
  73. #define BLEBRR_CON_ADVINTMIN 0x320
  74. #define BLEBRR_CON_ADVINTMAX 0x320
  75. #define BLEBRR_CON_ADVTYPE 0x00
  76. #define BLEBRR_CON_DIRADDRTYPE 0x00
  77. #define BLEBRR_CON_ADVCHMAP 0x07
  78. #define BLEBRR_CON_ADVFILTERPOLICY 0x00
  79. #define BLEBRR_CON_SCANRSP_DATALEN 31
  80. #define BLEBRR_SCANTYPE 0x00
  81. #define BLEBRR_SCANINTERVAL 0x18 //0x18->15ms 0x20->20ms
  82. #define BLEBRR_SCANWINDOW 0x18 //0x18->15ms 0x20->20ms
  83. #define BLEBRR_SCANFILTERPOLICY 0x00
  84. #define BLEBRR_SCANFILTERDUPS 0x00
  85. #define BLEBRR_CONN_FILTER_POLICY_WL 0x01
  86. #define BLEBRR_CONN_FILTER_POLICY_NWL 0x00
  87. #define BLEBRR_CONN_INTERVAL_MIN 0x0040
  88. #define BLEBRR_CONN_INTERVAL_MAX 0x0040
  89. #define BLEBRR_CONN_LATENCY 0x0000
  90. #define BLEBRR_CONN_SUPERVISION_TO 0x03BB
  91. #define BLEBRR_CONN_MIN_CE_LEN 0x0000
  92. #define BLEBRR_CONN_MAX_CE_LEN 0x0000
  93. #define BLEBRR_OWNADDRTYPE 0x00
  94. #define BLEBRR_ADVSCANEN_TIMEOUT 50
  95. /* Active connection handle used to send measurements */
  96. static uint16_t active_conn_hndl = BLEBRR_CONN_HNDL_INVALID;
  97. /* Call Back to Inform Application Layer about GATT Bearer Iface Events */
  98. typedef void (* blebrr_gatt_iface_event_pl_cb)
  99. (
  100. uint8_t ev_name,
  101. uint8_t status
  102. );
  103. API_RESULT blebrr_register_gatt_iface_event_pl
  104. (
  105. blebrr_gatt_iface_event_pl_cb gatt_iface_evt_cb
  106. );
  107. /* --------------------------------------------- External Global Variables */
  108. /* --------------------------------------------- Exported Global Variables */
  109. /* --------------------------------------------- Static Global Variables */
  110. /* Bearer Scan Response Data related */
  111. DECL_STATIC UCHAR blebrr_scanrsp_data[BLEBRR_CON_SCANRSP_DATALEN];
  112. DECL_STATIC UCHAR blebrr_scanrsp_datalen;
  113. //DECL_STATIC UCHAR BLEBRR_DIRADDR[6];
  114. DECL_STATIC UCHAR blebrr_advstate;
  115. DECL_STATIC UCHAR blebrr_scanstate;
  116. DECL_STATIC UCHAR curr_service;
  117. #ifdef BLE_CLIENT_ROLE
  118. /* BLE Bearer GAP Connection State */
  119. /**
  120. * 0x00 - Idle/Disconnected
  121. * 0x01 - To Initiate Connection
  122. * 0x02 - Connected Initiated
  123. * 0x03 - Connected
  124. */
  125. static UCHAR blebrr_connect_state = 0x00;
  126. static UCHAR blebrr_addr_to_conn[6] = {0};
  127. static UCHAR blebrr_addr_type_to_conn = 0xFF;
  128. #endif /* BLE_CLIENT_ROLE */
  129. /* Global to hold the Role of GATT */
  130. UCHAR blebrr_gatt_role;
  131. BRR_HANDLE blebrr_gatt_handle_pl;
  132. /* Mesh Provisioning service related data structures */
  133. static uint16_t appl_mesh_prov_data_out_ccd_cb(uint16_t conn_hndl, uint8_t enabled);
  134. static uint16_t appl_mesh_prov_data_in_wt_cb
  135. (
  136. uint16_t conn_hndl,
  137. uint16_t offset,
  138. uint16_t length,
  139. uint8_t * value
  140. );
  141. static mesh_prov_cb appl_mesh_prov_cb =
  142. {
  143. .prov_data_in_cb = appl_mesh_prov_data_in_wt_cb,
  144. .prov_data_out_ccd_cb = appl_mesh_prov_data_out_ccd_cb,
  145. };
  146. /* Mesh Proxy service related data structures */
  147. static uint16_t appl_mesh_proxy_data_out_ccd_cb(uint16_t conn_hndl, uint8_t enabled);
  148. static uint16_t appl_mesh_proxy_data_in_wt_cb
  149. (
  150. uint16_t conn_hndl,
  151. uint16_t offset,
  152. uint16_t length,
  153. uint8_t * value
  154. );
  155. static mesh_proxy_cb appl_mesh_proxy_cb =
  156. {
  157. .proxy_data_in_cb = appl_mesh_proxy_data_in_wt_cb,
  158. .proxy_data_out_ccd_cb = appl_mesh_proxy_data_out_ccd_cb,
  159. };
  160. /* Global to hold GATT Iface events Application Callback pointer */
  161. DECL_STATIC BLEBRR_GATT_IFACE_EVENT_PL_CB blebrr_gatt_iface_pl_cb;
  162. /* --------------------------------------------- Functions */
  163. API_RESULT blebrr_register_gatt_iface_event_pl
  164. (
  165. BLEBRR_GATT_IFACE_EVENT_PL_CB gatt_iface_evt_cb
  166. )
  167. {
  168. if (NULL != gatt_iface_evt_cb)
  169. {
  170. blebrr_gatt_iface_pl_cb = gatt_iface_evt_cb;
  171. BLEBRRPL_LOG("\r\n Registered GATT Bearer Iface Events Appl Callback!\r\n");
  172. return API_SUCCESS;
  173. }
  174. /* If NULL callback is registered */
  175. return API_FAILURE;
  176. }
  177. void appl_dump_bytes(UCHAR *buffer, UINT16 length)
  178. {
  179. char hex_stream[49];
  180. char char_stream[17];
  181. UINT32 i;
  182. UINT16 offset, count;
  183. UCHAR c;
  184. BLEBRRPL_LOG("\n");
  185. BLEBRRPL_LOG("-- Dumping %d Bytes --\n",
  186. (int)length);
  187. BLEBRRPL_LOG(
  188. "-------------------------------------------------------------------\n");
  189. count = 0;
  190. offset = 0;
  191. for (i = 0; i < length; i++)
  192. {
  193. c = buffer[i];
  194. sprintf(hex_stream + offset, "%02X ", c);
  195. if ((c >= 0x20) && (c <= 0x7E))
  196. {
  197. char_stream[count] = c;
  198. }
  199. else
  200. {
  201. char_stream[count] = '.';
  202. }
  203. count++;
  204. offset += 3;
  205. if (16 == count)
  206. {
  207. char_stream[count] = '\0';
  208. count = 0;
  209. offset = 0;
  210. BLEBRRPL_LOG("%s %s\n",
  211. hex_stream, char_stream);
  212. EM_mem_set(hex_stream, 0, 49);
  213. EM_mem_set(char_stream, 0, 17);
  214. }
  215. }
  216. if (offset != 0)
  217. {
  218. char_stream[count] = '\0';
  219. /* Maintain the alignment */
  220. BLEBRRPL_LOG("%-48s %s\n",
  221. hex_stream, char_stream);
  222. }
  223. BLEBRRPL_LOG(
  224. "-------------------------------------------------------------------\n");
  225. BLEBRRPL_LOG("\n");
  226. return;
  227. }
  228. void blebrr_handle_evt_adv_report (gapDeviceInfoEvent_t * adv)
  229. {
  230. UCHAR * pdata;
  231. UCHAR type;
  232. /* Reference the event type, data and datalength */
  233. type = (HCI_NONCONNECTABLE_UNDIRECTED_ADV == (UCHAR)adv->eventType)? BRR_BCON_PASSIVE: BRR_BCON_ACTIVE;
  234. pdata = (UCHAR *)adv->pEvtData;
  235. #if 0
  236. if (BRR_BCON_ACTIVE == type)
  237. {
  238. BLEBRRPL_LOG("Adv Report. Type: 0x%02X\r\n", adv->eventType);
  239. BLEBRRPL_LOG("BD Addr [0x%02X]: ", adv->addrType);
  240. BLEBRRPL_dump_bytes(adv->addr, 6);
  241. BLEBRRPL_LOG("Data [datalen]:");
  242. BLEBRRPL_dump_bytes(pdata, (UCHAR)adv->dataLen);
  243. }
  244. #endif /* 0 */
  245. /* Pass advertising data to the bearer */
  246. if ((MESH_AD_TYPE_BCON == pdata[1]) || (MESH_AD_TYPE_PB_ADV == pdata[1]) || (MESH_AD_TYPE_PKT == pdata[1]))
  247. {
  248. if (BRR_BCON_PASSIVE == type)
  249. {
  250. blebrr_pl_recv_advpacket (type, &pdata[1], pdata[0], (UCHAR)adv->rssi);
  251. }
  252. }
  253. }
  254. void blebrr_handle_evt_adv_complete (UINT8 enable)
  255. {
  256. /* if (blebrr_advstate == enable) */
  257. {
  258. blebrr_pl_advertise_setup (blebrr_advstate);
  259. }
  260. #if 0
  261. else if (0x01 == blebrr_advstate)
  262. {
  263. blebrr_advstate = 0x00;
  264. blebrr_pl_advertise_end();
  265. }
  266. #endif /* 0 */
  267. }
  268. void blebrr_handle_evt_scan_complete (UINT8 enable)
  269. {
  270. /* if (blebrr_scanstate == enable) */
  271. #ifdef BLE_CLIENT_ROLE
  272. bStatus_t ret;
  273. /* BLEBRRPL_LOG ("\r\n blebrr_handle_evt_scan_complete with 0x%04X\r\n", enable);
  274. BLEBRRPL_LOG ("\r\n blebrr_connect_state is 0x%04X\r\n", blebrr_connect_state); */
  275. if (0x00 == enable)
  276. {
  277. /* Initiate Connection on Scan Disable */
  278. if (0x01 == blebrr_connect_state)
  279. {
  280. BLEBRRPL_LOG ("Scan Disabled! Initiating Connection...");
  281. ret = BLE_gap_connect
  282. (
  283. 0x00,
  284. blebrr_addr_to_conn,
  285. blebrr_addr_type_to_conn
  286. );
  287. BLEBRRPL_LOG("Initiating Connection to Address "
  288. "0x%02X:0x%02X:0x%02X:0x%02X:0x%02X:0x%02X Type 0x%02X "
  289. "with retval 0x%04X\r\n",
  290. blebrr_addr_to_conn[0], blebrr_addr_to_conn[1], blebrr_addr_to_conn[2],
  291. blebrr_addr_to_conn[3], blebrr_addr_to_conn[4], blebrr_addr_to_conn[5],
  292. blebrr_addr_type_to_conn, ret);
  293. blebrr_connect_state = 0x02;
  294. /* Set GATT Role as Client */
  295. blebrr_gatt_role = BLEBRR_CLIENT_ROLE;
  296. }
  297. else
  298. {
  299. /* Indicate Scan disable to bearer */
  300. blebrr_pl_scan_setup (blebrr_scanstate);
  301. }
  302. }
  303. else
  304. #endif /* BLE_CLIENT_ROLE */
  305. {
  306. /* Indicate Scan disable to bearer */
  307. blebrr_pl_scan_setup (blebrr_scanstate);
  308. }
  309. }
  310. void blebrr_init_pl (void)
  311. {
  312. // hciStatus_t ret;
  313. BLEBRR_LOG("Done.\n");
  314. /* Configure the local device address */
  315. GAP_ConfigDeviceAddr(ADDRTYPE_PUBLIC, NULL);
  316. /* Initialize */
  317. blebrr_gatt_iface_pl_cb = NULL;
  318. BLE_gap_set_scan_params
  319. (
  320. BLEBRR_SCANTYPE,
  321. BLEBRR_SCANINTERVAL,
  322. BLEBRR_SCANWINDOW,
  323. BLEBRR_SCANFILTERPOLICY
  324. );
  325. /**
  326. * NOTE: Enabling Both the services at the start.
  327. *
  328. * TODO: Check if this needs to be flag protected.
  329. */
  330. BLEBRRPL_LOG ("Enabling Mesh Prov Service...\r\n");
  331. //mesh_prov_init((mesh_prov_cb *)&appl_mesh_prov_cb);
  332. /* Initialize the bearer handle */
  333. blebrr_gatt_handle_pl = BRR_HANDLE_INVALID;
  334. }
  335. void blebrr_scan_pl (UCHAR enable)
  336. {
  337. hciStatus_t ret;
  338. // UCHAR prevstate;
  339. // prevstate = blebrr_scanstate;
  340. /* Is request to enable? */
  341. if (MS_TRUE == enable)
  342. {
  343. /* Update global scan state */
  344. blebrr_scanstate = 0x01;
  345. #ifdef BLEBRR_ENABLE_SCAN_TRACE
  346. BLEBRRPL_LOG ("Enabling Scan...");
  347. #endif /* BLEBRR_ENABLE_SCAN_TRACE */
  348. /* Enable Scan */
  349. ret = BLE_gap_set_scan_enable (0x01);
  350. }
  351. else
  352. {
  353. /* Update global scan state */
  354. blebrr_scanstate = 0x00;
  355. #ifdef BLEBRR_ENABLE_SCAN_TRACE
  356. BLEBRRPL_LOG ("Disabling Scan...");
  357. #endif /* BLEBRR_ENABLE_SCAN_TRACE */
  358. /* Disable Scan */
  359. ret = BLE_gap_set_scan_enable (0x00);
  360. }
  361. /* Is operation failed? */
  362. if (0 != ret)
  363. {
  364. //BLEBRRPL_LOG ("Scan Operation (%d - %d) failed with reason 0x%04X", blebrr_scanstate, prevstate, ret);
  365. }
  366. }
  367. UCHAR blebrr_get_advdata_offset_pl (void)
  368. {
  369. return BLEBRR_ADVDATA_OFFSET;
  370. }
  371. API_RESULT blebrr_set_adv_scanrsp_data_pl
  372. (
  373. UCHAR * srp_data,
  374. UCHAR srp_datalen
  375. )
  376. {
  377. /* Initialize the Globals */
  378. EM_mem_set
  379. (
  380. blebrr_scanrsp_data,
  381. 0x0,
  382. sizeof(blebrr_scanrsp_data)
  383. );
  384. blebrr_scanrsp_datalen = 0;
  385. /* Set the application provided Scan Response Data to Global */
  386. if ((NULL != srp_data) && (0 != srp_datalen))
  387. {
  388. EM_mem_copy
  389. (
  390. blebrr_scanrsp_data,
  391. srp_data,
  392. srp_datalen
  393. );
  394. blebrr_scanrsp_datalen = srp_datalen;
  395. return API_SUCCESS;
  396. }
  397. return API_FAILURE;
  398. }
  399. void blebrr_advertise_data_pl (CHAR type, UCHAR * pdata, UINT16 pdatalen)
  400. {
  401. /* Is request to enable? */
  402. if ((NULL != pdata) && (0 != pdatalen))
  403. {
  404. /* Set Advertising Parameters */
  405. if (BRR_BCON_PASSIVE == type)
  406. {
  407. /* Set Non-Connectable Adv Params */
  408. BLE_gap_set_adv_params
  409. (
  410. BLEBRR_NCON_ADVTYPE,
  411. BLEBRR_NCON_ADVINTMIN,
  412. BLEBRR_NCON_ADVINTMAX,
  413. BLEBRR_NCON_ADVFILTERPOLICY
  414. );
  415. }
  416. else
  417. {
  418. /* Set Connectable Adv Params */
  419. BLE_gap_set_adv_params
  420. (
  421. BLEBRR_CON_ADVTYPE,
  422. BLEBRR_CON_ADVINTMIN,
  423. BLEBRR_CON_ADVINTMAX,
  424. BLEBRR_CON_ADVFILTERPOLICY
  425. );
  426. /* Set the Scan Response Data to Stack if length is valid */
  427. if (0 != blebrr_scanrsp_datalen)
  428. {
  429. BLE_gap_set_advscanrsp_data
  430. (
  431. FALSE,
  432. blebrr_scanrsp_data,
  433. blebrr_scanrsp_datalen
  434. );
  435. }
  436. }
  437. /* Set Advertising Data */
  438. BLE_gap_set_advscanrsp_data(TRUE, pdata, pdatalen);
  439. // BLEBRRPL_LOG ("Adv Data - Retval: \r\n");
  440. /* Enable Advertising */
  441. blebrr_advertise_pl(MS_TRUE);
  442. }
  443. else
  444. {
  445. /* Disable Advertising */
  446. blebrr_advertise_pl(MS_FALSE);
  447. }
  448. }
  449. extern UCHAR blebrr_state;
  450. API_RESULT blebrr_advertise_pl (UCHAR state) // HZF
  451. {
  452. hciStatus_t ret = API_SUCCESS; // HZF
  453. UCHAR prevstate;
  454. prevstate = blebrr_advstate;
  455. if (MS_TRUE == state)
  456. {
  457. #ifdef BLEBRR_ENABLE_ADV_TRACE
  458. BLEBRRPL_LOG ("Enabling Adv...");
  459. #endif /* BLEBRR_ENABLE_ADV_TRACE */
  460. /* Update global adv state */
  461. blebrr_advstate = 0x01;
  462. ret = BLE_gap_set_adv_enable(0x01);
  463. }
  464. else
  465. {
  466. #ifdef BLEBRR_ENABLE_ADV_TRACE
  467. BLEBRRPL_LOG ("Disabling Adv...");
  468. #endif /* BLEBRR_ENABLE_ADV_TRACE */
  469. /* Update global adv state */
  470. blebrr_advstate = 0x00;
  471. //BLEBRRPL_LOG ("Disabling Adv...");
  472. ret = BLE_gap_set_adv_enable(0x00);
  473. }
  474. /* Is operation failed? */
  475. if (0 != ret)
  476. {
  477. BLEBRRPL_LOG ("Adv Operation (%d - %d) failed with reason 0x%04X", blebrr_advstate, prevstate, ret);
  478. }
  479. return ret;
  480. }
  481. API_RESULT blebrr_gatt_send_pl(BRR_HANDLE * handle, UCHAR * data, UINT16 datalen)
  482. {
  483. UCHAR type;
  484. API_RESULT retval;
  485. /* TODO */
  486. /* BLEBRR_LOG("\n >>>> GATT PL Data Tx:\n");
  487. appl_dump_bytes(data, datalen); */
  488. retval = API_SUCCESS;
  489. /* Get the current mode */
  490. /* TODO: See if we need to get specific mode for a GATT transport */
  491. type = blebrr_gatt_mode_get();
  492. /* Check the PDU type received and Add bearer to Mesh stack */
  493. if (BLEBRR_GATT_PROV_MODE == type)
  494. {
  495. /* BLEBRRPL_LOG("\r\nBLEBRR_GATT_PROV_MODE with role 0x%02X\r\n", blebrr_gatt_role); */
  496. if (BLEBRR_SERVER_ROLE == blebrr_gatt_role)
  497. {
  498. retval = mesh_prov_notify_data_out
  499. (
  500. active_conn_hndl,
  501. MESH_PROV_DATA_OUT_VALUE_VAL,
  502. data,
  503. datalen
  504. );
  505. if(retval)
  506. {
  507. return retval;
  508. }
  509. }
  510. #ifdef BLE_CLIENT_ROLE
  511. else
  512. {
  513. /* TODO SRIKKANTH */
  514. mesh_prov_client_data_in_write(active_conn_hndl, data, datalen);
  515. retval = API_SUCCESS;
  516. }
  517. #endif /* BLE_CLIENT_ROLE */
  518. }
  519. else
  520. {
  521. /* BLEBRRPL_LOG("\r\nBLEBRR_GATT_PROXY_MODE with role 0x%02X\r\n", blebrr_gatt_role); */
  522. if (BLEBRR_SERVER_ROLE == blebrr_gatt_role)
  523. {
  524. mesh_proxy_notify_data_out
  525. (
  526. active_conn_hndl,
  527. MESH_PROXY_DATA_OUT_VALUE_VAL,
  528. data,
  529. datalen
  530. );
  531. }
  532. #ifdef BLE_CLIENT_ROLE
  533. else
  534. {
  535. mesh_proxy_client_data_in_write(active_conn_hndl, data, datalen);
  536. retval = API_SUCCESS;
  537. }
  538. #endif /* BLE_CLIENT_ROLE */
  539. }
  540. return retval;
  541. }
  542. static API_RESULT blebrr_recv_mesh_packet_pl
  543. (
  544. void * handle,
  545. UINT16 attr_handle,
  546. UCHAR * data,
  547. UINT16 data_len
  548. )
  549. {
  550. /**
  551. * TODO: MAP the incoming handle to BLEBRR specific handle for
  552. */
  553. /* BLEBRR_LOG("\n >>>> GATT PL Data Rx: %d bytes\n", data_len);
  554. appl_dump_bytes(data, data_len); */
  555. blebrr_pl_recv_gattpacket
  556. (
  557. &blebrr_gatt_handle_pl,
  558. data,
  559. data_len
  560. );
  561. return API_SUCCESS;
  562. }
  563. API_RESULT blebrr_handle_le_connection_pl
  564. (
  565. uint16_t conn_idx,
  566. uint16_t conn_hndl,
  567. uint8_t peer_addr_type,
  568. uint8_t * peer_addr
  569. )
  570. {
  571. /**
  572. * If Needed,
  573. * Store the provided handle according to platform exposed data type
  574. */
  575. BLEBRR_LOG("Device Connected - Handle: 0x%04X\r\n", conn_hndl);
  576. if (active_conn_hndl == BLEBRR_CONN_HNDL_INVALID)
  577. {
  578. /* Store the incoming connection handle in global */
  579. active_conn_hndl = conn_hndl;
  580. #ifdef BLE_CLIENT_ROLE
  581. mesh_client_update_conidx(active_conn_hndl);
  582. /* Setting State To Connected */
  583. blebrr_connect_state = 0x03;
  584. #endif /* BLE_CLIENT_ROLE */
  585. }
  586. /* Advertisement is disable by connection mostly */
  587. blebrr_advstate = 0x00;
  588. blebrr_pl_advertise_end();
  589. /**
  590. * Inform Application of GATT/BLE Link Layer Connection.
  591. */
  592. if (NULL != blebrr_gatt_iface_pl_cb)
  593. {
  594. blebrr_gatt_iface_pl_cb
  595. (
  596. BLEBRR_GATT_IFACE_UP, /* BLE Link Layer Connection */
  597. 0x00 /* Status is Success */
  598. );
  599. }
  600. return API_SUCCESS;
  601. }
  602. static API_RESULT blebrr_gatt_com_channel_setup_pl
  603. (
  604. UCHAR role,
  605. UCHAR mode,
  606. UCHAR evt
  607. )
  608. {
  609. API_RESULT retval;
  610. UINT16 mtu;
  611. /**
  612. * Possible Values of Role are
  613. * 1. 0x00 - GATT Client ~ BLEBRR_CLIENT_ROLE
  614. * 2. 0x01 - GATT Server ~ BLEBRR_SERVER_ROLE
  615. */
  616. /**
  617. * Possible Values of Mode are
  618. * 1. 0x00 - BLEBRR_GATT_PROV_MODE
  619. * 2. 0x01 - BLEBRR_GATT_PROXY_MODE
  620. */
  621. /**
  622. * Possible Values of evt are
  623. * 1. 0x00 - BLEBRR_COM_CHANNEL_CONNECT
  624. * 2. 0x01 - BLEBRR_COM_CHANNEL_DISCONNECT
  625. */
  626. retval = API_FAILURE;
  627. if (BLEBRR_COM_CHANNEL_CONNECT == evt)
  628. {
  629. /* Store the gatt role to be used during write */
  630. blebrr_gatt_role = role;
  631. /* Initialie MTU */
  632. mtu = BLEBRR_GATT_MIN_MTU;
  633. if (BLEBRR_SERVER_ROLE == role)
  634. {
  635. /* Fetch MTU from ATT and adjust it for Mesh */
  636. }
  637. else if (BLEBRR_CLIENT_ROLE == role)
  638. {
  639. /* Fetch MTU from ATT and adjust it for Mesh */
  640. }
  641. else
  642. {
  643. /* Empty */
  644. }
  645. retval = blebrr_pl_gatt_connection
  646. (
  647. &blebrr_gatt_handle_pl,
  648. role,
  649. mode,
  650. mtu
  651. );
  652. if (NULL != blebrr_gatt_iface_pl_cb)
  653. {
  654. blebrr_gatt_iface_pl_cb
  655. (
  656. BLEBRR_GATT_IFACE_ENABLE,
  657. mode /* BLEBRR_GATT_PROV_MODE or BLEBRR_GATT_PROXY_MODE */
  658. );
  659. }
  660. }
  661. else if (BLEBRR_COM_CHANNEL_DISCONNECT == evt)
  662. {
  663. /**
  664. * Currently BLE Bearer GATT Channel Disconnection
  665. * is called only from HCI/ACL link disconnection.
  666. */
  667. blebrr_gatt_role = 0xFF;
  668. /* Delete Device from the Bearer */
  669. retval = blebrr_pl_gatt_disconnection (&blebrr_gatt_handle_pl);
  670. blebrr_gatt_handle_pl = BRR_HANDLE_INVALID;
  671. if (NULL != blebrr_gatt_iface_pl_cb)
  672. {
  673. blebrr_gatt_iface_pl_cb
  674. (
  675. BLEBRR_GATT_IFACE_DISABLE,
  676. mode /* BLEBRR_GATT_PROV_MODE or BLEBRR_GATT_PROXY_MODE */
  677. );
  678. }
  679. }
  680. else
  681. {
  682. /* Empty */
  683. }
  684. return retval;
  685. }
  686. static uint16_t appl_mesh_prov_data_out_ccd_cb(uint16_t conn_hndl, uint8_t enabled)
  687. {
  688. /* Check the Current mode is not PROV */
  689. if (BLEBRR_GATT_PROV_MODE != blebrr_gatt_mode_get())
  690. {
  691. BLEBRRPL_LOG("Mesh Prov Out CCD being Written when PROV is not Active!\r\n");
  692. return 0xFFFF;
  693. }
  694. if (TRUE == enabled)
  695. {
  696. BLEBRRPL_LOG("Mesh Prov Out CCD Enabled");
  697. }
  698. else
  699. {
  700. BLEBRRPL_LOG("Mesh Prov Out CCD Disabled");
  701. }
  702. blebrr_gatt_com_channel_setup_pl
  703. (
  704. BLEBRR_SERVER_ROLE,
  705. BLEBRR_GATT_PROV_MODE,
  706. (enabled) ? BLEBRR_COM_CHANNEL_CONNECT : BLEBRR_COM_CHANNEL_DISCONNECT
  707. );
  708. return 0x0000;
  709. }
  710. static uint16_t appl_mesh_prov_data_in_wt_cb
  711. (
  712. uint16_t conn_hndl,
  713. uint16_t offset,
  714. uint16_t length,
  715. uint8_t * value
  716. )
  717. {
  718. if (NULL != value)
  719. {
  720. /* BLEBRRPL_LOG("Mesh Prov Data IN received");
  721. appl_dump_bytes(value, length); */
  722. BLEBRRPL_LOG("Mesh Prov Data IN received");
  723. blebrr_recv_mesh_packet_pl
  724. (
  725. &conn_hndl,
  726. offset,
  727. value,
  728. length
  729. );
  730. }
  731. return 0x0000;
  732. }
  733. static uint16_t appl_mesh_proxy_data_out_ccd_cb(uint16_t conn_hndl, uint8_t enabled)
  734. {
  735. /* Check the Current mode is not PROV */
  736. if (BLEBRR_GATT_PROXY_MODE != blebrr_gatt_mode_get())
  737. {
  738. BLEBRRPL_LOG("Mesh Proxy Out CCD being Written when PROXY is not Active!\r\n");
  739. return 0xFFFF;
  740. }
  741. if (TRUE == enabled)
  742. {
  743. BLEBRRPL_LOG("Mesh Proxy Out CCD Enabled");
  744. }
  745. else
  746. {
  747. BLEBRRPL_LOG("Mesh Proxy Out CCD Disabled");
  748. }
  749. blebrr_gatt_com_channel_setup_pl
  750. (
  751. BLEBRR_SERVER_ROLE,
  752. BLEBRR_GATT_PROXY_MODE,
  753. (enabled) ? BLEBRR_COM_CHANNEL_CONNECT : BLEBRR_COM_CHANNEL_DISCONNECT
  754. );
  755. return 0x0000;
  756. }
  757. static uint16_t appl_mesh_proxy_data_in_wt_cb
  758. (
  759. uint16_t conn_hndl,
  760. uint16_t offset,
  761. uint16_t length,
  762. uint8_t * value
  763. )
  764. {
  765. if (NULL != value)
  766. {
  767. BLEBRRPL_LOG("Mesh Proxy Data IN received");
  768. //appl_dump_bytes(value, length);
  769. blebrr_recv_mesh_packet_pl
  770. (
  771. &conn_hndl,
  772. offset,
  773. value,
  774. length
  775. );
  776. }
  777. return 0x0000;
  778. }
  779. #ifdef BLE_CLIENT_ROLE
  780. void appl_mesh_prov_data_out_notif_cb
  781. (
  782. uint16_t conidx,
  783. uint16_t length,
  784. uint8_t * value
  785. )
  786. {
  787. #if 0
  788. printf("\r\n Mesh PROV Data Out NTFs:\r\n");
  789. appl_dump_bytes
  790. (
  791. value,
  792. length
  793. );
  794. #endif /* 0 */
  795. blebrr_recv_mesh_packet_pl
  796. (
  797. &active_conn_hndl,
  798. 0x0000,
  799. (UCHAR *)value,
  800. length
  801. );
  802. }
  803. void appl_mesh_prov_notif_config_status_cb
  804. (
  805. uint16_t conidx,
  806. uint8_t flag,
  807. uint8_t status
  808. )
  809. {
  810. if (status == 0x00)
  811. {
  812. printf("Mesh Provisioning Data Out notifications %s\r\n",
  813. flag ? "enabled" : "disabled");
  814. blebrr_gatt_mode_set(BLEBRR_GATT_PROV_MODE);
  815. // appl_prov_register();
  816. // appl_prov_setup(PROV_ROLE_PROVISIONER, PROV_BRR_GATT);
  817. blebrr_gatt_com_channel_setup_pl
  818. (
  819. BLEBRR_CLIENT_ROLE,
  820. BLEBRR_GATT_PROV_MODE,
  821. (flag)? BLEBRR_COM_CHANNEL_CONNECT : BLEBRR_COM_CHANNEL_DISCONNECT
  822. );
  823. }
  824. else
  825. {
  826. printf("ERROR: failed to set notifications (0x%02x)\r\n", status);
  827. }
  828. }
  829. static mesh_prov_client_cb mesh_prov_callbacks =
  830. {
  831. .mesh_prov_data_out_notif = appl_mesh_prov_data_out_notif_cb,
  832. .mesh_prov_ntf_status = appl_mesh_prov_notif_config_status_cb,
  833. };
  834. void appl_mesh_proxy_data_out_notif_cb
  835. (
  836. uint16_t conidx,
  837. uint16_t length,
  838. uint8_t * value
  839. )
  840. {
  841. #if 0
  842. printf("\r\n Mesh PROXY Data Out NTFs:\r\n");
  843. appl_dump_bytes
  844. (
  845. value,
  846. length
  847. );
  848. #endif /* 0 */
  849. blebrr_recv_mesh_packet_pl
  850. (
  851. &active_conn_hndl,
  852. 0x0000,
  853. (UCHAR *)value,
  854. length
  855. );
  856. }
  857. void appl_mesh_proxy_notif_config_status_cb
  858. (
  859. uint16_t conidx,
  860. uint8_t flag,
  861. uint8_t status
  862. )
  863. {
  864. if (status == 0x00)
  865. {
  866. printf("Mesh Proxy Data Out notifications %s\r\n",
  867. flag ? "enabled" : "disabled");
  868. blebrr_gatt_mode_set(BLEBRR_GATT_PROXY_MODE);
  869. blebrr_gatt_com_channel_setup_pl
  870. (
  871. BLEBRR_CLIENT_ROLE,
  872. BLEBRR_GATT_PROXY_MODE,
  873. (flag)? BLEBRR_COM_CHANNEL_CONNECT : BLEBRR_COM_CHANNEL_DISCONNECT
  874. );
  875. }
  876. else
  877. {
  878. printf("ERROR: failed to set notifications (0x%02x)\r\n", status);
  879. }
  880. }
  881. static mesh_proxy_client_cb mesh_proxy_callbacks =
  882. {
  883. .mesh_proxy_data_out_notif = appl_mesh_proxy_data_out_notif_cb,
  884. .mesh_proxy_ntf_status = appl_mesh_proxy_notif_config_status_cb,
  885. };
  886. #endif /* BLE_CLIENT_ROLE */
  887. /** Dummy Interfaces: To be filled for Client Role */
  888. API_RESULT blebrr_scan_cmd_handler_pl(UCHAR enable)
  889. {
  890. if (enable)
  891. {
  892. BLEBRRPL_LOG("\n Scan Start Feature to be extended for CLI\n");
  893. }
  894. else
  895. {
  896. BLE_gap_set_scan_enable (0x00);
  897. }
  898. return API_SUCCESS;
  899. }
  900. API_RESULT blebrr_create_gatt_conn_pl
  901. (
  902. UCHAR p_bdaddr_type,
  903. UCHAR * p_bdaddr
  904. )
  905. {
  906. #ifdef BLE_CLIENT_ROLE
  907. bStatus_t ret;
  908. if (0x00 != blebrr_scanstate)
  909. {
  910. /* Update global scan state */
  911. blebrr_scanstate = 0x00;
  912. /* Disable Scan */
  913. ret = BLE_gap_set_scan_enable (0x00);
  914. BLEBRRPL_LOG ("Disabling Scan...with retval 0x%04X\r\n", ret);
  915. if (0x00 == ret)
  916. {
  917. /* Scan Disable Successful */
  918. /* Move to To Initiate Connection Phase */
  919. blebrr_connect_state = 0x01;
  920. /** Save the global Address */
  921. EM_mem_copy(blebrr_addr_to_conn, p_bdaddr, 6);
  922. blebrr_addr_type_to_conn = p_bdaddr_type;
  923. }
  924. else
  925. {
  926. /* Scan Stop Failed */
  927. BLEBRRPL_LOG ("Scan Disabled Failed wit ret 0x%02X", ret);
  928. }
  929. return (0 == ret) ? API_SUCCESS : API_FAILURE;
  930. }
  931. else
  932. {
  933. BLEBRRPL_LOG ("Scan already Disabled! Initiating Connection...");
  934. ret = BLE_gap_connect
  935. (
  936. 0x00,
  937. p_bdaddr,
  938. p_bdaddr_type
  939. );
  940. blebrr_connect_state = 0x02;
  941. /* Set GATT Role as Client */
  942. blebrr_gatt_role = BLEBRR_CLIENT_ROLE;
  943. BLEBRRPL_LOG("Initiating Connection to Address "
  944. "0x%02X:0x%02X:0x%02X:0x%02X:0x%02X:0x%02X Type 0x%02X "
  945. "with retval 0x%04X\r\n",
  946. p_bdaddr[0], p_bdaddr[1], p_bdaddr[2], p_bdaddr[3],
  947. p_bdaddr[4], p_bdaddr[5], p_bdaddr_type, ret);
  948. return (0 == ret) ? API_SUCCESS : API_FAILURE;
  949. }
  950. #else /* BLE_CLIENT_ROLE */
  951. return API_FAILURE;
  952. #endif /* BLE_CLIENT_ROLE */
  953. }
  954. API_RESULT blebrr_disconnect_pl(void)
  955. {
  956. bStatus_t ret;
  957. ret = BLE_gap_disconnect(active_conn_hndl);
  958. BLEBRRPL_LOG("\r\n Initiating Disconnection with Connection Handle 0x%04X"
  959. "with retval 0x%04X\r\n", active_conn_hndl, ret);
  960. return (0 == ret) ? API_SUCCESS : API_FAILURE;
  961. }
  962. API_RESULT blebrr_discover_service_pl(UCHAR serv)
  963. {
  964. /* Set the mode with bearer */
  965. (serv == 0) ? blebrr_gatt_mode_set(BLEBRR_GATT_PROV_MODE) :
  966. blebrr_gatt_mode_set(BLEBRR_GATT_PROXY_MODE);
  967. #ifdef BLE_CLIENT_ROLE
  968. /* Register the corresponding Callbacks */
  969. (serv == BLEBRR_GATT_PROV_MODE) ? \
  970. mesh_prov_client_init(&mesh_prov_callbacks) : \
  971. mesh_proxy_client_init(&mesh_proxy_callbacks);
  972. return mesh_client_discover_services
  973. (
  974. active_conn_hndl,
  975. serv
  976. );
  977. #endif /* BLE_CLIENT_ROLE */
  978. }
  979. API_RESULT blebrr_confige_ntf_pl(UCHAR config_ntf, UCHAR mode)
  980. {
  981. #ifdef BLE_CLIENT_ROLE
  982. return mesh_client_config_ntf(active_conn_hndl, mode, (0x00 == config_ntf) ? false:true);
  983. #else /* BLE_CLIENT_ROLE */
  984. BLEBRRPL_LOG("\r\n BLE_CLIENT_ROLE Disabled!\r\n");
  985. return API_FAILURE;
  986. #endif /* BLE_CLIENT_ROLE */
  987. }
  988. API_RESULT blebrr_handle_le_disconnection_pl
  989. (
  990. uint16_t conn_idx,
  991. uint16_t conn_hndl,
  992. uint8_t reason
  993. )
  994. {
  995. API_RESULT retval;
  996. retval = API_FAILURE;
  997. BLEBRRPL_LOG("Device Disconnected - Handle: 0x%04X with Reason: 0x%02X\r\n", conn_hndl, reason);
  998. if (active_conn_hndl == conn_hndl)
  999. {
  1000. /* Reinitialize Connection Handle */
  1001. active_conn_hndl = BLEBRR_CONN_HNDL_INVALID;
  1002. #ifdef BLE_CLIENT_ROLE
  1003. mesh_client_update_conidx(active_conn_hndl);
  1004. /* Setting State to Idle/Disconnected */
  1005. blebrr_connect_state = 0x00;
  1006. #endif /* BLE_CLIENT_ROLE */
  1007. }
  1008. /* Inform Disconnection to GATT Bearer */
  1009. blebrr_gatt_role = 0xFF;
  1010. retval = blebrr_pl_gatt_disconnection
  1011. (
  1012. &blebrr_gatt_handle_pl
  1013. );
  1014. blebrr_gatt_handle_pl = BRR_HANDLE_INVALID;
  1015. /**
  1016. * Inform Application of GATT/BLE Link Layer Connection.
  1017. */
  1018. if (NULL != blebrr_gatt_iface_pl_cb)
  1019. {
  1020. blebrr_gatt_iface_pl_cb
  1021. (
  1022. BLEBRR_GATT_IFACE_DOWN, /* BLE Link Layer Disconnection */
  1023. 0x00 /* Status is Success */
  1024. );
  1025. }
  1026. return retval;
  1027. }
  1028. void blebrr_enable_mesh_serv_pl (UCHAR serv_type)
  1029. {
  1030. BLEBRRPL_LOG ("Serv Enable called with 0x%02X", serv_type);
  1031. /* If serv_type :
  1032. * BLEBRR_GATT_PROVILINK - Mesh Prov
  1033. * BLEBRR_GATT_PROXYLINK - Mesh Proxy
  1034. */
  1035. if (BLEBRR_GATT_PROV_MODE == serv_type)
  1036. {
  1037. BLEBRRPL_LOG ("Enabling Mesh Prov Service...\r\n");
  1038. mesh_prov_init(&appl_mesh_prov_cb);
  1039. }
  1040. else
  1041. {
  1042. BLEBRRPL_LOG ("Enabling Mesh Proxy Service...\r\n");
  1043. mesh_proxy_init(&appl_mesh_proxy_cb);
  1044. }
  1045. }
  1046. void blebrr_disable_mesh_serv_pl (UCHAR serv_type)
  1047. {
  1048. BLEBRRPL_LOG ("Serv Disable called with 0x%02X", serv_type);
  1049. /* If serv_type :
  1050. * BLEBRR_GATT_PROVILINK - Mesh Prov
  1051. * BLEBRR_GATT_PROXYLINK - Mesh Proxy
  1052. */
  1053. if (BLEBRR_GATT_PROV_MODE == serv_type)
  1054. {
  1055. BLEBRRPL_LOG ("Disabling Mesh Prov Service...\r\n");
  1056. /*
  1057. * Disable Mesh Provisioing Serivce
  1058. */
  1059. mesh_prov_deinit();
  1060. }
  1061. else
  1062. {
  1063. BLEBRRPL_LOG ("Disabling Mesh Proxy Service...\r\n");
  1064. /*
  1065. * Disable Mesh Proxy Serivce
  1066. */
  1067. mesh_proxy_deinit();
  1068. }
  1069. BLEBRRPL_LOG("Service Disable yet to be Supported\r\n");
  1070. }
  1071. void blebrr_set_gattmode_pl (UCHAR flag)
  1072. {
  1073. /* Setting Provisioning or Proxy Mode */
  1074. if (0xFF != flag)
  1075. {
  1076. blebrr_disable_mesh_serv_pl(curr_service);
  1077. blebrr_enable_mesh_serv_pl(flag);
  1078. curr_service = flag;
  1079. }
  1080. else
  1081. {
  1082. /* Do Nothing */
  1083. }
  1084. }