1 /*
2  * Copyright (c) 2023 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 AUDIO_DEVICE_PARSER_H
17 #define AUDIO_DEVICE_PARSER_H
18 
19 #include <list>
20 #include <unordered_map>
21 #include <string>
22 #include <sstream>
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25 
26 #include "audio_policy_log.h"
27 #include "audio_info.h"
28 #include "iport_observer.h"
29 #include "parser.h"
30 #include "audio_device_manager.h"
31 
32 namespace OHOS {
33 namespace AudioStandard {
34 static const std::string PRIVACY_TYPE = "privacy";
35 static const std::string PUBLIC_TYPE = "public";
36 static const std::string NEGATIVE_TYPE = "negative";
37 
38 enum DeviceNodeName {
39     UNKNOWN_NODE = -1,
40     ADAPTER,
41     DEVICES,
42     DEVICE,
43 };
44 
45 static std::map<std::string, DeviceType> deviceTypeMap_ = {
46     {"DEVICE_TYPE_WIRED_HEADSET", DEVICE_TYPE_WIRED_HEADSET},
47     {"DEVICE_TYPE_WIRED_HEADPHONES", DEVICE_TYPE_WIRED_HEADPHONES},
48     {"DEVICE_TYPE_BLUETOOTH_SCO", DEVICE_TYPE_BLUETOOTH_SCO},
49     {"DEVICE_TYPE_BLUETOOTH_A2DP", DEVICE_TYPE_BLUETOOTH_A2DP},
50     {"DEVICE_TYPE_USB_HEADSET", DEVICE_TYPE_USB_HEADSET},
51     {"DEVICE_TYPE_DP", DEVICE_TYPE_DP},
52     {"DEVICE_TYPE_USB_ARM_HEADSET", DEVICE_TYPE_USB_ARM_HEADSET},
53 };
54 
55 class AudioDeviceParser : public Parser {
56 public:
57     static constexpr char DEVICE_CONFIG_FILE[] = "system/etc/audio/audio_device_privacy.xml";
58 
59     bool LoadConfiguration() final;
60     bool Parse() final;
61     void Destroy() final;
62 
AudioDeviceParser(AudioDeviceManager * audioDeviceManager)63     AudioDeviceParser(AudioDeviceManager *audioDeviceManager)
64     {
65         audioDeviceManager_ = audioDeviceManager;
66     }
67 
~AudioDeviceParser()68     virtual ~AudioDeviceParser()
69     {
70         AUDIO_INFO_LOG("AudioDeviceParser dtor");
71         Destroy();
72     }
73 
74 private:
75     DeviceNodeName GetDeviceNodeNameAsInt(xmlNode *node);
76     bool ParseInternal(xmlNode *node);
77     void ParseDevicePrivacyInfo(xmlNode *node, std::list<DevicePrivacyInfo> &deviceLists);
78     void ParserDevicePrivacyInfoList(xmlNode *node, std::list<DevicePrivacyInfo> &deviceLists);
79     void ParseAudioDevicePrivacyType(xmlNode *node, AudioDevicePrivacyType &deviceType);
80     void ParseDeviceRole(const std::string &deviceRole, uint32_t &deviceRoleFlag);
81     void ParseDeviceCategory(const std::string &deviceCategory, uint32_t &deviceCategoryFlag);
82     void ParseDeviceUsage(const std::string &deviceUsage, uint32_t &deviceUsageFlag);
83     std::string ExtractPropertyValue(const std::string &propName, xmlNode *node);
84     AudioDevicePrivacyType GetDevicePrivacyType(const std::string &devicePrivacyType);
85 
86     xmlDoc *mDoc_ = nullptr;
87     AudioDevicePrivacyType devicePrivacyType_ = {};
88     AudioDeviceManager *audioDeviceManager_;
89     std::unordered_map<AudioDevicePrivacyType, std::list<DevicePrivacyInfo>> devicePrivacyMaps_ = {};
90 };
91 } // namespace AudioStandard
92 } // namespace OHOS
93 
94 #endif // AUDIO_DEVICE_PARSER_H
95