• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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 bool 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         if (!reply.ReadUint32(hostInfo.hostId)) {
113             HDF_LOGE("failed to read hostId of DevInfo");
114             return false;
115         }
116 
117         if (!reply.ReadUint32(devCnt)) {
118             HDF_LOGE("failed to read size of DevInfo");
119             return false;
120         }
121 
122         if (devCnt > hostInfo.devInfo.max_size()) {
123             HDF_LOGE("invalid len of device info");
124             return false;
125         }
126 
127         for (uint32_t i = 0; i < devCnt; i++) {
128             if (reply.GetReadableBytes() == 0) {
129                 HDF_LOGE("no enough data to read");
130                 return HDF_ERR_INVALID_PARAM;
131             }
132 
133             name = reply.ReadCString();
134             devInfo.deviceName = (name == nullptr) ? "" : name;
135             if (!reply.ReadUint32(devInfo.devId)) {
136                 HDF_LOGE("failed to read devId of DevInfo");
137                 return false;
138             }
139 
140             name = reply.ReadCString();
141             devInfo.servName = (name == nullptr) ? "" : name;
142             hostInfo.devInfo.push_back(devInfo);
143         }
144         hostInfos.push_back(hostInfo);
145     }
146     return true;
147 }
148 
ListAllDevice(std::vector<HdiDevHostInfo> & deviceInfos)149 int32_t DeviceManagerProxy::ListAllDevice(std::vector<HdiDevHostInfo> &deviceInfos)
150 {
151     MessageParcel data;
152     MessageParcel reply;
153 
154     if (!data.WriteInterfaceToken(GetDescriptor())) {
155         return HDF_FAILURE;
156     }
157 
158     MessageOption option;
159     std::unique_lock<std::mutex> lock(g_remoteMutex);
160     int status = Remote()->SendRequest(DEVMGR_SERVICE_LIST_ALL_DEVICE, data, reply, option);
161     lock.unlock();
162     if (status != HDF_SUCCESS) {
163         HDF_LOGE("list all device info failed, %{public}d", status);
164         return status;
165     }
166 
167     if (!HdfDevMgrDbgFillDeviceInfo(deviceInfos, reply)) {
168         HDF_LOGE("failed to read all device info");
169         return HDF_ERR_INVALID_PARAM;
170     }
171     return status;
172 }
173 
Get()174 sptr<IDeviceManager> IDeviceManager::Get()
175 {
176     auto servmgr = ServiceManager::V1_0::IServiceManager::Get();
177     if (servmgr == nullptr) {
178         HDF_LOGE("failed to get hdi service manager");
179         return nullptr;
180     }
181 
182     std::unique_lock<std::mutex> lock(g_remoteMutex);
183     sptr<IRemoteObject> remote = servmgr->GetService("hdf_device_manager");
184     if (remote != nullptr) {
185         return hdi_facecast<IDeviceManager>(remote);
186     }
187 
188     HDF_LOGE("hdf device manager not exist");
189     return nullptr;
190 }
191 } // namespace V1_0
192 } // namespace DeviceManager
193 } // namespace HDI
194 } // namespace OHOS