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 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #include "esp_blufi_api.h"
17 #include "btc/btc_task.h"
18 #include "btc_blufi_prf.h"
19 #include "btc/btc_manage.h"
20 #include "osi/future.h"
21 #if (BLUFI_INCLUDED == TRUE)
esp_blufi_register_callbacks(esp_blufi_callbacks_t * callbacks)22 esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks)
23 {
24     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
25 
26     if (callbacks == NULL) {
27         return ESP_FAIL;
28     }
29 
30     btc_blufi_set_callbacks(callbacks);
31     return (btc_profile_cb_set(BTC_PID_BLUFI, callbacks->event_cb) == 0 ? ESP_OK : ESP_FAIL);
32 }
33 
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)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 
39     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
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 
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_blufi_send_wifi_list(uint16_t apCount,esp_blufi_ap_record_t * list)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 
57     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
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 
65     return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), btc_blufi_call_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
66 }
67 
esp_blufi_profile_init(void)68 esp_err_t esp_blufi_profile_init(void)
69 {
70     btc_msg_t msg;
71 
72     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
73 
74     msg.sig = BTC_SIG_API_CALL;
75     msg.pid = BTC_PID_BLUFI;
76     msg.act = BTC_BLUFI_ACT_INIT;
77 
78     return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
79 }
80 
esp_blufi_profile_deinit(void)81 esp_err_t esp_blufi_profile_deinit(void)
82 {
83     btc_msg_t msg;
84 
85     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
86 
87     msg.sig = BTC_SIG_API_CALL;
88     msg.pid = BTC_PID_BLUFI;
89     msg.act = BTC_BLUFI_ACT_DEINIT;
90 
91     return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
92 }
93 
esp_blufi_get_version(void)94 uint16_t esp_blufi_get_version(void)
95 {
96     return btc_blufi_get_version();
97 }
98 
esp_blufi_send_error_info(esp_blufi_error_state_t state)99 esp_err_t esp_blufi_send_error_info(esp_blufi_error_state_t state)
100 {
101     btc_msg_t msg;
102     btc_blufi_args_t arg;
103 
104     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
105 
106     msg.sig = BTC_SIG_API_CALL;
107     msg.pid = BTC_PID_BLUFI;
108     msg.act = BTC_BLUFI_ACT_SEND_ERR_INFO;
109     arg.blufi_err_infor.state = state;
110 
111     return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
112 }
113 
esp_blufi_send_custom_data(uint8_t * data,uint32_t data_len)114 esp_err_t esp_blufi_send_custom_data(uint8_t *data, uint32_t data_len)
115 {
116     btc_msg_t msg;
117     btc_blufi_args_t arg;
118     if(data == NULL || data_len == 0) {
119         return ESP_ERR_INVALID_ARG;
120     }
121     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
122 
123     msg.sig = BTC_SIG_API_CALL;
124     msg.pid = BTC_PID_BLUFI;
125     msg.act = BTC_BLUFI_ACT_SEND_CUSTOM_DATA;
126     arg.custom_data.data = data;
127     arg.custom_data.data_len = data_len;
128 
129     return (btc_transfer_context(&msg, &arg, sizeof(btc_blufi_args_t), btc_blufi_call_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
130 }
131 #endif  ///BLUFI_INCLUDED == TRUE
132