esp_gattc_api.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. #if (GATTC_INCLUDED == TRUE)
  22. esp_err_t esp_ble_gattc_register_callback(esp_gattc_cb_t callback)
  23. {
  24. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  25. if (callback == NULL) {
  26. return ESP_FAIL;
  27. }
  28. btc_profile_cb_set(BTC_PID_GATTC, callback);
  29. return ESP_OK;
  30. }
  31. esp_err_t esp_ble_gattc_app_register(uint16_t app_id)
  32. {
  33. btc_msg_t msg;
  34. btc_ble_gattc_args_t arg;
  35. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  36. if (app_id > ESP_APP_ID_MAX) {
  37. return ESP_ERR_INVALID_ARG;
  38. }
  39. msg.sig = BTC_SIG_API_CALL;
  40. msg.pid = BTC_PID_GATTC;
  41. msg.act = BTC_GATTC_ACT_APP_REGISTER;
  42. arg.app_reg.app_id = app_id;
  43. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  44. }
  45. esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)
  46. {
  47. btc_msg_t msg;
  48. btc_ble_gattc_args_t arg;
  49. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  50. msg.sig = BTC_SIG_API_CALL;
  51. msg.pid = BTC_PID_GATTC;
  52. msg.act = BTC_GATTC_ACT_APP_UNREGISTER;
  53. arg.app_unreg.gattc_if = gattc_if;
  54. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  55. }
  56. 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)
  57. {
  58. btc_msg_t msg;
  59. btc_ble_gattc_args_t arg;
  60. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  61. msg.sig = BTC_SIG_API_CALL;
  62. msg.pid = BTC_PID_GATTC;
  63. msg.act = BTC_GATTC_ACT_OPEN;
  64. arg.open.gattc_if = gattc_if;
  65. memcpy(arg.open.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
  66. arg.open.remote_addr_type = remote_addr_type;
  67. arg.open.is_direct = is_direct;
  68. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  69. }
  70. esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id)
  71. {
  72. btc_msg_t msg;
  73. btc_ble_gattc_args_t arg;
  74. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  75. msg.sig = BTC_SIG_API_CALL;
  76. msg.pid = BTC_PID_GATTC;
  77. msg.act = BTC_GATTC_ACT_CLOSE;
  78. arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  79. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  80. }
  81. esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id)
  82. {
  83. btc_msg_t msg;
  84. btc_ble_gattc_args_t arg;
  85. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  86. msg.sig = BTC_SIG_API_CALL;
  87. msg.pid = BTC_PID_GATTC;
  88. msg.act = BTC_GATTC_ACT_CFG_MTU;
  89. arg.cfg_mtu.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  90. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  91. }
  92. esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *filter_uuid)
  93. {
  94. btc_msg_t msg;
  95. btc_ble_gattc_args_t arg;
  96. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  97. msg.sig = BTC_SIG_API_CALL;
  98. msg.pid = BTC_PID_GATTC;
  99. msg.act = BTC_GATTC_ACT_SEARCH_SERVICE;
  100. arg.search_srvc.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  101. if (filter_uuid) {
  102. arg.search_srvc.filter_uuid_enable = true;
  103. memcpy(&arg.search_srvc.filter_uuid, filter_uuid, sizeof(esp_bt_uuid_t));
  104. } else {
  105. arg.search_srvc.filter_uuid_enable = false;
  106. }
  107. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  108. }
  109. 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,
  110. esp_gattc_service_elem_t *result, uint16_t *count, uint16_t offset)
  111. {
  112. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  113. if (result == NULL || count == NULL || *count == 0) {
  114. return ESP_GATT_INVALID_PDU;
  115. }
  116. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  117. return btc_ble_gattc_get_service(conn_hdl, svc_uuid, result, count, offset);
  118. }
  119. esp_gatt_status_t esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if,
  120. uint16_t conn_id,
  121. uint16_t start_handle,
  122. uint16_t end_handle,
  123. esp_gattc_char_elem_t *result,
  124. uint16_t *count, uint16_t offset)
  125. {
  126. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  127. if ((start_handle == 0) && (end_handle == 0)) {
  128. *count = 0;
  129. return ESP_GATT_INVALID_HANDLE;
  130. }
  131. if (result == NULL || count == NULL || *count == 0) {
  132. return ESP_GATT_INVALID_PDU;
  133. }
  134. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  135. return btc_ble_gattc_get_all_char(conn_hdl, start_handle, end_handle, result, count, offset);
  136. }
  137. esp_gatt_status_t esp_ble_gattc_get_all_descr(esp_gatt_if_t gattc_if,
  138. uint16_t conn_id,
  139. uint16_t char_handle,
  140. esp_gattc_descr_elem_t *result,
  141. uint16_t *count, uint16_t offset)
  142. {
  143. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  144. if (char_handle == 0) {
  145. return ESP_GATT_INVALID_HANDLE;
  146. }
  147. if (result == NULL || count == NULL || *count == 0) {
  148. return ESP_GATT_INVALID_PDU;
  149. }
  150. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  151. return btc_ble_gattc_get_all_descr(conn_hdl, char_handle, result, count, offset);
  152. }
  153. esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if,
  154. uint16_t conn_id,
  155. uint16_t start_handle,
  156. uint16_t end_handle,
  157. esp_bt_uuid_t char_uuid,
  158. esp_gattc_char_elem_t *result,
  159. uint16_t *count)
  160. {
  161. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  162. if (start_handle == 0 && end_handle == 0) {
  163. *count = 0;
  164. return ESP_GATT_INVALID_HANDLE;
  165. }
  166. if (result == NULL || count == NULL || *count == 0) {
  167. return ESP_GATT_INVALID_PDU;
  168. }
  169. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if,conn_id);
  170. return btc_ble_gattc_get_char_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, result, count);
  171. }
  172. esp_gatt_status_t esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if,
  173. uint16_t conn_id,
  174. uint16_t start_handle,
  175. uint16_t end_handle,
  176. esp_bt_uuid_t char_uuid,
  177. esp_bt_uuid_t descr_uuid,
  178. esp_gattc_descr_elem_t *result,
  179. uint16_t *count)
  180. {
  181. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  182. if (result == NULL || count == NULL || *count == 0) {
  183. return ESP_GATT_INVALID_PDU;
  184. }
  185. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  186. return btc_ble_gattc_get_descr_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, descr_uuid, result, count);
  187. }
  188. esp_gatt_status_t esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if,
  189. uint16_t conn_id,
  190. uint16_t char_handle,
  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 (char_handle == 0) {
  197. *count = 0;
  198. return ESP_GATT_INVALID_HANDLE;
  199. }
  200. if (result == NULL || count == NULL || *count == 0) {
  201. return ESP_GATT_INVALID_PDU;
  202. }
  203. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  204. return btc_ble_gattc_get_descr_by_char_handle(conn_hdl, char_handle, descr_uuid, result, count);
  205. }
  206. esp_gatt_status_t esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if,
  207. uint16_t conn_id,
  208. uint16_t start_handle,
  209. uint16_t end_handle,
  210. esp_bt_uuid_t *incl_uuid,
  211. esp_gattc_incl_svc_elem_t *result,
  212. uint16_t *count)
  213. {
  214. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  215. if (start_handle == 0 && end_handle == 0) {
  216. *count = 0;
  217. return ESP_GATT_INVALID_HANDLE;
  218. }
  219. if (result == NULL || count == NULL || *count == 0) {
  220. return ESP_GATT_INVALID_PDU;
  221. }
  222. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  223. return btc_ble_gattc_get_include_service(conn_hdl, start_handle, end_handle, incl_uuid, result, count);
  224. }
  225. esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if,
  226. uint16_t conn_id,
  227. esp_gatt_db_attr_type_t type,
  228. uint16_t start_handle,
  229. uint16_t end_handle,
  230. uint16_t char_handle,
  231. uint16_t *count)
  232. {
  233. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  234. if ((start_handle == 0 && end_handle == 0) && (type != ESP_GATT_DB_DESCRIPTOR)) {
  235. *count = 0;
  236. return ESP_GATT_INVALID_HANDLE;
  237. }
  238. if (count == NULL) {
  239. return ESP_GATT_INVALID_PDU;
  240. }
  241. uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  242. return btc_ble_gattc_get_attr_count(conn_hdl, type, start_handle, end_handle, char_handle, count);
  243. }
  244. 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,
  245. esp_gattc_db_elem_t *db, uint16_t *count)
  246. {
  247. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  248. if (start_handle == 0 && end_handle == 0) {
  249. *count = 0;
  250. return ESP_GATT_INVALID_HANDLE;
  251. }
  252. if (db == NULL || count == NULL || *count == 0) {
  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_db(conn_hdl, start_handle, end_handle, db, count);
  257. }
  258. esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if,
  259. uint16_t conn_id, uint16_t handle,
  260. esp_gatt_auth_req_t auth_req)
  261. {
  262. btc_msg_t msg;
  263. btc_ble_gattc_args_t arg;
  264. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  265. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) {
  266. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  267. return ESP_FAIL;
  268. }
  269. msg.sig = BTC_SIG_API_CALL;
  270. msg.pid = BTC_PID_GATTC;
  271. msg.act = BTC_GATTC_ACT_READ_CHAR;
  272. arg.read_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  273. arg.read_char.handle = handle;
  274. arg.read_char.auth_req = auth_req;
  275. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  276. }
  277. esp_err_t esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if,
  278. uint16_t conn_id, esp_gattc_multi_t *read_multi,
  279. esp_gatt_auth_req_t auth_req)
  280. {
  281. btc_msg_t msg;
  282. btc_ble_gattc_args_t arg;
  283. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  284. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) {
  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_MULTIPLE_CHAR;
  291. arg.read_multiple.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  292. arg.read_multiple.num_attr = read_multi->num_attr;
  293. arg.read_multiple.auth_req = auth_req;
  294. if (read_multi->num_attr > 0) {
  295. memcpy(arg.read_multiple.handles, read_multi->handles, sizeof(uint16_t)*read_multi->num_attr);
  296. } else {
  297. LOG_ERROR("%s(), the num_attr should not be 0.", __func__);
  298. return ESP_FAIL;
  299. }
  300. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  301. }
  302. esp_err_t esp_ble_gattc_read_char_descr (esp_gatt_if_t gattc_if,
  303. uint16_t conn_id, uint16_t handle,
  304. esp_gatt_auth_req_t auth_req)
  305. {
  306. btc_msg_t msg;
  307. btc_ble_gattc_args_t arg;
  308. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  309. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) {
  310. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  311. return ESP_FAIL;
  312. }
  313. msg.sig = BTC_SIG_API_CALL;
  314. msg.pid = BTC_PID_GATTC;
  315. msg.act = BTC_GATTC_ACT_READ_CHAR_DESCR;
  316. arg.read_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  317. arg.read_descr.handle = handle;
  318. arg.read_descr.auth_req = auth_req;
  319. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  320. }
  321. esp_err_t esp_ble_gattc_write_char(esp_gatt_if_t gattc_if,
  322. uint16_t conn_id, uint16_t handle,
  323. uint16_t value_len,
  324. uint8_t *value,
  325. esp_gatt_write_type_t write_type,
  326. esp_gatt_auth_req_t auth_req)
  327. {
  328. btc_msg_t msg;
  329. btc_ble_gattc_args_t arg;
  330. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  331. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) {
  332. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  333. return ESP_FAIL;
  334. }
  335. msg.sig = BTC_SIG_API_CALL;
  336. msg.pid = BTC_PID_GATTC;
  337. msg.act = BTC_GATTC_ACT_WRITE_CHAR;
  338. arg.write_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  339. arg.write_char.handle = handle;
  340. arg.write_char.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
  341. arg.write_char.value = value;
  342. arg.write_char.write_type = write_type;
  343. arg.write_char.auth_req = auth_req;
  344. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  345. }
  346. esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if,
  347. uint16_t conn_id, uint16_t handle,
  348. uint16_t value_len,
  349. uint8_t *value,
  350. esp_gatt_write_type_t write_type,
  351. esp_gatt_auth_req_t auth_req)
  352. {
  353. btc_msg_t msg;
  354. btc_ble_gattc_args_t arg;
  355. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  356. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) {
  357. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  358. return ESP_FAIL;
  359. }
  360. msg.sig = BTC_SIG_API_CALL;
  361. msg.pid = BTC_PID_GATTC;
  362. msg.act = BTC_GATTC_ACT_WRITE_CHAR_DESCR;
  363. arg.write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  364. arg.write_descr.handle = handle;
  365. arg.write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
  366. arg.write_descr.value = value;
  367. arg.write_descr.write_type = write_type;
  368. arg.write_descr.auth_req = auth_req;
  369. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  370. }
  371. esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if,
  372. uint16_t conn_id, uint16_t handle,
  373. uint16_t offset,
  374. uint16_t value_len,
  375. uint8_t *value,
  376. esp_gatt_auth_req_t auth_req)
  377. {
  378. btc_msg_t msg;
  379. btc_ble_gattc_args_t arg;
  380. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  381. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) {
  382. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  383. return ESP_FAIL;
  384. }
  385. msg.sig = BTC_SIG_API_CALL;
  386. msg.pid = BTC_PID_GATTC;
  387. msg.act = BTC_GATTC_ACT_PREPARE_WRITE;
  388. arg.prep_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  389. arg.prep_write.handle = handle;
  390. arg.prep_write.offset = offset;
  391. arg.prep_write.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
  392. arg.prep_write.value = value;
  393. arg.prep_write.auth_req = auth_req;
  394. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  395. }
  396. esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if,
  397. uint16_t conn_id, uint16_t handle,
  398. uint16_t offset,
  399. uint16_t value_len,
  400. uint8_t *value,
  401. esp_gatt_auth_req_t auth_req)
  402. {
  403. btc_msg_t msg;
  404. btc_ble_gattc_args_t arg;
  405. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  406. if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) {
  407. LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
  408. return ESP_FAIL;
  409. }
  410. msg.sig = BTC_SIG_API_CALL;
  411. msg.pid = BTC_PID_GATTC;
  412. msg.act = BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR;
  413. arg.prep_write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  414. arg.prep_write_descr.handle = handle;
  415. arg.prep_write_descr.offset = offset;
  416. arg.prep_write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
  417. arg.prep_write_descr.value = value;
  418. arg.prep_write_descr.auth_req = auth_req;
  419. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  420. }
  421. esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id, bool is_execute)
  422. {
  423. btc_msg_t msg;
  424. btc_ble_gattc_args_t arg;
  425. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  426. msg.sig = BTC_SIG_API_CALL;
  427. msg.pid = BTC_PID_GATTC;
  428. msg.act = BTC_GATTC_ACT_EXECUTE_WRITE;
  429. arg.exec_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
  430. arg.exec_write.is_execute = is_execute;
  431. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  432. }
  433. esp_err_t esp_ble_gattc_register_for_notify (esp_gatt_if_t gattc_if,
  434. esp_bd_addr_t server_bda, uint16_t handle)
  435. {
  436. btc_msg_t msg;
  437. btc_ble_gattc_args_t arg;
  438. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  439. msg.sig = BTC_SIG_API_CALL;
  440. msg.pid = BTC_PID_GATTC;
  441. msg.act = BTC_GATTC_ACT_REG_FOR_NOTIFY;
  442. arg.reg_for_notify.gattc_if = gattc_if;
  443. memcpy(arg.reg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
  444. arg.reg_for_notify.handle = handle;
  445. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  446. }
  447. esp_err_t esp_ble_gattc_unregister_for_notify (esp_gatt_if_t gattc_if,
  448. esp_bd_addr_t server_bda, uint16_t handle)
  449. {
  450. btc_msg_t msg;
  451. btc_ble_gattc_args_t arg;
  452. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  453. msg.sig = BTC_SIG_API_CALL;
  454. msg.pid = BTC_PID_GATTC;
  455. msg.act = BTC_GATTC_ACT_UNREG_FOR_NOTIFY;
  456. arg.unreg_for_notify.gattc_if = gattc_if;
  457. arg.unreg_for_notify.handle = handle;
  458. memcpy(arg.unreg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
  459. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  460. }
  461. esp_err_t esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda)
  462. {
  463. btc_msg_t msg;
  464. btc_ble_gattc_args_t arg;
  465. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  466. msg.sig = BTC_SIG_API_CALL;
  467. msg.pid = BTC_PID_GATTC;
  468. msg.act = BTC_GATTC_ACT_CACHE_REFRESH;
  469. memcpy(arg.cache_refresh.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
  470. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  471. }
  472. 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)
  473. {
  474. btc_msg_t msg;
  475. btc_ble_gattc_args_t arg;
  476. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  477. msg.sig = BTC_SIG_API_CALL;
  478. msg.pid = BTC_PID_GATTC;
  479. msg.act = BTC_GATTC_ACT_CACHE_ASSOC;
  480. arg.cache_assoc.is_assoc = is_assoc;
  481. arg.cache_assoc.gattc_if = gattc_if;
  482. memcpy(arg.cache_assoc.src_addr, src_addr, sizeof(esp_bd_addr_t));
  483. memcpy(arg.cache_assoc.assoc_addr, assoc_addr, sizeof(esp_bd_addr_t));
  484. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  485. }
  486. esp_err_t esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if)
  487. {
  488. btc_msg_t msg;
  489. btc_ble_gattc_args_t arg;
  490. ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
  491. msg.sig = BTC_SIG_API_CALL;
  492. msg.pid = BTC_PID_GATTC;
  493. msg.act = BTC_GATTC_ATC_CACHE_GET_ADDR_LIST;
  494. arg.get_addr_list.gattc_if = gattc_if;
  495. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  496. }
  497. #endif ///GATTC_INCLUDED == TRUE