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_proxy.h"
17 #include <cinttypes>
18
19 #include "hilog_wrapper.h"
20 #include "message_parcel.h"
21 #include "securec.h"
22
23 namespace OHOS {
24 namespace ExternalDeviceManager {
QueryDevice(uint32_t busType,std::vector<std::shared_ptr<DeviceData>> & devices)25 UsbErrCode DriverExtMgrProxy::QueryDevice(uint32_t busType, std::vector<std::shared_ptr<DeviceData>> &devices)
26 {
27 sptr<IRemoteObject> remote = Remote();
28 if (remote == nullptr) {
29 EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
30 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
31 }
32
33 MessageParcel data;
34 MessageParcel reply;
35 MessageOption option;
36
37 if (!data.WriteInterfaceToken(GetDescriptor())) {
38 EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
39 return UsbErrCode::EDM_ERR_INVALID_PARAM;
40 }
41
42 if (!data.WriteUint32(busType)) {
43 EDM_LOGE(MODULE_FRAMEWORK, "failed to write busType");
44 return UsbErrCode::EDM_ERR_INVALID_PARAM;
45 }
46
47 int32_t ret =
48 remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DEVICE), data, reply, option);
49 if (ret != UsbErrCode::EDM_OK) {
50 EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
51 return static_cast<UsbErrCode>(ret);
52 }
53
54 uint64_t deviceInfoSize = 0;
55 if (!reply.ReadUint64(deviceInfoSize)) {
56 EDM_LOGE(MODULE_FRAMEWORK, "failed to read size of DeviceData");
57 return UsbErrCode::EDM_ERR_INVALID_PARAM;
58 }
59
60 if (deviceInfoSize > devices.max_size()) {
61 EDM_LOGE(MODULE_FRAMEWORK, "invalid size of DeviceData");
62 return UsbErrCode::EDM_ERR_INVALID_PARAM;
63 }
64
65 for (uint64_t i = 0; i < deviceInfoSize; i++) {
66 std::shared_ptr<DeviceData> device = DeviceData::UnMarshalling(reply);
67 if (device == nullptr) {
68 EDM_LOGE(MODULE_FRAMEWORK, "failed to read %{public}016" PRIX64 " device", i);
69 return UsbErrCode::EDM_ERR_INVALID_PARAM;
70 }
71 devices.push_back(std::move(device));
72 }
73
74 return UsbErrCode::EDM_OK;
75 }
76
BindDevice(uint64_t deviceId,const sptr<IDriverExtMgrCallback> & connectCallback)77 UsbErrCode DriverExtMgrProxy::BindDevice(uint64_t deviceId, const sptr<IDriverExtMgrCallback> &connectCallback)
78 {
79 sptr<IRemoteObject> remote = Remote();
80 if (remote == nullptr) {
81 EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
82 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
83 }
84
85 MessageParcel data;
86 MessageParcel reply;
87 MessageOption option;
88
89 if (!data.WriteInterfaceToken(GetDescriptor())) {
90 EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
91 return UsbErrCode::EDM_ERR_INVALID_PARAM;
92 }
93
94 if (!data.WriteUint64(deviceId)) {
95 EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceId");
96 return UsbErrCode::EDM_ERR_INVALID_PARAM;
97 }
98
99 if (connectCallback == nullptr || !data.WriteRemoteObject(connectCallback->AsObject())) {
100 EDM_LOGE(MODULE_FRAMEWORK, "failed to write connectCallback object");
101 return UsbErrCode::EDM_ERR_INVALID_PARAM;
102 }
103
104 int32_t ret =
105 remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::BIND_DEVICE), data, reply, option);
106 if (ret != UsbErrCode::EDM_OK) {
107 EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
108 return static_cast<UsbErrCode>(ret);
109 }
110
111 return UsbErrCode::EDM_OK;
112 }
113
UnBindDevice(uint64_t deviceId)114 UsbErrCode DriverExtMgrProxy::UnBindDevice(uint64_t deviceId)
115 {
116 sptr<IRemoteObject> remote = Remote();
117 if (remote == nullptr) {
118 EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
119 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
120 }
121
122 MessageParcel data;
123 MessageParcel reply;
124 MessageOption option;
125
126 if (!data.WriteInterfaceToken(GetDescriptor())) {
127 EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
128 return UsbErrCode::EDM_ERR_INVALID_PARAM;
129 }
130
131 if (!data.WriteUint64(deviceId)) {
132 EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceId");
133 return UsbErrCode::EDM_ERR_INVALID_PARAM;
134 }
135
136 int32_t ret =
137 remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::UNBIND_DEVICE), data, reply, option);
138 if (ret != UsbErrCode::EDM_OK) {
139 EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
140 return static_cast<UsbErrCode>(ret);
141 }
142
143 return UsbErrCode::EDM_OK;
144 }
145
BindDriverWithDeviceId(uint64_t deviceId,const sptr<IDriverExtMgrCallback> & connectCallback)146 UsbErrCode DriverExtMgrProxy::BindDriverWithDeviceId(uint64_t deviceId,
147 const sptr<IDriverExtMgrCallback> &connectCallback)
148 {
149 sptr<IRemoteObject> remote = Remote();
150 if (remote == nullptr) {
151 EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
152 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
153 }
154
155 MessageParcel data;
156 MessageParcel reply;
157 MessageOption option;
158
159 if (!data.WriteInterfaceToken(GetDescriptor())) {
160 EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
161 return UsbErrCode::EDM_ERR_INVALID_PARAM;
162 }
163
164 if (!data.WriteUint64(deviceId)) {
165 EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceId");
166 return UsbErrCode::EDM_ERR_INVALID_PARAM;
167 }
168
169 if (connectCallback == nullptr || !data.WriteRemoteObject(connectCallback->AsObject())) {
170 EDM_LOGE(MODULE_FRAMEWORK, "failed to write connectCallback object");
171 return UsbErrCode::EDM_ERR_INVALID_PARAM;
172 }
173
174 int32_t ret = remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::BIND_DRIVER_WITH_DEVICE_ID),
175 data, reply, option);
176 if (ret != UsbErrCode::EDM_OK) {
177 EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
178 return static_cast<UsbErrCode>(ret);
179 }
180
181 return UsbErrCode::EDM_OK;
182 }
183
UnbindDriverWithDeviceId(uint64_t deviceId)184 UsbErrCode DriverExtMgrProxy::UnbindDriverWithDeviceId(uint64_t deviceId)
185 {
186 sptr<IRemoteObject> remote = Remote();
187 if (remote == nullptr) {
188 EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
189 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
190 }
191
192 MessageParcel data;
193 MessageParcel reply;
194 MessageOption option;
195
196 if (!data.WriteInterfaceToken(GetDescriptor())) {
197 EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
198 return UsbErrCode::EDM_ERR_INVALID_PARAM;
199 }
200
201 if (!data.WriteUint64(deviceId)) {
202 EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceId");
203 return UsbErrCode::EDM_ERR_INVALID_PARAM;
204 }
205
206 int32_t ret = remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::UNBIND_DRIVER_WITH_DEVICE_ID),
207 data, reply, option);
208 if (ret != UsbErrCode::EDM_OK) {
209 EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
210 return static_cast<UsbErrCode>(ret);
211 }
212
213 return UsbErrCode::EDM_OK;
214 }
215
QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> & deviceInfos,bool isByDeviceId,const uint64_t deviceId)216 UsbErrCode DriverExtMgrProxy::QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos,
217 bool isByDeviceId, const uint64_t deviceId)
218 {
219 EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDeviceInfo start");
220 sptr<IRemoteObject> remote = Remote();
221 if (remote == nullptr) {
222 EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
223 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
224 }
225
226 MessageParcel data;
227 MessageParcel reply;
228 MessageOption option;
229
230 if (!data.WriteInterfaceToken(GetDescriptor())) {
231 EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
232 return UsbErrCode::EDM_ERR_INVALID_PARAM;
233 }
234
235 if (!data.WriteBool(isByDeviceId)) {
236 EDM_LOGE(MODULE_FRAMEWORK, "failed to write isByDeviceId");
237 return UsbErrCode::EDM_ERR_INVALID_PARAM;
238 }
239
240 if (isByDeviceId && !data.WriteUint64(deviceId)) {
241 EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceId");
242 return UsbErrCode::EDM_ERR_INVALID_PARAM;
243 }
244
245 int32_t ret =
246 remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DEVICE_INFO), data, reply, option);
247 if (ret != UsbErrCode::EDM_OK) {
248 EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
249 return static_cast<UsbErrCode>(ret);
250 }
251
252 if (!DeviceInfoData::DeviceInfosUnMarshalling(reply, deviceInfos)) {
253 EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceInfos");
254 return UsbErrCode::EDM_ERR_INVALID_PARAM;
255 }
256
257 EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDeviceInfo end");
258
259 return UsbErrCode::EDM_OK;
260 }
261
QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> & driverInfos,bool isByDriverUid,const std::string & driverUid)262 UsbErrCode DriverExtMgrProxy::QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> &driverInfos,
263 bool isByDriverUid, const std::string &driverUid)
264 {
265 EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDriverInfo start");
266 sptr<IRemoteObject> remote = Remote();
267 if (remote == nullptr) {
268 EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
269 return UsbErrCode::EDM_ERR_INVALID_OBJECT;
270 }
271
272 MessageParcel data;
273 MessageParcel reply;
274 MessageOption option;
275
276 if (!data.WriteInterfaceToken(GetDescriptor())) {
277 EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
278 return UsbErrCode::EDM_ERR_INVALID_PARAM;
279 }
280
281 if (!data.WriteBool(isByDriverUid)) {
282 EDM_LOGE(MODULE_FRAMEWORK, "failed to write isByDriverUid");
283 return UsbErrCode::EDM_ERR_INVALID_PARAM;
284 }
285
286 if (isByDriverUid && !data.WriteString(driverUid)) {
287 EDM_LOGE(MODULE_FRAMEWORK, "failed to write driverUid");
288 return UsbErrCode::EDM_ERR_INVALID_PARAM;
289 }
290
291 int32_t ret =
292 remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DRIVER_INFO), data, reply, option);
293 if (ret != UsbErrCode::EDM_OK) {
294 EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
295 return static_cast<UsbErrCode>(ret);
296 }
297
298 if (!DriverInfoData::DriverInfosUnMarshalling(reply, driverInfos)) {
299 EDM_LOGE(MODULE_FRAMEWORK, "failed to write driverInfos");
300 return UsbErrCode::EDM_ERR_INVALID_PARAM;
301 }
302
303 EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDriverInfo end");
304
305 return UsbErrCode::EDM_OK;
306 }
307 } // namespace ExternalDeviceManager
308 } // namespace OHOS
309