component/bt : move blufi to new architecture btc task

1. move blufi to new architecture
2. delete some log trace
This commit is contained in:
Tian Hao
2016-11-16 22:11:59 +08:00
parent 9691fe9dae
commit aafaebefb4
14 changed files with 414 additions and 277 deletions

View File

@@ -54,7 +54,7 @@ static void btc_deinit_bluetooth(void)
void btc_main_call_handler(btc_msg_t *msg)
{
LOG_ERROR("%s act %d\n", __func__, msg->act);
LOG_DEBUG("%s act %d\n", __func__, msg->act);
switch (msg->act) {
case BTC_MAIN_ACT_INIT:

View File

@@ -23,6 +23,7 @@
#include "btc_gatts.h"
#include "btc_gattc.h"
#include "btc_gap_ble.h"
#include "btc_blufi_prf.h"
static xTaskHandle xBtcTaskHandle = NULL;
static xQueueHandle xBtcQueue = 0;
@@ -38,7 +39,7 @@ static btc_func_t profile_tab[BTC_PID_NUM] = {
[BTC_PID_BT_HID] = {NULL, NULL},
[BTC_PID_SPP] = {NULL, NULL},
[BTC_PID_SPPLIKE] = {NULL, NULL},
[BTC_PID_BLUFI] = {NULL, NULL},
[BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler },
};
/*****************************************************************************

View File

@@ -27,14 +27,21 @@
#include "bta_gatt_api.h"
#include "bta_gatts_int.h"
#include "blufi_prf.h"
#include "btc_blufi_prf.h"
#include "btc_task.h"
#include "btc_manage.h"
#include "blufi_adv.h"
#include "blufi_int.h"
static uint8_t *success_msg = "BLUFI_CONFIG_OK";
static uint8_t *failed_msg = "BLUFI_CONFIG_FAILED";
#define BTC_BLUFI_CB_TO_APP(_event, _param) ((esp_profile_cb_t)btc_profile_cb_get(BTC_PID_BLUFI))(_event, _param)
#define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
void blufi_config_success(void);
void blufi_config_failed(void);
UINT16 esp32_uuid = SVC_BLUFI_UUID;
UINT8 esp32_manu[17] = {0xff,0x20,0x14,0x07,0x22,0x00,0x02,0x5B,0x00,0x33,0x49,0x31,0x30,0x4a,0x30,0x30,0x31};
@@ -99,6 +106,33 @@ static tBLUFI_CB_ENV blufi_cb_env;
static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data);
/*******************************************************************************
**
** Function blufi_create_service
**
** Description Create a Service for the blufi profile
**
** Returns NULL
**
*******************************************************************************/
static void blufi_create_service(void)
{
tBTA_GATTS_IF server_if ;
tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}};
UINT16 num_handle = BLUFI_HDL_NUM;
UINT8 inst = 0x00;
server_if = blufi_cb_env.gatt_if;
blufi_cb_env.inst_id = inst;
if(!blufi_cb_env.enabled)
{
LOG_ERROR("blufi service added error.");
return;
}
BTA_GATTS_CreateService(server_if, &uuid, inst, num_handle, true);
}
/*******************************************************************************
**
** Function blufi_profile_cb
@@ -112,8 +146,6 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
{
tBTA_GATTS_RSP rsp;
tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}};
tBLUFI_INST *p_inst = &blufi_cb_env.blufi_inst;
UINT8 net_event = 0xff;
UINT8 len = 0;
UINT8 *p_rec_data = NULL;
tBTA_GATT_STATUS status;
@@ -169,10 +201,20 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
LOG_DEBUG("\n");
if (p_data->req_data.p_data->write_req.handle == blufi_cb_env.blufi_inst.blufi_hdl) {
btc_msg_t msg;
struct blufi_recv_evt_param recv_data;
memset(&recv_data, 0x00, sizeof(struct blufi_recv_evt_param));
p_rec_data = &p_data->req_data.p_data->write_req.value[0];
if (p_inst && p_inst->blufi_cback)
p_inst->blufi_cback(blufi_cb_env.blufi_inst.app_id, net_event, len, p_rec_data);
recv_data.data_len = p_data->req_data.p_data->write_req.len;
memcpy(recv_data.data, p_rec_data, recv_data.data_len);
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_BLUFI;
msg.act = BTC_BLUFI_CB_ACT_RECV_DATA;
btc_transfer_context(&msg, &recv_data, sizeof(struct blufi_recv_evt_param), NULL);
}
break;
case BTA_GATTS_CONF_EVT:
@@ -180,12 +222,12 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
break;
case BTA_GATTS_CREATE_EVT:
uuid.uu.uuid16 = CHAR_BLUFI_UUID;
blufi_cb_env.clcb.cur_srvc_id = p_data->create.service_id;
blufi_cb_env.cur_srvc_id = p_data->create.service_id;
blufi_cb_env.is_primery = p_data->create.is_primary;
//start the blufi service after created
BTA_GATTS_StartService(p_data->create.service_id, BTA_GATT_TRANSPORT_LE);
//add the frist blufi characteristic --> write characteristic
BTA_GATTS_AddCharacteristic(blufi_cb_env.clcb.cur_srvc_id, &uuid,
BTA_GATTS_AddCharacteristic(blufi_cb_env.cur_srvc_id, &uuid,
(GATT_PERM_WRITE | GATT_PERM_READ),
(GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE | GATT_CHAR_PROP_BIT_NOTIFY));
break;
@@ -196,20 +238,29 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
blufi_cb_env.blufi_inst.blufi_hdl = p_data->add_result.attr_id;
uuid.uu.uuid16 = GATT_UUID_CHAR_CLIENT_CONFIG;
BTA_GATTS_AddCharDescriptor (blufi_cb_env.clcb.cur_srvc_id,
BTA_GATTS_AddCharDescriptor (blufi_cb_env.cur_srvc_id,
(GATT_PERM_WRITE|GATT_PERM_WRITE),
&uuid);
}
break;
case BTA_GATTS_ADD_CHAR_DESCR_EVT:
/* Nothing */
case BTA_GATTS_ADD_CHAR_DESCR_EVT: {
/* call init finish */
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_BLUFI;
msg.act = BTC_BLUFI_CB_ACT_INIT_FINISH;
btc_transfer_context(&msg, NULL, 0, NULL);
break;
}
case BTA_GATTS_CONNECT_EVT:
//set the connection flag to true
blufi_env_clcb_alloc(p_data->conn.conn_id, p_data->conn.remote_bda);
LOG_DEBUG("\ndevice is connected "BT_BD_ADDR_STR", server_if=%d,reason=0x%x,connect_id=%d\n",
LOG_ERROR("\ndevice is connected "BT_BD_ADDR_STR", server_if=%d,reason=0x%x,connect_id=%d\n",
BT_BD_ADDR_HEX(p_data->conn.remote_bda), p_data->conn.server_if,
p_data->conn.reason, p_data->conn.conn_id);
blufi_cb_env.conn_id = p_data->conn.conn_id;
/*return whether the remote device is currently connected*/
int is_connected = BTA_DmGetConnectionState(p_data->conn.remote_bda);
LOG_DEBUG("is_connected=%d\n",is_connected);
@@ -217,15 +268,15 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
break;
case BTA_GATTS_DISCONNECT_EVT:
//set the connection flag to true
blufi_cb_env.clcb.connected = false;
blufi_cb_env.connected = false;
break;
case BTA_GATTS_OPEN_EVT:
break;
case BTA_GATTS_CLOSE_EVT:
if(blufi_cb_env.clcb.connected && (blufi_cb_env.clcb.conn_id == p_data->conn.conn_id))
if(blufi_cb_env.connected && (blufi_cb_env.conn_id == p_data->conn.conn_id))
{
//set the connection channal congested flag to true
blufi_cb_env.clcb.congest = p_data->congest.congested;
blufi_cb_env.congest = p_data->congest.congested;
}
break;
case BTA_GATTS_LISTEN_EVT:
@@ -237,111 +288,7 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
}
}
/*******************************************************************************
**
** Function blufi_create_service
**
** Description Create a Service for the blufi profile
**
** Returns NULL
**
*******************************************************************************/
void blufi_create_service(void)
{
tBTA_GATTS_IF server_if ;
tBT_UUID uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}};
UINT16 num_handle = BLUFI_HDL_NUM;
UINT8 inst = 0x00;
server_if = blufi_cb_env.gatt_if;
blufi_cb_env.inst_id = inst;
if(!blufi_cb_env.enabled)
{
LOG_ERROR("blufi service added error.");
return;
}
BTA_GATTS_CreateService(server_if, &uuid, inst, num_handle, true);
}
/*******************************************************************************
**
** Function blufi_env_clcb_alloc
**
** Description The function allocates a GATT profile connection link control block
**
** Returns NULL if not found. Otherwise pointer to the connection link block.
**
*******************************************************************************/
tBLUFI_CLCB *blufi_env_clcb_alloc (UINT16 conn_id, BD_ADDR remote_bda)
{
tBLUFI_CLCB *p_clcb = NULL;
p_clcb = &blufi_cb_env.clcb;
if(!p_clcb->in_use)
{
p_clcb->in_use = TRUE;
p_clcb->conn_id = conn_id;
p_clcb->connected = TRUE;
memcpy(p_clcb->remote_bda, remote_bda, BD_ADDR_LEN);
LOG_ERROR("p_clcb->conn_id = %x\n", conn_id);
}
return p_clcb;
}
/*******************************************************************************
**
** Function blufi_env_find_conn_id_by_bd_adddr
**
** Description The function searches all LCB with macthing bd address
**
** Returns total number of clcb found.
**
*******************************************************************************/
/*
UINT16 blufi_env_find_conn_id_by_bd_adddr(BD_ADDR remote_bda)
{
UINT8 i_clcb;
tBLUFI_CLCB *p_clcb = NULL;
for(i_clcb = 0, p_clcb = &blufi_cb_env.clcb; i_clcb < BLUFIT_MAX_APPS; i_clcb++, p_clcb++)
{
if(p_clcb->in_use && p_clcb->connected && memcmp(p_clcb->remote_bda, remote_bda, BD_ADDR_LEN) == 0)
{
return p_clcb->conn_id;
}
}
return GATT_INVALID_CONN_ID;
}
*/
/*******************************************************************************
**
** Function blufi_env_clcb_dealloc
**
** Description The function deallocates a GATT profile connection link control block
**
** Returns True the deallocation is successful
**
*******************************************************************************/
BOOLEAN blufi_env_clcb_dealloc(UINT16 conn_id)
{
// UINT8 i_clcb = 0;
// tBLUFI_CLCB *p_clcb = NULL;
return FALSE;
}
/*******************************************************************************
**
** Function blufi_init
**
** Description Initializa the GATT Service for blufi profiles.
**
*******************************************************************************/
tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back)
static tGATT_STATUS btc_blufi_profile_init(void)
{
tBT_UUID app_uuid = {LEN_UUID_16, {SVC_BLUFI_UUID}};
@@ -356,12 +303,6 @@ tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back)
memset(&blufi_cb_env,0,sizeof(tBLUFI_CB_ENV));
}
if(call_back != NULL)
{
blufi_cb_env.blufi_inst.blufi_cback = call_back;
}
/* register the blufi profile to the BTA_GATTS module*/
BTA_GATTS_AppRegister(&app_uuid, blufi_profile_cb);
@@ -369,14 +310,14 @@ tGATT_STATUS blufi_profile_init (tBLUFI_CBACK *call_back)
return GATT_SUCCESS;
}
void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len)
static void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len)
{
BOOLEAN conn_status = blufi_cb_env.clcb.connected;
UINT16 conn_id = blufi_cb_env.clcb.conn_id;
BOOLEAN conn_status = blufi_cb_env.connected;
UINT16 conn_id = blufi_cb_env.conn_id;
UINT16 attr_id = blufi_cb_env.blufi_inst.blufi_hdl;
//notify rsp==false; indicate rsp==true.
BOOLEAN rsp = false;
if(!conn_status && blufi_cb_env.clcb.congest)
if(!conn_status && blufi_cb_env.congest)
{
LOG_ERROR("the conneciton for blufi profile has been loss");
return;
@@ -386,14 +327,61 @@ void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len)
blufi_msg, rsp);
}
void blufi_config_success(void)
static void btc_blufi_config_success(void)
{
uint8_t *success_msg = "BLUFI_CONFIG_OK";
LOG_DEBUG("config success\n");
blufi_msg_notify(success_msg, strlen(success_msg));
}
void blufi_config_failed(void)
static void btc_blufi_config_failed(void)
{
uint8_t *failed_msg = "BLUFI_CONFIG_FAILED";
LOG_DEBUG("config faield\n");
blufi_msg_notify(failed_msg, strlen(failed_msg));
}
void btc_blufi_cb_handler(btc_msg_t *msg)
{
esp_blufi_cb_param_t param;
switch (msg->act) {
case BTC_BLUFI_CB_ACT_INIT_FINISH:
param.init_finish.state = ESP_BLUFI_INIT_OK;
BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_INIT_FINISH, &param);
break;
case BTC_BLUFI_CB_ACT_DEINIT_FINISH:
/* TODO: but now nothing */
break;
case BTC_BLUFI_CB_ACT_RECV_DATA:
memcpy(&param.recv_data, msg->arg, sizeof(struct blufi_recv_evt_param));
BTC_BLUFI_CB_TO_APP(ESP_BLUFI_EVENT_RECV_DATA, &param);
break;
default:
LOG_ERROR("%s UNKNOWN %d\n", msg->act);
break;
}
}
void btc_blufi_call_handler(btc_msg_t *msg)
{
btc_blufi_args_t *arg = (btc_blufi_args_t *)msg->arg;
switch (msg->act) {
case BTC_BLUFI_ACT_INIT:
btc_blufi_profile_init();
break;
case BTC_BLUFI_ACT_DEINIT:
/* TODO: but now nothing */
break;
case BTC_BLUFI_ACT_SEND_CFG_STATE:
if (arg->cfg_state.state == ESP_BLUFI_CONFIG_OK) {
btc_blufi_config_success();
} else {
btc_blufi_config_failed();
}
break;
default:
LOG_ERROR("%s UNKNOWN %d\n", msg->act);
break;
}
}

