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 
QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> & deviceInfos,bool isByDeviceId,const uint64_t deviceId)146 UsbErrCode DriverExtMgrProxy::QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos,
147     bool isByDeviceId, const uint64_t deviceId)
148 {
149     EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDeviceInfo start");
150     sptr<IRemoteObject> remote = Remote();
151     if (remote == nullptr) {
152         EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
153         return UsbErrCode::EDM_ERR_INVALID_OBJECT;
154     }
155 
156     MessageParcel data;
157     MessageParcel reply;
158     MessageOption option;
159 
160     if (!data.WriteInterfaceToken(GetDescriptor())) {
161         EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
162         return UsbErrCode::EDM_ERR_INVALID_PARAM;
163     }
164 
165     if (!data.WriteBool(isByDeviceId)) {
166         EDM_LOGE(MODULE_FRAMEWORK, "failed to write isByDeviceId");
167         return UsbErrCode::EDM_ERR_INVALID_PARAM;
168     }
169 
170     if (isByDeviceId && !data.WriteUint64(deviceId)) {
171         EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceId");
172         return UsbErrCode::EDM_ERR_INVALID_PARAM;
173     }
174 
175     int32_t ret =
176         remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DEVICE_INFO), data, reply, option);
177     if (ret != UsbErrCode::EDM_OK) {
178         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
179         return static_cast<UsbErrCode>(ret);
180     }
181 
182     if (!DeviceInfoData::DeviceInfosUnMarshalling(reply, deviceInfos)) {
183         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceInfos");
184         return UsbErrCode::EDM_ERR_INVALID_PARAM;
185     }
186 
187     EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDeviceInfo end");
188 
189     return UsbErrCode::EDM_OK;
190 }
191 
QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> & driverInfos,bool isByDriverUid,const std::string & driverUid)192 UsbErrCode DriverExtMgrProxy::QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> &driverInfos,
193     bool isByDriverUid, const std::string &driverUid)
194 {
195     EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDriverInfo start");
196     sptr<IRemoteObject> remote = Remote();
197     if (remote == nullptr) {
198         EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
199         return UsbErrCode::EDM_ERR_INVALID_OBJECT;
200     }
201 
202     MessageParcel data;
203     MessageParcel reply;
204     MessageOption option;
205 
206     if (!data.WriteInterfaceToken(GetDescriptor())) {
207         EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
208         return UsbErrCode::EDM_ERR_INVALID_PARAM;
209     }
210 
211     if (!data.WriteBool(isByDriverUid)) {
212         EDM_LOGE(MODULE_FRAMEWORK, "failed to write isByDriverUid");
213         return UsbErrCode::EDM_ERR_INVALID_PARAM;
214     }
215 
216     if (isByDriverUid && !data.WriteString(driverUid)) {
217         EDM_LOGE(MODULE_FRAMEWORK, "failed to write driverUid");
218         return UsbErrCode::EDM_ERR_INVALID_PARAM;
219     }
220 
221     int32_t ret =
222         remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DRIVER_INFO), data, reply, option);
223     if (ret != UsbErrCode::EDM_OK) {
224         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
225         return static_cast<UsbErrCode>(ret);
226     }
227 
228     if (!DriverInfoData::DriverInfosUnMarshalling(reply, driverInfos)) {
229         EDM_LOGE(MODULE_FRAMEWORK, "failed to write driverInfos");
230         return UsbErrCode::EDM_ERR_INVALID_PARAM;
231     }
232 
233     EDM_LOGD(MODULE_FRAMEWORK, "proxy QueryDriverInfo end");
234 
235     return UsbErrCode::EDM_OK;
236 }
237 } // namespace ExternalDeviceManager
238 } // namespace OHOS
239