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