• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "interface_configuration.h"
27 #include "net_all_capabilities.h"
28 #include "net_link_info.h"
29 #include "nlohmann/json.hpp"
30 
31 namespace OHOS {
32 namespace NetManagerStandard {
33 class EthernetConfiguration {
34 public:
35     EthernetConfiguration();
36     ~EthernetConfiguration() = default;
37 
38     bool ReadSysteamConfiguration(std::map<std::string, std::set<NetCap>> &devCaps,
39                                   std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs);
40     bool ReadUserConfiguration(std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs);
41     bool WriteUserConfiguration(const std::string &iface, sptr<InterfaceConfiguration> &cfg);
42     bool ClearAllUserConfiguration();
43     bool ConvertToConfiguration(const EthernetDhcpCallback::DhcpResult &dhcpResult, sptr<StaticConfiguration> &config);
44     sptr<InterfaceConfiguration> MakeInterfaceConfiguration(const sptr<InterfaceConfiguration> &devCfg,
45                                                             const sptr<NetLinkInfo> &devLinkInfo);
46 
47 private:
48     std::string ReadJsonFile(const std::string &filePath);
49     sptr<InterfaceConfiguration> ConvertJsonToConfiguration(const nlohmann::json &jsonData);
50     bool IsDirExist(const std::string &dirPath);
51     bool CreateDir(const std::string &dirPath);
52     bool DelDir(const std::string &dirPath);
53     bool IsFileExist(const std::string &filePath, std::string &realPath);
54     bool ReadFile(const std::string &filePath, std::string &fileContent);
55     bool WriteFile(const std::string &filePath, const std::string &fileContent);
56     void ParserFileConfig(const std::string &fileContent, std::string &iface, sptr<InterfaceConfiguration> cfg);
57     void GenCfgContent(const std::string &iface, sptr<InterfaceConfiguration> cfg, std::string &fileContent);
58     void MakeConfig(sptr<InterfaceConfiguration> cfg, const std::string &fileContent);
59 
60 private:
61     std::mutex mutex_;
62 };
63 } // namespace NetManagerStandard
64 } // namespace OHOS
65 #endif // ETHERNET_CONFIGURATION_H