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 #ifndef ADAPTER_DEVICE_CONFIG_H
17 #define ADAPTER_DEVICE_CONFIG_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 #include "base/base_def.h"
24 
25 /*
26  * @brief The Bluetooth subsystem.
27  */
28 namespace OHOS {
29 namespace bluetooth {
30 const std::string SECTION_HOST = "Host";
31 const std::string PROPERTY_DEVICE_ADDR = "Address";
32 const std::string PROPERTY_DEVICE_NAME = "DeviceName";
33 const std::string PROPERTY_CLASS_OF_DEVICE = "ClassOfDevice";
34 const std::string PROPERTY_IO_CAPABILITY = "IOCapability";
35 const std::string PROPERTY_DEVICE_TYPE = "DeviceType";
36 const std::string PROPERTY_SCAN_MODE = "ScanMode";
37 const std::string PROPERTY_BONDABLE_MODE = "BondableMode";
38 const std::string PROPERTY_DISCOVERABLE_TIMEOUT = "DiscoverableTimeout";
39 const std::string PROPERTY_LOCAL_PASSKEY = "LocalPasskey";
40 const std::string PROPERTY_SECURITY_MODE = "SecurityMode";
41 const std::string PROPERTY_LINK_KEY = "LinkKey";
42 const std::string PROPERTY_LINK_KEY_TYPE = "LinkKeyType";
43 const std::string PROPERTY_ALIAS_NAME = "AliasName";
44 const std::string PROPERTY_PAIR_FLAG = "PairFlag";
45 const std::string PROPERTY_BOND_FROM_LOCAL = "BondFromLocal";
46 const std::string PROPERTY_URI = "uri";
47 const std::string PROPERTY_REMOTE_UUIDS = "RemoteUuids";
48 const std::string PROPERTY_BREDR_TURNON = "BrEdrTurnOn";
49 const std::string PROPERTY_BLE_TURNON = "BleTurnOn";
50 
51 const std::string PROPERTY_BLE_ROLES = "BleRoles";
52 const std::string PROPERTY_BLE_MODE_1_LEVEL = "BleModel1Level";
53 const std::string PROPERTY_BLE_MODE_2_LEVEL = "BleModel2Level";
54 const std::string PROPERTY_BLE_SECURITY = "BleSecurity";
55 const std::string PROPERTY_BLE_APPEARANCE = "Appearance";
56 const std::string PROPERTY_BLE_SCAN_MODE = "BleScanMode";
57 const std::string PROPERTY_BLE_LOCAL_ADDR_TYPE = "LocalAddrType";
58 const std::string PROPERTY_BLE_ADDR_TYPE = "BleAddrType";
59 
60 const std::string PROPERTY_BLE_LOCAL_LTK = "LocalLtk";
61 const std::string PROPERTY_BLE_LOCAL_KEY_SIZE = "LocalKeySize";
62 const std::string PROPERTY_BLE_LOCAL_EDIV = "LocalEdiv";
63 const std::string PROPERTY_BLE_LOCAL_RAND = "LocalRand";
64 const std::string PROPERTY_BLE_LOCAL_IRK = "LocalIrk";
65 const std::string PROPERTY_BLE_LOCAL_CSRK = "LocalCsrk";
66 const std::string PROPERTY_BLE_LOCAL_IDENTITY_ADDR = "LocalIdentityAddr";
67 const std::string PROPERTY_BLE_LOCAL_PUBLIC_ADDR = "LocalPublicAddr";
68 const std::string PROPERTY_BLE_LOCAL_SIGN_COUNTER = "LocalSignCounter";
69 
70 const std::string PROPERTY_BLE_PEER_KEY_TYPE = "PeerKeyType";
71 const std::string PROPERTY_BLE_PEER_LTK = "PeerLtk";
72 const std::string PROPERTY_BLE_PEER_KEY_SIZE = "PeerKeySize";
73 const std::string PROPERTY_BLE_PEER_EDIV = "PeerEdiv";
74 const std::string PROPERTY_BLE_PEER_RAND = "PeerRand";
75 const std::string PROPERTY_BLE_PEER_IDENTITY_ADDR_TYPE = "PeerIdentityAddrType";
76 const std::string PROPERTY_BLE_PEER_IDENTITY_ADDR = "PeerIdentityAddr";
77 const std::string PROPERTY_BLE_PEER_IRK = "PeerIrk";
78 const std::string PROPERTY_BLE_PEER_CSRK = "PeerCsrk";
79 const std::string PROPERTY_BLE_PEER_SIGN_COUNTER = "PeerSignCounter";
80 const std::string PROPERTY_BLE_PEER_ADDR_TYPE = "PeerAddrType";
81 
82 const std::string SECTION_BLE_PAIRED_LIST = "Ble Paired Device List";
83 
84 const std::string SECTION_BREDR_PAIRED_LIST = "Classic Paired Device List";
85 
86 const std::string SECTION_GENERIC_ATTRIBUTE_SERVICE = "Generic Attribute Service";
87 const std::string PROPERTY_GATT_TRANSPORT = "GattTransport";
88 const std::string PROPERTY_GATTS_START_HANDLE = "GattsStartHandle";
89 const std::string PROPERTY_GATTS_END_HANDLE = "GattsEndHandle";
90 
91 const std::string PROPERTY_HID_VENDOR_ID = "HidVendorId";
92 const std::string PROPERTY_HID_PRODUCT_ID = "HidProductId";
93 const std::string PROPERTY_HID_VERSION = "HidVersion";
94 const std::string PROPERTY_HID_CTRY_CODE = "HidCtryCode";
95 const std::string PROPERTY_HID_DESC_INFO = "HidDescInfo";
96 
97 /**
98  * @brief BREDR/BLE config.
99  */
100 class IAdapterDeviceConfig {
101 public:
102     virtual ~IAdapterDeviceConfig() = default;
103     /**
104      * @brief Load XML Document from specified path.
105      * @return true Success Load XML Document.
106      * @return false Failed Load XML Document.
107      */
108     virtual bool Load() = 0;
109 
110     /**
111      * @brief Reload XML Document from specified path.
112      * @return true Success reload XML Document.
113      * @return false Failed reload XML Document.
114      */
115     virtual bool Reload() = 0;
116 
117     /**
118      * @brief Load XML Document from specified path.
119      * @param[in] path XML Document path.
120      * @return true Success Load XML Document.
121      * @return false Failed Load XML Document.
122      */
123     virtual bool Save() = 0;
124 
125     /**
126      * @brief Get specified property value.
127      *        Value type is int.
128      * @param[in] section
129      * @param[in] subSection
130      * @param[in] property
131      * @param[out] value Value type is int.
132      * @return true Success get specified property's value.
133      * @return false Failed get specified property's value.
134      */
135     virtual bool GetValue(
136         const std::string &section, const std::string &subSection, const std::string &property, int &value) = 0;
137 
138     /**
139      * @brief Get specified property value.
140      *        Value type is string.
141      * @param[in] section
142      * @param[in] subSection
143      * @param[in] property
144      * @param[out] value Value type is string.
145      * @return true Success get specified property's value.
146      * @return false Failed get specified property's value.
147      */
148     virtual bool GetValue(
149         const std::string &section, const std::string &subSection, const std::string &property, std::string &value) = 0;
150 
151     /**
152      * @brief Get specified property value.
153      *        Value type is bool.
154      * @param[in] section
155      * @param[in] subSection
156      * @param[in] property
157      * @param[out] value Value type is bool.
158      * @return true Success get specified property's value.
159      * @return false Failed get specified property's value.
160      */
161     virtual bool GetValue(
162         const std::string &section, const std::string &subSection, const std::string &property, bool &value) = 0;
163 
164     /**
165      * @brief Set specified property value.
166      *        Value type is int.
167      * @param[in] section
168      * @param[in] subSection
169      * @param[in] property
170      * @param[in] value Value type is const int.
171      * @return true Success set specified property's value.
172      * @return false Failed set specified property's value.
173      */
174     virtual bool SetValue(
175         const std::string &section, const std::string &subSection, const std::string &property, const int &value) = 0;
176 
177     /**
178      * @brief Set specified property value.
179      *        Value type is string.
180      * @param[in] section
181      * @param[in] subSection
182      * @param[in] property
183      * @param[in] value Value type is const string.
184      * @return true Success set specified property's value.
185      * @return false Failed set specified property's value.
186      */
187     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
188         const std::string &value) = 0;
189 
190     /**
191      * @brief Set specified property value.
192      *        Value type is bool.
193      * @param[in] section
194      * @param[in] subSection
195      * @param[in] property
196      * @param[in] value Value type is const bool.
197      * @return true Success set specified property's value.
198      * @return false Failed set specified property's value.
199      */
200     virtual bool SetValue(
201         const std::string &section, const std::string &subSection, const std::string &property, const bool &value) = 0;
202 
203     /**
204      * @brief Get Address
205      * @param[in] section
206      * @param[out] subSections
207      * @return true Specified section has one or Multiple subSections.
208      * @return false Specified section do not has any subSection.
209      */
210     virtual bool GetSubSections(const std::string &section, std::vector<std::string> &subSections) = 0;
211 
212     /**
213      * @brief Remove XML document specified section.
214      * @param[in] section
215      * @param[in] subSection
216      * @return true Success remove XML document specified section.
217      * @return false Failed remove XML document specified section.
218      */
219     virtual bool RemoveSection(const std::string &section, const std::string &subSection) = 0;
220 
221     /**
222      * @brief Get specified property value.
223      * @param[in] section
224      * @param[in] property
225      * @param[out] value Int type value.
226      * @return true Success get specified property's value.
227      * @return false Failed get specified property's value.
228      */
229     virtual bool GetValue(const std::string &section, const std::string &property, int &value) = 0;
230 
231     /**
232      * @brief Get specified property value.
233      * @param[in] section
234      * @param[in] property
235      * @param[out] value String type value.
236      * @return true Success get specified property's value.
237      * @return false Failed get specified property's value.
238      */
239     virtual bool GetValue(const std::string &section, const std::string &property, std::string &value) = 0;
240 
241     /**
242      * @brief Get specified property value.
243      * @param[in] section
244      * @param[in] property
245      * @param[out] value Bool type value.
246      * @return true Success get specified property's value.
247      * @return false Failed get specified property's value.
248      */
249     virtual bool GetValue(const std::string &section, const std::string &property, bool &value) = 0;
250 
251     /**
252      * @brief Set specified property value.
253      *        Value type is int.
254      * @param[in] section
255      * @param[in] property
256      * @param[in] value Value type is const int.
257      * @return true Success set specified property's value.
258      * @return false Failed set specified property's value.
259      */
260     virtual bool SetValue(const std::string &section, const std::string &property, const int &value) = 0;
261 
262     /**
263      * @brief Set specified property value.
264      *        Value type is string.
265      * @param[in] section
266      * @param[in] property
267      * @param[in] value Value type is const string.
268      * @return true Success set specified property's value.
269      * @return false Failed set specified property's value.
270      */
271     virtual bool SetValue(const std::string &section, const std::string &property, const std::string &value) = 0;
272 };
273 
274 class AdapterDeviceConfig : public IAdapterDeviceConfig {
275 public:
276     /**
277      * @brief Get the Instance object
278      * @return IAdapterConfig*
279      */
280     static IAdapterDeviceConfig *GetInstance();
281 
282     /**
283      * @brief Load XML Document from specified path.
284      * @return true Success Load XML Document.
285      * @return false Failed Load XML Document.
286      */
287     virtual bool Load() override;
288 
289     /**
290      * @brief Reload XML Document from specified path.
291      * @return true Success reload XML Document.
292      * @return false Failed reload XML Document.
293      */
294     virtual bool Reload() override;
295 
296     /**
297      * @brief Load XML Document from specified path.
298      * @param[in] path XML Document path.
299      * @return true Success Load XML Document.
300      * @return false Failed Load XML Document.
301      */
302     virtual bool Save() override;
303 
304     /**
305      * @brief Get specified property value.
306      *        Value type is int.
307      * @param[in] section
308      * @param[in] subSection
309      * @param[in] property
310      * @param[out] value Value type is int.
311      * @return true Success get specified property's value.
312      * @return false Failed get specified property's value.
313      */
314     virtual bool GetValue(
315         const std::string &section, const std::string &subSection, const std::string &property, int &value) override;
316 
317     /**
318      * @brief Get specified property value.
319      *        Value type is string.
320      * @param[in] section
321      * @param[in] subSection
322      * @param[in] property
323      * @param[out] value Value type is string.
324      * @return true Success get specified property's value.
325      * @return false Failed get specified property's value.
326      */
327     virtual bool GetValue(const std::string &section, const std::string &subSection, const std::string &property,
328         std::string &value) override;
329 
330     /**
331      * @brief Get specified property value.
332      *        Value type is bool.
333      * @param[in] section
334      * @param[in] subSection
335      * @param[in] property
336      * @param[out] value Value type is bool.
337      * @return true Success get specified property's value.
338      * @return false Failed get specified property's value.
339      */
340     virtual bool GetValue(
341         const std::string &section, const std::string &subSection, const std::string &property, bool &value) override;
342 
343     /**
344      * @brief Set specified property value.
345      *        Value type is int.
346      * @param[in] section
347      * @param[in] subSection
348      * @param[in] property
349      * @param[in] value Value type is const int.
350      * @return true Success set specified property's value.
351      * @return false Failed set specified property's value.
352      */
353     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
354         const int &value) override;
355 
356     /**
357      * @brief Set specified property value.
358      *        Value type is string.
359      * @param[in] section
360      * @param[in] subSection
361      * @param[in] property
362      * @param[in] value Value type is const string.
363      * @return true Success set specified property's value.
364      * @return false Failed set specified property's value.
365      */
366     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
367         const std::string &value) override;
368 
369     /**
370      * @brief Set specified property value.
371      *        Value type is bool.
372      * @param[in] section
373      * @param[in] subSection
374      * @param[in] property
375      * @param[in] value Value type is const bool.
376      * @return true Success set specified property's value.
377      * @return false Failed set specified property's value.
378      */
379     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
380         const bool &value) override;
381 
382     /**
383      * @brief Get Address
384      * @param[in] section
385      * @param[out] subSections
386      * @return true Specified section has one or multiple subSections.
387      * @return false Specified section do not has any subSection.
388      */
389     virtual bool GetSubSections(const std::string &section, std::vector<std::string> &subSections) override;
390 
391     /**
392      * @brief Remove XML document specified section.
393      * @param[in] section
394      * @param[in] subSection
395      * @return true Success remove XML document specified section.
396      * @return false Failed remove XML document specified section.
397      */
398     virtual bool RemoveSection(const std::string &section, const std::string &subSection) override;
399 
400     /**
401      * @brief Get specified property value.
402      * @param[in] section
403      * @param[in] property
404      * @param[out] value Int type value.
405      * @return true Success get specified property's value.
406      * @return false Failed get specified property's value.
407      */
408     virtual bool GetValue(const std::string &section, const std::string &property, int &value) override;
409 
410     /**
411      * @brief Get specified property value.
412      * @param[in] section
413      * @param[in] property
414      * @param[out] value String type value.
415      * @return true Success get specified property's value.
416      * @return false Failed get specified property's value.
417      */
418     virtual bool GetValue(const std::string &section, const std::string &property, std::string &value) override;
419 
420     /**
421      * @brief Get specified property value.
422      * @param[in] section
423      * @param[in] property
424      * @param[out] value Bool type value.
425      * @return true Success get specified property's value.
426      * @return false Failed get specified property's value.
427      */
428     virtual bool GetValue(const std::string &section, const std::string &property, bool &value) override;
429 
430     /**
431      * @brief Set specified property value.
432      *        Value type is int.
433      * @param[in] section
434      * @param[in] property
435      * @param[in] value Value type is const int.
436      * @return true Success set specified property's value.
437      * @return false Failed set specified property's value.
438      */
439     virtual bool SetValue(const std::string &section, const std::string &property, const int &value) override;
440 
441     /**
442      * @brief Set specified property value.
443      *        Value type is string.
444      * @param[in] section
445      * @param[in] property
446      * @param[in] value Value type is const string.
447      * @return true Success set specified property's value.
448      * @return false Failed set specified property's value.
449      */
450     virtual bool SetValue(const std::string &section, const std::string &property, const std::string &value) override;
451 
452 private:
453     /**
454      * @brief Construct a new Adapter Config object
455      */
456     AdapterDeviceConfig();
457 
458     /**
459      * @brief Destroy the Adapter Config object
460      */
461     ~AdapterDeviceConfig();
462 
463     std::mutex mutex_ {};
464     static AdapterDeviceConfig *g_instance;
465     DECLARE_IMPL();
466 };
467 }  // namespace bluetooth
468 }  // namespace OHOS
469 
470 #endif // ADAPTER_DEVICE_CONFIG_H