1 /*
2 * Copyright (C) 2023-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 #include "softap_parser.h"
17 #include "wifi_logger.h"
18 #include "wifi_global_func.h"
19 #include "wifi_common_util.h"
20
21 namespace OHOS {
22 namespace Wifi {
23 DEFINE_WIFILOG_LABEL("SoftapParser");
24 const int BAND_2GHZ = 1 << 0;
25 const int SECURITY_TYPE_WPA2_PSK = 1;
26
~SoftapXmlParser()27 SoftapXmlParser::~SoftapXmlParser()
28 {
29 }
30
ParseInternal(xmlNodePtr node)31 bool SoftapXmlParser::ParseInternal(xmlNodePtr node)
32 {
33 if (node == nullptr) {
34 WIFI_LOGE("ParseInternal node null");
35 return false;
36 }
37 if (IsDocValid(node) != true) {
38 WIFI_LOGI("ParseInternal error");
39 return false;
40 }
41 xmlNodePtr softapNode = GotoSoftApNode(node);
42 ParseSoftap(softapNode);
43 return true;
44 }
45
GotoSoftApNode(xmlNodePtr innode)46 xmlNodePtr SoftapXmlParser::GotoSoftApNode(xmlNodePtr innode)
47 {
48 if (innode == nullptr) {
49 WIFI_LOGE("GotoSoftApNode node null");
50 return nullptr;
51 }
52 for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
53 if (GetNodeValue(node) == XML_TAG_HEADER_SOFTAP) {
54 return node;
55 }
56 }
57 return nullptr;
58 }
59
ParseSoftap(xmlNodePtr innode)60 void SoftapXmlParser::ParseSoftap(xmlNodePtr innode)
61 {
62 if (innode == nullptr) {
63 WIFI_LOGE("ParseSoftap node null");
64 return;
65 }
66 for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
67 switch (GetConfigNameAsInt(node)) {
68 case HotspotConfigType::SOFTAP_SSID: {
69 hotspotConfig.SetSsid(GetStringValue(node));
70 WIFI_LOGI("Ssid is %{public}s", SsidAnonymize(hotspotConfig.GetSsid()).c_str());
71 break;
72 }
73 case HotspotConfigType::SECURITYTYPE: {
74 int securityType = GetPrimValue<int>(node, PrimType::INT);
75 WIFI_LOGI("SecurityType is %{public}d", securityType);
76 if (securityType == SECURITY_TYPE_WPA2_PSK) {
77 hotspotConfig.SetSecurityType(KeyMgmt::WPA2_PSK);
78 } else {
79 hotspotConfig.SetSecurityType(KeyMgmt::NONE);
80 }
81 break;
82 }
83 case HotspotConfigType::PASSPHRASE: {
84 hotspotConfig.SetPreSharedKey(GetStringValue(node));
85 WIFI_LOGI("PreSharedKey is %{public}s", PassWordAnonymize(hotspotConfig.GetPreSharedKey()).c_str());
86 break;
87 }
88 default: {
89 break;
90 }
91 }
92 GetBandInfo(node);
93 }
94 hotspotConfig.SetMaxConn(MAX_AP_CONN);
95 }
96
GetConfigNameAsInt(xmlNodePtr node)97 HotspotConfigType SoftapXmlParser::GetConfigNameAsInt(xmlNodePtr node)
98 {
99 if (node == nullptr) {
100 WIFI_LOGE("GetConfigNameAsInt node null");
101 return HotspotConfigType::UNUSED;
102 }
103 std::string tagName = GetNameValue(node);
104 if (g_hotspotConfigMap.find(tagName) != g_hotspotConfigMap.end()) {
105 return g_hotspotConfigMap.at(tagName);
106 }
107 return HotspotConfigType::UNUSED;
108 }
109
GetBandInfo(xmlNodePtr innode)110 void SoftapXmlParser::GetBandInfo(xmlNodePtr innode)
111 {
112 if (innode == nullptr || GetNodeValue(innode) != XML_BAND_CHANNEL_MAP) {
113 return;
114 }
115 for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
116 if (GetNodeValue(node) == XML_BAND_CHANNEL) {
117 TransBandinfo(node);
118 }
119 }
120 }
121
TransBandinfo(xmlNodePtr innode)122 void SoftapXmlParser::TransBandinfo(xmlNodePtr innode)
123 {
124 if (innode == nullptr) {
125 WIFI_LOGE("TransBandinfo node null");
126 return;
127 }
128 for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
129 if (GetNameValue(node) == XML_BAND) {
130 int band = GetPrimValue<int>(node, PrimType::INT);
131 if (band == BAND_2GHZ) {
132 hotspotConfig.SetBand(BandType::BAND_2GHZ);
133 } else {
134 hotspotConfig.SetBand(BandType::BAND_5GHZ);
135 }
136 }
137 }
138 }
139
GetSoftapConfigs()140 std::vector<HotspotConfig> SoftapXmlParser::GetSoftapConfigs()
141 {
142 std::vector<HotspotConfig> hotspotConfigs{};
143 if (hotspotConfig.GetSecurityType() == KeyMgmt::NONE) {
144 WIFI_LOGE("GetSoftapConfigs security is NONE");
145 return hotspotConfigs;
146 }
147 hotspotConfigs.push_back(hotspotConfig);
148 return hotspotConfigs;
149 }
150 }
151 }