Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2018 Nest Labs, Inc.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * Defines the abstract interface for the Device Layer's
22 : * internal BLEManager object.
23 : */
24 :
25 : #pragma once
26 :
27 : #include <lib/support/CodeUtils.h>
28 : #include <platform/ConnectivityManager.h>
29 :
30 : #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
31 :
32 : namespace chip {
33 : namespace DeviceLayer {
34 : namespace Internal {
35 :
36 : class BLEManagerImpl;
37 :
38 : /**
39 : * Provides control over CHIPoBLE services and connectivity for a chip device.
40 : *
41 : * BLEManager defines the abstract interface of a singleton object that provides
42 : * control over CHIPoBLE services and connectivity for a chip device. BLEManager
43 : * is an internal object that is used by other components with the chip Device
44 : * Layer, but is not directly accessible to the application.
45 : */
46 : class BLEManager
47 : {
48 : using ImplClass = BLEManagerImpl;
49 :
50 : public:
51 : // ===== Members that define the internal interface of the BLEManager
52 :
53 : using CHIPoBLEServiceMode = ConnectivityManager::CHIPoBLEServiceMode;
54 : using BLEAdvertisingMode = ConnectivityManager::BLEAdvertisingMode;
55 :
56 : CHIP_ERROR Init();
57 : void Shutdown();
58 : bool IsAdvertisingEnabled();
59 : CHIP_ERROR SetAdvertisingEnabled(bool val);
60 : bool IsAdvertising();
61 : CHIP_ERROR SetAdvertisingMode(BLEAdvertisingMode mode);
62 : CHIP_ERROR GetDeviceName(char * buf, size_t bufSize);
63 : CHIP_ERROR SetDeviceName(const char * deviceName);
64 : uint16_t NumConnections();
65 : void OnPlatformEvent(const ChipDeviceEvent * event);
66 : chip::Ble::BleLayer * GetBleLayer();
67 :
68 : protected:
69 : // Construction/destruction limited to subclasses.
70 : BLEManager() = default;
71 : ~BLEManager() = default;
72 :
73 : // No copy, move or assignment.
74 : BLEManager(const BLEManager &) = delete;
75 : BLEManager(const BLEManager &&) = delete;
76 : BLEManager & operator=(const BLEManager &) = delete;
77 : };
78 :
79 : /**
80 : * Returns a reference to the public interface of the BLEManager singleton object.
81 : *
82 : * Internal components should use this to access features of the BLEManager object
83 : * that are common to all platforms.
84 : */
85 : extern BLEManager & BLEMgr();
86 :
87 : /**
88 : * Returns the platform-specific implementation of the BLEManager singleton object.
89 : *
90 : * chip applications can use this to gain access to features of the BLEManager
91 : * that are specific to the selected platform.
92 : */
93 : extern BLEManagerImpl & BLEMgrImpl();
94 :
95 : } // namespace Internal
96 : } // namespace DeviceLayer
97 : } // namespace chip
98 :
99 : /* Include a header file containing the implementation of the BLEManager
100 : * object for the selected platform.
101 : */
102 : #ifdef EXTERNAL_BLEMANAGERIMPL_HEADER
103 : #include EXTERNAL_BLEMANAGERIMPL_HEADER
104 : #elif defined(CHIP_DEVICE_LAYER_TARGET)
105 : #define BLEMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/BLEManagerImpl.h>
106 : #include BLEMANAGERIMPL_HEADER
107 : #endif // defined(CHIP_DEVICE_LAYER_TARGET)
108 :
109 : namespace chip {
110 : namespace DeviceLayer {
111 : namespace Internal {
112 :
113 48 : inline CHIP_ERROR BLEManager::Init()
114 : {
115 48 : return static_cast<ImplClass *>(this)->_Init();
116 : }
117 :
118 48 : inline void BLEManager::Shutdown()
119 : {
120 : #if CONFIG_NETWORK_LAYER_BLE
121 48 : GetBleLayer()->Shutdown();
122 : #endif
123 48 : static_cast<ImplClass *>(this)->_Shutdown();
124 48 : }
125 :
126 5 : inline bool BLEManager::IsAdvertisingEnabled()
127 : {
128 5 : return static_cast<ImplClass *>(this)->_IsAdvertisingEnabled();
129 : }
130 :
131 2 : inline CHIP_ERROR BLEManager::SetAdvertisingEnabled(bool val)
132 : {
133 2 : return static_cast<ImplClass *>(this)->_SetAdvertisingEnabled(val);
134 : }
135 :
136 0 : inline bool BLEManager::IsAdvertising()
137 : {
138 0 : return static_cast<ImplClass *>(this)->_IsAdvertising();
139 : }
140 :
141 0 : inline CHIP_ERROR BLEManager::SetAdvertisingMode(BLEAdvertisingMode mode)
142 : {
143 0 : return static_cast<ImplClass *>(this)->_SetAdvertisingMode(mode);
144 : }
145 :
146 0 : inline CHIP_ERROR BLEManager::GetDeviceName(char * buf, size_t bufSize)
147 : {
148 0 : return static_cast<ImplClass *>(this)->_GetDeviceName(buf, bufSize);
149 : }
150 :
151 0 : inline CHIP_ERROR BLEManager::SetDeviceName(const char * deviceName)
152 : {
153 0 : return static_cast<ImplClass *>(this)->_SetDeviceName(deviceName);
154 : }
155 :
156 0 : inline uint16_t BLEManager::NumConnections()
157 : {
158 0 : return static_cast<ImplClass *>(this)->_NumConnections();
159 : }
160 :
161 19 : inline void BLEManager::OnPlatformEvent(const ChipDeviceEvent * event)
162 : {
163 19 : return static_cast<ImplClass *>(this)->_OnPlatformEvent(event);
164 : }
165 :
166 50 : inline chip::Ble::BleLayer * BLEManager::GetBleLayer()
167 : {
168 50 : return static_cast<ImplClass *>(this)->_GetBleLayer();
169 : }
170 :
171 : } // namespace Internal
172 : } // namespace DeviceLayer
173 : } // namespace chip
174 :
175 : #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
|