esp_gattc_api.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "esp_gattc_api.h"
  8. #include "esp_bt_main.h"
  9. #include "btc/btc_manage.h"
  10. #include "btc_gattc.h"
  11. #include "btc_gatt_util.h"
  12. #include "stack/l2cdefs.h"
  13. #include "stack/l2c_api.h"
  14. #include "gatt_int.h"
  15. #if (GATTC_INCLUDED == TRUE)
  16. esp_err_t esp_ble_gattc_register_callback(esp_gattc_cb_t callback)
  17. {
  18. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  19. if (callback == NULL) {
  20. return ESP_FAIL;
  21. }
  22. btc_profile_cb_set(BTC_PID_GATTC, callback);
  23. return ESP_OK;
  24. }
  25. esp_err_t esp_ble_gattc_app_register(uint16_t app_id)
  26. {
  27. btc_msg_t msg = {0};
  28. btc_ble_gattc_args_t arg;
  29. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  30. if (app_id > ESP_APP_ID_MAX) {
  31. return ESP_ERR_INVALID_ARG;
  32. }
  33. msg.sig = BTC_SIG_API_CALL;
  34. msg.pid = BTC_PID_GATTC;
  35. msg.act = BTC_GATTC_ACT_APP_REGISTER;
  36. arg.app_reg.app_id = app_id;
  37. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  38. }
  39. esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)
  40. {
  41. btc_msg_t msg = {0};
  42. btc_ble_gattc_args_t arg;
  43. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  44. msg.sig = BTC_SIG_API_CALL;
  45. msg.pid = BTC_PID_GATTC;
  46. msg.act = BTC_GATTC_ACT_APP_UNREGISTER;
  47. arg.app_unreg.gattc_if = gattc_if;
  48. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  49. }
  50. #if (BLE_42_FEATURE_SUPPORT == TRUE)
  51. esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct)
  52. {
  53. btc_msg_t msg = {0};
  54. btc_ble_gattc_args_t arg;
  55. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  56. msg.sig = BTC_SIG_API_CALL;
  57. msg.pid = BTC_PID_GATTC;
  58. msg.act = BTC_GATTC_ACT_OPEN;
  59. arg.open.gattc_if = gattc_if;
  60. memcpy(arg.open.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
  61. arg.open.remote_addr_type = remote_addr_type;
  62. arg.open.is_direct = is_direct;
  63. arg.open.is_aux = false;
  64. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  65. }
  66. #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
  67. #if (BLE_50_FEATURE_SUPPORT == TRUE)
  68. esp_err_t esp_ble_gattc_aux_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct)
  69. {
  70. btc_msg_t msg;
  71. btc_ble_gattc_args_t arg;
  72. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  73. msg.sig = BTC_SIG_API_CALL;
  74. msg.pid = BTC_PID_GATTC;
  75. msg.act = BTC_GATTC_ACT_AUX_OPEN;
  76. arg.open.gattc_if = gattc_if;
  77. memcpy(arg.open.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
  78. arg.open.remote_addr_type = remote_addr_type;
  79. arg.open.is_direct = is_direct;
  80. arg.open.is_aux = true;
  81. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  82. }
  83. #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
  84. esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id)
  85. {
  86. btc_msg_t msg = {0};
  87. btc_ble_gattc_args_t arg;
  88. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  89. msg.sig = BTC_SIG_API_CALL;
  90. msg.pid = BTC_PID_GATTC;
  91. msg.act = BTC_GATTC_ACT_CLOSE;
  92. arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  93. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  94. }
  95. esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id)
  96. {
  97. btc_msg_t msg = {0};
  98. btc_ble_gattc_args_t arg;
  99. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  100. msg.sig = BTC_SIG_API_CALL;
  101. msg.pid = BTC_PID_GATTC;
  102. msg.act = BTC_GATTC_ACT_CFG_MTU;
  103. arg.cfg_mtu.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  104. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  105. }
  106. esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *filter_uuid)
  107. {
  108. btc_msg_t msg = {0};
  109. btc_ble_gattc_args_t arg;
  110. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  111. msg.sig = BTC_SIG_API_CALL;
  112. msg.pid = BTC_PID_GATTC;
  113. msg.act = BTC_GATTC_ACT_SEARCH_SERVICE;
  114. arg.search_srvc.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  115. if (filter_uuid) {
  116. arg.search_srvc.filter_uuid_enable = true;
  117. memcpy(&arg.search_srvc.filter_uuid, filter_uuid, sizeof(esp_bt_uuid_t));
  118. } else {
  119. arg.search_srvc.filter_uuid_enable = false;
  120. }
  121. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  122. }
  123. esp_gatt_status_t esp_ble_gattc_get_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *svc_uuid,
  124. esp_gattc_service_elem_t *result, uint16_t *count, uint16_t offset)
  125. {
  126. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  127. if (result == NULL || count == NULL || *count == 0) {
  128. return ESP_GATT_INVALID_PDU;
  129. }
  130. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  131. return btc_ble_gattc_get_service(conn_hdl, svc_uuid, result, count, offset);
  132. }
  133. esp_gatt_status_t esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if,
  134. uint16_t conn_id,
  135. uint16_t start_handle,
  136. uint16_t end_handle,
  137. esp_gattc_char_elem_t *result,
  138. uint16_t *count, uint16_t offset)
  139. {
  140. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  141. if ((start_handle == 0) && (end_handle == 0)) {
  142. *count = 0;
  143. return ESP_GATT_INVALID_HANDLE;
  144. }
  145. if (result == NULL || count == NULL || *count == 0) {
  146. return ESP_GATT_INVALID_PDU;
  147. }
  148. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  149. return btc_ble_gattc_get_all_char(conn_hdl, start_handle, end_handle, result, count, offset);
  150. }
  151. esp_gatt_status_t esp_ble_gattc_get_all_descr(esp_gatt_if_t gattc_if,
  152. uint16_t conn_id,
  153. uint16_t char_handle,
  154. esp_gattc_descr_elem_t *result,
  155. uint16_t *count, uint16_t offset)
  156. {
  157. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  158. if (char_handle == 0) {
  159. return ESP_GATT_INVALID_HANDLE;
  160. }
  161. if (result == NULL || count == NULL || *count == 0) {
  162. return ESP_GATT_INVALID_PDU;
  163. }
  164. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  165. return btc_ble_gattc_get_all_descr(conn_hdl, char_handle, result, count, offset);
  166. }
  167. esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if,
  168. uint16_t conn_id,
  169. uint16_t start_handle,
  170. uint16_t end_handle,
  171. esp_bt_uuid_t char_uuid,
  172. esp_gattc_char_elem_t *result,
  173. uint16_t *count)
  174. {
  175. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  176. if (start_handle == 0 && end_handle == 0) {
  177. *count = 0;
  178. return ESP_GATT_INVALID_HANDLE;
  179. }
  180. if (result == NULL || count == NULL || *count == 0) {
  181. return ESP_GATT_INVALID_PDU;
  182. }
  183. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if,conn_id);
  184. return btc_ble_gattc_get_char_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, result, count);
  185. }
  186. esp_gatt_status_t esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if,
  187. uint16_t conn_id,
  188. uint16_t start_handle,
  189. uint16_t end_handle,
  190. esp_bt_uuid_t char_uuid,
  191. esp_bt_uuid_t descr_uuid,
  192. esp_gattc_descr_elem_t *result,
  193. uint16_t *count)
  194. {
  195. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  196. if (result == NULL || count == NULL || *count == 0) {
  197. return ESP_GATT_INVALID_PDU;
  198. }
  199. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  200. return btc_ble_gattc_get_descr_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, descr_uuid, result, count);
  201. }
  202. esp_gatt_status_t esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if,
  203. uint16_t conn_id,
  204. uint16_t char_handle,
  205. esp_bt_uuid_t descr_uuid,
  206. esp_gattc_descr_elem_t *result,
  207. uint16_t *count)
  208. {
  209. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  210. if (char_handle == 0) {
  211. *count = 0;
  212. return ESP_GATT_INVALID_HANDLE;
  213. }
  214. if (result == NULL || count == NULL || *count == 0) {
  215. return ESP_GATT_INVALID_PDU;
  216. }
  217. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  218. return btc_ble_gattc_get_descr_by_char_handle(conn_hdl, char_handle, descr_uuid, result, count);
  219. }
  220. esp_gatt_status_t esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if,
  221. uint16_t conn_id,
  222. uint16_t start_handle,
  223. uint16_t end_handle,
  224. esp_bt_uuid_t *incl_uuid,
  225. esp_gattc_incl_svc_elem_t *result,
  226. uint16_t *count)
  227. {
  228. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  229. if (start_handle == 0 && end_handle == 0) {
  230. *count = 0;
  231. return ESP_GATT_INVALID_HANDLE;
  232. }
  233. if (result == NULL || count == NULL || *count == 0) {
  234. return ESP_GATT_INVALID_PDU;
  235. }
  236. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  237. return btc_ble_gattc_get_include_service(conn_hdl, start_handle, end_handle, incl_uuid, result, count);
  238. }
  239. esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if,
  240. uint16_t conn_id,
  241. esp_gatt_db_attr_type_t type,
  242. uint16_t start_handle,
  243. uint16_t end_handle,
  244. uint16_t char_handle,
  245. uint16_t *count)
  246. {
  247. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  248. if ((start_handle == 0 && end_handle == 0) && (type != ESP_GATT_DB_DESCRIPTOR)) {
  249. *count = 0;
  250. return ESP_GATT_INVALID_HANDLE;
  251. }
  252. if (count == NULL) {
  253. return ESP_GATT_INVALID_PDU;
  254. }
  255. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  256. return btc_ble_gattc_get_attr_count(conn_hdl, type, start_handle, end_handle, char_handle, count);
  257. }
  258. esp_gatt_status_t esp_ble_gattc_get_db(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle,
  259. esp_gattc_db_elem_t *db, uint16_t *count)
  260. {
  261. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  262. if (start_handle == 0 && end_handle == 0) {
  263. *count = 0;
  264. return ESP_GATT_INVALID_HANDLE;
  265. }
  266. if (db == NULL || count == NULL || *count == 0) {
  267. return ESP_GATT_INVALID_PDU;
  268. }
  269. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  270. return btc_ble_gattc_get_db(conn_hdl, start_handle, end_handle, db, count);
  271. }
  272. esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if,
  273. uint16_t conn_id, uint16_t handle,
  274. esp_gatt_auth_req_t auth_req)
  275. {
  276. btc_msg_t msg = {0};
  277. btc_ble_gattc_args_t arg;
  278. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  279. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  280. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  281. LOG_WARN("%s, The connection not created.", __func__);
  282. return ESP_ERR_INVALID_STATE;
  283. }
  284. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  285. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  286. return ESP_FAIL;
  287. }
  288. msg.sig = BTC_SIG_API_CALL;
  289. msg.pid = BTC_PID_GATTC;
  290. msg.act = BTC_GATTC_ACT_READ_CHAR;
  291. arg.read_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  292. arg.read_char.handle = handle;
  293. arg.read_char.auth_req = auth_req;
  294. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  295. }
  296. esp_err_t esp_ble_gattc_read_by_type (esp_gatt_if_t gattc_if,
  297. uint16_t conn_id,
  298. uint16_t start_handle,
  299. uint16_t end_handle,
  300. esp_bt_uuid_t *uuid,
  301. esp_gatt_auth_req_t auth_req)
  302. {
  303. btc_msg_t msg = {0};
  304. btc_ble_gattc_args_t arg;
  305. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  306. if (uuid == NULL) {
  307. return ESP_GATT_ILLEGAL_PARAMETER;
  308. }
  309. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  310. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  311. LOG_WARN("%s, The connection not created.", __func__);
  312. return ESP_ERR_INVALID_STATE;
  313. }
  314. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  315. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  316. return ESP_FAIL;
  317. }
  318. msg.sig = BTC_SIG_API_CALL;
  319. msg.pid = BTC_PID_GATTC;
  320. msg.act = BTC_GATTC_ACT_READ_BY_TYPE;
  321. arg.read_by_type.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  322. arg.read_by_type.s_handle = start_handle;
  323. arg.read_by_type.e_handle = end_handle;
  324. arg.read_by_type.auth_req = auth_req;
  325. memcpy(&(arg.read_by_type.uuid), uuid, sizeof(esp_bt_uuid_t));
  326. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  327. }
  328. esp_err_t esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if,
  329. uint16_t conn_id, esp_gattc_multi_t *read_multi,
  330. esp_gatt_auth_req_t auth_req)
  331. {
  332. btc_msg_t msg = {0};
  333. btc_ble_gattc_args_t arg;
  334. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  335. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  336. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  337. LOG_WARN("%s, The connection not created.", __func__);
  338. return ESP_ERR_INVALID_STATE;
  339. }
  340. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  341. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  342. return ESP_FAIL;
  343. }
  344. msg.sig = BTC_SIG_API_CALL;
  345. msg.pid = BTC_PID_GATTC;
  346. msg.act = BTC_GATTC_ACT_READ_MULTIPLE_CHAR;
  347. arg.read_multiple.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  348. arg.read_multiple.num_attr = read_multi->num_attr;
  349. arg.read_multiple.auth_req = auth_req;
  350. if (read_multi->num_attr > 0) {
  351. memcpy(arg.read_multiple.handles, read_multi->handles, sizeof(uint16_t)*read_multi->num_attr);
  352. } else {
  353. LOG_ERROR("%s(), the num_attr should not be 0.", __func__);
  354. return ESP_FAIL;
  355. }
  356. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  357. }
  358. esp_err_t esp_ble_gattc_read_multiple_variable(esp_gatt_if_t gattc_if,
  359. uint16_t conn_id, esp_gattc_multi_t *read_multi,
  360. esp_gatt_auth_req_t auth_req)
  361. {
  362. btc_msg_t msg = {0};
  363. btc_ble_gattc_args_t arg;
  364. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  365. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  366. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  367. LOG_WARN("%s, The connection not created.", __func__);
  368. return ESP_ERR_INVALID_STATE;
  369. }
  370. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  371. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  372. return ESP_FAIL;
  373. }
  374. msg.sig = BTC_SIG_API_CALL;
  375. msg.pid = BTC_PID_GATTC;
  376. msg.act = BTC_GATTC_ACT_READ_MULTIPLE_VARIABLE_CHAR;
  377. arg.read_multiple.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  378. arg.read_multiple.num_attr = read_multi->num_attr;
  379. arg.read_multiple.auth_req = auth_req;
  380. if (read_multi->num_attr > 0) {
  381. memcpy(arg.read_multiple.handles, read_multi->handles, sizeof(uint16_t)*read_multi->num_attr);
  382. } else {
  383. LOG_ERROR("%s(), the num_attr should not be 0.", __func__);
  384. return ESP_FAIL;
  385. }
  386. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  387. }
  388. esp_err_t esp_ble_gattc_read_char_descr (esp_gatt_if_t gattc_if,
  389. uint16_t conn_id, uint16_t handle,
  390. esp_gatt_auth_req_t auth_req)
  391. {
  392. btc_msg_t msg = {0};
  393. btc_ble_gattc_args_t arg;
  394. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  395. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  396. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  397. LOG_WARN("%s, The connection not created.", __func__);
  398. return ESP_ERR_INVALID_STATE;
  399. }
  400. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  401. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  402. return ESP_FAIL;
  403. }
  404. msg.sig = BTC_SIG_API_CALL;
  405. msg.pid = BTC_PID_GATTC;
  406. msg.act = BTC_GATTC_ACT_READ_CHAR_DESCR;
  407. arg.read_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  408. arg.read_descr.handle = handle;
  409. arg.read_descr.auth_req = auth_req;
  410. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  411. }
  412. esp_err_t esp_ble_gattc_write_char(esp_gatt_if_t gattc_if,
  413. uint16_t conn_id, uint16_t handle,
  414. uint16_t value_len,
  415. uint8_t *value,
  416. esp_gatt_write_type_t write_type,
  417. esp_gatt_auth_req_t auth_req)
  418. {
  419. btc_msg_t msg = {0};
  420. btc_ble_gattc_args_t arg;
  421. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  422. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  423. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  424. LOG_WARN("%s, The connection not created.", __func__);
  425. return ESP_ERR_INVALID_STATE;
  426. }
  427. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  428. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  429. return ESP_FAIL;
  430. }
  431. msg.sig = BTC_SIG_API_CALL;
  432. msg.pid = BTC_PID_GATTC;
  433. msg.act = BTC_GATTC_ACT_WRITE_CHAR;
  434. arg.write_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  435. arg.write_char.handle = handle;
  436. arg.write_char.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
  437. arg.write_char.value = value;
  438. arg.write_char.write_type = write_type;
  439. arg.write_char.auth_req = auth_req;
  440. if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
  441. l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
  442. }
  443. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
  444. btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  445. }
  446. esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if,
  447. uint16_t conn_id, uint16_t handle,
  448. uint16_t value_len,
  449. uint8_t *value,
  450. esp_gatt_write_type_t write_type,
  451. esp_gatt_auth_req_t auth_req)
  452. {
  453. btc_msg_t msg = {0};
  454. btc_ble_gattc_args_t arg;
  455. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  456. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  457. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  458. LOG_WARN("%s, The connection not created.", __func__);
  459. return ESP_ERR_INVALID_STATE;
  460. }
  461. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  462. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  463. return ESP_FAIL;
  464. }
  465. msg.sig = BTC_SIG_API_CALL;
  466. msg.pid = BTC_PID_GATTC;
  467. msg.act = BTC_GATTC_ACT_WRITE_CHAR_DESCR;
  468. arg.write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  469. arg.write_descr.handle = handle;
  470. arg.write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
  471. arg.write_descr.value = value;
  472. arg.write_descr.write_type = write_type;
  473. arg.write_descr.auth_req = auth_req;
  474. if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
  475. l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
  476. }
  477. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
  478. btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  479. }
  480. esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if,
  481. uint16_t conn_id, uint16_t handle,
  482. uint16_t offset,
  483. uint16_t value_len,
  484. uint8_t *value,
  485. esp_gatt_auth_req_t auth_req)
  486. {
  487. btc_msg_t msg = {0};
  488. btc_ble_gattc_args_t arg;
  489. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  490. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  491. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  492. LOG_WARN("%s, The connection not created.", __func__);
  493. return ESP_ERR_INVALID_STATE;
  494. }
  495. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  496. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  497. return ESP_FAIL;
  498. }
  499. msg.sig = BTC_SIG_API_CALL;
  500. msg.pid = BTC_PID_GATTC;
  501. msg.act = BTC_GATTC_ACT_PREPARE_WRITE;
  502. arg.prep_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  503. arg.prep_write.handle = handle;
  504. arg.prep_write.offset = offset;
  505. arg.prep_write.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
  506. arg.prep_write.value = value;
  507. arg.prep_write.auth_req = auth_req;
  508. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
  509. btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  510. }
  511. esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if,
  512. uint16_t conn_id, uint16_t handle,
  513. uint16_t offset,
  514. uint16_t value_len,
  515. uint8_t *value,
  516. esp_gatt_auth_req_t auth_req)
  517. {
  518. btc_msg_t msg = {0};
  519. btc_ble_gattc_args_t arg;
  520. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  521. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(conn_id);
  522. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  523. LOG_WARN("%s, The connection not created.", __func__);
  524. return ESP_ERR_INVALID_STATE;
  525. }
  526. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
  527. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  528. return ESP_FAIL;
  529. }
  530. msg.sig = BTC_SIG_API_CALL;
  531. msg.pid = BTC_PID_GATTC;
  532. msg.act = BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR;
  533. arg.prep_write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  534. arg.prep_write_descr.handle = handle;
  535. arg.prep_write_descr.offset = offset;
  536. arg.prep_write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
  537. arg.prep_write_descr.value = value;
  538. arg.prep_write_descr.auth_req = auth_req;
  539. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
  540. btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  541. }
  542. esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id, bool is_execute)
  543. {
  544. btc_msg_t msg = {0};
  545. btc_ble_gattc_args_t arg;
  546. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  547. msg.sig = BTC_SIG_API_CALL;
  548. msg.pid = BTC_PID_GATTC;
  549. msg.act = BTC_GATTC_ACT_EXECUTE_WRITE;
  550. arg.exec_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  551. arg.exec_write.is_execute = is_execute;
  552. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  553. }
  554. esp_err_t esp_ble_gattc_register_for_notify (esp_gatt_if_t gattc_if,
  555. esp_bd_addr_t server_bda, uint16_t handle)
  556. {
  557. btc_msg_t msg = {0};
  558. btc_ble_gattc_args_t arg;
  559. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  560. msg.sig = BTC_SIG_API_CALL;
  561. msg.pid = BTC_PID_GATTC;
  562. msg.act = BTC_GATTC_ACT_REG_FOR_NOTIFY;
  563. arg.reg_for_notify.gattc_if = gattc_if;
  564. memcpy(arg.reg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
  565. arg.reg_for_notify.handle = handle;
  566. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  567. }
  568. esp_err_t esp_ble_gattc_unregister_for_notify (esp_gatt_if_t gattc_if,
  569. esp_bd_addr_t server_bda, uint16_t handle)
  570. {
  571. btc_msg_t msg = {0};
  572. btc_ble_gattc_args_t arg;
  573. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  574. msg.sig = BTC_SIG_API_CALL;
  575. msg.pid = BTC_PID_GATTC;
  576. msg.act = BTC_GATTC_ACT_UNREG_FOR_NOTIFY;
  577. arg.unreg_for_notify.gattc_if = gattc_if;
  578. arg.unreg_for_notify.handle = handle;
  579. memcpy(arg.unreg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
  580. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  581. }
  582. esp_err_t esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda)
  583. {
  584. btc_msg_t msg = {0};
  585. btc_ble_gattc_args_t arg;
  586. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  587. msg.sig = BTC_SIG_API_CALL;
  588. msg.pid = BTC_PID_GATTC;
  589. msg.act = BTC_GATTC_ACT_CACHE_REFRESH;
  590. memcpy(arg.cache_refresh.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
  591. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  592. }
  593. esp_err_t esp_ble_gattc_cache_clean(esp_bd_addr_t remote_bda)
  594. {
  595. btc_msg_t msg = {0};
  596. btc_ble_gattc_args_t arg;
  597. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  598. msg.sig = BTC_SIG_API_CALL;
  599. msg.pid = BTC_PID_GATTC;
  600. msg.act = BTC_GATTC_ACT_CACHE_CLEAN;
  601. memcpy(arg.cache_clean.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
  602. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  603. }
  604. esp_err_t esp_ble_gattc_cache_assoc(esp_gatt_if_t gattc_if, esp_bd_addr_t src_addr, esp_bd_addr_t assoc_addr, bool is_assoc)
  605. {
  606. btc_msg_t msg = {0};
  607. btc_ble_gattc_args_t arg;
  608. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  609. msg.sig = BTC_SIG_API_CALL;
  610. msg.pid = BTC_PID_GATTC;
  611. msg.act = BTC_GATTC_ACT_CACHE_ASSOC;
  612. arg.cache_assoc.is_assoc = is_assoc;
  613. arg.cache_assoc.gattc_if = gattc_if;
  614. memcpy(arg.cache_assoc.src_addr, src_addr, sizeof(esp_bd_addr_t));
  615. memcpy(arg.cache_assoc.assoc_addr, assoc_addr, sizeof(esp_bd_addr_t));
  616. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  617. }
  618. esp_err_t esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if)
  619. {
  620. btc_msg_t msg = {0};
  621. btc_ble_gattc_args_t arg;
  622. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  623. msg.sig = BTC_SIG_API_CALL;
  624. msg.pid = BTC_PID_GATTC;
  625. msg.act = BTC_GATTC_ATC_CACHE_GET_ADDR_LIST;
  626. arg.get_addr_list.gattc_if = gattc_if;
  627. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  628. }
  629. #endif ///GATTC_INCLUDED == TRUE