bt:Modify the member variable *arg in struct btc_msg to arg[0]

This commit is contained in:
xiongweichao 2022-08-19 16:32:36 +08:00
parent 0b0f2eeb01
commit b5a959f11d
4 changed files with 115 additions and 74 deletions

View File

@ -187,9 +187,6 @@ static void btc_thread_handler(void *arg)
break;
}
if (msg->arg) {
osi_free(msg->arg);
}
osi_free(msg);
}
@ -224,25 +221,18 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg
BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t));
lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + arg_len);
if (lmsg == NULL) {
return BT_STATUS_NOMEM;
}
memcpy(lmsg, msg, sizeof(btc_msg_t));
if (arg) {
lmsg->arg = (void *)osi_malloc(arg_len);
if (lmsg->arg == NULL) {
osi_free(lmsg);
return BT_STATUS_NOMEM;
}
memset(lmsg->arg, 0x00, arg_len); //important, avoid arg which have no length
memcpy(lmsg->arg, arg, arg_len);
if (copy_func) {
copy_func(lmsg, lmsg->arg, arg);
}
} else {
lmsg->arg = NULL;
}
ret = btc_task_post(lmsg, OSI_THREAD_MAX_TIMEOUT);
@ -250,9 +240,6 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg
if (copy_func && free_func) {
free_func(lmsg);
}
if (lmsg->arg) {
osi_free(lmsg->arg);
}
osi_free(lmsg);
}
@ -262,17 +249,15 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg
/**
* transfer an message to another module in tha same task.
* @param msg message
* @param arg paramter
* @return BT_STATUS_SUCCESS: success
* others: fail
*/
bt_status_t btc_inter_profile_call(btc_msg_t *msg, void *arg)
bt_status_t btc_inter_profile_call(btc_msg_t *msg)
{
if (msg == NULL) {
return BT_STATUS_PARM_INVALID;
}
msg->arg = arg;
switch (msg->sig) {
case BTC_SIG_API_CALL:
profile_tab[msg->pid].btc_call(msg);

View File

@ -28,7 +28,7 @@ typedef struct btc_msg {
uint8_t aid; //application id
uint8_t pid; //profile id
uint8_t act; //profile action, defined in seprerate header files
void *arg; //param for btc function or function param
uint8_t arg[0]; //param for btc function or function param
} btc_msg_t;
typedef struct btc_adv_packet {
@ -116,11 +116,10 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg
/**
* transfer an message to another module in tha same task.
* @param msg message
* @param arg paramter
* @return BT_STATUS_SUCCESS: success
* others: fail
*/
bt_status_t btc_inter_profile_call(btc_msg_t *msg, void *arg);
bt_status_t btc_inter_profile_call(btc_msg_t *msg);
bt_status_t btc_init(void);
void btc_deinit(void);

View File

@ -403,15 +403,23 @@ static void btc_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
#if (BTC_GAP_BT_INCLUDED == TRUE)
esp_bt_gap_cb_param_t param;
bt_status_t ret;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_AUTH_CMPL_EVT;
btc_msg_t *msg;
msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
if (msg == NULL) {
BTC_TRACE_ERROR("%s malloc fail", __func__);
return;
}
msg->sig = BTC_SIG_API_CB;
msg->pid = BTC_PID_GAP_BT;
msg->act = BTC_GAP_BT_AUTH_CMPL_EVT;
param.auth_cmpl.stat = status;
memcpy(param.auth_cmpl.bda, p_auth_cmpl->bd_addr, ESP_BD_ADDR_LEN);
memcpy(param.auth_cmpl.device_name, p_auth_cmpl->bd_name, ESP_BT_GAP_MAX_BDNAME_LEN + 1);
memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
ret = btc_inter_profile_call(&msg, &param);
ret = btc_inter_profile_call(msg);
osi_free(msg);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
@ -426,14 +434,22 @@ static void btc_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
#if (BTC_GAP_BT_INCLUDED == TRUE)
esp_bt_gap_cb_param_t param;
bt_status_t ret;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_PIN_REQ_EVT;
btc_msg_t *msg;
msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
if (msg == NULL) {
BTC_TRACE_ERROR("%s malloc fail", __func__);
return;
}
msg->sig = BTC_SIG_API_CB;
msg->pid = BTC_PID_GAP_BT;
msg->act = BTC_GAP_BT_PIN_REQ_EVT;
param.pin_req.min_16_digit = p_pin_req->min_16_digit;
memcpy(param.pin_req.bda, p_pin_req->bd_addr, ESP_BD_ADDR_LEN);
memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
ret = btc_inter_profile_call(&msg, &param);
ret = btc_inter_profile_call(msg);
osi_free(msg);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
@ -453,14 +469,22 @@ static void btc_dm_sp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_cfm_req)
esp_bt_gap_cb_param_t param;
bt_status_t ret;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_CFM_REQ_EVT;
btc_msg_t *msg;
msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
if (msg == NULL) {
BTC_TRACE_ERROR("%s malloc fail", __func__);
return;
}
msg->sig = BTC_SIG_API_CB;
msg->pid = BTC_PID_GAP_BT;
msg->act = BTC_GAP_BT_CFM_REQ_EVT;
param.cfm_req.num_val = p_cfm_req->num_val;
memcpy(param.cfm_req.bda, p_cfm_req->bd_addr, ESP_BD_ADDR_LEN);
memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
ret = btc_inter_profile_call(&msg, &param);
ret = btc_inter_profile_call(msg);
osi_free(msg);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
@ -473,14 +497,22 @@ static void btc_dm_sp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_key_notif)
#if (BTC_GAP_BT_INCLUDED == TRUE)
esp_bt_gap_cb_param_t param;
bt_status_t ret;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_KEY_NOTIF_EVT;
btc_msg_t *msg;
msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
if (msg == NULL) {
BTC_TRACE_ERROR("%s malloc fail", __func__);
return;
}
msg->sig = BTC_SIG_API_CB;
msg->pid = BTC_PID_GAP_BT;
msg->act = BTC_GAP_BT_KEY_NOTIF_EVT;
param.key_notif.passkey = p_key_notif->passkey;
memcpy(param.key_notif.bda, p_key_notif->bd_addr, ESP_BD_ADDR_LEN);
memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
ret = btc_inter_profile_call(&msg, &param);
ret = btc_inter_profile_call(msg);
osi_free(msg);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
@ -493,13 +525,21 @@ static void btc_dm_sp_key_req_evt(tBTA_DM_SP_KEY_REQ *p_key_req)
#if (BTC_GAP_BT_INCLUDED == TRUE)
esp_bt_gap_cb_param_t param;
bt_status_t ret;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_KEY_REQ_EVT;
memcpy(param.key_req.bda, p_key_req->bd_addr, ESP_BD_ADDR_LEN);
btc_msg_t *msg;
ret = btc_inter_profile_call(&msg, &param);
msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
if (msg == NULL) {
BTC_TRACE_ERROR("%s malloc fail", __func__);
return;
}
msg->sig = BTC_SIG_API_CB;
msg->pid = BTC_PID_GAP_BT;
msg->act = BTC_GAP_BT_KEY_REQ_EVT;
memcpy(param.key_req.bda, p_key_req->bd_addr, ESP_BD_ADDR_LEN);
memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
ret = btc_inter_profile_call(msg);
osi_free(msg);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
@ -523,12 +563,20 @@ static void btc_dm_dev_unpaired_evt(tBTA_DM_LINK_DOWN *p_link_down)
#if (BTC_GAP_BT_INCLUDED == TRUE)
bt_status_t ret;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_REMOVE_BOND_DEV_COMPLETE_EVT;
btc_msg_t *msg;
ret = btc_inter_profile_call(&msg, &param);
msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
if (msg == NULL) {
BTC_TRACE_ERROR("%s malloc fail", __func__);
return;
}
msg->sig = BTC_SIG_API_CB;
msg->pid = BTC_PID_GAP_BT;
msg->act = BTC_GAP_BT_REMOVE_BOND_DEV_COMPLETE_EVT;
memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
ret = btc_inter_profile_call(msg);
osi_free(msg);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
@ -613,11 +661,18 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
btc_dm_sec_args_t *arg = (btc_dm_sec_args_t *)(msg->arg);
tBTA_DM_SEC *p_data = &(arg->sec);
esp_ble_gap_cb_param_t param = {0};
btc_msg_t ble_msg = {0};
btc_msg_t *ble_msg;
bool rsp_app = false;
bt_status_t ret = BT_STATUS_SUCCESS;
ble_msg.sig = BTC_SIG_API_CB;
ble_msg.pid = BTC_PID_GAP_BLE;
ble_msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_ble_gap_cb_param_t));
if (ble_msg == NULL) {
BTC_TRACE_ERROR("%s malloc fail", __func__);
btc_dm_sec_arg_deep_free(msg);
return;
}
ble_msg->sig = BTC_SIG_API_CB;
ble_msg->pid = BTC_PID_GAP_BLE;
// tBTA_SERVICE_MASK service_mask;
BTC_TRACE_DEBUG("btc_dm_upstreams_cback ev: %d\n", msg->act);
@ -700,7 +755,7 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
btc_storage_remove_ble_dev_auth_mode(&bd_addr, false);
param.remove_bond_dev_cmpl.status = btc_storage_remove_ble_bonding_keys(&bd_addr);
}
ble_msg.act = ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT;
ble_msg->act = ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT;
memcpy(param.remove_bond_dev_cmpl.bd_addr, p_data->link_down.bd_addr, sizeof(BD_ADDR));
#endif /* #if (SMP_INCLUDED == TRUE) */
break;
@ -725,7 +780,7 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
#if ((BLE_INCLUDED == TRUE) && (SMP_INCLUDED == TRUE))
case BTA_DM_BLE_AUTH_CMPL_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_AUTH_CMPL_EVT;
ble_msg->act = ESP_GAP_BLE_AUTH_CMPL_EVT;
param.ble_security.auth_cmpl.addr_type = p_data->auth_cmpl.addr_type;
param.ble_security.auth_cmpl.dev_type = p_data->auth_cmpl.dev_type;
param.ble_security.auth_cmpl.key_type = p_data->auth_cmpl.key_type;
@ -740,7 +795,7 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
}
case BTA_DM_BLE_KEY_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_KEY_EVT;
ble_msg->act = ESP_GAP_BLE_KEY_EVT;
param.ble_security.ble_key.key_type = p_data->ble_key.key_type;
memcpy(param.ble_security.ble_key.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN);
switch (p_data->ble_key.key_type) {
@ -807,32 +862,32 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
}
case BTA_DM_BLE_SEC_REQ_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_SEC_REQ_EVT;
ble_msg->act = ESP_GAP_BLE_SEC_REQ_EVT;
memcpy(param.ble_security.ble_req.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
break;
}
case BTA_DM_BLE_PASSKEY_NOTIF_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_PASSKEY_NOTIF_EVT;
ble_msg->act = ESP_GAP_BLE_PASSKEY_NOTIF_EVT;
param.ble_security.key_notif.passkey = p_data->key_notif.passkey;
memcpy(param.ble_security.key_notif.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
break;
}
case BTA_DM_BLE_PASSKEY_REQ_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_PASSKEY_REQ_EVT;
ble_msg->act = ESP_GAP_BLE_PASSKEY_REQ_EVT;
memcpy(param.ble_security.ble_req.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
break;
}
case BTA_DM_BLE_OOB_REQ_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_OOB_REQ_EVT;
ble_msg->act = ESP_GAP_BLE_OOB_REQ_EVT;
memcpy(param.ble_security.ble_req.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
break;
}
case BTA_DM_BLE_LOCAL_IR_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_LOCAL_IR_EVT;
ble_msg->act = ESP_GAP_BLE_LOCAL_IR_EVT;
memcpy(&param.ble_security.ble_id_keys, &p_data->ble_id_keys, sizeof(tBTA_BLE_LOCAL_ID_KEYS));
BTC_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
btc_dm_cb.ble_local_key_cb.is_id_keys_rcvd = TRUE;
@ -855,7 +910,7 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
}
case BTA_DM_BLE_LOCAL_ER_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_LOCAL_ER_EVT;
ble_msg->act = ESP_GAP_BLE_LOCAL_ER_EVT;
memcpy(&param.ble_security.ble_id_keys, &p_data->ble_id_keys, sizeof(tBTA_BLE_LOCAL_ID_KEYS));
BTC_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
btc_dm_cb.ble_local_key_cb.is_er_rcvd = TRUE;
@ -867,7 +922,7 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
}
case BTA_DM_BLE_NC_REQ_EVT: {
rsp_app = true;
ble_msg.act = ESP_GAP_BLE_NC_REQ_EVT;
ble_msg->act = ESP_GAP_BLE_NC_REQ_EVT;
memcpy(param.ble_security.key_notif.bd_addr, p_data->key_notif.bd_addr, BD_ADDR_LEN);
param.ble_security.key_notif.passkey = p_data->key_notif.passkey;
break;
@ -885,11 +940,13 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
}
if (rsp_app) {
ret = btc_inter_profile_call(&ble_msg, &param);
memcpy(ble_msg->arg, &param, sizeof(esp_ble_gap_cb_param_t));
ret = btc_inter_profile_call(ble_msg);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
}
}
osi_free(ble_msg);
btc_dm_sec_arg_deep_free(msg);
}

