1 /*
2 * Copyright (c) 2023-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 "net_interface_callback_stub.h"
17
18 #include "net_conn_constants.h"
19 #include "net_mgr_log_wrapper.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
23
NetInterfaceStateCallbackStub()24 NetInterfaceStateCallbackStub::NetInterfaceStateCallbackStub()
25 {
26 memberFuncMap_[static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_ADDR_UPDATED)] =
27 &NetInterfaceStateCallbackStub::CmdInterfaceAddressUpdated;
28 memberFuncMap_[static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_ADDR_REMOVED)] =
29 &NetInterfaceStateCallbackStub::CmdInterfaceAddressRemoved;
30 memberFuncMap_[static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_ADDED)] =
31 &NetInterfaceStateCallbackStub::CmdInterfaceAdded;
32 memberFuncMap_[static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_REMOVED)] =
33 &NetInterfaceStateCallbackStub::CmdInterfaceRemoved;
34 memberFuncMap_[static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_CHANGED)] =
35 &NetInterfaceStateCallbackStub::CmdInterfaceChanged;
36 memberFuncMap_[static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_LINK_STATE_CHANGED)] =
37 &NetInterfaceStateCallbackStub::CmdInterfaceLinkStateChanged;
38 memberFuncMap_[static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_ROUTE_CHANGED)] =
39 &NetInterfaceStateCallbackStub::CmdRouteChanged;
40 }
41
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int32_t NetInterfaceStateCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
43 MessageOption &option)
44 {
45 NETMGR_LOG_D("Stub call start, code:[%{public}d]", code);
46 std::u16string myDescriptor = NetInterfaceStateCallbackStub::GetDescriptor();
47 std::u16string remoteDescriptor = data.ReadInterfaceToken();
48 if (myDescriptor != remoteDescriptor) {
49 NETMGR_LOG_E("Descriptor checked failed");
50 return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
51 }
52
53 auto itFunc = memberFuncMap_.find(code);
54 if (itFunc != memberFuncMap_.end()) {
55 auto requestFunc = itFunc->second;
56 if (requestFunc != nullptr) {
57 return (this->*requestFunc)(data, reply);
58 }
59 }
60
61 NETMGR_LOG_D("Stub default case, need check");
62 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
63 }
64
CmdInterfaceAddressUpdated(MessageParcel & data,MessageParcel & reply)65 int32_t NetInterfaceStateCallbackStub::CmdInterfaceAddressUpdated(MessageParcel &data, MessageParcel &reply)
66 {
67 std::string addr = data.ReadString();
68 std::string ifName = data.ReadString();
69 int32_t flags = data.ReadInt32();
70 int32_t scope = data.ReadInt32();
71
72 int32_t result = OnInterfaceAddressUpdated(addr, ifName, flags, scope);
73 if (!reply.WriteInt32(result)) {
74 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
75 }
76 return NETMANAGER_SUCCESS;
77 }
78
CmdInterfaceAddressRemoved(MessageParcel & data,MessageParcel & reply)79 int32_t NetInterfaceStateCallbackStub::CmdInterfaceAddressRemoved(MessageParcel &data, MessageParcel &reply)
80 {
81 std::string addr;
82 if (!data.ReadString(addr)) {
83 return NETMANAGER_ERR_READ_DATA_FAIL;
84 }
85
86 std::string ifName;
87 if (!data.ReadString(ifName)) {
88 return NETMANAGER_ERR_READ_DATA_FAIL;
89 }
90
91 int32_t flags = 0;
92 if (!data.ReadInt32(flags)) {
93 return NETMANAGER_ERR_READ_DATA_FAIL;
94 }
95
96 int32_t scope = 0;
97 if (!data.ReadInt32(scope)) {
98 return NETMANAGER_ERR_READ_DATA_FAIL;
99 }
100
101 int32_t result = OnInterfaceAddressRemoved(addr, ifName, flags, scope);
102 if (!reply.WriteInt32(result)) {
103 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
104 }
105 return NETMANAGER_SUCCESS;
106 }
107
CmdInterfaceAdded(MessageParcel & data,MessageParcel & reply)108 int32_t NetInterfaceStateCallbackStub::CmdInterfaceAdded(MessageParcel &data, MessageParcel &reply)
109 {
110 std::string ifName;
111 if (!data.ReadString(ifName)) {
112 return NETMANAGER_ERR_READ_DATA_FAIL;
113 }
114
115 int32_t result = OnInterfaceAdded(ifName);
116 if (!reply.WriteInt32(result)) {
117 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
118 }
119 return NETMANAGER_SUCCESS;
120 }
121
CmdInterfaceRemoved(MessageParcel & data,MessageParcel & reply)122 int32_t NetInterfaceStateCallbackStub::CmdInterfaceRemoved(MessageParcel &data, MessageParcel &reply)
123 {
124 std::string ifName;
125 if (!data.ReadString(ifName)) {
126 return NETMANAGER_ERR_READ_DATA_FAIL;
127 }
128
129 int32_t result = OnInterfaceRemoved(ifName);
130 if (!reply.WriteInt32(result)) {
131 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
132 }
133 return NETMANAGER_SUCCESS;
134 }
135
CmdInterfaceChanged(MessageParcel & data,MessageParcel & reply)136 int32_t NetInterfaceStateCallbackStub::CmdInterfaceChanged(MessageParcel &data, MessageParcel &reply)
137 {
138 std::string ifName;
139 if (!data.ReadString(ifName)) {
140 return NETMANAGER_ERR_READ_DATA_FAIL;
141 }
142
143 bool isUp = false;
144 if (!data.ReadBool(isUp)) {
145 return NETMANAGER_ERR_READ_DATA_FAIL;
146 }
147
148 int32_t result = OnInterfaceChanged(ifName, isUp);
149 if (!reply.WriteInt32(result)) {
150 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
151 }
152 return NETMANAGER_SUCCESS;
153 }
154
CmdInterfaceLinkStateChanged(MessageParcel & data,MessageParcel & reply)155 int32_t NetInterfaceStateCallbackStub::CmdInterfaceLinkStateChanged(MessageParcel &data, MessageParcel &reply)
156 {
157 std::string ifName;
158 if (!data.ReadString(ifName)) {
159 return NETMANAGER_ERR_READ_DATA_FAIL;
160 }
161
162 bool isUp = false;
163 if (!data.ReadBool(isUp)) {
164 return NETMANAGER_ERR_READ_DATA_FAIL;
165 }
166
167 int32_t result = OnInterfaceLinkStateChanged(ifName, isUp);
168 if (!reply.WriteInt32(result)) {
169 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
170 }
171 return NETMANAGER_SUCCESS;
172 }
173
CmdRouteChanged(MessageParcel & data,MessageParcel & reply)174 int32_t NetInterfaceStateCallbackStub::CmdRouteChanged(MessageParcel &data, MessageParcel &reply)
175 {
176 bool updated = false;
177 if (!data.ReadBool(updated)) {
178 return NETMANAGER_ERR_READ_DATA_FAIL;
179 }
180
181 std::string route;
182 if (!data.ReadString(route)) {
183 return NETMANAGER_ERR_READ_DATA_FAIL;
184 }
185
186 std::string gateway;
187 if (!data.ReadString(gateway)) {
188 return NETMANAGER_ERR_READ_DATA_FAIL;
189 }
190
191 std::string ifName;
192 if (!data.ReadString(ifName)) {
193 return NETMANAGER_ERR_READ_DATA_FAIL;
194 }
195
196 int32_t result = OnRouteChanged(updated, route, gateway, ifName);
197 if (!reply.WriteInt32(result)) {
198 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
199 }
200 return NETMANAGER_SUCCESS;
201 }
202
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)203 int32_t NetInterfaceStateCallbackStub::OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName,
204 int32_t flags, int32_t scope)
205 {
206 NETMGR_LOG_D("OnInterfaceAddressUpdated, addr, iface:[%{public}s], scope:[%{public}d]", ifName.c_str(), scope);
207 return NETMANAGER_SUCCESS;
208 }
209
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)210 int32_t NetInterfaceStateCallbackStub::OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName,
211 int32_t flags, int32_t scope)
212 {
213 NETMGR_LOG_D("OnInterfaceAddressRemoved, addr, iface:[%{public}s], scope:[%{public}d]", ifName.c_str(), scope);
214 return NETMANAGER_SUCCESS;
215 }
216
OnInterfaceAdded(const std::string & ifName)217 int32_t NetInterfaceStateCallbackStub::OnInterfaceAdded(const std::string &ifName)
218 {
219 NETMGR_LOG_D("OnInterfaceAdded, iface:[%{public}s]", ifName.c_str());
220 return NETMANAGER_SUCCESS;
221 }
222
OnInterfaceRemoved(const std::string & ifName)223 int32_t NetInterfaceStateCallbackStub::OnInterfaceRemoved(const std::string &ifName)
224 {
225 NETMGR_LOG_D("OnInterfaceRemoved, iface:[%{public}s]", ifName.c_str());
226 return NETMANAGER_SUCCESS;
227 }
228
OnInterfaceChanged(const std::string & ifName,bool up)229 int32_t NetInterfaceStateCallbackStub::OnInterfaceChanged(const std::string &ifName, bool up)
230 {
231 NETMGR_LOG_D("OnInterfaceChanged, iface:[%{public}s] -> Up:[%{public}d]", ifName.c_str(), up);
232 return NETMANAGER_SUCCESS;
233 }
234
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)235 int32_t NetInterfaceStateCallbackStub::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
236 {
237 NETMGR_LOG_D("OnInterfaceLinkStateChanged, iface:[%{public}s] -> Up:[%{public}d]", ifName.c_str(), up);
238 return NETMANAGER_SUCCESS;
239 }
240
OnRouteChanged(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)241 int32_t NetInterfaceStateCallbackStub::OnRouteChanged(bool updated, const std::string &route,
242 const std::string &gateway, const std::string &ifName)
243 {
244 NETMGR_LOG_D("OnRouteChanged, updated:[%{public}d], route:[%{public}s], gateway:[%{public}s], iface:[%{public}s]",
245 updated, route.c_str(), gateway.c_str(), ifName.c_str());
246 return NETMANAGER_SUCCESS;
247 }
248 } // namespace NetManagerStandard
249 } // namespace OHOS
250