diff --git a/components/bt/common/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c index e439056c078..bc2bd836d02 100644 --- a/components/bt/common/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -210,9 +210,6 @@ static void btc_thread_handler(void *arg) break; } - if (msg->arg) { - osi_free(msg->arg); - } osi_free(msg); } @@ -247,25 +244,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); @@ -273,9 +263,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); } @@ -285,17 +272,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); diff --git a/components/bt/common/btc/include/btc/btc_task.h b/components/bt/common/btc/include/btc/btc_task.h index 2ee87303543..ef286c32363 100644 --- a/components/bt/common/btc/include/btc/btc_task.h +++ b/components/bt/common/btc/include/btc/btc_task.h @@ -20,7 +20,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 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); diff --git a/components/bt/host/bluedroid/btc/core/btc_dm.c b/components/bt/host/bluedroid/btc/core/btc_dm.c index 9c313f25c84..e8ea0f47331 100644 --- a/components/bt/host/bluedroid/btc/core/btc_dm.c +++ b/components/bt/host/bluedroid/btc/core/btc_dm.c @@ -395,15 +395,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, ¶m, sizeof(esp_bt_gap_cb_param_t)); - ret = btc_inter_profile_call(&msg, ¶m); + 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__); @@ -418,14 +426,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, ¶m, sizeof(esp_bt_gap_cb_param_t)); - ret = btc_inter_profile_call(&msg, ¶m); + 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__); @@ -445,14 +461,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, ¶m, sizeof(esp_bt_gap_cb_param_t)); - ret = btc_inter_profile_call(&msg, ¶m); + 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__); @@ -465,14 +489,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, ¶m, sizeof(esp_bt_gap_cb_param_t)); - ret = btc_inter_profile_call(&msg, ¶m); + 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__); @@ -485,13 +517,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; + 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_REQ_EVT; memcpy(param.key_req.bda, p_key_req->bd_addr, ESP_BD_ADDR_LEN); + memcpy(msg->arg, ¶m, sizeof(esp_bt_gap_cb_param_t)); - ret = btc_inter_profile_call(&msg, ¶m); + 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__); @@ -515,12 +555,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; + + 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, ¶m, sizeof(esp_bt_gap_cb_param_t)); - ret = btc_inter_profile_call(&msg, ¶m); + 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__); @@ -534,14 +582,22 @@ static void btc_dm_pm_mode_chg_evt(tBTA_DM_MODE_CHG *p_mode_chg) { 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_MODE_CHG_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_MODE_CHG_EVT; memcpy(param.mode_chg.bda, p_mode_chg->bd_addr, ESP_BD_ADDR_LEN); param.mode_chg.mode = p_mode_chg->mode; + memcpy(msg->arg, ¶m, sizeof(esp_bt_gap_cb_param_t)); - ret = btc_inter_profile_call(&msg, ¶m); + 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__); @@ -626,11 +682,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); @@ -713,7 +776,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; @@ -738,7 +801,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; @@ -753,7 +816,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) { @@ -820,32 +883,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(¶m.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; @@ -868,7 +931,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(¶m.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; @@ -880,7 +943,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; @@ -905,11 +968,13 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg) } if (rsp_app) { - ret = btc_inter_profile_call(&ble_msg, ¶m); + memcpy(ble_msg->arg, ¶m, 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); } diff --git a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_bt.c b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_bt.c index 174800e6d52..aeb1e8c217a 100644 --- a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_bt.c +++ b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_bt.c @@ -899,7 +899,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: { @@ -907,23 +907,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:{ @@ -1026,15 +1026,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:{