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 #include <stdint.h>
16 
17 #include <string.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 
21 #include "btc_blufi_prf.h"
22 #include "btc/btc_task.h"
23 #include "btc/btc_manage.h"
24 
25 #include "blufi_int.h"
26 
27 //#include "esp_wifi.h"
28 #if (BLUFI_INCLUDED == TRUE)
29 // extern tBLUFI_ENV blufi_env;
30 
btc_blufi_protocol_handler(uint8_t type,uint8_t * data,int len)31 void btc_blufi_protocol_handler(uint8_t type, uint8_t *data, int len)
32 {
33     btc_msg_t msg;
34     esp_blufi_cb_param_t param;
35     uint8_t *output_data = NULL;
36     int output_len = 0;
37     bool need_free = false;
38 
39     switch (BLUFI_GET_TYPE(type)) {
40     case BLUFI_TYPE_CTRL:
41         switch (BLUFI_GET_SUBTYPE(type)) {
42         case BLUFI_TYPE_CTRL_SUBTYPE_ACK:
43             /* TODO: check sequence */
44             break;
45         case BLUFI_TYPE_CTRL_SUBTYPE_SET_SEC_MODE:
46             blufi_env.sec_mode = data[0];
47             break;
48         case BLUFI_TYPE_CTRL_SUBTYPE_SET_WIFI_OPMODE:
49             msg.sig = BTC_SIG_API_CB;
50             msg.pid = BTC_PID_BLUFI;
51             msg.act = ESP_BLUFI_EVENT_SET_WIFI_OPMODE;
52             param.wifi_mode.op_mode = data[0];
53 
54             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), NULL);
55             break;
56         case BLUFI_TYPE_CTRL_SUBTYPE_CONN_TO_AP:
57             msg.sig = BTC_SIG_API_CB;
58             msg.pid = BTC_PID_BLUFI;
59             msg.act = ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP;
60 
61             btc_transfer_context(&msg, NULL, 0, NULL);
62             break;
63         case BLUFI_TYPE_CTRL_SUBTYPE_DISCONN_FROM_AP:
64             msg.sig = BTC_SIG_API_CB;
65             msg.pid = BTC_PID_BLUFI;
66             msg.act = ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP;
67 
68             btc_transfer_context(&msg, NULL, 0, NULL);
69             break;
70         case BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS:
71             msg.sig = BTC_SIG_API_CB;
72             msg.pid = BTC_PID_BLUFI;
73             msg.act = ESP_BLUFI_EVENT_GET_WIFI_STATUS;
74 
75             btc_transfer_context(&msg, NULL, 0, NULL);
76             break;
77         case BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA:
78             msg.sig = BTC_SIG_API_CB;
79             msg.pid = BTC_PID_BLUFI;
80             msg.act = ESP_BLUFI_EVENT_DEAUTHENTICATE_STA;
81 
82             btc_transfer_context(&msg, NULL, 0, NULL);
83             break;
84         case BLUFI_TYPE_CTRL_SUBTYPE_GET_VERSION: {
85             uint8_t type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_REPLY_VERSION);
86             uint8_t data[2];
87 
88             data[0] = BTC_BLUFI_GREAT_VER;
89             data[1] = BTC_BLUFI_SUB_VER;
90             btc_blufi_send_encap(type, &data[0], sizeof(data));
91             break;
92         }
93         case BLUFI_TYPE_CTRL_SUBTYPE_DISCONNECT_BLE:
94             msg.sig = BTC_SIG_API_CB;
95             msg.pid = BTC_PID_BLUFI;
96             msg.act = ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE;
97             btc_transfer_context(&msg, NULL, 0, NULL);
98             break;
99         case BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_LIST:
100             msg.sig = BTC_SIG_API_CB;
101             msg.pid = BTC_PID_BLUFI;
102             msg.act = ESP_BLUFI_EVENT_GET_WIFI_LIST;
103             btc_transfer_context(&msg, NULL, 0, NULL);
104             break;
105         default:
106             BTC_TRACE_ERROR("%s Unkown Ctrl pkt %02x\n", __func__, type);
107             break;
108         }
109         break;
110     case BLUFI_TYPE_DATA:
111         switch (BLUFI_GET_SUBTYPE(type)) {
112         case BLUFI_TYPE_DATA_SUBTYPE_NEG:
113             if (blufi_env.cbs && blufi_env.cbs->negotiate_data_handler) {
114                 blufi_env.cbs->negotiate_data_handler(data, len, &output_data, &output_len, &need_free);
115             }
116 
117             if (output_data && output_len > 0) {
118                 btc_blufi_send_encap(BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_NEG),
119                              output_data, output_len);
120             }
121             break;
122         case BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID:
123             msg.sig = BTC_SIG_API_CB;
124             msg.pid = BTC_PID_BLUFI;
125             msg.act = ESP_BLUFI_EVENT_RECV_STA_BSSID;
126             memcpy(param.sta_bssid.bssid, &data[0], 6);
127 
128             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), NULL);
129             break;
130         case BLUFI_TYPE_DATA_SUBTYPE_STA_SSID:
131             msg.sig = BTC_SIG_API_CB;
132             msg.pid = BTC_PID_BLUFI;
133             msg.act = ESP_BLUFI_EVENT_RECV_STA_SSID;
134             param.sta_ssid.ssid = &data[0];
135             param.sta_ssid.ssid_len = len;
136 
137             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
138             break;
139         case BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD:
140             msg.sig = BTC_SIG_API_CB;
141             msg.pid = BTC_PID_BLUFI;
142             msg.act = ESP_BLUFI_EVENT_RECV_STA_PASSWD;
143             param.sta_passwd.passwd = &data[0];
144             param.sta_passwd.passwd_len = len;
145 
146             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
147             break;
148         case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID:
149             msg.sig = BTC_SIG_API_CB;
150             msg.pid = BTC_PID_BLUFI;
151             msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_SSID;
152             param.softap_ssid.ssid = &data[0];
153             param.softap_ssid.ssid_len = len;
154 
155             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
156             break;
157         case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD:
158             msg.sig = BTC_SIG_API_CB;
159             msg.pid = BTC_PID_BLUFI;
160             msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD;
161             param.softap_passwd.passwd = &data[0];
162             param.softap_passwd.passwd_len = len;
163 
164             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
165             break;
166         case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM:
167             msg.sig = BTC_SIG_API_CB;
168             msg.pid = BTC_PID_BLUFI;
169             msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM;
170             param.softap_max_conn_num.max_conn_num = data[0];
171 
172             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), NULL);
173             break;
174         case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE:
175             msg.sig = BTC_SIG_API_CB;
176             msg.pid = BTC_PID_BLUFI;
177             msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE;
178             param.softap_auth_mode.auth_mode = data[0];
179 
180             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), NULL);
181             break;
182         case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL:
183             msg.sig = BTC_SIG_API_CB;
184             msg.pid = BTC_PID_BLUFI;
185             msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL;
186             param.softap_channel.channel = data[0];
187 
188             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), NULL);
189             break;
190         case BLUFI_TYPE_DATA_SUBTYPE_USERNAME:
191             msg.sig = BTC_SIG_API_CB;
192             msg.pid = BTC_PID_BLUFI;
193             msg.act = ESP_BLUFI_EVENT_RECV_USERNAME;
194             param.username.name = &data[0];
195             param.username.name_len = len;
196 
197             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
198             break;
199         case BLUFI_TYPE_DATA_SUBTYPE_CA:
200             msg.sig = BTC_SIG_API_CB;
201             msg.pid = BTC_PID_BLUFI;
202             msg.act = ESP_BLUFI_EVENT_RECV_CA_CERT;
203             param.ca.cert = &data[0];
204             param.ca.cert_len = len;
205 
206             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
207             break;
208         case BLUFI_TYPE_DATA_SUBTYPE_CLIENT_CERT:
209             msg.sig = BTC_SIG_API_CB;
210             msg.pid = BTC_PID_BLUFI;
211             msg.act = ESP_BLUFI_EVENT_RECV_CLIENT_CERT;
212             param.client_cert.cert = &data[0];
213             param.client_cert.cert_len = len;
214 
215             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
216             break;
217         case BLUFI_TYPE_DATA_SUBTYPE_SERVER_CERT:
218             msg.sig = BTC_SIG_API_CB;
219             msg.pid = BTC_PID_BLUFI;
220             msg.act = ESP_BLUFI_EVENT_RECV_SERVER_CERT;
221             param.client_cert.cert = &data[0];
222             param.client_cert.cert_len = len;
223 
224             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
225             break;
226         case BLUFI_TYPE_DATA_SUBTYPE_CLIENT_PRIV_KEY:
227             msg.sig = BTC_SIG_API_CB;
228             msg.pid = BTC_PID_BLUFI;
229             msg.act = ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY;
230             param.client_pkey.pkey = &data[0];
231             param.client_pkey.pkey_len = len;
232 
233             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
234             break;
235         case BLUFI_TYPE_DATA_SUBTYPE_SERVER_PRIV_KEY:
236             msg.sig = BTC_SIG_API_CB;
237             msg.pid = BTC_PID_BLUFI;
238             msg.act = ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY;
239             param.client_pkey.pkey = &data[0];
240             param.client_pkey.pkey_len = len;
241 
242             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
243             break;
244         case BLUFI_TYPE_DATA_SUBTYPE_CUSTOM_DATA:
245             msg.sig = BTC_SIG_API_CB;
246             msg.pid = BTC_PID_BLUFI;
247             msg.act = ESP_BLUFI_EVENT_RECV_CUSTOM_DATA;
248             param.custom_data.data = &data[0];
249             param.custom_data.data_len = len;
250             btc_transfer_context(&msg, &param, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy);
251             break;
252         default:
253             BTC_TRACE_ERROR("%s Unkown Ctrl pkt %02x\n", __func__, type);
254             break;
255         }
256         break;
257     default:
258         break;
259     }
260 }
261 
262 #endif  ///BLUFI_INCLUDED == TRUE
263