1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines adapter classic properties.
21  *
22  */
23 
24 /**
25  * @file classic_adapter_properties.h
26  *
27  * @brief Adapter classic properties.
28  *
29  */
30 
31 #ifndef CLASSIC_ADAPTER_PROPERTIES_H
32 #define CLASSIC_ADAPTER_PROPERTIES_H
33 
34 #include <mutex>
35 #include <string>
36 #include <vector>
37 
38 #include "base_observer_list.h"
39 #include "bt_uuid.h"
40 #include "btstack.h"
41 #include "classic_bluetooth_data.h"
42 #include "classic_config.h"
43 #include "classic_remote_device.h"
44 #include "dispatcher.h"
45 #include "interface_adapter_classic.h"
46 #include "log.h"
47 #include "log_util.h"
48 #include "raw_address.h"
49 #include "timer.h"
50 
51 namespace OHOS {
52 namespace bluetooth {
53 /**
54  * @brief AdapterProperties to save and get classic properties.
55  *
56  */
57 class ClassicAdapterProperties {
58 public:
59     /**
60      * @brief Get classic adapter properties singleton instance pointer.
61      *
62      * @return Returns the singleton instance pointer.
63      */
64     static ClassicAdapterProperties &GetInstance();
65 
66     /**
67      * @brief Load config info.
68      *
69      * @return Returns <b>true</b> if the operation is successful;
70      *         returns <b>false</b> if the operation fails.
71      */
72     bool LoadConfigInfo();
73 
74     /**
75      * @brief Initialize scan, bondable, security mode.
76      *
77      * @return Returns <b>true</b> if the operation is successful;
78      *         returns <b>false</b> if the operation fails.
79      */
80     bool InitMode();
81 
82     /**
83      * @brief Set local class and name.
84      *
85      * @return Returns <b>true</b> if the operation is successful;
86      *         returns <b>false</b> if the operation fails.
87      */
88     bool ConfigProperties();
89 
90     /**
91      * @brief Set bondable mode.
92      *
93      * @param mode Bondable mode.
94      * @return Returns <b>true</b> if the operation is successful;
95      *         returns <b>false</b> if the operation fails.
96      */
97     bool SetBondableMode(int mode);
98 
99     /**
100      * @brief Set discoverable timeout.
101      *
102      * @param time Discoverable timeout.
103      * @return Returns <b>true</b> if the operation is successful;
104      *         returns <b>false</b> if the operation fails.
105      */
106     bool SetDiscoverableTimeout(int time);
107 
108     /**
109      * @brief Set Io capability.
110      *
111      * @param ioCapability Device Io capability.
112      * @return Returns <b>true</b> if the operation is successful;
113      *         returns <b>false</b> if the operation fails.
114      */
115     bool SetIoCapability(int ioCapability);
116 
117     /**
118      * @brief Set local address.
119      *
120      * @param addr Device address.
121      * @return Returns <b>true</b> if the operation is successful;
122      *         returns <b>false</b> if the operation fails.
123      */
124     bool SetLocalAddress(const std::string &addr);
125 
126     /**
127      * @brief Set local name.
128      *
129      * @param name Device name.
130      * @return Returns <b>true</b> if the operation is successful;
131      *         returns <b>false</b> if the operation fails.
132      */
133     bool SetLocalName(const std::string &name);
134 
135     /**
136      * @brief Set local device class.
137      *
138      * @param deviceClass Device class.
139      * @return Returns <b>true</b> if the operation is successful;
140      *         returns <b>false</b> if the operation fails.
141      */
142     bool SetLocalDeviceClass(int deviceClass);
143 
144     /**
145      * @brief Set security mode.
146      *
147      * @return Returns <b>true</b> if the operation is successful;
148      *         returns <b>false</b> if the operation fails.
149      */
150     bool SetSecurityMode();
151 
152     /**
153      * @brief Get passkey.
154      *
155      * @return Returns passkey.
156      */
157     std::string GetPasskey() const;
158 
159     /**
160      * @brief Get bondable mode.
161      *
162      * @return Returns bondable mode.
163      */
164     int GetBondableMode() const;
165 
166     /**
167      * @brief Get discoverable timeout.
168      *
169      * @return Returns discoverable timeout.
170      */
171     int GetDiscoverableTimeout() const;
172 
173     /**
174      * @brief Get device Io capability.
175      *
176      * @return Returns device Io capability.
177      */
178     int GetIoCapability() const;
179 
180     /**
181      * @brief Get local address.
182      *
183      * @return Returns local address.
184      */
185     std::string GetLocalAddress() const;
186 
187     /**
188      * @brief Get local name.
189      *
190      * @return Returns local name.
191      */
192     std::string GetLocalName() const;
193 
194     /**
195      * @brief Get local device class.
196      *
197      * @return Returns local device class.
198      */
199     int GetLocalDeviceClass() const;
200 
201     /**
202      * @brief Get paired address list.
203      *
204      * @return Returns address list.
205      */
206     std::vector<std::string> GetPairedAddrList() const;
207 
208     /**
209      * @brief Set local name scallback.
210      *
211      * @param result Set result.
212      */
213     void SetLocalNameCallback(int result);
214 
215     /**
216      * @brief Register classic adapter observer.
217      *
218      * @param observer Class ClassicAdapterObserver pointer to register observer.
219      */
220     void RegisterClassicAdapterObserver(IAdapterClassicObserver &observer);
221 
222     /**
223      * @brief Deregister classic adapter observer.
224      *
225      */
226     void DeregisterClassicAdapterObserver(IAdapterClassicObserver &observer);
227 
228     /**
229      * @brief Get paired device.
230      *
231      * @param addr Device address.
232      * @return Returns paired device.
233      */
234     std::shared_ptr<ClassicRemoteDevice> GetPairedDevice(std::string addr);
235 
236     /**
237      * @brief Save paired device info.
238      *
239      * @param remote Remote device.
240      */
241     void SavePairedDeviceInfo(std::shared_ptr<ClassicRemoteDevice> remote);
242 
243     /**
244      * @brief Remove paired device info.
245      *
246      * @param addr Remote device address.
247      */
248     void RemovePairedDeviceInfo(std::string addr) const;
249 
250     /**
251      * @brief Save config file.
252      *
253      */
254     void SaveConfigFile() const;
255 
256     /**
257      * @brief Save local support uuids.
258      *
259      * @param uuids Device uuids.
260      * @return Returns <b>true</b> if the operation is successful;
261      *         returns <b>false</b> if the operation fails.
262      */
263     bool SaveSupportUuids(const std::vector<Uuid> &uuids);
264 
265     bool SetHidPnpInfo(const std::string &addr, int vendorId, int productId, int version);
266     bool SetHidDescInfo(const std::string &addr, int ctryCode, const std::string &descInfo);
267     void GetHidPnpInfo(const std::string &addr, int &vendorId, int &productId, int &version);
268     void GetHidDescInfo(const std::string &addr, int &ctryCode, std::string &descInfo);
269 
270 private:
271     /**
272      * @brief A constructor used to create a <b>ClassicAdapterProperties</b> instance.
273      *
274      */
275     ClassicAdapterProperties();
276 
277     /**
278      * @brief A destructor used to delete the <b>ClassicAdapterProperties</b> instance.
279      *
280      */
281     ~ClassicAdapterProperties();
282 
283     /**
284      * @brief Load host info from config file.
285      *
286      */
287     void LoadHostInfo();
288 
289     /**
290      * @brief Read mac address from controller.
291      *
292      * @return Returns <b>true</b> if the operation is successful;
293      *         returns <b>false</b> if the operation fails.
294      */
295     bool ReadAddrFromController();
296 
297     /**
298      * @brief Update config.
299      *
300      * @param type Properties type.
301      * @return Returns <b>true</b> if the operation is successful;
302      *         returns <b>false</b> if the operation fails.
303      */
304     bool UpdateConfig(int type);
305 
306     /**
307      * @brief Set eir data.
308      *
309      * @return Returns <b>true</b> if the operation is successful;
310      *         returns <b>false</b> if the operation fails.
311      */
312     bool SetEirData();
313 
314     void SendDeviceNameChanged(const std::string &deviceName);
315     void SendDeviceAddrChanged(const std::string &address);
316 
317     ClassicConfig &config_;
318 
319     int cod_ {DEFAULT_CLASS_OF_DEVICE};
320     std::string passkey_ {DEFAULT_PASSKEY};
321     int bondableMode_ {INVALID_VALUE};
322     int securityMode_ {SEC_MODE_2};
323     int ioCapability_ {GAP_IO_DISPLAYYESNO};
324     int discoverableTimeout_ {DEFAULT_SCANMODE_DURATION_MILLIS};
325     std::string macAddr_ {INVALID_MAC_ADDRESS};
326     std::string deviceName_ {DEFAULT_DEVICE_NAME};
327     std::vector<Uuid> uuids_ {};
328     BaseObserverList<IAdapterClassicObserver> adapterObservers_ {};
329     std::recursive_mutex propertiesMutex_ {};
330 };
331 }  // namespace bluetooth
332 }  // namespace OHOS
333 #endif  // CLASSIC_ADAPTER_PROPERTIES_H