Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Tizen / 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 Linux platforms.
22  */
23
24 #pragma once
25
26 #include <ble/BleLayer.h>
27 #include <platform/internal/BLEManager.h>
28
29 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
30
31 #include "bluez/ChipDeviceScanner.h"
32 #include "bluez/Types.h"
33
34 namespace chip {
35 namespace DeviceLayer {
36 namespace Internal {
37
38 void HandleIncomingBleConnection(Ble::BLEEndPoint * bleEP);
39
40 struct BLEAdvConfig
41 {
42     char * mpBleName;
43     uint32_t mAdapterId;
44     uint8_t mMajor;
45     uint8_t mMinor;
46     uint16_t mVendorId;
47     uint16_t mProductId;
48     uint64_t mDeviceId;
49     uint8_t mPairingStatus;
50     ChipAdvType mType;
51     uint16_t mDuration;
52     const char * mpAdvertisingUUID;
53 };
54
55 enum class BleScanState : uint8_t
56 {
57     kNotScanning,
58     kScanForDiscriminator,
59     kScanForAddress,
60     kConnecting,
61 };
62
63 struct BLEScanConfig
64 {
65     // If an active scan for connection is being performed
66     BleScanState mBleScanState = BleScanState::kNotScanning;
67
68     // If scanning by discriminator, what are we scanning for
69     uint16_t mDiscriminator = 0;
70
71     // If scanning by address, what address are we searching for
72     std::string mAddress;
73
74     // Optional argument to be passed to callback functions provided by the BLE scan/connect requestor
75     void * mAppState = nullptr;
76 };
77
78 /**
79  * Concrete implementation of the BLEManagerImpl singleton object for the Linux platforms.
80  */
81 class BLEManagerImpl final : public BLEManager,
82                              private Ble::BleLayer,
83                              private Ble::BlePlatformDelegate,
84                              private Ble::BleApplicationDelegate,
85                              private Ble::BleConnectionDelegate,
86                              private ChipDeviceScannerDelegate
87 {
88     // Allow the BLEManager interface class to delegate method calls to
89     // the implementation methods provided by this class.
90     friend BLEManager;
91
92 public:
93     CHIP_ERROR ConfigureBle(uint32_t aAdapterId, bool aIsCentral);
94
95     // Driven by BlueZ IO
96     static void HandleNewConnection(BLE_CONNECTION_OBJECT conId);
97     static void HandleConnectFailed(CHIP_ERROR error);
98     static void HandleWriteComplete(BLE_CONNECTION_OBJECT conId);
99     static void HandleSubscribeOpComplete(BLE_CONNECTION_OBJECT conId, bool subscribed);
100     static void HandleTXCharChanged(BLE_CONNECTION_OBJECT conId, const uint8_t * value, size_t len);
101     static void HandleRXCharWrite(BLE_CONNECTION_OBJECT user_data, const uint8_t * value, size_t len);
102     static void CHIPoBluez_ConnectionClosed(BLE_CONNECTION_OBJECT user_data);
103     static void HandleTXCharCCCDWrite(BLE_CONNECTION_OBJECT user_data);
104     static void HandleTXComplete(BLE_CONNECTION_OBJECT user_data);
105
106     static void NotifyBLEPeripheralRegisterAppComplete(bool aIsSuccess, void * apAppstate);
107     static void NotifyBLEPeripheralAdvConfiguredComplete(bool aIsSuccess, void * apAppstate);
108     static void NotifyBLEPeripheralAdvStartComplete(bool aIsSuccess, void * apAppstate);
109     static void NotifyBLEPeripheralAdvStopComplete(bool aIsSuccess, void * apAppstate);
110
111 private:
112     // ===== Members that implement the BLEManager internal interface.
113
114     CHIP_ERROR _Init();
115     CHIPoBLEServiceMode _GetCHIPoBLEServiceMode();
116     CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
117     bool _IsAdvertisingEnabled();
118     CHIP_ERROR _SetAdvertisingEnabled(bool val);
119     bool _IsAdvertising();
120     CHIP_ERROR _SetAdvertisingMode(BLEAdvertisingMode mode);
121     CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
122     CHIP_ERROR _SetDeviceName(const char * deviceName);
123     uint16_t _NumConnections();
124
125     void _OnPlatformEvent(const ChipDeviceEvent * event);
126     void HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * event);
127     BleLayer * _GetBleLayer();
128
129     // ===== Members that implement virtual methods on BlePlatformDelegate.
130
131     bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
132                                  const Ble::ChipBleUUID * charId) override;
133     bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
134                                    const Ble::ChipBleUUID * charId) override;
135     bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
136     uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
137     bool SendIndication(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
138                         System::PacketBufferHandle pBuf) override;
139     bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
140                           System::PacketBufferHandle pBuf) override;
141     bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
142                          System::PacketBufferHandle pBuf) override;
143     bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const Ble::ChipBleUUID * svcId,
144                           const Ble::ChipBleUUID * charId) override;
145
146     // ===== Members that implement virtual methods on BleApplicationDelegate.
147
148     void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
149
150     // ===== Members that implement virtual methods on BleConnectionDelegate.
151
152     void NewConnection(BleLayer * bleLayer, void * appState, uint16_t connDiscriminator) override;
153     BLE_ERROR CancelConnection() override;
154
155     // ===== Members that implement virtual methods on ChipDeviceScannerDelegate
156     void OnDeviceScanned(BluezDevice1 * device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
157     void OnScanComplete() override;
158
159     // ===== Members for internal use by the following friends.
160
161     friend BLEManager & BLEMgr();
162     friend BLEManagerImpl & BLEMgrImpl();
163
164     static BLEManagerImpl sInstance;
165
166     // ===== Private members reserved for use by this class only.
167     enum class Flags : uint16_t
168     {
169         kAsyncInitCompleted       = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
170         kBluezBLELayerInitialized = 0x0002, /**< The Bluez layer has been initialized. */
171         kAppRegistered            = 0x0004, /**< The CHIPoBLE application has been registered with the Bluez layer. */
172         kAdvertisingConfigured    = 0x0008, /**< CHIPoBLE advertising has been configured in the Bluez layer. */
173         kAdvertising              = 0x0010, /**< The system is currently CHIPoBLE advertising. */
174         kControlOpInProgress      = 0x0020, /**< An async control operation has been issued to the ESP BLE layer. */
175         kAdvertisingEnabled       = 0x0040, /**< The application has enabled CHIPoBLE advertising. */
176         kFastAdvertisingEnabled   = 0x0080, /**< The application has enabled fast advertising. */
177         kUseCustomDeviceName      = 0x0100, /**< The application has configured a custom BLE device name. */
178         kAdvertisingRefreshNeeded = 0x0200, /**< The advertising configuration/state in BLE layer needs to be updated. */
179     };
180
181     enum
182     {
183         kMaxConnections             = 1,  // TODO: right max connection
184         kMaxDeviceNameLength        = 20, // TODO: right-size this
185         kMaxAdvertismentDataSetSize = 31  // TODO: verify this
186     };
187
188     CHIP_ERROR StartBLEAdvertising();
189     CHIP_ERROR StopBLEAdvertising();
190
191     void DriveBLEState();
192     static void DriveBLEState(intptr_t arg);
193
194     void InitiateScan(BleScanState scanType);
195     static void InitiateScan(intptr_t arg);
196     void CleanScanConfig();
197
198     CHIPoBLEServiceMode mServiceMode;
199     BLEAdvConfig mBLEAdvConfig;
200     BLEScanConfig mBLEScanConfig;
201     BitFlags<Flags> mFlags;
202     char mDeviceName[kMaxDeviceNameLength + 1];
203     bool mIsCentral            = false;
204     BluezEndpoint * mpEndpoint = nullptr;
205     std::unique_ptr<ChipDeviceScanner> mDeviceScanner;
206 };
207
208 /**
209  * Returns a reference to the public interface of the BLEManager singleton object.
210  *
211  * Internal components should use this to access features of the BLEManager object
212  * that are common to all platforms.
213  */
214 inline BLEManager & BLEMgr()
215 {
216     return BLEManagerImpl::sInstance;
217 }
218
219 /**
220  * Returns the platform-specific implementation of the BLEManager singleton object.
221  *
222  * Internal components can use this to gain access to features of the BLEManager
223  * that are specific to the Linux platforms.
224  */
225 inline BLEManagerImpl & BLEMgrImpl()
226 {
227     return BLEManagerImpl::sInstance;
228 }
229
230 inline Ble::BleLayer * BLEManagerImpl::_GetBleLayer()
231 {
232     return this;
233 }
234
235 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode()
236 {
237     return mServiceMode;
238 }
239
240 inline bool BLEManagerImpl::_IsAdvertisingEnabled()
241 {
242     return mFlags.Has(Flags::kAdvertisingEnabled);
243 }
244
245 inline bool BLEManagerImpl::_IsAdvertising()
246 {
247     return mFlags.Has(Flags::kAdvertising);
248 }
249
250 } // namespace Internal
251 } // namespace DeviceLayer
252 } // namespace chip
253
254 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE