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