esp_blufi_api.c 5.1 KB

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