• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <hdf_base.h>
17 #include <hdf_log.h>
18 #include <iproxy_broker.h>
19 #include <iservice_registry.h>
20 #include <object_collector.h>
21 
22 #include "idevmgr_hdi.h"
23 #include "iservmgr_hdi.h"
24 
25 #define HDF_LOG_TAG idevmgr_client
26 
27 namespace OHOS {
28 namespace HDI {
29 namespace DeviceManager {
30 namespace V1_0 {
31 enum DevmgrCmdId : uint32_t {
32     DEVMGR_SERVICE_ATTACH_DEVICE_HOST = 1,
33     DEVMGR_SERVICE_ATTACH_DEVICE,
34     DEVMGR_SERVICE_DETACH_DEVICE,
35     DEVMGR_SERVICE_LOAD_DEVICE,
36     DEVMGR_SERVICE_UNLOAD_DEVICE,
37     DEVMGR_SERVICE_QUERY_DEVICE,
38     DEVMGR_SERVICE_LIST_ALL_DEVICE,
39 };
40 
41 class DeviceManagerProxy : public IProxyBroker<IDeviceManager> {
42 public:
DeviceManagerProxy(const sptr<IRemoteObject> & impl)43     explicit DeviceManagerProxy(const sptr<IRemoteObject> &impl) : IProxyBroker<IDeviceManager>(impl) {}
~DeviceManagerProxy()44     ~DeviceManagerProxy() {}
45     int32_t LoadDevice(const std::string &serviceName) override;
46     int32_t UnloadDevice(const std::string &serviceName) override;
47     int32_t ListAllDevice(std::vector<HdiDevHostInfo> &deviceInfos) override;
48 
49 private:
50     static inline BrokerDelegator<DeviceManagerProxy> delegator_;
51 };
52 
LoadDevice(const std::string & serviceName)53 int32_t DeviceManagerProxy::LoadDevice(const std::string &serviceName)
54 {
55     MessageParcel data;
56     MessageParcel reply;
57     MessageOption option;
58     HDF_LOGI("load device: %{public}s", serviceName.data());
59     if (!data.WriteInterfaceToken(GetDescriptor())) {
60         return HDF_FAILURE;
61     }
62     if (!data.WriteCString(serviceName.data())) {
63         return HDF_FAILURE;
64     }
65 
66     int status = Remote()->SendRequest(DEVMGR_SERVICE_LOAD_DEVICE, data, reply, option);
67     if (status != HDF_SUCCESS) {
68         HDF_LOGE("load device failed, %{public}d", status);
69     }
70     return status;
71 }
72 
UnloadDevice(const std::string & serviceName)73 int32_t DeviceManagerProxy::UnloadDevice(const std::string &serviceName)
74 {
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option;
78     HDF_LOGI("unload device: %{public}s", serviceName.data());
79     if (!data.WriteInterfaceToken(DeviceManagerProxy::GetDescriptor())) {
80         return HDF_FAILURE;
81     }
82     if (!data.WriteCString(serviceName.data())) {
83         return HDF_FAILURE;
84     }
85 
86     int status = Remote()->SendRequest(DEVMGR_SERVICE_UNLOAD_DEVICE, data, reply, option);
87     if (status != HDF_SUCCESS) {
88         HDF_LOGE("unload device failed, %{public}d", status);
89     }
90     return status;
91 }
92 
HdfDevMgrDbgFillDeviceInfo(std::vector<HdiDevHostInfo> & hostInfos,MessageParcel & reply)93 static void HdfDevMgrDbgFillDeviceInfo(std::vector<HdiDevHostInfo> &hostInfos, MessageParcel &reply)
94 {
95     while (true) {
96         struct DevInfo devInfo;
97         uint32_t devCnt;
98         struct HdiDevHostInfo hostInfo;
99         const char *name = reply.ReadCString();
100         if (name == nullptr) {
101             break;
102         }
103         hostInfo.hostName = name;
104         hostInfo.hostId = reply.ReadUint32();
105         devCnt = reply.ReadUint32();
106         for (uint32_t i = 0; i < devCnt; i++) {
107             name = reply.ReadCString();
108             devInfo.deviceName = (name == nullptr) ? "" : name;
109             devInfo.devId = reply.ReadUint32();
110             name = reply.ReadCString();
111             devInfo.servName = (name == nullptr) ? "" : name;
112             hostInfo.devInfo.push_back(devInfo);
113         }
114         hostInfos.push_back(hostInfo);
115     }
116     return;
117 }
118 
ListAllDevice(std::vector<HdiDevHostInfo> & deviceInfos)119 int32_t DeviceManagerProxy::ListAllDevice(std::vector<HdiDevHostInfo> &deviceInfos)
120 {
121     MessageParcel data;
122     MessageParcel reply;
123 
124     if (!data.WriteInterfaceToken(GetDescriptor())) {
125         return HDF_FAILURE;
126     }
127 
128     MessageOption option;
129     int status = Remote()->SendRequest(DEVMGR_SERVICE_LIST_ALL_DEVICE, data, reply, option);
130     if (status != HDF_SUCCESS) {
131         HDF_LOGE("list all device info failed, %{public}d", status);
132     } else {
133         HdfDevMgrDbgFillDeviceInfo(deviceInfos, reply);
134     }
135     return status;
136 }
137 
Get()138 sptr<IDeviceManager> IDeviceManager::Get()
139 {
140     auto servmgr = ServiceManager::V1_0::IServiceManager::Get();
141     if (servmgr == nullptr) {
142         HDF_LOGE("failed to get hdi service manager");
143         return nullptr;
144     }
145     sptr<IRemoteObject> remote = servmgr->GetService("hdf_device_manager");
146     if (remote != nullptr) {
147         return hdi_facecast<IDeviceManager>(remote);
148     }
149 
150     HDF_LOGE("hdf device manager not exist");
151     return nullptr;
152 }
153 } // namespace V1_0
154 } // namespace DeviceManager
155 } // namespace HDI
156 } // namespace OHOS