View File

@@ -0,0 +1,40 @@
#ifndef __BLUFI_INT_H__
#define __BLUFI_INT_H__
//define the blufi serivce uuid
#define SVC_BLUFI_UUID 0xFFFF
//define the blufi Char uuid
#define CHAR_BLUFI_UUID 0xFF01
#define BLUFI_HDL_NUM 4
#define BLUFI_VAL_MAX_LEN (128)
#define BLUFI_MAX_STRING_DATA 128
typedef struct
{
UINT8 app_id;
UINT16 blufi_hdl;
}tBLUFI_INST;
/* service engine control block */
typedef struct
{
BOOLEAN enabled;
BOOLEAN is_primery;
UINT8 inst_id;
tGATT_IF gatt_if;
tBLUFI_INST blufi_inst;
BOOLEAN in_use;
BOOLEAN congest;
UINT16 conn_id;
BOOLEAN connected;
BD_ADDR remote_bda;
UINT32 trans_id;
UINT8 cur_srvc_id;
} tBLUFI_CB_ENV;
#endif /* __BLUFI_INT_H__ */

View File

@@ -1,76 +0,0 @@
#include "bt_target.h"
#include "gatt_api.h"
#include "gattdefs.h"
//define the blufi serivce uuid
#define SVC_BLUFI_UUID 0xFFFF
//define the blufi Char uuid
#define CHAR_BLUFI_UUID 0xFF01
#define BLUFI_HDL_NUM 4
#define BLUFI_VAL_MAX_LEN (20)
#define BLUFI_MAX_STRING_DATA 33
typedef void (tBLUFI_CBACK)(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *data);
typedef enum
{
RECV_ACT_PASSWD = 0,
RECV_ACT_SSID,
RECV_ACT_MAX
} RECV_ACTION;
typedef struct
{
BD_ADDR remote_bda;
BOOLEAN need_rsp;
UINT16 clt_cfg;
} tBLUFI_WRITE_DATA;
typedef struct
{
BOOLEAN in_use;
BOOLEAN congest;
UINT16 conn_id;
BOOLEAN connected;
BD_ADDR remote_bda;
UINT32 trans_id;
UINT8 cur_srvc_id;
} tBLUFI_CLCB;
typedef struct
{
UINT8 app_id;
UINT16 blufi_hdl;
tBLUFI_CBACK* blufi_cback;
}tBLUFI_INST;
/* service engine control block */
typedef struct
{
BOOLEAN enabled;
BOOLEAN is_primery;
UINT8 inst_id;
tGATT_IF gatt_if;
tBLUFI_CLCB clcb; /* connection link*/
tBLUFI_INST blufi_inst;
} tBLUFI_CB_ENV;
void blufi_create_service(void);
tBLUFI_CLCB *blufi_env_clcb_alloc(UINT16 conn_id, BD_ADDR bda);
UINT16 blufi_env_find_conn_id_by_bd_adddr(BD_ADDR bda);
BOOLEAN blufi_env_clcb_dealloc(UINT16 conn_id);
tGATT_STATUS blufi_profile_init(tBLUFI_CBACK *call_back);
void blufi_msg_notify(UINT8 *blufi_msg, UINT8 len);