View File

@ -907,7 +907,7 @@ void btc_gap_bt_call_handler(btc_msg_t *msg)
break;
}
case BTC_GAP_BT_ACT_START_DISCOVERY: {
btc_gap_bt_start_discovery(msg->arg);
btc_gap_bt_start_discovery(arg);
break;
}
case BTC_GAP_BT_ACT_CANCEL_DISCOVERY: {
@ -915,23 +915,23 @@ void btc_gap_bt_call_handler(btc_msg_t *msg)
break;
}
case BTC_GAP_BT_ACT_GET_REMOTE_SERVICES: {
btc_gap_bt_get_remote_services(msg->arg);
btc_gap_bt_get_remote_services((bt_bdaddr_t *)msg->arg);
break;
}
case BTC_GAP_BT_ACT_GET_REMOTE_SERVICE_RECORD: {
btc_gap_bt_get_remote_service_record(msg->arg);
btc_gap_bt_get_remote_service_record(arg);
break;
}
case BTC_GAP_BT_ACT_SET_COD: {
btc_gap_bt_set_cod(msg->arg);
btc_gap_bt_set_cod(arg);
break;
}
case BTC_GAP_BT_ACT_READ_RSSI_DELTA: {
btc_gap_bt_read_rssi_delta(msg->arg);
btc_gap_bt_read_rssi_delta(arg);
break;
}
case BTC_GAP_BT_ACT_REMOVE_BOND_DEVICE:{
btc_gap_bt_remove_bond_device(msg->arg);
btc_gap_bt_remove_bond_device(arg);
break;
}
case BTC_GAP_BT_ACT_SET_PIN_TYPE:{
@ -1031,15 +1031,15 @@ void btc_gap_bt_cb_handler(btc_msg_t *msg)
{
switch (msg->act) {
case BTC_GAP_BT_SEARCH_DEVICES_EVT: {
btc_gap_bt_search_devices_evt(msg->arg);
btc_gap_bt_search_devices_evt((tBTA_DM_SEARCH_PARAM *)msg->arg);
break;
}
case BTC_GAP_BT_SEARCH_SERVICES_EVT: {
btc_gap_bt_search_services(msg->arg);
btc_gap_bt_search_services((char *)msg->arg);
break;
}
case BTC_GAP_BT_SEARCH_SERVICE_RECORD_EVT: {
btc_gap_bt_search_service_record(msg->arg);
btc_gap_bt_search_service_record((char *)msg->arg);
break;
}
case BTC_GAP_BT_READ_RSSI_DELTA_EVT:{