esp_blufi_api.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "esp_blufi_api.h"
  14. #include "esp_bt_defs.h"
  15. #include "esp_bt_main.h"
  16. #include "btc/btc_task.h"
  17. #include "btc_blufi_prf.h"
  18. #include "btc/btc_manage.h"
  19. #include "btc/btc_main.h"
  20. #include "osi/future.h"
  21. #include "btc_gatts.h"
  22. #include "btc_gatt_util.h"
  23. esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks)
  24. {
  25. if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
  26. return ESP_ERR_INVALID_STATE;
  27. }
  28. if (callbacks == NULL) {
  29. return ESP_FAIL;
  30. }
  31. btc_blufi_set_callbacks(callbacks);
  32. return (btc_profile_cb_set(BTC_PID_BLUFI, callbacks->event_cb) == 0 ? ESP_OK : ESP_FAIL);
  33. }
  34. esp_err_t esp_blufi_send_wifi_conn_report(wifi_mode_t opmode, esp_blufi_sta_conn_state_t sta_conn_state, uint8_t softap_conn_num, esp_blufi_extra_info_t *extra_info)
  35. {
  36. btc_msg_t msg;
  37. btc_blufi_args_t arg;
  38. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  39. return ESP_ERR_INVALID_STATE;
  40. }
  41. msg.sig = BTC_SIG_API_CALL;
  42. msg.pid = BTC_PID_BLUFI;
  43. msg.act = BTC_BLUFI_ACT_SEND_CFG_REPORT;
  44. arg.wifi_conn_report.opmode = opmode;
  45. arg.wifi_conn_report.sta_conn_state = sta_conn_state;
  46. arg.wifi_conn_report.softap_conn_num = softap_conn_num;
  47. arg.wifi_conn_report.extra_info = extra_info;
  48. return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), btc_blufi_call_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  49. }
  50. esp_err_t esp_blufi_send_wifi_list(uint16_t apCount, esp_blufi_ap_record_t *list)
  51. {
  52. btc_msg_t msg;
  53. btc_blufi_args_t arg;
  54. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  55. return ESP_ERR_INVALID_STATE;
  56. }
  57. msg.sig = BTC_SIG_API_CALL;
  58. msg.pid = BTC_PID_BLUFI;
  59. msg.act = BTC_BLUFI_ACT_SEND_WIFI_LIST;
  60. arg.wifi_list.apCount = apCount;
  61. arg.wifi_list.list = list;
  62. return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), btc_blufi_call_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  63. }
  64. esp_err_t esp_blufi_profile_init(void)
  65. {
  66. btc_msg_t msg;
  67. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  68. return ESP_ERR_INVALID_STATE;
  69. }
  70. msg.sig = BTC_SIG_API_CALL;
  71. msg.pid = BTC_PID_BLUFI;
  72. msg.act = BTC_BLUFI_ACT_INIT;
  73. return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  74. }
  75. esp_err_t esp_blufi_profile_deinit(void)
  76. {
  77. btc_msg_t msg;
  78. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  79. return ESP_ERR_INVALID_STATE;
  80. }
  81. msg.sig = BTC_SIG_API_CALL;
  82. msg.pid = BTC_PID_BLUFI;
  83. msg.act = BTC_BLUFI_ACT_DEINIT;
  84. return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  85. }
  86. uint16_t esp_blufi_get_version(void)
  87. {
  88. return btc_blufi_get_version();
  89. }
  90. esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id)
  91. {
  92. btc_msg_t msg;
  93. btc_ble_gatts_args_t arg;
  94. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  95. return ESP_ERR_INVALID_STATE;
  96. }
  97. msg.sig = BTC_SIG_API_CALL;
  98. msg.pid = BTC_PID_GATTS;
  99. msg.act = BTC_GATTS_ACT_CLOSE;
  100. arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gatts_if, conn_id);
  101. return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL)
  102. == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  103. }
  104. esp_err_t esp_blufi_send_error_info(esp_blufi_error_state_t state)
  105. {
  106. btc_msg_t msg;
  107. btc_blufi_args_t arg;
  108. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  109. return ESP_ERR_INVALID_STATE;
  110. }
  111. msg.sig = BTC_SIG_API_CALL;
  112. msg.pid = BTC_PID_BLUFI;
  113. msg.act = BTC_BLUFI_ACT_SEND_ERR_INFO;
  114. arg.blufi_err_infor.state = state;
  115. return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  116. }
  117. esp_err_t esp_blufi_send_custom_data(uint8_t *data, uint32_t data_len)
  118. {
  119. btc_msg_t msg;
  120. btc_blufi_args_t arg;
  121. if(data == NULL || data_len == 0) {
  122. return ESP_ERR_INVALID_ARG;
  123. }
  124. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  125. return ESP_ERR_INVALID_STATE;
  126. }
  127. msg.sig = BTC_SIG_API_CALL;
  128. msg.pid = BTC_PID_BLUFI;
  129. msg.act = BTC_BLUFI_ACT_SEND_CUSTOM_DATA;
  130. arg.custom_data.data = data;
  131. arg.custom_data.data_len = data_len;
  132. return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), btc_blufi_call_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
  133. }