1 /*
2 * Copyright (c) 2021 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 <iservice_registry.h>
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include "idevmgr_hdi.h"
20 #include "iservmgr_hdi.h"
21
22 #define HDF_LOG_TAG idevmgr_client
23
24 namespace OHOS {
25 namespace HDI {
26 namespace DeviceManager {
27 namespace V1_0 {
28 enum DevngrCmdId : uint32_t {
29 DEVMGR_SERVICE_ATTACH_DEVICE_HOST = 1,
30 DEVMGR_SERVICE_ATTACH_DEVICE,
31 DEVMGR_SERVICE_DETACH_DEVICE,
32 DEVMGR_SERVICE_LOAD_DEVICE,
33 DEVMGR_SERVICE_UNLOAD_DEVICE,
34 DEVMGR_SERVICE_QUERY_DEVICE,
35 };
36
37 class DeviceManagerProxy : public IRemoteProxy<IDeviceManager> {
38 public:
DeviceManagerProxy(const sptr<IRemoteObject> & impl)39 explicit DeviceManagerProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<IDeviceManager>(impl) {}
~DeviceManagerProxy()40 ~DeviceManagerProxy() {}
41 virtual int32_t LoadDevice(const std::string &serviceName);
42 virtual int32_t UnloadDevice(const std::string &serviceName);
43 private:
44 static inline BrokerDelegator<DeviceManagerProxy> delegator_;
45 };
46
LoadDevice(const std::string & serviceName)47 int32_t DeviceManagerProxy::LoadDevice(const std::string &serviceName)
48 {
49 MessageParcel data;
50 MessageParcel reply;
51 MessageOption option;
52 HDF_LOGI("load device: %{public}s", serviceName.data());
53 if (!data.WriteInterfaceToken(DeviceManagerProxy::GetDescriptor())) {
54 return HDF_FAILURE;
55 }
56 if (!data.WriteCString(serviceName.data())) {
57 return HDF_FAILURE;
58 }
59
60 int status = Remote()->SendRequest(DEVMGR_SERVICE_LOAD_DEVICE, data, reply, option);
61 if (status) {
62 HDF_LOGE("load device failed, %{public}d", status);
63 }
64 return status;
65 }
66
UnloadDevice(const std::string & serviceName)67 int32_t DeviceManagerProxy::UnloadDevice(const std::string &serviceName)
68 {
69 MessageParcel data;
70 MessageParcel reply;
71 MessageOption option;
72 HDF_LOGI("unload device: %{public}s", serviceName.data());
73 if (!data.WriteInterfaceToken(DeviceManagerProxy::GetDescriptor())) {
74 return HDF_FAILURE;
75 }
76 if (!data.WriteCString(serviceName.data())) {
77 return HDF_FAILURE;
78 }
79
80 int status = Remote()->SendRequest(DEVMGR_SERVICE_UNLOAD_DEVICE, data, reply, option);
81 if (status) {
82 HDF_LOGE("unload device failed, %{public}d", status);
83 }
84 return status;
85 }
86
Get()87 sptr<IDeviceManager> IDeviceManager::Get()
88 {
89 auto servmgr = ServiceManager::V1_0::IServiceManager::Get();
90 if (servmgr == nullptr) {
91 HDF_LOGE("failed to get hdi service manager");
92 return nullptr;
93 }
94 sptr<IRemoteObject> remote = servmgr->GetService("hdf_device_manager");
95 if (remote != nullptr) {
96 return iface_cast<IDeviceManager>(remote);
97 }
98
99 HDF_LOGE("hdf device manager not exist");
100 return nullptr;
101 }
102 } // namespace V1_0
103 } // namespace DeviceManager
104 } // namespace HDI
105 } // namespace OHOS