1 /*
2 * Copyright (c) 2021-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 <mutex>
17
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include <iproxy_broker.h>
21 #include <iremote_stub.h>
22 #include <iservice_registry.h>
23 #include <object_collector.h>
24 #include <string_ex.h>
25
26 #include "hdf_device_manager_interface_code.h"
27 #include "iservmgr_hdi.h"
28
29 #define HDF_LOG_TAG hdi_servmgr_client
30
31 using OHOS::HDI::HdfDeviceManager::HdfDeviceManagerInterfaceCode;
32
33 namespace OHOS {
34 namespace HDI {
35 namespace ServiceManager {
36 namespace V1_0 {
37 constexpr int DEVICE_SERVICE_MANAGER_SA_ID = 5100;
38 std::mutex g_remoteMutex;
39
40 class ServiceManagerProxy : public IProxyBroker<IServiceManager> {
41 public:
ServiceManagerProxy(const sptr<IRemoteObject> & impl)42 explicit ServiceManagerProxy(const sptr<IRemoteObject> &impl) : IProxyBroker<IServiceManager>(impl) {}
~ServiceManagerProxy()43 ~ServiceManagerProxy() {}
44
45 sptr<IRemoteObject> GetService(const char *serviceName) override;
46 int32_t ListAllService(std::vector<HdiServiceInfo> &serviceInfos) override;
47 int32_t RegisterServiceStatusListener(sptr<IServStatListener> listener, uint16_t deviceClass) override;
48 int32_t UnregisterServiceStatusListener(sptr<IServStatListener> listener) override;
49 int32_t ListServiceByInterfaceDesc(std::vector<std::string> &serviceNames, const char *interfaceDesc) override;
50
51 private:
52 static inline BrokerDelegator<ServiceManagerProxy> delegator_;
53 };
54
Get()55 sptr<IServiceManager> IServiceManager::Get()
56 {
57 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (saManager == nullptr) {
59 HDF_LOGE("failed to get sa manager");
60 return nullptr;
61 }
62
63 std::unique_lock<std::mutex> lock(g_remoteMutex);
64 sptr<IRemoteObject> remote = saManager->GetSystemAbility(DEVICE_SERVICE_MANAGER_SA_ID);
65 if (remote != nullptr) {
66 return new ServiceManagerProxy(remote);
67 }
68
69 HDF_LOGE("failed to get sa hdf service manager");
70 return nullptr;
71 }
72
RegisterServiceStatusListener(::OHOS::sptr<IServStatListener> listener,uint16_t deviceClass)73 int32_t ServiceManagerProxy::RegisterServiceStatusListener(
74 ::OHOS::sptr<IServStatListener> listener, uint16_t deviceClass)
75 {
76 MessageParcel data;
77 MessageParcel reply;
78 MessageOption option;
79
80 if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteUint16(deviceClass) ||
81 !data.WriteRemoteObject(listener->AsObject())) {
82 return HDF_FAILURE;
83 }
84
85 std::unique_lock<std::mutex> lock(g_remoteMutex);
86 int status = Remote()->SendRequest(
87 static_cast<uint32_t>(HdfDeviceManagerInterfaceCode::DEVSVC_MANAGER_REGISTER_SVCLISTENER), data, reply, option);
88 lock.unlock();
89 if (status) {
90 HDF_LOGE("failed to register servstat listener, %{public}d", status);
91 }
92 return status;
93 }
94
UnregisterServiceStatusListener(::OHOS::sptr<IServStatListener> listener)95 int32_t ServiceManagerProxy::UnregisterServiceStatusListener(::OHOS::sptr<IServStatListener> listener)
96 {
97 MessageParcel data;
98 MessageParcel reply;
99 MessageOption option;
100
101 if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteRemoteObject(listener->AsObject())) {
102 return HDF_FAILURE;
103 }
104
105 std::unique_lock<std::mutex> lock(g_remoteMutex);
106 int status = Remote()->SendRequest(
107 static_cast<uint32_t>(HdfDeviceManagerInterfaceCode::DEVSVC_MANAGER_UNREGISTER_SVCLISTENER), data, reply,
108 option);
109 lock.unlock();
110 if (status) {
111 HDF_LOGE("failed to unregister servstat listener, %{public}d", status);
112 }
113 return status;
114 }
115
GetService(const char * serviceName)116 sptr<IRemoteObject> ServiceManagerProxy::GetService(const char *serviceName)
117 {
118 MessageParcel data;
119 MessageParcel reply;
120 if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteCString(serviceName)) {
121 return nullptr;
122 }
123
124 MessageOption option;
125 std::unique_lock<std::mutex> lock(g_remoteMutex);
126 int status = Remote()->SendRequest(
127 static_cast<uint32_t>(HdfDeviceManagerInterfaceCode::DEVSVC_MANAGER_GET_SERVICE), data, reply, option);
128 lock.unlock();
129 if (status) {
130 HDF_LOGE("get hdi service %{public}s failed, %{public}d", serviceName, status);
131 return nullptr;
132 }
133 HDF_LOGD("get hdi service %{public}s success ", serviceName);
134 return reply.ReadRemoteObject();
135 }
136
HdfDevMgrDbgFillServiceInfo(std::vector<HdiServiceInfo> & serviceInfos,MessageParcel & reply)137 static void HdfDevMgrDbgFillServiceInfo(std::vector<HdiServiceInfo> &serviceInfos, MessageParcel &reply)
138 {
139 while (true) {
140 HdiServiceInfo info;
141 const char *servName = reply.ReadCString();
142 if (servName == nullptr) {
143 break;
144 }
145 info.serviceName = servName;
146 info.devClass = reply.ReadUint16();
147 info.devId = reply.ReadUint32();
148 serviceInfos.push_back(info);
149 }
150 return;
151 }
152
ListAllService(std::vector<HdiServiceInfo> & serviceInfos)153 int32_t ServiceManagerProxy::ListAllService(std::vector<HdiServiceInfo> &serviceInfos)
154 {
155 MessageParcel data;
156 MessageParcel reply;
157 if (!data.WriteInterfaceToken(GetDescriptor())) {
158 return HDF_FAILURE;
159 }
160
161 MessageOption option;
162 std::unique_lock<std::mutex> lock(g_remoteMutex);
163 int status = Remote()->SendRequest(
164 static_cast<uint32_t>(HdfDeviceManagerInterfaceCode::DEVSVC_MANAGER_LIST_ALL_SERVICE), data, reply, option);
165 lock.unlock();
166 if (status != HDF_SUCCESS) {
167 HDF_LOGE("list all service info failed, %{public}d", status);
168 return status;
169 } else {
170 HdfDevMgrDbgFillServiceInfo(serviceInfos, reply);
171 }
172 HDF_LOGD("get all service info success");
173 return status;
174 }
175
ListServiceByInterfaceDesc(std::vector<std::string> & serviceNames,const char * interfaceDesc)176 int32_t ServiceManagerProxy::ListServiceByInterfaceDesc(
177 std::vector<std::string> &serviceNames, const char *interfaceDesc)
178 {
179 MessageParcel data;
180 MessageParcel reply;
181 if (interfaceDesc == nullptr || strlen(interfaceDesc) == 0) {
182 HDF_LOGE("%{public}s: invalid parameter, interfaceDesc is null or empty", __func__);
183 return HDF_ERR_INVALID_PARAM;
184 }
185 if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteCString(interfaceDesc)) {
186 return HDF_FAILURE;
187 }
188
189 MessageOption option;
190 std::unique_lock<std::mutex> lock(g_remoteMutex);
191 int status = Remote()->SendRequest(
192 static_cast<uint32_t>(HdfDeviceManagerInterfaceCode::DEVSVC_MANAGER_LIST_SERVICE_BY_INTERFACEDESC), data, reply,
193 option);
194 lock.unlock();
195 if (status != HDF_SUCCESS) {
196 HDF_LOGE("get hdi service collection by %{public}s failed, %{public}d", interfaceDesc, status);
197 return status;
198 }
199
200 uint32_t serviceNum = 0;
201 if (!reply.ReadUint32(serviceNum)) {
202 HDF_LOGE("failed to read number of service");
203 return HDF_ERR_INVALID_PARAM;
204 }
205
206 if (serviceNum > serviceNames.max_size()) {
207 HDF_LOGE("invalid len of serviceNames");
208 return HDF_ERR_INVALID_PARAM;
209 }
210
211 for (uint32_t i = 0; i < serviceNum; i++) {
212 if (reply.GetReadableBytes() == 0) {
213 HDF_LOGE("no enough data to read");
214 return HDF_ERR_INVALID_PARAM;
215 }
216
217 const char *serviceName = reply.ReadCString();
218 if (serviceName == NULL) {
219 break;
220 }
221 serviceNames.push_back(serviceName);
222 }
223
224 HDF_LOGD("get hdi service collection by %{public}s successfully", interfaceDesc);
225 return status;
226 }
227 } // namespace V1_0
228 } // namespace ServiceManager
229 } // namespace HDI
230 } // namespace OHOS