• 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_stub.h"
17 #include <cinttypes>
18 
19 #include "hilog_wrapper.h"
20 #include "securec.h"
21 
22 namespace OHOS {
23 namespace ExternalDeviceManager {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int DriverExtMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
25 {
26     EDM_LOGD(MODULE_FRAMEWORK, "cmd:%u, flags:%d", code, option.GetFlags());
27     if (data.ReadInterfaceToken() != GetDescriptor()) {
28         EDM_LOGE(MODULE_FRAMEWORK, "remote descriptor is not matched");
29         return UsbErrCode::EDM_ERR_INVALID_PARAM;
30     }
31 
32     switch (code) {
33         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DEVICE):
34             return OnQueryDevice(data, reply, option);
35         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::BIND_DEVICE):
36             return OnBindDevice(data, reply, option);
37         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::UNBIND_DEVICE):
38             return OnUnBindDevice(data, reply, option);
39         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_CREATE_DEVICE):
40             return OnCreateDevice(data, reply, option);
41         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_EMIT_EVENT):
42             return OnEmitEvent(data, reply, option);
43         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::INPUT_DESTROY_DEVICE):
44             return OnDestroyDevice(data, reply, option);
45         default:
46             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
47     }
48 }
49 
OnQueryDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int32_t DriverExtMgrStub::OnQueryDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
51 {
52     uint32_t busType = 0;
53     if (!data.ReadUint32(busType)) {
54         EDM_LOGE(MODULE_FRAMEWORK, "failed to read busType");
55         return UsbErrCode::EDM_ERR_INVALID_PARAM;
56     }
57 
58     std::vector<std::shared_ptr<DeviceData>> devices;
59     UsbErrCode ret = QueryDevice(busType, devices);
60     if (ret != UsbErrCode::EDM_OK) {
61         EDM_LOGE(MODULE_FRAMEWORK, "failed to call QueryDevice function:%{public}d", static_cast<int32_t>(ret));
62         return ret;
63     }
64 
65     if (!reply.WriteUint64(static_cast<uint64_t>(devices.size()))) {
66         EDM_LOGE(MODULE_FRAMEWORK, "failed to write size of devices");
67         return UsbErrCode::EDM_ERR_INVALID_PARAM;
68     }
69 
70     for (uint64_t i = 0; i < devices.size(); i++) {
71         if (devices[i] == nullptr) {
72             EDM_LOGE(MODULE_FRAMEWORK, "invalid %{public}016" PRIX64 " device", i);
73             return UsbErrCode::EDM_ERR_INVALID_PARAM;
74         }
75 
76         if (!devices[i]->Marshalling(reply)) {
77             EDM_LOGE(MODULE_FRAMEWORK, "failed to write %{public}016" PRIX64 " device", i);
78             return UsbErrCode::EDM_ERR_INVALID_PARAM;
79         }
80     }
81 
82     return UsbErrCode::EDM_OK;
83 }
84 
OnBindDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)85 int32_t DriverExtMgrStub::OnBindDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
86 {
87     uint64_t deviceId = 0;
88     if (!data.ReadUint64(deviceId)) {
89         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
90         return UsbErrCode::EDM_ERR_INVALID_PARAM;
91     }
92 
93     sptr<IRemoteObject> remote = data.ReadRemoteObject();
94     if (remote == nullptr) {
95         EDM_LOGE(MODULE_FRAMEWORK, "failed to read remote object of connectCallback");
96         return UsbErrCode::EDM_ERR_INVALID_PARAM;
97     }
98 
99     sptr<IDriverExtMgrCallback> connectCallback = iface_cast<IDriverExtMgrCallback>(remote);
100     if (connectCallback == nullptr) {
101         EDM_LOGE(MODULE_FRAMEWORK, "failed to create connectCallback object");
102         return UsbErrCode::EDM_ERR_INVALID_PARAM;
103     }
104 
105     UsbErrCode ret = BindDevice(deviceId, connectCallback);
106     if (ret != UsbErrCode::EDM_OK) {
107         EDM_LOGE(MODULE_FRAMEWORK, "failed to call BindDevice function:%{public}d", static_cast<int32_t>(ret));
108         return ret;
109     }
110 
111     return UsbErrCode::EDM_OK;
112 }
113 
OnUnBindDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)114 int32_t DriverExtMgrStub::OnUnBindDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
115 {
116     uint64_t deviceId = 0;
117     if (!data.ReadUint64(deviceId)) {
118         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
119         return UsbErrCode::EDM_ERR_INVALID_PARAM;
120     }
121 
122     UsbErrCode ret = UnBindDevice(deviceId);
123     if (ret != UsbErrCode::EDM_OK) {
124         EDM_LOGE(MODULE_FRAMEWORK, "failed to call UnBindDevice function:%{public}d", static_cast<int32_t>(ret));
125         return ret;
126     }
127 
128     return UsbErrCode::EDM_OK;
129 }
130 
OnCreateDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)131 int32_t DriverExtMgrStub::OnCreateDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
132 {
133     auto hidDevice = HidDeviceUnMarshalling(data);
134     if (!hidDevice.has_value()) {
135         EDM_LOGE(MODULE_FRAMEWORK, "failed to read hidDevice");
136         return HID_DDK_INVALID_PARAMETER;
137     }
138 
139     auto hidEventProperties = HidEventPropertiesUnMarshalling(data);
140     if (!hidEventProperties.has_value()) {
141         EDM_LOGE(MODULE_FRAMEWORK, "failed to read hidEventProperties");
142         return HID_DDK_INVALID_PARAMETER;
143     }
144 
145     auto device = &(hidDevice.value());
146     auto eventProperties = &(hidEventProperties.value());
147     int32_t ret = CreateDevice(device, eventProperties);
148     delete[] device->deviceName;
149     delete device->properties;
150     delete eventProperties->hidEventTypes.hidEventType;
151     delete eventProperties->hidKeys.hidKeyCode;
152     delete eventProperties->hidAbs.hidAbsAxes;
153     delete eventProperties->hidRelBits.hidRelAxes;
154     delete eventProperties->hidMiscellaneous.hidMscEvent;
155     if (ret < HID_DDK_SUCCESS) {
156         EDM_LOGE(MODULE_FRAMEWORK, "failed to call CreateDevice function:%{public}d", ret);
157     }
158     return ret;
159 }
160 
OnEmitEvent(MessageParcel & data,MessageParcel & reply,MessageOption & option)161 int32_t DriverExtMgrStub::OnEmitEvent(MessageParcel &data, MessageParcel &reply, MessageOption &option)
162 {
163     int32_t deviceId = -1;
164     auto items = EmitItemUnMarshalling(data, deviceId);
165     if (!items.has_value()) {
166         EDM_LOGE(MODULE_FRAMEWORK, "failed to read emit items");
167         return HID_DDK_INVALID_PARAMETER;
168     }
169 
170     int32_t ret = EmitEvent(deviceId, items.value());
171     if (ret != HID_DDK_SUCCESS) {
172         EDM_LOGE(MODULE_FRAMEWORK, "failed to call EmitEvent function:%{public}d", ret);
173         return ret;
174     }
175 
176     return HID_DDK_SUCCESS;
177 }
178 
OnDestroyDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)179 int32_t DriverExtMgrStub::OnDestroyDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
180 {
181     uint32_t deviceId = 0;
182     if (!data.ReadUint32(deviceId)) {
183         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
184         return HID_DDK_INVALID_PARAMETER;
185     }
186 
187     int32_t ret = DestroyDevice(static_cast<int32_t>(deviceId));
188     if (ret != HID_DDK_SUCCESS) {
189         EDM_LOGE(MODULE_FRAMEWORK, "failed to call DestroyDevice function:%{public}d", ret);
190         return ret;
191     }
192 
193     return HID_DDK_SUCCESS;
194 }
195 } // namespace ExternalDeviceManager
196 } // namespace OHOS
197