• 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_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 
CreateDevice(uint32_t maxX,uint32_t maxY,uint32_t maxPressure)146 UsbErrCode DriverExtMgrProxy::CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure)
147 {
148     sptr<IRemoteObject> remote = Remote();
149     if (remote == nullptr) {
150         EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
151         return UsbErrCode::EDM_ERR_INVALID_OBJECT;
152     }
153 
154     MessageParcel data;
155     MessageParcel reply;
156     MessageOption option;
157 
158     if (!data.WriteInterfaceToken(GetDescriptor())) {
159         EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
160         return UsbErrCode::EDM_ERR_INVALID_PARAM;
161     }
162 
163     if (!data.WriteUint32(maxX)) {
164         EDM_LOGE(MODULE_FRAMEWORK, "failed to write maxX");
165         return UsbErrCode::EDM_ERR_INVALID_PARAM;
166     }
167 
168     if (!data.WriteUint32(maxY)) {
169         EDM_LOGE(MODULE_FRAMEWORK, "failed to write maxY");
170         return UsbErrCode::EDM_ERR_INVALID_PARAM;
171     }
172 
173     if (!data.WriteUint32(maxPressure)) {
174         EDM_LOGE(MODULE_FRAMEWORK, "failed to write maxPressure");
175         return UsbErrCode::EDM_ERR_INVALID_PARAM;
176     }
177 
178     int32_t ret =
179         remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_CREATE_DEVICE), data, reply, option);
180     if (ret != UsbErrCode::EDM_OK) {
181         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
182         return static_cast<UsbErrCode>(ret);
183     }
184     return UsbErrCode::EDM_OK;
185 }
186 
EmitEvent(int32_t deviceId,const std::vector<EmitItem> & items)187 UsbErrCode DriverExtMgrProxy::EmitEvent(int32_t deviceId, const std::vector<EmitItem> &items)
188 {
189     sptr<IRemoteObject> remote = Remote();
190     if (remote == nullptr) {
191         EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
192         return UsbErrCode::EDM_ERR_INVALID_OBJECT;
193     }
194 
195     MessageParcel data;
196     MessageParcel reply;
197     MessageOption option;
198 
199     if (!data.WriteInterfaceToken(GetDescriptor())) {
200         EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
201         return UsbErrCode::EDM_ERR_INVALID_PARAM;
202     }
203 
204     if (!EmitItemMarshalling(deviceId, items, data)) {
205         EDM_LOGE(MODULE_FRAMEWORK, "failed to marshall EmitItem");
206         return UsbErrCode::EDM_ERR_INVALID_PARAM;
207     }
208 
209     int32_t ret =
210         remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_EMIT_EVENT), data, reply, option);
211     if (ret != UsbErrCode::EDM_OK) {
212         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
213         return static_cast<UsbErrCode>(ret);
214     }
215     return UsbErrCode::EDM_OK;
216 }
217 
DestroyDevice(void)218 UsbErrCode DriverExtMgrProxy::DestroyDevice(void)
219 {
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     int32_t ret = remote->SendRequest(
236         static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_DESTROY_DEVICE), data, reply, option);
237     if (ret != UsbErrCode::EDM_OK) {
238         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
239         return static_cast<UsbErrCode>(ret);
240     }
241     return UsbErrCode::EDM_OK;
242 }
243 } // namespace ExternalDeviceManager
244 } // namespace OHOS
245