| /* |
| * |
| * Copyright (c) 2020 Project CHIP Authors |
| * Copyright (c) 2019 Nest Labs, Inc. |
| * |
| * 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. |
| */ |
| |
| /** |
| * @file |
| * Provides an implementation of the BLEManager singleton object |
| * for the Silicon Labs EFR32 platforms. |
| */ |
| |
| #ifndef BLE_MANAGER_IMPL_H |
| #define BLE_MANAGER_IMPL_H |
| |
| #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE |
| |
| #include "bg_types.h" |
| #include "rtos_gecko.h" |
| #include "gatt_db.h" |
| |
| namespace chip { |
| namespace DeviceLayer { |
| namespace Internal { |
| |
| using namespace chip::Ble; |
| |
| /** |
| * Concrete implementation of the NetworkProvisioningServer singleton object for the EFR32 platforms. |
| */ |
| class BLEManagerImpl final : public BLEManager, |
| private BleLayer, |
| private BlePlatformDelegate, |
| private BleApplicationDelegate |
| { |
| // Allow the BLEManager interface class to delegate method calls to |
| // the implementation methods provided by this class. |
| friend BLEManager; |
| |
| // ===== Members that implement the BLEManager internal interface. |
| |
| CHIP_ERROR _Init(void); |
| CHIPoBLEServiceMode _GetCHIPoBLEServiceMode(void); |
| CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val); |
| bool _IsAdvertisingEnabled(void); |
| CHIP_ERROR _SetAdvertisingEnabled(bool val); |
| bool _IsFastAdvertisingEnabled(void); |
| CHIP_ERROR _SetFastAdvertisingEnabled(bool val); |
| bool _IsAdvertising(void); |
| CHIP_ERROR _GetDeviceName(char *buf, size_t bufSize); |
| CHIP_ERROR _SetDeviceName(const char *deviceName); |
| uint16_t _NumConnections(void); |
| void _OnPlatformEvent(const ChipDeviceEvent *event); |
| BleLayer *_GetBleLayer(void) const; |
| |
| // ===== Members that implement virtual methods on BlePlatformDelegate. |
| |
| bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, |
| const ChipBleUUID * svcId, |
| const ChipBleUUID * charId) override; |
| bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, |
| const ChipBleUUID * svcId, |
| const ChipBleUUID * charId) override; |
| bool CloseConnection(BLE_CONNECTION_OBJECT conId) override; |
| uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override; |
| bool SendIndication(BLE_CONNECTION_OBJECT conId, |
| const ChipBleUUID * svcId, |
| const ChipBleUUID * charId, |
| PacketBuffer * pBuf) override; |
| bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, |
| const ChipBleUUID * svcId, |
| const ChipBleUUID * charId, |
| PacketBuffer * pBuf) override; |
| bool SendReadRequest(BLE_CONNECTION_OBJECT conId, |
| const ChipBleUUID * svcId, |
| const ChipBleUUID * charId, |
| PacketBuffer * pBuf) override; |
| bool SendReadResponse(BLE_CONNECTION_OBJECT conId, |
| BLE_READ_REQUEST_CONTEXT requestContext, |
| const ChipBleUUID * svcId, |
| const ChipBleUUID * charId) override; |
| |
| // ===== Members that implement virtual methods on BleApplicationDelegate. |
| |
| void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override; |
| |
| // ===== Members for internal use by the following friends. |
| |
| friend BLEManager & BLEMgr(void); |
| friend BLEManagerImpl &BLEMgrImpl(void); |
| |
| static BLEManagerImpl sInstance; |
| |
| // ===== Private members reserved for use by this class only. |
| |
| enum |
| { |
| kFlag_AdvertisingEnabled = 0x0001, |
| kFlag_FastAdvertisingEnabled = 0x0002, |
| kFlag_Advertising = 0x0004, |
| kFlag_RestartAdvertising = 0x0008, |
| kFlag_EFRBLEStackInitialized = 0x0010, |
| kFlag_DeviceNameSet = 0x0020, |
| }; |
| |
| enum |
| { |
| kMaxConnections = BLE_LAYER_NUM_BLE_ENDPOINTS, |
| kMaxDeviceNameLength = 16, |
| kUnusedIndex = 0xFF, |
| }; |
| |
| struct CHIPoBLEConState |
| { |
| bd_addr address; |
| uint16_t mtu : 10; |
| uint16_t allocated : 1; |
| uint16_t subscribed : 1; |
| uint16_t unused : 4; |
| uint8_t connectionHandle; |
| uint8_t bondingHandle; |
| }; |
| |
| CHIPoBLEConState mBleConnections[kMaxConnections]; |
| uint8_t mIndConfId[kMaxConnections]; |
| CHIPoBLEServiceMode mServiceMode; |
| uint16_t mFlags; |
| char mDeviceName[kMaxDeviceNameLength + 1]; |
| |
| CHIP_ERROR MapBLEError(int bleErr); |
| void DriveBLEState(void); |
| CHIP_ERROR ConfigureAdvertisingData(void); |
| CHIP_ERROR StartAdvertising(void); |
| CHIP_ERROR StopAdvertising(void); |
| void UpdateMtu(volatile struct gecko_cmd_packet *evt); |
| void HandleBootEvent(void); |
| void HandleConnectEvent(volatile struct gecko_cmd_packet *evt); |
| void HandleConnectionCloseEvent(volatile struct gecko_cmd_packet *evt); |
| void HandleWriteEvent(volatile struct gecko_cmd_packet *evt); |
| void HandleTXCharCCCDWrite(volatile struct gecko_cmd_packet *evt); |
| void HandleRXCharWrite(volatile struct gecko_cmd_packet *evt); |
| void HandleTxConfirmationEvent(volatile struct gecko_cmd_packet *evt); |
| void HandleSoftTimerEvent(volatile struct gecko_cmd_packet *evt); |
| bool RemoveConnection(uint8_t connectionHandle); |
| void AddConnection(uint8_t connectionHandle, uint8_t bondingHandle); |
| CHIPoBLEConState *GetConnectionState(uint8_t conId, bool allocate = false); |
| uint8_t GetTimerHandle(uint8_t connectionHandle, bool allocate = false); |
| static void DriveBLEState(intptr_t arg); |
| static void bluetoothStackEventHandler(void *p_arg); |
| }; |
| |
| /** |
| * Returns a reference to the public interface of the BLEManager singleton object. |
| * |
| * Internal components should use this to access features of the BLEManager object |
| * that are common to all platforms. |
| */ |
| inline BLEManager &BLEMgr(void) |
| { |
| return BLEManagerImpl::sInstance; |
| } |
| |
| /** |
| * Returns the platform-specific implementation of the BLEManager singleton object. |
| * |
| * Internal components can use this to gain access to features of the BLEManager |
| * that are specific to the EFR32 platforms. |
| */ |
| inline BLEManagerImpl &BLEMgrImpl(void) |
| { |
| return BLEManagerImpl::sInstance; |
| } |
| |
| inline BleLayer *BLEManagerImpl::_GetBleLayer() const |
| { |
| return (BleLayer *)(this); |
| } |
| |
| inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void) |
| { |
| return mServiceMode; |
| } |
| |
| inline bool BLEManagerImpl::_IsAdvertisingEnabled(void) |
| { |
| return GetFlag(mFlags, kFlag_AdvertisingEnabled); |
| } |
| |
| inline bool BLEManagerImpl::_IsFastAdvertisingEnabled(void) |
| { |
| return GetFlag(mFlags, kFlag_FastAdvertisingEnabled); |
| } |
| |
| } // namespace Internal |
| } // namespace DeviceLayer |
| } // namespace chip |
| |
| #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE |
| |
| #endif // BLE_MANAGER_IMPL_H |