esp_gatts_api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_gatt_defs.h"
  15. #include "esp_gatts_api.h"
  16. #include "esp_bt_main.h"
  17. #include "btc_manage.h"
  18. #include "btc_gatts.h"
  19. #include "btc_gatt_util.h"
  20. #define COPY_TO_GATTS_ARGS(_gatt_args, _arg, _arg_type) memcpy(_gatt_args, _arg, sizeof(_arg_type))
  21. esp_err_t esp_ble_gatts_register_callback(esp_gatts_cb_t callback)
  22. {
  23. if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
  24. return ESP_ERR_INVALID_STATE;
  25. }
  26. return (btc_profile_cb_set(BTC_PID_GATTS, callback) == 0 ? ESP_OK : ESP_FAIL);
  27. }
  28. esp_err_t esp_ble_gatts_app_register(uint16_t app_id)
  29. {
  30. btc_msg_t msg;
  31. btc_ble_gatts_args_t arg;
  32. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  33. return ESP_ERR_INVALID_STATE;
  34. }
  35. //if (app_id < ESP_APP_ID_MIN || app_id > ESP_APP_ID_MAX) {
  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_GATTS;
  41. msg.act = BTC_GATTS_ACT_APP_REGISTER;
  42. arg.app_reg.app_id = app_id;
  43. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  44. }
  45. esp_err_t esp_ble_gatts_app_unregister(esp_gatt_if_t gatts_if)
  46. {
  47. btc_msg_t msg;
  48. btc_ble_gatts_args_t arg;
  49. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  50. return ESP_ERR_INVALID_STATE;
  51. }
  52. msg.sig = BTC_SIG_API_CALL;
  53. msg.pid = BTC_PID_GATTS;
  54. msg.act = BTC_GATTS_ACT_APP_UNREGISTER;
  55. arg.app_unreg.gatts_if = gatts_if;
  56. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  57. }
  58. esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatts_if,
  59. esp_gatt_srvc_id_t *service_id, uint16_t num_handle)
  60. {
  61. btc_msg_t msg;
  62. btc_ble_gatts_args_t arg;
  63. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  64. return ESP_ERR_INVALID_STATE;
  65. }
  66. msg.sig = BTC_SIG_API_CALL;
  67. msg.pid = BTC_PID_GATTS;
  68. msg.act = BTC_GATTS_ACT_CREATE_SERVICE;
  69. arg.create_srvc.gatts_if = gatts_if;
  70. arg.create_srvc.num_handle = num_handle;
  71. memcpy(&arg.create_srvc.service_id, service_id, sizeof(esp_gatt_srvc_id_t));
  72. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  73. }
  74. esp_err_t esp_ble_gatts_create_attr_tab(const esp_gatts_attr_db_t *gatts_attr_db,
  75. esp_gatt_if_t gatts_if,
  76. uint8_t max_nb_attr,
  77. uint8_t srvc_inst_id)
  78. {
  79. btc_msg_t msg;
  80. btc_ble_gatts_args_t arg;
  81. msg.sig = BTC_SIG_API_CALL;
  82. msg.pid = BTC_PID_GATTS;
  83. msg.act = BTC_GATTS_ACT_CREATE_ATTR_TAB;
  84. arg.create_attr_tab.gatts_if = gatts_if;
  85. arg.create_attr_tab.max_nb_attr = max_nb_attr;
  86. arg.create_attr_tab.srvc_inst_id = srvc_inst_id;
  87. arg.create_attr_tab.gatts_attr_db = (esp_gatts_attr_db_t *)gatts_attr_db;
  88. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), btc_gatts_arg_deep_copy)
  89. == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  90. }
  91. esp_err_t esp_ble_gatts_add_included_service(uint16_t service_handle, uint16_t included_service_handle)
  92. {
  93. btc_msg_t msg;
  94. btc_ble_gatts_args_t arg;
  95. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  96. return ESP_ERR_INVALID_STATE;
  97. }
  98. msg.sig = BTC_SIG_API_CALL;
  99. msg.pid = BTC_PID_GATTS;
  100. msg.act = BTC_GATTS_ACT_ADD_INCLUDE_SERVICE;
  101. arg.add_incl_srvc.service_handle = service_handle;
  102. arg.add_incl_srvc.included_service_handle = included_service_handle;
  103. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  104. }
  105. esp_err_t esp_ble_gatts_add_char(uint16_t service_handle, esp_bt_uuid_t *char_uuid,
  106. esp_gatt_perm_t perm, esp_gatt_char_prop_t property, esp_attr_value_t *char_val,
  107. esp_attr_control_t *control)
  108. {
  109. btc_msg_t msg;
  110. btc_ble_gatts_args_t arg;
  111. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  112. return ESP_ERR_INVALID_STATE;
  113. }
  114. /* parameter validation check */
  115. if ((control != NULL) && (control->auto_rsp == GATT_STACK_RSP)){
  116. if (char_val == NULL){
  117. LOG_ERROR("Error in %s, line=%d, for stack respond attribute, char_val should not be NULL here\n",\
  118. __func__, __LINE__);
  119. return ESP_ERR_INVALID_ARG;
  120. } else if (char_val->attr_max_len == 0){
  121. LOG_ERROR("Error in %s, line=%d, for stack respond attribute, attribute max length should not be 0\n",\
  122. __func__, __LINE__);
  123. return ESP_ERR_INVALID_ARG;
  124. }
  125. }
  126. if (char_val != NULL){
  127. if (char_val->attr_len > char_val->attr_max_len){
  128. LOG_ERROR("Error in %s, line=%d,attribute actual length (%d) should not be larger than max length (%d)\n",\
  129. __func__, __LINE__, char_val->attr_len, char_val->attr_max_len);
  130. return ESP_ERR_INVALID_ARG;
  131. }
  132. }
  133. memset(&arg, 0, sizeof(btc_ble_gatts_args_t));
  134. msg.sig = BTC_SIG_API_CALL;
  135. msg.pid = BTC_PID_GATTS;
  136. msg.act = BTC_GATTS_ACT_ADD_CHAR;
  137. arg.add_char.service_handle = service_handle;
  138. arg.add_char.perm = perm;
  139. arg.add_char.property = property;
  140. if (char_val != NULL) {
  141. arg.add_char.char_val.attr_max_len = char_val->attr_max_len;
  142. arg.add_char.char_val.attr_len = char_val->attr_len;
  143. arg.add_char.char_val.attr_value = char_val->attr_value;
  144. }
  145. if (control != NULL) {
  146. arg.add_char.attr_control.auto_rsp = control->auto_rsp;
  147. }
  148. memcpy(&arg.add_char.char_uuid, char_uuid, sizeof(esp_bt_uuid_t));
  149. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  150. }
  151. esp_err_t esp_ble_gatts_add_char_descr (uint16_t service_handle,
  152. esp_bt_uuid_t *descr_uuid,
  153. esp_gatt_perm_t perm, esp_attr_value_t *char_descr_val,
  154. esp_attr_control_t *control)
  155. {
  156. btc_msg_t msg;
  157. btc_ble_gatts_args_t arg;
  158. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  159. return ESP_ERR_INVALID_STATE;
  160. }
  161. /* parameter validation check */
  162. if ((control != NULL) && (control->auto_rsp == GATT_STACK_RSP)){
  163. if (char_descr_val == NULL){
  164. LOG_ERROR("Error in %s, line=%d, for stack respond attribute, char_descr_val should not be NULL here\n",\
  165. __func__, __LINE__);
  166. return ESP_ERR_INVALID_ARG;
  167. }
  168. else if (char_descr_val->attr_max_len == 0){
  169. LOG_ERROR("Error in %s, line=%d, for stack respond attribute, attribute max length should not be 0\n",\
  170. __func__, __LINE__);
  171. return ESP_ERR_INVALID_ARG;
  172. }
  173. }
  174. if (char_descr_val != NULL){
  175. if (char_descr_val->attr_len > char_descr_val->attr_max_len){
  176. LOG_ERROR("Error in %s, line=%d,attribute actual length (%d) should not be larger than max length (%d)\n",\
  177. __func__, __LINE__, char_descr_val->attr_len, char_descr_val->attr_max_len);
  178. return ESP_ERR_INVALID_ARG;
  179. }
  180. }
  181. memset(&arg, 0, sizeof(btc_ble_gatts_args_t));
  182. msg.sig = BTC_SIG_API_CALL;
  183. msg.pid = BTC_PID_GATTS;
  184. msg.act = BTC_GATTS_ACT_ADD_CHAR_DESCR;
  185. arg.add_descr.service_handle = service_handle;
  186. arg.add_descr.perm = perm;
  187. if (char_descr_val != NULL) {
  188. arg.add_descr.descr_val.attr_max_len = char_descr_val->attr_max_len;
  189. arg.add_descr.descr_val.attr_len = char_descr_val->attr_len;
  190. arg.add_descr.descr_val.attr_value = char_descr_val->attr_value;
  191. }
  192. if (control != NULL) {
  193. arg.add_descr.attr_control.auto_rsp = control->auto_rsp;
  194. }
  195. memcpy(&arg.add_descr.descr_uuid, descr_uuid, sizeof(esp_bt_uuid_t));
  196. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  197. }
  198. esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle)
  199. {
  200. btc_msg_t msg;
  201. btc_ble_gatts_args_t arg;
  202. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  203. return ESP_ERR_INVALID_STATE;
  204. }
  205. msg.sig = BTC_SIG_API_CALL;
  206. msg.pid = BTC_PID_GATTS;
  207. msg.act = BTC_GATTS_ACT_DELETE_SERVICE;
  208. arg.delete_srvc.service_handle = service_handle;
  209. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  210. }
  211. esp_err_t esp_ble_gatts_start_service(uint16_t service_handle)
  212. {
  213. btc_msg_t msg;
  214. btc_ble_gatts_args_t arg;
  215. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  216. return ESP_ERR_INVALID_STATE;
  217. }
  218. msg.sig = BTC_SIG_API_CALL;
  219. msg.pid = BTC_PID_GATTS;
  220. msg.act = BTC_GATTS_ACT_START_SERVICE;
  221. arg.start_srvc.service_handle = service_handle;
  222. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  223. }
  224. esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle)
  225. {
  226. btc_msg_t msg;
  227. btc_ble_gatts_args_t arg;
  228. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  229. return ESP_ERR_INVALID_STATE;
  230. }
  231. msg.sig = BTC_SIG_API_CALL;
  232. msg.pid = BTC_PID_GATTS;
  233. msg.act = BTC_GATTS_ACT_STOP_SERVICE;
  234. arg.stop_srvc.service_handle = service_handle;
  235. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  236. }
  237. esp_err_t esp_ble_gatts_send_indicate(esp_gatt_if_t gatts_if, uint16_t conn_id, uint16_t attr_handle,
  238. uint16_t value_len, uint8_t *value, bool need_confirm)
  239. {
  240. btc_msg_t msg;
  241. btc_ble_gatts_args_t arg;
  242. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  243. return ESP_ERR_INVALID_STATE;
  244. }
  245. msg.sig = BTC_SIG_API_CALL;
  246. msg.pid = BTC_PID_GATTS;
  247. msg.act = BTC_GATTS_ACT_SEND_INDICATE;
  248. arg.send_ind.conn_id = BTC_GATT_CREATE_CONN_ID(gatts_if, conn_id);
  249. arg.send_ind.attr_handle = attr_handle;
  250. arg.send_ind.need_confirm = need_confirm;
  251. arg.send_ind.value_len = value_len;
  252. arg.send_ind.value = value;
  253. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t),
  254. btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  255. }
  256. esp_err_t esp_ble_gatts_send_response(esp_gatt_if_t gatts_if, uint16_t conn_id, uint32_t trans_id,
  257. esp_gatt_status_t status, esp_gatt_rsp_t *rsp)
  258. {
  259. btc_msg_t msg;
  260. btc_ble_gatts_args_t arg;
  261. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  262. return ESP_ERR_INVALID_STATE;
  263. }
  264. msg.sig = BTC_SIG_API_CALL;
  265. msg.pid = BTC_PID_GATTS;
  266. msg.act = BTC_GATTS_ACT_SEND_RESPONSE;
  267. arg.send_rsp.conn_id = BTC_GATT_CREATE_CONN_ID(gatts_if, conn_id);
  268. arg.send_rsp.trans_id = trans_id;
  269. arg.send_rsp.status = status;
  270. arg.send_rsp.rsp = rsp;
  271. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t),
  272. btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  273. }
  274. esp_err_t esp_ble_gatts_set_attr_value(uint16_t attr_handle, uint16_t length, const uint8_t *value)
  275. {
  276. btc_msg_t msg;
  277. btc_ble_gatts_args_t arg;
  278. msg.sig = BTC_SIG_API_CALL;
  279. msg.pid = BTC_PID_GATTS;
  280. msg.act = BTC_GATTS_ACT_SET_ATTR_VALUE;
  281. arg.set_attr_val.length = length;
  282. arg.set_attr_val.value = (uint8_t *)value;
  283. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t),
  284. btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  285. }
  286. esp_err_t esp_ble_gatts_get_attr_value(uint16_t attr_handle, uint16_t *length, const uint8_t **value)
  287. {
  288. if (attr_handle == ESP_GATT_ILLEGAL_HANDLE) {
  289. return ESP_FAIL;
  290. }
  291. btc_gatts_get_attr_value(attr_handle, length, (uint8_t **)value);
  292. return ESP_OK;
  293. }
  294. esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatts_if, esp_bd_addr_t remote_bda, bool is_direct)
  295. {
  296. btc_msg_t msg;
  297. btc_ble_gatts_args_t arg;
  298. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  299. return ESP_ERR_INVALID_STATE;
  300. }
  301. msg.sig = BTC_SIG_API_CALL;
  302. msg.pid = BTC_PID_GATTS;
  303. msg.act = BTC_GATTS_ACT_OPEN;
  304. arg.open.gatts_if = gatts_if;
  305. arg.open.is_direct = is_direct;
  306. memcpy(&arg.open.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
  307. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL)
  308. == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  309. }
  310. esp_err_t esp_ble_gatts_close(esp_gatt_if_t gatts_if, uint16_t conn_id)
  311. {
  312. btc_msg_t msg;
  313. btc_ble_gatts_args_t arg;
  314. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  315. return ESP_ERR_INVALID_STATE;
  316. }
  317. msg.sig = BTC_SIG_API_CALL;
  318. msg.pid = BTC_PID_GATTS;
  319. msg.act = BTC_GATTS_ACT_CLOSE;
  320. arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gatts_if, conn_id);
  321. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL)
  322. == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  323. }