1 /*
2 * Copyright (c) 2024 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 "link_info.h"
17 #include "conn_log.h"
18 #include "softbus_error_code.h"
19 #include "protocol/wifi_direct_protocol.h"
20 #include "utils/wifi_direct_utils.h"
21
22 namespace OHOS::SoftBus {
23 template<> InfoContainer<LinkInfoKey>::KeyTypeTable InfoContainer<LinkInfoKey>::keyTypeTable_ = {
24 { LinkInfoKey::LOCAL_INTERFACE, Serializable::ValueType::STRING },
25 { LinkInfoKey::REMOTE_INTERFACE, Serializable::ValueType::STRING },
26 { LinkInfoKey::LOCAL_LINK_MODE, Serializable::ValueType::INT },
27 { LinkInfoKey::REMOTE_LINK_MODE, Serializable::ValueType::INT },
28 { LinkInfoKey::CENTER_20M, Serializable::ValueType::INT },
29 { LinkInfoKey::CENTER_FREQUENCY1, Serializable::ValueType::INT },
30 { LinkInfoKey::CENTER_FREQUENCY2, Serializable::ValueType::INT },
31 { LinkInfoKey::BANDWIDTH, Serializable::ValueType::INT },
32 { LinkInfoKey::SSID, Serializable::ValueType::STRING },
33 { LinkInfoKey::BSSID, Serializable::ValueType::STRING },
34 { LinkInfoKey::PSK, Serializable::ValueType::STRING },
35 { LinkInfoKey::IS_DHCP, Serializable::ValueType::BOOL },
36 { LinkInfoKey::LOCAL_IPV4, Serializable::ValueType::IPV4_INFO },
37 { LinkInfoKey::REMOTE_IPV4, Serializable::ValueType::IPV4_INFO },
38 { LinkInfoKey::AUTH_PORT, Serializable::ValueType::INT },
39 { LinkInfoKey::MAX_PHYSICAL_RATE, Serializable::ValueType::INT },
40 { LinkInfoKey::REMOTE_DEVICE, Serializable::ValueType::STRING },
41 { LinkInfoKey::STATUS, Serializable::ValueType::INT },
42 { LinkInfoKey::LOCAL_BASE_MAC, Serializable::ValueType::STRING },
43 { LinkInfoKey::REMOTE_BASE_MAC, Serializable::ValueType::STRING },
44 { LinkInfoKey::LOCAL_IPV6, Serializable::ValueType::STRING },
45 { LinkInfoKey::REMOTE_IPV6, Serializable::ValueType::STRING },
46 { LinkInfoKey::CUSTOM_PORT, Serializable::ValueType::INT },
47 { LinkInfoKey::IPADDR_TYPE, Serializable::ValueType::INT },
48 };
49
LinkInfo(const std::string & localInterface,const std::string & remoteInterface,LinkMode localMode,LinkMode remoteMode)50 LinkInfo::LinkInfo(const std::string &localInterface, const std::string &remoteInterface, LinkMode localMode,
51 LinkMode remoteMode)
52 {
53 SetLocalInterface(localInterface);
54 SetRemoteInterface(remoteInterface);
55 SetLocalLinkMode(localMode);
56 SetRemoteLinkMode(remoteMode);
57 }
58
Marshalling(WifiDirectProtocol & protocol,std::vector<uint8_t> & output) const59 int LinkInfo::Marshalling(WifiDirectProtocol &protocol, std::vector<uint8_t> &output) const
60 {
61 for (const auto &[key, value] : values_) {
62 auto type = keyTypeTable_[key];
63 switch (type) {
64 case Serializable::ValueType::BOOL: {
65 uint8_t data = std::any_cast<bool>(value);
66 protocol.Write(static_cast<int>(key), type, &data, sizeof(data));
67 }
68 break;
69 case Serializable::ValueType::INT: {
70 std::vector<uint8_t> data;
71 WifiDirectUtils::IntToBytes(std::any_cast<int>(value), sizeof(int), data);
72 protocol.Write(static_cast<int>(key), type, data.data(), data.size());
73 }
74 break;
75 case Serializable::ValueType::STRING: {
76 const auto &data = std::any_cast<const std::string &>(value);
77 protocol.Write(static_cast<int>(key), type, (uint8_t *)data.c_str(), data.length());
78 }
79 break;
80 case Serializable::ValueType::IPV4_INFO: {
81 const auto &ipv4Info = std::any_cast<const Ipv4Info &>(value);
82 std::vector<uint8_t> ipv4InfoOutput;
83 ipv4Info.Marshalling(ipv4InfoOutput);
84 protocol.Write(static_cast<int>(key), type, ipv4InfoOutput.data(), ipv4InfoOutput.size());
85 }
86 break;
87 default:
88 continue;
89 }
90 }
91
92 protocol.GetOutput(output);
93 return SOFTBUS_OK;
94 }
95
Unmarshalling(WifiDirectProtocol & protocol,const std::vector<uint8_t> & input)96 int LinkInfo::Unmarshalling(WifiDirectProtocol &protocol, const std::vector<uint8_t> &input)
97 {
98 int key = 0;
99 uint8_t *data = nullptr;
100 size_t size = 0;
101
102 protocol.SetInput(input);
103 while (protocol.Read(key, data, size)) {
104 auto valueType = keyTypeTable_[LinkInfoKey(key)];
105 switch (valueType) {
106 case Serializable::ValueType::BOOL:
107 Set(LinkInfoKey(key), *reinterpret_cast<bool *>(data));
108 break;
109 case Serializable::ValueType::INT:
110 Set(LinkInfoKey(key), *reinterpret_cast<int *>(data));
111 break;
112 case Serializable::ValueType::STRING:
113 size = WifiDirectUtils::CalculateStringLength((char *)data, size);
114 Set(LinkInfoKey(key), std::string(reinterpret_cast<const char *>(data), size));
115 break;
116 case Serializable::ValueType::IPV4_INFO: {
117 Ipv4Info ipv4Info;
118 ipv4Info.Unmarshalling(data, size);
119 Set(LinkInfoKey(key), ipv4Info);
120 }
121 break;
122 default:
123 continue;
124 }
125 }
126
127 return SOFTBUS_OK;
128 }
129
SetLocalInterface(const std::string & interface)130 void LinkInfo::SetLocalInterface(const std::string &interface)
131 {
132 Set(LinkInfoKey::LOCAL_INTERFACE, interface);
133 }
134
GetLocalInterface() const135 std::string LinkInfo::GetLocalInterface() const
136 {
137 return Get(LinkInfoKey::LOCAL_INTERFACE, std::string(""));
138 }
139
SetRemoteInterface(const std::string & interface)140 void LinkInfo::SetRemoteInterface(const std::string &interface)
141 {
142 Set(LinkInfoKey::REMOTE_INTERFACE, interface);
143 }
144
GetRemoteInterface() const145 std::string LinkInfo::GetRemoteInterface() const
146 {
147 return Get(LinkInfoKey::REMOTE_INTERFACE, std::string(""));
148 }
149
SetLocalLinkMode(LinkMode mode)150 void LinkInfo::SetLocalLinkMode(LinkMode mode)
151 {
152 Set(LinkInfoKey::LOCAL_LINK_MODE, static_cast<int>(mode));
153 }
154
GetLocalLinkMode() const155 LinkInfo::LinkMode LinkInfo::GetLocalLinkMode() const
156 {
157 auto value = Get(LinkInfoKey::LOCAL_LINK_MODE, static_cast<int>(LinkMode::INVALID));
158 return static_cast<LinkInfo::LinkMode>(value);
159 }
160
SetRemoteLinkMode(LinkMode mode)161 void LinkInfo::SetRemoteLinkMode(LinkMode mode)
162 {
163 Set(LinkInfoKey::REMOTE_LINK_MODE, static_cast<int>(mode));
164 }
165
GetRemoteLinkMode() const166 LinkInfo::LinkMode LinkInfo::GetRemoteLinkMode() const
167 {
168 auto value = Get(LinkInfoKey::REMOTE_LINK_MODE, static_cast<int>(LinkMode::INVALID));
169 return static_cast<LinkInfo::LinkMode>(value);
170 }
171
SetCenter20M(int freq)172 void LinkInfo::SetCenter20M(int freq)
173 {
174 Set(LinkInfoKey::CENTER_20M, freq);
175 }
176
GetCenter20M() const177 int LinkInfo::GetCenter20M() const
178 {
179 return Get(LinkInfoKey::CENTER_20M, 0);
180 }
181
SetCenterFrequency1(int freq)182 void LinkInfo::SetCenterFrequency1(int freq)
183 {
184 Set(LinkInfoKey::CENTER_FREQUENCY1, freq);
185 }
186
GetCenterFrequency1() const187 int LinkInfo::GetCenterFrequency1() const
188 {
189 return Get(LinkInfoKey::CENTER_FREQUENCY1, 0);
190 }
191
SetCenterFrequency2(int freq)192 void LinkInfo::SetCenterFrequency2(int freq)
193 {
194 Set(LinkInfoKey::CENTER_FREQUENCY2, freq);
195 }
196
GetCenterFrequency2() const197 int LinkInfo::GetCenterFrequency2() const
198 {
199 return Get(LinkInfoKey::CENTER_FREQUENCY2, 0);
200 }
201
SetBandWidth(int bandWidth)202 void LinkInfo::SetBandWidth(int bandWidth)
203 {
204 Set(LinkInfoKey::BANDWIDTH, bandWidth);
205 }
206
GetBandWidth() const207 int LinkInfo::GetBandWidth() const
208 {
209 return Get(LinkInfoKey::BANDWIDTH, 0);
210 }
211
SetSsid(const std::string & ssid)212 void LinkInfo::SetSsid(const std::string &ssid)
213 {
214 Set(LinkInfoKey::SSID, ssid);
215 }
216
GetSsid() const217 std::string LinkInfo::GetSsid() const
218 {
219 return Get(LinkInfoKey::SSID, std::string(""));
220 }
221
SetBssid(const std::string & bssid)222 void LinkInfo::SetBssid(const std::string &bssid)
223 {
224 Set(LinkInfoKey::BSSID, bssid);
225 }
226
GetBssid() const227 std::string LinkInfo::GetBssid() const
228 {
229 return Get(LinkInfoKey::BSSID, std::string(""));
230 }
231
SetPsk(const std::string & psk)232 void LinkInfo::SetPsk(const std::string &psk)
233 {
234 Set(LinkInfoKey::PSK, psk);
235 }
236
GetPsk() const237 std::string LinkInfo::GetPsk() const
238 {
239 return Get(LinkInfoKey::PSK, std::string(""));
240 }
241
SetIsDhcp(bool isDhcp)242 void LinkInfo::SetIsDhcp(bool isDhcp)
243 {
244 Set(LinkInfoKey::IS_DHCP, isDhcp);
245 }
246
GetIsDhcp() const247 bool LinkInfo::GetIsDhcp() const
248 {
249 return Get(LinkInfoKey::IS_DHCP, false);
250 }
251
SetLocalIpv4Info(const Ipv4Info & ipv4Info)252 void LinkInfo::SetLocalIpv4Info(const Ipv4Info &ipv4Info)
253 {
254 Set(LinkInfoKey::LOCAL_IPV4, (const std::any &)ipv4Info);
255 }
256
GetLocalIpv4Info() const257 Ipv4Info LinkInfo::GetLocalIpv4Info() const
258 {
259 return Get(LinkInfoKey::LOCAL_IPV4, Ipv4Info());
260 }
261
SetRemoteIpv4Info(const Ipv4Info & ipv4Info)262 void LinkInfo::SetRemoteIpv4Info(const Ipv4Info &ipv4Info)
263 {
264 Set(LinkInfoKey::REMOTE_IPV4, (const std::any &)ipv4Info);
265 }
266
GetRemoteIpv4Info() const267 Ipv4Info LinkInfo::GetRemoteIpv4Info() const
268 {
269 return Get(LinkInfoKey::REMOTE_IPV4, Ipv4Info());
270 }
271
SetAuthPort(int port)272 void LinkInfo::SetAuthPort(int port)
273 {
274 Set(LinkInfoKey::AUTH_PORT, port);
275 }
276
GetAuthPort() const277 int LinkInfo::GetAuthPort() const
278 {
279 return Get(LinkInfoKey::AUTH_PORT, 0);
280 }
281
SetMaxPhysicalRate(int rate)282 void LinkInfo::SetMaxPhysicalRate(int rate)
283 {
284 Set(LinkInfoKey::MAX_PHYSICAL_RATE, rate);
285 }
286
GetMaxPhysicalRate() const287 int LinkInfo::GetMaxPhysicalRate() const
288 {
289 return Get(LinkInfoKey::MAX_PHYSICAL_RATE, 0);
290 }
291
SetRemoteDevice(const std::string & device)292 void LinkInfo::SetRemoteDevice(const std::string &device)
293 {
294 Set(LinkInfoKey::REMOTE_DEVICE, device);
295 }
296
GetRemoteDevice() const297 std::string LinkInfo::GetRemoteDevice() const
298 {
299 return Get(LinkInfoKey::REMOTE_DEVICE, std::string());
300 }
301
SetStatus(int status)302 void LinkInfo::SetStatus(int status)
303 {
304 Set(LinkInfoKey::STATUS, status);
305 }
306
GetStatus() const307 int LinkInfo::GetStatus() const
308 {
309 return Get(LinkInfoKey::STATUS, 0);
310 }
311
SetLocalBaseMac(const std::string & mac)312 void LinkInfo::SetLocalBaseMac(const std::string &mac)
313 {
314 Set(LinkInfoKey::LOCAL_BASE_MAC, mac);
315 }
316
GetLocalBaseMac() const317 std::string LinkInfo::GetLocalBaseMac() const
318 {
319 return Get(LinkInfoKey::LOCAL_BASE_MAC, std::string());
320 }
321
SetRemoteBaseMac(const std::string & mac)322 void LinkInfo::SetRemoteBaseMac(const std::string &mac)
323 {
324 Set(LinkInfoKey::REMOTE_BASE_MAC, mac);
325 }
326
GetRemoteBaseMac() const327 std::string LinkInfo::GetRemoteBaseMac() const
328 {
329 return Get(LinkInfoKey::REMOTE_BASE_MAC, std::string());
330 }
331
SetIsClient(bool client)332 void LinkInfo::SetIsClient(bool client)
333 {
334 Set(LinkInfoKey::IS_CLIENT, client);
335 }
336
GetIsClient() const337 bool LinkInfo::GetIsClient() const
338 {
339 return Get(LinkInfoKey::IS_CLIENT, false);
340 }
341
SetLocalIpv6(const std::string & value)342 void LinkInfo::SetLocalIpv6(const std::string &value)
343 {
344 Set(LinkInfoKey::LOCAL_IPV6, value);
345 }
346
GetLocalIpv6() const347 std::string LinkInfo::GetLocalIpv6() const
348 {
349 return Get(LinkInfoKey::LOCAL_IPV6, std::string());
350 }
351
SetRemoteIpv6(const std::string & value)352 void LinkInfo::SetRemoteIpv6(const std::string &value)
353 {
354 Set(LinkInfoKey::REMOTE_IPV6, value);
355 }
356
GetRemoteIpv6() const357 std::string LinkInfo::GetRemoteIpv6() const
358 {
359 return Get(LinkInfoKey::REMOTE_IPV6, std::string());
360 }
361
SetCustomPort(int value)362 void LinkInfo::SetCustomPort(int value)
363 {
364 Set(LinkInfoKey::CUSTOM_PORT, value);
365 }
366
GetCustomPort()367 int LinkInfo::GetCustomPort()
368 {
369 return Get(LinkInfoKey::CUSTOM_PORT, 0);
370 }
371
SetIpAddrType(enum IpAddrType value)372 void LinkInfo::SetIpAddrType(enum IpAddrType value)
373 {
374 Set(LinkInfoKey::IPADDR_TYPE, static_cast<int>(value));
375 }
376
GetIpAddrType()377 enum IpAddrType LinkInfo::GetIpAddrType()
378 {
379 auto ret = Get(LinkInfoKey::IPADDR_TYPE, 0);
380 return static_cast<enum IpAddrType>(ret);
381 }
382 }
383