• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ProxyRetTranslate(int32_t proxyRet)36 static UsbErrCode ProxyRetTranslate(int32_t proxyRet)
37 {
38     UsbErrCode ret = UsbErrCode::EDM_OK;
39     switch (proxyRet) {
40         case ERR_INVALID_VALUE:
41             ret = UsbErrCode::EDM_ERR_INVALID_OBJECT;
42             break;
43         case ERR_INVALID_DATA:
44             ret = UsbErrCode::EDM_ERR_INVALID_PARAM;
45             break;
46         default:
47             ret = static_cast<UsbErrCode>(proxyRet);
48             break;
49     }
50     return ret;
51 }
52 
Connect()53 UsbErrCode DriverExtMgrClient::Connect()
54 {
55     std::lock_guard<std::mutex> lock(mutex_);
56     if (proxy_ != nullptr) {
57         return UsbErrCode::EDM_OK;
58     }
59 
60     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (sam == nullptr) {
62         EDM_LOGF(MODULE_FRAMEWORK, "Failed to obtain SystemAbilityMgr");
63         return UsbErrCode::EDM_ERR_GET_SYSTEM_ABILITY_MANAGER_FAILED;
64     }
65 
66     sptr<IRemoteObject> remote = sam->CheckSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
67     if (remote == nullptr) {
68         EDM_LOGF(MODULE_FRAMEWORK, "Check SystemAbility failed");
69         return UsbErrCode::EDM_ERR_GET_SERVICE_FAILED;
70     }
71 
72     deathRecipient_ = new DriverExtMgrDeathRecipient();
73     if (deathRecipient_ == nullptr) {
74         EDM_LOGF(MODULE_FRAMEWORK, "Failed to create DriverExtMgrDeathRecipient");
75         return UsbErrCode::EDM_ERR_INVALID_OBJECT;
76     }
77 
78     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
79         EDM_LOGF(MODULE_FRAMEWORK, "Failed to add death recipient to DriverExtMgrProxy service");
80         return EDM_ERR_NOT_SUPPORT;
81     }
82 
83     proxy_ = iface_cast<IDriverExtMgr>(remote);
84     if (proxy_ == nullptr) {
85         EDM_LOGF(MODULE_FRAMEWORK, "Failed to cast DriverExtMgrProxy object");
86     }
87     EDM_LOGI(MODULE_FRAMEWORK, "Connecting DriverExtMgrProxy success");
88     return UsbErrCode::EDM_OK;
89 }
90 
DisConnect(const wptr<IRemoteObject> & remote)91 void DriverExtMgrClient::DisConnect(const wptr<IRemoteObject> &remote)
92 {
93     std::lock_guard<std::mutex> lock(mutex_);
94     if (proxy_ == nullptr) {
95         return;
96     }
97 
98     auto serviceRemote = proxy_->AsObject();
99     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
100         serviceRemote->RemoveDeathRecipient(deathRecipient_);
101         proxy_ = nullptr;
102     }
103 }
104 
OnRemoteDied(const wptr<IRemoteObject> & remote)105 void DriverExtMgrClient::DriverExtMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
106 {
107     if (remote == nullptr) {
108         EDM_LOGF(MODULE_FRAMEWORK, "OnRemoteDied failed, remote is nullptr");
109         return;
110     }
111 
112     DriverExtMgrClient::GetInstance().DisConnect(remote);
113     EDM_LOGF(MODULE_FRAMEWORK, "received death notification of remote and finished to disconnect");
114 }
115 
QueryDevice(uint32_t busType,std::vector<std::shared_ptr<DeviceData>> & devices)116 UsbErrCode DriverExtMgrClient::QueryDevice(uint32_t busType, std::vector<std::shared_ptr<DeviceData>> &devices)
117 {
118     if (Connect() != UsbErrCode::EDM_OK) {
119         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
120     }
121     int32_t ret = EDM_OK;
122     int32_t proxyRet = proxy_->QueryDevice(ret, busType, devices);
123     if (proxyRet != ERR_OK) {
124         return ProxyRetTranslate(proxyRet);
125     }
126     return static_cast<UsbErrCode>(ret);
127 }
128 
BindDevice(uint64_t deviceId,const sptr<IDriverExtMgrCallback> & connectCallback)129 UsbErrCode DriverExtMgrClient::BindDevice(uint64_t deviceId, const sptr<IDriverExtMgrCallback> &connectCallback)
130 {
131     if (Connect() != UsbErrCode::EDM_OK) {
132         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
133     }
134     int32_t ret = EDM_OK;
135     int32_t proxyRet = proxy_->BindDevice(ret, deviceId, connectCallback);
136     if (proxyRet != ERR_OK) {
137         EDM_LOGI(MODULE_FRAMEWORK, "proxyRet = %{public}d", proxyRet);
138         return ProxyRetTranslate(proxyRet);
139     }
140     return static_cast<UsbErrCode>(ret);
141 }
142 
UnBindDevice(uint64_t deviceId)143 UsbErrCode DriverExtMgrClient::UnBindDevice(uint64_t deviceId)
144 {
145     if (Connect() != UsbErrCode::EDM_OK) {
146         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
147     }
148     int32_t ret = EDM_OK;
149     int32_t proxyRet = proxy_->UnBindDevice(ret, deviceId);
150     if (proxyRet != ERR_OK) {
151         return ProxyRetTranslate(proxyRet);
152     }
153     return static_cast<UsbErrCode>(ret);
154 }
155 
BindDriverWithDeviceId(uint64_t deviceId,const sptr<IDriverExtMgrCallback> & connectCallback)156 UsbErrCode DriverExtMgrClient::BindDriverWithDeviceId(uint64_t deviceId,
157     const sptr<IDriverExtMgrCallback> &connectCallback)
158 {
159     if (Connect() != UsbErrCode::EDM_OK) {
160         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
161     }
162     int32_t ret = EDM_OK;
163     int32_t proxyRet = proxy_->BindDriverWithDeviceId(ret, deviceId, connectCallback);
164     if (proxyRet != ERR_OK) {
165         return ProxyRetTranslate(proxyRet);
166     }
167     return static_cast<UsbErrCode>(ret);
168 }
169 
UnbindDriverWithDeviceId(uint64_t deviceId)170 UsbErrCode DriverExtMgrClient::UnbindDriverWithDeviceId(uint64_t deviceId)
171 {
172     if (Connect() != UsbErrCode::EDM_OK) {
173         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
174     }
175     int32_t ret = EDM_OK;
176     int32_t proxyRet = proxy_->UnBindDriverWithDeviceId(ret, deviceId);
177     if (proxyRet != ERR_OK) {
178         return ProxyRetTranslate(proxyRet);
179     }
180     return static_cast<UsbErrCode>(ret);
181 }
182 
QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> & deviceInfos)183 UsbErrCode DriverExtMgrClient::QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos)
184 {
185     if (Connect() != UsbErrCode::EDM_OK) {
186         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
187     }
188     int32_t ret = EDM_OK;
189     int32_t proxyRet = proxy_->QueryDeviceInfo(ret, deviceInfos, false, 0);
190     if (proxyRet != ERR_OK) {
191         return ProxyRetTranslate(proxyRet);
192     }
193     return static_cast<UsbErrCode>(ret);
194 }
195 
QueryDeviceInfo(const uint64_t deviceId,std::vector<std::shared_ptr<DeviceInfoData>> & deviceInfos)196 UsbErrCode DriverExtMgrClient::QueryDeviceInfo(const uint64_t deviceId,
197     std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos)
198 {
199     if (Connect() != UsbErrCode::EDM_OK) {
200         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
201     }
202     int32_t ret = EDM_OK;
203     int32_t proxyRet = proxy_->QueryDeviceInfo(ret, deviceInfos, true, deviceId);
204     if (proxyRet != ERR_OK) {
205         return ProxyRetTranslate(proxyRet);
206     }
207     return static_cast<UsbErrCode>(ret);
208 }
209 
QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> & driverInfos)210 UsbErrCode DriverExtMgrClient::QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> &driverInfos)
211 {
212     if (Connect() != UsbErrCode::EDM_OK) {
213         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
214     }
215     int32_t ret = EDM_OK;
216     int32_t proxyRet = proxy_->QueryDriverInfo(ret, driverInfos, false, "");
217     if (proxyRet != ERR_OK) {
218         return ProxyRetTranslate(proxyRet);
219     }
220     return static_cast<UsbErrCode>(ret);
221 }
222 
QueryDriverInfo(const std::string & driverUid,std::vector<std::shared_ptr<DriverInfoData>> & driverInfos)223 UsbErrCode DriverExtMgrClient::QueryDriverInfo(const std::string &driverUid,
224     std::vector<std::shared_ptr<DriverInfoData>> &driverInfos)
225 {
226     if (Connect() != UsbErrCode::EDM_OK) {
227         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
228     }
229     int32_t ret = EDM_OK;
230     int32_t proxyRet = proxy_->QueryDriverInfo(ret, driverInfos, true, driverUid);
231     if (proxyRet != ERR_OK) {
232         return ProxyRetTranslate(proxyRet);
233     }
234     return static_cast<UsbErrCode>(ret);
235 }
236 
NotifyUsbPeripheralFault(const std::string & domain,const std::string & faultName)237 UsbErrCode DriverExtMgrClient::NotifyUsbPeripheralFault(const std::string &domain, const std::string &faultName)
238 {
239     if (Connect() != UsbErrCode::EDM_OK) {
240         return UsbErrCode::EDM_ERR_CONNECTION_FAILED;
241     }
242 
243     int32_t ret = EDM_OK;
244     int32_t proxyRet = proxy_->NotifyUsbPeripheralFault(domain, faultName);
245     if (proxyRet != ERR_OK) {
246         EDM_LOGE(MODULE_FRAMEWORK, "Usb peripheral fault notify failed.");
247         return ProxyRetTranslate(proxyRet);
248     }
249     return static_cast<UsbErrCode>(ret);
250 }
251 
252 } // namespace ExternalDeviceManager
253 } // namespace OHOS
254