luat_ble_server_api.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. luat_heap_free(wmsg);
  154. return 0;
  155. }
  156. static int
  157. gatt_svr_chr_access_func(uint16_t conn_handle, uint16_t attr_handle,
  158. struct ble_gatt_access_ctxt *ctxt, void *arg)
  159. {
  160. int i = 0;
  161. struct os_mbuf *om = ctxt->om;
  162. ble_write_msg_t* wmsg;
  163. rtos_msg_t msg = {0};
  164. LLOGD("gatt_svr_chr_access_func %d %d %d", conn_handle, attr_handle, ctxt->op);
  165. switch (ctxt->op) {
  166. case BLE_GATT_ACCESS_OP_WRITE_CHR:
  167. while(om) {
  168. wmsg = (ble_write_msg_t*)(luat_heap_malloc(sizeof(ble_write_msg_t) + om->om_len - 1));
  169. if (wmsg != NULL) {
  170. wmsg->len = om->om_len;
  171. msg.handler = l_ble_chr_write_cb;
  172. msg.ptr = wmsg;
  173. msg.arg1 = conn_handle;
  174. msg.arg2 = attr_handle;
  175. memcpy(wmsg->buff, om->om_data, om->om_len);
  176. luat_msgbus_put(&msg, 0);
  177. }
  178. // if(g_ble_uart_output_fptr)
  179. // {
  180. // g_ble_uart_output_fptr((uint8_t *)om->om_data, om->om_len);
  181. // }else
  182. // {
  183. // print_bytes(om->om_data, om->om_len);
  184. // }
  185. om = SLIST_NEXT(om, om_next);
  186. }
  187. return 0;
  188. case BLE_GATT_ACCESS_OP_READ_CHR:
  189. return 0;
  190. default:
  191. assert(0);
  192. return BLE_ATT_ERR_UNLIKELY;
  193. }
  194. }
  195. int
  196. wm_ble_server_gatt_svr_init(void)
  197. {
  198. int rc;
  199. rc = ble_gatts_count_cfg(gatt_svr_svcs);
  200. if (rc != 0) {
  201. goto err;
  202. }
  203. rc = ble_gatts_add_svcs(gatt_svr_svcs);
  204. if (rc != 0) {
  205. return rc;
  206. }
  207. err:
  208. return rc;
  209. }
  210. // static uint8_t ss = 0x00;
  211. static int indication_sent_cb(lua_State *L, void* ptr) {
  212. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  213. lua_getglobal(L, "sys_pub");
  214. if (lua_isfunction(L, -1)) {
  215. lua_pushstring(L, "BLE_IND_SENT");
  216. lua_pushinteger(L, msg->arg1);
  217. lua_pushinteger(L, msg->arg2);
  218. lua_call(L, 3, 0);
  219. }
  220. return 0;
  221. }
  222. static void ble_server_indication_sent_cb(int conn_id, int status)
  223. {
  224. int len = 0;
  225. tls_bt_status_t ret;
  226. g_send_pending = 0;
  227. LLOGD("indication_sent_cb %d %d", conn_id, status);
  228. // if(!g_ble_indicate_enable)
  229. // {
  230. // TLS_BT_APPL_TRACE_DEBUG("Indicate disabled... when trying to send...\r\n");
  231. // return;
  232. // }
  233. rtos_msg_t msg = {0};
  234. msg.handler = indication_sent_cb;
  235. msg.arg1 = conn_id;
  236. msg.arg2 = status;
  237. luat_msgbus_put(&msg, 0);
  238. // if(g_ble_uart_output_fptr == NULL)
  239. // {
  240. // memset(g_ind_data, ss, sizeof(g_ind_data));
  241. // ss++;
  242. // if(ss > 0xFE) ss = 0x00;
  243. // tls_ble_server_api_send_msg(g_ind_data, g_mtu);
  244. // }
  245. }
  246. static void wm_ble_server_start_indicate(void *arg)
  247. {
  248. int len;
  249. int rc;
  250. uint8_t *tmp_ptr = NULL;
  251. tls_bt_status_t status;
  252. /*No uart ble interface*/
  253. if(g_ble_uart_output_fptr == NULL)
  254. {
  255. rc = tls_ble_server_api_send_msg(g_ind_data, g_mtu);
  256. TLS_BT_APPL_TRACE_DEBUG("Indicating sending...rc=%d\r\n", rc);
  257. }
  258. // else
  259. // {
  260. // /*check and send*/
  261. // len = tls_ble_uart_buffer_size();
  262. // len = MIN(len, g_mtu);
  263. // if(len)
  264. // {
  265. // tls_ble_uart_buffer_peek(g_ind_data, len);
  266. // status = tls_ble_server_api_send_msg(g_ind_data, g_mtu);
  267. // if(status == TLS_BT_STATUS_SUCCESS)
  268. // {
  269. // tls_ble_uart_buffer_delete(len);
  270. // }else
  271. // {
  272. // TLS_BT_APPL_TRACE_DEBUG("Server send failed(%d), retry...\r\n", status);
  273. // tls_bt_async_proc_func(wm_ble_server_start_indicate,(void*)g_ble_indicate_enable,1000);
  274. // }
  275. // }
  276. // }
  277. }
  278. static void conn_param_update_cb(uint16_t conn_handle, int status, void *arg)
  279. {
  280. LLOGD("conn param update complete; conn_handle=%d status=%d", conn_handle, status);
  281. }
  282. static void wm_ble_server_conn_param_update_slave()
  283. {
  284. int rc;
  285. struct ble_l2cap_sig_update_params params;
  286. params.itvl_min = 0x0006;
  287. params.itvl_max = 0x0006;
  288. params.slave_latency = 0;
  289. params.timeout_multiplier = 0x07d0;
  290. rc = ble_l2cap_sig_update(g_ble_conn_handle, &params,
  291. conn_param_update_cb, NULL);
  292. assert(rc == 0);
  293. }
  294. static int luat_ble_gap_evt(lua_State *L, void* ptr) {
  295. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  296. lua_getglobal(L, "sys_pub");
  297. if (lua_isfunction(L, -1)) {
  298. lua_pushstring(L, "BLE_GAP_EVT");
  299. lua_pushinteger(L, msg->arg1);
  300. lua_pushinteger(L, msg->arg2);
  301. lua_call(L, 3, 0);
  302. }
  303. return 0;
  304. }
  305. static int ble_gap_evt_cb(struct ble_gap_event *event, void *arg)
  306. {
  307. int rc;
  308. struct ble_gap_conn_desc desc;
  309. rtos_msg_t msg = {0};
  310. msg.handler = luat_ble_gap_evt;
  311. msg.arg1 = event->type;
  312. switch(event->type)
  313. {
  314. case BLE_GAP_EVENT_CONNECT:
  315. g_ble_prof_connected = 1;
  316. g_send_pending = 0;
  317. msg.arg2 = event->connect.status;
  318. LLOGD("connected status=%d handle=%d,g_ble_attr_indicate_handle=%d",event->connect.status, g_ble_conn_handle, g_ble_attr_indicate_handle );
  319. if (event->connect.status == 0) {
  320. g_ble_server_state = BLE_SERVER_MODE_CONNECTED;
  321. //re set this flag, to prevent stop adv, but connected evt reported when deinit this demo
  322. g_ble_conn_handle = event->connect.conn_handle;
  323. rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
  324. assert(rc == 0);
  325. print_conn_desc(&desc);
  326. #if MYNEWT_VAL(BLEPRPH_LE_PHY_SUPPORT)
  327. phy_conn_changed(event->connect.conn_handle);
  328. #endif
  329. }
  330. //TLS_BT_APPL_TRACE_DEBUG("\r\n");
  331. if (event->connect.status != 0) {
  332. /* Connection failed; resume advertising. */
  333. tls_nimble_gap_adv(WM_BLE_ADV_IND, 0);
  334. }
  335. luat_msgbus_put(&msg, 0);
  336. break;
  337. case BLE_GAP_EVENT_DISCONNECT:
  338. g_ble_prof_connected = 0;
  339. g_ble_indicate_enable = 0;
  340. g_send_pending = 0;
  341. LLOGD("disconnect reason=%d,state=%d", event->disconnect.reason,g_ble_server_state);
  342. if(g_ble_server_state == BLE_SERVER_MODE_EXITING)
  343. {
  344. if(g_ble_uart_output_fptr)
  345. {
  346. g_ble_uart_output_fptr = NULL;
  347. }
  348. g_ble_server_state = BLE_SERVER_MODE_IDLE;
  349. }else
  350. {
  351. rc = tls_nimble_gap_adv(WM_BLE_ADV_IND, 0);
  352. if(!rc)
  353. {
  354. g_ble_server_state = BLE_SERVER_MODE_ADVERTISING;
  355. }
  356. }
  357. #if 0
  358. if(event->disconnect.reason == 534)
  359. {
  360. //hci error code: 0x16 + 0x200 = 534; //local host terminate the connection;
  361. }else
  362. {
  363. tls_nimble_gap_adv(WM_BLE_ADV_IND, 0);
  364. }
  365. #endif
  366. luat_msgbus_put(&msg, 0);
  367. break;
  368. case BLE_GAP_EVENT_NOTIFY_TX:
  369. if(event->notify_tx.status == BLE_HS_EDONE)
  370. {
  371. ble_server_indication_sent_cb(event->notify_tx.attr_handle, event->notify_tx.status);
  372. }else
  373. {
  374. /*Application will handle other cases*/
  375. }
  376. msg.arg2 = event->notify_tx.status;
  377. luat_msgbus_put(&msg, 0);
  378. break;
  379. case BLE_GAP_EVENT_SUBSCRIBE:
  380. LLOGD("subscribe indicate(%d,%d)", event->subscribe.prev_indicate,event->subscribe.cur_indicate );
  381. LLOGD("subscribe notify(%d,%d)", event->subscribe.prev_notify,event->subscribe.cur_notify );
  382. g_ble_indicate_enable = event->subscribe.cur_indicate;
  383. // if(g_ble_indicate_enable)
  384. // {
  385. // g_ble_server_state = BLE_SERVER_MODE_INDICATING;
  386. // /*To reach the max passthrough, in ble_uart mode, I conifg the min connection_interval*/
  387. // if(g_ble_uart_output_fptr)
  388. // {
  389. // tls_bt_async_proc_func(wm_ble_server_conn_param_update_slave, NULL, 30);
  390. // }
  391. // tls_bt_async_proc_func(wm_ble_server_start_indicate,(void*)g_ble_indicate_enable, 30);
  392. // }else
  393. // {
  394. // if(g_ble_server_state != BLE_SERVER_MODE_EXITING)
  395. // {
  396. // g_ble_server_state = BLE_SERVER_MODE_CONNECTED;
  397. // }
  398. // }
  399. msg.arg2 = event->subscribe.cur_indicate;
  400. luat_msgbus_put(&msg, 0);
  401. break;
  402. case BLE_GAP_EVENT_MTU:
  403. LLOGD("wm ble dm mtu changed to(%d)", event->mtu.value);
  404. /*nimBLE config prefered ATT_MTU is 256. here 256-12 = 244. */
  405. /* preamble(1)+access address(4)+pdu(2~257)+crc*/
  406. /* ATT_MTU(247):pdu= pdu_header(2)+l2cap_len(2)+l2cap_chn(2)+mic(4)*/
  407. /* GATT MTU(244): ATT_MTU +opcode+chn*/
  408. g_mtu = min(event->mtu.value - 12, 244);
  409. msg.arg2 = event->mtu.value;
  410. luat_msgbus_put(&msg, 0);
  411. break;
  412. case BLE_GAP_EVENT_REPEAT_PAIRING:
  413. /* We already have a bond with the peer, but it is attempting to
  414. * establish a new secure link. This app sacrifices security for
  415. * convenience: just throw away the old bond and accept the new link.
  416. */
  417. /* Delete the old bond. */
  418. rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
  419. assert(rc == 0);
  420. ble_store_util_delete_peer(&desc.peer_id_addr);
  421. LLOGD("!!!BLE_GAP_EVENT_REPEAT_PAIRING");
  422. return BLE_GAP_REPEAT_PAIRING_RETRY;
  423. case BLE_GAP_EVENT_PASSKEY_ACTION:
  424. LLOGD(">>>BLE_GAP_EVENT_REPEAT_PAIRING");
  425. return 0;
  426. default:
  427. break;
  428. }
  429. return 0;
  430. }
  431. /*
  432. * EXPORTED FUNCTION DEFINITIONS
  433. ****************************************************************************************
  434. */
  435. int tls_ble_server_api_init(tls_ble_output_func_ptr output_func_ptr)
  436. {
  437. int rc = BLE_HS_EAPP;
  438. if(bt_adapter_state == WM_BT_STATE_OFF)
  439. {
  440. TLS_BT_APPL_TRACE_ERROR("%s failed rc=%s\r\n", __FUNCTION__, tls_bt_rc_2_str(BLE_HS_EDISABLED));
  441. return BLE_HS_EDISABLED;
  442. }
  443. TLS_BT_APPL_TRACE_DEBUG("%s, state=%d\r\n", __FUNCTION__, g_ble_server_state);
  444. if(g_ble_server_state == BLE_SERVER_MODE_IDLE)
  445. {
  446. g_ble_prof_connected = 0;
  447. //step 0: reset other services. Note
  448. rc = ble_gatts_reset();
  449. if(rc != 0)
  450. {
  451. TLS_BT_APPL_TRACE_ERROR("tls_ble_server_api_init failed rc=%d\r\n", rc);
  452. return rc;
  453. }
  454. //step 1: config/adding the services
  455. rc = wm_ble_server_gatt_svr_init();
  456. if(rc == 0)
  457. {
  458. 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);
  459. TLS_BT_APPL_TRACE_DEBUG("### wm_ble_server_api_init \r\n");
  460. g_ble_uart_output_fptr = output_func_ptr;
  461. /*step 2: start the service*/
  462. rc = ble_gatts_start();
  463. assert(rc == 0);
  464. /*step 3: start advertisement*/
  465. rc = wm_ble_server_api_adv(true);
  466. if(rc == 0)
  467. {
  468. g_ble_server_state = BLE_SERVER_MODE_ADVERTISING;
  469. }
  470. }else
  471. {
  472. TLS_BT_APPL_TRACE_ERROR("### wm_ble_server_api_init failed(rc=%d)\r\n", rc);
  473. }
  474. }
  475. else
  476. {
  477. TLS_BT_APPL_TRACE_WARNING("wm_ble_server_api_init registered\r\n");
  478. rc = BLE_HS_EALREADY;
  479. }
  480. return rc;
  481. }
  482. int tls_ble_server_api_deinit()
  483. {
  484. int rc = BLE_HS_EAPP;
  485. if(bt_adapter_state == WM_BT_STATE_OFF)
  486. {
  487. TLS_BT_APPL_TRACE_ERROR("%s failed rc=%s\r\n", __FUNCTION__, tls_bt_rc_2_str(BLE_HS_EDISABLED));
  488. return BLE_HS_EDISABLED;
  489. }
  490. TLS_BT_APPL_TRACE_DEBUG("%s, state=%d\r\n", __FUNCTION__, g_ble_server_state);
  491. if(g_ble_server_state == BLE_SERVER_MODE_CONNECTED || g_ble_server_state == BLE_SERVER_MODE_INDICATING)
  492. {
  493. g_ble_indicate_enable = 0;
  494. rc = ble_gap_terminate(g_ble_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
  495. if(rc == 0)
  496. {
  497. g_ble_server_state = BLE_SERVER_MODE_EXITING;
  498. }
  499. }else if(g_ble_server_state == BLE_SERVER_MODE_ADVERTISING)
  500. {
  501. rc = tls_nimble_gap_adv(WM_BLE_ADV_STOP, 0);
  502. if(rc == 0)
  503. {
  504. if(g_ble_uart_output_fptr)
  505. {
  506. g_ble_uart_output_fptr = NULL;
  507. }
  508. g_send_pending = 0;
  509. g_ble_server_state = BLE_SERVER_MODE_IDLE;
  510. }
  511. }else if(g_ble_server_state == BLE_SERVER_MODE_IDLE)
  512. {
  513. rc = 0;
  514. }else
  515. {
  516. rc = BLE_HS_EALREADY;
  517. }
  518. return rc;
  519. }
  520. uint32_t tls_ble_server_api_get_mtu()
  521. {
  522. return g_mtu;
  523. }
  524. int tls_ble_server_api_send_msg(uint8_t *data, int data_len)
  525. {
  526. int rc;
  527. struct os_mbuf *om;
  528. //TLS_BT_APPL_TRACE_DEBUG("### %s len=%d\r\n", __FUNCTION__, data_len);
  529. if(g_send_pending) return BLE_HS_EBUSY;
  530. if(data_len<=0 || data == NULL)
  531. {
  532. return BLE_HS_EINVAL;
  533. }
  534. om = ble_hs_mbuf_from_flat(data, data_len);
  535. if (!om) {
  536. return BLE_HS_ENOMEM;
  537. }
  538. rc = ble_gattc_indicate_custom(g_ble_conn_handle,g_ble_attr_indicate_handle, om);
  539. // rc = ble_gattc_notify_custom(g_ble_conn_handle,g_ble_attr_notify_handle, om);
  540. if(rc == 0)
  541. {
  542. g_send_pending = 1;
  543. }
  544. return rc;
  545. }
  546. // #endif