--- /dev/null
+// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <string.h>
+#include "esp_gatt_common_api.h"
+#include "esp_bt_main.h"
+#include "esp_gatt_defs.h"
+#include "btc_main.h"
+
+/**
+ * @brief This function is called to set local MTU,
+ * the function is called before BLE connection.
+ *
+ * @param[in] mtu: the size of MTU.
+ *
+ * @return
+ * - ESP_OK: success
+ * - other: failed
+ *
+ */
+esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu)
+{
+ btc_msg_t msg;
+ btc_ble_main_args_t arg;
+
+ ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+
+ if ((mtu < ESP_GATT_DEF_BLE_MTU_SIZE) || (mtu > ESP_GATT_MAX_MTU_SIZE)) {
+ return ESP_GATT_ILLEGAL_PARAMETER;
+ }
+
+ msg.sig = BTC_SIG_API_CALL;
+ msg.pid = BTC_PID_MAIN_INIT;
+ msg.act = BTC_GATT_ACT_SET_LOCAL_MTU;
+ arg.set_mtu.mtu = mtu;
+
+ return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_main_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
+}
\ No newline at end of file
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
-esp_err_t esp_ble_gattc_config_mtu (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t mtu)
+esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id)
{
btc_msg_t msg;
btc_ble_gattc_args_t arg;
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
-
- if ((mtu < ESP_GATT_DEF_BLE_MTU_SIZE) || (mtu > ESP_GATT_MAX_MTU_SIZE)) {
- return ESP_GATT_ILLEGAL_PARAMETER;
- }
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GATTC;
msg.act = BTC_GATTC_ACT_CFG_MTU;
arg.cfg_mtu.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
- arg.cfg_mtu.mtu = mtu;
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
--- /dev/null
+// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "esp_err.h"
+#include "esp_bt_defs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Maximum Transmission Unit used in GATT
+#define ESP_GATT_DEF_BLE_MTU_SIZE 23 /* relate to GATT_DEF_BLE_MTU_SIZE in gatt_api.h */
+
+// Maximum Transmission Unit allowed in GATT
+#define ESP_GATT_MAX_MTU_SIZE 517 /* relate to GATT_MAX_MTU_SIZE in gatt_api.h */
+
+/**
+ * @brief This function is called to set local MTU,
+ * the function is called before BLE connection.
+ *
+ * @param[in] mtu: the size of MTU.
+ *
+ * @return
+ * - ESP_OK: success
+ * - other: failed
+ *
+ */
+extern esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu);
+
+#ifdef __cplusplus
+}
+#endif
ESP_GATTC_DISCONNECT_EVT = 41, /*!< When the ble physical connection disconnected, the event comes */
} esp_gattc_cb_event_t;
-/// Maximum Transmission Unit used in GATT
-#define ESP_GATT_DEF_BLE_MTU_SIZE 23
-
-/// Maximum Transmission Unit allowed in GATT
-#define ESP_GATT_MAX_MTU_SIZE 517
/**
* @brief Gatt client callback parameters union
/**
* @brief Configure the MTU size in the GATT channel. This can be done
- * only once per connection.
+ * only once per connection. Before using, use esp_ble_gatt_set_local_mtu()
+ * to configure the local MTU size.
+ *
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID.
- * @param[in] mtu: desired MTU size to use.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
-esp_err_t esp_ble_gattc_config_mtu (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t mtu);
+esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id);
/**
bta_dm_set_visibility, /* 3 BTA_DM_API_SET_VISIBILITY_EVT */
bta_dm_acl_change, /* 8 BTA_DM_ACL_CHANGE_EVT */
bta_dm_add_device, /* 9 BTA_DM_API_ADD_DEVICE_EVT */
- bta_dm_close_acl, /* 10 BTA_DM_API_ADD_DEVICE_EVT */
+ bta_dm_close_acl, /* 10 BTA_DM_API_REMOVE_ACL_EVT */
#if (SMP_INCLUDED == TRUE)
/* security API events */
bta_dm_bond, /* 11 BTA_DM_API_BOND_EVT */
--- /dev/null
+/******************************************************************************
+* Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This file contains the action functions for gatts and gattc.
+ *
+ *
+ ******************************************************************************/
+
+#include "bta_gatt_common.h"
+#include "gatt_int.h"
+
+void BTA_GATT_SetLocalMTU(uint16_t mtu)
+{
+ gatt_set_local_mtu(mtu);
+}
\ No newline at end of file
tBTA_GATT_STATUS status;
if (bta_gattc_enqueue(p_clcb, p_data)) {
- status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
+ status = GATTC_ConfigureMTU (p_clcb->bta_conn_id);
/* if failed, return callback here */
if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) {
** Returns void
**
*******************************************************************************/
-void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu)
+void BTA_GATTC_ConfigureMTU (UINT16 conn_id)
{
tBTA_GATTC_API_CFG_MTU *p_buf;
p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
p_buf->hdr.layer_specific = conn_id;
- p_buf->mtu = mtu;
-
bta_sys_sendmsg(p_buf);
}
return;
** only once per connection.
**
** Parameters conn_id: connection ID.
-** mtu: desired MTU size to use.
+**
**
** Returns void
**
*******************************************************************************/
-extern void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu);
+extern void BTA_GATTC_ConfigureMTU (UINT16 conn_id);
/*******************************************************************************
** BTA GATT Server API
--- /dev/null
+/******************************************************************************
+* Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ * This file contains the action functions for gatts and gattc.
+ *
+ *
+ ******************************************************************************/
+
+#include "bt_types.h"
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+extern void BTA_GATT_SetLocalMTU(uint16_t mtu);
+
+#ifdef __cplusplus
+}
+#endif
typedef struct {
BT_HDR hdr;
- UINT16 mtu;
} tBTA_GATTC_API_CFG_MTU;
typedef struct {
#include "btc_config.h"
#include "alarm.h"
#include "btc_ble_storage.h"
+#include "bta_gatt_common.h"
static future_t *main_future[BTC_MAIN_FUTURE_NUM];
future_ready(*btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE), FUTURE_SUCCESS);
}
+static void btc_set_local_mtu(uint16_t mtu)
+{
+ BTA_GATT_SetLocalMTU(mtu);
+}
+
void btc_main_call_handler(btc_msg_t *msg)
{
LOG_DEBUG("%s act %d\n", __func__, msg->act);
case BTC_MAIN_ACT_DISABLE:
btc_disable_bluetooth();
break;
+ case BTC_GATT_ACT_SET_LOCAL_MTU:
+ {
+ btc_ble_main_args_t *arg = (btc_ble_main_args_t *)(msg->arg);
+ btc_set_local_mtu(arg->set_mtu.mtu);
+ break;
+ }
default:
LOG_ERROR("%s UNKNOWN ACT %d\n", __func__, msg->act);
break;
BTC_MAIN_ACT_DEINIT,
BTC_MAIN_ACT_ENABLE,
BTC_MAIN_ACT_DISABLE,
+ BTC_GATT_ACT_SET_LOCAL_MTU,
} btc_main_act_t;
typedef enum {
void btc_deinit_bluetooth(future_t *future);
#endif
+/* btc_ble_gattc_args_t */
+typedef union {
+ //BTC_GATT_ACT_SET_LOCAL_MTU,
+ struct set_mtu_arg {
+ uint16_t mtu;
+ } set_mtu;
+} btc_ble_main_args_t;
+
void btc_main_call_handler(btc_msg_t *msg);
#endif /* __BTC_BT_MAIN_H__ */
static void btc_gattc_cfg_mtu(btc_ble_gattc_args_t *arg)
{
- BTA_GATTC_ConfigureMTU (arg->cfg_mtu.conn_id, arg->cfg_mtu.mtu);
+ BTA_GATTC_ConfigureMTU (arg->cfg_mtu.conn_id);
}
static void btc_gattc_search_service(btc_ble_gattc_args_t *arg)
//BTC_GATTC_ACT_CFG_MTU,
struct cfg_mtu_arg {
uint16_t conn_id;
- uint16_t mtu;
} cfg_mtu;
//BTC_GATTC_ACT_SEARCH_SERVICE,
struct search_srvc_arg {
** Returns GATT_SUCCESS if command started successfully.
**
*******************************************************************************/
-tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu)
+tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id)
{
UINT8 ret = GATT_NO_RESOURCES;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_CLCB *p_clcb;
+ uint16_t mtu = gatt_get_local_mtu();
GATT_TRACE_API ("GATTC_ConfigureMTU conn_id=%d mtu=%d", conn_id, mtu );
tGATT_CB gatt_cb;
#endif
+tGATT_DEFAULT gatt_default;
+
/*******************************************************************************
**
** Function gatt_init
#if (GATTS_INCLUDED == TRUE)
gatt_profile_db_init();
#endif ///GATTS_INCLUDED == TRUE
-
+ //init local MTU size
+ gatt_default.local_mtu = GATT_MAX_MTU_SIZE;
}
/* Send L2CAP config req */
memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
cfg.mtu_present = TRUE;
- cfg.mtu = GATT_MAX_MTU_SIZE;
+ cfg.mtu = gatt_default.local_mtu;
L2CA_ConfigReq(lcid, &cfg);
}
/* Send L2CAP config req */
memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
cfg.mtu_present = TRUE;
- cfg.mtu = GATT_MAX_MTU_SIZE;
+ cfg.mtu = gatt_default.local_mtu;
L2CA_ConfigReq(lcid, &cfg);
}
/* else initiating connection failure */
return ch_state;
}
+uint16_t gatt_get_local_mtu(void)
+{
+ return gatt_default.local_mtu;
+}
+
+void gatt_set_local_mtu(uint16_t mtu)
+{
+ gatt_default.local_mtu = mtu;
+}
+
#endif /* BLE_INCLUDED */
/* mtu must be greater than default MTU which is 23/48 */
if (mtu < GATT_DEF_BLE_MTU_SIZE) {
p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
- } else if (mtu > GATT_MAX_MTU_SIZE) {
- p_tcb->payload_size = GATT_MAX_MTU_SIZE;
+ } else if (mtu > gatt_default.local_mtu) {
+ p_tcb->payload_size = gatt_default.local_mtu;
} else {
p_tcb->payload_size = mtu;
}
- GATT_TRACE_ERROR("MTU request PDU with MTU size %d\n", p_tcb->payload_size);
-
l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, p_tcb->payload_size);
if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL) {
} tGATT_CB;
+typedef struct{
+ UINT16 local_mtu;
+} tGATT_DEFAULT;
#define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
extern "C" {
#endif
+extern tGATT_DEFAULT gatt_default;
+
/* Global GATT data */
#if GATT_DYNAMIC_MEMORY == FALSE
extern tGATT_CB gatt_cb;
extern tBT_UUID *gatts_get_service_uuid (tGATT_SVC_DB *p_db);
extern void gatt_reset_bgdev_list(void);
+extern uint16_t gatt_get_local_mtu(void);
+extern void gatt_set_local_mtu(uint16_t mtu);
#endif
** Returns GATT_SUCCESS if command started successfully.
**
*******************************************************************************/
-extern tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu);
+extern tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id);
/*******************************************************************************
**
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
+#include "esp_gatt_common_api.h"
#define GATTC_TAG "GATTC_DEMO"
#define REMOTE_SERVICE_UUID 0x00FF
ESP_LOGE(GATTC_TAG, "set scan params error, error code = %x", scan_ret);
}
break;
- case ESP_GATTC_CONNECT_EVT:
+ case ESP_GATTC_CONNECT_EVT:{
//p_data->connect.status always be ESP_GATT_OK
ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d, status %d", conn_id, gattc_if, p_data->connect.status);
conn_id = p_data->connect.conn_id;
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
- esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, conn_id, 200);
+ esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, conn_id);
if (mtu_ret){
ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
}
break;
+ }
case ESP_GATTC_OPEN_EVT:
if (param->open.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "open failed, status %d", p_data->open.status);
if (ret){
ESP_LOGE(GATTC_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
}
+ esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
+ if (local_mtu_ret){
+ ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
+ }
}
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
+#include "esp_gatt_common_api.h"
#define GATTC_TAG "SEC_GATTC_DEMO"
#define REMOTE_SERVICE_UUID 0x1809
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
- esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, conn_id, 200);
+ esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, conn_id);
if (mtu_ret){
ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
}
if (ret){
ESP_LOGE(GATTC_TAG, "%s gattc app register error, error code = %x\n", __func__, ret);
}
+ ret = esp_ble_gatt_set_local_mtu(200);
+ if (ret){
+ ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", ret);
+ }
}
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_bt_main.h"
+#include "esp_gatt_common_api.h"
#include "sdkconfig.h"
ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
return;
}
+ esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
+ if (local_mtu_ret){
+ ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
+ }
return;
}
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
+#include "esp_gatt_common_api.h"
#define GATTC_TAG "GATTC_MULTIPLE_DEMO"
#define REMOTE_SERVICE_UUID 0x00FF
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
esp_log_buffer_hex(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
- esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, p_data->open.conn_id, 200);
+ esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id);
if (mtu_ret){
ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
}
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
esp_log_buffer_hex(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
- esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, p_data->open.conn_id, 200);
+ esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id);
if (mtu_ret){
ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
}
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
esp_log_buffer_hex(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
- esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, p_data->open.conn_id, 200);
+ esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id);
if (mtu_ret){
ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
}
ESP_LOGE(GATTC_TAG, "gattc app register error, error code = %x", ret);
return;
}
+ ret = esp_ble_gatt_set_local_mtu(200);
+ if (ret){
+ ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", ret);
+ }
+
}