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_service_proxy.h"
17
18 #include "iremote_object.h"
19 #include "net_manager_constants.h"
20 #include "netmgr_ext_log_wrapper.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
MDnsServiceProxy(const sptr<IRemoteObject> & impl)24 MDnsServiceProxy::MDnsServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IMDnsService>(impl) {}
25
26 MDnsServiceProxy::~MDnsServiceProxy() = default;
27
RegisterService(const MDnsServiceInfo & serviceInfo,const sptr<IRegistrationCallback> & cb)28 int32_t MDnsServiceProxy::RegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb)
29 {
30 MessageParcel data;
31 if (!data.WriteInterfaceToken(MDnsServiceProxy::GetDescriptor())) {
32 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
33 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
34 }
35
36 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
37 if (info == nullptr) {
38 NETMGR_EXT_LOG_E("info is nullptr");
39 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
40 }
41 if (!MDnsServiceInfo::Marshalling(data, info)) {
42 NETMGR_EXT_LOG_E("Marshalling failed");
43 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
44 }
45 if (!data.WriteRemoteObject(cb->AsObject().GetRefPtr())) {
46 NETMGR_EXT_LOG_E("proxy write callback failed");
47 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
48 }
49
50 sptr<IRemoteObject> remote = Remote();
51 if (remote == nullptr) {
52 NETMGR_EXT_LOG_E("Remote is null");
53 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
54 }
55 MessageParcel reply;
56 MessageOption option;
57 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MdnsServiceInterfaceCode::CMD_REGISTER),
58 data, reply, option);
59 if (ret != ERR_NONE) {
60 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
61 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
62 }
63 int32_t retCode = reply.ReadInt32();
64 if (retCode != NETMANAGER_EXT_SUCCESS) {
65 NETMGR_EXT_LOG_E("MDnsService::RegisterService return: [%{public}d]", retCode);
66 }
67 return retCode;
68 }
69
UnRegisterService(const sptr<IRegistrationCallback> & cb)70 int32_t MDnsServiceProxy::UnRegisterService(const sptr<IRegistrationCallback> &cb)
71 {
72 MessageParcel data;
73 if (!data.WriteInterfaceToken(MDnsServiceProxy::GetDescriptor())) {
74 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
75 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
76 }
77
78 if (!data.WriteRemoteObject(cb->AsObject().GetRefPtr())) {
79 NETMGR_EXT_LOG_E("proxy write callback failed");
80 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
81 }
82
83 sptr<IRemoteObject> remote = Remote();
84 if (remote == nullptr) {
85 NETMGR_EXT_LOG_E("Remote is null");
86 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
87 }
88 MessageParcel reply;
89 MessageOption option;
90 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MdnsServiceInterfaceCode::CMD_STOP_REGISTER),
91 data, reply, option);
92 if (ret != ERR_NONE) {
93 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
94 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
95 }
96 int32_t retCode = reply.ReadInt32();
97 if (retCode != NETMANAGER_EXT_SUCCESS) {
98 NETMGR_EXT_LOG_E("MDnsService::UnRegisterService return: [%{public}d]", retCode);
99 }
100 return retCode;
101 }
102
StartDiscoverService(const std::string & serviceType,const sptr<IDiscoveryCallback> & cb)103 int32_t MDnsServiceProxy::StartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb)
104 {
105 MessageParcel data;
106 if (!data.WriteInterfaceToken(MDnsServiceProxy::GetDescriptor())) {
107 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
108 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
109 }
110
111 if (!data.WriteString(serviceType)) {
112 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
113 }
114 if (!data.WriteRemoteObject(cb->AsObject().GetRefPtr())) {
115 NETMGR_EXT_LOG_E("proxy write callback failed");
116 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
117 }
118
119 sptr<IRemoteObject> remote = Remote();
120 if (remote == nullptr) {
121 NETMGR_EXT_LOG_E("Remote is null");
122 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
123 }
124 MessageParcel reply;
125 MessageOption option;
126 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MdnsServiceInterfaceCode::CMD_DISCOVER),
127 data, reply, option);
128 if (ret != ERR_NONE) {
129 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
130 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
131 }
132
133 int32_t retCode = reply.ReadInt32();
134 if (retCode != NETMANAGER_EXT_SUCCESS) {
135 NETMGR_EXT_LOG_E("MDnsService::DiscoverServices return: [%{public}d]", retCode);
136 }
137 return retCode;
138 }
139
StopDiscoverService(const sptr<IDiscoveryCallback> & cb)140 int32_t MDnsServiceProxy::StopDiscoverService(const sptr<IDiscoveryCallback> &cb)
141 {
142 MessageParcel data;
143 if (!data.WriteInterfaceToken(MDnsServiceProxy::GetDescriptor())) {
144 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
145 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
146 }
147
148 if (!data.WriteRemoteObject(cb->AsObject().GetRefPtr())) {
149 NETMGR_EXT_LOG_E("proxy write callback failed");
150 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
151 }
152
153 sptr<IRemoteObject> remote = Remote();
154 if (remote == nullptr) {
155 NETMGR_EXT_LOG_E("Remote is null");
156 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
157 }
158 MessageParcel reply;
159 MessageOption option;
160 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MdnsServiceInterfaceCode::CMD_STOP_DISCOVER),
161 data, reply, option);
162 if (ret != ERR_NONE) {
163 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%{public}d]", ret);
164 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
165 }
166 int32_t retCode = reply.ReadInt32();
167 if (retCode != NETMANAGER_EXT_SUCCESS) {
168 NETMGR_EXT_LOG_E("MDnsService::StopServiceDiscovery return: [%{public}d]", retCode);
169 }
170 return retCode;
171 }
172
ResolveService(const MDnsServiceInfo & serviceInfo,const sptr<IResolveCallback> & cb)173 int32_t MDnsServiceProxy::ResolveService(const MDnsServiceInfo &serviceInfo, const sptr<IResolveCallback> &cb)
174 {
175 MessageParcel data;
176 if (!data.WriteInterfaceToken(MDnsServiceProxy::GetDescriptor())) {
177 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
178 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
179 }
180
181 sptr<MDnsServiceInfo> info = new (std::nothrow) MDnsServiceInfo(serviceInfo);
182 if (info == nullptr) {
183 NETMGR_EXT_LOG_E("info is nullptr");
184 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
185 }
186 if (!MDnsServiceInfo::Marshalling(data, info)) {
187 NETMGR_EXT_LOG_E("Marshalling failed");
188 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
189 }
190 if (!data.WriteRemoteObject(cb->AsObject().GetRefPtr())) {
191 NETMGR_EXT_LOG_E("proxy write callback failed");
192 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
193 }
194
195 sptr<IRemoteObject> remote = Remote();
196 if (remote == nullptr) {
197 NETMGR_EXT_LOG_E("Remote is null");
198 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
199 }
200 MessageParcel reply;
201 MessageOption option;
202 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MdnsServiceInterfaceCode::CMD_RESOLVE),
203 data, reply, option);
204 if (ret != ERR_NONE) {
205 NETMGR_EXT_LOG_E("SendRequest failed, error code: [%d]", ret);
206 return ret;
207 }
208 int32_t retCode = reply.ReadInt32();
209 if (retCode != NETMANAGER_EXT_SUCCESS) {
210 NETMGR_EXT_LOG_E("MDnsService::ResolveService return: [%{public}d]", retCode);
211 }
212 return retCode;
213 }
214 } // namespace NetManagerStandard
215 } // namespace OHOS