• 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(Hid_Device * hidDevice,Hid_EventProperties * hidEventProperties)146 int32_t DriverExtMgrProxy::CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties)
147 {
148     sptr<IRemoteObject> remote = Remote();
149     if (remote == nullptr) {
150         EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
151         return HID_DDK_FAILURE;
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 HID_DDK_INVALID_PARAMETER;
161     }
162 
163     if (!HidDeviceMarshalling(hidDevice, data)) {
164         EDM_LOGE(MODULE_FRAMEWORK, "failed to marshall HidDevice");
165         return HID_DDK_INVALID_PARAMETER;
166     }
167 
168     if (!HidEventPropertiesMarshalling(hidEventProperties, data)) {
169         EDM_LOGE(MODULE_FRAMEWORK, "failed to marshall HidEventProperties");
170         return HID_DDK_INVALID_PARAMETER;
171     }
172 
173     int32_t ret =
174         remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_CREATE_DEVICE), data, reply, option);
175     if (ret < HID_DDK_SUCCESS) {
176         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
177     }
178     return ret;
179 }
180 
EmitEvent(int32_t deviceId,const std::vector<Hid_EmitItem> & items)181 int32_t DriverExtMgrProxy::EmitEvent(int32_t deviceId, const std::vector<Hid_EmitItem> &items)
182 {
183     sptr<IRemoteObject> remote = Remote();
184     if (remote == nullptr) {
185         EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
186         return HID_DDK_FAILURE;
187     }
188 
189     MessageParcel data;
190     MessageParcel reply;
191     MessageOption option;
192 
193     if (!data.WriteInterfaceToken(GetDescriptor())) {
194         EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
195         return HID_DDK_INVALID_PARAMETER;
196     }
197 
198     if (!EmitItemMarshalling(deviceId, items, data)) {
199         EDM_LOGE(MODULE_FRAMEWORK, "failed to marshall EmitItem");
200         return HID_DDK_INVALID_PARAMETER;
201     }
202 
203     int32_t ret =
204         remote->SendRequest(static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_EMIT_EVENT), data, reply, option);
205     if (ret != HID_DDK_SUCCESS) {
206         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
207         return ret;
208     }
209     return HID_DDK_SUCCESS;
210 }
211 
DestroyDevice(int32_t deviceId)212 int32_t DriverExtMgrProxy::DestroyDevice(int32_t deviceId)
213 {
214     sptr<IRemoteObject> remote = Remote();
215     if (remote == nullptr) {
216         EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr");
217         return HID_DDK_FAILURE;
218     }
219 
220     MessageParcel data;
221     MessageParcel reply;
222     MessageOption option;
223 
224     if (!data.WriteInterfaceToken(GetDescriptor())) {
225         EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token");
226         return HID_DDK_INVALID_PARAMETER;
227     }
228 
229     if (!data.WriteUint32(deviceId)) {
230         EDM_LOGE(MODULE_FRAMEWORK, "failed to write device id");
231         return HID_DDK_INVALID_PARAMETER;
232     }
233 
234     int32_t ret = remote->SendRequest(
235         static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_DESTROY_DEVICE), data, reply, option);
236     if (ret != HID_DDK_SUCCESS) {
237         EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret);
238         return ret;
239     }
240     return HID_DDK_SUCCESS;
241 }
242 } // namespace ExternalDeviceManager
243 } // namespace OHOS
244