blob: 64ecf3edafe28c98582735b7451d69f96994ac26 [file] [log] [blame]
/*
*
* Copyright (c) 2020 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 P64343W 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 P64343W 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);
#if 0
CHIP_ERROR _SetFastAdvertisingEnabled(bool val);
#endif
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,
};
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];
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_read_t * p_read_data);
wiced_bt_gatt_status_t HandleGattServiceWrite(uint16_t conn_id, wiced_bt_gatt_write_t * p_data);
wiced_bt_gatt_status_t HandleGattServiceMtuReq(wiced_bt_gatt_attribute_request_t * p_data, CHIPoBLEConState * p_conn);
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);
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 P64343W 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