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 <iservice_registry.h>
22 #include <object_collector.h>
23
24 #include "idevmgr_hdi.h"
25 #include "iservmgr_hdi.h"
26
27 #define HDF_LOG_TAG idevmgr_client
28
29 namespace OHOS {
30 namespace HDI {
31 namespace DeviceManager {
32 namespace V1_0 {
33 std::mutex g_remoteMutex;
34
35 enum DevmgrCmdId : uint32_t {
36 DEVMGR_SERVICE_ATTACH_DEVICE_HOST = 1,
37 DEVMGR_SERVICE_ATTACH_DEVICE,
38 DEVMGR_SERVICE_DETACH_DEVICE,
39 DEVMGR_SERVICE_LOAD_DEVICE,
40 DEVMGR_SERVICE_UNLOAD_DEVICE,
41 DEVMGR_SERVICE_QUERY_DEVICE,
42 DEVMGR_SERVICE_LIST_ALL_DEVICE,
43 };
44
45 class DeviceManagerProxy : public IProxyBroker<IDeviceManager> {
46 public:
DeviceManagerProxy(const sptr<IRemoteObject> & impl)47 explicit DeviceManagerProxy(const sptr<IRemoteObject> &impl) : IProxyBroker<IDeviceManager>(impl) {}
~DeviceManagerProxy()48 ~DeviceManagerProxy() {}
49 int32_t LoadDevice(const std::string &serviceName) override;
50 int32_t UnloadDevice(const std::string &serviceName) override;
51 int32_t ListAllDevice(std::vector<HdiDevHostInfo> &deviceInfos) override;
52
53 private:
54 static inline BrokerDelegator<DeviceManagerProxy> delegator_;
55 };
56
LoadDevice(const std::string & serviceName)57 int32_t DeviceManagerProxy::LoadDevice(const std::string &serviceName)
58 {
59 MessageParcel data;
60 MessageParcel reply;
61 MessageOption option;
62 HDF_LOGI("load device: %{public}s", serviceName.data());
63 if (!data.WriteInterfaceToken(GetDescriptor())) {
64 return HDF_FAILURE;
65 }
66 if (!data.WriteCString(serviceName.data())) {
67 return HDF_FAILURE;
68 }
69
70 std::unique_lock<std::mutex> lock(g_remoteMutex);
71 if (Remote() == nullptr) {
72 HDF_LOGE("invalid param Remote()");
73 return HDF_ERR_INVALID_PARAM;
74 }
75 int status = Remote()->SendRequest(DEVMGR_SERVICE_LOAD_DEVICE, data, reply, option);
76 lock.unlock();
77 if (status != HDF_SUCCESS) {
78 HDF_LOGE("load device failed, %{public}d", status);
79 }
80 return status;
81 }
82
UnloadDevice(const std::string & serviceName)83 int32_t DeviceManagerProxy::UnloadDevice(const std::string &serviceName)
84 {
85 MessageParcel data;
86 MessageParcel reply;
87 MessageOption option;
88 HDF_LOGI("unload device: %{public}s", serviceName.data());
89 if (!data.WriteInterfaceToken(DeviceManagerProxy::GetDescriptor())) {
90 return HDF_FAILURE;
91 }
92 if (!data.WriteCString(serviceName.data())) {
93 return HDF_FAILURE;
94 }
95
96 std::unique_lock<std::mutex> lock(g_remoteMutex);
97 if (Remote() == nullptr) {
98 HDF_LOGE("invalid param Remote()");
99 return HDF_ERR_INVALID_PARAM;
100 }
101 int status = Remote()->SendRequest(DEVMGR_SERVICE_UNLOAD_DEVICE, data, reply, option);
102 lock.unlock();
103 if (status != HDF_SUCCESS) {
104 HDF_LOGE("unload device failed, %{public}d", status);
105 }
106 return status;
107 }
108
HdfDevMgrDbgFillDeviceInfo(std::vector<HdiDevHostInfo> & hostInfos,MessageParcel & reply)109 static bool HdfDevMgrDbgFillDeviceInfo(std::vector<HdiDevHostInfo> &hostInfos, MessageParcel &reply)
110 {
111 while (true) {
112 struct DevInfo devInfo;
113 uint32_t devCnt;
114 struct HdiDevHostInfo hostInfo;
115 const char *name = reply.ReadCString();
116 if (name == nullptr) {
117 break;
118 }
119 hostInfo.hostName = name;
120 if (!reply.ReadUint32(hostInfo.hostId)) {
121 HDF_LOGE("failed to read hostId of DevInfo");
122 return false;
123 }
124
125 if (!reply.ReadUint32(devCnt)) {
126 HDF_LOGE("failed to read size of DevInfo");
127 return false;
128 }
129
130 if (devCnt > hostInfo.devInfo.max_size()) {
131 HDF_LOGE("invalid len of device info");
132 return false;
133 }
134
135 for (uint32_t i = 0; i < devCnt; i++) {
136 if (reply.GetReadableBytes() == 0) {
137 HDF_LOGE("no enough data to read");
138 return false;
139 }
140
141 name = reply.ReadCString();
142 devInfo.deviceName = (name == nullptr) ? "" : name;
143 if (!reply.ReadUint32(devInfo.devId)) {
144 HDF_LOGE("failed to read devId of DevInfo");
145 return false;
146 }
147
148 name = reply.ReadCString();
149 devInfo.servName = (name == nullptr) ? "" : name;
150 hostInfo.devInfo.push_back(devInfo);
151 }
152 hostInfos.push_back(hostInfo);
153 }
154 return true;
155 }
156
ListAllDevice(std::vector<HdiDevHostInfo> & deviceInfos)157 int32_t DeviceManagerProxy::ListAllDevice(std::vector<HdiDevHostInfo> &deviceInfos)
158 {
159 MessageParcel data;
160 MessageParcel reply;
161
162 if (!data.WriteInterfaceToken(GetDescriptor())) {
163 return HDF_FAILURE;
164 }
165
166 MessageOption option;
167 std::unique_lock<std::mutex> lock(g_remoteMutex);
168 if (Remote() == nullptr) {
169 HDF_LOGE("invalid param Remote()");
170 return HDF_ERR_INVALID_PARAM;
171 }
172 int status = Remote()->SendRequest(DEVMGR_SERVICE_LIST_ALL_DEVICE, data, reply, option);
173 lock.unlock();
174 if (status != HDF_SUCCESS) {
175 HDF_LOGE("list all device info failed, %{public}d", status);
176 return status;
177 }
178
179 if (!HdfDevMgrDbgFillDeviceInfo(deviceInfos, reply)) {
180 HDF_LOGE("failed to read all device info");
181 return HDF_ERR_INVALID_PARAM;
182 }
183 return status;
184 }
185
Get()186 sptr<IDeviceManager> IDeviceManager::Get()
187 {
188 auto servmgr = ServiceManager::V1_0::IServiceManager::Get();
189 if (servmgr == nullptr) {
190 HDF_LOGE("failed to get hdi service manager");
191 return nullptr;
192 }
193
194 std::unique_lock<std::mutex> lock(g_remoteMutex);
195 sptr<IRemoteObject> remote = servmgr->GetService("hdf_device_manager");
196 if (remote != nullptr) {
197 return hdi_facecast<IDeviceManager>(remote);
198 }
199
200 HDF_LOGE("hdf device manager not exist");
201 return nullptr;
202 }
203 } // namespace V1_0
204 } // namespace DeviceManager
205 } // namespace HDI
206 } // namespace OHOS
207