From: Hrishikesh Dhayagude Date: Fri, 8 Mar 2019 06:48:40 +0000 (+0530) Subject: Protocomm BLE: Make changes in handling BLE read/write requests X-Git-Tag: v4.0-beta1~524^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ce065f1a4f779f55ead0ccfc2147f230d393e398;p=esp-idf Protocomm BLE: Make changes in handling BLE read/write requests 1. Remove unwanted malloc during BLE send response 2. Populate the missing parameters in the response - handle, offset, auth_req --- diff --git a/components/protocomm/src/transports/protocomm_ble.c b/components/protocomm/src/transports/protocomm_ble.c index b374f055e8..3a1f11287d 100644 --- a/components/protocomm/src/transports/protocomm_ble.c +++ b/components/protocomm/src/transports/protocomm_ble.c @@ -110,25 +110,22 @@ static void transport_simple_ble_read(esp_gatts_cb_event_t event, esp_gatt_if_t ESP_LOGD(TAG, "Subsequent read request for attr value"); } - esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *) malloc(sizeof(esp_gatt_rsp_t)); - if (gatt_rsp != NULL) { - gatt_rsp->attr_value.len = MIN(read_len, (protoble_internal->gatt_mtu - 1)); - if (read_len && read_buf) { - memcpy(gatt_rsp->attr_value.value, - read_buf + param->read.offset, - gatt_rsp->attr_value.len); - } - read_len -= gatt_rsp->attr_value.len; - } else { - ESP_LOGE(TAG, "%s, malloc failed", __func__); - return; - } + esp_gatt_rsp_t gatt_rsp = {0}; + gatt_rsp.attr_value.len = MIN(read_len, (protoble_internal->gatt_mtu - 1)); + gatt_rsp.attr_value.handle = param->read.handle; + gatt_rsp.attr_value.offset = param->read.offset; + gatt_rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; + if (gatt_rsp.attr_value.len && read_buf) { + memcpy(gatt_rsp.attr_value.value, + read_buf + param->read.offset, + gatt_rsp.attr_value.len); + } + read_len -= gatt_rsp.attr_value.len; esp_err_t err = esp_ble_gatts_send_response(gatts_if, param->read.conn_id, - param->read.trans_id, status, gatt_rsp); + param->read.trans_id, status, &gatt_rsp); if (err != ESP_OK) { ESP_LOGE(TAG, "Send response error in read"); } - free(gatt_rsp); } static esp_err_t prepare_write_event_env(esp_gatt_if_t gatts_if, @@ -157,22 +154,19 @@ static esp_err_t prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_write_env.prepare_len += param->write.len; prepare_write_env.handle = param->write.handle; if (param->write.need_rsp) { - esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *) malloc(sizeof(esp_gatt_rsp_t)); - if (gatt_rsp != NULL) { - gatt_rsp->attr_value.len = param->write.len; - gatt_rsp->attr_value.handle = param->write.handle; - gatt_rsp->attr_value.offset = param->write.offset; - gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; - memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); - esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, - param->write.trans_id, status, - gatt_rsp); - if (response_err != ESP_OK) { - ESP_LOGE(TAG, "Send response error in prep write"); - } - free(gatt_rsp); - } else { - ESP_LOGE(TAG, "%s, malloc failed", __func__); + esp_gatt_rsp_t gatt_rsp = {0}; + gatt_rsp.attr_value.len = param->write.len; + gatt_rsp.attr_value.handle = param->write.handle; + gatt_rsp.attr_value.offset = param->write.offset; + gatt_rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; + if (gatt_rsp.attr_value.len && param->write.value) { + memcpy(gatt_rsp.attr_value.value, param->write.value, param->write.len); + } + esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, + param->write.trans_id, status, + &gatt_rsp); + if (response_err != ESP_OK) { + ESP_LOGE(TAG, "Send response error in prep write"); } } if (status != ESP_GATT_OK) {