luat_ble_server_api.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <assert.h>
  5. #include "wm_bt_config.h"
  6. // #if (WM_NIMBLE_INCLUDED == CFG_ON)
  7. #include "wm_bt_app.h"
  8. #include "wm_bt_util.h"
  9. #include "host/ble_hs.h"
  10. #include "wm_ble_gap.h"
  11. #include "wm_ble_uart_if.h"
  12. int tls_ble_server_api_init(tls_ble_output_func_ptr output_func_ptr);
  13. int tls_ble_server_api_deinit();
  14. uint32_t tls_ble_server_api_get_mtu();
  15. int tls_ble_server_api_send_msg(uint8_t *data, int data_len);
  16. #include "luat_base.h"
  17. #include "luat_msgbus.h"
  18. #include "luat_malloc.h"
  19. #define LUAT_LOG_TAG "ble"
  20. #include "luat_log.h"
  21. typedef struct ble_write_msg {
  22. // uint16_t conn_handle,
  23. // uint16_t attr_handle,
  24. ble_uuid_t* uuid;
  25. uint16_t len;
  26. char buff[1];
  27. }ble_write_msg_t;
  28. /*
  29. * GLOBAL VARIABLE DEFINITIONS
  30. ****************************************************************************************
  31. */
  32. typedef enum{
  33. BLE_SERVER_MODE_IDLE = 0x00,
  34. BLE_SERVER_MODE_ADVERTISING = 0x01,
  35. BLE_SERVER_MODE_CONNECTED,
  36. BLE_SERVER_MODE_INDICATING,
  37. BLE_SERVER_MODE_EXITING
  38. } ble_server_state_t;
  39. static uint8_t g_ble_prof_connected = 0;
  40. static uint8_t g_ble_indicate_enable = 0;
  41. static tls_ble_output_func_ptr g_ble_uart_output_fptr = NULL;
  42. static int g_mtu = 20;
  43. static uint8_t g_ind_data[255];
  44. static volatile uint8_t g_send_pending = 0;
  45. static volatile ble_server_state_t g_ble_server_state = BLE_SERVER_MODE_IDLE;
  46. /* ble attr write/notify handle */
  47. uint16_t g_ble_attr_indicate_handle;
  48. uint16_t g_ble_attr_write_handle;
  49. uint16_t g_ble_attr_notify_handle;
  50. uint16_t g_ble_conn_handle ;
  51. #define WM_GATT_SVC_UUID 0xFFF0
  52. #define WM_GATT_INDICATE_UUID 0xFFF1
  53. #define WM_GATT_WRITE_UUID 0xFFF2
  54. #define WM_GATT_NOTIFY_UUID 0xFFF3
  55. static int
  56. gatt_svr_chr_access_func(uint16_t conn_handle, uint16_t attr_handle,
  57. struct ble_gatt_access_ctxt *ctxt, void *arg);
  58. /*
  59. * LOCAL FUNCTION DECLARATIONS
  60. ****************************************************************************************
  61. */
  62. static int
  63. gatt_svr_chr_write_func(uint16_t conn_handle, uint16_t attr_handle,
  64. struct ble_gatt_access_ctxt *ctxt, void *arg);
  65. static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
  66. {
  67. /* Service: uart */
  68. .type = BLE_GATT_SVC_TYPE_PRIMARY,
  69. .uuid = BLE_UUID16_DECLARE(WM_GATT_SVC_UUID),
  70. .characteristics = (struct ble_gatt_chr_def[]) { {
  71. .uuid = BLE_UUID16_DECLARE(WM_GATT_WRITE_UUID),
  72. .val_handle = &g_ble_attr_write_handle,
  73. .access_cb = gatt_svr_chr_access_func,
  74. .flags = BLE_GATT_CHR_F_WRITE,
  75. },{
  76. .uuid = BLE_UUID16_DECLARE(WM_GATT_INDICATE_UUID),
  77. .val_handle = &g_ble_attr_indicate_handle,
  78. .access_cb = gatt_svr_chr_access_func,
  79. .flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_READ,
  80. },{
  81. // 暂不支持NOTIFY
  82. // .uuid = BLE_UUID16_DECLARE(WM_GATT_NOTIFY_UUID),
  83. // .val_handle = &g_ble_attr_notify_handle,
  84. // .access_cb = gatt_svr_chr_access_func,
  85. // .flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ,
  86. // },{
  87. 0, /* No more characteristics in this service */
  88. }
  89. },
  90. },
  91. {
  92. 0, /* No more services */
  93. },
  94. };
  95. int wm_ble_server_api_adv(bool enable)
  96. {
  97. int rc;
  98. if(enable)
  99. {
  100. struct ble_hs_adv_fields fields;
  101. const char *name;
  102. /**
  103. * Set the advertisement data included in our advertisements:
  104. * o Flags (indicates advertisement type and other general info).
  105. * o Device name.
  106. * o user specific field (winner micro).
  107. */
  108. memset(&fields, 0, sizeof fields);
  109. /* Advertise two flags:
  110. * o Discoverability in forthcoming advertisement (general)
  111. * o BLE-only (BR/EDR unsupported).
  112. */
  113. fields.flags = BLE_HS_ADV_F_DISC_GEN |
  114. BLE_HS_ADV_F_BREDR_UNSUP;
  115. name = ble_svc_gap_device_name();
  116. fields.name = (uint8_t *)name;
  117. fields.name_len = strlen(name);
  118. fields.name_is_complete = 1;
  119. fields.uuids16 = (ble_uuid16_t[]){
  120. BLE_UUID16_INIT(0xFFF0)
  121. };
  122. fields.num_uuids16 = 1;
  123. fields.uuids16_is_complete = 1;
  124. rc = ble_gap_adv_set_fields(&fields);
  125. if (rc != 0) {
  126. TLS_BT_APPL_TRACE_ERROR("error setting advertisement data; rc=%d\r\n", rc);
  127. return rc;
  128. }
  129. /* As own address type we use hard-coded value, because we generate
  130. NRPA and by definition it's random */
  131. rc = tls_nimble_gap_adv(WM_BLE_ADV_IND, 0);
  132. assert(rc == 0);
  133. }else
  134. {
  135. rc = tls_nimble_gap_adv(WM_BLE_ADV_STOP, 0);
  136. }
  137. return rc;
  138. }
  139. /*
  140. * LOCAL FUNCTION DEFINITIONS
  141. ****************************************************************************************
  142. */
  143. static int l_ble_chr_write_cb(lua_State* L, void* ptr) {
  144. ble_write_msg_t* wmsg = (ble_write_msg_t*)ptr;
  145. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  146. lua_getglobal(L, "sys_pub");
  147. if (lua_isfunction(L, -1)) {
  148. lua_pushstring(L, "BLE_GATT_WRITE_CHR");
  149. lua_newtable(L);
  150. lua_pushlstring(L, wmsg->buff, wmsg->len);
  151. lua_call(L, 3, 0);
  152. }
  153. return 0;
  154. }
  155. static int
  156. gatt_svr_chr_access_func(uint16_t conn_handle, uint16_t attr_handle,
  157. struct ble_gatt_access_ctxt *ctxt, void *arg)
  158. {
  159. int i = 0;
  160. struct os_mbuf *om = ctxt->om;
  161. ble_write_msg_t* wmsg;
  162. rtos_msg_t msg = {0};
  163. LLOGD("gatt_svr_chr_access_func %d %d %d", conn_handle, attr_handle, ctxt->op);
  164. switch (ctxt->op) {
  165. case BLE_GATT_ACCESS_OP_WRITE_CHR:
  166. while(om) {
  167. wmsg = (ble_write_msg_t*)(luat_heap_malloc(sizeof(ble_write_msg_t) + om->om_len - 1));
  168. if (wmsg != NULL) {
  169. wmsg->len = om->om_len;
  170. msg.handler = l_ble_chr_write_cb;
  171. msg.ptr = wmsg;
  172. msg.arg1 = conn_handle;
  173. msg.arg2 = attr_handle;
  174. memcpy(wmsg->buff, om->om_data, om->om_len);
  175. luat_msgbus_put(&msg, 0);
  176. }
  177. // if(g_ble_uart_output_fptr)
  178. // {
  179. // g_ble_uart_output_fptr((uint8_t *)om->om_data, om->om_len);
  180. // }else
  181. // {
  182. // print_bytes(om->om_data, om->om_len);
  183. // }
  184. om = SLIST_NEXT(om, om_next);
  185. }
  186. return 0;
  187. case BLE_GATT_ACCESS_OP_READ_CHR:
  188. return 0;
  189. default:
  190. assert(0);
  191. return BLE_ATT_ERR_UNLIKELY;
  192. }
  193. }
  194. int
  195. wm_ble_server_gatt_svr_init(void)
  196. {
  197. int rc;
  198. rc = ble_gatts_count_cfg(gatt_svr_svcs);
  199. if (rc != 0) {
  200. goto err;
  201. }
  202. rc = ble_gatts_add_svcs(gatt_svr_svcs);
  203. if (rc != 0) {
  204. return rc;
  205. }
  206. err:
  207. return rc;
  208. }
  209. // static uint8_t ss = 0x00;
  210. static int indication_sent_cb(lua_State *L, void* ptr) {
  211. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  212. lua_getglobal(L, "sys_pub");
  213. if (lua_isfunction(L, -1)) {
  214. lua_pushstring(L, "BLE_IND_SENT");
  215. lua_pushinteger(L, msg->arg1);
  216. lua_pushinteger(L, msg->arg2);
  217. lua_call(L, 3, 0);
  218. }
  219. return 0;
  220. }
  221. static void ble_server_indication_sent_cb(int conn_id, int status)
  222. {
  223. int len = 0;
  224. tls_bt_status_t ret;
  225. g_send_pending = 0;
  226. LLOGD("indication_sent_cb %d %d", conn_id, status);
  227. // if(!g_ble_indicate_enable)
  228. // {
  229. // TLS_BT_APPL_TRACE_DEBUG("Indicate disabled... when trying to send...\r\n");
  230. // return;
  231. // }
  232. rtos_msg_t msg = {0};
  233. msg.handler = indication_sent_cb;
  234. msg.arg1 = conn_id;
  235. msg.arg2 = status;
  236. luat_msgbus_put(&msg, 0);
  237. // if(g_ble_uart_output_fptr == NULL)
  238. // {
  239. // memset(g_ind_data, ss, sizeof(g_ind_data));
  240. // ss++;
  241. // if(ss > 0xFE) ss = 0x00;
  242. // tls_ble_server_api_send_msg(g_ind_data, g_mtu);
  243. // }
  244. }
  245. static void wm_ble_server_start_indicate(void *arg)
  246. {
  247. int len;
  248. int rc;
  249. uint8_t *tmp_ptr = NULL;
  250. tls_bt_status_t status;
  251. /*No uart ble interface*/
  252. if(g_ble_uart_output_fptr == NULL)
  253. {
  254. rc = tls_ble_server_api_send_msg(g_ind_data, g_mtu);
  255. TLS_BT_APPL_TRACE_DEBUG("Indicating sending...rc=%d\r\n", rc);
  256. }
  257. // else
  258. // {
  259. // /*check and send*/
  260. // len = tls_ble_uart_buffer_size();
  261. // len = MIN(len, g_mtu);
  262. // if(len)
  263. // {
  264. // tls_ble_uart_buffer_peek(g_ind_data, len);
  265. // status = tls_ble_server_api_send_msg(g_ind_data, g_mtu);
  266. // if(status == TLS_BT_STATUS_SUCCESS)
  267. // {
  268. // tls_ble_uart_buffer_delete(len);
  269. // }else
  270. // {
  271. // TLS_BT_APPL_TRACE_DEBUG("Server send failed(%d), retry...\r\n", status);
  272. // tls_bt_async_proc_func(wm_ble_server_start_indicate,(void*)g_ble_indicate_enable,1000);
  273. // }
  274. // }
  275. // }
  276. }
  277. static void conn_param_update_cb(uint16_t conn_handle, int status, void *arg)
  278. {
  279. LLOGD("conn param update complete; conn_handle=%d status=%d", conn_handle, status);
  280. }
  281. static void wm_ble_server_conn_param_update_slave()
  282. {
  283. int rc;
  284. struct ble_l2cap_sig_update_params params;
  285. params.itvl_min = 0x0006;
  286. params.itvl_max = 0x0006;
  287. params.slave_latency = 0;
  288. params.timeout_multiplier = 0x07d0;
  289. rc = ble_l2cap_sig_update(g_ble_conn_handle, &params,
  290. conn_param_update_cb, NULL);
  291. assert(rc == 0);
  292. }
  293. static int luat_ble_gap_evt(lua_State *L, void* ptr) {
  294. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  295. lua_getglobal(L, "sys_pub");
  296. if (lua_isfunction(L, -1)) {
  297. lua_pushstring(L, "BLE_GAP_EVT");
  298. lua_pushinteger(L, msg->arg1);
  299. lua_pushinteger(L, msg->arg2);
  300. lua_call(L, 3, 0);
  301. }
  302. return 0;
  303. }
  304. static int ble_gap_evt_cb(struct ble_gap_event *event, void *arg)
  305. {
  306. int rc;
  307. struct ble_gap_conn_desc desc;
  308. rtos_msg_t msg = {0};
  309. msg.handler = luat_ble_gap_evt;
  310. msg.arg1 = event->type;
  311. switch(event->type)
  312. {
  313. case BLE_GAP_EVENT_CONNECT:
  314. g_ble_prof_connected = 1;
  315. g_send_pending = 0;
  316. msg.arg2 = event->connect.status;
  317. LLOGD("connected status=%d handle=%d,g_ble_attr_indicate_handle=%d",event->connect.status, g_ble_conn_handle, g_ble_attr_indicate_handle );
  318. if (event->connect.status == 0) {
  319. g_ble_server_state = BLE_SERVER_MODE_CONNECTED;
  320. //re set this flag, to prevent stop adv, but connected evt reported when deinit this demo
  321. g_ble_conn_handle = event->connect.conn_handle;
  322. rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
  323. assert(rc == 0);
  324. print_conn_desc(&desc);
  325. #if MYNEWT_VAL(BLEPRPH_LE_PHY_SUPPORT)
  326. phy_conn_changed(event->connect.conn_handle);
  327. #endif
  328. }
  329. //TLS_BT_APPL_TRACE_DEBUG("\r\n");
  330. if (event->connect.status != 0) {
  331. /* Connection failed; resume advertising. */
  332. tls_nimble_gap_adv(WM_BLE_ADV_IND, 0);
  333. }
  334. luat_msgbus_put(&msg, 0);
  335. break;
  336. case BLE_GAP_EVENT_DISCONNECT:
  337. g_ble_prof_connected = 0;
  338. g_ble_indicate_enable = 0;
  339. g_send_pending = 0;
  340. LLOGD("disconnect reason=%d,state=%d", event->disconnect.reason,g_ble_server_state);
  341. if(g_ble_server_state == BLE_SERVER_MODE_EXITING)
  342. {
  343. if(g_ble_uart_output_fptr)
  344. {
  345. g_ble_uart_output_fptr = NULL;
  346. }
  347. g_ble_server_state = BLE_SERVER_MODE_IDLE;
  348. }else
  349. {
  350. rc = tls_nimble_gap_adv(WM_BLE_ADV_IND, 0);
  351. if(!rc)
  352. {
  353. g_ble_server_state = BLE_SERVER_MODE_ADVERTISING;
  354. }
  355. }
  356. #if 0
  357. if(event->disconnect.reason == 534)
  358. {
  359. //hci error code: 0x16 + 0x200 = 534; //local host terminate the connection;
  360. }else
  361. {
  362. tls_nimble_gap_adv(WM_BLE_ADV_IND, 0);
  363. }
  364. #endif
  365. luat_msgbus_put(&msg, 0);
  366. break;
  367. case BLE_GAP_EVENT_NOTIFY_TX:
  368. if(event->notify_tx.status == BLE_HS_EDONE)
  369. {
  370. ble_server_indication_sent_cb(event->notify_tx.attr_handle, event->notify_tx.status);
  371. }else
  372. {
  373. /*Application will handle other cases*/
  374. }
  375. msg.arg2 = event->notify_tx.status;
  376. luat_msgbus_put(&msg, 0);
  377. break;
  378. case BLE_GAP_EVENT_SUBSCRIBE:
  379. LLOGD("subscribe indicate(%d,%d)", event->subscribe.prev_indicate,event->subscribe.cur_indicate );
  380. LLOGD("subscribe notify(%d,%d)", event->subscribe.prev_notify,event->subscribe.cur_notify );
  381. g_ble_indicate_enable = event->subscribe.cur_indicate;
  382. // if(g_ble_indicate_enable)
  383. // {
  384. // g_ble_server_state = BLE_SERVER_MODE_INDICATING;
  385. // /*To reach the max passthrough, in ble_uart mode, I conifg the min connection_interval*/
  386. // if(g_ble_uart_output_fptr)
  387. // {
  388. // tls_bt_async_proc_func(wm_ble_server_conn_param_update_slave, NULL, 30);
  389. // }
  390. // tls_bt_async_proc_func(wm_ble_server_start_indicate,(void*)g_ble_indicate_enable, 30);
  391. // }else
  392. // {
  393. // if(g_ble_server_state != BLE_SERVER_MODE_EXITING)
  394. // {
  395. // g_ble_server_state = BLE_SERVER_MODE_CONNECTED;
  396. // }
  397. // }
  398. msg.arg2 = event->subscribe.cur_indicate;
  399. luat_msgbus_put(&msg, 0);
  400. break;
  401. case BLE_GAP_EVENT_MTU:
  402. LLOGD("wm ble dm mtu changed to(%d)", event->mtu.value);
  403. /*nimBLE config prefered ATT_MTU is 256. here 256-12 = 244. */
  404. /* preamble(1)+access address(4)+pdu(2~257)+crc*/
  405. /* ATT_MTU(247):pdu= pdu_header(2)+l2cap_len(2)+l2cap_chn(2)+mic(4)*/
  406. /* GATT MTU(244): ATT_MTU +opcode+chn*/
  407. g_mtu = min(event->mtu.value - 12, 244);
  408. msg.arg2 = event->mtu.value;
  409. luat_msgbus_put(&msg, 0);
  410. break;
  411. case BLE_GAP_EVENT_REPEAT_PAIRING:
  412. /* We already have a bond with the peer, but it is attempting to
  413. * establish a new secure link. This app sacrifices security for
  414. * convenience: just throw away the old bond and accept the new link.
  415. */
  416. /* Delete the old bond. */
  417. rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
  418. assert(rc == 0);
  419. ble_store_util_delete_peer(&desc.peer_id_addr);
  420. LLOGD("!!!BLE_GAP_EVENT_REPEAT_PAIRING");
  421. return BLE_GAP_REPEAT_PAIRING_RETRY;
  422. case BLE_GAP_EVENT_PASSKEY_ACTION:
  423. LLOGD(">>>BLE_GAP_EVENT_REPEAT_PAIRING");
  424. return 0;
  425. default:
  426. break;
  427. }
  428. return 0;
  429. }
  430. /*
  431. * EXPORTED FUNCTION DEFINITIONS
  432. ****************************************************************************************
  433. */
  434. int tls_ble_server_api_init(tls_ble_output_func_ptr output_func_ptr)
  435. {
  436. int rc = BLE_HS_EAPP;
  437. if(bt_adapter_state == WM_BT_STATE_OFF)
  438. {
  439. TLS_BT_APPL_TRACE_ERROR("%s failed rc=%s\r\n", __FUNCTION__, tls_bt_rc_2_str(BLE_HS_EDISABLED));
  440. return BLE_HS_EDISABLED;
  441. }
  442. TLS_BT_APPL_TRACE_DEBUG("%s, state=%d\r\n", __FUNCTION__, g_ble_server_state);
  443. if(g_ble_server_state == BLE_SERVER_MODE_IDLE)
  444. {
  445. g_ble_prof_connected = 0;
  446. //step 0: reset other services. Note
  447. rc = ble_gatts_reset();
  448. if(rc != 0)
  449. {
  450. TLS_BT_APPL_TRACE_ERROR("tls_ble_server_api_init failed rc=%d\r\n", rc);
  451. return rc;
  452. }
  453. //step 1: config/adding the services
  454. rc = wm_ble_server_gatt_svr_init();
  455. if(rc == 0)
  456. {
  457. tls_ble_register_gap_evt(WM_BLE_GAP_EVENT_CONNECT|WM_BLE_GAP_EVENT_DISCONNECT|WM_BLE_GAP_EVENT_NOTIFY_TX|WM_BLE_GAP_EVENT_SUBSCRIBE|WM_BLE_GAP_EVENT_MTU|WM_BLE_GAP_EVENT_REPEAT_PAIRING, ble_gap_evt_cb);
  458. TLS_BT_APPL_TRACE_DEBUG("### wm_ble_server_api_init \r\n");
  459. g_ble_uart_output_fptr = output_func_ptr;
  460. /*step 2: start the service*/
  461. rc = ble_gatts_start();
  462. assert(rc == 0);
  463. /*step 3: start advertisement*/
  464. rc = wm_ble_server_api_adv(true);
  465. if(rc == 0)
  466. {
  467. g_ble_server_state = BLE_SERVER_MODE_ADVERTISING;
  468. }
  469. }else
  470. {
  471. TLS_BT_APPL_TRACE_ERROR("### wm_ble_server_api_init failed(rc=%d)\r\n", rc);
  472. }
  473. }
  474. else
  475. {
  476. TLS_BT_APPL_TRACE_WARNING("wm_ble_server_api_init registered\r\n");
  477. rc = BLE_HS_EALREADY;
  478. }
  479. return rc;
  480. }
  481. int tls_ble_server_api_deinit()
  482. {
  483. int rc = BLE_HS_EAPP;
  484. if(bt_adapter_state == WM_BT_STATE_OFF)
  485. {
  486. TLS_BT_APPL_TRACE_ERROR("%s failed rc=%s\r\n", __FUNCTION__, tls_bt_rc_2_str(BLE_HS_EDISABLED));
  487. return BLE_HS_EDISABLED;
  488. }
  489. TLS_BT_APPL_TRACE_DEBUG("%s, state=%d\r\n", __FUNCTION__, g_ble_server_state);
  490. if(g_ble_server_state == BLE_SERVER_MODE_CONNECTED || g_ble_server_state == BLE_SERVER_MODE_INDICATING)
  491. {
  492. g_ble_indicate_enable = 0;
  493. rc = ble_gap_terminate(g_ble_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
  494. if(rc == 0)
  495. {
  496. g_ble_server_state = BLE_SERVER_MODE_EXITING;
  497. }
  498. }else if(g_ble_server_state == BLE_SERVER_MODE_ADVERTISING)
  499. {
  500. rc = tls_nimble_gap_adv(WM_BLE_ADV_STOP, 0);
  501. if(rc == 0)
  502. {
  503. if(g_ble_uart_output_fptr)
  504. {
  505. g_ble_uart_output_fptr = NULL;
  506. }
  507. g_send_pending = 0;
  508. g_ble_server_state = BLE_SERVER_MODE_IDLE;
  509. }
  510. }else if(g_ble_server_state == BLE_SERVER_MODE_IDLE)
  511. {
  512. rc = 0;
  513. }else
  514. {
  515. rc = BLE_HS_EALREADY;
  516. }
  517. return rc;
  518. }
  519. uint32_t tls_ble_server_api_get_mtu()
  520. {
  521. return g_mtu;
  522. }
  523. int tls_ble_server_api_send_msg(uint8_t *data, int data_len)
  524. {
  525. int rc;
  526. struct os_mbuf *om;
  527. //TLS_BT_APPL_TRACE_DEBUG("### %s len=%d\r\n", __FUNCTION__, data_len);
  528. if(g_send_pending) return BLE_HS_EBUSY;
  529. if(data_len<=0 || data == NULL)
  530. {
  531. return BLE_HS_EINVAL;
  532. }
  533. om = ble_hs_mbuf_from_flat(data, data_len);
  534. if (!om) {
  535. return BLE_HS_ENOMEM;
  536. }
  537. rc = ble_gattc_indicate_custom(g_ble_conn_handle,g_ble_attr_indicate_handle, om);
  538. // rc = ble_gattc_notify_custom(g_ble_conn_handle,g_ble_attr_notify_handle, om);
  539. if(rc == 0)
  540. {
  541. g_send_pending = 1;
  542. }
  543. return rc;
  544. }
  545. // #endif