View File

@@ -0,0 +1,38 @@
#ifndef __BTC_BLUFI_PRF_H__
#define __BTC_BLUFI_PRF_H__
#include "bt_target.h"
#include "btc_task.h"
#include "esp_blufi_api.h"
typedef enum {
BTC_BLUFI_ACT_INIT = 0,
BTC_BLUFI_ACT_DEINIT,
BTC_BLUFI_ACT_SEND_CFG_STATE,
} btc_blufi_act_t;
typedef enum {
BTC_BLUFI_CB_ACT_INIT_FINISH = 0,
BTC_BLUFI_CB_ACT_DEINIT_FINISH,
BTC_BLUFI_CB_ACT_RECV_DATA,
} btc_blufi_cb_act_t;
typedef union {
#if 0
//BTC_BLUFI_ACT_INIT = 0,
struct blufi_init_param {
} init;
//BTC_BLUFI_ACT_DEINIT,
struct blufi_deinit_param {
} deinit;
#endif
//BTC_BLUFI_ACT_SEND_CFG_STATE,
struct blufi_send_cfg_state_pram {
esp_blufi_config_state_t state;
} cfg_state;
} btc_blufi_args_t;
void btc_blufi_cb_handler(btc_msg_t *msg);
void btc_blufi_call_handler(btc_msg_t *msg);
#endif /* __BTC_BLUFI_PRF_H__ */