1 /*
2 * Copyright (c) 2021-2022 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 int status = Remote()->SendRequest(DEVMGR_SERVICE_LOAD_DEVICE, data, reply, option);
72 lock.unlock();
73 if (status != HDF_SUCCESS) {
74 HDF_LOGE("load device failed, %{public}d", status);
75 }
76 return status;
77 }
78
UnloadDevice(const std::string & serviceName)79 int32_t DeviceManagerProxy::UnloadDevice(const std::string &serviceName)
80 {
81 MessageParcel data;
82 MessageParcel reply;
83 MessageOption option;
84 HDF_LOGI("unload device: %{public}s", serviceName.data());
85 if (!data.WriteInterfaceToken(DeviceManagerProxy::GetDescriptor())) {
86 return HDF_FAILURE;
87 }
88 if (!data.WriteCString(serviceName.data())) {
89 return HDF_FAILURE;
90 }
91
92 std::unique_lock<std::mutex> lock(g_remoteMutex);
93 int status = Remote()->SendRequest(DEVMGR_SERVICE_UNLOAD_DEVICE, data, reply, option);
94 lock.unlock();
95 if (status != HDF_SUCCESS) {
96 HDF_LOGE("unload device failed, %{public}d", status);
97 }
98 return status;
99 }
100
HdfDevMgrDbgFillDeviceInfo(std::vector<HdiDevHostInfo> & hostInfos,MessageParcel & reply)101 static void HdfDevMgrDbgFillDeviceInfo(std::vector<HdiDevHostInfo> &hostInfos, MessageParcel &reply)
102 {
103 while (true) {
104 struct DevInfo devInfo;
105 uint32_t devCnt;
106 struct HdiDevHostInfo hostInfo;
107 const char *name = reply.ReadCString();
108 if (name == nullptr) {
109 break;
110 }
111 hostInfo.hostName = name;
112 hostInfo.hostId = reply.ReadUint32();
113 devCnt = reply.ReadUint32();
114 for (uint32_t i = 0; i < devCnt; i++) {
115 name = reply.ReadCString();
116 devInfo.deviceName = (name == nullptr) ? "" : name;
117 devInfo.devId = reply.ReadUint32();
118 name = reply.ReadCString();
119 devInfo.servName = (name == nullptr) ? "" : name;
120 hostInfo.devInfo.push_back(devInfo);
121 }
122 hostInfos.push_back(hostInfo);
123 }
124 return;
125 }
126
ListAllDevice(std::vector<HdiDevHostInfo> & deviceInfos)127 int32_t DeviceManagerProxy::ListAllDevice(std::vector<HdiDevHostInfo> &deviceInfos)
128 {
129 MessageParcel data;
130 MessageParcel reply;
131
132 if (!data.WriteInterfaceToken(GetDescriptor())) {
133 return HDF_FAILURE;
134 }
135
136 MessageOption option;
137 std::unique_lock<std::mutex> lock(g_remoteMutex);
138 int status = Remote()->SendRequest(DEVMGR_SERVICE_LIST_ALL_DEVICE, data, reply, option);
139 lock.unlock();
140 if (status != HDF_SUCCESS) {
141 HDF_LOGE("list all device info failed, %{public}d", status);
142 } else {
143 HdfDevMgrDbgFillDeviceInfo(deviceInfos, reply);
144 }
145 return status;
146 }
147
Get()148 sptr<IDeviceManager> IDeviceManager::Get()
149 {
150 auto servmgr = ServiceManager::V1_0::IServiceManager::Get();
151 if (servmgr == nullptr) {
152 HDF_LOGE("failed to get hdi service manager");
153 return nullptr;
154 }
155
156 std::unique_lock<std::mutex> lock(g_remoteMutex);
157 sptr<IRemoteObject> remote = servmgr->GetService("hdf_device_manager");
158 if (remote != nullptr) {
159 return hdi_facecast<IDeviceManager>(remote);
160 }
161
162 HDF_LOGE("hdf device manager not exist");
163 return nullptr;
164 }
165 } // namespace V1_0
166 } // namespace DeviceManager
167 } // namespace HDI
168 } // namespace OHOS