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 /**
92  * @brief BREDR/BLE config.
93  */
94 class IAdapterDeviceConfig {
95 public:
96     virtual ~IAdapterDeviceConfig() = default;
97     /**
98      * @brief Load XML Document from specified path.
99      * @return true Success Load XML Document.
100      * @return false Failed Load XML Document.
101      */
102     virtual bool Load() = 0;
103 
104     /**
105      * @brief Reload XML Document from specified path.
106      * @return true Success reload XML Document.
107      * @return false Failed reload XML Document.
108      */
109     virtual bool Reload() = 0;
110 
111     /**
112      * @brief Load XML Document from specified path.
113      * @param[in] path XML Document path.
114      * @return true Success Load XML Document.
115      * @return false Failed Load XML Document.
116      */
117     virtual bool Save() = 0;
118 
119     /**
120      * @brief Get specified property value.
121      *        Value type is int.
122      * @param[in] section
123      * @param[in] subSection
124      * @param[in] property
125      * @param[out] value Value type is int.
126      * @return true Success get specified property's value.
127      * @return false Failed get specified property's value.
128      */
129     virtual bool GetValue(
130         const std::string &section, const std::string &subSection, const std::string &property, int &value) = 0;
131 
132     /**
133      * @brief Get specified property value.
134      *        Value type is string.
135      * @param[in] section
136      * @param[in] subSection
137      * @param[in] property
138      * @param[out] value Value type is string.
139      * @return true Success get specified property's value.
140      * @return false Failed get specified property's value.
141      */
142     virtual bool GetValue(
143         const std::string &section, const std::string &subSection, const std::string &property, std::string &value) = 0;
144 
145     /**
146      * @brief Get specified property value.
147      *        Value type is bool.
148      * @param[in] section
149      * @param[in] subSection
150      * @param[in] property
151      * @param[out] value Value type is bool.
152      * @return true Success get specified property's value.
153      * @return false Failed get specified property's value.
154      */
155     virtual bool GetValue(
156         const std::string &section, const std::string &subSection, const std::string &property, bool &value) = 0;
157 
158     /**
159      * @brief Set specified property value.
160      *        Value type is int.
161      * @param[in] section
162      * @param[in] subSection
163      * @param[in] property
164      * @param[in] value Value type is const int.
165      * @return true Success set specified property's value.
166      * @return false Failed set specified property's value.
167      */
168     virtual bool SetValue(
169         const std::string &section, const std::string &subSection, const std::string &property, const int &value) = 0;
170 
171     /**
172      * @brief Set specified property value.
173      *        Value type is string.
174      * @param[in] section
175      * @param[in] subSection
176      * @param[in] property
177      * @param[in] value Value type is const string.
178      * @return true Success set specified property's value.
179      * @return false Failed set specified property's value.
180      */
181     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
182         const std::string &value) = 0;
183 
184     /**
185      * @brief Set specified property value.
186      *        Value type is bool.
187      * @param[in] section
188      * @param[in] subSection
189      * @param[in] property
190      * @param[in] value Value type is const bool.
191      * @return true Success set specified property's value.
192      * @return false Failed set specified property's value.
193      */
194     virtual bool SetValue(
195         const std::string &section, const std::string &subSection, const std::string &property, const bool &value) = 0;
196 
197     /**
198      * @brief Get Address
199      * @param[in] section
200      * @param[out] subSections
201      * @return true Specified section has one or Multiple subSections.
202      * @return false Specified section do not has any subSection.
203      */
204     virtual bool GetSubSections(const std::string &section, std::vector<std::string> &subSections) = 0;
205 
206     /**
207      * @brief Remove XML document specified section.
208      * @param[in] section
209      * @param[in] subSection
210      * @return true Success remove XML document specified section.
211      * @return false Failed remove XML document specified section.
212      */
213     virtual bool RemoveSection(const std::string &section, const std::string &subSection) = 0;
214 
215     /**
216      * @brief Get specified property value.
217      * @param[in] section
218      * @param[in] property
219      * @param[out] value Int type value.
220      * @return true Success get specified property's value.
221      * @return false Failed get specified property's value.
222      */
223     virtual bool GetValue(const std::string &section, const std::string &property, int &value) = 0;
224 
225     /**
226      * @brief Get specified property value.
227      * @param[in] section
228      * @param[in] property
229      * @param[out] value String type value.
230      * @return true Success get specified property's value.
231      * @return false Failed get specified property's value.
232      */
233     virtual bool GetValue(const std::string &section, const std::string &property, std::string &value) = 0;
234 
235     /**
236      * @brief Get specified property value.
237      * @param[in] section
238      * @param[in] property
239      * @param[out] value Bool type value.
240      * @return true Success get specified property's value.
241      * @return false Failed get specified property's value.
242      */
243     virtual bool GetValue(const std::string &section, const std::string &property, bool &value) = 0;
244 
245     /**
246      * @brief Set specified property value.
247      *        Value type is int.
248      * @param[in] section
249      * @param[in] property
250      * @param[in] value Value type is const int.
251      * @return true Success set specified property's value.
252      * @return false Failed set specified property's value.
253      */
254     virtual bool SetValue(const std::string &section, const std::string &property, const int &value) = 0;
255 
256     /**
257      * @brief Set specified property value.
258      *        Value type is string.
259      * @param[in] section
260      * @param[in] property
261      * @param[in] value Value type is const string.
262      * @return true Success set specified property's value.
263      * @return false Failed set specified property's value.
264      */
265     virtual bool SetValue(const std::string &section, const std::string &property, const std::string &value) = 0;
266 };
267 
268 class AdapterDeviceConfig : public IAdapterDeviceConfig {
269 public:
270     /**
271      * @brief Get the Instance object
272      * @return IAdapterConfig*
273      */
274     static IAdapterDeviceConfig *GetInstance();
275 
276     /**
277      * @brief Load XML Document from specified path.
278      * @return true Success Load XML Document.
279      * @return false Failed Load XML Document.
280      */
281     virtual bool Load() override;
282 
283     /**
284      * @brief Reload XML Document from specified path.
285      * @return true Success reload XML Document.
286      * @return false Failed reload XML Document.
287      */
288     virtual bool Reload() override;
289 
290     /**
291      * @brief Load XML Document from specified path.
292      * @param[in] path XML Document path.
293      * @return true Success Load XML Document.
294      * @return false Failed Load XML Document.
295      */
296     virtual bool Save() override;
297 
298     /**
299      * @brief Get specified property value.
300      *        Value type is int.
301      * @param[in] section
302      * @param[in] subSection
303      * @param[in] property
304      * @param[out] value Value type is int.
305      * @return true Success get specified property's value.
306      * @return false Failed get specified property's value.
307      */
308     virtual bool GetValue(
309         const std::string &section, const std::string &subSection, const std::string &property, int &value) override;
310 
311     /**
312      * @brief Get specified property value.
313      *        Value type is string.
314      * @param[in] section
315      * @param[in] subSection
316      * @param[in] property
317      * @param[out] value Value type is string.
318      * @return true Success get specified property's value.
319      * @return false Failed get specified property's value.
320      */
321     virtual bool GetValue(const std::string &section, const std::string &subSection, const std::string &property,
322         std::string &value) override;
323 
324     /**
325      * @brief Get specified property value.
326      *        Value type is bool.
327      * @param[in] section
328      * @param[in] subSection
329      * @param[in] property
330      * @param[out] value Value type is bool.
331      * @return true Success get specified property's value.
332      * @return false Failed get specified property's value.
333      */
334     virtual bool GetValue(
335         const std::string &section, const std::string &subSection, const std::string &property, bool &value) override;
336 
337     /**
338      * @brief Set specified property value.
339      *        Value type is int.
340      * @param[in] section
341      * @param[in] subSection
342      * @param[in] property
343      * @param[in] value Value type is const int.
344      * @return true Success set specified property's value.
345      * @return false Failed set specified property's value.
346      */
347     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
348         const int &value) override;
349 
350     /**
351      * @brief Set specified property value.
352      *        Value type is string.
353      * @param[in] section
354      * @param[in] subSection
355      * @param[in] property
356      * @param[in] value Value type is const string.
357      * @return true Success set specified property's value.
358      * @return false Failed set specified property's value.
359      */
360     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
361         const std::string &value) override;
362 
363     /**
364      * @brief Set specified property value.
365      *        Value type is bool.
366      * @param[in] section
367      * @param[in] subSection
368      * @param[in] property
369      * @param[in] value Value type is const bool.
370      * @return true Success set specified property's value.
371      * @return false Failed set specified property's value.
372      */
373     virtual bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
374         const bool &value) override;
375 
376     /**
377      * @brief Get Address
378      * @param[in] section
379      * @param[out] subSections
380      * @return true Specified section has one or multiple subSections.
381      * @return false Specified section do not has any subSection.
382      */
383     virtual bool GetSubSections(const std::string &section, std::vector<std::string> &subSections) override;
384 
385     /**
386      * @brief Remove XML document specified section.
387      * @param[in] section
388      * @param[in] subSection
389      * @return true Success remove XML document specified section.
390      * @return false Failed remove XML document specified section.
391      */
392     virtual bool RemoveSection(const std::string &section, const std::string &subSection) override;
393 
394     /**
395      * @brief Get specified property value.
396      * @param[in] section
397      * @param[in] property
398      * @param[out] value Int type value.
399      * @return true Success get specified property's value.
400      * @return false Failed get specified property's value.
401      */
402     virtual bool GetValue(const std::string &section, const std::string &property, int &value) override;
403 
404     /**
405      * @brief Get specified property value.
406      * @param[in] section
407      * @param[in] property
408      * @param[out] value String type value.
409      * @return true Success get specified property's value.
410      * @return false Failed get specified property's value.
411      */
412     virtual bool GetValue(const std::string &section, const std::string &property, std::string &value) override;
413 
414     /**
415      * @brief Get specified property value.
416      * @param[in] section
417      * @param[in] property
418      * @param[out] value Bool type value.
419      * @return true Success get specified property's value.
420      * @return false Failed get specified property's value.
421      */
422     virtual bool GetValue(const std::string &section, const std::string &property, bool &value) override;
423 
424     /**
425      * @brief Set specified property value.
426      *        Value type is int.
427      * @param[in] section
428      * @param[in] property
429      * @param[in] value Value type is const int.
430      * @return true Success set specified property's value.
431      * @return false Failed set specified property's value.
432      */
433     virtual bool SetValue(const std::string &section, const std::string &property, const int &value) override;
434 
435     /**
436      * @brief Set specified property value.
437      *        Value type is string.
438      * @param[in] section
439      * @param[in] property
440      * @param[in] value Value type is const string.
441      * @return true Success set specified property's value.
442      * @return false Failed set specified property's value.
443      */
444     virtual bool SetValue(const std::string &section, const std::string &property, const std::string &value) override;
445 
446 private:
447     /**
448      * @brief Construct a new Adapter Config object
449      */
450     AdapterDeviceConfig();
451 
452     /**
453      * @brief Destroy the Adapter Config object
454      */
455     ~AdapterDeviceConfig();
456 
457     std::mutex mutex_ {};
458     static AdapterDeviceConfig *g_instance;
459     DECLARE_IMPL();
460 };
461 }  // namespace bluetooth
462 }  // namespace OHOS
463 
464 #endif // ADAPTER_DEVICE_CONFIG_H