Skip to content

Commit

Permalink
Esp32/ble controller (#23921)
Browse files Browse the repository at this point in the history
* ESP32 as a controller

* Add bluedroid support for esp32/ble-commissioner

* Addressed review comments
  • Loading branch information
PSONALl authored and pull[bot] committed Jul 19, 2023
1 parent 606e52a commit d8f8528
Show file tree
Hide file tree
Showing 14 changed files with 3,099 additions and 46 deletions.
12 changes: 12 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ else()
chip_gn_arg_append("chip_enable_wifi" "false")
endif()

if (CONFIG_ENABLE_CHIPOBLE)
chip_gn_arg_append("chip_enable_chipoble" "true")
endif()

if ((CONFIG_BT_ENABLED) AND (CONFIG_ENABLE_CHIPOBLE))
if (CONFIG_BT_NIMBLE_ENABLED)
chip_gn_arg_append("chip_bt_nimble_enabled" "true")
else()
chip_gn_arg_append("chip_bt_bluedroid_enabled" "true")
endif()
endif()

if (CONFIG_OPENTHREAD_ENABLED)
chip_gn_arg_append("chip_enable_openthread" "true")
endif()
Expand Down
15 changes: 15 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,21 @@ menu "CHIP Device Layer"
default 900
help
The amount of time (in seconds) after which the CHIP platform will close the Commissioning Window
endmenu

menu "Enable ESP32 as a BLE Commissioner"
config ENABLE_ESP32_BLE_CONTROLLER
bool "Enable ESP32 as a BLE Commissioner"
default n
help
Enable esp32 as a BLE Commissioner.

config ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
bool "Enable Commissionee and Commissioner mode"
default n
depends on ENABLE_ESP32_BLE_Controller
help
Enable including commissioner code (CHIPDeviceController.cpp) in the commissionee (Server.cpp) code.

endmenu

Expand Down
125 changes: 124 additions & 1 deletion src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#include "esp_gattc_api.h"
#endif
#include "esp_gatts_api.h"
#include <lib/core/CHIPCallback.h>
#elif CONFIG_BT_NIMBLE_ENABLED
Expand All @@ -56,22 +59,85 @@ struct ble_gatt_char_context

#endif

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER && CONFIG_BT_NIMBLE_ENABLED
#include "nimble/blecent.h"
#endif

#include "ble/Ble.h"
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#include <ble/BleLayer.h>
#include <ble/BleUUID.h>
#include <platform/ESP32/ChipDeviceScanner.h>
#endif

namespace chip {
namespace DeviceLayer {
namespace Internal {

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
void HandleIncomingBleConnection(Ble::BLEEndPoint * bleEP);

enum class BleScanState : uint8_t
{
kNotScanning,
kScanForDiscriminator,
kScanForAddress,
kConnecting,
};

struct BLEAdvConfig
{
char * mpBleName;
uint32_t mAdapterId;
uint8_t mMajor;
uint8_t mMinor;
uint16_t mVendorId;
uint16_t mProductId;
uint64_t mDeviceId;
uint8_t mPairingStatus;
uint8_t mType;
uint16_t mDuration;
const char * mpAdvertisingUUID;
};

struct BLEScanConfig
{
// If an active scan for connection is being performed
BleScanState mBleScanState = BleScanState::kNotScanning;

// If scanning by discriminator, what are we scanning for
SetupDiscriminator mDiscriminator;

// If scanning by address, what address are we searching for
std::string mAddress;

// Optional argument to be passed to callback functions provided by the BLE scan/connect requestor
void * mAppState = nullptr;
};

#endif
/**
* Concrete implementation of the BLEManager singleton object for the ESP32 platform.
*/
class BLEManagerImpl final : public BLEManager,
private Ble::BleLayer,
private Ble::BlePlatformDelegate,
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
private Ble::BleApplicationDelegate,
private Ble::BleConnectionDelegate,
private ChipDeviceScannerDelegate
#else
private Ble::BleApplicationDelegate
#endif
{
public:
BLEManagerImpl() {}
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
CHIP_ERROR ConfigureBle(uint32_t aAdapterId, bool aIsCentral);
#if CONFIG_BT_BLUEDROID_ENABLED
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t * param);
#endif
#endif

private:
// Allow the BLEManager interface class to delegate method calls to
Expand All @@ -90,6 +156,10 @@ class BLEManagerImpl final : public BLEManager,
CHIP_ERROR _SetDeviceName(const char * deviceName);
uint16_t _NumConnections(void);
void _OnPlatformEvent(const ChipDeviceEvent * event);
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
void HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * event);
CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
#endif
::chip::Ble::BleLayer * _GetBleLayer(void);

// ===== Members that implement virtual methods on BlePlatformDelegate.
Expand All @@ -112,7 +182,23 @@ class BLEManagerImpl final : public BLEManager,
// ===== Members that implement virtual methods on BleApplicationDelegate.

void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
// ===== Members that implement virtual methods on BleConnectionDelegate.
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER

void NewConnection(chip::Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator) override;
CHIP_ERROR CancelConnection() override;

// ===== Members that implement virtual methods on ChipDeviceScannerDelegate
#if CONFIG_BT_NIMBLE_ENABLED
virtual void OnDeviceScanned(const struct ble_hs_adv_fields & fields, const ble_addr_t & addr,
const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
#elif CONFIG_BT_BLUEDROID_ENABLED
virtual void OnDeviceScanned(esp_ble_addr_type_t & addr_type, esp_bd_addr_t & addr,
const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
#endif

void OnScanComplete() override;
#endif
// ===== Members for internal use by the following friends.

friend BLEManager & BLEMgr(void);
Expand Down Expand Up @@ -144,6 +230,9 @@ class BLEManagerImpl final : public BLEManager,
kMaxDeviceNameLength = 16
};

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
BLEAdvConfig mBLEAdvConfig;
#endif
#if CONFIG_BT_NIMBLE_ENABLED
uint16_t mSubscribedConIds[kMaxConnections];
#endif
Expand Down Expand Up @@ -218,7 +307,15 @@ class BLEManagerImpl final : public BLEManager,
void HandleDisconnect(esp_ble_gatts_cb_param_t * param);
CHIPoBLEConState * GetConnectionState(uint16_t conId, bool allocate = false);
bool ReleaseConnectionState(uint16_t conId);

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
CHIP_ERROR HandleGAPConnect(esp_ble_gattc_cb_param_t p_data);
CHIP_ERROR HandleGAPCentralConnect(esp_ble_gattc_cb_param_t p_data);

static void HandleConnectFailed(CHIP_ERROR error);
static void ConnectDevice(esp_bd_addr_t & addr, esp_ble_addr_type_t addr_type, uint16_t timeout);
void HandleGAPConnectionFailed();
CHIP_ERROR HandleRXNotify(esp_ble_gattc_cb_param_t p_data);
#endif
static void HandleGATTEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
static void HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);

Expand All @@ -231,10 +328,14 @@ class BLEManagerImpl final : public BLEManager,
void HandleTXCharCCCDWrite(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleTXComplete(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleGAPConnect(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleGAPPeripheralConnect(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleGAPDisconnect(struct ble_gap_event * gapEvent);
CHIP_ERROR SetSubscribed(uint16_t conId);
bool UnsetSubscribed(uint16_t conId);
bool IsSubscribed(uint16_t conId);
static void ConnectDevice(const ble_addr_t & addr, uint16_t timeout);
CHIP_ERROR HandleGAPCentralConnect(struct ble_gap_event * gapEvent);
void HandleGAPConnectionFailed(struct ble_gap_event * gapEvent, CHIP_ERROR error);

static CHIP_ERROR bleprph_set_random_addr(void);
static void bleprph_host_task(void * param);
Expand All @@ -249,6 +350,28 @@ class BLEManagerImpl final : public BLEManager,
void * arg);
void HandleC3CharRead(struct ble_gatt_char_context * param);
#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
static int btshell_on_mtu(uint16_t conn_handle, const struct ble_gatt_error * error, uint16_t mtu, void * arg);

bool SubOrUnsubChar(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
bool subscribe);

static void OnGattDiscComplete(const struct peer * peer, int status, void * arg);
static void HandleConnectFailed(CHIP_ERROR error);
CHIP_ERROR HandleRXNotify(struct ble_gap_event * event);
#endif
#endif
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
static void CancelConnect(void);
static void HandleConnectTimeout(chip::System::Layer *, void * context);
void InitiateScan(BleScanState scanType);
static void InitiateScan(intptr_t arg);
void HandleAdvertisementTimer(System::Layer * systemLayer, void * context);
void HandleAdvertisementTimer();
void CleanScanConfig();
BLEScanConfig mBLEScanConfig;
bool mIsCentral;
#endif

static void DriveBLEState(intptr_t arg);
Expand Down
31 changes: 28 additions & 3 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ declare_args() {
chip_use_factory_data_provider = false
chip_use_device_info_provider = false
chip_config_software_version_number = 0
chip_enable_chipoble = true
chip_bt_nimble_enabled = false
chip_bt_bluedroid_enabled = false
}

defines = [
Expand All @@ -33,7 +36,6 @@ defines = [
static_library("ESP32") {
sources = [
"../SingletonConfigurationManager.cpp",
"BLEManagerImpl.h",
"CHIPDevicePlatformConfig.h",
"CHIPDevicePlatformEvent.h",
"ConfigurationManagerImpl.cpp",
Expand All @@ -55,8 +57,6 @@ static_library("ESP32") {
"PlatformManagerImpl.h",
"SystemTimeSupport.cpp",
"SystemTimeSupport.h",
"bluedroid/BLEManagerImpl.cpp",
"nimble/BLEManagerImpl.cpp",
]

deps = [
Expand All @@ -70,13 +70,38 @@ static_library("ESP32") {
"${chip_root}/src/crypto",
"${chip_root}/src/platform:platform_base",
]

if (chip_enable_ota_requestor) {
sources += [
"OTAImageProcessorImpl.cpp",
"OTAImageProcessorImpl.h",
]
}

if (chip_enable_chipoble) {
sources += [
"BLEManagerImpl.h",
"ChipDeviceScanner.h",
]
}

if (chip_bt_nimble_enabled) {
sources += [
"nimble/BLEManagerImpl.cpp",
"nimble/ChipDeviceScanner.cpp",
"nimble/blecent.h",
"nimble/misc.c",
"nimble/peer.c",
]
}

if (chip_bt_bluedroid_enabled) {
sources += [
"bluedroid/BLEManagerImpl.cpp",
"bluedroid/ChipDeviceScanner.cpp",
]
}

if (chip_enable_wifi) {
sources += [
"ConnectivityManagerImpl_WiFi.cpp",
Expand Down
1 change: 1 addition & 0 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@
#define CHIP_DEVICE_CONFIG_CHIP_KVS_NAMESPACE_PARTITION CONFIG_CHIP_KVS_NAMESPACE_PARTITION_LABEL
#define CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS CONFIG_CHIP_DISCOVERY_TIMEOUT_SECS
#define CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
39 changes: 39 additions & 0 deletions src/platform/ESP32/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ enum
kESPSystemEvent = kRange_PublicPlatformSpecific,
};

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
/**
* Enumerates ESP32 platform-specific event types that are internal to the Chip Device Layer.
*/
enum InternalPlatformSpecificEventTypes
{
kPlatformESP32Event = kRange_InternalPlatformSpecific,
kPlatformESP32BLECentralConnected,
kPlatformESP32BLECentralConnectFailed,
kPlatformESP32BLEWriteComplete,
kPlatformESP32BLESubscribeOpComplete,
kPlatformESP32BLEIndicationReceived,
};

#endif
} // namespace DeviceEventType

/**
Expand Down Expand Up @@ -74,6 +89,30 @@ struct ChipDevicePlatformEvent final
wifi_event_ap_probe_req_rx_t WiFiApProbeReqRecved;
} Data;
} ESPSystemEvent;
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
struct
{
BLE_CONNECTION_OBJECT mConnection;
} BLECentralConnected;
struct
{
CHIP_ERROR mError;
} BLECentralConnectFailed;
struct
{
BLE_CONNECTION_OBJECT mConnection;
} BLEWriteComplete;
struct
{
BLE_CONNECTION_OBJECT mConnection;
bool mIsSubscribed;
} BLESubscribeOpComplete;
struct
{
BLE_CONNECTION_OBJECT mConnection;
chip::System::PacketBuffer * mData;
} BLEIndicationReceived;
#endif
};
};

Expand Down
Loading

0 comments on commit d8f8528

Please sign in to comment.