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
22 #include "osi/allocator.h"
23
24 #include "btc_blufi_prf.h"
25 #include "btc/btc_task.h"
26 #include "btc/btc_manage.h"
27
28 #include "blufi_int.h"
29
30 #include "esp_log.h"
31 #include "esp_blufi_api.h"
32 #include "esp_blufi.h"
33
34 #if (BLUFI_INCLUDED == TRUE)
35
36 #if GATT_DYNAMIC_MEMORY == FALSE
37 tBLUFI_ENV blufi_env;
38 #else
39 tBLUFI_ENV *blufi_env_ptr;
40 #endif
41
42 // static functions declare
43 static void btc_blufi_send_ack(uint8_t seq);
44
btc_blufi_cb_to_app(esp_blufi_cb_event_t event,esp_blufi_cb_param_t * param)45 inline void btc_blufi_cb_to_app(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
46 {
47 esp_blufi_event_cb_t btc_blufi_cb = (esp_blufi_event_cb_t)btc_profile_cb_get(BTC_PID_BLUFI);
48 if (btc_blufi_cb) {
49 btc_blufi_cb(event, param);
50 }
51 }
52
btc_blufi_profile_init(void)53 static uint8_t btc_blufi_profile_init(void)
54 {
55 esp_blufi_callbacks_t *store_p = blufi_env.cbs;
56
57 uint8_t rc;
58 if (blufi_env.enabled) {
59 BLUFI_TRACE_ERROR("BLUFI already initialized");
60 return ESP_BLUFI_ERROR;
61 }
62
63 memset(&blufi_env, 0x0, sizeof(blufi_env));
64 blufi_env.cbs = store_p; /* if set callback prior, restore the point */
65 blufi_env.frag_size = BLUFI_FRAG_DATA_DEFAULT_LEN;
66 rc = esp_blufi_init();
67 if(rc != 0 ){
68 return rc;
69 }
70
71 return ESP_BLUFI_SUCCESS;
72 }
73
btc_blufi_profile_deinit(void)74 static uint8_t btc_blufi_profile_deinit(void)
75 {
76 if (!blufi_env.enabled) {
77 BTC_TRACE_ERROR("BLUFI already de-initialized");
78 return ESP_BLUFI_ERROR;
79 }
80
81 esp_blufi_deinit();
82 return ESP_BLUFI_SUCCESS;
83 }
84
btc_blufi_send_notify(uint8_t * pkt,int pkt_len)85 void btc_blufi_send_notify(uint8_t *pkt, int pkt_len)
86 {
87 struct pkt_info pkts;
88 pkts.pkt = pkt;
89 pkts.pkt_len = pkt_len;
90 esp_blufi_send_notify(&pkts);
91 }
92
btc_blufi_report_error(esp_blufi_error_state_t state)93 void btc_blufi_report_error(esp_blufi_error_state_t state)
94 {
95 btc_msg_t msg;
96 msg.sig = BTC_SIG_API_CB;
97 msg.pid = BTC_PID_BLUFI;
98 msg.act = ESP_BLUFI_EVENT_REPORT_ERROR;
99 esp_blufi_cb_param_t param;
100 param.report_error.state = state;
101 btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL);
102 }
103
btc_blufi_recv_handler(uint8_t * data,int len)104 void btc_blufi_recv_handler(uint8_t *data, int len)
105 {
106 struct blufi_hdr *hdr = (struct blufi_hdr *)data;
107 uint16_t checksum, checksum_pkt;
108 int ret;
109
110 if (hdr->seq != blufi_env.recv_seq) {
111 BTC_TRACE_ERROR("%s seq %d is not expect %d\n", __func__, hdr->seq, blufi_env.recv_seq + 1);
112 btc_blufi_report_error(ESP_BLUFI_SEQUENCE_ERROR);
113 return;
114 }
115
116 blufi_env.recv_seq++;
117
118 // first step, decrypt
119 if (BLUFI_FC_IS_ENC(hdr->fc)
120 && (blufi_env.cbs && blufi_env.cbs->decrypt_func)) {
121 ret = blufi_env.cbs->decrypt_func(hdr->seq, hdr->data, hdr->data_len);
122 if (ret != hdr->data_len) { /* enc must be success and enc len must equal to plain len */
123 BTC_TRACE_ERROR("%s decrypt error %d\n", __func__, ret);
124 btc_blufi_report_error(ESP_BLUFI_DECRYPT_ERROR);
125 return;
126 }
127 }
128
129 // second step, check sum
130 if (BLUFI_FC_IS_CHECK(hdr->fc)
131 && (blufi_env.cbs && blufi_env.cbs->checksum_func)) {
132 checksum = blufi_env.cbs->checksum_func(hdr->seq, &hdr->seq, hdr->data_len + 2);
133 checksum_pkt = hdr->data[hdr->data_len] | (((uint16_t) hdr->data[hdr->data_len + 1]) << 8);
134 if (checksum != checksum_pkt) {
135 BTC_TRACE_ERROR("%s checksum error %04x, pkt %04x\n", __func__, checksum, checksum_pkt);
136 btc_blufi_report_error(ESP_BLUFI_CHECKSUM_ERROR);
137 return;
138 }
139 }
140
141 if (BLUFI_FC_IS_REQ_ACK(hdr->fc)) {
142 btc_blufi_send_ack(hdr->seq);
143 }
144
145 if (BLUFI_FC_IS_FRAG(hdr->fc)) {
146 if (blufi_env.offset == 0) {
147 blufi_env.total_len = hdr->data[0] | (((uint16_t) hdr->data[1]) << 8);
148 blufi_env.aggr_buf = osi_malloc(blufi_env.total_len);
149 if (blufi_env.aggr_buf == NULL) {
150 BTC_TRACE_ERROR("%s no mem, len %d\n", __func__, blufi_env.total_len);
151 btc_blufi_report_error(ESP_BLUFI_DH_MALLOC_ERROR);
152 return;
153 }
154 }
155 if (blufi_env.offset + hdr->data_len - 2 <= blufi_env.total_len){
156 memcpy(blufi_env.aggr_buf + blufi_env.offset, hdr->data + 2, hdr->data_len - 2);
157 blufi_env.offset += (hdr->data_len - 2);
158 } else {
159 BTC_TRACE_ERROR("%s payload is longer than packet length, len %d \n", __func__, blufi_env.total_len);
160 btc_blufi_report_error(ESP_BLUFI_DATA_FORMAT_ERROR);
161 return;
162 }
163
164 } else {
165 if (blufi_env.offset > 0) { /* if previous pkt is frag */
166 memcpy(blufi_env.aggr_buf + blufi_env.offset, hdr->data, hdr->data_len);
167
168 btc_blufi_protocol_handler(hdr->type, blufi_env.aggr_buf, blufi_env.total_len);
169 blufi_env.offset = 0;
170 osi_free(blufi_env.aggr_buf);
171 blufi_env.aggr_buf = NULL;
172 } else {
173 btc_blufi_protocol_handler(hdr->type, hdr->data, hdr->data_len);
174 blufi_env.offset = 0;
175 }
176 }
177 }
btc_blufi_send_encap(uint8_t type,uint8_t * data,int total_data_len)178 void btc_blufi_send_encap(uint8_t type, uint8_t *data, int total_data_len)
179 {
180 struct blufi_hdr *hdr = NULL;
181 int remain_len = total_data_len;
182 uint16_t checksum;
183 int ret;
184
185 if (blufi_env.is_connected == false) {
186 BTC_TRACE_ERROR("blufi connection has been disconnected \n");
187 return;
188 }
189
190 while (remain_len > 0) {
191 if (remain_len > blufi_env.frag_size) {
192 hdr = osi_malloc(sizeof(struct blufi_hdr) + 2 + blufi_env.frag_size + 2);
193 if (hdr == NULL) {
194 BTC_TRACE_ERROR("%s no mem\n", __func__);
195 return;
196 }
197 hdr->fc = 0x0;
198 hdr->data_len = blufi_env.frag_size + 2;
199 hdr->data[0] = remain_len & 0xff;
200 hdr->data[1] = (remain_len >> 8) & 0xff;
201 memcpy(hdr->data + 2, &data[total_data_len - remain_len], blufi_env.frag_size); //copy first, easy for check sum
202 hdr->fc |= BLUFI_FC_FRAG;
203 } else {
204 hdr = osi_malloc(sizeof(struct blufi_hdr) + remain_len + 2);
205 if (hdr == NULL) {
206 BTC_TRACE_ERROR("%s no mem\n", __func__);
207 return;
208 }
209 hdr->fc = 0x0;
210 hdr->data_len = remain_len;
211 memcpy(hdr->data, &data[total_data_len - remain_len], hdr->data_len); //copy first, easy for check sum
212 }
213
214 hdr->type = type;
215 hdr->fc |= BLUFI_FC_DIR_E2P;
216 hdr->seq = blufi_env.send_seq++;
217
218 if (BLUFI_TYPE_IS_CTRL(hdr->type)) {
219 if ((blufi_env.sec_mode & BLUFI_CTRL_SEC_MODE_CHECK_MASK)
220 && (blufi_env.cbs && blufi_env.cbs->checksum_func)) {
221 hdr->fc |= BLUFI_FC_CHECK;
222 checksum = blufi_env.cbs->checksum_func(hdr->seq, &hdr->seq, hdr->data_len + 2);
223 memcpy(&hdr->data[hdr->data_len], &checksum, 2);
224 }
225 } else if (!BLUFI_TYPE_IS_DATA_NEG(hdr->type) && !BLUFI_TYPE_IS_DATA_ERROR_INFO(hdr->type)) {
226 if ((blufi_env.sec_mode & BLUFI_DATA_SEC_MODE_CHECK_MASK)
227 && (blufi_env.cbs && blufi_env.cbs->checksum_func)) {
228 hdr->fc |= BLUFI_FC_CHECK;
229 checksum = blufi_env.cbs->checksum_func(hdr->seq, &hdr->seq, hdr->data_len + 2);
230 memcpy(&hdr->data[hdr->data_len], &checksum, 2);
231 }
232
233 if ((blufi_env.sec_mode & BLUFI_DATA_SEC_MODE_ENC_MASK)
234 && (blufi_env.cbs && blufi_env.cbs->encrypt_func)) {
235 ret = blufi_env.cbs->encrypt_func(hdr->seq, hdr->data, hdr->data_len);
236 if (ret == hdr->data_len) { /* enc must be success and enc len must equal to plain len */
237 hdr->fc |= BLUFI_FC_ENC;
238 } else {
239 BTC_TRACE_ERROR("%s encrypt error %d\n", __func__, ret);
240 btc_blufi_report_error(ESP_BLUFI_ENCRYPT_ERROR);
241 osi_free(hdr);
242 return;
243 }
244 }
245 }
246
247 if (hdr->fc & BLUFI_FC_FRAG) {
248 remain_len -= (hdr->data_len - 2);
249 } else {
250 remain_len -= hdr->data_len;
251 }
252
253 esp_blufi_send_encap(hdr);
254
255 osi_free(hdr);
256 hdr = NULL;
257 }
258 }
259
btc_blufi_wifi_conn_report(uint8_t opmode,uint8_t sta_conn_state,uint8_t softap_conn_num,esp_blufi_extra_info_t * info,int info_len)260 static void btc_blufi_wifi_conn_report(uint8_t opmode, uint8_t sta_conn_state, uint8_t softap_conn_num, esp_blufi_extra_info_t *info, int info_len)
261 {
262 uint8_t type;
263 uint8_t *data;
264 int data_len;
265 uint8_t *p;
266
267 data_len = info_len + 3;
268 p = data = osi_malloc(data_len);
269 if (data == NULL) {
270 return;
271 }
272
273 type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_WIFI_REP);
274 *p++ = opmode;
275 *p++ = sta_conn_state;
276 *p++ = softap_conn_num;
277
278 if (info) {
279 if (info->sta_bssid_set) {
280 *p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID;
281 *p++ = 6;
282 memcpy(p, info->sta_bssid, 6);
283 p += 6;
284 }
285 if (info->sta_ssid) {
286 *p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_SSID;
287 *p++ = info->sta_ssid_len;
288 memcpy(p, info->sta_ssid, info->sta_ssid_len);
289 p += info->sta_ssid_len;
290 }
291 if (info->sta_passwd) {
292 *p++ = BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD;
293 *p++ = info->sta_passwd_len;
294 memcpy(p, info->sta_passwd, info->sta_passwd_len);
295 p += info->sta_passwd_len;
296 }
297 if (info->softap_ssid) {
298 *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID;
299 *p++ = info->softap_ssid_len;
300 memcpy(p, info->softap_ssid, info->softap_ssid_len);
301 p += info->softap_ssid_len;
302 }
303 if (info->softap_passwd) {
304 *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD;
305 *p++ = info->softap_passwd_len;
306 memcpy(p, info->softap_passwd, info->softap_passwd_len);
307 p += info->softap_passwd_len;
308 }
309 if (info->softap_authmode_set) {
310 *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE;
311 *p++ = 1;
312 *p++ = info->softap_authmode;
313 }
314 if (info->softap_max_conn_num_set) {
315 *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM;
316 *p++ = 1;
317 *p++ = info->softap_max_conn_num;
318 }
319 if (info->softap_channel_set) {
320 *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL;
321 *p++ = 1;
322 *p++ = info->softap_channel;
323 }
324 }
325 if (p - data > data_len) {
326 BTC_TRACE_ERROR("%s len error %d %d\n", __func__, (int)(p - data), data_len);
327 }
328
329 btc_blufi_send_encap(type, data, data_len);
330 osi_free(data);
331 }
332
btc_blufi_send_wifi_list(uint16_t apCount,esp_blufi_ap_record_t * list)333 void btc_blufi_send_wifi_list(uint16_t apCount, esp_blufi_ap_record_t *list)
334 {
335 uint8_t type;
336 uint8_t *data;
337 int data_len;
338 uint8_t *p;
339 // malloc size: (len + RSSI + ssid buffer) * apCount;
340 uint malloc_size = (1 + 1 + sizeof(list->ssid)) * apCount;
341 p = data = osi_malloc(malloc_size);
342 if (data == NULL) {
343 BTC_TRACE_ERROR("malloc error\n");
344 return;
345 }
346 type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_WIFI_LIST);
347 for (int i = 0; i < apCount; ++i)
348 {
349 uint len = strlen((const char *)list[i].ssid);
350 data_len = (p - data);
351 //current_len + ssid + rssi + total_len_value
352 if((data_len + len + 1 + 1) > malloc_size) {
353 BTC_TRACE_ERROR("%s len error", __func__);
354 osi_free(data);
355 return;
356 }
357 *p++ = len + 1; // length of ssid + rssi
358 *p++ = list[i].rssi;
359 memcpy(p, list[i].ssid, len);
360 p = p + len;
361 }
362 data_len = (p - data);
363 btc_blufi_send_encap(type, data, data_len);
364 osi_free(data);
365 }
366
btc_blufi_send_ack(uint8_t seq)367 static void btc_blufi_send_ack(uint8_t seq)
368 {
369 uint8_t type;
370 uint8_t data;
371
372 type = BLUFI_BUILD_TYPE(BLUFI_TYPE_CTRL, BLUFI_TYPE_CTRL_SUBTYPE_ACK);
373 data = seq;
374
375 btc_blufi_send_encap(type, &data, 1);
376 }
btc_blufi_send_error_info(uint8_t state)377 static void btc_blufi_send_error_info(uint8_t state)
378 {
379 uint8_t type;
380 uint8_t *data;
381 int data_len;
382 uint8_t *p;
383
384 data_len = 1;
385 p = data = osi_malloc(data_len);
386 if (data == NULL) {
387 return;
388 }
389
390 type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_ERROR_INFO);
391 *p++ = state;
392 if (p - data > data_len) {
393 BTC_TRACE_ERROR("%s len error %d %d\n", __func__, (int)(p - data), data_len);
394 }
395
396 btc_blufi_send_encap(type, data, data_len);
397 osi_free(data);
398 }
399
btc_blufi_send_custom_data(uint8_t * value,uint32_t value_len)400 static void btc_blufi_send_custom_data(uint8_t *value, uint32_t value_len)
401 {
402 if(value == NULL || value_len == 0) {
403 BTC_TRACE_ERROR("%s value or value len error", __func__);
404 return;
405 }
406 uint8_t *data = osi_malloc(value_len);
407 if (data == NULL) {
408 BTC_TRACE_ERROR("%s mem malloc error", __func__);
409 return;
410 }
411 uint8_t type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_CUSTOM_DATA);
412 memcpy(data, value, value_len);
413 btc_blufi_send_encap(type, data, value_len);
414 osi_free(data);
415 }
416
btc_blufi_cb_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)417 void btc_blufi_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
418 {
419 esp_blufi_cb_param_t *dst = (esp_blufi_cb_param_t *) p_dest;
420 esp_blufi_cb_param_t *src = (esp_blufi_cb_param_t *) p_src;
421
422 switch (msg->act) {
423 case ESP_BLUFI_EVENT_RECV_STA_SSID:
424 dst->sta_ssid.ssid = osi_malloc(src->sta_ssid.ssid_len);
425 if (dst->sta_ssid.ssid == NULL) {
426 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
427 }
428 memcpy(dst->sta_ssid.ssid, src->sta_ssid.ssid, src->sta_ssid.ssid_len);
429 break;
430 case ESP_BLUFI_EVENT_RECV_STA_PASSWD:
431 dst->sta_passwd.passwd = osi_malloc(src->sta_passwd.passwd_len);
432 if (dst->sta_passwd.passwd == NULL) {
433 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
434 }
435 memcpy(dst->sta_passwd.passwd, src->sta_passwd.passwd, src->sta_passwd.passwd_len);
436 break;
437 case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID:
438 dst->softap_ssid.ssid = osi_malloc(src->softap_ssid.ssid_len);
439 if (dst->softap_ssid.ssid == NULL) {
440 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
441 }
442 memcpy(dst->softap_ssid.ssid, src->softap_ssid.ssid, src->softap_ssid.ssid_len);
443 break;
444 case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD:
445 dst->softap_passwd.passwd = osi_malloc(src->softap_passwd.passwd_len);
446 if (dst->softap_passwd.passwd == NULL) {
447 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
448 }
449 memcpy(dst->softap_passwd.passwd, src->softap_passwd.passwd, src->softap_passwd.passwd_len);
450 break;
451 case ESP_BLUFI_EVENT_RECV_USERNAME:
452 dst->username.name = osi_malloc(src->username.name_len);
453 if (dst->username.name == NULL) {
454 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
455 }
456 memcpy(dst->username.name, src->username.name, src->username.name_len);
457 break;
458 case ESP_BLUFI_EVENT_RECV_CA_CERT:
459 dst->ca.cert = osi_malloc(src->ca.cert_len);
460 if (dst->ca.cert == NULL) {
461 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
462 }
463 memcpy(dst->ca.cert, src->ca.cert, src->ca.cert_len);
464 break;
465 case ESP_BLUFI_EVENT_RECV_CLIENT_CERT:
466 dst->client_cert.cert = osi_malloc(src->client_cert.cert_len);
467 if (dst->client_cert.cert == NULL) {
468 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
469 }
470 memcpy(dst->client_cert.cert, src->client_cert.cert, src->client_cert.cert_len);
471 break;
472 case ESP_BLUFI_EVENT_RECV_SERVER_CERT:
473 dst->server_cert.cert = osi_malloc(src->server_cert.cert_len);
474 if (dst->server_cert.cert == NULL) {
475 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
476 }
477 memcpy(dst->server_cert.cert, src->server_cert.cert, src->server_cert.cert_len);
478 break;
479 case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY:
480 dst->client_pkey.pkey = osi_malloc(src->client_pkey.pkey_len);
481 if (dst->client_pkey.pkey == NULL) {
482 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
483 }
484 memcpy(dst->client_pkey.pkey, src->client_pkey.pkey, src->client_pkey.pkey_len);
485 break;
486 case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY:
487 dst->server_pkey.pkey = osi_malloc(src->server_pkey.pkey_len);
488 if (dst->server_pkey.pkey == NULL) {
489 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
490 }
491 memcpy(dst->server_pkey.pkey, src->server_pkey.pkey, src->server_pkey.pkey_len);
492 break;
493 case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA:
494 dst->custom_data.data = osi_malloc(src->custom_data.data_len);
495 if (dst->custom_data.data == NULL) {
496 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
497 break;
498 }
499 memcpy(dst->custom_data.data, src->custom_data.data, src->custom_data.data_len);
500 break;
501 default:
502 break;
503 }
504 }
505
btc_blufi_cb_deep_free(btc_msg_t * msg)506 void btc_blufi_cb_deep_free(btc_msg_t *msg)
507 {
508 esp_blufi_cb_param_t *param = (esp_blufi_cb_param_t *)msg->arg;
509
510 switch (msg->act) {
511 case ESP_BLUFI_EVENT_RECV_STA_SSID:
512 osi_free(param->sta_ssid.ssid);
513 break;
514 case ESP_BLUFI_EVENT_RECV_STA_PASSWD:
515 osi_free(param->sta_passwd.passwd);
516 break;
517 case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID:
518 osi_free(param->softap_ssid.ssid);
519 break;
520 case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD:
521 osi_free(param->softap_passwd.passwd);
522 break;
523 case ESP_BLUFI_EVENT_RECV_USERNAME:
524 osi_free(param->username.name);
525 break;
526 case ESP_BLUFI_EVENT_RECV_CA_CERT:
527 osi_free(param->ca.cert);
528 break;
529 case ESP_BLUFI_EVENT_RECV_CLIENT_CERT:
530 osi_free(param->client_cert.cert);
531 break;
532 case ESP_BLUFI_EVENT_RECV_SERVER_CERT:
533 osi_free(param->server_cert.cert);
534 break;
535 case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY:
536 osi_free(param->client_pkey.pkey);
537 break;
538 case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY:
539 osi_free(param->server_pkey.pkey);
540 break;
541 case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA:
542 osi_free(param->custom_data.data);
543 break;
544 default:
545 break;
546 }
547 }
548
btc_blufi_cb_handler(btc_msg_t * msg)549 void btc_blufi_cb_handler(btc_msg_t *msg)
550 {
551 esp_blufi_cb_param_t *param = (esp_blufi_cb_param_t *)msg->arg;
552
553 switch (msg->act) {
554 case ESP_BLUFI_EVENT_INIT_FINISH: {
555 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_INIT_FINISH, param);
556 break;
557 }
558 case ESP_BLUFI_EVENT_DEINIT_FINISH: {
559 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_DEINIT_FINISH, param);
560 break;
561 }
562 case ESP_BLUFI_EVENT_BLE_CONNECT:
563 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_BLE_CONNECT, param);
564 break;
565 case ESP_BLUFI_EVENT_BLE_DISCONNECT:
566 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_BLE_DISCONNECT, param);
567 break;
568 case ESP_BLUFI_EVENT_SET_WIFI_OPMODE:
569 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_SET_WIFI_OPMODE, param);
570 break;
571 case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP:
572 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP, NULL);
573 break;
574 case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP:
575 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP, NULL);
576 break;
577 case ESP_BLUFI_EVENT_GET_WIFI_STATUS:
578 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_GET_WIFI_STATUS, NULL);
579 break;
580 case ESP_BLUFI_EVENT_GET_WIFI_LIST:
581 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_GET_WIFI_LIST, NULL);
582 break;
583 case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA:
584 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_DEAUTHENTICATE_STA, NULL);
585 break;
586 case ESP_BLUFI_EVENT_RECV_STA_BSSID:
587 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_STA_BSSID, param);
588 break;
589 case ESP_BLUFI_EVENT_RECV_STA_SSID:
590 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_STA_SSID, param);
591 break;
592 case ESP_BLUFI_EVENT_RECV_STA_PASSWD:
593 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_STA_PASSWD, param);
594 break;
595 case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID:
596 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SOFTAP_SSID, param);
597 break;
598 case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD:
599 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD, param);
600 break;
601 case ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM:
602 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM, param);
603 break;
604 case ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE:
605 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE, param);
606 break;
607 case ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL:
608 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL, param);
609 break;
610 case ESP_BLUFI_EVENT_RECV_USERNAME:
611 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_USERNAME, param);
612 break;
613 case ESP_BLUFI_EVENT_RECV_CA_CERT:
614 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_CA_CERT, param);
615 break;
616 case ESP_BLUFI_EVENT_RECV_CLIENT_CERT:
617 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_CLIENT_CERT, param);
618 break;
619 case ESP_BLUFI_EVENT_RECV_SERVER_CERT:
620 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SERVER_CERT, param);
621 break;
622 case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY:
623 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY, param);
624 break;
625 case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY:
626 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY, param);
627 break;
628 case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE:
629 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE, param);
630 break;
631 case ESP_BLUFI_EVENT_REPORT_ERROR:
632 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_REPORT_ERROR, param);
633 break;
634 case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA:
635 btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_CUSTOM_DATA, param);
636 break;
637 default:
638 BTC_TRACE_ERROR("%s UNKNOWN %d\n", __func__, msg->act);
639 break;
640 }
641
642 btc_blufi_cb_deep_free(msg);
643 }
644
btc_blufi_call_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)645 void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
646 {
647 btc_blufi_args_t *dst = (btc_blufi_args_t *) p_dest;
648 btc_blufi_args_t *src = (btc_blufi_args_t *) p_src;
649
650 switch (msg->act) {
651 case BTC_BLUFI_ACT_SEND_CFG_REPORT: {
652 esp_blufi_extra_info_t *src_info = src->wifi_conn_report.extra_info;
653 dst->wifi_conn_report.extra_info_len = 0;
654 dst->wifi_conn_report.extra_info = NULL;
655
656 if (src_info == NULL) {
657 return;
658 }
659
660 dst->wifi_conn_report.extra_info = osi_calloc(sizeof(esp_blufi_extra_info_t));
661 if (dst->wifi_conn_report.extra_info == NULL) {
662 return;
663 }
664
665 if (src_info->sta_bssid_set) {
666 memcpy(dst->wifi_conn_report.extra_info->sta_bssid, src_info->sta_bssid, 6);
667 dst->wifi_conn_report.extra_info->sta_bssid_set = src_info->sta_bssid_set;
668 dst->wifi_conn_report.extra_info_len += (6 + 2);
669 }
670 if (src_info->sta_ssid) {
671 dst->wifi_conn_report.extra_info->sta_ssid = osi_malloc(src_info->sta_ssid_len);
672 if (dst->wifi_conn_report.extra_info->sta_ssid) {
673 memcpy(dst->wifi_conn_report.extra_info->sta_ssid, src_info->sta_ssid, src_info->sta_ssid_len);
674 dst->wifi_conn_report.extra_info->sta_ssid_len = src_info->sta_ssid_len;
675 dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_ssid_len + 2);
676 }
677 }
678 if (src_info->sta_passwd) {
679 dst->wifi_conn_report.extra_info->sta_passwd = osi_malloc(src_info->sta_passwd_len);
680 if (dst->wifi_conn_report.extra_info->sta_passwd) {
681 memcpy(dst->wifi_conn_report.extra_info->sta_passwd, src_info->sta_passwd, src_info->sta_passwd_len);
682 dst->wifi_conn_report.extra_info->sta_passwd_len = src_info->sta_passwd_len;
683 dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_passwd_len + 2);
684 }
685 }
686 if (src_info->softap_ssid) {
687 dst->wifi_conn_report.extra_info->softap_ssid = osi_malloc(src_info->softap_ssid_len);
688 if (dst->wifi_conn_report.extra_info->softap_ssid) {
689 memcpy(dst->wifi_conn_report.extra_info->softap_ssid, src_info->softap_ssid, src_info->softap_ssid_len);
690 dst->wifi_conn_report.extra_info->softap_ssid_len = src_info->softap_ssid_len;
691 dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_ssid_len + 2);
692 }
693 }
694 if (src_info->softap_passwd) {
695 dst->wifi_conn_report.extra_info->softap_passwd = osi_malloc(src_info->softap_passwd_len);
696 if (dst->wifi_conn_report.extra_info->softap_passwd) {
697 memcpy(dst->wifi_conn_report.extra_info->softap_passwd, src_info->softap_passwd, src_info->softap_passwd_len);
698 dst->wifi_conn_report.extra_info->softap_passwd_len = src_info->softap_passwd_len;
699 dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_passwd_len + 2);
700 }
701 }
702 if (src_info->softap_authmode_set) {
703 dst->wifi_conn_report.extra_info->softap_authmode_set = src_info->softap_authmode_set;
704 dst->wifi_conn_report.extra_info->softap_authmode = src_info->softap_authmode;
705 dst->wifi_conn_report.extra_info_len += (1 + 2);
706 }
707 if (src_info->softap_max_conn_num_set) {
708 dst->wifi_conn_report.extra_info->softap_max_conn_num_set = src_info->softap_max_conn_num_set;
709 dst->wifi_conn_report.extra_info->softap_max_conn_num = src_info->softap_max_conn_num;
710 dst->wifi_conn_report.extra_info_len += (1 + 2);
711 }
712 if (src_info->softap_channel_set) {
713 dst->wifi_conn_report.extra_info->softap_channel_set = src_info->softap_channel_set;
714 dst->wifi_conn_report.extra_info->softap_channel = src_info->softap_channel;
715 dst->wifi_conn_report.extra_info_len += (1 + 2);
716 }
717 break;
718 }
719 case BTC_BLUFI_ACT_SEND_WIFI_LIST:{
720 esp_blufi_ap_record_t *list = src->wifi_list.list;
721 src->wifi_list.list = NULL;
722 if (list == NULL || src->wifi_list.apCount <= 0) {
723 break;
724 }
725 dst->wifi_list.list = (esp_blufi_ap_record_t *)osi_malloc(sizeof(esp_blufi_ap_record_t) * src->wifi_list.apCount);
726 if (dst->wifi_list.list == NULL) {
727 break;
728 }
729 memcpy(dst->wifi_list.list, list, sizeof(esp_blufi_ap_record_t) * src->wifi_list.apCount);
730 break;
731 }
732 case BTC_BLUFI_ACT_SEND_CUSTOM_DATA:{
733 uint8_t *data = src->custom_data.data;
734 if(data == NULL) {
735 BTC_TRACE_ERROR("custom data is NULL\n");
736 break;
737 }
738 dst->custom_data.data = osi_malloc(src->custom_data.data_len);
739 if(dst->custom_data.data == NULL) {
740 BTC_TRACE_ERROR("custom data malloc error\n");
741 break;
742 }
743 memcpy(dst->custom_data.data, src->custom_data.data, src->custom_data.data_len);
744 break;
745 }
746 default:
747 break;
748 }
749 }
750
btc_blufi_call_deep_free(btc_msg_t * msg)751 void btc_blufi_call_deep_free(btc_msg_t *msg)
752 {
753 btc_blufi_args_t *arg = (btc_blufi_args_t *)msg->arg;
754
755 switch (msg->act) {
756 case BTC_BLUFI_ACT_SEND_CFG_REPORT: {
757 esp_blufi_extra_info_t *info = (esp_blufi_extra_info_t *)arg->wifi_conn_report.extra_info;
758
759 if (info == NULL) {
760 return;
761 }
762 if (info->sta_ssid) {
763 osi_free(info->sta_ssid);
764 }
765 if (info->sta_passwd) {
766 osi_free(info->sta_passwd);
767 }
768 if (info->softap_ssid) {
769 osi_free(info->softap_ssid);
770 }
771 if (info->softap_passwd) {
772 osi_free(info->softap_passwd);
773 }
774 osi_free(info);
775 break;
776 }
777 case BTC_BLUFI_ACT_SEND_WIFI_LIST:{
778 esp_blufi_ap_record_t *list = (esp_blufi_ap_record_t *)arg->wifi_list.list;
779 if (list){
780 osi_free(list);
781 }
782 break;
783 }
784 case BTC_BLUFI_ACT_SEND_CUSTOM_DATA:{
785 uint8_t *data = arg->custom_data.data;
786 if(data) {
787 osi_free(data);
788 }
789 break;
790 }
791 default:
792 break;
793 }
794 }
795
btc_blufi_call_handler(btc_msg_t * msg)796 void btc_blufi_call_handler(btc_msg_t *msg)
797 {
798 btc_blufi_args_t *arg = (btc_blufi_args_t *)msg->arg;
799
800 switch (msg->act) {
801 case BTC_BLUFI_ACT_INIT:
802 btc_blufi_profile_init();
803 break;
804 case BTC_BLUFI_ACT_DEINIT:
805 btc_blufi_profile_deinit();
806 break;
807 case BTC_BLUFI_ACT_SEND_CFG_REPORT:
808 btc_blufi_wifi_conn_report(arg->wifi_conn_report.opmode,
809 arg->wifi_conn_report.sta_conn_state,
810 arg->wifi_conn_report.softap_conn_num,
811 arg->wifi_conn_report.extra_info,
812 arg->wifi_conn_report.extra_info_len);
813 break;
814 case BTC_BLUFI_ACT_SEND_WIFI_LIST:{
815 btc_blufi_send_wifi_list(arg->wifi_list.apCount, arg->wifi_list.list);
816 break;
817 }
818 case BTC_BLUFI_ACT_SEND_ERR_INFO:
819 btc_blufi_send_error_info(arg->blufi_err_infor.state);
820 break;
821 case BTC_BLUFI_ACT_SEND_CUSTOM_DATA:
822 btc_blufi_send_custom_data(arg->custom_data.data, arg->custom_data.data_len);
823 break;
824 default:
825 BTC_TRACE_ERROR("%s UNKNOWN %d\n", __func__, msg->act);
826 break;
827 }
828 btc_blufi_call_deep_free(msg);
829 }
830
btc_blufi_set_callbacks(esp_blufi_callbacks_t * callbacks)831 void btc_blufi_set_callbacks(esp_blufi_callbacks_t *callbacks)
832 {
833 blufi_env.cbs = callbacks;
834 }
835
btc_blufi_get_version(void)836 uint16_t btc_blufi_get_version(void)
837 {
838 return BTC_BLUFI_VERSION;
839 }
840
841 #endif ///BLUFI_INCLUDED == TRUE
842