dd08bab188e90ec988ab137577cdc6c8b33f3b3e
[platform/upstream/connectedhomeip.git] / src / platform / qpg6100 / BLEManagerImpl.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *    @file
20  *          Provides an implementation of the BLEManager singleton object
21  *          for the QPG6100 platforms.
22  */
23
24 #pragma once
25
26 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
27
28 #include "qvCHIP.h"
29
30 namespace chip {
31 namespace DeviceLayer {
32 namespace Internal {
33
34 using namespace chip::Ble;
35
36 /**
37  * Concrete implementation of the NetworkProvisioningServer singleton object for the platform.
38  */
39 class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePlatformDelegate, private BleApplicationDelegate
40 {
41     // Allow the BLEManager interface class to delegate method calls to
42     // the implementation methods provided by this class.
43     friend BLEManager;
44
45 private:
46     // ===== Members that implement the BLEManager internal interface.
47
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);
61
62     // ===== Members that implement virtual methods on BlePlatformDelegate.
63
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;
76
77     // ===== Members that implement virtual methods on BleApplicationDelegate.
78
79     void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
80
81     // ===== Members for internal use by the following friends.
82
83     friend BLEManager & BLEMgr(void);
84     friend BLEManagerImpl & BLEMgrImpl(void);
85
86     static BLEManagerImpl sInstance;
87
88     // ===== Private members reserved for use by this class only.
89
90     enum
91     {
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,
99     };
100
101     enum
102     {
103         kMaxConnections              = BLE_LAYER_NUM_BLE_ENDPOINTS,
104         kMaxDeviceNameLength         = 20, // TODO: right-size this
105         kMaxAdvertisementDataSetSize = 31
106     };
107
108     CHIPoBLEServiceMode mServiceMode;
109     uint16_t mFlags;
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;
116
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);
124
125     void HandleDmMsg(qvCHIP_Ble_DmEvt_t * pDmEvt);
126     void HandleAttMsg(qvCHIP_Ble_AttEvt_t * pAttEvt);
127
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);
135
136     static void DriveBLEState(intptr_t arg);
137
138     /* Handlers for stack events */
139     static void _handleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * event);
140 };
141
142 /**
143  * Returns a reference to the public interface of the BLEManager singleton object.
144  *
145  * Internal components should use this to access features of the BLEManager object
146  * that are common to all platforms.
147  */
148 inline BLEManager & BLEMgr(void)
149 {
150     return BLEManagerImpl::sInstance;
151 }
152
153 /**
154  * Returns the platform-specific implementation of the BLEManager singleton object.
155  *
156  * Internal components can use this to gain access to features of the BLEManager
157  * that are specific to the platform.
158  */
159 inline BLEManagerImpl & BLEMgrImpl(void)
160 {
161     return BLEManagerImpl::sInstance;
162 }
163
164 inline BleLayer * BLEManagerImpl::_GetBleLayer()
165 {
166     return this;
167 }
168
169 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void)
170 {
171     return mServiceMode;
172 }
173
174 inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
175 {
176     return GetFlag(mFlags, kFlag_AdvertisingEnabled);
177 }
178
179 inline bool BLEManagerImpl::_IsFastAdvertisingEnabled(void)
180 {
181     return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
182 }
183
184 inline bool BLEManagerImpl::_IsAdvertising(void)
185 {
186     return GetFlag(mFlags, kFlag_Advertising);
187 }
188
189 } // namespace Internal
190 } // namespace DeviceLayer
191 } // namespace chip
192
193 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE