Skip to content

Commit

Permalink
Fixes for bluedroid and Added ChipLock to thread in ble-controller (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PSONALl authored and pull[bot] committed Sep 14, 2023
1 parent 87e3bf5 commit 1724039
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ static_library("ESP32") {
]
}

if (chip_enable_ble_controller) {
sources += [ "ChipDeviceScanner.h" ]
}

if (chip_bt_nimble_enabled) {
sources += [ "nimble/BLEManagerImpl.cpp" ]
if (chip_enable_ble_controller) {
Expand Down
5 changes: 1 addition & 4 deletions src/platform/ESP32/ChipDeviceScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
*/

#pragma once
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER

#include <ble/CHIPBleServiceData.h>
#include <lib/core/CHIPError.h>
#include <system/SystemLayer.h>

#if CONFIG_BT_NIMBLE_ENABLED
#include "host/ble_hs.h"
#else
#elif CONFIG_BT_BLUEDROID_ENABLED
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "esp_gatt_common_api.h"
#include "esp_gatt_defs.h"
#include "esp_gattc_api.h"
#include "esp_gatts_api.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include <ble/CHIPBleServiceData.h>
Expand Down Expand Up @@ -111,4 +109,3 @@ class ChipDeviceScanner
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
59 changes: 32 additions & 27 deletions src/platform/ESP32/bluedroid/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
.gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
},
};
static esp_gatt_if_t chip_ctrl_gattc_if = 0;
#endif

