• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stub.h"
17 
18 #include "net_manager_ext_constants.h"
19 #include "netmgr_ext_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
MDnsServiceStub()23 MDnsServiceStub::MDnsServiceStub()
24 {
25     memberFuncMap_[CMD_DISCOVER] = &MDnsServiceStub::OnStartDiscoverService;
26     memberFuncMap_[CMD_STOP_DISCOVER] = &MDnsServiceStub::OnStopDiscoverService;
27     memberFuncMap_[CMD_REGISTER] = &MDnsServiceStub::OnRegisterService;
28     memberFuncMap_[CMD_STOP_REGISTER] = &MDnsServiceStub::OnUnRegisterService;
29     memberFuncMap_[CMD_RESOLVE] = &MDnsServiceStub::OnResolveService;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t MDnsServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
33                                          MessageOption &option)
34 {
35     NETMGR_EXT_LOG_D("stub call start, code = [%{public}d]", code);
36 
37     if (GetDescriptor() != data.ReadInterfaceToken()) {
38         NETMGR_EXT_LOG_E("descriptor checked fail");
39         return NETMANAGER_EXT_ERR_DESCRIPTOR_MISMATCH;
40     }
41 
42     auto itFunc = memberFuncMap_.find(code);
43     if (itFunc != memberFuncMap_.end()) {
44         auto requestFunc = itFunc->second;
45         if (requestFunc != nullptr) {
46             return (this->*requestFunc)(data, reply);
47         }
48     }
49 
50     NETMGR_EXT_LOG_D("stub default case, need check");
51     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52 }
53 
OnRegisterService(MessageParcel & data,MessageParcel & reply)54 int32_t MDnsServiceStub::OnRegisterService(MessageParcel &data, MessageParcel &reply)
55 {
56     sptr<MDnsServiceInfo> serviceInfo = MDnsServiceInfo::Unmarshalling(data);
57     if (!serviceInfo) {
58         NETMGR_EXT_LOG_E("serviceInfo is nullptr");
59         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
60     }
61 
62     NETMGR_EXT_LOG_D("MDnsServiceProxy [%{public}s][%{public}s][%{public}d]", serviceInfo->name.c_str(),
63                      serviceInfo->type.c_str(), serviceInfo->port);
64 
65     sptr<IRemoteObject> remote = data.ReadRemoteObject();
66     if (remote == nullptr) {
67         NETMGR_EXT_LOG_E("remote is nullptr.");
68         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
69     }
70 
71     sptr<IRegistrationCallback> callback = iface_cast<IRegistrationCallback>(remote);
72     if (callback == nullptr) {
73         NETMGR_EXT_LOG_E("iface_cast callback is nullptr.");
74         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
75     }
76 
77     int32_t err = RegisterService(*serviceInfo, callback);
78     NETMGR_EXT_LOG_D("MDnsService::RegisterService return:[%{public}d]", err);
79     if (!reply.WriteInt32(err)) {
80         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
81     }
82     return ERR_NONE;
83 }
84 
OnUnRegisterService(MessageParcel & data,MessageParcel & reply)85 int32_t MDnsServiceStub::OnUnRegisterService(MessageParcel &data, MessageParcel &reply)
86 {
87     sptr<IRemoteObject> remote = data.ReadRemoteObject();
88     if (remote == nullptr) {
89         NETMGR_EXT_LOG_E("remote ptr is nullptr.");
90         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
91     }
92 
93     sptr<IRegistrationCallback> callback = iface_cast<IRegistrationCallback>(remote);
94     if (callback == nullptr) {
95         NETMGR_EXT_LOG_E("iface_cast callback is nullptr.");
96         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
97     }
98 
99     int32_t err = UnRegisterService(callback);
100     NETMGR_EXT_LOG_D("MDnsService::UnRegisterService return:[%{public}d]", err);
101     if (!reply.WriteInt32(err)) {
102         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
103     }
104     return ERR_NONE;
105 }
106 
OnStartDiscoverService(MessageParcel & data,MessageParcel & reply)107 int32_t MDnsServiceStub::OnStartDiscoverService(MessageParcel &data, MessageParcel &reply)
108 {
109     std::string type;
110     if (!data.ReadString(type)) {
111         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
112     }
113 
114     sptr<IRemoteObject> remote = data.ReadRemoteObject();
115     if (remote == nullptr) {
116         NETMGR_EXT_LOG_E("remote ptr is nullptr.");
117         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
118     }
119 
120     sptr<IDiscoveryCallback> callback = iface_cast<IDiscoveryCallback>(remote);
121     if (callback == nullptr) {
122         NETMGR_EXT_LOG_E("iface_cast callback is nullptr.");
123         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
124     }
125 
126     int32_t err = StartDiscoverService(type, callback);
127     NETMGR_EXT_LOG_D("MDnsService::StartDiscoverService :[%{public}d]", err);
128     if (!reply.WriteInt32(err)) {
129         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
130     }
131     return ERR_NONE;
132 }
133 
OnStopDiscoverService(MessageParcel & data,MessageParcel & reply)134 int32_t MDnsServiceStub::OnStopDiscoverService(MessageParcel &data, MessageParcel &reply)
135 {
136     sptr<IRemoteObject> remote = data.ReadRemoteObject();
137     if (remote == nullptr) {
138         NETMGR_EXT_LOG_E("remote ptr is nullptr.");
139         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
140     }
141 
142     sptr<IDiscoveryCallback> callback = iface_cast<IDiscoveryCallback>(remote);
143     if (callback == nullptr) {
144         NETMGR_EXT_LOG_E("iface_cast callback is nullptr.");
145         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
146     }
147 
148     int32_t err = StopDiscoverService(callback);
149     NETMGR_EXT_LOG_D("MDnsService::StopDiscoverService :[%{public}d]", err);
150     if (!reply.WriteInt32(err)) {
151         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
152     }
153     return ERR_NONE;
154 }
155 
OnResolveService(MessageParcel & data,MessageParcel & reply)156 int32_t MDnsServiceStub::OnResolveService(MessageParcel &data, MessageParcel &reply)
157 {
158     NETMGR_EXT_LOG_D("MDnsServiceStub::OnResolveService In");
159     sptr<MDnsServiceInfo> serviceInfo = MDnsServiceInfo::Unmarshalling(data);
160     if (!serviceInfo) {
161         NETMGR_EXT_LOG_E("serviceInfo is nullptr");
162         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
163     }
164 
165     sptr<IRemoteObject> remote = data.ReadRemoteObject();
166     if (remote == nullptr) {
167         NETMGR_EXT_LOG_E("remote ptr is nullptr.");
168         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
169     }
170 
171     sptr<IResolveCallback> callback = iface_cast<IResolveCallback>(remote);
172     if (callback == nullptr) {
173         NETMGR_EXT_LOG_E("iface_cast callback is nullptr.");
174         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
175     }
176 
177     int32_t err = ResolveService(*serviceInfo, callback);
178     NETMGR_EXT_LOG_D("MDnsService::ResolveService :[%{public}d]", err);
179     if (!reply.WriteInt32(err)) {
180         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
181     }
182     return ERR_NONE;
183 }
184 } // namespace NetManagerStandard
185 } // namespace OHOS