1 /*
2 * Copyright (C) 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
16 #include "mdns_event_proxy.h"
17
18 #include <codecvt>
19
20 #include "iremote_object.h"
21 #include "net_manager_ext_constants.h"
22 #include "netmgr_ext_log_wrapper.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
RegistrationCallbackProxy(const sptr<IRemoteObject> & impl)26 RegistrationCallbackProxy::RegistrationCallbackProxy(const sptr<IRemoteObject> &impl)
27 : IRemoteProxy<IRegistrationCallback>(impl)
28 {
29 }
30
HandleRegister(const MDnsServiceInfo & serviceInfo,int32_t retCode)31 void RegistrationCallbackProxy::HandleRegister(const MDnsServiceInfo &serviceInfo, int32_t retCode)
32 {
33 MessageParcel data;
34 if (!data.WriteInterfaceToken(GetDescriptor())) {
35 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
36 return;
37 }
38
39 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
40 if (info == nullptr) {
41 NETMGR_EXT_LOG_E("info is nullptr");
42 return;
43 }
44 if (!MDnsServiceInfo::Marshalling(data, info)) {
45 NETMGR_EXT_LOG_E("Marshalling MDnsServiceInfo failed");
46 return;
47 }
48 if (!data.WriteInt32(retCode)) {
49 NETMGR_EXT_LOG_E("WriteInt32 failed");
50 return;
51 }
52
53 sptr<IRemoteObject> remote = Remote();
54 if (remote == nullptr) {
55 NETMGR_EXT_LOG_E("Remote is null");
56 return;
57 }
58 MessageParcel reply;
59 MessageOption option;
60 int32_t ret =
61 remote->SendRequest(static_cast<uint32_t>(IRegistrationCallback::Message::REGISTERED), data, reply, option);
62 if (ret != ERR_NONE) {
63 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
64 }
65 }
66
HandleUnRegister(const MDnsServiceInfo & serviceInfo,int32_t retCode)67 void RegistrationCallbackProxy::HandleUnRegister(const MDnsServiceInfo &serviceInfo, int32_t retCode)
68 {
69 MessageParcel data;
70 if (!data.WriteInterfaceToken(GetDescriptor())) {
71 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
72 return;
73 }
74
75 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
76 if (info == nullptr) {
77 NETMGR_EXT_LOG_E("info is nullptr");
78 return;
79 }
80 if (!MDnsServiceInfo::Marshalling(data, info)) {
81 NETMGR_EXT_LOG_E("Marshalling failed");
82 return;
83 }
84 if (!data.WriteInt32(retCode)) {
85 NETMGR_EXT_LOG_E("WriteInt32 failed");
86 return;
87 }
88
89 sptr<IRemoteObject> remote = Remote();
90 if (remote == nullptr) {
91 NETMGR_EXT_LOG_E("Remote is null");
92 return;
93 }
94 MessageParcel reply;
95 MessageOption option;
96 NETMGR_EXT_LOG_I("SendRequest");
97 int32_t ret =
98 remote->SendRequest(static_cast<uint32_t>(IRegistrationCallback::Message::UNREGISTERED), data, reply, option);
99 if (ret != ERR_NONE) {
100 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
101 }
102 }
103
HandleRegisterResult(const MDnsServiceInfo & serviceInfo,int32_t retCode)104 void RegistrationCallbackProxy::HandleRegisterResult(const MDnsServiceInfo &serviceInfo, int32_t retCode)
105 {
106 MessageParcel data;
107 if (!data.WriteInterfaceToken(GetDescriptor())) {
108 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
109 return;
110 }
111
112 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
113 if (info == nullptr) {
114 NETMGR_EXT_LOG_E("info is nullptr");
115 return;
116 }
117 if (!MDnsServiceInfo::Marshalling(data, info)) {
118 NETMGR_EXT_LOG_E("Marshalling MDnsServiceInfo failed");
119 return;
120 }
121 if (!data.WriteInt32(retCode)) {
122 NETMGR_EXT_LOG_E("WriteInt32 failed");
123 return;
124 }
125
126 sptr<IRemoteObject> remote = Remote();
127 if (remote == nullptr) {
128 NETMGR_EXT_LOG_E("Remote is null");
129 return;
130 }
131 MessageParcel reply;
132 MessageOption option;
133 option.SetFlags(MessageOption::TF_ASYNC);
134 int32_t ret =
135 remote->SendRequest(static_cast<uint32_t>(IRegistrationCallback::Message::RESULT), data, reply, option);
136 if (ret != ERR_NONE) {
137 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
138 }
139 }
140
DiscoveryCallbackProxy(const sptr<IRemoteObject> & impl)141 DiscoveryCallbackProxy::DiscoveryCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IDiscoveryCallback>(impl)
142 {
143 }
144
HandleStartDiscover(const MDnsServiceInfo & serviceInfo,int32_t retCode)145 void DiscoveryCallbackProxy::HandleStartDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode)
146 {
147 MessageParcel data;
148 if (!data.WriteInterfaceToken(GetDescriptor())) {
149 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
150 return;
151 }
152
153 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
154 if (info == nullptr) {
155 NETMGR_EXT_LOG_E("info is nullptr");
156 return;
157 }
158 if (!MDnsServiceInfo::Marshalling(data, info)) {
159 NETMGR_EXT_LOG_E("Marshalling MDnsServiceInfo failed");
160 return;
161 }
162
163 if (!data.WriteInt32(retCode)) {
164 NETMGR_EXT_LOG_E("WriteInt32 failed");
165 return;
166 }
167
168 sptr<IRemoteObject> remote = Remote();
169 if (remote == nullptr) {
170 NETMGR_EXT_LOG_E("Remote is null");
171 return;
172 }
173 MessageParcel reply;
174 MessageOption option;
175 option.SetFlags(MessageOption::TF_ASYNC);
176 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDiscoveryCallback::Message::STARTED), data, reply, option);
177 if (ret != ERR_NONE) {
178 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
179 }
180 }
181
HandleStopDiscover(const MDnsServiceInfo & serviceInfo,int32_t retCode)182 void DiscoveryCallbackProxy::HandleStopDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode)
183 {
184 MessageParcel data;
185 if (!data.WriteInterfaceToken(GetDescriptor())) {
186 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
187 return;
188 }
189
190 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
191 if (info == nullptr) {
192 NETMGR_EXT_LOG_E("info is nullptr");
193 return;
194 }
195 if (!MDnsServiceInfo::Marshalling(data, info)) {
196 NETMGR_EXT_LOG_E("Marshalling MDnsServiceInfo failed");
197 return;
198 }
199
200 if (!data.WriteInt32(retCode)) {
201 NETMGR_EXT_LOG_E("WriteInt32 failed");
202 return;
203 }
204
205 sptr<IRemoteObject> remote = Remote();
206 if (remote == nullptr) {
207 NETMGR_EXT_LOG_E("Remote is null");
208 return;
209 }
210 MessageParcel reply;
211 MessageOption option;
212 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDiscoveryCallback::Message::STOPPED), data, reply, option);
213 option.SetFlags(MessageOption::TF_ASYNC);
214 if (ret != ERR_NONE) {
215 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
216 }
217 }
218
HandleServiceFound(const MDnsServiceInfo & serviceInfo,int32_t retCode)219 void DiscoveryCallbackProxy::HandleServiceFound(const MDnsServiceInfo &serviceInfo, int32_t retCode)
220 {
221 MessageParcel data;
222 if (!data.WriteInterfaceToken(GetDescriptor())) {
223 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
224 return;
225 }
226
227 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
228 if (info == nullptr) {
229 NETMGR_EXT_LOG_E("info is nullptr");
230 return;
231 }
232 if (!MDnsServiceInfo::Marshalling(data, info)) {
233 NETMGR_EXT_LOG_E("Marshalling MDnsServiceInfo failed");
234 return;
235 }
236 if (!data.WriteInt32(retCode)) {
237 NETMGR_EXT_LOG_E("WriteInt32 failed");
238 return;
239 }
240
241 sptr<IRemoteObject> remote = Remote();
242 if (remote == nullptr) {
243 NETMGR_EXT_LOG_E("Remote is null");
244 return;
245 }
246 MessageParcel reply;
247 MessageOption option;
248 option.SetFlags(MessageOption::TF_ASYNC);
249 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDiscoveryCallback::Message::FOUND), data, reply, option);
250 if (ret != ERR_NONE) {
251 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
252 }
253 }
254
HandleServiceLost(const MDnsServiceInfo & serviceInfo,int32_t retCode)255 void DiscoveryCallbackProxy::HandleServiceLost(const MDnsServiceInfo &serviceInfo, int32_t retCode)
256 {
257 MessageParcel data;
258 if (!data.WriteInterfaceToken(GetDescriptor())) {
259 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
260 return;
261 }
262
263 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
264 if (info == nullptr) {
265 NETMGR_EXT_LOG_E("info is nullptr");
266 return;
267 }
268 if (!MDnsServiceInfo::Marshalling(data, info)) {
269 NETMGR_EXT_LOG_E("Marshalling MDnsServiceInfo failed");
270 return;
271 }
272 if (!data.WriteInt32(retCode)) {
273 NETMGR_EXT_LOG_E("WriteInt32 failed");
274 return;
275 }
276
277 sptr<IRemoteObject> remote = Remote();
278 if (remote == nullptr) {
279 NETMGR_EXT_LOG_E("Remote is null");
280 return;
281 }
282 MessageParcel reply;
283 MessageOption option;
284 option.SetFlags(MessageOption::TF_ASYNC);
285 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDiscoveryCallback::Message::LOST), data, reply, option);
286 if (ret != ERR_NONE) {
287 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
288 }
289 }
290
ResolveCallbackProxy(const sptr<IRemoteObject> & impl)291 ResolveCallbackProxy::ResolveCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IResolveCallback>(impl) {}
292
HandleResolveResult(const MDnsServiceInfo & serviceInfo,int32_t retCode)293 void ResolveCallbackProxy::HandleResolveResult(const MDnsServiceInfo &serviceInfo, int32_t retCode)
294 {
295 MessageParcel data;
296 if (!data.WriteInterfaceToken(GetDescriptor())) {
297 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
298 return;
299 }
300
301 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
302 if (info == nullptr) {
303 NETMGR_EXT_LOG_E("info is nullptr");
304 return;
305 }
306 if (!MDnsServiceInfo::Marshalling(data, info)) {
307 NETMGR_EXT_LOG_E("Marshalling MDnsServiceInfo failed");
308 return;
309 }
310 if (!data.WriteInt32(retCode)) {
311 NETMGR_EXT_LOG_E("WriteInt32 failed");
312 return;
313 }
314
315 sptr<IRemoteObject> remote = Remote();
316 if (remote == nullptr) {
317 NETMGR_EXT_LOG_E("Remote is null");
318 return;
319 }
320 MessageParcel reply;
321 MessageOption option;
322 option.SetFlags(MessageOption::TF_ASYNC);
323 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IResolveCallback::Message::RESULT), data, reply, option);
324 if (ret != ERR_NONE) {
325 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
326 }
327 }
328 } // namespace NetManagerStandard
329 } // namespace OHOS