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