2021-12-06 01:59:55 -05:00
|
|
|
/*
|
2022-01-14 12:57:46 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
2021-12-06 01:59:55 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-12-27 23:02:43 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "btc_blufi_prf.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "btc/btc_task.h"
|
|
|
|
#include "btc/btc_manage.h"
|
2016-12-27 23:02:43 -05:00
|
|
|
|
|
|
|
#include "blufi_int.h"
|
|
|
|
|
2017-08-24 02:24:04 -04:00
|
|
|
//#include "esp_wifi.h"
|
2020-08-07 03:02:53 -04:00
|
|
|
#if (BLUFI_INCLUDED == TRUE)
|
2016-12-27 23:02:43 -05:00
|
|
|
|
|
|
|
void btc_blufi_protocol_handler(uint8_t type, uint8_t *data, int len)
|
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
|
|
|
esp_blufi_cb_param_t param;
|
|
|
|
uint8_t *output_data = NULL;
|
|
|
|
int output_len = 0;
|
|
|
|
bool need_free = false;
|
|
|
|
|
|
|
|
switch (BLUFI_GET_TYPE(type)) {
|
|
|
|
case BLUFI_TYPE_CTRL:
|
|
|
|
switch (BLUFI_GET_SUBTYPE(type)) {
|
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_ACK:
|
|
|
|
/* TODO: check sequence */
|
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_SET_SEC_MODE:
|
|
|
|
blufi_env.sec_mode = data[0];
|
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_SET_WIFI_OPMODE:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_SET_WIFI_OPMODE;
|
|
|
|
param.wifi_mode.op_mode = data[0];
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_CONN_TO_AP:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, NULL, 0, NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_DISCONN_FROM_AP:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, NULL, 0, NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_GET_WIFI_STATUS;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, NULL, 0, NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_DEAUTHENTICATE_STA;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, NULL, 0, NULL,NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
2017-01-06 04:40:46 -05:00
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_GET_VERSION: {
|
|
|
|
uint8_t type = BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_REPLY_VERSION);
|
|
|
|
uint8_t data[2];
|
|
|
|
|
|
|
|
data[0] = BTC_BLUFI_GREAT_VER;
|
|
|
|
data[1] = BTC_BLUFI_SUB_VER;
|
|
|
|
btc_blufi_send_encap(type, &data[0], sizeof(data));
|
|
|
|
break;
|
|
|
|
}
|
2017-04-20 23:04:53 -04:00
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_DISCONNECT_BLE:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE;
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, NULL, 0, NULL, NULL);
|
2017-04-20 23:04:53 -04:00
|
|
|
break;
|
2017-12-25 05:04:07 -05:00
|
|
|
case BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_LIST:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_GET_WIFI_LIST;
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, NULL, 0, NULL, NULL);
|
2017-12-25 05:04:07 -05:00
|
|
|
break;
|
2016-12-27 23:02:43 -05:00
|
|
|
default:
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s Unkown Ctrl pkt %02x\n", __func__, type);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA:
|
|
|
|
switch (BLUFI_GET_SUBTYPE(type)) {
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_NEG:
|
|
|
|
if (blufi_env.cbs && blufi_env.cbs->negotiate_data_handler) {
|
|
|
|
blufi_env.cbs->negotiate_data_handler(data, len, &output_data, &output_len, &need_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_data && output_len > 0) {
|
|
|
|
btc_blufi_send_encap(BLUFI_BUILD_TYPE(BLUFI_TYPE_DATA, BLUFI_TYPE_DATA_SUBTYPE_NEG),
|
|
|
|
output_data, output_len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_STA_BSSID;
|
|
|
|
memcpy(param.sta_bssid.bssid, &data[0], 6);
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_STA_SSID:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_STA_SSID;
|
|
|
|
param.sta_ssid.ssid = &data[0];
|
|
|
|
param.sta_ssid.ssid_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_STA_PASSWD;
|
|
|
|
param.sta_passwd.passwd = &data[0];
|
|
|
|
param.sta_passwd.passwd_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_SSID;
|
|
|
|
param.softap_ssid.ssid = &data[0];
|
|
|
|
param.softap_ssid.ssid_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD;
|
|
|
|
param.softap_passwd.passwd = &data[0];
|
|
|
|
param.softap_passwd.passwd_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM;
|
|
|
|
param.softap_max_conn_num.max_conn_num = data[0];
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE;
|
|
|
|
param.softap_auth_mode.auth_mode = data[0];
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL;
|
|
|
|
param.softap_channel.channel = data[0];
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL, NULL);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_USERNAME:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_USERNAME;
|
|
|
|
param.username.name = &data[0];
|
|
|
|
param.username.name_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_CA:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_CA_CERT;
|
|
|
|
param.ca.cert = &data[0];
|
|
|
|
param.ca.cert_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_CLIENT_CERT:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_CLIENT_CERT;
|
|
|
|
param.client_cert.cert = &data[0];
|
|
|
|
param.client_cert.cert_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_SERVER_CERT:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SERVER_CERT;
|
|
|
|
param.client_cert.cert = &data[0];
|
|
|
|
param.client_cert.cert_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_CLIENT_PRIV_KEY:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY;
|
|
|
|
param.client_pkey.pkey = &data[0];
|
|
|
|
param.client_pkey.pkey_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_SERVER_PRIV_KEY:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY;
|
|
|
|
param.client_pkey.pkey = &data[0];
|
|
|
|
param.client_pkey.pkey_len = len;
|
|
|
|
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2018-03-06 03:18:38 -05:00
|
|
|
break;
|
|
|
|
case BLUFI_TYPE_DATA_SUBTYPE_CUSTOM_DATA:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_BLUFI;
|
|
|
|
msg.act = ESP_BLUFI_EVENT_RECV_CUSTOM_DATA;
|
|
|
|
param.custom_data.data = &data[0];
|
|
|
|
param.custom_data.data_len = len;
|
2022-08-09 02:16:30 -04:00
|
|
|
btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), btc_blufi_cb_deep_copy, btc_blufi_cb_deep_free);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
default:
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s Unkown Ctrl pkt %02x\n", __func__, type);
|
2016-12-27 23:02:43 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-20 07:18:05 -04:00
|
|
|
|
2020-08-07 03:02:53 -04:00
|
|
|
#endif ///BLUFI_INCLUDED == TRUE
|