3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Provides an implementation of the BLEManager singleton object
21 * for the QPG6100 platforms.
26 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
31 namespace DeviceLayer {
34 using namespace chip::Ble;
37 * Concrete implementation of the NetworkProvisioningServer singleton object for the platform.
39 class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePlatformDelegate, private BleApplicationDelegate
41 // Allow the BLEManager interface class to delegate method calls to
42 // the implementation methods provided by this class.
46 // ===== Members that implement the BLEManager internal interface.
48 CHIP_ERROR _Init(void);
49 CHIPoBLEServiceMode _GetCHIPoBLEServiceMode(void);
50 CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
51 bool _IsAdvertisingEnabled(void);
52 CHIP_ERROR _SetAdvertisingEnabled(bool val);
53 bool _IsFastAdvertisingEnabled(void);
54 CHIP_ERROR _SetFastAdvertisingEnabled(bool val);
55 bool _IsAdvertising(void);
56 CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
57 CHIP_ERROR _SetDeviceName(const char * deviceName);
58 uint16_t _NumConnections(void);
59 void _OnPlatformEvent(const ChipDeviceEvent * event);
60 BleLayer * _GetBleLayer(void);
62 // ===== Members that implement virtual methods on BlePlatformDelegate.
64 bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId) override;
65 bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId) override;
66 bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
67 uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
68 bool SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
69 PacketBufferHandle pBuf) override;
70 bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
71 PacketBufferHandle pBuf) override;
72 bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
73 PacketBufferHandle pBuf) override;
74 bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const ChipBleUUID * svcId,
75 const ChipBleUUID * charId) override;
77 // ===== Members that implement virtual methods on BleApplicationDelegate.
79 void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
81 // ===== Members for internal use by the following friends.
83 friend BLEManager & BLEMgr(void);
84 friend BLEManagerImpl & BLEMgrImpl(void);
86 static BLEManagerImpl sInstance;
88 // ===== Private members reserved for use by this class only.
92 kFlag_AsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
93 kFlag_AdvertisingEnabled = 0x0002, /**< The application has enabled CHIPoBLE advertising. */
94 kFlag_FastAdvertisingEnabled = 0x0004, /**< The application has enabled fast advertising. */
95 kFlag_Advertising = 0x0008, /**< The system is currently CHIPoBLE advertising. */
96 kFlag_AdvertisingRefreshNeeded =
97 0x0010, /**< The advertising state/configuration state in the BLE layer needs to be updated. */
98 kFlag_DeviceNameSet = 0x0020,
103 kMaxConnections = BLE_LAYER_NUM_BLE_ENDPOINTS,
104 kMaxDeviceNameLength = 20, // TODO: right-size this
105 kMaxAdvertisementDataSetSize = 31
108 CHIPoBLEServiceMode mServiceMode;
110 char mDeviceName[kMaxDeviceNameLength + 1];
111 uint16_t mNumGAPCons;
112 uint16_t mSubscribedConIds[kMaxConnections];
113 uint8_t mAdvDataBuf[kMaxAdvertisementDataSetSize];
114 uint8_t mScanRespDataBuf[kMaxAdvertisementDataSetSize];
115 qvCHIP_Ble_Callbacks_t appCbacks;
117 void DriveBLEState(void);
118 CHIP_ERROR ConfigureAdvertisingData(void);
119 CHIP_ERROR StartAdvertising(void);
120 CHIP_ERROR StopAdvertising(void);
121 CHIP_ERROR SetSubscribed(uint16_t conId);
122 bool UnsetSubscribed(uint16_t conId);
123 bool IsSubscribed(uint16_t conId);
125 void HandleDmMsg(qvCHIP_Ble_DmEvt_t * pDmEvt);
126 void HandleAttMsg(qvCHIP_Ble_AttEvt_t * pAttEvt);
128 CHIP_ERROR MapBLEError(int bleErr) const;
129 /* Callbacks from BLE stack*/
130 static void ExternalCbHandler(qvCHIP_Ble_MsgHdr_t * pMsg);
131 static void HandleTXCharRead(uint16_t connId, uint16_t handle, uint8_t operation, uint16_t offset, qvCHIP_Ble_Attr_t * pAttr);
132 static void HandleRXCharWrite(uint16_t connId, uint16_t handle, uint8_t operation, uint16_t offset, uint16_t len,
133 uint8_t * pValue, qvCHIP_Ble_Attr_t * pAttr);
134 void HandleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * event);
136 static void DriveBLEState(intptr_t arg);
138 /* Handlers for stack events */
139 static void _handleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * event);
143 * Returns a reference to the public interface of the BLEManager singleton object.
145 * Internal components should use this to access features of the BLEManager object
146 * that are common to all platforms.
148 inline BLEManager & BLEMgr(void)
150 return BLEManagerImpl::sInstance;
154 * Returns the platform-specific implementation of the BLEManager singleton object.
156 * Internal components can use this to gain access to features of the BLEManager
157 * that are specific to the platform.
159 inline BLEManagerImpl & BLEMgrImpl(void)
161 return BLEManagerImpl::sInstance;
164 inline BleLayer * BLEManagerImpl::_GetBleLayer()
169 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void)
174 inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
176 return GetFlag(mFlags, kFlag_AdvertisingEnabled);
179 inline bool BLEManagerImpl::_IsFastAdvertisingEnabled(void)
181 return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
184 inline bool BLEManagerImpl::_IsAdvertising(void)
186 return GetFlag(mFlags, kFlag_Advertising);
189 } // namespace Internal
190 } // namespace DeviceLayer
193 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE