• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "ethernet_service.h"
17 
18 #include <new>
19 #include <sys/time.h>
20 
21 #include "ethernet_management.h"
22 #include "interface_configuration.h"
23 #include "iremote_object.h"
24 #include "net_ethernet_base_service.h"
25 #include "net_manager_center.h"
26 #include "net_manager_constants.h"
27 #include "netmanager_base_permission.h"
28 #include "netmgr_ext_log_wrapper.h"
29 #include "netsys_controller.h"
30 #include "system_ability_definition.h"
31 
32 namespace OHOS {
33 namespace NetManagerStandard {
34 namespace {
35 constexpr uint16_t DEPENDENT_SERVICE_NET_CONN_MANAGER = 0x0001;
36 constexpr uint16_t DEPENDENT_SERVICE_COMMON_EVENT = 0x0002;
37 constexpr uint16_t DEPENDENT_SERVICE_All = 0x0003;
38 const bool REGISTER_LOCAL_RESULT_ETH =
39     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<EthernetService>::GetInstance().get());
40 } // namespace
41 
EthernetService()42 EthernetService::EthernetService() : SystemAbility(COMM_ETHERNET_MANAGER_SYS_ABILITY_ID, true) {}
43 
44 EthernetService::~EthernetService() = default;
45 
OnStart()46 void EthernetService::OnStart()
47 {
48     struct timeval tv;
49     gettimeofday(&tv, nullptr);
50     NETMGR_EXT_LOG_D("EthernetService::OnStart begin");
51     if (state_ == STATE_RUNNING) {
52         NETMGR_EXT_LOG_D("EthernetService the state is already running");
53         return;
54     }
55     if (!Init()) {
56         NETMGR_EXT_LOG_E("EthernetService init failed");
57         return;
58     }
59     state_ = STATE_RUNNING;
60     gettimeofday(&tv, nullptr);
61     NETMGR_EXT_LOG_D("EthernetService::OnStart end");
62 }
63 
OnStop()64 void EthernetService::OnStop()
65 {
66     state_ = STATE_STOPPED;
67     registerToService_ = false;
68 }
69 
Dump(int32_t fd,const std::vector<std::u16string> & args)70 int32_t EthernetService::Dump(int32_t fd, const std::vector<std::u16string> &args)
71 {
72     NETMGR_EXT_LOG_D("Start Dump, fd: %{public}d", fd);
73     std::string result;
74     if (ethManagement_ == nullptr) {
75         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
76     }
77     ethManagement_->GetDumpInfo(result);
78     int32_t ret = dprintf(fd, "%s\n", result.c_str());
79     return ret < 0 ? NETMANAGER_EXT_ERR_LOCAL_PTR_NULL : NETMANAGER_EXT_SUCCESS;
80 }
81 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)82 void EthernetService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
83 {
84     switch (systemAbilityId) {
85         case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID:
86             NETMGR_EXT_LOG_D("EthernetService::OnAddSystemAbility Conn");
87             dependentServiceState_ |= DEPENDENT_SERVICE_NET_CONN_MANAGER;
88             break;
89         case COMMON_EVENT_SERVICE_ID:
90             NETMGR_EXT_LOG_D("EthernetService::OnAddSystemAbility CES");
91             dependentServiceState_ |= DEPENDENT_SERVICE_COMMON_EVENT;
92             break;
93         default:
94             NETMGR_EXT_LOG_D("EthernetService::OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId);
95             break;
96     }
97     if (dependentServiceState_ == DEPENDENT_SERVICE_All) {
98         InitManagement();
99     }
100 }
101 
Init()102 bool EthernetService::Init()
103 {
104     if (!REGISTER_LOCAL_RESULT_ETH) {
105         NETMGR_EXT_LOG_E("EthernetService Register to local sa manager failed");
106         return false;
107     }
108     if (!registerToService_) {
109         if (!Publish(DelayedSingleton<EthernetService>::GetInstance().get())) {
110             NETMGR_EXT_LOG_E("EthernetService Register to sa manager failed");
111             return false;
112         }
113         registerToService_ = true;
114     }
115     AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
116     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
117     serviceComm_ = new (std::nothrow) EthernetServiceCommon();
118     if (serviceComm_ == nullptr) {
119         NETMGR_EXT_LOG_E("serviceComm_ is nullptr");
120         return false;
121     }
122     NetManagerCenter::GetInstance().RegisterEthernetService(serviceComm_);
123     return true;
124 }
125 
InitManagement()126 void EthernetService::InitManagement()
127 {
128     NETMGR_EXT_LOG_D("EthernetService::InitManagement Enter");
129     if (ethManagement_ == nullptr) {
130         ethManagement_ = std::make_unique<EthernetManagement>();
131         ethManagement_->Init();
132     }
133 }
134 
SetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ic)135 int32_t EthernetService::SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic)
136 {
137     NETMGR_EXT_LOG_D("Set iface: %{public}s config", iface.c_str());
138     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
139         NETMGR_EXT_LOG_E("EthernetService SetIfaceConfig no js permission");
140         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
141     }
142 
143     if (ethManagement_ == nullptr) {
144         NETMGR_EXT_LOG_E("ethManagement is null");
145         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
146     }
147     return ethManagement_->UpdateDevInterfaceCfg(iface, ic);
148 }
149 
GetIfaceConfig(const std::string & iface,sptr<InterfaceConfiguration> & ifaceConfig)150 int32_t EthernetService::GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
151 {
152     NETMGR_EXT_LOG_D("Get iface: %{public}s config", iface.c_str());
153     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
154         NETMGR_EXT_LOG_E("EthernetService GetIfaceConfig no js permission");
155         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
156     }
157 
158     if (ethManagement_ == nullptr) {
159         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
160     }
161     return ethManagement_->GetDevInterfaceCfg(iface, ifaceConfig);
162 }
163 
IsIfaceActive(const std::string & iface,int32_t & activeStatus)164 int32_t EthernetService::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
165 {
166     NETMGR_EXT_LOG_D("Get iface: %{public}s is active", iface.c_str());
167     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
168         NETMGR_EXT_LOG_E("EthernetService IsIfaceActive no js permission");
169         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
170     }
171 
172     if (ethManagement_ == nullptr) {
173         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
174     }
175     return ethManagement_->IsIfaceActive(iface, activeStatus);
176 }
177 
GetAllActiveIfaces(std::vector<std::string> & activeIfaces)178 int32_t EthernetService::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
179 {
180     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
181         NETMGR_EXT_LOG_E("EthernetService GetAllActiveIfaces no js permission");
182         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
183     }
184 
185     if (ethManagement_ == nullptr) {
186         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
187     }
188     return ethManagement_->GetAllActiveIfaces(activeIfaces);
189 }
190 
ResetFactory()191 int32_t EthernetService::ResetFactory()
192 {
193     if (ethManagement_ == nullptr) {
194         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
195     }
196     return ethManagement_->ResetFactory();
197 }
198 
SetInterfaceUp(const std::string & iface)199 int32_t EthernetService::SetInterfaceUp(const std::string &iface)
200 {
201     NETMGR_EXT_LOG_D("Set interface: %{public}s up", iface.c_str());
202     return NetsysController::GetInstance().SetInterfaceUp(iface);
203 }
204 
SetInterfaceDown(const std::string & iface)205 int32_t EthernetService::SetInterfaceDown(const std::string &iface)
206 {
207     NETMGR_EXT_LOG_D("Set interface: %{public}s down", iface.c_str());
208     return NetsysController::GetInstance().SetInterfaceDown(iface);
209 }
210 
GetInterfaceConfig(const std::string & iface,OHOS::nmd::InterfaceConfigurationParcel & config)211 int32_t EthernetService::GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &config)
212 {
213     NETMGR_EXT_LOG_D("Get interface: %{public}s config", iface.c_str());
214     config.ifName = iface;
215     return NetsysController::GetInstance().InterfaceGetConfig(config);
216 }
217 } // namespace NetManagerStandard
218 } // namespace OHOS
219