diff --git a/components/bt/bluedroid/api/esp_gatt_common_api.c b/components/bt/bluedroid/api/esp_gatt_common_api.c index 1146750c7..9b532953f 100644 --- a/components/bt/bluedroid/api/esp_gatt_common_api.c +++ b/components/bt/bluedroid/api/esp_gatt_common_api.c @@ -46,4 +46,13 @@ esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu) arg.set_mtu.mtu = mtu; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatt_com_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); -} \ No newline at end of file +} + +#if (BLE_INCLUDED == TRUE) +extern uint16_t L2CA_GetFreePktBufferNum_LE(void); + +uint16_t esp_ble_get_sendable_packets_num () +{ + return L2CA_GetFreePktBufferNum_LE(); +} +#endif diff --git a/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h b/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h index 3a9b74458..bee2c28f0 100644 --- a/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h @@ -44,6 +44,10 @@ extern "C" { */ extern esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu); +#if (BLE_INCLUDED == TRUE) +extern uint16_t esp_ble_get_sendable_packets_num (void); +#endif + #ifdef __cplusplus } #endif diff --git a/components/bt/bluedroid/api/include/api/esp_gatt_defs.h b/components/bt/bluedroid/api/include/api/esp_gatt_defs.h index d6c140a14..615ea418b 100644 --- a/components/bt/bluedroid/api/include/api/esp_gatt_defs.h +++ b/components/bt/bluedroid/api/include/api/esp_gatt_defs.h @@ -34,7 +34,7 @@ extern "C" { * All "ESP_GATT_UUID_xxx" is attribute types */ #define ESP_GATT_UUID_IMMEDIATE_ALERT_SVC 0x1802 /* Immediate alert Service*/ -#define ESP_GATT_UUID_LINK_LOSS_SVC 0x1803 /* Link Loss Service*/ +#define ESP_GATT_UUID_LINK_LOSS_SVC 0x1803 /* Link Loss Service*/ #define ESP_GATT_UUID_TX_POWER_SVC 0x1804 /* TX Power Service*/ #define ESP_GATT_UUID_CURRENT_TIME_SVC 0x1805 /* Current Time Service Service*/ #define ESP_GATT_UUID_REF_TIME_UPDATE_SVC 0x1806 /* Reference Time Update Service*/ @@ -68,8 +68,14 @@ extern "C" { #define ESP_GATT_UUID_CHAR_PRESENT_FORMAT 0x2904 /* Characteristic Presentation Format*/ #define ESP_GATT_UUID_CHAR_AGG_FORMAT 0x2905 /* Characteristic Aggregate Format*/ #define ESP_GATT_UUID_CHAR_VALID_RANGE 0x2906 /* Characteristic Valid Range */ -#define ESP_GATT_UUID_EXT_RPT_REF_DESCR 0x2907 -#define ESP_GATT_UUID_RPT_REF_DESCR 0x2908 +#define ESP_GATT_UUID_EXT_RPT_REF_DESCR 0x2907 /* External Report Reference */ +#define ESP_GATT_UUID_RPT_REF_DESCR 0x2908 /* Report Reference */ +#define ESP_GATT_UUID_NUM_DIGITALS_DESCR 0x2909 /* Number of Digitals */ +#define ESP_GATT_UUID_VALUE_TRIGGER_DESCR 0x290A /* Value Trigger Setting */ +#define ESP_GATT_UUID_ENV_SENSING_CONFIG_DESCR 0x290B /* Environmental Sensing Configuration */ +#define ESP_GATT_UUID_ENV_SENSING_MEASUREMENT_DESCR 0x290C /* Environmental Sensing Measurement */ +#define ESP_GATT_UUID_ENV_SENSING_TRIGGER_DESCR 0x290D /* Environmental Sensing Trigger Setting */ +#define ESP_GATT_UUID_TIME_TRIGGER_DESCR 0x290E /* Time Trigger Setting */ /* GAP Profile Attributes */ #define ESP_GATT_UUID_GAP_DEVICE_NAME 0x2A00 @@ -299,7 +305,7 @@ typedef enum { * @brief Attribute description (used to create database) */ typedef struct - { + { uint16_t uuid_length; /*!< UUID length */ uint8_t *uuid_p; /*!< UUID value */ uint16_t perm; /*!< Attribute permission */ @@ -348,23 +354,23 @@ typedef struct /** * @brief Gatt include service entry element */ -typedef struct +typedef struct { - uint16_t start_hdl; /*!< Gatt start handle value of included service */ - uint16_t end_hdl; /*!< Gatt end handle value of included service */ - uint16_t uuid; /*!< Gatt attribute value UUID of included service */ + uint16_t start_hdl; /*!< Gatt start handle value of included service */ + uint16_t end_hdl; /*!< Gatt end handle value of included service */ + uint16_t uuid; /*!< Gatt attribute value UUID of included service */ } esp_gatts_incl_svc_desc_t; /*!< Gatt include service entry element */ /** * @brief Gatt include 128 bit service entry element */ -typedef struct +typedef struct { - uint16_t start_hdl; /*!< Gatt start handle value of included 128 bit service */ - uint16_t end_hdl; /*!< Gatt end handle value of included 128 bit service */ -} esp_gatts_incl128_svc_desc_t; /*!< Gatt include 128 bit service entry element */ + uint16_t start_hdl; /*!< Gatt start handle value of included 128 bit service */ + uint16_t end_hdl; /*!< Gatt end handle value of included 128 bit service */ +} esp_gatts_incl128_svc_desc_t; /*!< Gatt include 128 bit service entry element */ -/// Gatt attribute value +/// Gatt attribute value typedef struct { uint8_t value[ESP_GATT_MAX_ATTR_LEN]; /*!< Gatt attribute value */ uint16_t handle; /*!< Gatt attribute handle */ @@ -426,8 +432,8 @@ typedef struct { /** * @brief service element */ -typedef struct { - bool is_primary; /*!< The service flag, true if the service is primary service, else is secondly service */ +typedef struct { + bool is_primary; /*!< The service flag, true if the service is primary service, else is secondary service */ uint16_t start_handle; /*!< The start handle of the service */ uint16_t end_handle; /*!< The end handle of the service */ esp_bt_uuid_t uuid; /*!< The uuid of the service */ diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c index 00771981d..9a43c5588 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c @@ -76,7 +76,7 @@ void BTA_GATTC_Disable(void) ** Description This function is called to register application callbacks ** with BTA GATTC module. ** -** Parameters p_app_uuid - applicaiton UUID +** Parameters p_app_uuid - application UUID ** p_client_cb - pointer to the application callback function. ** ** Returns None @@ -338,13 +338,13 @@ const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 hand } void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_service_with_uuid(conn_id, svc_uuid, db, count); } void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_ALL_CHAR, @@ -359,7 +359,7 @@ void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle } void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_ALL_DESCRI, @@ -374,7 +374,7 @@ void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle, } void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, tBT_UUID char_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_CHAR_BY_UUID, @@ -390,7 +390,7 @@ void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_han void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t end_handle, tBT_UUID char_uuid, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_DESCRI_BY_UUID, @@ -405,7 +405,7 @@ void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t en } void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_DESCRI_BY_HANDLE, @@ -420,7 +420,7 @@ void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID } void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - tBT_UUID *incl_uuid, btgatt_db_element_t **db, int *count) + tBT_UUID *incl_uuid, btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_INCLUDE_SVC, @@ -434,13 +434,13 @@ void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end count); } -void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count) +void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count) { bta_gattc_get_db_size_handle(conn_id, start_handle, end_handle, count); } void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count) + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count) { bta_gattc_get_db_size_with_type_handle(conn_id, type, start_handle, end_handle, char_handle, count); } @@ -459,7 +459,7 @@ void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type, ** *******************************************************************************/ void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count); } diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c b/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c index 128dc299c..2347457a2 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c @@ -1227,7 +1227,7 @@ tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(UINT16 conn_id, UINT16 handle) void bta_gattc_get_service_with_uuid(UINT16 conn_id, tBT_UUID *svc_uuid, btgatt_db_element_t **svc_db, - int *count) + UINT16 *count) { const list_t* svc = bta_gattc_get_services(conn_id); if(!svc) { @@ -1301,7 +1301,7 @@ void bta_gattc_get_db_with_opration(UINT16 conn_id, tBT_UUID *descr_uuid, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **char_db, - int *count) + UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); @@ -1666,7 +1666,7 @@ static size_t bta_gattc_get_db_size(list_t *services, return db_size; } -void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count) +void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); @@ -1685,7 +1685,7 @@ void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 en } void bta_gattc_get_db_size_with_type_handle(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count) + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); @@ -1732,7 +1732,7 @@ void bta_gattc_get_db_size_with_type_handle(UINT16 conn_id, bt_gatt_db_attribute static void bta_gattc_get_gatt_db_impl(tBTA_GATTC_SERV *p_srvc_cb, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, - int *count) + UINT16 *count) { APPL_TRACE_DEBUG("%s: start_handle 0x%04x, end_handle 0x%04x", __func__, start_handle, end_handle); @@ -1880,7 +1880,7 @@ static void bta_gattc_get_gatt_db_impl(tBTA_GATTC_SERV *p_srvc_cb, ** Returns None. ** *******************************************************************************/ -void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, int *count) +void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); diff --git a/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h index 788670767..5c2b1a85a 100644 --- a/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h +++ b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h @@ -512,12 +512,12 @@ extern const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle(UINT16 conn_id tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb(tBTA_GATTC_SERV *p_srcb, UINT16 handle); extern tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic(UINT16 conn_id, UINT16 handle); extern tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(UINT16 conn_id, UINT16 handle); -extern void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count); +extern void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count); extern void bta_gattc_get_db_size_with_type_handle(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count); + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count); extern void bta_gattc_get_service_with_uuid(UINT16 conn_id, tBT_UUID *svc_uuid, btgatt_db_element_t **svc_db, - int *count); + UINT16 *count); extern void bta_gattc_get_db_with_opration(UINT16 conn_id, bt_gatt_get_db_op_t op, @@ -527,9 +527,9 @@ extern void bta_gattc_get_db_with_opration(UINT16 conn_id, tBT_UUID *descr_uuid, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **char_db, - int *count); + UINT16 *count); -extern void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, int *count); +extern void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, UINT16 *count); extern tBTA_GATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV *p_srvc_cb); extern void bta_gattc_rebuild_cache(tBTA_GATTC_SERV *p_srcv, UINT16 num_attr, tBTA_GATTC_NV_ATTR *attr); diff --git a/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h b/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h index 5048511c4..997d7b7d1 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h +++ b/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h @@ -152,7 +152,7 @@ typedef UINT8 tBTA_GATT_STATUS; #define BTA_GATTC_CLOSE_EVT 5 /* GATTC close request status event */ #define BTA_GATTC_SEARCH_CMPL_EVT 6 /* GATT discovery complete event */ #define BTA_GATTC_SEARCH_RES_EVT 7 /* GATT discovery result event */ -#define BTA_GATTC_READ_DESCR_EVT 8 /* GATT read characterisitc descriptor event */ +#define BTA_GATTC_READ_DESCR_EVT 8 /* GATT read characteristic descriptor event */ #define BTA_GATTC_WRITE_DESCR_EVT 9 /* GATT write characteristic descriptor event */ #define BTA_GATTC_NOTIF_EVT 10 /* GATT attribute notification event */ #define BTA_GATTC_PREP_WRITE_EVT 11 /* GATT prepare write event */ @@ -739,7 +739,7 @@ extern void BTA_GATTC_Disable(void); ** Description This function is called to register application callbacks ** with BTA GATTC module. ** -** Parameters p_app_uuid - applicaiton UUID +** Parameters p_app_uuid - application UUID ** p_client_cb - pointer to the application callback function. ** ** Returns None @@ -867,32 +867,32 @@ extern const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetCharacteristic(UINT16 conn_ *******************************************************************************/ extern const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 handle); -extern void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid, - btgatt_db_element_t **db, int *count); +extern void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid, + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, tBT_UUID char_uuid, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t end_handle, tBT_UUID char_uuid, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - tBT_UUID *incl_uuid, btgatt_db_element_t **db, int *count); + tBT_UUID *incl_uuid, btgatt_db_element_t **db, UINT16 *count); -extern void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count); +extern void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count); extern void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count); + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count); /******************************************************************************* ** @@ -907,7 +907,7 @@ extern void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_ ** *******************************************************************************/ extern void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); /******************************************************************************* ** @@ -1202,7 +1202,7 @@ extern void BTA_GATTS_Disable(void); ** Description This function is called to register application callbacks ** with BTA GATTS module. ** -** Parameters p_app_uuid - applicaiton UUID +** Parameters p_app_uuid - application UUID ** p_cback - pointer to the application callback function. ** ** Returns None @@ -1325,7 +1325,7 @@ extern void BTA_GATTS_DeleteService(UINT16 service_id); ** Description This function is called to start a service. ** ** Parameters service_id: the service ID to be started. -** sup_transport: supported trasnport. +** sup_transport: supported transport. ** ** Returns None. ** diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c index 4c45f829d..300d802f9 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -315,7 +315,7 @@ esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int svc_num = 0; + uint16_t svc_num = 0; tBT_UUID *bta_uuid = NULL; if (svc_uuid) { bta_uuid = osi_malloc(sizeof(tBT_UUID)); @@ -324,7 +324,7 @@ esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc BTA_GATTC_GetServiceWithUUID(conn_id, bta_uuid, &db, &svc_num); - if ((status = btc_gattc_check_valid_param(svc_num, offset)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)svc_num, offset)) != ESP_GATT_OK) { if (db) { osi_free(db); } @@ -334,7 +334,7 @@ esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)svc_num, ESP_GATT_DB_PRIMARY_SERVICE, offset, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, svc_num, ESP_GATT_DB_PRIMARY_SERVICE, offset, (void *)result, db); } *count = svc_num; @@ -356,17 +356,17 @@ esp_gatt_status_t btc_ble_gattc_get_all_char(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int char_num = 0; + uint16_t char_num = 0; BTA_GATTC_GetAllChar(conn_id, start_handle, end_handle, &db, &char_num); - if ((status = btc_gattc_check_valid_param(char_num, offset)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)char_num, offset)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)char_num, ESP_GATT_DB_CHARACTERISTIC, offset, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, char_num, ESP_GATT_DB_CHARACTERISTIC, offset, (void *)result, db); } *count = char_num; @@ -384,17 +384,17 @@ esp_gatt_status_t btc_ble_gattc_get_all_descr(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int descr_num = 0; + uint16_t descr_num = 0; BTA_GATTC_GetAllDescriptor(conn_id, char_handle, &db, &descr_num); - if ((status = btc_gattc_check_valid_param(descr_num, offset)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)descr_num, offset)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, offset, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, offset, (void *)result, db); } *count = descr_num; @@ -414,19 +414,19 @@ esp_gatt_status_t btc_ble_gattc_get_char_by_uuid(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int char_num = 0; + uint16_t char_num = 0; tBT_UUID bta_uuid = {0}; btc_to_bta_uuid(&bta_uuid, &char_uuid); BTA_GATTC_GetCharByUUID(conn_id, start_handle, end_handle, bta_uuid, &db, &char_num); - if ((status = btc_gattc_check_valid_param(char_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)char_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)char_num, ESP_GATT_DB_CHARACTERISTIC, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, char_num, ESP_GATT_DB_CHARACTERISTIC, 0, (void *)result, db); } *count = char_num; @@ -447,7 +447,7 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_uuid(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int descr_num = 0; + uint16_t descr_num = 0; tBT_UUID bta_char_uuid = {0}; tBT_UUID bta_descr_uuid = {0}; btc_to_bta_uuid(&bta_char_uuid, &char_uuid); @@ -456,14 +456,14 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_uuid(uint16_t conn_id, BTA_GATTC_GetDescrByUUID(conn_id, start_handle, end_handle, bta_char_uuid, bta_descr_uuid, &db, &descr_num); - if ((status = btc_gattc_check_valid_param(descr_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)descr_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); } *count = descr_num; @@ -482,20 +482,20 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_char_handle(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int descr_num = 0; + uint16_t descr_num = 0; tBT_UUID bta_descr_uuid = {0}; btc_to_bta_uuid(&bta_descr_uuid, &descr_uuid); BTA_GATTC_GetDescrByCharHandle(conn_id, char_handle, bta_descr_uuid, &db, &descr_num); - if ((status = btc_gattc_check_valid_param(descr_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)descr_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); } *count = descr_num; @@ -516,7 +516,7 @@ esp_gatt_status_t btc_ble_gattc_get_include_service(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int incl_num = 0; + uint16_t incl_num = 0; tBT_UUID bta_uuid = {0}; if (incl_uuid != NULL) { @@ -526,14 +526,14 @@ esp_gatt_status_t btc_ble_gattc_get_include_service(uint16_t conn_id, BTA_GATTC_GetIncludeService(conn_id, start_handle, end_handle, NULL, &db, &incl_num); } - if ((status = btc_gattc_check_valid_param(incl_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)incl_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; }else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)incl_num, ESP_GATT_DB_INCLUDED_SERVICE, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, incl_num, ESP_GATT_DB_INCLUDED_SERVICE, 0, (void *)result, db); } *count = incl_num; @@ -552,9 +552,9 @@ esp_gatt_status_t btc_ble_gattc_get_attr_count(uint16_t conn_id, uint16_t *count) { if (type == ESP_GATT_DB_ALL) { - BTA_GATTC_GetDBSize(conn_id, start_handle, end_handle, (int *)count); + BTA_GATTC_GetDBSize(conn_id, start_handle, end_handle, count); } else { - BTA_GATTC_GetDBSizeByType(conn_id, type, start_handle, end_handle, char_handle, (int *)count); + BTA_GATTC_GetDBSizeByType(conn_id, type, start_handle, end_handle, char_handle, count); } return ESP_GATT_OK; @@ -564,7 +564,7 @@ esp_gatt_status_t btc_ble_gattc_get_db(uint16_t conn_id, uint16_t start_handle, esp_gattc_db_elem_t *db, uint16_t *count) { btgatt_db_element_t *get_db = NULL; - int num = 0; + uint16_t num = 0; tBT_UUID bta_uuid; uint16_t db_size = 0; BTA_GATTC_GetGattDb(conn_id, start_handle, end_handle, &get_db, &num); 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 eff4d41a1..577f08248 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c @@ -389,7 +389,13 @@ static void btc_gatts_act_create_attr_tab(esp_gatts_attr_db_t *gatts_attr_db, case ESP_GATT_UUID_CHAR_AGG_FORMAT: case ESP_GATT_UUID_CHAR_VALID_RANGE: case ESP_GATT_UUID_EXT_RPT_REF_DESCR: - case ESP_GATT_UUID_RPT_REF_DESCR:{ + case ESP_GATT_UUID_RPT_REF_DESCR: + case ESP_GATT_UUID_NUM_DIGITALS_DESCR: + case ESP_GATT_UUID_VALUE_TRIGGER_DESCR: + case ESP_GATT_UUID_ENV_SENSING_CONFIG_DESCR: + case ESP_GATT_UUID_ENV_SENSING_MEASUREMENT_DESCR: + case ESP_GATT_UUID_ENV_SENSING_TRIGGER_DESCR: + case ESP_GATT_UUID_TIME_TRIGGER_DESCR: { uint16_t svc_hal = btc_creat_tab_env.svc_start_hdl; tBT_UUID bta_char_uuid; esp_bt_uuid_t uuid_temp; @@ -449,7 +455,7 @@ static esp_gatt_status_t btc_gatts_check_valid_attr_tab(esp_gatts_attr_db_t *gat case ESP_GATT_UUID_PRI_SERVICE: case ESP_GATT_UUID_SEC_SERVICE: if (++svc_num > 1) { - BTC_TRACE_ERROR("Each service table can only created one primary service or secondly service."); + BTC_TRACE_ERROR("Each service table can only created one primary service or secondary service."); return ESP_GATT_ERROR; } break; diff --git a/components/bt/bluedroid/stack/l2cap/l2c_api.c b/components/bt/bluedroid/stack/l2cap/l2c_api.c index 906b961d2..778659f29 100644 --- a/components/bt/bluedroid/stack/l2cap/l2c_api.c +++ b/components/bt/bluedroid/stack/l2cap/l2c_api.c @@ -1871,6 +1871,14 @@ BOOLEAN L2CA_CheckIsCongest(UINT16 fixed_cid, UINT16 handle) return TRUE; } + +#if (BLE_INCLUDED == TRUE) +UINT16 L2CA_GetFreePktBufferNum_LE(void) +{ + return l2cb.controller_le_xmit_window; +} +#endif + /******************************************************************************* ** ** Function L2CA_RemoveFixedChnl diff --git a/components/bt/common/osi/config.c b/components/bt/common/osi/config.c index 75ec63967..698c37be0 100644 --- a/components/bt/common/osi/config.c +++ b/components/bt/common/osi/config.c @@ -389,7 +389,7 @@ bool config_save(const config_t *config, const char *filename) const size_t keyname_bufsz = sizeof(CONFIG_KEY) + 5 + 1; // including log10(sizeof(i)) char *keyname = osi_calloc(keyname_bufsz); int config_size = get_config_size(config); - char *buf = osi_calloc(config_size + 100); + char *buf = osi_calloc(config_size); if (!line || !buf || !keyname) { err_code |= 0x01; goto error; @@ -414,8 +414,8 @@ bool config_save(const config_t *config, const char *filename) err_code |= 0x10; goto error; } - if(w_cnt_total + w_cnt > config_size + 100) { - OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size.", __func__, w_cnt + w_cnt_total); + if(w_cnt_total + w_cnt > config_size) { + OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size (config_size = %d).", __func__, (w_cnt + w_cnt_total), config_size); err_code |= 0x20; goto error; } @@ -432,8 +432,8 @@ bool config_save(const config_t *config, const char *filename) err_code |= 0x10; goto error; } - if(w_cnt_total + w_cnt > config_size + 100) { - OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size.", __func__, w_cnt + w_cnt_total); + if(w_cnt_total + w_cnt > config_size) { + OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size.(config_size = %d)", __func__, (w_cnt + w_cnt_total), config_size); err_code |= 0x20; goto error; } @@ -543,7 +543,10 @@ static void config_parse(nvs_handle fp, config_t *config) const size_t keyname_bufsz = sizeof(CONFIG_KEY) + 5 + 1; // including log10(sizeof(i)) char *keyname = osi_calloc(keyname_bufsz); int buf_size = get_config_size_from_flash(fp); - char *buf = osi_calloc(buf_size + 100); + char *buf = osi_calloc(buf_size); + if(buf_size == 0) { //First use nvs + goto error; + } if (!line || !section || !buf || !keyname) { err_code |= 0x01; goto error; diff --git a/components/bt/common/osi/fixed_queue.c b/components/bt/common/osi/fixed_queue.c index d0e68edb2..df8bfff33 100644 --- a/components/bt/common/osi/fixed_queue.c +++ b/components/bt/common/osi/fixed_queue.c @@ -129,17 +129,19 @@ size_t fixed_queue_capacity(fixed_queue_t *queue) void fixed_queue_enqueue(fixed_queue_t *queue, void *data) { + bool status=false; //Flag whether enqueued success + assert(queue != NULL); assert(data != NULL); osi_sem_take(&queue->enqueue_sem, OSI_SEM_MAX_TIMEOUT); osi_mutex_lock(&queue->lock, OSI_MUTEX_MAX_TIMEOUT); - - list_append(queue->list, data); + status = list_append(queue->list, data); //Check whether enqueued success osi_mutex_unlock(&queue->lock); - osi_sem_give(&queue->dequeue_sem); + if(status == true) + osi_sem_give(&queue->dequeue_sem); } void *fixed_queue_dequeue(fixed_queue_t *queue) diff --git a/components/bt/common/osi/list.c b/components/bt/common/osi/list.c index 730fe4e97..93db17fb3 100644 --- a/components/bt/common/osi/list.c +++ b/components/bt/common/osi/list.c @@ -102,6 +102,7 @@ bool list_insert_after(list_t *list, list_node_t *prev_node, void *data) { assert(data != NULL); list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t)); if (!node) { + OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ ); return false; } node->next = prev_node->next; @@ -120,6 +121,7 @@ bool list_prepend(list_t *list, void *data) assert(data != NULL); list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t)); if (!node) { + OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ ); return false; } node->next = list->head; @@ -138,6 +140,7 @@ bool list_append(list_t *list, void *data) assert(data != NULL); list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t)); if (!node) { + OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ ); return false; } node->next = NULL; diff --git a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c index 183c8d3fd..1824e74fc 100644 --- a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c @@ -1337,8 +1337,8 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) tBT_UUID char_uuid = {0}; tBTA_GATT_STATUS status = 0U; tBTA_GATT_UNFMT write = {0}; - int count = 0; - int num = 0; + u16_t count = 0; + u16_t num = 0; /* Get the characteristic num within Mesh Provisioning/Proxy Service */ BTA_GATTC_GetDBSizeByType(p_data->search_cmpl.conn_id, BTGATT_DB_CHARACTERISTIC, diff --git a/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h b/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h index 32c8c6c0c..513bcbef3 100644 --- a/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h +++ b/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h @@ -50,14 +50,15 @@ typedef enum { ESP_HIDD_DEINIT_FAILED = 0, } esp_hidd_deinit_state_t; -#define LEFT_CONTROL_KEY_MASK (1 >> 0) -#define LEFT_SHIFT_KEY_MASK (1 >> 1) -#define LEFT_ALT_KEY_MASK (1 >> 2) -#define LEFT_GUI_KEY_MASK (1 >> 3) -#define RIGHT_CONTROL_KEY_MASK (1 >> 4) -#define RIGHT_SHIFT_KEY_MASK (1 >> 5) -#define RIGHT_ALT_KEY_MASK (1 >> 6) -#define RIGHT_GUI_KEY_MASK (1 >> 7) +#define LEFT_CONTROL_KEY_MASK (1 << 0) +#define LEFT_SHIFT_KEY_MASK (1 << 1) +#define LEFT_ALT_KEY_MASK (1 << 2) +#define LEFT_GUI_KEY_MASK (1 << 3) +#define RIGHT_CONTROL_KEY_MASK (1 << 4) +#define RIGHT_SHIFT_KEY_MASK (1 << 5) +#define RIGHT_ALT_KEY_MASK (1 << 6) +#define RIGHT_GUI_KEY_MASK (1 << 7) + typedef uint8_t key_mask_t; /** * @brief HIDD callback parameters union diff --git a/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c b/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c index b0ca2daa9..d438b7fd9 100644 --- a/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c +++ b/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c @@ -30,6 +30,15 @@ #include "freertos/semphr.h" #include "freertos/task.h" +/********************************************************** + * Thread/Task reference + **********************************************************/ +#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE +#define BLUETOOTH_TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) +#else +#define BLUETOOTH_TASK_PINNED_TO_CORE (0) +#endif + #define GATTC_TAG "GATTC_DEMO" #define REMOTE_SERVICE_UUID 0x00FF #define REMOTE_NOTIFY_CHAR_UUID 0xFF01 @@ -58,7 +67,7 @@ static SemaphoreHandle_t gattc_semaphore; uint8_t write_data[GATTC_WRITE_LEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f}; #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ -static bool is_connecet = false; +static bool is_connect = false; /* Declare static functions */ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); @@ -141,7 +150,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } break; case ESP_GATTC_CONNECT_EVT: { - is_connecet = true; + is_connect = true; ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if); gl_profile_tab[PROFILE_A_APP_ID].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)); @@ -331,7 +340,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ ESP_LOGI(GATTC_TAG, "write char success "); break; case ESP_GATTC_DISCONNECT_EVT: - is_connecet = false; + is_connect = false; get_server = false; #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) start = false; @@ -477,15 +486,15 @@ static void throughput_client_task(void *param) while(1) { #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) vTaskDelay(2000 / portTICK_PERIOD_MS); - if(is_connecet){ + if(is_connect){ uint32_t bit_rate = 0; if (start_time) { current_time = esp_timer_get_time(); bit_rate = notify_len * SECOND_TO_USECOND / (current_time - start_time); - ESP_LOGI(GATTC_TAG, "Notify Bit rate = %d Btye/s, = %d bit/s, time = %ds", + ESP_LOGI(GATTC_TAG, "Notify Bit rate = %d Byte/s, = %d bit/s, time = %ds", bit_rate, bit_rate<<3, (int)((current_time - start_time) / SECOND_TO_USECOND)); } else { - ESP_LOGI(GATTC_TAG, "Notify Bit rate = 0 Btye/s, = 0 bit/s"); + ESP_LOGI(GATTC_TAG, "Notify Bit rate = 0 Byte/s, = 0 bit/s"); } } #endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */ @@ -494,15 +503,22 @@ static void throughput_client_task(void *param) int res = xSemaphoreTake(gattc_semaphore, portMAX_DELAY); assert(res == pdTRUE); } else { - if (is_connecet) { - // the app data set to 490 just for divided into two packages to send in the low layer - // when the packet length set to 251. - esp_ble_gattc_write_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, - gl_profile_tab[PROFILE_A_APP_ID].conn_id, - gl_profile_tab[PROFILE_A_APP_ID].char_handle, - sizeof(write_data), write_data, - ESP_GATT_WRITE_TYPE_NO_RSP, - ESP_GATT_AUTH_REQ_NONE); + if (is_connect) { + int free_buff_num = esp_ble_get_sendable_packets_num(); + if(free_buff_num > 0) { + for( ; free_buff_num > 0; free_buff_num--) { + // the app data set to 490 just for divided into two packages to send in the low layer + // when the packet length set to 251. + esp_ble_gattc_write_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + gl_profile_tab[PROFILE_A_APP_ID].conn_id, + gl_profile_tab[PROFILE_A_APP_ID].char_handle, + sizeof(write_data), write_data, + ESP_GATT_WRITE_TYPE_NO_RSP, + ESP_GATT_AUTH_REQ_NONE); + } + } else { //Add the vTaskDelay to prevent this task from consuming the CPU all the time, causing low-priority tasks to not be executed at all. + vTaskDelay( 10 / portTICK_PERIOD_MS ); + } } } #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ @@ -518,7 +534,7 @@ void app_main() ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } - ESP_ERROR_CHECK( ret ); + ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); @@ -570,10 +586,12 @@ void app_main() if (local_mtu_ret){ ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret); } + // The task is only created on the CPU core that Bluetooth is working on, + // preventing the sending task from using the un-updated Bluetooth state on another CPU. + xTaskCreatePinnedToCore(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL, BLUETOOTH_TASK_PINNED_TO_CORE); - xTaskCreate(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL); #if (CONFIG_GATTC_WRITE_THROUGHPUT) - gattc_semaphore = xSemaphoreCreateMutex(); + gattc_semaphore = xSemaphoreCreateBinary(); if (!gattc_semaphore) { ESP_LOGE(GATTC_TAG, "%s, init fail, the gattc semaphore create fail.", __func__); return; diff --git a/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c b/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c index 59a36a01a..9087de10b 100644 --- a/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c +++ b/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c @@ -24,16 +24,23 @@ #include "esp_log.h" #include "nvs_flash.h" #include "esp_bt.h" - #include "esp_gap_ble_api.h" #include "esp_gatts_api.h" #include "esp_bt_defs.h" #include "esp_bt_main.h" -#include "esp_bt_main.h" #include "esp_gatt_common_api.h" #include "sdkconfig.h" +/********************************************************** + * Thread/Task reference + **********************************************************/ +#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE +#define BLUETOOTH_TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) +#else +#define BLUETOOTH_TASK_PINNED_TO_CORE (0) +#endif + #define SECOND_TO_USECOND 1000000 #define GATTS_TAG "GATTS_DEMO" @@ -53,7 +60,7 @@ static uint64_t start_time = 0; static uint64_t current_time = 0; #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ -static bool is_connecet = false; +static bool is_connect = false; ///Declare the static function static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); @@ -522,7 +529,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i case ESP_GATTS_STOP_EVT: break; case ESP_GATTS_CONNECT_EVT: { - is_connecet = true; + is_connect = true; esp_ble_conn_update_params_t conn_params = {0}; memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t)); /* For the IOS system, please reference the apple official documents about the ble connection parameters restrictions. */ @@ -540,7 +547,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i break; } case ESP_GATTS_DISCONNECT_EVT: - is_connecet = false; + is_connect = false; ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT"); esp_ble_gap_start_advertising(&adv_params); break; @@ -611,15 +618,22 @@ void throughput_server_task(void *param) #endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */ while(1) { -#if (CONFIG_GATTS_NOTIFY_THROUGHPUT) +#if (CONFIG_GATTS_NOTIFY_THROUGHPUT) if (!can_send_notify) { int res = xSemaphoreTake(gatts_semaphore, portMAX_DELAY); assert(res == pdTRUE); } else { - if (is_connecet) { - esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, - gl_profile_tab[PROFILE_A_APP_ID].char_handle, - sizeof(indicate_data), indicate_data, false); + if (is_connect) { + int free_buff_num = esp_ble_get_sendable_packets_num(); + if(free_buff_num > 0) { + for( ; free_buff_num > 0; free_buff_num--) { + esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, + gl_profile_tab[PROFILE_A_APP_ID].char_handle, + sizeof(indicate_data), indicate_data, false); + } + } else { //Add the vTaskDelay to prevent this task from consuming the CPU all the time, causing low-priority tasks to not be executed at all. + vTaskDelay( 10 / portTICK_PERIOD_MS ); + } } } #endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */ @@ -630,10 +644,10 @@ void throughput_server_task(void *param) if (start_time) { current_time = esp_timer_get_time(); bit_rate = write_len * SECOND_TO_USECOND / (current_time - start_time); - ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = %d Btye/s, = %d bit/s, time = %ds", + ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = %d Byte/s, = %d bit/s, time = %ds", bit_rate, bit_rate<<3, (int)((current_time - start_time) / SECOND_TO_USECOND)); } else { - ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = 0 Btye/s, = 0 bit/s"); + ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = 0 Byte/s, = 0 bit/s"); } #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ @@ -695,12 +709,13 @@ void app_main() esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(517); if (local_mtu_ret){ - ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret); + ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret); } - - xTaskCreate(&throughput_server_task, "throughput_server_task", 4048, NULL, 15, NULL); + // The task is only created on the CPU core that Bluetooth is working on, + // preventing the sending task from using the un-updated Bluetooth state on another CPU. + xTaskCreatePinnedToCore(&throughput_server_task, "throughput_server_task", 4096, NULL, 15, NULL, BLUETOOTH_TASK_PINNED_TO_CORE); #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) - gatts_semaphore = xSemaphoreCreateMutex(); + gatts_semaphore = xSemaphoreCreateBinary(); if (!gatts_semaphore) { ESP_LOGE(GATTS_TAG, "%s, init fail, the gatts semaphore create fail.", __func__); return; diff --git a/examples/bluetooth/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c index 4106dd24e..04415327c 100644 --- a/examples/bluetooth/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/blufi/main/blufi_example_main.c @@ -86,6 +86,7 @@ const int CONNECTED_BIT = BIT0; /* store the station info for send back to phone */ static bool gl_sta_connected = false; +static bool ble_is_connected = false; static uint8_t gl_sta_bssid[6]; static uint8_t gl_sta_ssid[32]; static int gl_sta_ssid_len; @@ -112,7 +113,11 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) info.sta_bssid_set = true; info.sta_ssid = gl_sta_ssid; info.sta_ssid_len = gl_sta_ssid_len; - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); + if (ble_is_connected == true) { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); + } else { + BLUFI_INFO("BLUFI BLE is not connected yet\n"); + } break; } case SYSTEM_EVENT_STA_CONNECTED: @@ -135,10 +140,14 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) esp_wifi_get_mode(&mode); /* TODO: get config or information of softap, then set to report extra_info */ - if (gl_sta_connected) { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, NULL); + if (ble_is_connected == true) { + if (gl_sta_connected) { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, NULL); + } else { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); + } } else { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); + BLUFI_INFO("BLUFI BLE is not connected yet\n"); } break; case SYSTEM_EVENT_SCAN_DONE: { @@ -167,7 +176,13 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) blufi_ap_list[i].rssi = ap_list[i].rssi; memcpy(blufi_ap_list[i].ssid, ap_list[i].ssid, sizeof(ap_list[i].ssid)); } - esp_blufi_send_wifi_list(apCount, blufi_ap_list); + + if (ble_is_connected == true) { + esp_blufi_send_wifi_list(apCount, blufi_ap_list); + } else { + BLUFI_INFO("BLUFI BLE is not connected yet\n"); + } + esp_wifi_scan_stop(); free(ap_list); free(blufi_ap_list); @@ -186,7 +201,6 @@ static void initialise_wifi(void) ESP_ERROR_CHECK( esp_event_loop_init(example_net_event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_start() ); } @@ -215,6 +229,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para break; case ESP_BLUFI_EVENT_BLE_CONNECT: BLUFI_INFO("BLUFI ble connect\n"); + ble_is_connected = true; server_if = param->connect.server_if; conn_id = param->connect.conn_id; esp_ble_gap_stop_advertising(); @@ -222,6 +237,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para break; case ESP_BLUFI_EVENT_BLE_DISCONNECT: BLUFI_INFO("BLUFI ble disconnect\n"); + ble_is_connected = false; blufi_security_deinit(); esp_ble_gap_start_advertising(&example_adv_params); break; @@ -251,7 +267,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para esp_wifi_get_mode(&mode); - if (gl_sta_connected ) { + if (gl_sta_connected) { memset(&info, 0, sizeof(esp_blufi_extra_info_t)); memcpy(info.sta_bssid, gl_sta_bssid, 6); info.sta_bssid_set = true;