• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 ETHERNET_CONFIGURATION_H
17 #define ETHERNET_CONFIGURATION_H
18 
19 #include <map>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "ethernet_dhcp_callback.h"
26 #include "http_proxy.h"
27 #include "interface_configuration.h"
28 #include "net_all_capabilities.h"
29 #include "net_link_info.h"
30 #include "nlohmann/json.hpp"
31 
32 namespace OHOS {
33 namespace NetManagerStandard {
34 class EthernetConfiguration {
35 public:
36     EthernetConfiguration();
37     ~EthernetConfiguration() = default;
38 
39     bool ReadSystemConfiguration(std::map<std::string, std::set<NetCap>> &devCaps,
40                                  std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs);
41     bool ReadUserConfiguration(std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs);
42     bool WriteUserConfiguration(const std::string &iface, sptr<InterfaceConfiguration> &cfg);
43     bool ClearAllUserConfiguration();
44     bool ConvertToConfiguration(const EthernetDhcpCallback::DhcpResult &dhcpResult, sptr<StaticConfiguration> &config);
45     sptr<InterfaceConfiguration> MakeInterfaceConfiguration(const sptr<InterfaceConfiguration> &devCfg,
46                                                             const sptr<NetLinkInfo> &devLinkInfo);
47 
48 private:
49     void ParseDevice(const std::string &fileContent, std::string &iface);
50     void ParseBootProto(const std::string &fileContent, sptr<InterfaceConfiguration> cfg);
51     void ParseStaticConfig(const std::string &fileContent, sptr<InterfaceConfiguration> cfg);
52 
53     std::string ReadJsonFile(const std::string &filePath);
54     sptr<InterfaceConfiguration> ConvertJsonToConfiguration(const nlohmann::json &jsonData);
55     bool IsDirExist(const std::string &dirPath);
56     bool CreateDir(const std::string &dirPath);
57     bool DelDir(const std::string &dirPath);
58     bool IsFileExist(const std::string &filePath, std::string &realPath);
59     bool ReadFile(const std::string &filePath, std::string &fileContent);
60     bool WriteFile(const std::string &filePath, const std::string &fileContent);
61     void ParserFileConfig(const std::string &fileContent, std::string &iface, sptr<InterfaceConfiguration> cfg);
62     void ParserFileHttpProxy(const std::string &fileContent, const sptr<InterfaceConfiguration> &cfg);
63     void ParserIfaceIpAndRoute(sptr<InterfaceConfiguration> &cfg, const std::string &rootNetMask);
64     void GenCfgContent(const std::string &iface, sptr<InterfaceConfiguration> cfg, std::string &fileContent);
65     void GenHttpProxyContent(const sptr<InterfaceConfiguration> &cfg, std::string &fileContent);
66     std::string AccumulateNetAddress(const std::vector<INetAddr> &netAddrList);
67     bool IsValidDhcpResult(const EthernetDhcpCallback::DhcpResult &dhcpResult, sptr<StaticConfiguration> &config);
68 
69 private:
70     std::mutex mutex_;
71 };
72 } // namespace NetManagerStandard
73 } // namespace OHOS
74 #endif // ETHERNET_CONFIGURATION_H