CHIP_ERROR BLEManagerImpl::_Init()
Expand Down Expand Up @@ -432,6 +433,15 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
switch (event)
{
case ESP_GATTC_REG_EVT:
if (param->reg.status == ESP_GATT_OK)
{
chip_ctrl_gattc_if = gattc_if;
}
else
{
ChipLogProgress(Ble, "Reg app failed, app_id %04x, status 0x%x", param->reg.app_id, param->reg.status);
return;
}
break;
case ESP_GATTC_CONNECT_EVT:
err = sInstance.HandleGAPConnect(*p_data);
Expand All @@ -452,8 +462,7 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
break;
}
ChipLogProgress(Ble, "discover service complete conn_id %d", param->dis_srvc_cmpl.conn_id);
esp_ble_gattc_search_service(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, param->cfg_mtu.conn_id,
&remote_filter_service_uuid);
esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_service_uuid);
break;
case ESP_GATTC_CFG_MTU_EVT:
if (param->cfg_mtu.status != ESP_GATT_OK)
Expand Down Expand Up @@ -502,8 +511,8 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
uint16_t count = 0;
uint16_t offset = 0;
esp_gatt_status_t status =
esp_ble_gattc_get_attr_count(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, p_data->search_cmpl.conn_id,
ESP_GATT_DB_CHARACTERISTIC, gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
esp_ble_gattc_get_attr_count(gattc_if, p_data->search_cmpl.conn_id, ESP_GATT_DB_CHARACTERISTIC,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle, INVALID_HANDLE, &count);
if (status != ESP_GATT_OK)
{
Expand All @@ -522,10 +531,9 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
}
else
{
status = esp_ble_gattc_get_all_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, p_data->search_cmpl.conn_id,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle, char_elem_result,
&count, offset);
status = esp_ble_gattc_get_all_char(
gattc_if, p_data->search_cmpl.conn_id, gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle, char_elem_result, &count, offset);
if (status != 0)
{
ChipLogProgress(Ble, "esp_ble_gattc_get_char_by_uuid error");
Expand All @@ -547,8 +555,7 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
else if (char_elem_result[i].properties & CharProps_ReadNotify)
{
gl_profile_tab[PROFILE_A_APP_ID].notify_char_handle = char_elem_result[i].char_handle;
esp_ble_gattc_register_for_notify(gl_profile_tab[PROFILE_A_APP_ID].gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].remote_bda,
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda,
char_elem_result[i].char_handle);
}
}
Expand All @@ -572,7 +579,7 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
{
uint16_t count = 0;
esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count(
gl_profile_tab[PROFILE_A_APP_ID].gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, ESP_GATT_DB_DESCRIPTOR,
gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, ESP_GATT_DB_DESCRIPTOR,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle, gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
gl_profile_tab[PROFILE_A_APP_ID].notify_char_handle, &count);
if (ret_status != ESP_GATT_OK)
Expand All @@ -589,9 +596,9 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
else
{
memcpy(&notify_descr_uuid.uuid.uuid16, ShortUUID_CHIPoBLE_CharTx_Desc, 2);
ret_status = esp_ble_gattc_get_descr_by_char_handle(
gl_profile_tab[PROFILE_A_APP_ID].gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
p_data->reg_for_notify.handle, notify_descr_uuid, descr_elem_result, &count);
ret_status = esp_ble_gattc_get_descr_by_char_handle(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
p_data->reg_for_notify.handle, notify_descr_uuid,
descr_elem_result, &count);
ChipLogProgress(Ble, "discoverd all chars and discr.........\n\n");

ChipDeviceEvent chipEvent;
Expand Down Expand Up @@ -722,14 +729,14 @@ void BLEManagerImpl::HandleConnectFailed(CHIP_ERROR error)

void BLEManagerImpl::CancelConnect(void)
{
int rc = esp_ble_gattc_close(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, connId);
int rc = esp_ble_gattc_close(chip_ctrl_gattc_if, connId);
VerifyOrReturn(rc == 0, ChipLogError(Ble, "Failed to cancel connection rc=%d", rc));
}

void BLEManagerImpl::ConnectDevice(esp_bd_addr_t & addr, esp_ble_addr_type_t addr_type, uint16_t timeout)
{
int rc;
rc = esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, addr, addr_type, true);
rc = esp_ble_gattc_open(chip_ctrl_gattc_if, addr, addr_type, true);
if (rc != 0)
{
ChipLogError(Ble, "Failed to connect to rc=%d", rc);
Expand All @@ -751,13 +758,12 @@ bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const
value[0] = 0x02;
value[1] = 0x00;

rc = esp_ble_gattc_write_char_descr(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
descr_elem_result[0].handle, sizeof(value), value, ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
rc = esp_ble_gattc_write_char_descr(chip_ctrl_gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, descr_elem_result[0].handle,
sizeof(value), value, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
if (rc != 0)
{
ChipLogError(Ble, "esp_ble_gattc_get_descr_by_char_handle failed: %d", rc);
esp_ble_gattc_close(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, conId);
esp_ble_gattc_close(chip_ctrl_gattc_if, conId);
return false;
}
ChipDeviceEvent event;
Expand All @@ -780,13 +786,12 @@ bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, cons
value[0] = 0x00;
value[1] = 0x00;

rc = esp_ble_gattc_write_char_descr(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
descr_elem_result[0].handle, sizeof(value), value, ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
rc = esp_ble_gattc_write_char_descr(chip_ctrl_gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, descr_elem_result[0].handle,
sizeof(value), value, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
if (rc != 0)
{
ChipLogError(Ble, "ble_gattc_write_flat failed: %d", rc);
esp_ble_gattc_close(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, conId);
esp_ble_gattc_close(chip_ctrl_gattc_if, conId);
return false;
}
ChipDeviceEvent event;
Expand Down Expand Up @@ -874,7 +879,7 @@ bool BLEManagerImpl::SendWriteRequest(BLE_CONNECTION_OBJECT conId, const ChipBle
ChipLogProgress(Ble, "In send write request\n");
int rc;

rc = esp_ble_gattc_write_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
rc = esp_ble_gattc_write_char(chip_ctrl_gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].write_char_handle, pBuf->DataLength(), pBuf->Start(),
ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
if (rc != 0)
Expand Down Expand Up @@ -1915,7 +1920,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPCentralConnect(esp_ble_gattc_cb_param_t p_da
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data.connect.remote_bda, sizeof(esp_bd_addr_t));

// Start the GATT discovery process
int rc = esp_ble_gattc_search_service(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, connId, &remote_filter_service_uuid);
int rc = esp_ble_gattc_search_service(chip_ctrl_gattc_if, connId, &remote_filter_service_uuid);
if (rc != 0)
{
HandleGAPConnectionFailed();
Expand All @@ -1935,7 +1940,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPConnect(esp_ble_gattc_cb_param_t p_data)
gl_profile_tab[PROFILE_A_APP_ID].conn_id = p_data.connect.conn_id;
connId = 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));
rc = esp_ble_gattc_send_mtu_req(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, p_data.connect.conn_id);
rc = esp_ble_gattc_send_mtu_req(chip_ctrl_gattc_if, p_data.connect.conn_id);

if (rc != 0)
{
Expand Down
12 changes: 1 addition & 11 deletions src/platform/ESP32/bluedroid/ChipDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER

#include <platform/ESP32/ChipDeviceScanner.h>

#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "esp_gatt_common_api.h"
#include "esp_gatt_defs.h"
#include "esp_gattc_api.h"
#include "esp_gatts_api.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

Expand Down Expand Up @@ -116,6 +106,7 @@ CHIP_ERROR ChipDeviceScanner::StopScan()
ChipLogError(DeviceLayer, "ble_gap_disc_cancel failed: %d", rc);
return CHIP_ERROR_INTERNAL;
}

mIsScanning = false;
mDelegate->OnScanComplete();
return CHIP_NO_ERROR;
Expand All @@ -124,4 +115,3 @@ CHIP_ERROR ChipDeviceScanner::StopScan()
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
4 changes: 4 additions & 0 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,7 @@ void BLEManagerImpl::OnDeviceScanned(const struct ble_hs_adv_fields & fields, co
{
if (!mBLEScanConfig.mDiscriminator.MatchesLongDiscriminator(info.GetDeviceDiscriminator()))
{
printf("Discriminator didi not match \n");
return;
}
ChipLogProgress(Ble, "Device Discriminator match. Attempting to connect");
Expand All @@ -1667,7 +1668,10 @@ void BLEManagerImpl::OnDeviceScanned(const struct ble_hs_adv_fields & fields, co
}

mBLEScanConfig.mBleScanState = BleScanState::kConnecting;
chip::DeviceLayer::PlatformMgr().LockChipStack();
DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds16(kConnectTimeout), HandleConnectTimeout, nullptr);
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

mDeviceScanner.StopScan();

ConnectDevice(addr, kConnectTimeout);
Expand Down

0 comments on commit 1724039

Please sign in to comment.