| /* |
| * |
| * Copyright (c) 2021 Project CHIP Authors |
| * Copyright (c) 2018 Nest Labs, Inc. |
| * All rights reserved. |
| * |
| * 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 PSoC 6 platform. |
| */ |
| |
| #pragma once |
| |
| #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE |
| |
| #include "cycfg_gatt_db.h" |
| #include <wiced_bt_ble.h> |
| #include <wiced_bt_gatt.h> |
| #include <wiced_bt_stack.h> |
| |
| namespace chip { |
| namespace DeviceLayer { |
| namespace Internal { |
| |
| /** |
| * Concrete implementation of the NetworkProvisioningServer singleton object for the PSoC 6 platform. |
| */ |
| class BLEManagerImpl final : public BLEManager, |
| private Ble::BleLayer, |
| private Ble::BlePlatformDelegate, |
| private Ble::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); |
| void _Shutdown() {} |
| bool _IsAdvertisingEnabled(void); |
| CHIP_ERROR _SetAdvertisingEnabled(bool val); |
| bool _IsFastAdvertisingEnabled(void); |
| bool _IsAdvertising(void); |
| CHIP_ERROR _SetAdvertisingMode(BLEAdvertisingMode mode); |
| CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize); |
| CHIP_ERROR _SetDeviceName(const char * deviceName); |
| uint16_t _NumConnections(void); |
| void _OnPlatformEvent(const ChipDeviceEvent * event); |
| ::chip::Ble::BleLayer * _GetBleLayer(void); |
| |
| // ===== Members that implement virtual methods on BlePlatformDelegate. |
| |
| bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, |
| const Ble::ChipBleUUID * charId) override; |
| bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, |
| const Ble::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 Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId, |
| System::PacketBufferHandle data) override; |
| bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId, |
| System::PacketBufferHandle data) override; |
| bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId, |
| System::PacketBufferHandle data) override; |
| bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const Ble::ChipBleUUID * svcId, |
| const Ble::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); |
| |
| public: |
| static BLEManagerImpl sInstance; |
| |
| // ===== Private members reserved for use by this class only. |
| enum class Flags : uint16_t |
| { |
| kFlag_AsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */ |
| kFlag_AdvertisingEnabled = 0x0002, /**< The application has enabled CHIPoBLE advertising. */ |
| kFlag_FastAdvertisingEnabled = 0x0004, /**< The application has enabled fast advertising. */ |
| kFlag_Advertising = 0x0008, /**< The system is currently CHIPoBLE advertising. */ |
| kFlag_AdvertisingRefreshNeeded = |
| 0x0010, /**< The advertising state/configuration has changed, but the SoftDevice has yet to be updated. */ |
| kFlag_DeviceNameSet = 0x0020, |
| kFlag_StackInitialized = 0x0040, |
| }; |
| |
| enum |
| { |
| kMaxConnections = BLE_LAYER_NUM_BLE_ENDPOINTS, |
| }; |
| |
| struct CHIPoBLEConState |
| { |
| // System::PacketBuffer * PendingIndBuf; |
| uint16_t ConId; |
| uint16_t Mtu; |
| bool connected; |
| }; |
| |
| CHIPoBLEConState mCons[kMaxConnections]; |
| uint16_t mNumCons; |
| CHIPoBLEServiceMode mServiceMode; |
| BitFlags<Flags> mFlags; |
| char mDeviceName[kMaxDeviceNameLength + 1]; |
| |
| void DriveBLEState(void); |
| void SetAdvertisingData(void); |
| |
| wiced_bt_gatt_status_t HandleGattConnectEvent(wiced_bt_gatt_connection_status_t * p_conn_status, CHIPoBLEConState * p_conn); |
| wiced_bt_gatt_status_t HandleGattServiceRead(uint16_t conn_id, wiced_bt_gatt_opcode_t opcode, wiced_bt_gatt_read_t * p_read_req, |
| uint16_t len_requested); |
| wiced_bt_gatt_status_t HandleGattServiceReadByTypeHandler(uint16_t conn_id, wiced_bt_gatt_opcode_t opcode, |
| wiced_bt_gatt_read_by_type_t * p_read_req, uint16_t len_requested); |
| wiced_bt_gatt_status_t HandleGattServiceWrite(uint16_t conn_id, wiced_bt_gatt_write_req_t * p_data); |
| wiced_bt_gatt_status_t HandleGattServiceMtuReq(uint16_t conn_id, uint16_t mtu); |
| wiced_bt_gatt_status_t HandleGattServiceIndCfm(uint16_t conn_id, uint16_t handle); |
| wiced_bt_gatt_status_t HandleGattServiceRequestEvent(wiced_bt_gatt_attribute_request_t * p_request, CHIPoBLEConState * p_conn); |
| uint8_t * gatt_alloc_buffer(uint16_t len); |
| void gatt_free_buffer(uint8_t * p_data); |
| static wiced_result_t BLEManagerCallback(wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t * p_event_data); |
| |
| CHIPoBLEConState * AllocConnectionState(uint16_t conId); |
| CHIPoBLEConState * GetConnectionState(uint16_t conId); |
| bool ReleaseConnectionState(uint16_t conId); |
| gatt_db_lookup_table_t * GetGattAttr(uint16_t handle); |
| |
| static void DriveBLEState(intptr_t 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 PSoC 6 platform. |
| */ |
| inline BLEManagerImpl & BLEMgrImpl(void) |
| { |
| return BLEManagerImpl::sInstance; |
| } |
| |
| inline Ble::BleLayer * BLEManagerImpl::_GetBleLayer() |
| { |
| return this; |
| } |
| |
| } // namespace Internal |
| } // namespace DeviceLayer |
| } // namespace chip |
| |
| #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE |