1 /*
2  * Copyright (c) 2021-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 #include "notify_callback_stub.h"
16 
17 #include "netnative_log_wrapper.h"
18 
19 namespace OHOS {
20 namespace NetsysNative {
NotifyCallbackStub()21 NotifyCallbackStub::NotifyCallbackStub()
22 {
23     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDRESS_UPDATED)] =
24         &NotifyCallbackStub::CmdOnInterfaceAddressUpdated;
25     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDRESS_REMOVED)] =
26         &NotifyCallbackStub::CmdOnInterfaceAddressRemoved;
27     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDED)] =
28         &NotifyCallbackStub::CmdOnInterfaceAdded;
29     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_REMOVED)] =
30         &NotifyCallbackStub::CmdOnInterfaceRemoved;
31     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_CHANGED)] =
32         &NotifyCallbackStub::CmdOnInterfaceChanged;
33     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_LINK_STATE_CHANGED)] =
34         &NotifyCallbackStub::CmdOnInterfaceLinkStateChanged;
35     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_ROUTE_CHANGED)] =
36         &NotifyCallbackStub::CmdOnRouteChanged;
37     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_DHCP_SUCCESS)] = &NotifyCallbackStub::CmdDhcpSuccess;
38     memberFuncMap_[static_cast<uint32_t>(NotifyInterfaceCode::ON_BANDWIDTH_REACHED_LIMIT)] =
39         &NotifyCallbackStub::CmdOnBandwidthReachedLimit;
40 }
41 
~NotifyCallbackStub()42 NotifyCallbackStub::~NotifyCallbackStub() {}
43 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t NotifyCallbackStub::OnRemoteRequest(
45     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
46 {
47     NETNATIVE_LOG_D("Stub call start, code:[%{public}d]", code);
48     std::u16string myDescripter = NotifyCallbackStub::GetDescriptor();
49     std::u16string remoteDescripter = data.ReadInterfaceToken();
50     if (myDescripter != remoteDescripter) {
51         NETNATIVE_LOGE("Descriptor checked failed");
52         return ERR_FLATTEN_OBJECT;
53     }
54 
55     auto itFunc = memberFuncMap_.find(code);
56     if (itFunc != memberFuncMap_.end()) {
57         auto requestFunc = itFunc->second;
58         if (requestFunc != nullptr) {
59             return (this->*requestFunc)(data, reply);
60         }
61     }
62 
63     NETNATIVE_LOGI("Stub default case, need check");
64     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65 }
66 
CmdOnInterfaceAddressUpdated(MessageParcel & data,MessageParcel & reply)67 int32_t NotifyCallbackStub::CmdOnInterfaceAddressUpdated(MessageParcel &data, MessageParcel &reply)
68 {
69     std::string addr = data.ReadString();
70     std::string ifName = data.ReadString();
71     int32_t flags = data.ReadInt32();
72     int32_t scope = data.ReadInt32();
73 
74     int32_t result = OnInterfaceAddressUpdated(addr, ifName, flags, scope);
75     if (!reply.WriteInt32(result)) {
76         NETNATIVE_LOGE("Write parcel failed");
77         return result;
78     }
79 
80     return ERR_NONE;
81 }
82 
CmdOnInterfaceAddressRemoved(MessageParcel & data,MessageParcel & reply)83 int32_t NotifyCallbackStub::CmdOnInterfaceAddressRemoved(MessageParcel &data, MessageParcel &reply)
84 {
85     std::string addr = data.ReadString();
86     std::string ifName = data.ReadString();
87     int32_t flags = data.ReadInt32();
88     int32_t scope = data.ReadInt32();
89 
90     int32_t result = OnInterfaceAddressRemoved(addr, ifName, flags, scope);
91     if (!reply.WriteInt32(result)) {
92         NETNATIVE_LOGE("Write parcel failed");
93         return result;
94     }
95 
96     return ERR_NONE;
97 }
98 
CmdOnInterfaceAdded(MessageParcel & data,MessageParcel & reply)99 int32_t NotifyCallbackStub::CmdOnInterfaceAdded(MessageParcel &data, MessageParcel &reply)
100 {
101     std::string ifName = data.ReadString();
102 
103     int32_t result = OnInterfaceAdded(ifName);
104     if (!reply.WriteInt32(result)) {
105         NETNATIVE_LOGE("Write parcel failed");
106         return result;
107     }
108 
109     return ERR_NONE;
110 }
CmdOnInterfaceRemoved(MessageParcel & data,MessageParcel & reply)111 int32_t NotifyCallbackStub::CmdOnInterfaceRemoved(MessageParcel &data, MessageParcel &reply)
112 {
113     std::string ifName = data.ReadString();
114 
115     int32_t result = OnInterfaceRemoved(ifName);
116     if (!reply.WriteInt32(result)) {
117         NETNATIVE_LOGE("Write parcel failed");
118         return result;
119     }
120 
121     return ERR_NONE;
122 }
123 
CmdOnInterfaceChanged(MessageParcel & data,MessageParcel & reply)124 int32_t NotifyCallbackStub::CmdOnInterfaceChanged(MessageParcel &data, MessageParcel &reply)
125 {
126     std::string ifName = data.ReadString();
127     bool up = data.ReadBool();
128 
129     int32_t result = OnInterfaceChanged(ifName, up);
130     if (!reply.WriteInt32(result)) {
131         NETNATIVE_LOGE("Write parcel failed");
132         return result;
133     }
134 
135     return ERR_NONE;
136 }
137 
CmdOnInterfaceLinkStateChanged(MessageParcel & data,MessageParcel & reply)138 int32_t NotifyCallbackStub::CmdOnInterfaceLinkStateChanged(MessageParcel &data, MessageParcel &reply)
139 {
140     std::string ifName = data.ReadString();
141     bool up = data.ReadBool();
142 
143     int32_t result = OnInterfaceLinkStateChanged(ifName, up);
144     if (!reply.WriteInt32(result)) {
145         NETNATIVE_LOGE("Write parcel failed");
146         return result;
147     }
148 
149     return ERR_NONE;
150 }
151 
CmdOnRouteChanged(MessageParcel & data,MessageParcel & reply)152 int32_t NotifyCallbackStub::CmdOnRouteChanged(MessageParcel &data, MessageParcel &reply)
153 {
154     bool up = data.ReadBool();
155     std::string route = data.ReadString();
156     std::string gateway = data.ReadString();
157     std::string ifName = data.ReadString();
158 
159     int32_t result = OnRouteChanged(up, route, gateway, ifName);
160     if (!reply.WriteInt32(result)) {
161         NETNATIVE_LOGE("Write parcel failed");
162         return result;
163     }
164 
165     return ERR_NONE;
166 }
167 
CmdDhcpSuccess(MessageParcel & data,MessageParcel & reply)168 int32_t NotifyCallbackStub::CmdDhcpSuccess(MessageParcel &data, MessageParcel &reply)
169 {
170     NETNATIVE_LOGI("CmdDhcpSuccess");
171     static sptr<DhcpResultParcel> dhcpResult = DhcpResultParcel::Unmarshalling(data);
172     OnDhcpSuccess(dhcpResult);
173     return ERR_NONE;
174 }
175 
CmdOnBandwidthReachedLimit(MessageParcel & data,MessageParcel & reply)176 int32_t NotifyCallbackStub::CmdOnBandwidthReachedLimit(MessageParcel &data, MessageParcel &reply)
177 {
178     std::string limitName = data.ReadString();
179     std::string iface = data.ReadString();
180 
181     int32_t result = OnBandwidthReachedLimit(limitName, iface);
182     if (!reply.WriteInt32(result)) {
183         NETNATIVE_LOGE("Write parcel failed");
184         return result;
185     }
186 
187     return ERR_NONE;
188 }
189 } // namespace NetsysNative
190 } // namespace OHOS
191