bt_app_api.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /**
  2. ****************************************************************************************
  3. *
  4. * @file bt_app_api.c
  5. *
  6. * @brief Application entry point
  7. *
  8. * Copyright (C) Espressif 2016
  9. * Created by Yulong at 2016/10/17
  10. *
  11. *
  12. ****************************************************************************************
  13. */
  14. #include "bt_app_api.h"
  15. #include "btm_ble_api.h"
  16. //#include "btm_ble_int.h"
  17. /*******************************************************************************
  18. **
  19. ** @function esp_ble_config_adv_data
  20. **
  21. ** @brief This function is called to override the BTA default ADV parameters.
  22. **
  23. ** @param[in] adv_data: Pointer to User defined ADV data structure. This
  24. ** memory space can not be freed until p_adv_data_cback
  25. ** is received.
  26. ** @param[in|out] adv_data_cback: set adv data complete callback.
  27. **
  28. ** @return None
  29. **
  30. *******************************************************************************/
  31. void esp_ble_config_adv_data (esp_ble_adv_data_cfg_t *adv_data,
  32. esp_ble_set_adv_data_cmpl_cb_t *adv_data_cback)
  33. {
  34. if(adv_data != NULL)
  35. {
  36. ble_config_adv_data(adv_data, adv_data_cback);
  37. }else{
  38. LOG_ERROR("The adv_data is NULL\n");
  39. }
  40. if(++adv_data != NULL)
  41. {
  42. ble_set_scan_rsp(adv_data, NULL);
  43. }
  44. }
  45. /*******************************************************************************
  46. **
  47. ** @function esp_ble_start_advertising
  48. **
  49. ** @brief This function is called to start advertising.
  50. **
  51. ** @param[in] esp_ble_adv_params_all_t: ointer to User defined adv_params data structure.
  52. **
  53. ** @return None
  54. **
  55. *******************************************************************************/
  56. void esp_ble_start_advertising (esp_ble_adv_params_all_t *ble_adv_params)
  57. {
  58. if (!API_BLE_ISVALID_PARAM(ble_adv_params->adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
  59. !API_BLE_ISVALID_PARAM(ble_adv_params->adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX))
  60. {
  61. LOG_ERROR("Invalid advertisting interval parameters.\n");
  62. return ;
  63. }
  64. if ((ble_adv_params->adv_type < API_NON_DISCOVERABLE) &&
  65. (ble_adv_params->adv_type > API_BROADCASTER_MODE) )
  66. {
  67. LOG_ERROR("Invalid advertisting type parameters.\n");
  68. return;
  69. }
  70. if ((ble_adv_params->adv_filter_policy < ADV_ALLOW_SCAN_ANY_CON_ANY) &&
  71. (ble_adv_params->adv_filter_policy > ADV_ALLOW_SCAN_WLST_CON_WLST) )
  72. {
  73. LOG_ERROR("Invalid advertisting type parameters.\n");
  74. return;
  75. }
  76. LOG_ERROR("API_Ble_AppStartAdvertising\n");
  77. ///
  78. BTA_DmSetBleAdvParamsAll(ble_adv_params->adv_int_min,
  79. ble_adv_params->adv_int_max,
  80. ble_adv_params->adv_type,
  81. ble_adv_params->addr_type_own,
  82. ble_adv_params->channel_map,
  83. ble_adv_params->adv_filter_policy,
  84. ble_adv_params->p_dir_bda);
  85. }
  86. /*******************************************************************************
  87. **
  88. ** @function esp_ble_set_scan_params
  89. **
  90. ** @brief This function is called to set scan parameters
  91. **
  92. ** @param[in] esp_ble_scan_params: Pointer to User defined scan_params data structure. This
  93. ** memory space can not be freed until scan_param_setup_cback
  94. ** @param[in] client_if: Client interface ID
  95. ** @param[in|out] scan_param_setup_status_cback - Set scan param status callback
  96. **
  97. ** @return None
  98. **
  99. *******************************************************************************/
  100. void esp_ble_set_scan_params (esp_ble_scan_params *scan_params, esp_gattc_if_t client_if,
  101. esp_scan_param_setup_cb_t scan_param_setup_cback)
  102. {
  103. if (API_BLE_ISVALID_PARAM(scan_params->scan_intv, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
  104. API_BLE_ISVALID_PARAM(scan_params->scan_win, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX) &&
  105. (scan_params->scan_type == BTM_BLE_SCAN_MODE_ACTI || scan_params->scan_type == BTM_BLE_SCAN_MODE_PASS))
  106. {
  107. BTA_DmSetBleScanFilterParams(client_if,
  108. scan_params->scan_intv,
  109. scan_params->scan_win,
  110. scan_params->scan_type,
  111. scan_params->scan_fil_policy,
  112. scan_params->addr_type_own,
  113. scan_param_setup_cback);
  114. }
  115. }
  116. /*******************************************************************************
  117. **
  118. ** @function esp_ble_start_scanning
  119. **
  120. ** @brief This procedure keep the device scanning the peer device whith advertising on the air
  121. **
  122. ** @param[in] duration: Keeping the scaning time, the unit is second.
  123. ** @param[in|out] results_cb: The scanning function callback when the peer device has been scan
  124. ** which advertising on the air
  125. **
  126. ** @return None
  127. **
  128. *******************************************************************************/
  129. void esp_ble_start_scanning (UINT8 duration, esp_dm_search_cb_t *results_cb)
  130. {
  131. if((duration != 0) && (results_cb != NULL))
  132. {
  133. ///Start scan the device
  134. BTA_DmBleObserve(true, duration, results_cb);
  135. }else{
  136. LOG_ERROR("The scan duration or p_results_cb invalid\n");
  137. }
  138. }
  139. /*******************************************************************************
  140. **
  141. ** @function esp_ble_stop_advertising
  142. **
  143. ** @brief This function is called to stop advertising.
  144. **
  145. ** @param None
  146. **
  147. ** @return None
  148. **
  149. *******************************************************************************/
  150. void esp_ble_stop_advertising (void)
  151. {
  152. bool stop_adv = false;
  153. BTA_DmBleBroadcast(stop_adv);
  154. }
  155. /*******************************************************************************
  156. **
  157. ** @function esp_ble_update_conn_params
  158. **
  159. ** @brief Update connection parameters, can only be used when connection is up.
  160. **
  161. ** @param[in] bd_addr - BD address of the peer
  162. ** @param[in] min_int - minimum connection interval, [0x0004~ 0x4000]
  163. ** @param[in] max_int - maximum connection interval, [0x0004~ 0x4000]
  164. ** @param[in] latency - slave latency [0 ~ 500]
  165. ** @param[in] timeout - supervision timeout [0x000a ~ 0xc80]
  166. **
  167. ** @return None
  168. **
  169. *******************************************************************************/
  170. void esp_ble_update_conn_params (BD_ADDR bd_addr, uint16_t min_int,
  171. uint16_t max_int, uint16_t latency, uint16_t timeout)
  172. {
  173. if (min_int > max_int){
  174. min_int = max_int;
  175. }
  176. if (min_int < BTM_BLE_CONN_INT_MIN || max_int > BTM_BLE_CONN_INT_MAX){
  177. LOG_ERROR("Invalid interval value.\n");
  178. }
  179. BTA_DmBleUpdateConnectionParams(bd_addr, min_int, max_int,
  180. latency, timeout);
  181. }
  182. /*******************************************************************************
  183. **
  184. ** @function esp_ble_set_pkt_data_len
  185. **
  186. ** @brief This function is to set maximum LE data packet size
  187. **
  188. ** @return None
  189. **
  190. **
  191. *******************************************************************************/
  192. void esp_ble_set_pkt_data_len (BD_ADDR remote_device, uint16_t tx_data_length)
  193. {
  194. if (tx_data_length > BTM_BLE_DATA_SIZE_MAX){
  195. tx_data_length = BTM_BLE_DATA_SIZE_MAX;
  196. }else if (tx_data_length < BTM_BLE_DATA_SIZE_MIN){
  197. tx_data_length = BTM_BLE_DATA_SIZE_MIN;
  198. }
  199. BTA_DmBleSetDataLength(remote_device, tx_data_length);
  200. }
  201. /*******************************************************************************
  202. **
  203. ** @function esp_ble_set_rand_addr
  204. **
  205. ** @brief This function set the random address for the appliction
  206. **
  207. ** @param[in] rand_addr: the random address whith should be setting
  208. **
  209. ** @return None
  210. **
  211. **
  212. *******************************************************************************/
  213. void esp_ble_set_rand_addr (BD_ADDR rand_addr)
  214. {
  215. if (rand_addr != NULL){
  216. BTA_DmSetRandAddress(rand_addr);
  217. }else{
  218. LOG_ERROR("Invalid randrom address.\n");
  219. }
  220. }
  221. /*******************************************************************************
  222. **
  223. ** @function esp_ble_config_local_privacy
  224. **
  225. ** @brief Enable/disable privacy on the local device
  226. **
  227. ** @param[in] privacy_enable - enable/disabe privacy on remote device.
  228. **
  229. ** @return None
  230. **
  231. *******************************************************************************/
  232. void esp_ble_config_local_privacy (BOOLEAN privacy_enable)
  233. {
  234. BTA_DmBleConfigLocalPrivacy(privacy_enable);
  235. }
  236. /*******************************************************************************
  237. ** @function esp_gattc_disable
  238. ** @brief This function is called by application to disable GATTC module
  239. **
  240. ** @param NULL
  241. **
  242. ** @return None.
  243. **
  244. *******************************************************************************/
  245. void esp_gattc_disable(void)
  246. {
  247. // TODO: This API funciton didn't finish, will be added it in future
  248. }
  249. /*******************************************************************************
  250. **
  251. ** @function esp_ble_gattc_app_register
  252. **
  253. ** @brief This function is called to register application callbacks
  254. ** with GATTC module.
  255. **
  256. ** @param[in] app_uuid - applicaiton UUID
  257. ** @param[in] client_cb - pointer to the application callback function.
  258. **
  259. ** @return None
  260. **
  261. *******************************************************************************/
  262. void esp_ble_gattc_app_register(esp_bt_uuid_t *app_uuid, esp_gattc_cb_t *client_cb)
  263. {
  264. if (app_uuid != NULL)
  265. {
  266. BTA_GATTC_AppRegister(app_uuid, client_cb);
  267. }else{
  268. LOG_ERROR("The app uuid invalid.\n");
  269. }
  270. }
  271. /*******************************************************************************
  272. **
  273. ** @function esp_ble_gattc_app_unregister
  274. **
  275. ** @brief This function is called to unregister an application
  276. ** from GATTC module.
  277. **
  278. ** @param[in] client_if - client interface identifier.
  279. **
  280. ** @return None
  281. **
  282. *******************************************************************************/
  283. void esp_ble_gattc_app_unregister(esp_gattc_if_t client_if)
  284. {
  285. BTA_GATTC_AppDeregister(client_if);
  286. }
  287. /*******************************************************************************
  288. **
  289. ** @function esp_ble_gattc_conn
  290. **
  291. ** @brief Open a direct connection or add a background auto connection
  292. ** bd address
  293. **
  294. ** @param[in] client_if: server interface.
  295. ** @param[in] remote_bda: remote device BD address.
  296. ** @param[in] is_direct: direct connection or background auto connection
  297. **
  298. ** @return None
  299. **
  300. *******************************************************************************/
  301. void esp_ble_gattc_conn(esp_gattc_if_t client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
  302. {
  303. tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
  304. if(remote_bda == NULL ){
  305. LOG_ERROR("Invaild address data \n");
  306. return;
  307. }
  308. BTA_GATTC_Open(client_if, remote_bda, is_direct, transport);
  309. }
  310. /*******************************************************************************
  311. **
  312. ** @function esp_ble_gattc_cancel_conn
  313. **
  314. ** @brief Cancel a direct open connection or remove a background auto connection
  315. ** bd address
  316. **
  317. ** @param[in] client_if: server interface.
  318. ** @param[in] remote_bda: remote device BD address.
  319. ** @param[in] is_direct: direct connection or background auto connection
  320. **
  321. ** @return None
  322. **
  323. *******************************************************************************/
  324. void esp_ble_gattc_cancel_conn (esp_gattc_if_t client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
  325. {
  326. if(remote_bda == NULL ){
  327. LOG_ERROR("Invaild address data \n");
  328. return;
  329. }
  330. BTA_GATTC_CancelOpen(client_if, remote_bda, is_direct);
  331. }
  332. /*******************************************************************************
  333. **
  334. ** @function esp_ble_gattc_close
  335. **
  336. ** @brief Close a connection to a GATT server.
  337. **
  338. ** @param[in] conn_id: connectino ID to be closed.
  339. **
  340. ** @return None
  341. **
  342. *******************************************************************************/
  343. void esp_ble_gattc_close (uint16_t conn_id)
  344. {
  345. BTA_GATTC_Close(conn_id);
  346. }
  347. /*******************************************************************************
  348. **
  349. ** @function esp_ble_gattc_config_mtu
  350. **
  351. ** @brief Configure the MTU size in the GATT channel. This can be done
  352. ** only once per connection.
  353. **
  354. ** @param[in] conn_id: connection ID.
  355. ** mtu: desired MTU size to use.
  356. **
  357. ** @return Command status code:
  358. ** - @ref ESP_GATT_OK: If request succeeds
  359. ** - @ref ESP_GATT_ILLEGAL_PARAMETER: If the mtu value invalid
  360. **
  361. *******************************************************************************/
  362. esp_gatt_status_t esp_ble_gattc_config_mtu (uint16_t conn_id, uint16_t mtu)
  363. {
  364. if ((mtu < GATT_DEF_BLE_MTU_SIZE) || (mtu > GATT_MAX_MTU_SIZE)){
  365. LOG_ERROR("Invalid MTU parameters\n");
  366. return ESP_GATT_ILLEGAL_PARAMETER;
  367. }
  368. BTA_GATTC_ConfigureMTU (conn_id, mtu);
  369. }
  370. /*******************************************************************************
  371. **
  372. ** @function esp_ble_gattc_svc_search_req
  373. **
  374. ** @brief This function is called to request a GATT service discovery
  375. ** on a GATT server. This function report service search result
  376. ** by a callback event, and followed by a service search complete
  377. ** event.
  378. **
  379. ** @param[in] conn_id: connection ID.
  380. ** @param[in] srvc_uuid: a UUID of the service application is interested in.
  381. ** If Null, discover for all services.
  382. **
  383. ** @return None
  384. **
  385. *******************************************************************************/
  386. void esp_ble_gattc_svc_search_req (uint16_t conn_id, esp_bt_uuid_t *srvc_uuid)
  387. {
  388. BTA_GATTC_ServiceSearchRequest(conn_id, srvc_uuid);
  389. }
  390. /****************************************************************************************************
  391. **
  392. ** @function esp_ble_gattc_get_first_char
  393. **
  394. ** @brief This function is called to find the first characteristic of the
  395. ** service on the given server.
  396. **
  397. ** @param[in] conn_id: connection ID which identify the server.
  398. ** @param[in] srvc_id: the service ID of which the characteristic is belonged to.
  399. ** @param[in] char_uuid_cond: Characteristic UUID, if NULL find the first available
  400. ** characteristic.
  401. ** @param[in] char_result: output parameter which will store the GATT
  402. ** characteristic ID.
  403. ** @param[in] property: output parameter to carry the characteristic property.
  404. **
  405. ** @return Command status code:
  406. ** - @ref ESP_GATT_OK: If request succeeds
  407. ** - @ref ESP_GATT_ILLEGAL_PARAMETER: If the srvc_id or char_result parameter is NULL.
  408. **
  409. *****************************************************************************************************/
  410. esp_gatt_status_t esp_ble_gattc_get_first_char (uint16_t conn_id, esp_gatt_srvc_id_t *srvc_id,
  411. esp_bt_uuid_t *char_uuid_cond,
  412. esp_gattc_char_id_t *char_result,
  413. esp_gatt_char_prop_t *property)
  414. {
  415. esp_gatt_status_t status = 0;
  416. if (!srvc_id || !char_result){
  417. return ESP_GATT_ILLEGAL_PARAMETER;
  418. }
  419. status = BTA_GATTC_GetFirstChar (conn_id, srvc_id, char_uuid_cond,
  420. char_result, property);
  421. return status;
  422. }
  423. /*******************************************************************************
  424. **
  425. ** @function esp_ble_gattc_get_first_char_descr
  426. **
  427. ** @brief This function is called to find the first characteristic descriptor of the
  428. ** characteristic on the given server.
  429. **
  430. ** @param[in] conn_id: connection ID which identify the server.
  431. ** @param[in] char_id: the characteristic ID of which the descriptor is belonged to.
  432. ** @param[in] descr_uuid_cond: Characteristic Descr UUID, if NULL find the first available
  433. ** characteristic.
  434. ** @param[in] descr_result: output parameter which will store the GATT
  435. ** characteristic descriptor ID.
  436. **
  437. ** @return Command status code:
  438. ** - @ref ESP_GATT_OK: If request succeeds
  439. ** - @ref ESP_GATT_ILLEGAL_PARAMETER: If the char_id or descr_result parameter is NULL.
  440. **
  441. *******************************************************************************/
  442. esp_gatt_status_t esp_ble_gattc_get_first_char_descr (uint16_t conn_id, esp_gattc_char_id_t *char_id,
  443. esp_bt_uuid_t *descr_uuid_cond,
  444. esp_gattc_char_descr_id_t *descr_result)
  445. {
  446. esp_gatt_status_t status;
  447. if (!char_id || !descr_result){
  448. return ESP_GATT_ILLEGAL_PARAMETER;
  449. }
  450. status = BTA_GATTC_GetFirstCharDescr (conn_id, char_id, descr_uuid_cond, descr_result);
  451. return status;
  452. }
  453. /*******************************************************************************
  454. **
  455. ** @function esp_ble_gattc_get_next_char
  456. **
  457. ** @brief This function is called to find the next characteristic of the
  458. ** service on the given server.
  459. **
  460. ** @param[in] conn_id: connection ID which identify the server.
  461. ** @param[in] start_char_id: start the characteristic search from the next record
  462. ** after the one identified by char_id.
  463. ** @param[in] char_uuid_cond: Characteristic UUID, if NULL find the first available
  464. ** characteristic.
  465. ** @param[in] char_result: output parameter which will store the GATT
  466. ** characteristic ID.
  467. ** @param[in] property: output parameter to carry the characteristic property.
  468. **
  469. ** @return Command status code:
  470. ** - @ref ESP_GATT_OK: If request succeeds
  471. ** - @ref ESP_GATT_ILLEGAL_PARAMETER: If the start_char_id or char_result parameter is NULL.
  472. **
  473. *******************************************************************************/
  474. esp_gatt_status_t esp_ble_gattc_get_next_char (uint16_t conn_id,
  475. esp_gattc_char_id_t *start_char_id,
  476. esp_bt_uuid_t *char_uuid_cond,
  477. esp_gattc_char_id_t *char_result,
  478. esp_gatt_char_prop_t *property)
  479. {
  480. esp_gatt_status_t status;
  481. if (!start_char_id || !char_result){
  482. return ESP_GATT_ILLEGAL_PARAMETER;
  483. }
  484. status = BTA_GATTC_GetNextChar(conn_id, start_char_id, char_uuid_cond,
  485. char_result, property);
  486. return status;
  487. }
  488. /*******************************************************************************
  489. **
  490. ** @function esp_ble_gattc_get_next_char_descr
  491. **
  492. ** @brief This function is called to find the next characteristic descriptor
  493. ** of the characterisctic.
  494. **
  495. ** @param[in] conn_id: connection ID which identify the server.
  496. ** @param[in] start_descr_id: start the descriptor search from the next record
  497. ** after the one identified by p_start_descr_id.
  498. ** @param[in] descr_uuid_cond: Characteristic descriptor UUID, if NULL find
  499. ** the first available characteristic descriptor.
  500. ** @param[in] descr_result: output parameter which will store the GATT
  501. ** characteristic descriptor ID.
  502. **
  503. ** @return Command status code:
  504. ** - @ref ESP_GATT_OK: If request succeeds
  505. ** - @ref ESP_GATT_ILLEGAL_PARAMETER: If the start_descr_id or descr_result parameter is NULL.
  506. **
  507. *******************************************************************************/
  508. esp_gatt_status_t esp_ble_gattc_get_next_char_descr (uint16_t conn_id,
  509. esp_gattc_char_descr_id_t *start_descr_id,
  510. esp_bt_uuid_t *descr_uuid_cond,
  511. esp_gattc_char_descr_id_t *descr_result)
  512. {
  513. esp_gatt_status_t status;
  514. if (!start_descr_id || !descr_result){
  515. return ESP_GATT_ILLEGAL_PARAMETER;
  516. }
  517. status = BTA_GATTC_GetNextCharDescr (conn_id, start_descr_id, descr_uuid_cond, descr_result);
  518. return status;
  519. }
  520. /*******************************************************************************
  521. **
  522. ** @function esp_ble_gattc_get_first_inclu_srvc
  523. **
  524. ** @brief This function is called to find the first included service of the
  525. ** service on the given server.
  526. **
  527. ** @param[in] conn_id: connection ID which identify the server.
  528. ** @param[in] srvc_id: the service ID of which the characteristic is belonged to.
  529. ** @param[in] uuid_cond: Characteristic UUID, if NULL find the first available
  530. ** characteristic.
  531. ** @param[in] result: output parameter which will store the GATT ID
  532. ** of the included service found.
  533. **
  534. ** @return Command status code:
  535. ** - @ref ESP_GATT_OK: If request succeeds
  536. ** - @ref ESP_GATT_ILLEGAL_PARAMETER: If the srvc_id or result parameter is NULL.
  537. **
  538. *******************************************************************************/
  539. esp_gatt_status_t esp_ble_gattc_get_first_inclu_srvc (uint16_t conn_id, esp_gatt_srvc_id_t *srvc_id,
  540. esp_bt_uuid_t *uuid_cond, esp_gattc_incl_srvc_id_t *result)
  541. {
  542. esp_gatt_status_t status;
  543. if (!srvc_id || !result){
  544. return ESP_GATT_ILLEGAL_PARAMETER;
  545. }
  546. status = BTA_GATTC_GetFirstIncludedService(conn_id, srvc_id, uuid_cond, result);
  547. return status;
  548. }
  549. /*******************************************************************************
  550. **
  551. ** @function esp_ble_gattc_get_next_inclu_srvc
  552. **
  553. ** @brief This function is called to find the next included service of the
  554. ** service on the given server.
  555. **
  556. ** @param[in] conn_id: connection ID which identify the server.
  557. ** @param[in] start_id: start the search from the next record
  558. ** after the one identified by p_start_id.
  559. ** @param[in] uuid_cond: Included service UUID, if NULL find the first available
  560. ** included service.
  561. ** @param[in] result: output parameter which will store the GATT ID
  562. ** of the included service found.
  563. **
  564. ** @return Command status code:
  565. ** - @ref ESP_GATT_OK: If request succeeds
  566. ** - @ref ESP_GATT_ILLEGAL_PARAMETER: If the p_char_id or p_descr_result parameter is NULL.
  567. *******************************************************************************/
  568. esp_gatt_status_t esp_ble_gattc_get_next_inclu_srvc (uint16_t conn_id,
  569. esp_gattc_incl_srvc_id_t *start_id,
  570. esp_bt_uuid_t *uuid_cond,
  571. esp_gattc_incl_srvc_id_t *result)
  572. {
  573. esp_gatt_status_t status;
  574. if (!start_id || !result){
  575. return ESP_GATT_ILLEGAL_PARAMETER;
  576. }
  577. status = BTA_GATTC_GetNextIncludedService(conn_id, start_id, uuid_cond, result);
  578. return status;
  579. }
  580. /*******************************************************************************
  581. **
  582. ** @function esp_ble_gattc_read_char
  583. **
  584. ** @brief This function is called to read a service's characteristics of
  585. ** the given characteritisc ID.
  586. **
  587. ** @param[in] conn_id - connectino ID.
  588. ** @param[in] char_id - characteritic ID to read.
  589. **
  590. ** @return None
  591. **
  592. *******************************************************************************/
  593. void esp_ble_gattc_read_char (uint16_t conn_id, esp_gattc_char_id_t *char_id,
  594. esp_gatt_auth_req_t auth_req)
  595. {
  596. BTA_GATTC_ReadCharacteristic(conn_id, char_id, auth_req);
  597. }
  598. /*******************************************************************************
  599. **
  600. ** @function esp_ble_gattc_read_char_descr
  601. **
  602. ** @brief This function is called to read a characteristics descriptor.
  603. **
  604. ** @param[in] conn_id - connection ID.
  605. ** @param[in] descr_id - characteritic descriptor ID to read.
  606. **
  607. ** @return None
  608. **
  609. *******************************************************************************/
  610. void esp_ble_gattc_read_char_descr (uint16_t conn_id,
  611. esp_gattc_char_descr_id_t *descr_id,
  612. esp_gatt_auth_req_t auth_req)
  613. {
  614. BTA_GATTC_ReadCharDescr (conn_id, descr_id, auth_req);
  615. }
  616. /*******************************************************************************
  617. **
  618. ** @function esp_ble_gattc_read_multi
  619. **
  620. ** @brief This function is called to read multiple characteristic or
  621. ** characteristic descriptors.
  622. **
  623. ** @param[in] conn_id - connectino ID.
  624. ** @param[in] read_multi - pointer to the read multiple parameter.
  625. **
  626. ** @return None
  627. **
  628. *******************************************************************************/
  629. void esp_ble_gattc_read_multi (uint16_t conn_id, esp_gattc_multi_t *read_multi,
  630. esp_gatt_auth_req_t auth_req)
  631. {
  632. BTA_GATTC_ReadMultiple(conn_id, read_multi, auth_req);
  633. }
  634. /*******************************************************************************
  635. **
  636. ** @function esp_ble_gattc_write_char_val
  637. **
  638. ** @brief This function is called to write characteristic value.
  639. **
  640. ** @param[in] conn_id - connection ID.
  641. ** @param[in] char_id - characteristic ID to write.
  642. ** @param[in] write_type - type of write.
  643. ** @param[in] len: length of the data to be written.
  644. ** @param[in] value - the value to be written.
  645. **
  646. ** @return None
  647. **
  648. *******************************************************************************/
  649. void esp_ble_gattc_write_char_val ( uint16_t conn_id,
  650. esp_gattc_char_id_t *char_id,
  651. esp_gattc_write_type_t write_type,
  652. uint16_t len,
  653. uint8_t *value,
  654. esp_gatt_auth_req_t auth_req)
  655. {
  656. BTA_GATTC_WriteCharValue (conn_id, char_id, write_type, len, value, auth_req);
  657. }
  658. /*******************************************************************************
  659. **
  660. ** @function esp_ble_gattc_write_char_descr
  661. **
  662. ** @brief This function is called to write characteristic descriptor value.
  663. **
  664. ** @param[in] conn_id - connection ID
  665. ** @param[in] char_descr_id - characteristic descriptor ID to write.
  666. ** @param[in] write_type - write type.
  667. ** @param[in] data - the value to be written.
  668. **
  669. ** @return None
  670. **
  671. *******************************************************************************/
  672. void esp_ble_gattc_write_char_descr (uint16_t conn_id,
  673. esp_gattc_char_descr_id_t *char_descr_id,
  674. esp_gattc_write_type_t write_type,
  675. esp_gatt_unfmt_t *data,
  676. esp_gatt_auth_req_t auth_req)
  677. {
  678. BTA_GATTC_WriteCharDescr (conn_id, char_descr_id, write_type, data, auth_req);
  679. }
  680. /*******************************************************************************
  681. **
  682. ** @function esp_ble_gattc_prepa_write
  683. **
  684. ** @brief This function is called to prepare write a characteristic value.
  685. **
  686. ** @param[in] conn_id - connection ID.
  687. ** @param[in] char_id - GATT characteritic ID of the service.
  688. ** @param[in] offset - offset of the write value.
  689. ** @param[in] len: length of the data to be written.
  690. ** @param[in] value - the value to be written.
  691. **
  692. ** @return None
  693. **
  694. *******************************************************************************/
  695. void esp_ble_gattc_prepa_write (uint16_t conn_id, esp_gattc_char_id_t *char_id,
  696. uint16_t offset, uint16_t len, uint8_t *value,
  697. esp_gatt_auth_req_t auth_req)
  698. {
  699. BTA_GATTC_PrepareWrite (conn_id, char_id, offset, len, value, auth_req);
  700. }
  701. /*******************************************************************************
  702. **
  703. ** @function esp_ble_gattc_execu_write
  704. **
  705. ** @brief This function is called to execute write a prepare write sequence.
  706. **
  707. ** @param[in] conn_id - connection ID.
  708. ** @param[in] is_execute - execute or cancel.
  709. **
  710. ** @return None
  711. **
  712. *******************************************************************************/
  713. void esp_ble_gattc_execu_write (uint16_t conn_id, BOOLEAN is_execute)
  714. {
  715. BTA_GATTC_ExecuteWrite (conn_id, is_execute);
  716. }
  717. /*******************************************************************************
  718. **
  719. ** @function esp_ble_gattc_send_ind_cfm
  720. **
  721. ** @brief This function is called to send handle value confirmation.
  722. **
  723. ** @param[in] conn_id - connection ID.
  724. ** @param[in] char_id - characteristic ID to confirm.
  725. **
  726. ** @return None
  727. **
  728. *******************************************************************************/
  729. void esp_ble_gattc_send_ind_cfm (uint16_t conn_id, esp_gattc_char_id_t *char_id)
  730. {
  731. BTA_GATTC_SendIndConfirm (conn_id, char_id);
  732. }
  733. /*******************************************************************************
  734. **
  735. ** @function esp_ble_gattc_register_ntf
  736. **
  737. ** @brief This function is called to register for notification of a service.
  738. **
  739. ** @param[in] client_if - client interface.
  740. ** @param[in] bda - target GATT server.
  741. ** @param[in] char_id - pointer to GATT characteristic ID.
  742. **
  743. ** @return OK if registration succeed, otherwise failed.
  744. **
  745. *******************************************************************************/
  746. esp_gatt_status_t esp_ble_gattc_register_ntf (esp_gattc_if_t client_if,
  747. BD_ADDR bda,
  748. esp_gattc_char_id_t *char_id)
  749. {
  750. esp_gatt_status_t status = ESP_GATT_ILLEGAL_PARAMETER;
  751. status = BTA_GATTC_RegisterForNotifications (client_if, bda, char_id);
  752. return status;
  753. }
  754. /*******************************************************************************
  755. **
  756. ** @function esp_ble_gattc_unregister_ntf
  757. **
  758. ** @brief This function is called to de-register for notification of a service.
  759. **
  760. ** @param[in] client_if - client interface.
  761. ** @param[in] bda - target GATT server.
  762. ** @param[in] char_id - pointer to GATT characteristic ID.
  763. **
  764. ** @return OK if deregistration succeed, otherwise failed.
  765. **
  766. *******************************************************************************/
  767. tBTA_GATT_STATUS esp_ble_gattc_unregister_ntf (esp_gattc_if_t client_if,
  768. BD_ADDR bda,
  769. esp_gattc_char_id_t *char_id)
  770. {
  771. esp_gatt_status_t status = ESP_GATT_ILLEGAL_PARAMETER;
  772. status = BTA_GATTC_DeregisterForNotifications (client_if, bda, char_id);
  773. return status;
  774. }
  775. /*******************************************************************************
  776. **
  777. ** @function esp_ble_gatts_disale
  778. **
  779. ** @brief This function is called to disable GATTS module
  780. **
  781. ** @param[in] None.
  782. **
  783. ** @return None
  784. **
  785. *******************************************************************************/
  786. void esp_ble_gatts_disale(void)
  787. {
  788. BTA_GATTS_Disable();
  789. }
  790. /*******************************************************************************
  791. **
  792. ** @function esp_ble_gatts_app_register
  793. **
  794. ** @brief This function is called to register application callbacks
  795. ** with BTA GATTS module.
  796. **
  797. ** @param[in] app_uuid - applicaiton UUID
  798. ** @param[in] cback - pointer to the application callback function.
  799. **
  800. ** @return None
  801. **
  802. *******************************************************************************/
  803. void esp_ble_gatts_app_register(esp_bt_uuid_t *app_uuid, esp_gatts_cb_t *cback)
  804. {
  805. BTA_GATTS_AppRegister(app_uuid, cback);
  806. }
  807. /*******************************************************************************
  808. **
  809. ** @function esp_ble_gatts_app_unregister
  810. **
  811. ** @brief un-register with GATT Server.
  812. **
  813. ** @param[in] server_if: service interface id.
  814. **
  815. ** @return None
  816. **
  817. *******************************************************************************/
  818. void esp_ble_gatts_app_unregister(esp_gatts_if_t server_if)
  819. {
  820. BTA_GATTS_AppDeregister(server_if);
  821. }
  822. /*******************************************************************************
  823. **
  824. ** @function esp_ble_gatts_create_srvc
  825. **
  826. ** @brief Create a service. When service creation is done, a callback
  827. ** event BTA_GATTS_CREATE_SRVC_EVT is called to report status
  828. ** and service ID to the profile. The service ID obtained in
  829. ** the callback function needs to be used when adding included
  830. ** service and characteristics/descriptors into the service.
  831. **
  832. ** @param[in] server_if: Profile ID this service is belonged to.
  833. ** @param[in] service_uuid: service UUID.
  834. ** @param[in] inst: instance ID number of this service.
  835. ** @param[in] num_handle: numble of handle requessted for this service.
  836. ** @param[in] is_primary: is this service a primary one or not.
  837. **
  838. ** @return None
  839. **
  840. *******************************************************************************/
  841. void esp_ble_gatts_create_srvc(esp_gatts_if_t server_if, esp_bt_uuid_t *service_uuid, uint8_t inst,
  842. uint16_t num_handle, BOOLEAN is_primary)
  843. {
  844. BTA_GATTS_CreateService(server_if, service_uuid, inst, num_handle, is_primary);
  845. }
  846. /*******************************************************************************
  847. **
  848. ** @function esp_ble_gatts_add_inclu_srvc
  849. **
  850. ** @brief This function is called to add an included service. After included
  851. ** service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT
  852. ** is reported the included service ID.
  853. **
  854. ** @param[in] service_id: service ID to which this included service is to
  855. ** be added.
  856. ** @param[in] included_service_id: the service ID to be included.
  857. **
  858. ** @return None
  859. **
  860. *******************************************************************************/
  861. void esp_ble_gatts_add_inclu_srvc (uint16_t service_id, uint16_t included_service_id)
  862. {
  863. BTA_GATTS_AddIncludeService(service_id, included_service_id);
  864. }
  865. /*******************************************************************************
  866. **
  867. ** @function esp_ble_gatts_add_char
  868. **
  869. ** @brief This function is called to add a characteristic into a service.
  870. **
  871. ** @param[in] service_id: service ID to which this included service is to
  872. ** be added.
  873. ** @param[in] char_uuid : Characteristic UUID.
  874. ** @param[in] perm : Characteristic value declaration attribute permission.
  875. ** @param[in] property : Characteristic Properties
  876. **
  877. ** @return None
  878. **
  879. *******************************************************************************/
  880. void esp_ble_gatts_add_char (uint16_t service_id, esp_bt_uuid_t *char_uuid,
  881. esp_gatt_perm_t perm, esp_gatt_char_prop_t property)
  882. {
  883. BTA_GATTS_AddCharacteristic (service_id, char_uuid, perm, property);
  884. }
  885. /*******************************************************************************
  886. **
  887. ** @function esp_ble_gatts_add_char_descr
  888. **
  889. ** @brief This function is called to add characteristic descriptor. When
  890. ** it's done, a callback event BTA_GATTS_ADD_DESCR_EVT is called
  891. ** to report the status and an ID number for this descriptor.
  892. **
  893. ** @param[in] service_id: service ID to which this charatceristic descriptor is to
  894. ** be added.
  895. ** @param[in] perm: descriptor access permission.
  896. ** @param[in] descr_uuid: descriptor UUID.
  897. **
  898. ** @return None
  899. **
  900. *******************************************************************************/
  901. void esp_ble_gatts_add_char_descr (uint16_t service_id,
  902. esp_gatt_perm_t perm,
  903. esp_bt_uuid_t * descr_uuid)
  904. {
  905. BTA_GATTS_AddCharDescriptor (service_id, perm, descr_uuid);
  906. }
  907. /*******************************************************************************
  908. **
  909. ** @function esp_ble_gatts_dele_srvc
  910. **
  911. ** @brief This function is called to delete a service. When this is done,
  912. ** a callback event BTA_GATTS_DELETE_EVT is report with the status.
  913. **
  914. ** @param[in] service_id: service_id to be deleted.
  915. **
  916. ** @return None
  917. **
  918. *******************************************************************************/
  919. void esp_ble_gatts_dele_srvc (uint16_t service_id)
  920. {
  921. BTA_GATTS_DeleteService(service_id);
  922. }
  923. /*******************************************************************************
  924. **
  925. ** @function esp_ble_gatts_start_srvc
  926. **
  927. ** @brief This function is called to start a service.
  928. **
  929. ** @param[in] service_id: the service ID to be started.
  930. ** @param[in] sup_transport: supported trasnport.
  931. **
  932. ** @return None
  933. **
  934. *******************************************************************************/
  935. void esp_ble_gatts_start_srvc(uint16_t service_id)
  936. {
  937. tBTA_GATT_TRANSPORT transport = BT_TRANSPORT_LE;
  938. BTA_GATTS_StartService(service_id, transport);
  939. }
  940. /*******************************************************************************
  941. **
  942. ** @function esp_ble_gatts_stop_srvc
  943. **
  944. ** @brief This function is called to stop a service.
  945. **
  946. ** @param[in] service_id - service to be topped.
  947. **
  948. ** @return None
  949. **
  950. *******************************************************************************/
  951. void esp_ble_gatts_stop_srvc(uint16_t service_id)
  952. {
  953. BTA_GATTS_StopService(service_id);
  954. }
  955. /*******************************************************************************
  956. **
  957. ** @function esp_ble_gatts_hdl_val_indica
  958. **
  959. ** @brief This function is called to read a characteristics descriptor.
  960. **
  961. ** @param[in] bda - remote device bd address to indicate.
  962. ** @param[in] attr_id - attribute ID to indicate.
  963. ** @param[in] data_len - indicate data length.
  964. ** @param[in] data: data to indicate.
  965. ** @param[in] need_confirm - if this indication expects a confirmation or not.
  966. **
  967. ** @return None
  968. **
  969. *******************************************************************************/
  970. void esp_ble_gatts_hdl_val_indica (uint16_t conn_id, uint16_t attr_id, uint16_t data_len,
  971. uint8_t *data, BOOLEAN need_confirm)
  972. {
  973. BTA_GATTS_HandleValueIndication (conn_id, attr_id, data_len, data, need_confirm);
  974. }
  975. /*******************************************************************************
  976. **
  977. ** @function esp_ble_gatts_send_rsp
  978. **
  979. ** @brief This function is called to send a response to a request.
  980. **
  981. ** @param[in] conn_id - connection identifier.
  982. ** @param[in] trans_id - transaction ID.
  983. ** @param[in] status - response status
  984. ** @param[in] msg - response data.
  985. **
  986. ** @return None
  987. **
  988. *******************************************************************************/
  989. void esp_ble_gatts_send_rsp (uint16_t conn_id, uint32_t trans_id,
  990. esp_gatt_status_t status, esp_gatts_rsp_t *msg)
  991. {
  992. BTA_GATTS_SendRsp (conn_id, trans_id, status, msg);
  993. }
  994. /*******************************************************************************
  995. **
  996. ** @function esp_ble_gatts_conn
  997. **
  998. ** @brief Open a direct open connection or add a background auto connection
  999. ** bd address
  1000. **
  1001. ** @param[in] server_if: server interface.
  1002. ** @param[in] remote_bda: remote device BD address.
  1003. ** @param[in] is_direct: direct connection or background auto connection
  1004. **
  1005. ** @return None
  1006. **
  1007. *******************************************************************************/
  1008. void esp_ble_gatts_conn (esp_gatts_if_t server_if, BD_ADDR remote_bda, BOOLEAN is_direct)
  1009. {
  1010. tBTA_GATT_TRANSPORT transport = BT_TRANSPORT_LE;
  1011. BTA_GATTS_Open(server_if, remote_bda, is_direct, transport);
  1012. }
  1013. /*******************************************************************************
  1014. **
  1015. ** @function esp_ble_gatts_cancel_conn
  1016. **
  1017. ** @brief Cancel a direct open connection or remove a background auto connection
  1018. ** bd address
  1019. **
  1020. ** @param[in] server_if: server interface.
  1021. ** @param[in] remote_bda: remote device BD address.
  1022. ** @param[in] is_direct: direct connection or background auto connection
  1023. **
  1024. ** @return None
  1025. **
  1026. *******************************************************************************/
  1027. void esp_ble_gatts_cancel_conn (esp_gatts_if_t server_if, BD_ADDR remote_bda, BOOLEAN is_direct)
  1028. {
  1029. BTA_GATTS_CancelOpen(server_if, remote_bda, is_direct);
  1030. }
  1031. /*******************************************************************************
  1032. **
  1033. ** @function esp_ble_gatts_close
  1034. **
  1035. ** @param[in] Close a connection a remote device.
  1036. **
  1037. ** @param[in] conn_id: connectino ID to be closed.
  1038. **
  1039. ** @return None
  1040. **
  1041. *******************************************************************************/
  1042. void esp_ble_gatts_close(uint16_t conn_id)
  1043. {
  1044. BTA_GATTS_Close(conn_id);
  1045. }
  1046. /*******************************************************************************
  1047. **
  1048. ** @function esp_prf_app_register
  1049. **
  1050. ** @brief This function is called to register application callbacks
  1051. ** with BTA GATTS module.
  1052. **
  1053. ** @param[in] p_app_uuid - applicaiton UUID
  1054. ** @param[in] p_cback - pointer to the application callback function.
  1055. **
  1056. ** @return None
  1057. **
  1058. *******************************************************************************/
  1059. void esp_prf_app_register(uint8_t prf_id, void *p_cback)
  1060. {
  1061. //tBTA_GATTS_API_REG *p_buf;
  1062. /* register with BTA system manager */
  1063. if (bt_prf_sys_is_register(PRF_ID_SYS) == FALSE)
  1064. {
  1065. bt_prf_sys_register(PRF_ID_SYS, NULL);
  1066. }
  1067. // if ((p_buf = (tBTA_GATTS_API_REG *) GKI_getbuf(sizeof(tBTA_GATTS_API_REG))) != NULL)
  1068. // {
  1069. // p_buf->hdr.event = BTA_GATTS_API_REG_EVT;
  1070. // if (p_app_uuid != NULL)
  1071. // memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
  1072. // p_buf->p_cback = p_cback;
  1073. // bta_sys_sendmsg(p_buf);
  1074. // }
  1075. return;
  1076. }