1 /*
2 * Copyright (c) 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 "driver_ext_mgr_client.h"
17 #include <if_system_ability_manager.h>
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20
21 #include "hilog_wrapper.h"
22 namespace OHOS {
23 namespace ExternalDeviceManager {
DriverExtMgrClient()24 DriverExtMgrClient::DriverExtMgrClient() {}
25
~DriverExtMgrClient()26 DriverExtMgrClient::~DriverExtMgrClient()
27 {
28 if (proxy_ != nullptr) {
29 auto remote = proxy_->AsObject();
30 if (remote != nullptr) {
31 remote->RemoveDeathRecipient(deathRecipient_);
32 }
33 }
34 }
35
Connect()36 UsbErrCode DriverExtMgrClient::Connect()
37 {
38 std::lock_guard<std::mutex> lock(mutex_);
39 if (proxy_ != nullptr) {
40 return UsbErrCode::EDM_OK;
41 }
42
43 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 if (sam == nullptr) {
45 EDM_LOGF(MODULE_FRAMEWORK, "Failed to obtain SystemAbilityMgr");
46 return UsbErrCode::EDM_ERR_GET_SYSTEM_ABILITY_MANAGER_FAILED;
47 }
48
49 sptr<IRemoteObject> remote = sam->CheckSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
50 if (remote == nullptr) {
51 EDM_LOGF(MODULE_FRAMEWORK, "Check SystemAbility failed");
52 return UsbErrCode::EDM_ERR_GET_SERVICE_FAILED;
53 }
54
55 deathRecipient_ = new DriverExtMgrDeathRecipient();
56 if (deathRecipient_ == nullptr) {
57 EDM_LOGF(MODULE_FRAMEWORK, "Failed to create DriverExtMgrDeathRecipient");
58 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
59 }
60
61 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
62 EDM_LOGF(MODULE_FRAMEWORK, "Failed to add death recipient to DriverExtMgrProxy service");
63 return EDM_ERR_NOT_SUPPORT;
64 }
65
66 proxy_ = iface_cast<IDriverExtMgr>(remote);
67 if (proxy_ == nullptr) {
68 EDM_LOGF(MODULE_FRAMEWORK, "Failed to cast DriverExtMgrProxy object");
69 }
70 EDM_LOGI(MODULE_FRAMEWORK, "Connecting DriverExtMgrProxy success");
71 return UsbErrCode::EDM_OK;
72 }
73
DisConnect(const wptr<IRemoteObject> & remote)74 void DriverExtMgrClient::DisConnect(const wptr<IRemoteObject> &remote)
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 if (proxy_ == nullptr) {
78 return;
79 }
80
81 auto serviceRemote = proxy_->AsObject();
82 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
83 serviceRemote->RemoveDeathRecipient(deathRecipient_);
84 proxy_ = nullptr;
85 }
86 }
87
OnRemoteDied(const wptr<IRemoteObject> & remote)88 void DriverExtMgrClient::DriverExtMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
89 {
90 if (remote == nullptr) {
91 EDM_LOGF(MODULE_FRAMEWORK, "OnRemoteDied failed, remote is nullptr");
92 return;
93 }
94
95 DriverExtMgrClient::GetInstance().DisConnect(remote);
96 EDM_LOGF(MODULE_FRAMEWORK, "received death notification of remote and finished to disconnect");
97 }
98
QueryDevice(uint32_t busType,std::vector<std::shared_ptr<DeviceData>> & devices)99 UsbErrCode DriverExtMgrClient::QueryDevice(uint32_t busType, std::vector<std::shared_ptr<DeviceData>> &devices)
100 {
101 if (Connect() != UsbErrCode::EDM_OK) {
102 return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
103 }
104 return proxy_->QueryDevice(busType, devices);
105 }
106
BindDevice(uint64_t deviceId,const sptr<IDriverExtMgrCallback> & connectCallback)107 UsbErrCode DriverExtMgrClient::BindDevice(uint64_t deviceId, const sptr<IDriverExtMgrCallback> &connectCallback)
108 {
109 if (Connect() != UsbErrCode::EDM_OK) {
110 return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
111 }
112 return proxy_->BindDevice(deviceId, connectCallback);
113 }
114
UnBindDevice(uint64_t deviceId)115 UsbErrCode DriverExtMgrClient::UnBindDevice(uint64_t deviceId)
116 {
117 if (Connect() != UsbErrCode::EDM_OK) {
118 return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
119 }
120 return proxy_->UnBindDevice(deviceId);
121 }
122
QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> & deviceInfos)123 UsbErrCode DriverExtMgrClient::QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos)
124 {
125 if (Connect() != UsbErrCode::EDM_OK) {
126 return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
127 }
128 return proxy_->QueryDeviceInfo(deviceInfos);
129 }
130
QueryDeviceInfo(const uint64_t deviceId,std::vector<std::shared_ptr<DeviceInfoData>> & deviceInfos)131 UsbErrCode DriverExtMgrClient::QueryDeviceInfo(const uint64_t deviceId,
132 std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos)
133 {
134 if (Connect() != UsbErrCode::EDM_OK) {
135 return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
136 }
137 return proxy_->QueryDeviceInfo(deviceInfos, true, deviceId);
138 }
139
QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> & driverInfos)140 UsbErrCode DriverExtMgrClient::QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> &driverInfos)
141 {
142 if (Connect() != UsbErrCode::EDM_OK) {
143 return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
144 }
145 return proxy_->QueryDriverInfo(driverInfos);
146 }
147
QueryDriverInfo(const std::string & driverUid,std::vector<std::shared_ptr<DriverInfoData>> & driverInfos)148 UsbErrCode DriverExtMgrClient::QueryDriverInfo(const std::string &driverUid,
149 std::vector<std::shared_ptr<DriverInfoData>> &driverInfos)
150 {
151 if (Connect() != UsbErrCode::EDM_OK) {
152 return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
153 }
154 return proxy_->QueryDriverInfo(driverInfos, true, driverUid);
155 }
156
157 } // namespace ExternalDeviceManager
158 } // namespace OHOS