Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / qpg6100 / BLEManagerImpl.h
1 /*
2  *
3  *    Copyright (c) 2020-2021 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 BLEManager 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 _IsAdvertising(void);
54     CHIP_ERROR _SetAdvertisingMode(BLEAdvertisingMode mode);
55     CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
56     CHIP_ERROR _SetDeviceName(const char * deviceName);
57     uint16_t _NumConnections(void);
58     void _OnPlatformEvent(const ChipDeviceEvent * event);
59     BleLayer * _GetBleLayer(void);
60
61     // ===== Members that implement virtual methods on BlePlatformDelegate.
62
63     bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId) override;
64     bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId) override;
65     bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
66     uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
67     bool SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
68                         PacketBufferHandle pBuf) override;
69     bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
70                           PacketBufferHandle pBuf) override;
71     bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
72                          PacketBufferHandle pBuf) override;
73     bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const ChipBleUUID * svcId,
74                           const ChipBleUUID * charId) override;
75
76     // ===== Members that implement virtual methods on BleApplicationDelegate.
77
78     void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
79
80     // ===== Members for internal use by the following friends.
81
82     friend BLEManager & BLEMgr(void);
83     friend BLEManagerImpl & BLEMgrImpl(void);
84
85     static BLEManagerImpl sInstance;
86
87     // ===== Private members reserved for use by this class only.
88
89     enum class Flags : uint16_t
90     {
91         kAsyncInitCompleted       = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
92         kAdvertisingEnabled       = 0x0002, /**< The application has enabled CHIPoBLE advertising. */
93         kFastAdvertisingEnabled   = 0x0004, /**< The application has enabled fast advertising. */
94         kAdvertising              = 0x0008, /**< The system is currently CHIPoBLE advertising. */
95         kAdvertisingRefreshNeeded = 0x0010, /**< The advertising state/configuration state in the BLE layer needs to be updated. */
96         kDeviceNameSet            = 0x0020,
97     };
98
99     enum
100     {
101         kMaxConnections              = BLE_LAYER_NUM_BLE_ENDPOINTS,
102         kMaxDeviceNameLength         = 20, // TODO: right-size this
103         kMaxAdvertisementDataSetSize = 31
104     };
105
106     CHIPoBLEServiceMode mServiceMode;
107     BitFlags<Flags> mFlags;
108     char mDeviceName[kMaxDeviceNameLength + 1];
109     uint16_t mNumGAPCons;
110     uint16_t mSubscribedConIds[kMaxConnections];
111     uint8_t mAdvDataBuf[kMaxAdvertisementDataSetSize];
112     uint8_t mScanRespDataBuf[kMaxAdvertisementDataSetSize];
113     qvCHIP_Ble_Callbacks_t appCbacks;
114
115     void DriveBLEState(void);
116     CHIP_ERROR ConfigureAdvertisingData(void);
117     CHIP_ERROR StartAdvertising(void);
118     CHIP_ERROR StopAdvertising(void);
119     CHIP_ERROR SetSubscribed(uint16_t conId);
120     bool UnsetSubscribed(uint16_t conId);
121     bool IsSubscribed(uint16_t conId);
122
123     void HandleDmMsg(qvCHIP_Ble_DmEvt_t * pDmEvt);
124     void HandleAttMsg(qvCHIP_Ble_AttEvt_t * pAttEvt);
125
126     CHIP_ERROR MapBLEError(int bleErr) const;
127     /* Callbacks from BLE stack*/
128     static void ExternalCbHandler(qvCHIP_Ble_MsgHdr_t * pMsg);
129     static void HandleTXCharRead(uint16_t connId, uint16_t handle, uint8_t operation, uint16_t offset, qvCHIP_Ble_Attr_t * pAttr);
130     static void HandleRXCharWrite(uint16_t connId, uint16_t handle, uint8_t operation, uint16_t offset, uint16_t len,
131                                   uint8_t * pValue, qvCHIP_Ble_Attr_t * pAttr);
132     void HandleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * event);
133
134     static void DriveBLEState(intptr_t arg);
135
136     /* Handlers for stack events */
137     static void _handleTXCharCCCDWrite(qvCHIP_Ble_AttsCccEvt_t * event);
138 };
139
140 /**
141  * Returns a reference to the public interface of the BLEManager singleton object.
142  *
143  * Internal components should use this to access features of the BLEManager object
144  * that are common to all platforms.
145  */
146 inline BLEManager & BLEMgr(void)
147 {
148     return BLEManagerImpl::sInstance;
149 }
150
151 /**
152  * Returns the platform-specific implementation of the BLEManager singleton object.
153  *
154  * Internal components can use this to gain access to features of the BLEManager
155  * that are specific to the platform.
156  */
157 inline BLEManagerImpl & BLEMgrImpl(void)
158 {
159     return BLEManagerImpl::sInstance;
160 }
161
162 inline BleLayer * BLEManagerImpl::_GetBleLayer()
163 {
164     return this;
165 }
166
167 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void)
168 {
169     return mServiceMode;
170 }
171
172 inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
173 {
174     return mFlags.Has(Flags::kAdvertisingEnabled);
175 }
176
177 inline bool BLEManagerImpl::_IsAdvertising(void)
178 {
179     return mFlags.Has(Flags::kAdvertising);
180 }
181
182 } // namespace Internal
183 } // namespace DeviceLayer
184 } // namespace chip
185
186 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE