esp_blufi_api.c 5.1 KB

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