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 #include "wifi_config_info.h"
16 #include "conn_log.h"
17 #include "protocol/wifi_direct_protocol_factory.h"
18 #include "softbus_error_code.h"
19 
20 namespace OHOS::SoftBus {
21 template <>
22 InfoContainer<WifiConfigInfoKey>::KeyTypeTable InfoContainer<WifiConfigInfoKey>::keyTypeTable_ = {
23     {WifiConfigInfoKey::INTERFACE_INFO_ARRAY, Serializable::ValueType::INTERFACE_INFO_ARRAY},
24     { WifiConfigInfoKey::DEVICE_ID,           Serializable::ValueType::STRING              },
25 };
26 
WifiConfigInfo(std::vector<uint8_t> & config)27 WifiConfigInfo::WifiConfigInfo(std::vector<uint8_t> &config)
28 {
29     auto pro = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::TLV);
30     if (pro == nullptr) {
31         CONN_LOGE(CONN_WIFI_DIRECT, "create tlv protocol failed");
32         return;
33     }
34     pro->SetFormat(ProtocolFormat { TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE1 });
35     Unmarshalling(*pro, std::vector<uint8_t>(config.begin() + HEADER_LEN, config.end()));
36 }
37 
Unmarshalling(WifiDirectProtocol & protocol,const std::vector<uint8_t> & input)38 int WifiConfigInfo::Unmarshalling(WifiDirectProtocol &protocol, const std::vector<uint8_t> &input)
39 {
40     int key = 0;
41     uint8_t *data = nullptr;
42     size_t size = 0;
43 
44     protocol.SetInput(input);
45     while (protocol.Read(key, data, size)) {
46         auto type = keyTypeTable_[static_cast<WifiConfigInfoKey>(key)];
47         switch (Serializable::ValueType(type)) {
48             case Serializable::ValueType::INTERFACE_INFO_ARRAY:
49                 UnmarshallingInterfaceArray(protocol, data, size);
50                 break;
51             default:
52                 continue;
53         }
54     }
55     return SOFTBUS_OK;
56 }
57 
MarshallingInterfaceArray(WifiDirectProtocol & protocol) const58 void WifiConfigInfo::MarshallingInterfaceArray(WifiDirectProtocol &protocol) const
59 {
60     auto interfaceArray = GetInterfaceInfoArray();
61     for (const auto &interface : interfaceArray) {
62         auto pro = WifiDirectProtocolFactory::CreateProtocol(protocol.GetType());
63         if (pro != nullptr) {
64             std::vector<uint8_t> output;
65             pro->SetFormat(protocol.GetFormat());
66             interface.Marshalling(*pro, output);
67             protocol.Write(static_cast<int>(WifiConfigInfoKey::INTERFACE_INFO_ARRAY),
68                            Serializable::ValueType::INTERFACE_INFO_ARRAY, output.data(), output.size());
69         }
70     }
71 }
72 
Marshalling(WifiDirectProtocol & protocol,std::vector<uint8_t> & output) const73 int WifiConfigInfo::Marshalling(WifiDirectProtocol &protocol, std::vector<uint8_t> &output) const
74 {
75     for (const auto &[key, value] : values_) {
76         auto type = keyTypeTable_[key];
77         switch (type) {
78             case Serializable::ValueType::INTERFACE_INFO_ARRAY:
79                 MarshallingInterfaceArray(protocol);
80                 break;
81             default:
82                 continue;
83         }
84     }
85     protocol.GetOutput(output);
86     return SOFTBUS_OK;
87 }
88 
UnmarshallingInterfaceArray(WifiDirectProtocol & protocol,uint8_t * data,size_t size)89 void WifiConfigInfo::UnmarshallingInterfaceArray(WifiDirectProtocol &protocol, uint8_t *data, size_t size)
90 {
91     CONN_CHECK_AND_RETURN_LOGW(data != nullptr, CONN_WIFI_DIRECT, "data is nullptr");
92     auto pro = WifiDirectProtocolFactory::CreateProtocol(protocol.GetType());
93     CONN_CHECK_AND_RETURN_LOGE(pro != nullptr, CONN_WIFI_DIRECT, "create protocol failed");
94     pro->SetFormat(ProtocolFormat { TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE1 });
95 
96     InterfaceInfo info;
97     std::vector<uint8_t> input(data, data + size);
98     info.Unmarshalling(*pro, input);
99     auto interfaceArray = GetInterfaceInfoArray();
100     interfaceArray.push_back(info);
101     SetInterfaceInfoArray(interfaceArray);
102 }
103 
SetInterfaceInfoArray(const std::vector<InterfaceInfo> & value)104 void WifiConfigInfo::SetInterfaceInfoArray(const std::vector<InterfaceInfo> &value)
105 {
106     Set(WifiConfigInfoKey::INTERFACE_INFO_ARRAY, value);
107 }
108 
GetInterfaceInfoArray() const109 std::vector<InterfaceInfo> WifiConfigInfo::GetInterfaceInfoArray() const
110 {
111     return Get(WifiConfigInfoKey::INTERFACE_INFO_ARRAY, std::vector<InterfaceInfo>());
112 }
113 
GetInterfaceInfo(const std::string & name) const114 InterfaceInfo WifiConfigInfo::GetInterfaceInfo(const std::string &name) const
115 {
116     auto interfaces = GetInterfaceInfoArray();
117     for (const auto &interface : interfaces) {
118         if (name == interface.GetName()) {
119             return interface;
120         }
121     }
122     return {};
123 }
124 
SetDeviceId(const std::string & value)125 void WifiConfigInfo::SetDeviceId(const std::string &value)
126 {
127     Set(WifiConfigInfoKey::DEVICE_ID, value);
128 }
129 
GetDeviceId() const130 std::string WifiConfigInfo::GetDeviceId() const
131 {
132     return Get(WifiConfigInfoKey::DEVICE_ID, std::string(""));
133 }
134 } // namespace OHOS::SoftBus
135