From 9ec0fea8e18020890cb5d2fbe3325135247ec637 Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Wed, 23 Nov 2016 13:45:21 +0800 Subject: [PATCH] component/bt : gatt arg use union 1. union is 28 bytes for each btc task msg arg 2. as gap, individual arg(not use union) is the best mem occupy. --- components/bt/bluedroid/api/esp_gatts_api.c | 64 +++++------ .../btc/profile/std/gatt/btc_gatts.c | 107 +++++++++++++----- .../btc/profile/std/include/btc_gatts.h | 93 +++++++++++---- 3 files changed, 184 insertions(+), 80 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gatts_api.c b/components/bt/bluedroid/api/esp_gatts_api.c index 68b7453417..fe9ead0c47 100644 --- a/components/bt/bluedroid/api/esp_gatts_api.c +++ b/components/bt/bluedroid/api/esp_gatts_api.c @@ -36,7 +36,7 @@ esp_err_t esp_ble_gatts_app_register(uint16_t app_id) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_APP_REGISTER; - arg.app_uuid = app_id; + arg.app_reg.app_id = app_id; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -50,7 +50,7 @@ esp_err_t esp_ble_gatts_app_unregister(esp_gatt_if_t gatt_if) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_APP_UNREGISTER; - arg.gatt_if = gatt_if; + arg.app_unreg.gatt_if = gatt_if; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -64,9 +64,9 @@ esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatt_if, msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_CREATE_SERVICE; - arg.gatt_if = gatt_if; - arg.num_handle = num_handle; - memcpy(&arg.service_id, service_id, sizeof(esp_gatt_srvc_id_t)); + arg.create_srvc.gatt_if = gatt_if; + arg.create_srvc.num_handle = num_handle; + memcpy(&arg.create_srvc.service_id, service_id, sizeof(esp_gatt_srvc_id_t)); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -80,8 +80,8 @@ esp_err_t esp_ble_gatts_add_include_service(uint16_t service_handle, uint16_t in msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_ADD_INCLUDE_SERVICE; - arg.service_handle = service_handle; - arg.included_service_handle = included_service_handle; + arg.add_incl_srvc.service_handle = service_handle; + arg.add_incl_srvc.included_service_handle = included_service_handle; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -96,10 +96,10 @@ esp_err_t esp_ble_gatts_add_char(uint16_t service_handle, esp_bt_uuid_t *char_ msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_ADD_CHAR; - arg.service_handle = service_handle; - arg.perm = perm; - arg.property = property; - memcpy(&arg.uuid, char_uuid, sizeof(esp_bt_uuid_t)); + arg.add_char.service_handle = service_handle; + arg.add_char.perm = perm; + arg.add_char.property = property; + memcpy(&arg.add_char.char_uuid, char_uuid, sizeof(esp_bt_uuid_t)); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -115,9 +115,9 @@ esp_err_t esp_ble_gatts_add_char_descr (uint16_t service_handle, msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_ADD_CHAR_DESCR; - arg.service_handle = service_handle; - arg.perm = perm; - memcpy(&arg.descr_uuid, descr_uuid, sizeof(esp_bt_uuid_t)); + arg.add_descr.service_handle = service_handle; + arg.add_descr.perm = perm; + memcpy(&arg.add_descr.descr_uuid, descr_uuid, sizeof(esp_bt_uuid_t)); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -130,7 +130,7 @@ esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_DELETE_SERVICE; - arg.service_handle = service_handle; + arg.delete_srvc.service_handle = service_handle; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -143,7 +143,7 @@ esp_err_t esp_ble_gatts_start_service(uint16_t service_handle) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_START_SERVICE; - arg.service_handle = service_handle; + arg.start_srvc.service_handle = service_handle; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -156,7 +156,7 @@ esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_STOP_SERVICE; - arg.service_handle = service_handle; + arg.stop_srvc.service_handle = service_handle; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -171,13 +171,13 @@ esp_err_t esp_ble_gatts_send_indicate(uint16_t conn_id, uint16_t attr_handle, msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_SEND_INDICATE; - arg.conn_id = conn_id; - arg.attr_handle = attr_handle; - arg.value_len = value_len; - arg.need_confirm = need_confirm; - memcpy(&arg.value, value, value_len); + arg.send_ind.conn_id = conn_id; + arg.send_ind.attr_handle = attr_handle; + arg.send_ind.need_confirm = need_confirm; + arg.send_ind.value_len = value_len; + arg.send_ind.value = value; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_gatts_send_response(uint16_t conn_id, uint32_t trans_id, @@ -189,12 +189,12 @@ esp_err_t esp_ble_gatts_send_response(uint16_t conn_id, uint32_t trans_id, msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_SEND_RESPONSE; - arg.conn_id = conn_id; - arg.trans_id = trans_id; - arg.status = status; - memcpy(&arg.rsp, rsp, sizeof(esp_gatt_rsp_t)); + arg.send_rsp.conn_id = conn_id; + arg.send_rsp.trans_id = trans_id; + arg.send_rsp.status = status; + arg.send_rsp.rsp = rsp; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatt_if, esp_bd_addr_t remote_bda, bool is_direct) @@ -205,9 +205,9 @@ esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatt_if, esp_bd_addr_t remote_bda, bo msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_OPEN; - arg.gatt_if = gatt_if; - arg.is_direct = is_direct; - memcpy(&arg.remote_bda, remote_bda, sizeof(esp_bd_addr_t)); + arg.open.gatt_if = gatt_if; + arg.open.is_direct = is_direct; + memcpy(&arg.open.remote_bda, remote_bda, sizeof(esp_bd_addr_t)); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -220,7 +220,7 @@ esp_err_t esp_ble_gatts_close(uint16_t conn_id) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTS; msg.act = BTC_GATTS_ACT_CLOSE; - arg.conn_id = conn_id; + arg.close.conn_id = conn_id; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c index f28ab8faea..99a2624fc1 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c @@ -28,6 +28,62 @@ #define A2C_GATTS_EVT(_bta_event) (_bta_event) //BTA TO BTC EVT #define C2A_GATTS_EVT(_btc_event) (_btc_event) //BTC TO BTA EVT +void btc_gatts_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_gatts_args_t *dst = (btc_ble_gatts_args_t*) p_dest; + btc_ble_gatts_args_t *src = (btc_ble_gatts_args_t *)p_src; + + switch (msg->act) { + case BTC_GATTS_ACT_SEND_INDICATE: { + dst->send_ind.value = (uint8_t *)GKI_getbuf(src->send_ind.value_len); + if (dst->send_ind.value) { + memcpy(dst->send_ind.value, src->send_ind.value, src->send_ind.value_len); + } else { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + break; + } + case BTC_GATTS_ACT_SEND_RESPONSE: { + dst->send_rsp.rsp = (esp_gatt_rsp_t *)GKI_getbuf(sizeof(esp_gatt_rsp_t)); + if (dst->send_rsp.rsp) { + memcpy(dst->send_rsp.rsp, src->send_rsp.rsp, sizeof(esp_gatt_rsp_t)); + } else { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + break; + } + default: + LOG_DEBUG("%s Unhandled deep copy %d\n", __func__, msg->act); + break; + } + +} + +void btc_gatts_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_gatts_args_t *arg = (btc_ble_gatts_args_t*)msg->arg; + + switch (msg->act) { + case BTC_GATTS_ACT_SEND_INDICATE: { + if (arg->send_ind.value) { + GKI_freebuf(arg->send_ind.value); + } + break; + } + case BTC_GATTS_ACT_SEND_RESPONSE: { + if (arg->send_rsp.rsp) { + GKI_freebuf(arg->send_rsp.rsp); + } + break; + } + default: + LOG_DEBUG("%s Unhandled deep free %d\n", __func__, msg->act); + break; + } + +} + + static void btc_gatts_cb_param_copy_req(btc_msg_t *msg, void *p_dest, void *p_src) { uint16_t event = msg->act; @@ -107,61 +163,61 @@ void btc_gatts_call_handler(btc_msg_t *msg) tBT_UUID uuid; uuid.len = LEN_UUID_16; - uuid.uu.uuid16 = arg->app_uuid; + uuid.uu.uuid16 = arg->app_reg.app_id; BTA_GATTS_AppRegister(&uuid, btc_gatts_inter_cb); break; } case BTC_GATTS_ACT_APP_UNREGISTER: - BTA_GATTS_AppDeregister(arg->gatt_if); + BTA_GATTS_AppDeregister(arg->app_unreg.gatt_if); break; case BTC_GATTS_ACT_CREATE_SERVICE: { tBTA_GATT_SRVC_ID srvc_id; - btc_to_bta_srvc_id(&srvc_id, &arg->service_id); - BTA_GATTS_CreateService(arg->gatt_if, &srvc_id.id.uuid, - srvc_id.id.inst_id, arg->num_handle, + btc_to_bta_srvc_id(&srvc_id, &arg->create_srvc.service_id); + BTA_GATTS_CreateService(arg->create_srvc.gatt_if, &srvc_id.id.uuid, + srvc_id.id.inst_id, arg->create_srvc.num_handle, srvc_id.is_primary); break; } case BTC_GATTS_ACT_DELETE_SERVICE: - BTA_GATTS_DeleteService(arg->service_handle); + BTA_GATTS_DeleteService(arg->delete_srvc.service_handle); break; case BTC_GATTS_ACT_START_SERVICE: - BTA_GATTS_StartService(arg->service_handle, BTA_GATT_TRANSPORT_LE); + BTA_GATTS_StartService(arg->start_srvc.service_handle, BTA_GATT_TRANSPORT_LE); break; case BTC_GATTS_ACT_STOP_SERVICE: - BTA_GATTS_StopService(arg->service_handle); + BTA_GATTS_StopService(arg->stop_srvc.service_handle); break; case BTC_GATTS_ACT_ADD_INCLUDE_SERVICE: - BTA_GATTS_AddIncludeService(arg->service_handle, arg->included_service_handle); + BTA_GATTS_AddIncludeService(arg->add_incl_srvc.service_handle, arg->add_incl_srvc.included_service_handle); break; case BTC_GATTS_ACT_ADD_CHAR: { tBT_UUID uuid; - btc_to_bta_uuid(&uuid, &arg->uuid); + btc_to_bta_uuid(&uuid, &arg->add_char.char_uuid); - BTA_GATTS_AddCharacteristic(arg->service_handle, &uuid, - arg->perm, arg->property); + BTA_GATTS_AddCharacteristic(arg->add_char.service_handle, &uuid, + arg->add_char.perm, arg->add_char.property); break; } case BTC_GATTS_ACT_ADD_CHAR_DESCR: { tBT_UUID uuid; - btc_to_bta_uuid(&uuid, &arg->uuid); - BTA_GATTS_AddCharDescriptor(arg->service_handle, arg->perm, &uuid); + btc_to_bta_uuid(&uuid, &arg->add_descr.descr_uuid); + BTA_GATTS_AddCharDescriptor(arg->add_descr.service_handle, arg->add_descr.perm, &uuid); break; } case BTC_GATTS_ACT_SEND_INDICATE: - BTA_GATTS_HandleValueIndication(arg->conn_id, arg->attr_handle, - arg->value_len, arg->value, arg->need_confirm); + BTA_GATTS_HandleValueIndication(arg->send_ind.conn_id, arg->send_ind.attr_handle, + arg->send_ind.value_len, arg->send_ind.value, arg->send_ind.need_confirm); break; case BTC_GATTS_ACT_SEND_RESPONSE: { esp_ble_gatts_cb_param_t param; tBTA_GATTS_RSP rsp_struct; - esp_gatt_rsp_t *p_rsp = &arg->rsp; + esp_gatt_rsp_t *p_rsp = arg->send_rsp.rsp; btc_to_bta_response(&rsp_struct, p_rsp); - BTA_GATTS_SendRsp(arg->conn_id, arg->trans_id, - arg->status, &rsp_struct); + BTA_GATTS_SendRsp(arg->send_rsp.conn_id, arg->send_rsp.trans_id, + arg->send_rsp.status, &rsp_struct); param.rsp.status = 0; param.rsp.handle = rsp_struct.attr_value.handle; @@ -185,14 +241,14 @@ void btc_gatts_call_handler(btc_msg_t *msg) //BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type); #endif // Mark background connections - if (!arg->is_direct) + if (!arg->open.is_direct) BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL); transport = BTA_GATT_TRANSPORT_LE; // Connect! - BTA_GATTS_Open(arg->gatt_if, arg->remote_bda, - arg->is_direct, transport); + BTA_GATTS_Open(arg->open.gatt_if, arg->open.remote_bda, + arg->open.is_direct, transport); break; } case BTC_GATTS_ACT_CLOSE: @@ -202,15 +258,14 @@ void btc_gatts_call_handler(btc_msg_t *msg) //BTA_GATTS_CancelOpen(p_cb->server_if, p_cb->bd_addr.address, FALSE); // Close active connection - if (arg->conn_id != 0) - BTA_GATTS_Close(arg->conn_id); + if (arg->close.conn_id != 0) + BTA_GATTS_Close(arg->close.conn_id); break; default: break; } - - + btc_gatts_arg_deep_free(msg); } void btc_gatts_cb_handler(btc_msg_t *msg) diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gatts.h b/components/bt/bluedroid/btc/profile/std/include/btc_gatts.h index a488d75d0f..de2e8f9980 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gatts.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gatts.h @@ -23,32 +23,81 @@ typedef enum { } btc_gatts_act_t; /* btc_ble_gatts_args_t */ -typedef struct { - esp_gatt_if_t gatt_if; /* internal is server_if or client_if */ - esp_gatt_srvc_id_t service_id; - esp_gatt_id_t char_id; - esp_gatt_id_t descr_uuid; - esp_bt_uuid_t uuid; - esp_gatt_rsp_t rsp; - esp_gatt_perm_t perm; - esp_gatt_char_prop_t property; - esp_bd_addr_t remote_bda; - esp_gatt_status_t status; - uint16_t service_handle; - uint16_t included_service_handle; - uint16_t attr_handle; - uint16_t num_handle; - uint16_t conn_id; - uint16_t trans_id; - bool need_confirm; - bool is_direct; - uint16_t app_uuid; - uint16_t value_len; - uint8_t value[ESP_GATT_MAX_ATTR_LEN]; +typedef union{ + //BTC_GATTS_ACT_APP_REGISTER = 0, + struct app_reg_args { + uint16_t app_id; + } app_reg; + //BTC_GATTS_ACT_APP_UNREGISTER, + struct app_unreg_args { + esp_gatt_if_t gatt_if; + } app_unreg; + //BTC_GATTS_ACT_CREATE_SERVICE, + struct create_srvc_args { + esp_gatt_if_t gatt_if; + esp_gatt_srvc_id_t service_id; + uint16_t num_handle; + } create_srvc; + //BTC_GATTS_ACT_DELETE_SERVICE, + struct delete_srvc_args { + uint16_t service_handle; + } delete_srvc; + //BTC_GATTS_ACT_START_SERVICE, + struct start_srvc_args { + uint16_t service_handle; + } start_srvc; + //BTC_GATTS_ACT_STOP_SERVICE, + struct stop_srvc_args { + uint16_t service_handle; + } stop_srvc; + //BTC_GATTS_ACT_ADD_INCLUDE_SERVICE, + struct add_incl_srvc_args { + uint16_t service_handle; + uint16_t included_service_handle; + } add_incl_srvc; + //BTC_GATTS_ACT_ADD_CHAR, + struct add_char_args { + uint16_t service_handle; + esp_bt_uuid_t char_uuid; + esp_gatt_perm_t perm; + esp_gatt_char_prop_t property; + } add_char; + //BTC_GATTS_ACT_ADD_CHAR_DESCR, + struct add_descr_args { + uint16_t service_handle; + esp_bt_uuid_t descr_uuid; + esp_gatt_perm_t perm; + } add_descr; + //BTC_GATTS_ACT_SEND_INDICATE, + struct send_indicate_args { + uint16_t conn_id; + uint16_t attr_handle; + bool need_confirm; + uint16_t value_len; + uint8_t *value; + } send_ind; + //BTC_GATTS_ACT_SEND_RESPONSE, + struct send_rsp_args { + uint16_t conn_id; + uint32_t trans_id; + esp_gatt_status_t status; + esp_gatt_rsp_t *rsp; + } send_rsp; + //BTC_GATTS_ACT_OPEN, + struct open_args { + esp_gatt_if_t gatt_if; + esp_bd_addr_t remote_bda; + bool is_direct; + } open; + //BTC_GATTS_ACT_CLOSE, + struct close_args { + uint16_t conn_id; + } close; } btc_ble_gatts_args_t; void btc_gatts_call_handler(btc_msg_t *msg); void btc_gatts_cb_handler(btc_msg_t *msg); +void btc_gatts_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); #endif /* __BTC_GATTS_H__ */ -- 2.40.0