diff --git a/frameworks/ddk/BUILD.gn b/frameworks/ddk/BUILD.gn index 5bcfe4d178f36139a3a6187786691637b2137fa5..01c25758e0966a09b273b3425e3561629d3bd34f 100644 --- a/frameworks/ddk/BUILD.gn +++ b/frameworks/ddk/BUILD.gn @@ -12,5 +12,8 @@ # limitations under the License. group("ddk") { - deps = [ "usb:usb_ndk" ] + deps = [ + "hid:hid_ndk", + "usb:usb_ndk", + ] } diff --git a/frameworks/ddk/hid/BUILD.gn b/frameworks/ddk/hid/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e877b2016bebf7c1540c80ebe81e72b94048bafa --- /dev/null +++ b/frameworks/ddk/hid/BUILD.gn @@ -0,0 +1,37 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("../../../extdevmgr.gni") + +ohos_shared_library("hid_ndk") { + include_dirs = [ + "${ext_mgr_path}/interfaces/ddk/hid/", + "${utils_path}/include/", + "./", + "${ext_mgr_path}/interfaces/innerkits/", + "${ext_mgr_path}/services/zidl/include/", + ] + + sources = [ "input_emit_event.cpp" ] + + deps = [ "${ext_mgr_path}/interfaces/innerkits:driver_ext_mgr_client" ] + + external_deps = [ + "c_utils:utils", + "drivers_interface_usb:libusb_ddk_proxy_1.0", + "hilog:libhilog", + ] + install_enable = true + subsystem_name = "hdf" + part_name = "external_device_manager" +} diff --git a/frameworks/ddk/hid/input_emit_event.cpp b/frameworks/ddk/hid/input_emit_event.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35a1da71fb5299a6414b8ebf64034bb9a1644dd8 --- /dev/null +++ b/frameworks/ddk/hid/input_emit_event.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "driver_ext_mgr_client.h" +#include "hilog_wrapper.h" +#include "hid_ddk_api.h" + +using namespace OHOS::ExternalDeviceManager; +namespace { +static DriverExtMgrClient &g_edmClient = DriverExtMgrClient::GetInstance(); +} +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +int32_t OH_Hid_CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) +{ + if (hidDevice == nullptr) { + EDM_LOGE(MODULE_USB_DDK, "hidDevice is null"); + return HID_DDK_INVALID_PARAMETER; + } + + if (hidEventProperties == nullptr) { + EDM_LOGE(MODULE_USB_DDK, "hidEventProperties is null"); + return HID_DDK_INVALID_PARAMETER; + } + + auto ret = g_edmClient.CreateDevice(hidDevice, hidEventProperties); + if (ret < 0) { + EDM_LOGE(MODULE_HID_DDK, "create device failed:%{public}d", ret); + } + return ret; +} + +int32_t OH_Hid_EmitEvent(int32_t deviceId, const Hid_EmitItem items[], uint16_t length) +{ + if (length > MAX_EMIT_ITEM_NUM) { + EDM_LOGE(MODULE_HID_DDK, "length out of range"); + return HID_DDK_INVALID_PARAMETER; + } + + if (items == nullptr) { + EDM_LOGE(MODULE_USB_DDK, "items is null"); + return HID_DDK_INVALID_PARAMETER; + } + + std::vector itemsTmp(items, items + length); + if (auto ret = g_edmClient.EmitEvent(deviceId, itemsTmp); ret != HID_DDK_SUCCESS) { + EDM_LOGE(MODULE_USB_DDK, "emit event failed:%{public}d", ret); + return ret; + } + return HID_DDK_SUCCESS; +} + +int32_t OH_Hid_DestroyDevice(int32_t deviceId) +{ + if (auto ret = g_edmClient.DestroyDevice(deviceId); ret != HID_DDK_SUCCESS) { + EDM_LOGE(MODULE_USB_DDK, "destroy device failed:%{public}d", ret); + return ret; + } + return HID_DDK_SUCCESS; +} +#ifdef __cplusplus +} +#endif /* __cplusplus */ diff --git a/frameworks/ddk/usb/BUILD.gn b/frameworks/ddk/usb/BUILD.gn index 2bf082b53de3912201f502df2224b0f5c6a2b8c1..4d19f54713381f1c78a3431e45644442fb763305 100644 --- a/frameworks/ddk/usb/BUILD.gn +++ b/frameworks/ddk/usb/BUILD.gn @@ -23,7 +23,6 @@ ohos_shared_library("usb_ndk") { ] sources = [ - "input_emit_event.cpp", "usb_config_desc_parser.cpp", "usb_ddk_api.cpp", ] diff --git a/frameworks/ddk/usb/input_emit_event.cpp b/frameworks/ddk/usb/input_emit_event.cpp index 71054006509a3b1ef4cfa353290fc999cbd3281a..283b98f654366e07c6908976900931bc0723cea9 100644 --- a/frameworks/ddk/usb/input_emit_event.cpp +++ b/frameworks/ddk/usb/input_emit_event.cpp @@ -32,14 +32,14 @@ int32_t OH_Usb_CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) return EDM_OK; } -int32_t OH_Usb_EmitEvent(int32_t deviceId, const EmitItem items[], uint32_t length) +int32_t OH_Usb_EmitEvent(int32_t deviceId, const Hid_EmitItem items[], uint32_t length) { if (length > MAX_EMIT_ITEM_NUM) { EDM_LOGE(MODULE_DEV_MGR, "length out of range"); return EDM_ERR_INVALID_PARAM; } - std::vector itemsTmp(items, items + length); + std::vector itemsTmp(items, items + length); if (auto ret = g_edmClient.EmitEvent(deviceId, itemsTmp); ret != UsbErrCode::EDM_OK) { EDM_LOGE(MODULE_USB_DDK, "emit event failed:%{public}d", ret); return ret; diff --git a/frameworks/js/napi/device_manager/BUILD.gn b/frameworks/js/napi/device_manager/BUILD.gn index fd7b7ede36a0fd42a9ad67427242d85e14bf56de..6fa37b85afda373027ee3c72925966f3f8422d87 100644 --- a/frameworks/js/napi/device_manager/BUILD.gn +++ b/frameworks/js/napi/device_manager/BUILD.gn @@ -18,6 +18,7 @@ ohos_shared_library("devicemanager_napi") { include_dirs = [ "${utils_path}/include/", + "${ext_mgr_path}/interfaces/ddk/hid/", "${ext_mgr_path}/interfaces/innerkits/", "${ext_mgr_path}/services/zidl/include/", ] diff --git a/frameworks/native/driver_ext_mgr_client.cpp b/frameworks/native/driver_ext_mgr_client.cpp index 58802e74191b2e4caa2cd214a7947e4de72f26a3..38c14f9bba4027d706c25e28ee459d4822e69e72 100644 --- a/frameworks/native/driver_ext_mgr_client.cpp +++ b/frameworks/native/driver_ext_mgr_client.cpp @@ -120,28 +120,28 @@ UsbErrCode DriverExtMgrClient::UnBindDevice(uint64_t deviceId) return proxy_->UnBindDevice(deviceId); } -UsbErrCode DriverExtMgrClient::EmitEvent(int32_t deviceId, const std::vector &items) +int32_t DriverExtMgrClient::EmitEvent(int32_t deviceId, const std::vector &items) { if (Connect() != UsbErrCode::EDM_OK) { - return UsbErrCode::EDM_ERR_CONNECTION_FAILED; + return HID_DDK_FAILURE; } return proxy_->EmitEvent(deviceId, items); } -UsbErrCode DriverExtMgrClient::CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) +int32_t DriverExtMgrClient::CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) { if (Connect() != UsbErrCode::EDM_OK) { - return UsbErrCode::EDM_ERR_CONNECTION_FAILED; + return HID_DDK_FAILURE; } - return proxy_->CreateDevice(maxX, maxY, maxPressure); + return proxy_->CreateDevice(hidDevice, hidEventProperties); } -UsbErrCode DriverExtMgrClient::DestroyDevice(void) +int32_t DriverExtMgrClient::DestroyDevice(int32_t deviceId) { if (Connect() != UsbErrCode::EDM_OK) { - return UsbErrCode::EDM_ERR_CONNECTION_FAILED; + return HID_DDK_FAILURE; } - return proxy_->DestroyDevice(); + return proxy_->DestroyDevice(deviceId); } } // namespace ExternalDeviceManager } // namespace OHOS \ No newline at end of file diff --git a/interfaces/ddk/hid/BUILD.gn b/interfaces/ddk/hid/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4bb08a5d40d9fc54f01c6a99a9d961ca15e97e57 --- /dev/null +++ b/interfaces/ddk/hid/BUILD.gn @@ -0,0 +1,33 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_ndk_headers("hid_header") { + dest_dir = "$ndk_headers_out_dir/hid/" + sources = [ + "hid_ddk_api.h", + "hid_ddk_types.h", + ] +} + +ohos_ndk_library("libhid_ndk") { + ndk_description_file = "./libhid.ndk.json" + min_compact_version = "9" + output_name = "hid_ndk" + system_capability = "SystemCapability.Driver.HID.Extension" + system_capability_headers = [ + "hid/hid_ddk_api.h", + "hid/hid_ddk_types.h", + ] +} diff --git a/interfaces/ddk/hid/hid_ddk_api.h b/interfaces/ddk/hid/hid_ddk_api.h new file mode 100644 index 0000000000000000000000000000000000000000..706601d0bfa48869c6c0c64118a2e572ad9e255a --- /dev/null +++ b/interfaces/ddk/hid/hid_ddk_api.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HID_DDK_API_H +#define HID_DDK_API_H + +/** + * @addtogroup HidDdk + * @{ + * + * @brief Provides HID DDK interfaces, including creating a device, sending an event, and destroying a device. + * + * @syscap SystemCapability.Driver.HID.Extension + * @since 11 + * @version 1.0 + */ + +/** + * @file hid_ddk_api.h + * + * @brief Declares the HID DDK interfaces for the host to access an input device. + * + * File to include: + * @since 11 + * @version 1.0 + */ + +#include +#include "hid_ddk_types.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Creates a device. + * + * @permission ohos.permission.ACCESS_DDK_HID + * @param hidDevice Pointer to the basic information required for creating a device, including the device name, + * vendor ID, and product ID. + * @param hidEventProperties Pointer to the events of the device to be observed, including the event type and + * properties of the key event, absolute coordinate event, and relative coordinate event. + * @return Returns the device ID (a non-negative number) if the operation is successful; + * returns a negative number otherwise. + * @since 11 + * @version 1.0 + */ +int32_t OH_Hid_CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties); + +/** + * @brief Sends an event list to a device. + * + * @permission ohos.permission.ACCESS_DDK_HID + * @param deviceId ID of the device, to which the event list is sent. + * @param items List of events to sent. The event information includes the event type (HidEventType), + * event code (HidSynEvent for a synchronization event code, HidKeyCode for a key code, HidBtnCode + * for a button code, HidAbsAxes for an absolute coordinate code, HidRelAxes + * for a relative coordinate event, and HidMscEvent for other input event code), and value input by the device. + * @param length Length of the event list (number of events sent at a time). + * @return Returns 0 if the operation is successful; returns a negative number otherwise. + * @since 11 + * @version 1.0 + */ +int32_t OH_Hid_EmitEvent(int32_t deviceId, const Hid_EmitItem items[], uint16_t length); + +/** + * @brief Destroys a device. + * + * @permission ohos.permission.ACCESS_DDK_HID + * @param deviceId ID of the device to destroy. + * @return Returns 0 if the operation is successful; returns a negative number otherwise. + * @since 11 + * @version 1.0 + */ +int32_t OH_Hid_DestroyDevice(int32_t deviceId); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif // HID_DDK_API_H diff --git a/interfaces/ddk/hid/hid_ddk_types.h b/interfaces/ddk/hid/hid_ddk_types.h new file mode 100644 index 0000000000000000000000000000000000000000..e90ba7d3d03f9c51cad9eb12715c3f1d3acd485f --- /dev/null +++ b/interfaces/ddk/hid/hid_ddk_types.h @@ -0,0 +1,605 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HID_DDK_TYPES_H +#define HID_DDK_TYPES_H +/** + * @addtogroup HidDdk + * @{ + * + * @brief Provides HID DDK interfaces, including creating a device, sending an event, and destroying a device. + * + * @syscap SystemCapability.Driver.HID.Extension + * @since 11 + * @version 1.0 + */ + +/** + * @file hid_ddk_types.h + * + * @brief Provides definitions of enum variables and structs in the HID DDK. + * + * File to include: + * @since 11 + * @version 1.0 + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Defines event information. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_EmitItem { + /** Event type */ + uint16_t type; + /** Event code */ + uint16_t code; + /** Event value */ + uint32_t value; +} Hid_EmitItem; + +/** + * @brief Enumerates the input devices. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** Pointer device */ + HID_PROP_POINTER = 0x00, + /** Direct input device */ + HID_PROP_DIRECT = 0x01, + /** Touch device with bottom keys */ + HID_PROP_BUTTON_PAD = 0x02, + /** Full multi-touch device */ + HID_PROP_SEMI_MT = 0x03, + /** Touch device with top soft keys */ + HID_PROP_TOP_BUTTON_PAD = 0x04, + /** Pointing stick */ + HID_PROP_POINTING_STICK = 0x05, + /** Accelerometer */ + HID_PROP_ACCELEROMETER = 0x06 +} Hid_DeviceProp; + +/** + * @brief Defines the basic device information. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_Device { + /** Device name */ + const char *deviceName; + /** Vendor ID */ + uint16_t vendorId; + /** Product ID */ + uint16_t productId; + /** Version */ + uint16_t version; + /** Bus type */ + uint16_t bustype; + /** Device properties */ + Hid_DeviceProp *properties; + /** Number of device properties */ + uint16_t propLength; +} Hid_Device; + +/** + * @brief Enumerates the event types. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** Synchronization event */ + HID_EV_SYN = 0x00, + /** Key event */ + HID_EV_KEY = 0x01, + /** Relative coordinate event */ + HID_EV_REL = 0x02, + /** Absolute coordinate event */ + HID_EV_ABS = 0x03, + /** Other special event */ + HID_EV_MSC = 0x04 +} Hid_EventType; + +/** + * @brief Enumerates the synchronization event codes. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** Indicates the end of an event. */ + HID_SYN_REPORT = 0, + /** Indicates configuration synchronization. */ + HID_SYN_CONFIG = 1, + /** Indicates the end of a multi-touch ABS data packet. */ + HID_SYN_MT_REPORT = 2, + /** Indicates that the event is discarded. */ + HID_SYN_DROPPED = 3 +} Hid_SynEvent; + +/** + * @brief Enumerates the key value codes. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** Key A */ + HID_KEY_A = 30, + /** Key B */ + HID_KEY_B = 48, + /** Key C */ + HID_KEY_C = 46, + /** Key D */ + HID_KEY_D = 32, + /** Key E */ + HID_KEY_E = 18, + /** Key F */ + HID_KEY_F = 33, + /** Key G */ + HID_KEY_G = 34, + /** Key H */ + HID_KEY_H = 35, + /** Key I */ + HID_KEY_I = 23, + /** Key J */ + HID_KEY_J = 36, + /** Key K */ + HID_KEY_K = 37, + /** Key L */ + HID_KEY_L = 38, + /** Key M */ + HID_KEY_M = 50, + /** Key N */ + HID_KEY_N = 49, + /** Key O */ + HID_KEY_O = 24, + /** Key P */ + HID_KEY_P = 25, + /** Key Q */ + HID_KEY_Q = 16, + /** Key R */ + HID_KEY_R = 19, + /** Key S */ + HID_KEY_S = 31, + /** Key T */ + HID_KEY_T = 20, + /** Key U */ + HID_KEY_U = 22, + /** Key V */ + HID_KEY_V = 47, + /** Key W */ + HID_KEY_W = 17, + /** Key X */ + HID_KEY_X = 45, + /** Key Y */ + HID_KEY_Y = 21, + /** Key Z */ + HID_KEY_Z = 44, + /** Key Esc */ + HID_KEY_ESC = 1, + /** Key 0 */ + HID_KEY_0 = 11, + /** Key 1 */ + HID_KEY_1 = 2, + /** Key 2 */ + HID_KEY_2 = 3, + /** Key 3 */ + HID_KEY_3 = 4, + /** Key 4 */ + HID_KEY_4 = 5, + /** Key 5 */ + HID_KEY_5 = 6, + /** Key 6 */ + HID_KEY_6 = 7, + /** Key 7 */ + HID_KEY_7 = 8, + /** Key 8 */ + HID_KEY_8 = 9, + /** Key 9 */ + HID_KEY_9 = 10, + /** Key grave (`) */ + HID_KEY_GRAVE = 41, + /** Key minum (-) */ + HID_KEY_MINUS = 12, + /** Key equals (=) */ + HID_KEY_EQUALS = 13, + /** Key backspace */ + HID_KEY_BACKSPACE = 14, + /** Key left bracket ([) */ + HID_KEY_LEFT_BRACKET = 26, + /** Key right bracket (]) */ + HID_KEY_RIGHT_BRACKET = 27, + /** Key Enter */ + HID_KEY_ENTER = 28, + /** Key left Shift */ + HID_KEY_LEFT_SHIFT = 42, + /** Key backslash (\) */ + HID_KEY_BACKSLASH = 43, + /** Key semicolon (;) */ + HID_KEY_SEMICOLON = 39, + /** Key apostrophe (') */ + HID_KEY_APOSTROPHE = 40, + /** Key space */ + HID_KEY_SPACE = 57, + /** Key slash (/) */ + HID_KEY_SLASH = 53, + /** Key comma (,) */ + HID_KEY_COMMA = 51, + /** Key period (.) */ + HID_KEY_PERIOD = 52, + /** Key right Shift */ + HID_KEY_RIGHT_SHIFT = 54, + /** Numeral 0 on the numeric keypad */ + HID_KEY_NUMPAD_0 = 82, + /** Numeral 1 on the numeric keypad */ + HID_KEY_NUMPAD_1 = 79, + /** Numeral 2 on the numeric keypad */ + HID_KEY_NUMPAD_2 = 80, + /** Numeral 3 on the numeric keypad */ + HID_KEY_NUMPAD_3 = 81, + /** Numeral 4 on the numeric keypad */ + HID_KEY_NUMPAD_4 = 75, + /** Numeral 5 on the numeric keypad */ + HID_KEY_NUMPAD_5 = 76, + /** Numeral 6 on the numeric keypad*/ + HID_KEY_NUMPAD_6 = 77, + /** Numeral 7 on the numeric keypad */ + HID_KEY_NUMPAD_7 = 71, + /** Numeral 8 on the numeric keypad */ + HID_KEY_NUMPAD_8 = 72, + /** Numeral 9 on the numeric keypad */ + HID_KEY_NUMPAD_9 = 73, + /** Arithmetic operator / (division) on the numeric keypad */ + HID_KEY_NUMPAD_DIVIDE = 70, + /** Arithmetic operator * (multiplication) on the numeric keypad */ + HID_KEY_NUMPAD_MULTIPLY = 55, + /** Arithmetic operator - (subtraction) on the numeric keypad */ + HID_KEY_NUMPAD_SUBTRACT = 74, + /** Arithmetic operator + (addition) on the numeric keypad */ + HID_KEY_NUMPAD_ADD = 78, + /** Decimal point (.) on the numeric keypad */ + HID_KEY_NUMPAD_DOT = 83, + /** Key Print Screen */ + HID_KEY_SYSRQ = 99, + /** Key Mute */ + HID_KEY_MUTE = 113, + /** Key for volume down */ + HID_KEY_VOLUME_DOWN = 114, + /** Key for volume up */ + HID_KEY_VOLUME_UP = 115, + /** Key for decreasing brightness */ + HID_KEY_BRIGHTNESS_DOWN = 224, + /** Key for increasing brightness */ + HID_KEY_BRIGHTNESS_UP = 225, + /** Button 0 */ + HID_BTN_0 = 0x100, + /** Button 1 */ + HID_BTN_1 = 0x101, + /** Button 2 */ + HID_BTN_2 = 0x102, + /** Button 3 */ + HID_BTN_3 = 0x103, + /** Button 4 */ + HID_BTN_4 = 0x104, + /** Button 5 */ + HID_BTN_5 = 0x105, + /** Button 6 */ + HID_BTN_6 = 0x106, + /** Button 7 */ + HID_BTN_7 = 0x107, + /** Button 8 */ + HID_BTN_8 = 0x108, + /** Button 9 */ + HID_BTN_9 = 0x109, + /** Left mouse button */ + HID_BTN_LEFT = 0x110, + /** Right mouse button */ + HID_BTN_RIGHT = 0x111, + /** Middle mouse button */ + HID_BTN_MIDDLE = 0x112, + /** Side mouse button */ + HID_BTN_SIDE = 0x113, + /** Extra mouse button */ + HID_BTN_EXTRA = 0x114, + /** Mouse forward button */ + HID_BTN_FORWARD = 0x115, + /** Mouse backward button */ + HID_BTN_BACKWARD = 0x116, + /** Mouse task button */ + HID_BTN_TASK = 0x117, + /** Pen */ + HID_BTN_TOOL_PEN = 0x140, + /** Rubber */ + HID_BTN_TOOL_RUBBER = 0x141, + /** Brush */ + HID_BTN_TOOL_BRUSH = 0x142, + /** Pencil */ + HID_BTN_TOOL_PENCIL = 0x143, + /** Air brush */ + HID_BTN_TOOL_AIRBRUSH = 0x144, + /** Finger */ + HID_BTN_TOOL_FINGER = 0x145, + /** Mouse */ + HID_BTN_TOOL_MOUSE = 0x146, + /** Lens */ + HID_BTN_TOOL_LENS = 0x147, + /** Five-finger touch */ + HID_BTN_TOOL_QUINTTAP = 0x148, + /** Stylus 3 */ + HID_BTN_STYLUS3 = 0x149, + /** Touch */ + HID_BTN_TOUCH = 0x14a, + /** Stylus */ + HID_BTN_STYLUS = 0x14b, + /** Stylus 2 */ + HID_BTN_STYLUS2 = 0x14c, + /** Two-finger touch */ + HID_BTN_TOOL_DOUBLETAP = 0x14d, + /** Three-finger touch */ + HID_BTN_TOOL_TRIPLETAP = 0x14e, + /** Four-finger touch */ + HID_BTN_TOOL_QUADTAP = 0x14f, + /** Scroll wheel */ + HID_BTN_WHEEL = 0x150 +} Hid_KeyCode; + +/** + * @brief Enumerates the absolute coordinate codes. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** X axis */ + HID_ABS_X = 0x00, + /** Y axis */ + HID_ABS_Y = 0x01, + /** Z axis */ + HID_ABS_Z = 0x02, + /** X axis of the right analog stick */ + HID_ABS_RX = 0x03, + /** Y axis of the right analog stick */ + HID_ABS_RY = 0x04, + /** Z axis of the right analog stick */ + HID_ABS_RZ = 0x05, + /** Throttle */ + HID_ABS_THROTTLE = 0x06, + /** Rudder */ + HID_ABS_RUDDER = 0x07, + /** Scroll wheel */ + HID_ABS_WHEEL = 0x08, + /** Gas */ + HID_ABS_GAS = 0x09, + /** Brake */ + HID_ABS_BRAKE = 0x0a, + /** HAT0X */ + HID_ABS_HAT0X = 0x10, + /** HAT0Y */ + HID_ABS_HAT0Y = 0x11, + /** HAT1X */ + HID_ABS_HAT1X = 0x12, + /** HAT1Y */ + HID_ABS_HAT1Y = 0x13, + /** HAT2X */ + HID_ABS_HAT2X = 0x14, + /** HAT2Y */ + HID_ABS_HAT2Y = 0x15, + /** HAT3X */ + HID_ABS_HAT3X = 0x16, + /** HAT3Y */ + HID_ABS_HAT3Y = 0x17, + /** Pressure */ + HID_ABS_PRESSURE = 0x18, + /** Distance */ + HID_ABS_DISTANCE = 0x19, + /** Inclination of X axis */ + HID_ABS_TILT_X = 0x1a, + /** Inclination of Y axis */ + HID_ABS_TILT_Y = 0x1b, + /** Width of the touch tool */ + HID_ABS_TOOL_WIDTH = 0x1c, + /** Volume */ + HID_ABS_VOLUME = 0x20, + /** Others */ + HID_ABS_MISC = 0x28 +} Hid_AbsAxes; + +/** + * @brief Enumerates the relative coordinate codes. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** X axis */ + HID_REL_X = 0x00, + /** Y axis */ + HID_REL_Y = 0x01, + /** Z axis */ + HID_REL_Z = 0x02, + /** X axis of the right analog stick */ + HID_REL_RX = 0x03, + /** Y axis of the right analog stick */ + HID_REL_RY = 0x04, + /** Z axis of the right analog stick */ + HID_REL_RZ = 0x05, + /** Horizontal scroll wheel */ + HID_REL_HWHEEL = 0x06, + /** Scale */ + HID_REL_DIAL = 0x07, + /** Scroll wheel */ + HID_REL_WHEEL = 0x08, + /** Others */ + HID_REL_MISC = 0x09, + /* Reserved */ + HID_REL_RESERVED = 0x0a, + /** High-resolution scroll wheel */ + HID_REL_WHEEL_HI_RES = 0x0b, + /** High-resolution horizontal scroll wheel */ + HID_REL_HWHEEL_HI_RES = 0x0c +} Hid_RelAxes; + +/** + * @brief Enumerates the codes of other input events. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** Serial number */ + HID_MSC_SERIAL = 0x00, + /** Pulse */ + HID_MSC_PULSELED = 0x01, + /** Gesture */ + HID_MSC_GESTURE = 0x02, + /** Start event */ + HID_MSC_RAW = 0x03, + /** Scan */ + HID_MSC_SCAN = 0x04, + /** Timestamp */ + HID_MSC_TIMESTAMP = 0x05 +} Hid_MscEvent; + +/** + * @brief Defines an array of the event type codes. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_EventTypeArray { + /** Event type code */ + Hid_EventType *hidEventType; + /** Length of the array */ + uint16_t length; +} Hid_EventTypeArray; + +/** + * @brief Defines an array of key value properties. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_KeyCodeArray { + /** Key value code */ + Hid_KeyCode *hidKeyCode; + /** Length of the array */ + uint16_t length; +} Hid_KeyCodeArray; + +/** + * @brief Defines an array of absolute coordinate properties. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_AbsAxesArray { + /** Absolute coordinate property code */ + Hid_AbsAxes *hidAbsAxes; + /** Length of the array */ + uint16_t length; +} Hid_AbsAxesArray; + +/** + * @brief Defines an array of relative coordinate properties. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_RelAxesArray { + /** Relative coordinate property code */ + Hid_RelAxes *hidRelAxes; + /** Length of the array */ + uint16_t length; +} Hid_RelAxesArray; + +/** + * @brief Defines an array of other special event properties. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_MscEventArray { + /** Code of the event property */ + Hid_MscEvent *hidMscEvent; + /** Length of the array */ + uint16_t length; +} Hid_MscEventArray; + +/** + * @brief Defines the event properties of a device to be observed. + * + * @since 11 + * @version 1.0 + */ +typedef struct Hid_EventProperties { + /** Array of event type codes */ + struct Hid_EventTypeArray hidEventTypes; + /** Array of key value codes */ + struct Hid_KeyCodeArray hidKeys; + /** Array of absolute coordinate property codes */ + struct Hid_AbsAxesArray hidAbs; + /** Array of relative coordinate property codes */ + struct Hid_RelAxesArray hidRelBits; + /** Array of other event property codes */ + struct Hid_MscEventArray hidMiscellaneous; + + /** Maximum values of the absolute coordinates */ + int32_t hidAbsMax[64]; + /** Minimum values of the absolute coordinates */ + int32_t hidAbsMin[64]; + /** Fuzzy values of the absolute coordinates */ + int32_t hidAbsFuzz[64]; + /** Fixed values of the absolute coordinates */ + int32_t hidAbsFlat[64]; +} Hid_EventProperties; + +/** + * @brief Defines the error codes used in the HID DDK. + * + * @since 11 + * @version 1.0 + */ +typedef enum { + /** Operation successful */ + HID_DDK_SUCCESS = 0, + /** Operation failed */ + HID_DDK_FAILURE = -1, + /** Invalid parameter */ + HID_DDK_INVALID_PARAMETER = -2, + /** Invalid operation */ + HID_DDK_INVALID_OPERATION = -3, + /** Null pointer exception */ + HID_DDK_NULL_PTR = -4, + /** Timeout */ + HID_DDK_TIMEOUT = -5, + /** Permission denied */ + HID_DDK_NO_PERM = -6 +} Hid_DdkErrCode; +#ifdef __cplusplus +} +/** @} */ +#endif /* __cplusplus */ +#endif // HID_DDK_TYPES_H diff --git a/interfaces/ddk/hid/libhid.ndk.json b/interfaces/ddk/hid/libhid.ndk.json new file mode 100644 index 0000000000000000000000000000000000000000..192bc43e1f6a8e50368956f02e8b5e896c010421 --- /dev/null +++ b/interfaces/ddk/hid/libhid.ndk.json @@ -0,0 +1,11 @@ +[ + { + "name": "OH_Hid_CreateDevice" + }, + { + "name": "OH_Hid_EmitEvent" + }, + { + "name": "OH_Hid_DestroyDevice" + } +] \ No newline at end of file diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index f6ebc13a36f3db00eac3c6fcd2e072ac59142192..3c08a08dfb5bd705d95d13a495b4d115e0cd8d89 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -23,6 +23,7 @@ config("ext_dvc_mgr_public_config") { ohos_shared_library("driver_ext_mgr_client") { include_dirs = [ + "${ext_mgr_path}/interfaces/ddk/hid", "${ext_mgr_path}/services/zidl/include", "${ext_mgr_path}/utils/include", ] diff --git a/interfaces/innerkits/driver_ext_mgr_client.h b/interfaces/innerkits/driver_ext_mgr_client.h index 4c38ce305a31030e0f15e215f622c835dfb46f74..394e5d78964225ae2e27905efd9a89d2acecd726 100644 --- a/interfaces/innerkits/driver_ext_mgr_client.h +++ b/interfaces/innerkits/driver_ext_mgr_client.h @@ -30,9 +30,9 @@ public: UsbErrCode QueryDevice(uint32_t busType, std::vector> &devices); UsbErrCode BindDevice(uint64_t deviceId, const sptr &connectCallback); UsbErrCode UnBindDevice(uint64_t deviceId); - UsbErrCode CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure); - UsbErrCode EmitEvent(int32_t deviceId, const std::vector &items); - UsbErrCode DestroyDevice(void); + int32_t CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties); + int32_t EmitEvent(int32_t deviceId, const std::vector &items); + int32_t DestroyDevice(int32_t deviceId); private: UsbErrCode Connect(); diff --git a/interfaces/innerkits/driver_ext_mgr_types.h b/interfaces/innerkits/driver_ext_mgr_types.h index 44318a701ad67fd4eb25930bed57ac07378bf693..e334083678f14867243411f306bf56394e285f4c 100644 --- a/interfaces/innerkits/driver_ext_mgr_types.h +++ b/interfaces/innerkits/driver_ext_mgr_types.h @@ -19,7 +19,7 @@ #include #include -#include "emit_event_types.h" +#include "hid_ddk_types.h" #include "ext_object.h" #include "message_parcel.h" @@ -66,8 +66,23 @@ public: }; constexpr uint32_t MAX_EMIT_ITEM_NUM = 20; -bool EmitItemMarshalling(int32_t deviceId, const std::vector &items, MessageParcel &parcel); -std::optional> EmitItemUnMarshalling(MessageParcel &parcel, int32_t &deviceId); +constexpr uint32_t MAX_HID_DEVICE_PROP_LEN = 7; +constexpr uint32_t MAX_HID_EVENT_TYPES_LEN = 5; +constexpr uint32_t MAX_HID_KEYS_LEN = 100; +constexpr uint32_t MAX_HID_ABS_LEN = 26; +constexpr uint32_t MAX_HID_REL_BITS_LEN = 13; +constexpr uint32_t MAX_HID_MISC_EVENT_LEN = 6; +bool EmitItemMarshalling(int32_t deviceId, const std::vector &items, MessageParcel &parcel); +std::optional> EmitItemUnMarshalling(MessageParcel &parcel, int32_t &deviceId); +bool HidDeviceMarshalling(Hid_Device *hidDevice, MessageParcel &parcel); +std::optional HidDeviceUnMarshalling(MessageParcel &parcel); +bool HidEventPropertiesMarshalling(Hid_EventProperties *hidEventProperties, MessageParcel &parcel); +bool HidKeysOrAxisPropertiesMarshalling(Hid_EventProperties *hidEventProperties, MessageParcel &parcel); +bool HidAbsValueMarshalling(Hid_EventProperties *hidEventProperties, MessageParcel &parcel); +std::optional HidEventPropertiesUnMarshalling(MessageParcel &parcel); +bool HidEventTypeOrKeysPropertiesUnMarshalling(MessageParcel &parcel, Hid_EventProperties &hidEventProperties); +bool HidAxisPropertiesUnMarshalling(MessageParcel &parcel, Hid_EventProperties &hidEventProperties); +bool HidAbsValueUnMarshalling(MessageParcel &parcel, Hid_EventProperties &hidEventProperties); } // namespace ExternalDeviceManager } // namespace OHOS #endif // DRIVER_EXTENSION_MANAGER_TYPES_H diff --git a/interfaces/innerkits/emit_event_types.h b/interfaces/innerkits/emit_event_types.h deleted file mode 100644 index ef70f8d1b6fe3fc5fb342468e6557ad457cd096c..0000000000000000000000000000000000000000 --- a/interfaces/innerkits/emit_event_types.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef EMIT_EVENT_TYPES_H -#define EMIT_EVENT_TYPES_H - -#include - -struct EmitItem { - uint16_t type; - uint16_t code; - uint32_t value; -}; -#endif // EMIT_EVENT_TYPES_H \ No newline at end of file diff --git a/interfaces/innerkits/idriver_ext_mgr.h b/interfaces/innerkits/idriver_ext_mgr.h index 7ed91212d84b3717d152e11953755bc83d4fe7e0..6e00cf5fa857ed126f2cbb4d5a55b2626d89b694 100644 --- a/interfaces/innerkits/idriver_ext_mgr.h +++ b/interfaces/innerkits/idriver_ext_mgr.h @@ -34,9 +34,9 @@ public: virtual UsbErrCode QueryDevice(uint32_t busType, std::vector> &devices) = 0; virtual UsbErrCode BindDevice(uint64_t deviceId, const sptr &connectCallback) = 0; virtual UsbErrCode UnBindDevice(uint64_t deviceId) = 0; - virtual UsbErrCode CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) = 0; - virtual UsbErrCode EmitEvent(int32_t deviceId, const std::vector &items) = 0; - virtual UsbErrCode DestroyDevice(void) = 0; + virtual int32_t CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) = 0; + virtual int32_t EmitEvent(int32_t deviceId, const std::vector &items) = 0; + virtual int32_t DestroyDevice(int32_t deviceId) = 0; }; } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/services/BUILD.gn b/services/BUILD.gn index 0ed8e4cb815c148a6fe1528539df40794c342263..3209099a9ef4a9156ee0f619372ae0b6fd79b3bd 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -24,6 +24,7 @@ ohos_shared_library("driver_extension_manager") { include_dirs = [ "${ext_mgr_path}/services/native/driver_extension_manager/include", + "${ext_mgr_path}/interfaces/ddk/hid/", "${ext_mgr_path}/interfaces/innerkits/", "${ext_mgr_path}/services/zidl/include/", "${ext_mgr_path}/services/native/driver_extension_manager/include/drivers_pkg_manager", @@ -61,6 +62,7 @@ ohos_shared_library("driver_extension_manager") { config("driver_extension_manager_test_public_config") { include_dirs = [ "${ext_mgr_path}/services/native/driver_extension_manager/include", + "${ext_mgr_path}/interfaces/ddk/hid/", "${ext_mgr_path}/interfaces/innerkits/", "${ext_mgr_path}/services/zidl/include/", "${ext_mgr_path}/utils/include/", diff --git a/services/native/driver_extension_manager/include/driver_ext_mgr.h b/services/native/driver_extension_manager/include/driver_ext_mgr.h index eec1e0cc23f13c6f4801fbb12795654fa35d6654..3a197c8f31be4186f0417a4cd4253aedab00c136 100644 --- a/services/native/driver_extension_manager/include/driver_ext_mgr.h +++ b/services/native/driver_extension_manager/include/driver_ext_mgr.h @@ -33,9 +33,9 @@ public: UsbErrCode QueryDevice(uint32_t busType, std::vector> &devices) override; UsbErrCode BindDevice(uint64_t deviceId, const sptr &connectCallback) override; UsbErrCode UnBindDevice(uint64_t deviceId) override; - UsbErrCode CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) override; - UsbErrCode EmitEvent(int32_t deviceId, const std::vector &items) override; - UsbErrCode DestroyDevice(void) override; + int32_t CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) override; + int32_t EmitEvent(int32_t deviceId, const std::vector &items) override; + int32_t DestroyDevice(int32_t deviceId) override; private: std::mutex connectCallbackMutex; diff --git a/services/native/driver_extension_manager/include/input_emit_event/emit_event_manager.h b/services/native/driver_extension_manager/include/input_emit_event/emit_event_manager.h index 55262c03ea631d2c879ab2feaa80309abc62f2d4..a6d00a4e5001c248d2b83a0c8e26cb32f50e4e69 100644 --- a/services/native/driver_extension_manager/include/input_emit_event/emit_event_manager.h +++ b/services/native/driver_extension_manager/include/input_emit_event/emit_event_manager.h @@ -17,7 +17,7 @@ #define INPUT_EMIT_EVENT_EMIT_EVENT_MANAGER_H #include -#include "emit_event_types.h" +#include "hid_ddk_types.h" #include "single_instance.h" #include "virtual_device_inject.h" @@ -27,15 +27,17 @@ class EmitEventManager final { DECLARE_SINGLE_INSTANCE_BASE(EmitEventManager); public: - int32_t CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure); - int32_t EmitEvent(int32_t deviceId, const std::vector &items); - int32_t DestroyDevice(); + int32_t CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties); + int32_t EmitEvent(int32_t deviceId, const std::vector &items); + int32_t DestroyDevice(int32_t deviceId); + int32_t GetCurDeviceId(); private: EmitEventManager() = default; bool HasPermission(void); bool CheckHapPermission(uint32_t tokenId); - std::vector> vitualDeviceList_; + std::map> virtualDeviceMap_; + int32_t lastDeviceId_ = 0; }; } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/services/native/driver_extension_manager/include/input_emit_event/inject_thread.h b/services/native/driver_extension_manager/include/input_emit_event/inject_thread.h index a84efe2f3e22a969b14c3d511e4bbf869cc0db3c..ec18816319642188c261f2fcc83c0a29bb43829c 100644 --- a/services/native/driver_extension_manager/include/input_emit_event/inject_thread.h +++ b/services/native/driver_extension_manager/include/input_emit_event/inject_thread.h @@ -22,7 +22,7 @@ #include #include -#include "emit_event_types.h" +#include "hid_ddk_types.h" #include "virtual_device.h" namespace OHOS { @@ -31,7 +31,7 @@ class InjectThread { public: InjectThread(std::shared_ptr virtualDevice); virtual ~InjectThread(); - void WaitFunc(const std::vector &items); + void WaitFunc(const std::vector &items); void Start(); void Stop(); @@ -43,7 +43,7 @@ private: std::mutex mutex_; std::condition_variable conditionVariable_; bool threadRun_; - std::vector injectQueue_; + std::vector injectQueue_; std::thread thread_; std::shared_ptr virtualDevice_; }; diff --git a/services/native/driver_extension_manager/include/input_emit_event/virtual_device.h b/services/native/driver_extension_manager/include/input_emit_event/virtual_device.h index 36213c1a628cb155449af0c191bdb925d7d6adbf..46a865e8c04cc3475d33c4bb93e20b0cae016fff 100644 --- a/services/native/driver_extension_manager/include/input_emit_event/virtual_device.h +++ b/services/native/driver_extension_manager/include/input_emit_event/virtual_device.h @@ -22,12 +22,14 @@ #include #include "nocopyable.h" +#include "hid_ddk_types.h" namespace OHOS { namespace ExternalDeviceManager { class VirtualDevice { public: VirtualDevice(const char *deviceName, uint16_t productId); + VirtualDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties); DISALLOW_COPY_AND_MOVE(VirtualDevice); virtual ~VirtualDevice(); bool EmitEvent(uint16_t type, uint16_t code, uint32_t value) const; diff --git a/services/native/driver_extension_manager/include/input_emit_event/virtual_device_inject.h b/services/native/driver_extension_manager/include/input_emit_event/virtual_device_inject.h index 7b57a6a4ebb0bbcf5f11f5669fa628689591b4fb..bcfd7aae081fea5fa51bcd3be4981513f3bf1d6e 100644 --- a/services/native/driver_extension_manager/include/input_emit_event/virtual_device_inject.h +++ b/services/native/driver_extension_manager/include/input_emit_event/virtual_device_inject.h @@ -29,10 +29,9 @@ public: VirtualDeviceInject(std::shared_ptr virtualDevice); DISALLOW_COPY_AND_MOVE(VirtualDeviceInject); virtual ~VirtualDeviceInject(); - void EmitEvent(const std::vector &items); + void EmitEvent(const std::vector &items); private: std::unique_ptr injectThread_; - std::shared_ptr virtualDevice_; }; } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/services/native/driver_extension_manager/src/device_manager/BUILD.gn b/services/native/driver_extension_manager/src/device_manager/BUILD.gn index 2f0423b98127128aed428c5ac8f569a3bc36a824..c7b76318e0863279c2fd3ba71aa6eed16d771491 100644 --- a/services/native/driver_extension_manager/src/device_manager/BUILD.gn +++ b/services/native/driver_extension_manager/src/device_manager/BUILD.gn @@ -20,6 +20,7 @@ ohos_shared_library("driver_extension_dev_callback") { include_dirs = [ "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", "${ext_mgr_path}/services/native/driver_extension_manager/include/driver_controller", + "${ext_mgr_path}/interfaces/ddk/hid/", "${ext_mgr_path}/interfaces/innerkits/", ] configs = [ "${utils_path}:utils_config" ] @@ -41,6 +42,7 @@ ohos_shared_library("driver_extension_device_manager") { ] include_dirs = [ + "${ext_mgr_path}/interfaces/ddk/hid", "${ext_mgr_path}/interfaces/innerkits/", "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", "${ext_mgr_path}/services/native/driver_extension_manager/include/drivers_pkg_manager", diff --git a/services/native/driver_extension_manager/src/driver_ext_mgr.cpp b/services/native/driver_extension_manager/src/driver_ext_mgr.cpp index f219ecdbed6f316009baccd0a8dd58408bc33edf..d07ac61feae72fc1e73497974008285836d89aec 100644 --- a/services/native/driver_extension_manager/src/driver_ext_mgr.cpp +++ b/services/native/driver_extension_manager/src/driver_ext_mgr.cpp @@ -116,22 +116,22 @@ UsbErrCode DriverExtMgr::UnBindDevice(uint64_t deviceId) return static_cast(ExtDeviceManager::GetInstance().DisConnectDevice(deviceId)); } -UsbErrCode DriverExtMgr::CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) +int32_t DriverExtMgr::CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) { EDM_LOGD(MODULE_DEV_MGR, "%{public}s enter", __func__); - return static_cast(EmitEventManager::GetInstance().CreateDevice(maxX, maxY, maxPressure)); + return EmitEventManager::GetInstance().CreateDevice(hidDevice, hidEventProperties); } -UsbErrCode DriverExtMgr::EmitEvent(int32_t deviceId, const std::vector &items) +int32_t DriverExtMgr::EmitEvent(int32_t deviceId, const std::vector &items) { EDM_LOGD(MODULE_DEV_MGR, "%{public}s enter", __func__); - return static_cast(EmitEventManager::GetInstance().EmitEvent(deviceId, items)); + return EmitEventManager::GetInstance().EmitEvent(deviceId, items); } -UsbErrCode DriverExtMgr::DestroyDevice(void) +int32_t DriverExtMgr::DestroyDevice(int32_t deviceId) { EDM_LOGD(MODULE_DEV_MGR, "%{public}s enter", __func__); - return static_cast(EmitEventManager::GetInstance().DestroyDevice()); + return EmitEventManager::GetInstance().DestroyDevice(deviceId); } } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/services/native/driver_extension_manager/src/input_emit_event/BUILD.gn b/services/native/driver_extension_manager/src/input_emit_event/BUILD.gn index 09b4ab79b8624f800b24fd249b64e270b25d7a55..59e74db2fc8510b12a91c115f8c88f079da8df40 100644 --- a/services/native/driver_extension_manager/src/input_emit_event/BUILD.gn +++ b/services/native/driver_extension_manager/src/input_emit_event/BUILD.gn @@ -26,6 +26,7 @@ ohos_shared_library("input_emit_event") { include_dirs = [ "${ext_mgr_path}/services/native/driver_extension_manager/include/input_emit_event", + "${ext_mgr_path}/interfaces/ddk/hid", "${ext_mgr_path}/interfaces/innerkits", ] configs = [ "${utils_path}:utils_config" ] diff --git a/services/native/driver_extension_manager/src/input_emit_event/emit_event_manager.cpp b/services/native/driver_extension_manager/src/input_emit_event/emit_event_manager.cpp index 3a862c503fc00b1685f381383f751e75cedc04f0..0eeffcb4b1842135a1d30257dc8d4e6307e2f6a4 100644 --- a/services/native/driver_extension_manager/src/input_emit_event/emit_event_manager.cpp +++ b/services/native/driver_extension_manager/src/input_emit_event/emit_event_manager.cpp @@ -26,35 +26,60 @@ namespace OHOS { namespace ExternalDeviceManager { using namespace Security::AccessToken; IMPLEMENT_SINGLE_INSTANCE(EmitEventManager); -int32_t EmitEventManager::CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) +const uint16_t MAX_VIRTUAL_DEVICE_NUM = 200; +int32_t EmitEventManager::CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) { if (!HasPermission()) { - EDM_LOGE(MODULE_USB_DDK, "permission denied"); - return EDM_ERR_NO_PERM; + EDM_LOGE(MODULE_HID_DDK, "permission denied"); + return HID_DDK_NO_PERM; } - - DestroyDevice(); - vitualDeviceList_.push_back(std::make_unique(std::make_shared())); - vitualDeviceList_.push_back(std::make_unique - (std::make_shared(maxX, maxY, maxPressure))); - return EDM_OK; + // check device number + if (virtualDeviceMap_.size() >= MAX_VIRTUAL_DEVICE_NUM) { + EDM_LOGE(MODULE_HID_DDK, "device num exceeds maximum %{public}d", MAX_VIRTUAL_DEVICE_NUM); + return HID_DDK_FAILURE; + } + // get device id + int32_t id = GetCurDeviceId(); + if (id < 0) { + EDM_LOGE(MODULE_HID_DDK, "faild to generate device id"); + return HID_DDK_FAILURE; + } + // create device + virtualDeviceMap_[id] = + std::make_unique(std::make_shared(hidDevice, hidEventProperties)); + return id; } -int32_t EmitEventManager::EmitEvent(int32_t deviceId, const std::vector &items) +int32_t EmitEventManager::EmitEvent(int32_t deviceId, const std::vector &items) { - if (deviceId < 0 || deviceId >= vitualDeviceList_.size()) { - EDM_LOGE(MODULE_USB_DDK, "error deviceId"); - return EDM_NOK; + if (deviceId < 0) { + EDM_LOGE(MODULE_HID_DDK, "error deviceId"); + return HID_DDK_FAILURE; } - vitualDeviceList_[deviceId]->EmitEvent(items); - return EDM_OK; + if (virtualDeviceMap_.count(deviceId) == 0) { + EDM_LOGE(MODULE_HID_DDK, "device is not exit"); + return HID_DDK_FAILURE; + } + + virtualDeviceMap_[deviceId]->EmitEvent(items); + return HID_DDK_SUCCESS; } -int32_t EmitEventManager::DestroyDevice() +int32_t EmitEventManager::DestroyDevice(int32_t deviceId) { - vitualDeviceList_.clear(); - return EDM_OK; + if (deviceId < 0) { + EDM_LOGE(MODULE_HID_DDK, "error deviceId"); + return HID_DDK_FAILURE; + } + + if (virtualDeviceMap_.count(deviceId) == 0) { + EDM_LOGE(MODULE_HID_DDK, "device is not exit"); + return HID_DDK_FAILURE; + } + virtualDeviceMap_.erase(deviceId); + lastDeviceId_ = deviceId; + return HID_DDK_SUCCESS; } bool EmitEventManager::CheckHapPermission(uint32_t tokenId) @@ -83,5 +108,17 @@ bool EmitEventManager::HasPermission(void) } return false; } + +int32_t EmitEventManager::GetCurDeviceId(void) +{ + if (virtualDeviceMap_.count(lastDeviceId_) == 0) { + return lastDeviceId_; + } + int32_t id = virtualDeviceMap_.size(); + while (virtualDeviceMap_.count(id) != 0 && virtualDeviceMap_.size() < MAX_VIRTUAL_DEVICE_NUM) { + id++; + } + return virtualDeviceMap_.size() < MAX_VIRTUAL_DEVICE_NUM ? id : -1; +} } // namespace ExternalDeviceManager } // namespace OHOS \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/input_emit_event/inject_thread.cpp b/services/native/driver_extension_manager/src/input_emit_event/inject_thread.cpp index 6f9645aeec37a0fc17da7723f8e076a79e8cd2f2..3320cecd167b94a0f243012ee42cbd1ad9a6a2fe 100644 --- a/services/native/driver_extension_manager/src/input_emit_event/inject_thread.cpp +++ b/services/native/driver_extension_manager/src/input_emit_event/inject_thread.cpp @@ -62,7 +62,7 @@ void InjectThread::InjectFunc() } } -void InjectThread::WaitFunc(const std::vector &items) +void InjectThread::WaitFunc(const std::vector &items) { std::lock_guard lockGuard(mutex_); injectQueue_.insert(injectQueue_.begin(), items.begin(), items.end()); diff --git a/services/native/driver_extension_manager/src/input_emit_event/virtual_device.cpp b/services/native/driver_extension_manager/src/input_emit_event/virtual_device.cpp index e7885f20021f76ef3a58df68a9b012fada8fdab1..2d11ed9894c6be01d486c1e3f3f63172ee9d335c 100644 --- a/services/native/driver_extension_manager/src/input_emit_event/virtual_device.cpp +++ b/services/native/driver_extension_manager/src/input_emit_event/virtual_device.cpp @@ -44,6 +44,31 @@ VirtualDevice::VirtualDevice(const char *deviceName, uint16_t productId) { } +VirtualDevice::VirtualDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) + : deviceName_(hidDevice->deviceName), + busType_(hidDevice->bustype), + vendorId_(hidDevice->vendorId), + productId_(hidDevice->productId), + version_(hidDevice->version) +{ + properties_ = std::vector(hidDevice->properties, hidDevice->properties + hidDevice->propLength); + eventTypes_ = std::vector(hidEventProperties->hidEventTypes.hidEventType, + hidEventProperties->hidEventTypes.hidEventType + hidEventProperties->hidEventTypes.length); + keys_ = std::vector(hidEventProperties->hidKeys.hidKeyCode, + hidEventProperties->hidKeys.hidKeyCode + hidEventProperties->hidKeys.length); + abs_ = std::vector(hidEventProperties->hidAbs.hidAbsAxes, + hidEventProperties->hidAbs.hidAbsAxes + hidEventProperties->hidAbs.length); + relBits_ = std::vector(hidEventProperties->hidRelBits.hidRelAxes, + hidEventProperties->hidRelBits.hidRelAxes + hidEventProperties->hidRelBits.length); + miscellaneous_ = std::vector(hidEventProperties->hidMiscellaneous.hidMscEvent, + hidEventProperties->hidMiscellaneous.hidMscEvent + hidEventProperties->hidMiscellaneous.length); + const int absLength = 64; + std::copy(hidEventProperties->hidAbsMax, hidEventProperties->hidAbsMax + absLength, uinputDev_.absmax); + std::copy(hidEventProperties->hidAbsMin, hidEventProperties->hidAbsMin + absLength, uinputDev_.absmin); + std::copy(hidEventProperties->hidAbsFuzz, hidEventProperties->hidAbsFuzz + absLength, uinputDev_.absfuzz); + std::copy(hidEventProperties->hidAbsFlat, hidEventProperties->hidAbsFlat + absLength, uinputDev_.absflat); +} + VirtualDevice::~VirtualDevice() { if (fd_ >= 0) { diff --git a/services/native/driver_extension_manager/src/input_emit_event/virtual_device_inject.cpp b/services/native/driver_extension_manager/src/input_emit_event/virtual_device_inject.cpp index d71b8b45b8d6dea1a7b314c1ed62c480d0c48365..717e6825b44842d9dae61b0214af9b49cf3cc5a8 100644 --- a/services/native/driver_extension_manager/src/input_emit_event/virtual_device_inject.cpp +++ b/services/native/driver_extension_manager/src/input_emit_event/virtual_device_inject.cpp @@ -19,9 +19,8 @@ namespace OHOS { namespace ExternalDeviceManager { VirtualDeviceInject::VirtualDeviceInject(std::shared_ptr virtualDevice) { - virtualDevice_ = virtualDevice; - virtualDevice_->SetUp(); - injectThread_ = std::make_unique(virtualDevice_); + virtualDevice->SetUp(); + injectThread_ = std::make_unique(virtualDevice); injectThread_->Start(); } @@ -30,7 +29,7 @@ VirtualDeviceInject::~VirtualDeviceInject() injectThread_->Stop(); } -void VirtualDeviceInject::EmitEvent(const std::vector &items) +void VirtualDeviceInject::EmitEvent(const std::vector &items) { injectThread_->WaitFunc(items); } diff --git a/services/zidl/include/driver_ext_mgr_proxy.h b/services/zidl/include/driver_ext_mgr_proxy.h index 7faffc3bf3d0085fdbace282087880c7f183abef..3471223c59bca83b4e3dc81cd40d342787835b3c 100644 --- a/services/zidl/include/driver_ext_mgr_proxy.h +++ b/services/zidl/include/driver_ext_mgr_proxy.h @@ -29,9 +29,9 @@ public: UsbErrCode QueryDevice(uint32_t busType, std::vector> &devices) override; UsbErrCode BindDevice(uint64_t deviceId, const sptr &connectCallback) override; UsbErrCode UnBindDevice(uint64_t deviceId) override; - UsbErrCode CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) override; - UsbErrCode EmitEvent(int32_t deviceId, const std::vector &items) override; - UsbErrCode DestroyDevice(void) override; + int32_t CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) override; + int32_t EmitEvent(int32_t deviceId, const std::vector &items) override; + int32_t DestroyDevice(int32_t deviceId) override; private: static inline BrokerDelegator delegator_; diff --git a/services/zidl/src/driver_ext_mgr_proxy.cpp b/services/zidl/src/driver_ext_mgr_proxy.cpp index d84fa6b14a6fc4037b05ff0142e2bd5bd6b80be0..3cf79711cf38c13b275705ad4b4d39cd36cce5d5 100644 --- a/services/zidl/src/driver_ext_mgr_proxy.cpp +++ b/services/zidl/src/driver_ext_mgr_proxy.cpp @@ -143,12 +143,12 @@ UsbErrCode DriverExtMgrProxy::UnBindDevice(uint64_t deviceId) return UsbErrCode::EDM_OK; } -UsbErrCode DriverExtMgrProxy::CreateDevice(uint32_t maxX, uint32_t maxY, uint32_t maxPressure) +int32_t DriverExtMgrProxy::CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) { sptr remote = Remote(); if (remote == nullptr) { EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr"); - return UsbErrCode::EDM_ERR_INVALID_OBJECT; + return HID_DDK_FAILURE; } MessageParcel data; @@ -157,39 +157,33 @@ UsbErrCode DriverExtMgrProxy::CreateDevice(uint32_t maxX, uint32_t maxY, uint32_ if (!data.WriteInterfaceToken(GetDescriptor())) { EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; - } - - if (!data.WriteUint32(maxX)) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to write maxX"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; + return HID_DDK_INVALID_PARAMETER; } - if (!data.WriteUint32(maxY)) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to write maxY"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; + if (!HidDeviceMarshalling(hidDevice, data)) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to marshall HidDevice"); + return HID_DDK_INVALID_PARAMETER; } - if (!data.WriteUint32(maxPressure)) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to write maxPressure"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; + if (!HidEventPropertiesMarshalling(hidEventProperties, data)) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to marshall HidEventProperties"); + return HID_DDK_INVALID_PARAMETER; } int32_t ret = remote->SendRequest(static_cast(DriverExtMgrInterfaceCode::INPUT_CREATE_DEVICE), data, reply, option); - if (ret != UsbErrCode::EDM_OK) { + if (ret < HID_DDK_SUCCESS) { EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret); - return static_cast(ret); } - return UsbErrCode::EDM_OK; + return ret; } -UsbErrCode DriverExtMgrProxy::EmitEvent(int32_t deviceId, const std::vector &items) +int32_t DriverExtMgrProxy::EmitEvent(int32_t deviceId, const std::vector &items) { sptr remote = Remote(); if (remote == nullptr) { EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr"); - return UsbErrCode::EDM_ERR_INVALID_OBJECT; + return HID_DDK_FAILURE; } MessageParcel data; @@ -198,29 +192,29 @@ UsbErrCode DriverExtMgrProxy::EmitEvent(int32_t deviceId, const std::vectorSendRequest(static_cast(DriverExtMgrInterfaceCode::INPUT_EMIT_EVENT), data, reply, option); - if (ret != UsbErrCode::EDM_OK) { + if (ret != HID_DDK_SUCCESS) { EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret); - return static_cast(ret); + return ret; } - return UsbErrCode::EDM_OK; + return HID_DDK_SUCCESS; } -UsbErrCode DriverExtMgrProxy::DestroyDevice(void) +int32_t DriverExtMgrProxy::DestroyDevice(int32_t deviceId) { sptr remote = Remote(); if (remote == nullptr) { EDM_LOGE(MODULE_FRAMEWORK, "remote is nullptr"); - return UsbErrCode::EDM_ERR_INVALID_OBJECT; + return HID_DDK_FAILURE; } MessageParcel data; @@ -229,16 +223,21 @@ UsbErrCode DriverExtMgrProxy::DestroyDevice(void) if (!data.WriteInterfaceToken(GetDescriptor())) { EDM_LOGE(MODULE_FRAMEWORK, "failed to write interface token"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; + return HID_DDK_INVALID_PARAMETER; + } + + if (!data.WriteUint32(deviceId)) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to write device id"); + return HID_DDK_INVALID_PARAMETER; } int32_t ret = remote->SendRequest( static_cast(DriverExtMgrInterfaceCode::INPUT_DESTROY_DEVICE), data, reply, option); - if (ret != UsbErrCode::EDM_OK) { + if (ret != HID_DDK_SUCCESS) { EDM_LOGE(MODULE_FRAMEWORK, "SendRequest is failed, ret: %{public}d", ret); - return static_cast(ret); + return ret; } - return UsbErrCode::EDM_OK; + return HID_DDK_SUCCESS; } } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/services/zidl/src/driver_ext_mgr_stub.cpp b/services/zidl/src/driver_ext_mgr_stub.cpp index 966e6e6e6866ad406e57c0ead0b6a0b034403e46..58515dc97f2bfbddee03c7e3bf6d3519d34f370d 100644 --- a/services/zidl/src/driver_ext_mgr_stub.cpp +++ b/services/zidl/src/driver_ext_mgr_stub.cpp @@ -130,31 +130,31 @@ int32_t DriverExtMgrStub::OnUnBindDevice(MessageParcel &data, MessageParcel &rep int32_t DriverExtMgrStub::OnCreateDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option) { - uint32_t maxX = 0; - if (!data.ReadUint32(maxX)) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to read maxX"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; - } - - uint32_t maxY = 0; - if (!data.ReadUint32(maxY)) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to read maxY"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; - } - - uint32_t maxPressure = 0; - if (!data.ReadUint32(maxPressure)) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to read maxPressure"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; - } - - UsbErrCode ret = CreateDevice(maxX, maxY, maxPressure); - if (ret != UsbErrCode::EDM_OK) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to call CreateDevice function:%{public}d", static_cast(ret)); - return ret; - } - - return UsbErrCode::EDM_OK; + auto hidDevice = HidDeviceUnMarshalling(data); + if (!hidDevice.has_value()) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to read hidDevice"); + return HID_DDK_INVALID_PARAMETER; + } + + auto hidEventProperties = HidEventPropertiesUnMarshalling(data); + if (!hidEventProperties.has_value()) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to read hidEventProperties"); + return HID_DDK_INVALID_PARAMETER; + } + + auto device = &(hidDevice.value()); + auto eventProperties = &(hidEventProperties.value()); + int32_t ret = CreateDevice(device, eventProperties); + delete device->properties; + delete eventProperties->hidEventTypes.hidEventType; + delete eventProperties->hidKeys.hidKeyCode; + delete eventProperties->hidAbs.hidAbsAxes; + delete eventProperties->hidRelBits.hidRelAxes; + delete eventProperties->hidMiscellaneous.hidMscEvent; + if (ret < HID_DDK_SUCCESS) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to call CreateDevice function:%{public}d", ret); + } + return ret; } int32_t DriverExtMgrStub::OnEmitEvent(MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -163,27 +163,33 @@ int32_t DriverExtMgrStub::OnEmitEvent(MessageParcel &data, MessageParcel &reply, auto items = EmitItemUnMarshalling(data, deviceId); if (!items.has_value()) { EDM_LOGE(MODULE_FRAMEWORK, "failed to read emit items"); - return UsbErrCode::EDM_ERR_INVALID_PARAM; + return HID_DDK_INVALID_PARAMETER; } - UsbErrCode ret = EmitEvent(deviceId, items.value()); - if (ret != UsbErrCode::EDM_OK) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to call EmitEvent function:%{public}d", static_cast(ret)); + int32_t ret = EmitEvent(deviceId, items.value()); + if (ret != HID_DDK_SUCCESS) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to call EmitEvent function:%{public}d", ret); return ret; } - return UsbErrCode::EDM_OK; + return HID_DDK_SUCCESS; } int32_t DriverExtMgrStub::OnDestroyDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option) { - UsbErrCode ret = DestroyDevice(); - if (ret != UsbErrCode::EDM_OK) { - EDM_LOGE(MODULE_FRAMEWORK, "failed to call DestroyDevice function:%{public}d", static_cast(ret)); + uint32_t deviceId = 0; + if (!data.ReadUint32(deviceId)) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId"); + return HID_DDK_INVALID_PARAMETER; + } + + int32_t ret = DestroyDevice(static_cast(deviceId)); + if (ret != HID_DDK_SUCCESS) { + EDM_LOGE(MODULE_FRAMEWORK, "failed to call DestroyDevice function:%{public}d", ret); return ret; } - return UsbErrCode::EDM_OK; + return HID_DDK_SUCCESS; } } // namespace ExternalDeviceManager } // namespace OHOS diff --git a/services/zidl/src/driver_ext_mgr_types.cpp b/services/zidl/src/driver_ext_mgr_types.cpp index a9cf0512983e48ad3479497c64e836a730d37f0b..3b55652ed2379b6898e853627bd11615fe979e86 100644 --- a/services/zidl/src/driver_ext_mgr_types.cpp +++ b/services/zidl/src/driver_ext_mgr_types.cpp @@ -155,7 +155,7 @@ std::shared_ptr USBDevice::UnMarshalling(MessageParcel &parcel) return device; } -bool EmitItemMarshalling(int32_t deviceId, const std::vector &items, MessageParcel &parcel) +bool EmitItemMarshalling(int32_t deviceId, const std::vector &items, MessageParcel &parcel) { if (!parcel.WriteUint32(deviceId)) { EDM_LOGE(MODULE_DEV_MGR, "failed to write device id"); @@ -186,7 +186,7 @@ bool EmitItemMarshalling(int32_t deviceId, const std::vector &items, M return true; } -std::optional> EmitItemUnMarshalling(MessageParcel &parcel, int32_t &deviceId) +std::optional> EmitItemUnMarshalling(MessageParcel &parcel, int32_t &deviceId) { uint32_t id = 0; if (!parcel.ReadUint32(id)) { @@ -206,8 +206,8 @@ std::optional> EmitItemUnMarshalling(MessageParcel &parcel return std::nullopt; } - std::vector items; - EmitItem item; + std::vector items; + Hid_EmitItem item; for (uint32_t i = 0; i < size; ++i) { if (!parcel.ReadUint16(item.type)) { EDM_LOGE(MODULE_DEV_MGR, "failed to read type"); @@ -228,6 +228,412 @@ std::optional> EmitItemUnMarshalling(MessageParcel &parcel return items; } +bool HidDeviceMarshalling(Hid_Device *hidDevice, MessageParcel &parcel) +{ + if (!parcel.WriteString(static_cast(hidDevice->deviceName))) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write deviceName"); + return false; + } + + if (!parcel.WriteUint16(hidDevice->vendorId)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write vendorId"); + return false; + } + + if (!parcel.WriteUint16(hidDevice->productId)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write productId"); + return false; + } + + if (!parcel.WriteUint16(hidDevice->version)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write version"); + return false; + } + + if (!parcel.WriteUint16(hidDevice->bustype)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write bustype"); + return false; + } + + if (!parcel.WriteUint16(hidDevice->propLength)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write properties length"); + return false; + } + + for (uint16_t i = 0; i < hidDevice->propLength; ++i) { + uint32_t ele = static_cast(hidDevice->properties[i]); + if (!parcel.WriteUint32(ele)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write properties"); + return false; + } + } + + return true; +} + +std::optional HidDeviceUnMarshalling(MessageParcel &parcel) +{ + Hid_Device hidDevice; + std::string str; + if (!parcel.ReadString(str)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read deviceName"); + return std::nullopt; + } + hidDevice.deviceName = (char *)str.c_str(); + + if (!parcel.ReadUint16(hidDevice.vendorId)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read vendorId"); + return std::nullopt; + } + + if (!parcel.ReadUint16(hidDevice.productId)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read productId"); + return std::nullopt; + } + + if (!parcel.ReadUint16(hidDevice.version)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read version"); + return std::nullopt; + } + + if (!parcel.ReadUint16(hidDevice.bustype)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read bustype"); + return std::nullopt; + } + + if (!parcel.ReadUint16(hidDevice.propLength)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read properties length"); + return std::nullopt; + } + + if (hidDevice.propLength > MAX_HID_DEVICE_PROP_LEN) { + EDM_LOGE(MODULE_DEV_MGR, "properties length out of range"); + return std::nullopt; + } + + uint32_t property = 0; + Hid_DeviceProp *hidDeviceProp = new Hid_DeviceProp[MAX_HID_DEVICE_PROP_LEN]; + for (uint16_t i = 0; i < hidDevice.propLength; ++i) { + if (!parcel.ReadUint32(property)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read properties"); + return std::nullopt; + } + hidDeviceProp[i] = static_cast(property); + } + hidDevice.properties = hidDeviceProp; + + return hidDevice; +} + +bool HidEventPropertiesMarshalling(Hid_EventProperties *hidEventProperties, MessageParcel &parcel) +{ + uint16_t hidEventTypesLen = hidEventProperties->hidEventTypes.length; + if (!parcel.WriteUint16(hidEventTypesLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidEventTypes length"); + return false; + } + + for (uint16_t i = 0; i < hidEventTypesLen; ++i) { + uint32_t ele = static_cast(hidEventProperties->hidEventTypes.hidEventType[i]); + if (!parcel.WriteUint32(ele)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidEventTypes"); + return false; + } + } + + if (!HidKeysOrAxisPropertiesMarshalling(hidEventProperties, parcel)) { + return false; + } + + uint16_t hidMiscellaneousLen = hidEventProperties->hidMiscellaneous.length; + if (!parcel.WriteUint16(hidMiscellaneousLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidMiscellaneous length"); + return false; + } + + for (uint16_t i = 0; i < hidMiscellaneousLen; ++i) { + uint32_t ele = static_cast(hidEventProperties->hidMiscellaneous.hidMscEvent[i]); + if (!parcel.WriteUint32(ele)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidMiscellaneous"); + return false; + } + } + + if (!HidAbsValueMarshalling(hidEventProperties, parcel)) { + return false; + } + + return true; +} + +bool HidKeysOrAxisPropertiesMarshalling(Hid_EventProperties *hidEventProperties, MessageParcel &parcel) +{ + uint16_t hidKeysLen = hidEventProperties->hidKeys.length; + if (!parcel.WriteUint16(hidKeysLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidKeys length"); + return false; + } + + for (uint16_t i = 0; i < hidKeysLen; ++i) { + uint32_t ele = static_cast(hidEventProperties->hidKeys.hidKeyCode[i]); + if (!parcel.WriteUint32(ele)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidKeys"); + return false; + } + } + + uint16_t hidAbsLen = hidEventProperties->hidAbs.length; + if (!parcel.WriteUint16(hidAbsLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidAbs length"); + return false; + } + + for (uint16_t i = 0; i < hidAbsLen; ++i) { + uint32_t ele = static_cast(hidEventProperties->hidAbs.hidAbsAxes[i]); + if (!parcel.WriteUint32(ele)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidAbs"); + return false; + } + } + + uint16_t hidRelBitsLen = hidEventProperties->hidRelBits.length; + if (!parcel.WriteUint16(hidRelBitsLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidRelBits length"); + return false; + } + + for (uint16_t i = 0; i < hidRelBitsLen; ++i) { + uint32_t ele = static_cast(hidEventProperties->hidRelBits.hidRelAxes[i]); + if (!parcel.WriteUint32(ele)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidRelBits"); + return false; + } + } + + return true; +} + +bool HidAbsValueMarshalling(Hid_EventProperties *hidEventProperties, MessageParcel &parcel) +{ + const int absLength = 64; + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.WriteInt32(hidEventProperties->hidAbsMax[i])) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidAbsMax"); + return false; + } + } + + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.WriteInt32(hidEventProperties->hidAbsMin[i])) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidAbsMin"); + return false; + } + } + + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.WriteInt32(hidEventProperties->hidAbsFuzz[i])) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidAbsFuzz"); + return false; + } + } + + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.WriteInt32(hidEventProperties->hidAbsFlat[i])) { + EDM_LOGE(MODULE_DEV_MGR, "failed to write hidAbsFlat"); + return false; + } + } + + return true; +} + +std::optional HidEventPropertiesUnMarshalling(MessageParcel &parcel) +{ + Hid_EventProperties hidEventProperties; + + if (!HidEventTypeOrKeysPropertiesUnMarshalling(parcel, hidEventProperties)) { + return std::nullopt; + } + + if (!HidAxisPropertiesUnMarshalling(parcel, hidEventProperties)) { + return std::nullopt; + } + + uint16_t hidMiscellaneousLen = 0; + if (!parcel.ReadUint16(hidMiscellaneousLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidMiscellaneous length"); + return std::nullopt; + } + hidEventProperties.hidMiscellaneous.length = hidMiscellaneousLen; + + if (hidMiscellaneousLen > MAX_HID_MISC_EVENT_LEN) { + EDM_LOGE(MODULE_DEV_MGR, "hidMiscellaneous length out of range"); + return std::nullopt; + } + + uint32_t hidMiscellaneous = 0; + Hid_MscEvent *hidMscEvent = new Hid_MscEvent[MAX_HID_MISC_EVENT_LEN]; + for (uint16_t i = 0; i < hidMiscellaneousLen; ++i) { + if (!parcel.ReadUint32(hidMiscellaneous)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidMiscellaneous"); + return std::nullopt; + } + hidMscEvent[i] = static_cast(hidMiscellaneous); + } + hidEventProperties.hidMiscellaneous.hidMscEvent = hidMscEvent; + + ; + if (!HidAbsValueUnMarshalling(parcel, hidEventProperties)) { + return std::nullopt; + } + + return hidEventProperties; +} + +bool HidEventTypeOrKeysPropertiesUnMarshalling(MessageParcel &parcel, Hid_EventProperties &hidEventProperties) +{ + uint16_t hidEventTypesLen = 0; + if (!parcel.ReadUint16(hidEventTypesLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidEventTypes length"); + return false; + } + hidEventProperties.hidEventTypes.length = hidEventTypesLen; + + if (hidEventTypesLen > MAX_HID_EVENT_TYPES_LEN) { + EDM_LOGE(MODULE_DEV_MGR, "hidEventTypes length out of range"); + return false; + } + + uint32_t hidEventType = 0; + Hid_EventType *hidEventTypes = new Hid_EventType[MAX_HID_EVENT_TYPES_LEN]; + for (uint16_t i = 0; i < hidEventTypesLen; ++i) { + if (!parcel.ReadUint32(hidEventType)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidEventTypes"); + return false; + } + hidEventTypes[i] = static_cast(hidEventType); + } + hidEventProperties.hidEventTypes.hidEventType = hidEventTypes; + + uint16_t hidKeysLen = 0; + if (!parcel.ReadUint16(hidKeysLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidKeys length"); + return false; + } + hidEventProperties.hidKeys.length = hidKeysLen; + + if (hidKeysLen > MAX_HID_KEYS_LEN) { + EDM_LOGE(MODULE_DEV_MGR, "hidKeys length out of range"); + return false; + } + + uint32_t hidKey = 0; + Hid_KeyCode *hidKeyCode = new Hid_KeyCode[MAX_HID_KEYS_LEN]; + for (uint16_t i = 0; i < hidKeysLen; ++i) { + if (!parcel.ReadUint32(hidKey)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidKeys"); + return false; + } + hidKeyCode[i] = static_cast(hidKey); + } + hidEventProperties.hidKeys.hidKeyCode = hidKeyCode; + + return true; +} + +bool HidAxisPropertiesUnMarshalling(MessageParcel &parcel, Hid_EventProperties &hidEventProperties) +{ + uint16_t hidAbsLen = 0; + if (!parcel.ReadUint16(hidAbsLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidAbs length"); + return false; + } + hidEventProperties.hidAbs.length = hidAbsLen; + + if (hidAbsLen > MAX_HID_ABS_LEN) { + EDM_LOGE(MODULE_DEV_MGR, "hidAbs length out of range"); + return false; + } + + uint32_t hidAbs = 0; + Hid_AbsAxes *hidAbsAxes = new Hid_AbsAxes[MAX_HID_ABS_LEN]; + for (uint16_t i = 0; i < hidAbsLen; ++i) { + if (!parcel.ReadUint32(hidAbs)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidAbs"); + return false; + } + hidAbsAxes[i] = static_cast(hidAbs); + } + hidEventProperties.hidAbs.hidAbsAxes = hidAbsAxes; + + uint16_t hidRelBitsLen = 0; + if (!parcel.ReadUint16(hidRelBitsLen)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidRelBits length"); + return false; + } + hidEventProperties.hidRelBits.length = hidRelBitsLen; + + if (hidRelBitsLen > MAX_HID_REL_BITS_LEN) { + EDM_LOGE(MODULE_DEV_MGR, "hidRelBits length out of range"); + return false; + } + + uint32_t hidRelBit = 0; + Hid_RelAxes *hidRelAxes = new Hid_RelAxes[MAX_HID_REL_BITS_LEN]; + for (uint16_t i = 0; i < hidRelBitsLen; ++i) { + if (!parcel.ReadUint32(hidRelBit)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidRelBits"); + return false; + } + hidRelAxes[i] = static_cast(hidRelBit); + } + hidEventProperties.hidRelBits.hidRelAxes = hidRelAxes; + + return true; +} + +bool HidAbsValueUnMarshalling(MessageParcel &parcel, Hid_EventProperties &hidEventProperties) +{ + const int absLength = 64; + int32_t absMax = 0; + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.ReadInt32(absMax)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidAbsMax"); + return false; + } + hidEventProperties.hidAbsMax[i] = absMax; + } + + int32_t absMin = 0; + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.ReadInt32(absMin)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidAbsMin"); + return false; + } + hidEventProperties.hidAbsMin[i] = absMin; + } + + int32_t absFuzz = 0; + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.ReadInt32(absFuzz)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidAbsFuzz"); + return false; + } + hidEventProperties.hidAbsFuzz[i] = absFuzz; + } + + int32_t absFlat = 0; + for (uint16_t i = 0; i < absLength; ++i) { + if (!parcel.ReadInt32(absFlat)) { + EDM_LOGE(MODULE_DEV_MGR, "failed to read hidAbsFlat"); + return false; + } + hidEventProperties.hidAbsFlat[i] = absFlat; + } + + return true; +} + std::string USBDevice::Dump() { std::stringstream os; diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 2a080b9481d0ba900bedb9ed139d029dbfa2c994..a25ba42951d2a265a3acf85113808d029342f311 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -54,6 +54,7 @@ ohos_executable("bus_extension_core_mt") { "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/usb", "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/core", + "${ext_mgr_path}/interfaces/ddk/hid/", "${ext_mgr_path}/interfaces/innerkits/", ] configs = [ "${utils_path}:utils_config" ] @@ -78,6 +79,7 @@ ohos_executable("ext_device_manager_mt") { "${ext_mgr_path}/services/native/driver_extension_manager/include/device_manager", "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/usb", "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/core", + "${ext_mgr_path}/interfaces/ddk/hid/", "${ext_mgr_path}/interfaces/innerkits/", "${ext_mgr_path}/services/native/driver_extension_manager/include/drivers_pkg_manager", "${ext_mgr_path}/services/native/driver_extension_manager/include/driver_controller", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index e18a9890bb6f0c8e3f6086b79e9884032598139f..dfe1256aa6013ede217f448002094950689156fd 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -106,7 +106,10 @@ ohos_unittest("driver_extension_manager_client_test") { sources = [ "driver_extension_manager_client_test/driver_extension_manager_client_test.cpp" ] - include_dirs = [ "${ext_mgr_path}/services/zidl/include" ] + include_dirs = [ + "${ext_mgr_path}/interfaces/ddk/hid", + "${ext_mgr_path}/services/zidl/include", + ] deps = [ "${ext_mgr_path}/interfaces/innerkits:driver_ext_mgr_client" ] @@ -127,5 +130,6 @@ group("external_device_manager_ut") { ":drivers_pkg_manager_test", "device_manager_js_test:DeviceManagerJsTest", "device_manager_test:device_manager_test", + "emit_event_test:emit_event_test", ] } diff --git a/test/unittest/device_manager_test/BUILD.gn b/test/unittest/device_manager_test/BUILD.gn index 31286a831987194bba25c018f5b552322c162a35..69a30bdd36c667ca6fd80b8a980d632b3985a3f1 100644 --- a/test/unittest/device_manager_test/BUILD.gn +++ b/test/unittest/device_manager_test/BUILD.gn @@ -23,6 +23,7 @@ ohos_unittest("device_manager_test") { "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/usb", "${ext_mgr_path}/services/native/driver_extension_manager/include/bus_extension/core", "${ext_mgr_path}/services/native/driver_extension_manager/include/drivers_pkg_manager", + "${ext_mgr_path}/interfaces/ddk/hid/", "${ext_mgr_path}/interfaces/innerkits/", ] deps = [ diff --git a/test/unittest/emit_event_test/BUILD.gn b/test/unittest/emit_event_test/BUILD.gn index 98cefc8ebdef2f15d49ec5f14ef78af70ed8eb74..22565511b622c5060f91e5553e9ac7e7867a64e7 100644 --- a/test/unittest/emit_event_test/BUILD.gn +++ b/test/unittest/emit_event_test/BUILD.gn @@ -18,7 +18,10 @@ module_output_path = "external_device_manager/unittest" ohos_unittest("emit_event_test") { module_out_path = "${module_output_path}" sources = [ "emit_event_test.cpp" ] - include_dirs = [ "${ext_mgr_path}/interfaces/innerkits/" ] + include_dirs = [ + "${ext_mgr_path}/interfaces/ddk/hid/", + "${ext_mgr_path}/interfaces/innerkits/", + ] deps = [ "${ext_mgr_path}/interfaces/innerkits:driver_ext_mgr_client" ] external_deps = [ diff --git a/test/unittest/emit_event_test/emit_event_test.cpp b/test/unittest/emit_event_test/emit_event_test.cpp index 3d2df65387cc8280b39e03886bc2782aa3c51ade..c010976e6587d892294f0aea5ed85eec9fb97da9 100644 --- a/test/unittest/emit_event_test/emit_event_test.cpp +++ b/test/unittest/emit_event_test/emit_event_test.cpp @@ -15,6 +15,7 @@ #include #include +#include #include "driver_ext_mgr_client.h" #include "hilog_wrapper.h" @@ -29,47 +30,184 @@ public: void SetUp() override {}; void TearDown() override {}; + static Hid_Device* CreateDeviceMock(int8_t isPropExMax); + static Hid_EventProperties* CreateEventPropertiesMock(int8_t isEventTypeExMax, int8_t isKeyExMax, int8_t isAbsExMax, + int8_t isRelExMax, int8_t isMscExMax); static DriverExtMgrClient &edmClient; + static int32_t deviceId; }; DriverExtMgrClient &EmitEventTest::edmClient = DriverExtMgrClient::GetInstance(); +int32_t EmitEventTest::deviceId = -1; -void EmitEventTest::SetUpTestCase() +void EmitEventTest::SetUpTestCase() {} + +void EmitEventTest::TearDownTestCase() {} + +Hid_Device* EmitEventTest::CreateDeviceMock(int8_t isPropExMax) { - auto ret = edmClient.CreateDevice(100, 100, 100); - ASSERT_GE(ret, 0); + Hid_DeviceProp hidDeviceProp[MAX_HID_DEVICE_PROP_LEN] = { HID_PROP_POINTER, HID_PROP_DIRECT }; + Hid_Device hidDevice = { + .deviceName = "VSoC keyboard", + .vendorId = 0x6006, + .productId = 0x6008, + .version = 1, + .bustype = BUS_USB, + .properties = hidDeviceProp, + .propLength = isPropExMax ? (MAX_HID_DEVICE_PROP_LEN + 1) : 2 + }; + return &hidDevice; } -void EmitEventTest::TearDownTestCase() +Hid_EventProperties* EmitEventTest::CreateEventPropertiesMock(int8_t isEventTypeExMax, int8_t isKeyExMax, + int8_t isAbsExMax, int8_t isRelExMax, int8_t isMscExMax) { - int32_t ret = edmClient.DestroyDevice(); - ASSERT_EQ(ret, 0); + Hid_EventType hidEventType[MAX_HID_EVENT_TYPES_LEN] = { HID_EV_SYN, HID_EV_KEY }; + Hid_KeyCode hidKeyCode[MAX_HID_KEYS_LEN] = { HID_KEY_A, HID_KEY_B }; + Hid_AbsAxes hidAbsAxes[MAX_HID_ABS_LEN] = { HID_ABS_X, HID_ABS_Y }; + Hid_RelAxes hidRelAxes[MAX_HID_REL_BITS_LEN] = { HID_REL_X, HID_REL_Y }; + Hid_MscEvent hidMscEvent[MAX_HID_MISC_EVENT_LEN] = { HID_MSC_SERIAL, HID_MSC_PULSELED }; + + Hid_EventProperties hidEventProperties = { + .hidEventTypes = { hidEventType, isEventTypeExMax ? (MAX_HID_EVENT_TYPES_LEN + 1) : 2 }, + .hidKeys = { hidKeyCode, isKeyExMax ? (MAX_HID_KEYS_LEN + 1) : 2 }, + .hidAbs = { hidAbsAxes, isAbsExMax ? (MAX_HID_ABS_LEN + 1) : 2 }, + .hidRelBits = { hidRelAxes, isRelExMax ? (MAX_HID_REL_BITS_LEN + 1) : 2 }, + .hidMiscellaneous = { hidMscEvent, isMscExMax ? (MAX_HID_MISC_EVENT_LEN + 1) : 2 } + }; + return &hidEventProperties; } HWTEST_F(EmitEventTest, EmitEvent001, TestSize.Level1) { - std::vector items({ - {0, 1, 0x14a, 108}, - {0, 3, 0, 50 }, - {0, 3, 1, 50 }, - {0, 0, 0, 0 } - }); + std::vector items = { + {1, 0x14a, 108}, + {3, 0, 50 }, + {3, 1, 50 } + }; auto ret = edmClient.EmitEvent(0, items); ASSERT_EQ(ret, 0); } HWTEST_F(EmitEventTest, EmitEvent002, TestSize.Level1) { - std::vector items(21, {0, 1, 0x14a, 108}); - auto ret = edmClient.EmitEvent(0, items); + const uint16_t len = 21; + std::vector items; + Hid_EmitItem item = {1, 0x14a, 108}; + for (uint16_t i = 0; i < len; ++i) { + items[i] = item; + } + auto ret = edmClient.EmitEvent(deviceId, items); ASSERT_NE(ret, 0); } HWTEST_F(EmitEventTest, EmitEvent003, TestSize.Level1) { - std::vector items(20, {0, 1, 0x14a, 108}); - auto ret = edmClient.EmitEvent(0, items); + const uint16_t len = 20; + std::vector items; + Hid_EmitItem item = {1, 0x14a, 108}; + for (uint16_t i = 0; i < len; ++i) { + items[i] = item; + } + auto ret = edmClient.EmitEvent(deviceId, items); + ASSERT_EQ(ret, 0); +} + +HWTEST_F(EmitEventTest, CreateDevice001, TestSize.Level1) +{ + auto device = CreateDeviceMock(0); + auto eventProperties = CreateEventPropertiesMock(0, 0, 0, 0, 0); + auto ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_GE(ret, 0); + deviceId = ret; +} + +HWTEST_F(EmitEventTest, CreateDevice002, TestSize.Level1) +{ + auto device = CreateDeviceMock(1); + auto eventProperties = CreateEventPropertiesMock(0, 0, 0, 0, 0); + auto ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_LT(ret, 0); +} + +HWTEST_F(EmitEventTest, CreateDevice003, TestSize.Level1) +{ + auto device = CreateDeviceMock(0); + auto eventProperties = CreateEventPropertiesMock(1, 0, 0, 0, 0); + auto ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_LT(ret, 0); +} + +HWTEST_F(EmitEventTest, CreateDevice004, TestSize.Level1) +{ + auto device = CreateDeviceMock(0); + auto eventProperties = CreateEventPropertiesMock(0, 1, 0, 0, 0); + auto ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_LT(ret, 0); +} + +HWTEST_F(EmitEventTest, CreateDevice005, TestSize.Level1) +{ + auto device = CreateDeviceMock(0); + auto eventProperties = CreateEventPropertiesMock(0, 0, 1, 0, 0); + auto ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_LT(ret, 0); +} + +HWTEST_F(EmitEventTest, CreateDevice006, TestSize.Level1) +{ + auto device = CreateDeviceMock(0); + auto eventProperties = CreateEventPropertiesMock(0, 0, 0, 1, 0); + auto ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_LT(ret, 0); +} + +HWTEST_F(EmitEventTest, CreateDevice007, TestSize.Level1) +{ + auto device = CreateDeviceMock(0); + auto eventProperties = CreateEventPropertiesMock(0, 0, 0, 0, 1); + auto ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_LT(ret, 0); +} + +HWTEST_F(EmitEventTest, CreateDevice008, TestSize.Level1) +{ + const int16_t num = 200; + int16_t idx = deviceId < 0 ? 0 : 1; + Hid_EventProperties *eventProperties; + Hid_Device* device; + int32_t ret; + while (idx <= num) { + device = CreateDeviceMock(0); + eventProperties = CreateEventPropertiesMock(0, 0, 0, 0, 0); + ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + if (ret >= 0) { + idx++; + } + } + device = CreateDeviceMock(0); + eventProperties = CreateEventPropertiesMock(0, 0, 0, 0, 0); + ret = EmitEventTest::edmClient.CreateDevice(device, eventProperties); + ASSERT_LT(ret, 0); +} + +HWTEST_F(EmitEventTest, DestroyDevice001, TestSize.Level1) +{ + int32_t ret = edmClient.DestroyDevice(deviceId); ASSERT_EQ(ret, 0); } + +HWTEST_F(EmitEventTest, DestroyDevice002, TestSize.Level1) +{ + int32_t ret = edmClient.DestroyDevice(-1); + ASSERT_NE(ret, 0); +} + +HWTEST_F(EmitEventTest, DestroyDevice003, TestSize.Level1) +{ + const int16_t deviceId = 200; + int32_t ret = edmClient.DestroyDevice(deviceId); + ASSERT_NE(ret, 0); +} } // namespace ExternalDeviceManager } // namespace OHOS \ No newline at end of file diff --git a/utils/include/hilog_wrapper.h b/utils/include/hilog_wrapper.h index 2167ff6cbede678c3740da29d0177eb3b549b70d..0f45f3b493a3231b639cea70dc9c97bec62fbc24 100644 --- a/utils/include/hilog_wrapper.h +++ b/utils/include/hilog_wrapper.h @@ -54,6 +54,7 @@ enum UsbMgrSubModule { MODULE_COMMON, MODULE_USB_DDK, EDM_MODULE_TEST, + MODULE_HID_DDK, EDM_MODULE_BUTT, }; @@ -70,6 +71,7 @@ enum UsbMgrDomainId { EDM_COMMON_DOMAIN, EDM_USB_DDK_DOMAIN, EDM_TEST, + EDM_HID_DDK_DOMAIN, EDM_BUTT, }; @@ -94,6 +96,7 @@ constexpr OHOS::HiviewDFX::HiLogLabel EDM_MGR_LABEL[EDM_MODULE_BUTT] = { {LOG_CORE, EDM_COMMON_DOMAIN, "EdmCommon" }, {LOG_CORE, EDM_USB_DDK_DOMAIN, "EdmUsbDdk" }, {LOG_CORE, EDM_TEST, "EdmTest" }, + {LOG_CORE, EDM_HID_DDK_DOMAIN, "EdmHidDdk" }, }; // In order to improve performance, do not check the module range, module should less than EDM_MODULE_BUTT.