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