esp_gattc_api.c 27 KB

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