diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index 396d9de..e5cfcfc 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -40,8 +40,9 @@ namespace DistributedInput { const uint32_t KEY_DOWN_STATE = 1; const uint32_t READ_SLEEP_TIME_MS = 50; const uint32_t READ_RETRY_MAX = 5; - const uint32_t DHID_LENGTH_MAX = 256; - const uint32_t DEVID_LENGTH_MAX = 256; + const uint32_t DH_ID_LENGTH_MAX = 256; + const uint32_t DEV_ID_LENGTH_MAX = 256; + const uint32_t STRING_MAX_SIZE = 40 * 1024 * 1024; const uint32_t SCREEN_MSG_MAX = 40 * 1024 * 1024; const uint32_t AUTH_SESSION_SIDE_SERVER = 0; @@ -148,6 +149,36 @@ namespace DistributedInput { const std::string SINK_WINDOW_SHOW_Y = "sinkWinShowY"; + const std::string DEVICE_NAME = "name"; + + const std::string PHYSICAL_PATH = "physicalPath"; + + const std::string UNIQUE_ID = "uniqueId"; + + const std::string BUS = "bus"; + + const std::string VENDOR = "vendor"; + + const std::string PRODUCT = "product"; + + const std::string VERSION = "version"; + + const std::string DESCRIPTOR = "descriptor"; + + const std::string CLASSES = "classes"; + + const std::string EVENT_TYPES = "eventTypes"; + + const std::string EVENT_KEYS = "eventKeys"; + + const std::string ABS_TYPES = "absTypes"; + + const std::string ABS_INFOS = "absInfos"; + + const std::string REL_TYPES = "relTypes"; + + const std::string PROPERTIES = "properties"; + constexpr uint32_t SCREEN_ID_DEFAULT = 0; constexpr uint32_t DEFAULT_VALUE = 0; @@ -199,6 +230,12 @@ namespace DistributedInput { uint16_t version; std::string descriptor; uint32_t classes; + std::vector eventTypes; + std::vector eventKeys; + std::vector absTypes; + std::map> absInfos; + std::vector relTypes; + std::vector properties; }; /* diff --git a/common/include/input_check_param.cpp b/common/include/input_check_param.cpp index fcca90a..a9998ee 100644 --- a/common/include/input_check_param.cpp +++ b/common/include/input_check_param.cpp @@ -29,7 +29,7 @@ DInputCheckParam &DInputCheckParam::GetInstance(void) } bool DInputCheckParam::CheckParam(const std::string &deviceId, sptr callback) { - if (deviceId.empty() || deviceId.size() > DEVID_LENGTH_MAX) { + if (deviceId.empty() || deviceId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam deviceId is empty or deviceId size too long."); return false; } @@ -43,7 +43,7 @@ bool DInputCheckParam::CheckParam(const std::string &deviceId, sptr callback) { - if (deviceId.empty() || deviceId.size() > DEVID_LENGTH_MAX) { + if (deviceId.empty() || deviceId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam deviceId is empty or deviceId size too long."); return false; } @@ -63,11 +63,11 @@ bool DInputCheckParam::CheckParam(const std::string& deviceId, const uint32_t& i bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, sptr callback) { - if (srcId.empty() || srcId.size() > DEVID_LENGTH_MAX) { + if (srcId.empty() || srcId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam srcId is empty or srcId size too long."); return false; } - if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam sinkId is empty or sinkId size too long."); return false; } @@ -87,11 +87,11 @@ bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &s bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId, sptr callback) { - if (srcId.empty() || srcId.size() > DEVID_LENGTH_MAX) { + if (srcId.empty() || srcId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam srcId is empty or srcId size too long."); return false; } - if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam sinkId is empty or sinkId size too long."); return false; } @@ -105,7 +105,7 @@ bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &s bool DInputCheckParam::CheckParam(const std::string &sinkId, const std::vector &dhIds, sptr callback) { - if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam sinkId is empty or sinkId size too long."); return false; } @@ -114,7 +114,7 @@ bool DInputCheckParam::CheckParam(const std::string &sinkId, const std::vector DHID_LENGTH_MAX) { + if (iter.size() > DH_ID_LENGTH_MAX) { DHLOGE("CheckParam dhId size is too long."); return false; } @@ -129,11 +129,11 @@ bool DInputCheckParam::CheckParam(const std::string &sinkId, const std::vector &dhIds, sptr callback) { - if (srcId.empty() || srcId.size() > DEVID_LENGTH_MAX) { + if (srcId.empty() || srcId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam srcId is empty or srcId size too long."); return false; } - if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + if (sinkId.empty() || sinkId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam sinkId is empty or sinkId size too long."); return false; } @@ -142,7 +142,7 @@ bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &s return false; } for (auto iter : dhIds) { - if (iter.size() > DHID_LENGTH_MAX) { + if (iter.size() > DH_ID_LENGTH_MAX) { DHLOGE("CheckParam dhId size is too long."); return false; } @@ -157,11 +157,11 @@ bool DInputCheckParam::CheckParam(const std::string &srcId, const std::string &s bool DInputCheckParam::CheckRegisterParam(const std::string &devId, const std::string &dhId, const std::string ¶meters, const std::shared_ptr &callback) { - if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam devId is empty or devId size too long."); return false; } - if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam dhId is empty or dhId size too long."); return false; } @@ -179,11 +179,11 @@ bool DInputCheckParam::CheckRegisterParam(const std::string &devId, const std::s bool DInputCheckParam::CheckUnregisterParam(const std::string &devId, const std::string &dhId, const std::shared_ptr &callback) { - if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam devId is empty or devId size too long."); return false; } - if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam dhId is empty or dhId size too long."); return false; } diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index 672daef..d1ccada 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -17,17 +17,16 @@ #include #include -#include -#include -#include - #include #include +#include #include #include +#include #include #include #include +#include #include "anonymous_string.h" #include "distributed_hardware_log.h" @@ -102,6 +101,7 @@ int32_t InputHub::Release() ::close(iNotifyFd_); StopCollectInputEvents(); StopCollectInputHandler(); + sharedDHIds_.clear(); return DH_SUCCESS; } @@ -114,7 +114,6 @@ size_t InputHub::StartCollectInputEvents(RawEvent* buffer, size_t bufferSize) needToScanDevices_ = false; ScanInputDevices(DEVICE_PATH); } - { std::unique_lock deviceLock(devicesMutex_); while (!openingDevices_.empty()) { @@ -186,9 +185,11 @@ size_t InputHub::GetEvents(RawEvent* buffer, size_t bufferSize) if (!device) { continue; } - if (!device->isShare) { + if (!sharedDHIds_[device->identifier.descriptor]) { continue; } + DHLOGD("shared device dhId: %s, name: %s", GetAnonyString(device->identifier.descriptor).c_str(), + device->identifier.name.c_str()); if (eventItem.events & EPOLLIN) { event += CollectEvent(event, capacity, device, readBuffer, count); @@ -512,7 +513,7 @@ int32_t InputHub::QueryInputDeviceInfo(int fd, InputDevice& identifier) buffer[sizeof(buffer) - 1] = '\0'; identifier.name = buffer; } - + DHLOGD("QueryInputDeviceInfo deviceName: %s", buffer); // If the device is already a virtual device, don't monitor it. if (identifier.name.find(VIRTUAL_DEVICE_NAME) != std::string::npos) { return ERR_DH_INPUT_HUB_IS_VIRTUAL_DEVICE; @@ -550,9 +551,127 @@ int32_t InputHub::QueryInputDeviceInfo(int fd, InputDevice& identifier) identifier.uniqueId = buffer; } + QueryEventInfo(fd, identifier); return DH_SUCCESS; } +void InputHub::QueryEventInfo(int fd, InputDevice& identifier) +{ + DHLOGI("QueryEventInfo: devName: %s, dhId: %s!", identifier.name.c_str(), + GetAnonyString(identifier.descriptor).c_str()); + struct libevdev *dev = GetLibEvDev(fd); + if (dev == nullptr) { + DHLOGE("dev is nullptr"); + return; + } + GetEventTypes(dev, identifier); + GetEventKeys(dev, identifier); + GetABSInfo(dev, identifier); + GetRELTypes(dev, identifier); + GetProperties(dev, identifier); + libevdev_free(dev); +} + +struct libevdev* InputHub::GetLibEvDev(int fd) +{ + struct libevdev *dev = nullptr; + int rc = 1; + rc = libevdev_new_from_fd(fd, &dev); + if (rc < 0) { + DHLOGE("Failed to init libevdev (%s)", strerror(-rc)); + return nullptr; + } + return dev; +} + +void InputHub::GetEventTypes(struct libevdev* dev, InputDevice& identifier) +{ + for (uint32_t eventType = 0; eventType < EV_CNT; eventType++) { + if (!libevdev_has_event_type(dev, eventType)) { + DHLOGD("The device is not support eventType: %d", eventType); + continue; + } + DHLOGI("QueryInputDeviceInfo eventType: %d", eventType); + identifier.eventTypes.push_back(eventType); + } +} + +int32_t InputHub::GetEventKeys(struct libevdev* dev, InputDevice& identifier) +{ + if (!libevdev_has_event_type(dev, EV_KEY)) { + DHLOGE("The device doesn't has EV_KEY type!"); + return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; + } + for (uint32_t eventKey = 0; eventKey < KEY_CNT; eventKey++) { + if (!libevdev_has_event_code(dev, EV_KEY, eventKey)) { + DHLOGD("The device is not support eventKey: %d", eventKey); + continue; + } + DHLOGI("QueryInputDeviceInfo eventKey: %d", eventKey); + identifier.eventKeys.push_back(eventKey); + } + return DH_SUCCESS; +} + +int32_t InputHub::GetABSInfo(struct libevdev* dev, InputDevice& identifier) +{ + if (!libevdev_has_event_type(dev, EV_ABS)) { + DHLOGE("The device doesn't has EV_ABS type!"); + return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; + } + DHLOGI("The device has abs info, devName: %s, dhId: %s!", identifier.name.c_str(), + GetAnonyString(identifier.descriptor).c_str()); + for (uint32_t absType = 0; absType < ABS_CNT; absType++) { + if (!libevdev_has_event_code(dev, EV_ABS, absType)) { + DHLOGD("The device is not support absType: %d", absType); + continue; + } + DHLOGI("QueryInputDeviceInfo abs type: %d", absType); + identifier.absTypes.push_back(absType); + const struct input_absinfo *abs = libevdev_get_abs_info(dev, absType); + if (abs == nullptr) { + DHLOGE("absInfo is nullptr!"); + continue; + } + DHLOGI("QueryInputDeviceInfo abs info value: %d, min: %d, max: %d, fuzz: %d, flat: %d, res: %d", abs->value, + abs->minimum, abs->maximum, abs->fuzz, abs->flat, abs->resolution); + identifier.absInfos[absType].push_back(abs->value); + identifier.absInfos[absType].push_back(abs->minimum); + identifier.absInfos[absType].push_back(abs->maximum); + identifier.absInfos[absType].push_back(abs->fuzz); + identifier.absInfos[absType].push_back(abs->flat); + identifier.absInfos[absType].push_back(abs->resolution); + } + return DH_SUCCESS; +} + +int32_t InputHub::GetRELTypes(struct libevdev* dev, InputDevice& identifier) +{ + if (!libevdev_has_event_type(dev, EV_REL)) { + DHLOGE("The device doesn't has EV_REL type!"); + return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; + } + for (uint32_t code = 0; code < REL_CNT; code++) { + if (!libevdev_has_event_code(dev, EV_REL, code)) { + DHLOGD("The device is not support eventCode: %d", code); + continue; + } + DHLOGI("QueryInputDeviceInfo rel types: %d", code); + identifier.relTypes.push_back(code); + } + return DH_SUCCESS; +} + +void InputHub::GetProperties(struct libevdev* dev, InputDevice& identifier) +{ + for (uint32_t prop = 0; prop < INPUT_PROP_CNT; prop++) { + if (libevdev_has_property(dev, prop)) { + DHLOGI("QueryInputDeviceInfo rel prop: %d", prop); + identifier.properties.push_back(prop); + } + } +} + int32_t InputHub::MakeDevice(int fd, std::unique_ptr device) { // Figure out the kinds of events the device reports. @@ -567,8 +686,8 @@ int32_t InputHub::MakeDevice(int fd, std::unique_ptr device) QueryLocalTouchScreenInfo(fd); device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT; } else if (TestBit(BTN_TOUCH, device->keyBitmask) && - TestBit(ABS_X, device->absBitmask) && - TestBit(ABS_Y, device->absBitmask)) { + TestBit(ABS_X, device->absBitmask) && + TestBit(ABS_Y, device->absBitmask)) { QueryLocalTouchScreenInfo(fd); device->classes |= INPUT_DEVICE_CLASS_TOUCH; } @@ -601,12 +720,10 @@ int32_t InputHub::MakeDevice(int fd, std::unique_ptr device) } device->identifier.classes = device->classes; - if (device->classes & inputTypes_) { - device->isShare = true; - } + DHLOGI("inputType=%d", inputTypes_.load()); - DHLOGI("New device: fd=%d, name='%s', classes=0x%x, isShare=%d", - fd, device->identifier.name.c_str(), device->classes, device->isShare); + DHLOGI("New device: fd=%d, name='%s', classes=0x%x, isShare=%d", fd, device->identifier.name.c_str(), + device->classes); AddDeviceLocked(std::move(device)); return DH_SUCCESS; @@ -882,9 +999,12 @@ InputHub::Device* InputHub::GetDeviceByFdLocked(int fd) InputHub::Device* InputHub::GetSupportDeviceByFd(int fd) { + DHLOGI("GetSupportDeviceByFd fd: %d", fd); std::unique_lock deviceLock(devicesMutex_); for (const auto& [id, device] : devices_) { - if (device->fd == fd) { + DHLOGI("GetSupportDeviceByFd device: %d, fd: %d, path: %s, dhId: %s, classes=0x%x", id, device->fd, + device->path.c_str(), GetAnonyString(device->identifier.descriptor).c_str(), device->classes); + if (device != nullptr && device->fd == fd) { return device.get(); } } @@ -952,8 +1072,14 @@ AffectDhIds InputHub::SetSharingDevices(bool enabled, std::vector d { AffectDhIds affDhIds; std::unique_lock deviceLock(devicesMutex_); + DHLOGI("SetSharingDevices start"); for (auto dhId : dhIds) { + DHLOGI("SetSharingDevices dhId: %s, size: %d, enabled: %d", GetAnonyString(dhId).c_str(), devices_.size(), + enabled); + sharedDHIds_[dhId] = enabled; for (const auto &[id, device] : devices_) { + DHLOGI("deviceName %s ,dhId: %s ", device->identifier.name.c_str(), + GetAnonyString(device->identifier.descriptor).c_str()); if (device->identifier.descriptor == dhId) { device->isShare = enabled; DHLOGW("dhid:%s, isshare:%d", GetAnonyString(device->identifier.descriptor).c_str(), enabled); @@ -962,7 +1088,7 @@ AffectDhIds InputHub::SetSharingDevices(bool enabled, std::vector d } } } - + DHLOGI("SetSharingDevices end"); return affDhIds; } @@ -1023,8 +1149,9 @@ void InputHub::GetDevicesInfoByDhId(std::vector dhidsVec, std::map< bool InputHub::IsAllDevicesStoped() { std::unique_lock deviceLock(devicesMutex_); - for (const auto &[id, device] : devices_) { - if (device->isShare) { + for (const auto &[dhId, isShared] : sharedDHIds_) { + DHLOGI("the dhId: %s, isShared: %d", GetAnonyString(dhId).c_str(), isShared); + if (isShared) { return false; } } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index f8849b2..306253e 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -21,11 +21,11 @@ #include #include +#include +#include #include #include -#include - #include "constants_dinput.h" namespace OHOS { @@ -95,7 +95,14 @@ private: int32_t RefreshEpollItem(bool isSleep); int32_t OpenInputDeviceLocked(const std::string& devicePath); - int32_t QueryInputDeviceInfo(int fd, InputDevice& identifier); + int32_t QueryInputDeviceInfo(int32_t fd, InputDevice& identifier); + void QueryEventInfo(int32_t fd, InputDevice& identifier); + struct libevdev* GetLibEvDev(int32_t fd); + void GetEventTypes(struct libevdev* dev, InputDevice& identifier); + int32_t GetEventKeys(struct libevdev* dev, InputDevice& identifier); + int32_t GetABSInfo(struct libevdev* dev, InputDevice& identifier); + int32_t GetRELTypes(struct libevdev* dev, InputDevice& identifier); + void GetProperties(struct libevdev* dev, InputDevice& identifier); int32_t MakeDevice(int fd, std::unique_ptr device); void GenerateDescriptor(InputDevice& identifier) const; std::string StringPrintf(const char* format, ...) const; @@ -164,6 +171,7 @@ private: std::atomic inputTypes_; std::atomic isStartCollectEvent_; std::atomic isStartCollectHandler_; + std::unordered_map sharedDHIds_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/inputdevicehandler/BUILD.gn b/inputdevicehandler/BUILD.gn index a828abf..a6e789f 100755 --- a/inputdevicehandler/BUILD.gn +++ b/inputdevicehandler/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("libdinput_handler") { "${frameworks_path}/include", "${utils_path}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", @@ -51,6 +52,7 @@ ohos_shared_library("libdinput_handler") { "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", "//third_party/openssl:libcrypto_static", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/inputdevicehandler/src/distributed_input_handler.cpp b/inputdevicehandler/src/distributed_input_handler.cpp index b0294bf..d549aa1 100644 --- a/inputdevicehandler/src/distributed_input_handler.cpp +++ b/inputdevicehandler/src/distributed_input_handler.cpp @@ -57,15 +57,21 @@ void DistributedInputHandler::StructTransJson(const InputDevice& pBuf, std::stri DHLOGI("[%s] %d, %d, %d, %d, %s.\n", (pBuf.name).c_str(), pBuf.bus, pBuf.vendor, pBuf.product, pBuf.version, GetAnonyString(pBuf.descriptor).c_str()); nlohmann::json tmpJson; - tmpJson["name"] = pBuf.name; - tmpJson["physicalPath"] = pBuf.physicalPath; - tmpJson["uniqueId"] = pBuf.uniqueId; - tmpJson["bus"] = pBuf.bus; - tmpJson["vendor"] = pBuf.vendor; - tmpJson["product"] = pBuf.product; - tmpJson["version"] = pBuf.version; - tmpJson["descriptor"] = pBuf.descriptor; - tmpJson["classes"] = pBuf.classes; + tmpJson[DEVICE_NAME] = pBuf.name; + tmpJson[PHYSICAL_PATH] = pBuf.physicalPath; + tmpJson[UNIQUE_ID] = pBuf.uniqueId; + tmpJson[BUS] = pBuf.bus; + tmpJson[VENDOR] = pBuf.vendor; + tmpJson[PRODUCT] = pBuf.product; + tmpJson[VERSION] = pBuf.version; + tmpJson[DESCRIPTOR] = pBuf.descriptor; + tmpJson[CLASSES] = pBuf.classes; + tmpJson[EVENT_TYPES] = pBuf.eventTypes; + tmpJson[EVENT_KEYS] = pBuf.eventKeys; + tmpJson[ABS_TYPES] = pBuf.absTypes; + tmpJson[ABS_INFOS] = pBuf.absInfos; + tmpJson[REL_TYPES] = pBuf.relTypes; + tmpJson[PROPERTIES] = pBuf.properties; std::ostringstream stream; stream << tmpJson.dump(); diff --git a/inputdevicehandler/test/inputhandlertest/BUILD.gn b/inputdevicehandler/test/inputhandlertest/BUILD.gn index cb9955d..466ceee 100644 --- a/inputdevicehandler/test/inputhandlertest/BUILD.gn +++ b/inputdevicehandler/test/inputhandlertest/BUILD.gn @@ -38,6 +38,7 @@ ohos_unittest("distributed_input_handler_test") { "${frameworks_path}/include", "${utils_path}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", @@ -70,6 +71,7 @@ ohos_unittest("distributed_input_handler_test") { "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", "//third_party/openssl:libcrypto_static", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index 95d46c3..4358fe0 100644 --- a/interfaces/inner_kits/BUILD.gn +++ b/interfaces/inner_kits/BUILD.gn @@ -36,6 +36,7 @@ config("input_sdk_public_config") { "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", "//foundation/communication/dsoftbus/interfaces/kits/bus_center", "//foundation/communication/dsoftbus/interfaces/kits/common", + "//third_party/libevdev/libevdev/" ] } ohos_shared_library("libdinput_sdk") { @@ -85,6 +86,7 @@ ohos_shared_library("libdinput_sdk") { "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", + "//third_party/libevdev:libevdev", ] defines = [ diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index fcf9871..11949e9 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -551,7 +551,7 @@ int32_t DistributedInputClient::StopRemoteInput(const std::string &srcId, const bool DistributedInputClient::IsNeedFilterOut(const std::string& deviceId, const BusinessEvent& event) { DHLOGI("IsNeedFilterOut called, deviceId: %s", GetAnonyString(deviceId).c_str()); - if (deviceId.empty() || (deviceId.size() > DEVID_LENGTH_MAX)) { + if (deviceId.empty() || (deviceId.size() > DEV_ID_LENGTH_MAX)) { DHLOGE("IsNeedFilterOut param deviceId is empty."); return false; } @@ -574,7 +574,7 @@ bool DistributedInputClient::IsTouchEventNeedFilterOut(const TouchScreenEvent &e bool DistributedInputClient::IsStartDistributedInput(const std::string& dhId) { std::lock_guard lock(sharingDhIdsMtx_); - if (dhId.empty() || (dhId.size() > DHID_LENGTH_MAX)) { + if (dhId.empty() || (dhId.size() > DH_ID_LENGTH_MAX)) { DHLOGE("IsStartDistributedInput param dhid is error."); return false; } diff --git a/interfaces/ipc/test/clientunittest/BUILD.gn b/interfaces/ipc/test/clientunittest/BUILD.gn index 49d6eca..504c75f 100644 --- a/interfaces/ipc/test/clientunittest/BUILD.gn +++ b/interfaces/ipc/test/clientunittest/BUILD.gn @@ -42,6 +42,7 @@ ohos_unittest("distributed_input_client_test") { "${utils_path}/include", "//commonlibrary/c_utils/base/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "//utils/system/safwk/native/include", "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", "//foundation/communication/dsoftbus/interfaces/kits/bus_center", @@ -128,6 +129,7 @@ ohos_unittest("distributed_input_client_test") { "//foundation/distributedhardware/distributed_input/services/sink/sinkmanager:libdinput_sink", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/window/window_manager/dm:libdm", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/sink/inputcollector/BUILD.gn b/services/sink/inputcollector/BUILD.gn index 0c82693..9af2f05 100755 --- a/services/sink/inputcollector/BUILD.gn +++ b/services/sink/inputcollector/BUILD.gn @@ -28,6 +28,7 @@ ohos_shared_library("libdinput_collector") { "${fwk_utils_path}/include", "${utils_path}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", @@ -51,6 +52,7 @@ ohos_shared_library("libdinput_collector") { "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//third_party/openssl:libcrypto_static", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 3d97076..b740aa0 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -95,6 +95,7 @@ void *DistributedInputCollector::CollectEventsThread(void *param) void DistributedInputCollector::StartCollectEventsThread() { + DHLOGI("StartCollectEventsThread!"); while (isCollectingEvents_) { memset_s(&mEventBuffer, sizeof(mEventBuffer), 0, sizeof(mEventBuffer)); if (inputHub_ == nullptr) { diff --git a/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn b/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn index 6b9829b..e362355 100644 --- a/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn +++ b/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn @@ -38,6 +38,7 @@ ohos_unittest("distributed_input_inner_sinkcollector_test") { "${fwk_utils_path}/include", "${utils_path}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", @@ -58,7 +59,7 @@ ohos_unittest("distributed_input_inner_sinkcollector_test") { "-Dprivate=public", "-Dprotected=public", ] - + defines = [ "HI_LOG_ENABLE", "DH_LOG_TAG=\"distributedinpututtest\"", @@ -67,6 +68,7 @@ ohos_unittest("distributed_input_inner_sinkcollector_test") { deps = [ "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_static", "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", diff --git a/services/sink/sinkmanager/BUILD.gn b/services/sink/sinkmanager/BUILD.gn index 69517c1..2670c74 100644 --- a/services/sink/sinkmanager/BUILD.gn +++ b/services/sink/sinkmanager/BUILD.gn @@ -48,6 +48,7 @@ ohos_shared_library("libdinput_sink") { "//drivers/peripheral/display/interfaces/include", "//drivers/peripheral/base", "//foundation/graphic/graphic_2d/utils/buffer_handle/export", + "//third_party/libevdev/libevdev/" ] sources = [ @@ -73,6 +74,7 @@ ohos_shared_library("libdinput_sink") { "//foundation/distributedhardware/distributed_input/services/sink/transport:libdinput_sink_trans", "${fwk_interfaces_path}:libdhfwk_sdk", "//foundation/window/window_manager/dm:libdm", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 06cd429..78f3d46 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -848,32 +848,32 @@ int32_t DistributedInputSinkManager::ProjectWindowListener::ParseMessage(const s return ERR_DH_INPUT_JSON_PARSE_FAIL; } srcDeviceId = jsonObj[SOURCE_DEVICE_ID].get(); - if (!IsUint64(jsonObj, SOURCE_WINDOW_ID)) { + if (!IsUInt64(jsonObj, SOURCE_WINDOW_ID)) { DHLOGE("sourceWinId key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } srcWinId = jsonObj[SOURCE_WINDOW_ID].get(); - if (!IsUint64(jsonObj, SINK_SHOW_WINDOW_ID)) { + if (!IsUInt64(jsonObj, SINK_SHOW_WINDOW_ID)) { DHLOGE("sinkWinId key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } sinkScreenInfo.sinkShowWinId = jsonObj[SINK_SHOW_WINDOW_ID].get(); - if (!IsUint32(jsonObj, SINK_PROJECT_SHOW_WIDTH)) { + if (!IsUInt32(jsonObj, SINK_PROJECT_SHOW_WIDTH)) { DHLOGE("sourceWinHeight key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } sinkScreenInfo.sinkProjShowWidth = jsonObj[SINK_PROJECT_SHOW_WIDTH].get(); - if (!IsUint32(jsonObj, SINK_PROJECT_SHOW_HEIGHT)) { + if (!IsUInt32(jsonObj, SINK_PROJECT_SHOW_HEIGHT)) { DHLOGE("sourceWinHeight key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } sinkScreenInfo.sinkProjShowHeight = jsonObj[SINK_PROJECT_SHOW_HEIGHT].get(); - if (!IsUint32(jsonObj, SINK_WINDOW_SHOW_X)) { + if (!IsUInt32(jsonObj, SINK_WINDOW_SHOW_X)) { DHLOGE("sourceWinHeight key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } sinkScreenInfo.sinkWinShowX = jsonObj[SINK_WINDOW_SHOW_X].get(); - if (!IsUint32(jsonObj, SINK_WINDOW_SHOW_Y)) { + if (!IsUInt32(jsonObj, SINK_WINDOW_SHOW_Y)) { DHLOGE("sourceWinHeight key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } diff --git a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn index bebc64c..b2ff2dc 100755 --- a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn +++ b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("distributed_input_sinkmanager_test") { "${fwk_utils_path}/include", "${service_common}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "//foundation/communication/dsoftbus/interfaces/kits/transport", "${services_sink_path}/inputcollector/include", "${dfx_utils_path}/include", @@ -95,6 +96,7 @@ ohos_unittest("distributed_input_sinkmanager_test") { "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_static", "${fwk_interfaces_path}:libdhfwk_sdk", "//foundation/window/window_manager/dm:libdm", diff --git a/services/sink/transport/BUILD.gn b/services/sink/transport/BUILD.gn index a847b29..c4ee990 100644 --- a/services/sink/transport/BUILD.gn +++ b/services/sink/transport/BUILD.gn @@ -37,6 +37,7 @@ ohos_shared_library("libdinput_sink_trans") { "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", + "//third_party/libevdev/libevdev/" ] sources = [ @@ -58,6 +59,7 @@ ohos_shared_library("libdinput_sink_trans") { "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/communication/dsoftbus/sdk:softbus_client", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/sink/transport/src/distributed_input_sink_transport.cpp b/services/sink/transport/src/distributed_input_sink_transport.cpp index 26a0aa7..bb9992f 100644 --- a/services/sink/transport/src/distributed_input_sink_transport.cpp +++ b/services/sink/transport/src/distributed_input_sink_transport.cpp @@ -87,6 +87,7 @@ static void StreamReceived(int32_t sessionId, const StreamData *data, const Stre (void)param; DHLOGI("sessionId: %d", sessionId); } + DistributedInputSinkTransport &DistributedInputSinkTransport::GetInstance() { static DistributedInputSinkTransport instance; diff --git a/services/sink/transport/test/sinktransunittest/BUILD.gn b/services/sink/transport/test/sinktransunittest/BUILD.gn index 098a53c..0beb294 100755 --- a/services/sink/transport/test/sinktransunittest/BUILD.gn +++ b/services/sink/transport/test/sinktransunittest/BUILD.gn @@ -39,6 +39,7 @@ ohos_unittest("distributed_input_sinktrans_test") { "${fwk_utils_path}/include", "${service_common}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${dfx_utils_path}/include", "${utils_path}/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", @@ -94,6 +95,7 @@ ohos_unittest("distributed_input_sinktrans_test") { "${services_sink_path}/sinkmanager:libdinput_sink", "${services_sink_path}/inputcollector:libdinput_collector", "//foundation/window/window_manager/dm:libdm", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn index 62c77a1..8872180 100644 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -36,6 +36,7 @@ ohos_shared_library("libdinput_inject") { "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", + "//third_party/libevdev/libevdev/" ] sources = [ @@ -43,10 +44,6 @@ ohos_shared_library("libdinput_inject") { "src/distributed_input_inject.cpp", "src/distributed_input_node_manager.cpp", "src/virtual_device.cpp", - "src/virtual_keyboard.cpp", - "src/virtual_mouse.cpp", - "src/virtual_touchpad.cpp", - "src/virtual_touchscreen.cpp", ] defines = [ @@ -60,6 +57,7 @@ ohos_shared_library("libdinput_inject") { "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", "//third_party/openssl:libcrypto_static", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 5644541..83e93b1 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -24,6 +24,8 @@ #include #include +#include "nlohmann/json.hpp" + #include "constants_dinput.h" #include "input_hub.h" #include "virtual_device.h" @@ -56,8 +58,9 @@ public: private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); - int32_t CreateHandle(InputDevice event, const std::string& devId, const std::string& dhId); - void stringTransJsonTransStruct(const std::string& str, InputDevice& pBuf); + int32_t CreateHandle(const InputDevice& inputDevice, const std::string& devId, const std::string& dhId); + void ParseInputDeviceJson(const std::string& str, InputDevice& pBuf); + void VerifyInputDevice(const nlohmann::json& inputDeviceJson, InputDevice& pBuf); void InjectEvent(); /* the key is dhId, and the value is virtualDevice */ @@ -71,6 +74,7 @@ private: std::queue> injectQueue_; std::unique_ptr inputHub_; int32_t virtualTouchScreenFd_; + std::once_flag callOnceFlag_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/source/inputinject/include/virtual_device.h b/services/source/inputinject/include/virtual_device.h index a7bc086..6bbe883 100644 --- a/services/source/inputinject/include/virtual_device.h +++ b/services/source/inputinject/include/virtual_device.h @@ -37,9 +37,10 @@ public: explicit VirtualDevice(const InputDevice& event); virtual ~VirtualDevice(); bool DoIoctl(int32_t fd, int32_t request, const uint32_t value); - bool CreateKey(); + bool CreateKey(const InputDevice& inputDevice); + void SetABSInfo(struct uinput_user_dev& inputUserDev, const InputDevice& inputDevice); bool SetPhys(const std::string deviceName, std::string dhId); - bool SetUp(const std::string &devId, const std::string &dhId); + bool SetUp(const InputDevice& inputDevice, const std::string &devId, const std::string &dhId); bool InjectInputEvent(const input_event &event); void SetNetWorkId(const std::string netWorkId); std::string GetNetWorkId(); @@ -47,14 +48,7 @@ public: int32_t GetDeviceFd(); uint16_t GetDeviceType(); -protected: - VirtualDevice(); - virtual const std::vector& GetEventTypes() const = 0; - virtual const std::vector& GetKeys() const = 0; - virtual const std::vector& GetProperties() const = 0; - virtual const std::vector& GetAbs() const = 0; - virtual const std::vector& GetRelBits() const = 0; - +private: int32_t fd_ = -1; std::string deviceName_; std::string netWorkId_; @@ -64,8 +58,6 @@ protected: const uint16_t version_; const uint16_t classes_; struct uinput_user_dev dev_ {}; - struct uinput_abs_setup absTemp_ = {}; - std::vector absInit_; const std::string pid_ = std::to_string(getpid()); private: diff --git a/services/source/inputinject/include/virtual_keyboard.h b/services/source/inputinject/include/virtual_keyboard.h deleted file mode 100644 index a2bcf5a..0000000 --- a/services/source/inputinject/include/virtual_keyboard.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2021-2022 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 OHOS_VIRTUAL_KEYBOARD_H -#define OHOS_VIRTUAL_KEYBOARD_H - -#include "virtual_device.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -class VirtualKeyboard : public VirtualDevice { -public: - explicit VirtualKeyboard(const InputDevice& event); - ~VirtualKeyboard() override; -protected: - const std::vector& GetEventTypes() const override; - const std::vector& GetKeys() const override; - const std::vector& GetProperties() const override; - const std::vector& GetAbs() const override; - const std::vector& GetRelBits() const override; -}; -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - -#endif // OHOS_VIRTUAL_KEYBOARD_H diff --git a/services/source/inputinject/include/virtual_mouse.h b/services/source/inputinject/include/virtual_mouse.h deleted file mode 100644 index 9f974da..0000000 --- a/services/source/inputinject/include/virtual_mouse.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2021-2022 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 OHOS_VIRTUAL_MOUSE_H -#define OHOS_VIRTUAL_MOUSE_H - -#include "virtual_device.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -class VirtualMouse : public VirtualDevice { -public: - explicit VirtualMouse(const InputDevice& event); - ~VirtualMouse() override; -protected: - const std::vector& GetEventTypes() const override; - const std::vector& GetKeys() const override; - const std::vector& GetRelBits() const override; - const std::vector& GetProperties() const override; - const std::vector& GetAbs() const override; -}; -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - -#endif // VIRTUALKEYBOARD_H diff --git a/services/source/inputinject/include/virtual_touchpad.h b/services/source/inputinject/include/virtual_touchpad.h deleted file mode 100644 index 11635e0..0000000 --- a/services/source/inputinject/include/virtual_touchpad.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2021-2022 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 OHOS_VIRTUAL_TOUCHPAD_H -#define OHOS_VIRTUAL_TOUCHPAD_H - -#include "virtual_device.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -class VirtualTouchpad : public VirtualDevice { -public: - explicit VirtualTouchpad(const InputDevice& event); - ~VirtualTouchpad() override; -protected: - const std::vector& GetEventTypes() const override; - const std::vector& GetKeys() const override; - const std::vector& GetAbs() const override; - const std::vector& GetProperties() const override; - const std::vector& GetRelBits() const override; -}; -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - -#endif // OHOS_VIRTUAL_TOUCHPAD_H diff --git a/services/source/inputinject/include/virtual_touchscreen.h b/services/source/inputinject/include/virtual_touchscreen.h deleted file mode 100644 index ba7645d..0000000 --- a/services/source/inputinject/include/virtual_touchscreen.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2022 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 OHOS_VIRTUAL_TOUCHSCREEN_H -#define OHOS_VIRTUAL_TOUCHSCREEN_H - -#include "dinput_context.h" -#include "virtual_device.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -class VirtualTouchScreen : public VirtualDevice { -public: - VirtualTouchScreen(const InputDevice& event, LocalAbsInfo& absInfo, uint32_t phyWidth, uint32_t phyHeight); - ~VirtualTouchScreen() override; -protected: - const std::vector& GetEventTypes() const override; - const std::vector& GetKeys() const override; - const std::vector& GetAbs() const override; - const std::vector& GetProperties() const override; - const std::vector& GetRelBits() const override; -}; -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS -#endif diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 3ba943d..9d9a055 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -131,15 +131,21 @@ int32_t DistributedInputInject::StructTransJson(const InputDevice &pBuf, std::st DHLOGI("[%s] %d, %d, %d, %d, %s.\n", (pBuf.name).c_str(), pBuf.bus, pBuf.vendor, pBuf.product, pBuf.version, GetAnonyString(pBuf.descriptor).c_str()); nlohmann::json tmpJson; - tmpJson["name"] = pBuf.name; - tmpJson["physicalPath"] = pBuf.physicalPath; - tmpJson["uniqueId"] = pBuf.uniqueId; - tmpJson["bus"] = pBuf.bus; - tmpJson["vendor"] = pBuf.vendor; - tmpJson["product"] = pBuf.product; - tmpJson["version"] = pBuf.version; - tmpJson["descriptor"] = pBuf.descriptor; - tmpJson["classes"] = pBuf.classes; + tmpJson[DEVICE_NAME] = pBuf.name; + tmpJson[PHYSICAL_PATH] = pBuf.physicalPath; + tmpJson[UNIQUE_ID] = pBuf.uniqueId; + tmpJson[BUS] = pBuf.bus; + tmpJson[VENDOR] = pBuf.vendor; + tmpJson[PRODUCT] = pBuf.product; + tmpJson[VERSION] = pBuf.version; + tmpJson[DESCRIPTOR] = pBuf.descriptor; + tmpJson[CLASSES] = pBuf.classes; + tmpJson[EVENT_TYPES] = pBuf.eventTypes; + tmpJson[EVENT_KEYS] = pBuf.eventKeys; + tmpJson[ABS_TYPES] = pBuf.absTypes; + tmpJson[ABS_INFOS] = pBuf.absInfos; + tmpJson[REL_TYPES] = pBuf.relTypes; + tmpJson[PROPERTIES] = pBuf.properties; std::ostringstream stream; stream << tmpJson.dump(); diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 48941e1..744c561 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -22,18 +22,14 @@ #include #include "anonymous_string.h" +#include "dinput_utils_tool.h" #include "distributed_hardware_log.h" -#include "nlohmann/json.hpp" #include "softbus_bus_center.h" #include "dinput_context.h" #include "dinput_errcode.h" #include "dinput_softbus_define.h" -#include "virtual_keyboard.h" -#include "virtual_mouse.h" -#include "virtual_touchpad.h" -#include "virtual_touchscreen.h" namespace OHOS { namespace DistributedHardware { @@ -60,64 +56,94 @@ DistributedInputNodeManager::~DistributedInputNodeManager() int32_t DistributedInputNodeManager::openDevicesNode(const std::string& devId, const std::string& dhId, const std::string& parameters) { + if (devId.size() > DEV_ID_LENGTH_MAX || devId.empty() || dhId.size() > DH_ID_LENGTH_MAX || dhId.empty() || + parameters.size() > STRING_MAX_SIZE || parameters.empty()) { + DHLOGE("Params is invalid!"); + return ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL; + } InputDevice event; - stringTransJsonTransStruct(parameters, event); + ParseInputDeviceJson(parameters, event); if (CreateHandle(event, devId, dhId) < 0) { + DHLOGE("Can not create virtual node!"); return ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL; } return DH_SUCCESS; } -void DistributedInputNodeManager::stringTransJsonTransStruct(const std::string& str, InputDevice& pBuf) +void DistributedInputNodeManager::ParseInputDeviceJson(const std::string& str, InputDevice& pBuf) { - nlohmann::json recMsg = nlohmann::json::parse(str, nullptr, false); - if (recMsg.is_discarded()) { + nlohmann::json inputDeviceJson = nlohmann::json::parse(str, nullptr, false); + if (inputDeviceJson.is_discarded()) { DHLOGE("recMsg parse failed!"); return; } - recMsg.at("name").get_to(pBuf.name); - recMsg.at("physicalPath").get_to(pBuf.physicalPath); - recMsg.at("uniqueId").get_to(pBuf.uniqueId); - recMsg.at("bus").get_to(pBuf.bus); - recMsg.at("vendor").get_to(pBuf.vendor); - recMsg.at("product").get_to(pBuf.product); - recMsg.at("version").get_to(pBuf.version); - recMsg.at("descriptor").get_to(pBuf.descriptor); - recMsg.at("classes").get_to(pBuf.classes); + VerifyInputDevice(inputDeviceJson, pBuf); } -int32_t DistributedInputNodeManager::CreateHandle(InputDevice event, const std::string& devId, const std::string& dhId) +void DistributedInputNodeManager::VerifyInputDevice(const nlohmann::json& inputDeviceJson, InputDevice& pBuf) +{ + if (IsString(inputDeviceJson, DEVICE_NAME)) { + pBuf.name = inputDeviceJson[DEVICE_NAME].get(); + } + if (IsString(inputDeviceJson, PHYSICAL_PATH)) { + pBuf.physicalPath = inputDeviceJson[PHYSICAL_PATH].get(); + } + if (IsString(inputDeviceJson, UNIQUE_ID)) { + pBuf.uniqueId = inputDeviceJson[UNIQUE_ID].get(); + } + if (IsUInt16(inputDeviceJson, BUS)) { + pBuf.bus = inputDeviceJson[BUS].get(); + } + if (IsUInt16(inputDeviceJson, VENDOR)) { + pBuf.vendor = inputDeviceJson[VENDOR].get(); + } + if (IsUInt16(inputDeviceJson, PRODUCT)) { + pBuf.product = inputDeviceJson[PRODUCT].get(); + } + if (IsUInt16(inputDeviceJson, VERSION)) { + pBuf.version = inputDeviceJson[VERSION].get(); + } + if (IsString(inputDeviceJson, DESCRIPTOR)) { + pBuf.descriptor = inputDeviceJson[DESCRIPTOR].get(); + } + if (IsUInt32(inputDeviceJson, CLASSES)) { + pBuf.classes = inputDeviceJson[CLASSES].get(); + } + if (IsArray(inputDeviceJson, EVENT_TYPES)) { + pBuf.eventTypes = inputDeviceJson[EVENT_TYPES].get>(); + } + if (IsArray(inputDeviceJson, EVENT_KEYS)) { + pBuf.eventKeys = inputDeviceJson[EVENT_KEYS].get>(); + } + if (IsArray(inputDeviceJson, ABS_TYPES)) { + pBuf.absTypes = inputDeviceJson[ABS_TYPES].get>(); + } + if (IsArray(inputDeviceJson, ABS_INFOS)) { + pBuf.absInfos = inputDeviceJson[ABS_INFOS].get>>(); + } + if (IsArray(inputDeviceJson, REL_TYPES)) { + pBuf.relTypes = inputDeviceJson[REL_TYPES].get>(); + } + if (IsArray(inputDeviceJson, PROPERTIES)) { + pBuf.properties = inputDeviceJson[PROPERTIES].get>(); + } +} + +int32_t DistributedInputNodeManager::CreateHandle(const InputDevice& inputDevice, const std::string& devId, + const std::string& dhId) { std::unique_lock my_lock(operationMutex_); - std::unique_ptr device; - if (event.classes & INPUT_DEVICE_CLASS_KEYBOARD) { - device = std::make_unique(event); - } else if (event.classes & INPUT_DEVICE_CLASS_CURSOR) { - device = std::make_unique(event); - } else if (event.classes & INPUT_DEVICE_CLASS_TOUCH_MT) { - inputHub_->ScanInputDevices(DEVICE_PATH); - LocalAbsInfo info = DInputContext::GetInstance().GetLocalTouchScreenInfo().localAbsInfo; - device = std::make_unique(event, info, info.absMtPositionXMax, info.absMtPositionYMax); - } else if (event.classes & INPUT_DEVICE_CLASS_TOUCH) { - device = std::make_unique(event); - } else { - DHLOGW("could not find the deviceType\n"); - return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; - } + std::call_once(callOnceFlag_, [this]() { inputHub_->ScanInputDevices(DEVICE_PATH); }); + std::unique_ptr virtualDevice = std::make_unique(inputDevice); - if (device == nullptr) { - DHLOGE("could not create new virtual device == null\n"); - return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; - } + virtualDevice->SetNetWorkId(devId); - device->SetNetWorkId(devId); - - if (!device->SetUp(devId, dhId)) { + if (!virtualDevice->SetUp(inputDevice, devId, dhId)) { DHLOGE("could not create new virtual device\n"); return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } - AddDeviceLocked(event.descriptor, std::move(device)); + AddDeviceLocked(inputDevice.descriptor, std::move(virtualDevice)); return DH_SUCCESS; } @@ -129,8 +155,8 @@ int32_t DistributedInputNodeManager::CreateVirtualTouchScreenNode(const std::str LocalAbsInfo info = DInputContext::GetInstance().GetLocalTouchScreenInfo().localAbsInfo; DHLOGI("CreateVirtualTouchScreenNode start, dhId: %s, sourcePhyWidth: %d, sourcePhyHeight: %d", GetAnonyString(dhId).c_str(), sourcePhyWidth, sourcePhyHeight); - device = std::make_unique(info.deviceInfo, info, sourcePhyWidth - 1, sourcePhyHeight - 1); - if (!device->SetUp(devId, dhId)) { + device = std::make_unique(info.deviceInfo); + if (!device->SetUp(info.deviceInfo, devId, dhId)) { DHLOGE("Virtual touch Screen setUp fail, devId: %s, dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index c439415..444db30 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -26,14 +26,16 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -VirtualDevice::VirtualDevice() - : deviceName_(""), netWorkId_(""), busType_(0), vendorId_(0), productId_(0), version_(0), classes_(0) -{ +namespace { + constexpr uint32_t ABS_MIN_POS = 1; + constexpr uint32_t ABS_MAX_POS = 2; + constexpr uint32_t ABS_FUZZ_POS = 3; + constexpr uint32_t ABS_FLAT_POS = 4; } - VirtualDevice::VirtualDevice(const InputDevice& event) : deviceName_(event.name), busType_(event.bus), vendorId_(event.vendor), productId_(event.product), version_(event.version), classes_(event.classes) { + DHLOGI("VirtualDevice ctor!"); } VirtualDevice::~VirtualDevice() @@ -55,7 +57,7 @@ bool VirtualDevice::DoIoctl(int32_t fd, int32_t request, const uint32_t value) return true; } -bool VirtualDevice::CreateKey() +bool VirtualDevice::CreateKey(const InputDevice& inputDevice) { auto fun = [this](int32_t uiSet, const std::vector& list) -> bool { for (uint32_t evt_type : list) { @@ -66,22 +68,36 @@ bool VirtualDevice::CreateKey() } return true; }; - std::map> evt_type; - evt_type[UI_SET_EVBIT] = GetEventTypes(); - evt_type[UI_SET_KEYBIT] = GetKeys(); - evt_type[UI_SET_PROPBIT] = GetProperties(); - evt_type[UI_SET_ABSBIT] = GetAbs(); - evt_type[UI_SET_RELBIT] = GetRelBits(); + evt_type[UI_SET_EVBIT] = inputDevice.eventTypes; + evt_type[UI_SET_KEYBIT] = inputDevice.eventKeys; + evt_type[UI_SET_PROPBIT] = inputDevice.properties; + evt_type[UI_SET_ABSBIT] = inputDevice.absTypes; + evt_type[UI_SET_RELBIT] = inputDevice.relTypes; for (auto &it : evt_type) { if (!fun(it.first, it.second)) { return false; } } - return true; } +void VirtualDevice::SetABSInfo(struct uinput_user_dev& inputUserDev, const InputDevice& inputDevice) +{ + DHLOGI("SetABSInfo!"); + for (const auto item : inputDevice.absInfos) { + int absCode = item.first; + std::vector absInfo = item.second; + DHLOGI("SetABSInfo nodeName: %s, absCode: %d, absMin: %d, absMax: %d, absFuzz: %d, absFlat: %d", + inputDevice.name.c_str(), absCode, absInfo[ABS_MIN_POS], absInfo[ABS_MAX_POS], absInfo[ABS_FUZZ_POS], + absInfo[ABS_FLAT_POS]); + inputUserDev.absmin[absCode] = absInfo[ABS_MIN_POS]; + inputUserDev.absmax[absCode] = absInfo[ABS_MAX_POS]; + inputUserDev.absfuzz[absCode] = absInfo[ABS_FUZZ_POS]; + inputUserDev.absflat[absCode] = absInfo[ABS_FLAT_POS]; + } +} + bool VirtualDevice::SetPhys(const std::string deviceName, std::string dhId) { std::string phys; @@ -94,7 +110,7 @@ bool VirtualDevice::SetPhys(const std::string deviceName, std::string dhId) return true; } -bool VirtualDevice::SetUp(const std::string& devId, const std::string& dhId) +bool VirtualDevice::SetUp(const InputDevice& inputDevice, const std::string& devId, const std::string& dhId) { fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK); if (fd_ < 0) { @@ -120,11 +136,13 @@ bool VirtualDevice::SetUp(const std::string& devId, const std::string& dhId) return false; } - if (!CreateKey()) { + if (!CreateKey(inputDevice)) { DHLOGE("Failed to create KeyValue"); return false; } + SetABSInfo(dev_, inputDevice); + if (write(fd_, &dev_, sizeof(dev_)) < 0) { DHLOGE("Unable to set input device info"); return false; diff --git a/services/source/inputinject/src/virtual_keyboard.cpp b/services/source/inputinject/src/virtual_keyboard.cpp deleted file mode 100644 index 41136d3..0000000 --- a/services/source/inputinject/src/virtual_keyboard.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2021-2022 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 "virtual_keyboard.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -namespace { -const std::vector EVT_TYPES { - EV_KEY, EV_MSC, EV_LED, EV_REP, EV_SYN - }; -const std::vector KEYS { - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, - 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 85, - 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, - 113, 114, 115, 116, 117, 119, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, - 137, 138, 140, 142, 150, 152, 158, 159, 161, 163, 164, 165, 166, 173, 176, 177, 178, 179, 180, 183, 184, 185, - 186, 187, 188, 189, 190, 191, 192, 193, 194, 240, 211, 213, 214, 215, 218, 220, 221, 222, 223, 226, 227, 231, - 232, 233, 236, 237, 238, 239, 242, 243, 245, 246, 247, 248, 464, 522, 523, 141, 145, 146, 147, 148, 149, 151, - 153, 154, 157, 160, 162, 170, 175, 182, 200, 201, 202, 203, 204, 205, 101, 112, 118, 120 - }; -const std::vector PROPERTIES {}; -const std::vector ABS {}; -const std::vector RELBITS {}; -} - -VirtualKeyboard::VirtualKeyboard(const InputDevice& event) : VirtualDevice(event) {} - -VirtualKeyboard::~VirtualKeyboard() {} - -const std::vector& VirtualKeyboard::GetEventTypes() const -{ - return EVT_TYPES; -} - -const std::vector& VirtualKeyboard::GetKeys() const -{ - return KEYS; -} - -const std::vector& VirtualKeyboard::GetProperties() const -{ - return PROPERTIES; -} - -const std::vector& VirtualKeyboard::GetAbs() const -{ - return ABS; -} - -const std::vector& VirtualKeyboard::GetRelBits() const -{ - return RELBITS; -} -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS diff --git a/services/source/inputinject/src/virtual_mouse.cpp b/services/source/inputinject/src/virtual_mouse.cpp deleted file mode 100644 index fe063fd..0000000 --- a/services/source/inputinject/src/virtual_mouse.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2021-2022 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 "virtual_mouse.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -namespace { -const std::vector EVT_TYPES { - EV_KEY, EV_REL, EV_MSC, EV_SYN - }; -const std::vector KEYS { - BTN_MOUSE, BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, BTN_SIDE, BTN_EXTRA, BTN_FORWARD, BTN_BACK, BTN_TASK - }; -const std::vector PROPERTIES {}; -const std::vector ABS {}; -const std::vector RELBITS { - REL_X, REL_Y, REL_WHEEL, REL_WHEEL_HI_RES -}; -} - -VirtualMouse::VirtualMouse(const InputDevice& event) : VirtualDevice(event) {} - -VirtualMouse::~VirtualMouse() {} - -const std::vector& VirtualMouse::GetEventTypes() const -{ - return EVT_TYPES; -} - -const std::vector& VirtualMouse::GetKeys() const -{ - return KEYS; -} - -const std::vector& VirtualMouse::GetProperties() const -{ - return PROPERTIES; -} - -const std::vector& VirtualMouse::GetAbs() const -{ - return ABS; -} - -const std::vector& VirtualMouse::GetRelBits() const -{ - return RELBITS; -} -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - diff --git a/services/source/inputinject/src/virtual_touchpad.cpp b/services/source/inputinject/src/virtual_touchpad.cpp deleted file mode 100644 index 1f7d206..0000000 --- a/services/source/inputinject/src/virtual_touchpad.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2021-2022 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 "virtual_touchpad.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -namespace { -const std::vector EVT_TYPES { - EV_KEY, EV_ABS - }; -const std::vector KEYS { - BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_STYLUS, BTN_TOOL_PEN - }; -const std::vector PROPERTIES {}; -const std::vector ABS { - ABS_X, ABS_Y, ABS_WHEEL, ABS_MISC -}; -const std::vector RELBITS {}; -} - -VirtualTouchpad::VirtualTouchpad(const InputDevice& event) : VirtualDevice(event) -{ - const int absMaxWheel = 71; - - dev_.absmin[ABS_X] = 0; - dev_.absmax[ABS_X] = 1; - dev_.absfuzz[ABS_X] = 0; - dev_.absflat[ABS_X] = 0; - - dev_.absmin[ABS_Y] = 0; - dev_.absmax[ABS_Y] = 1; - dev_.absfuzz[ABS_Y] = 0; - dev_.absflat[ABS_Y] = 0; - - dev_.absmin[ABS_WHEEL] = 0; - dev_.absmax[ABS_WHEEL] = absMaxWheel; - dev_.absfuzz[ABS_WHEEL] = 0; - dev_.absflat[ABS_WHEEL] = 0; - - dev_.absmin[ABS_MISC] = 0; - dev_.absmax[ABS_MISC] = 0; - dev_.absfuzz[ABS_MISC] = 0; - dev_.absflat[ABS_MISC] = 0; -} - -VirtualTouchpad::~VirtualTouchpad() {} - -const std::vector& VirtualTouchpad::GetEventTypes() const -{ - return EVT_TYPES; -} - -const std::vector& VirtualTouchpad::GetKeys() const -{ - return KEYS; -} - -const std::vector& VirtualTouchpad::GetProperties() const -{ - return PROPERTIES; -} - -const std::vector& VirtualTouchpad::GetAbs() const -{ - return ABS; -} - -const std::vector& VirtualTouchpad::GetRelBits() const -{ - return RELBITS; -} -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - diff --git a/services/source/inputinject/src/virtual_touchscreen.cpp b/services/source/inputinject/src/virtual_touchscreen.cpp deleted file mode 100644 index 7eb797a..0000000 --- a/services/source/inputinject/src/virtual_touchscreen.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2021-2022 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 "virtual_touchscreen.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -namespace { -const std::vector EVT_TYPES { - EV_KEY, EV_ABS - }; -const std::vector KEYS { - KEY_F1, KEY_VOLUMEDOWN, KEY_VOLUMEUP, KEY_POWER, BTN_TOOL_FINGER, BTN_TOUCH, BTN_TRIGGER_HAPPY2, - BTN_TRIGGER_HAPPY3 - }; -const std::vector PROPERTIES { - INPUT_PROP_DIRECT - }; -const std::vector ABS { - ABS_X, ABS_Y, ABS_PRESSURE, ABS_MT_TOUCH_MAJOR, ABS_MT_TOUCH_MINOR, ABS_MT_ORIENTATION, ABS_MT_POSITION_X, - ABS_MT_POSITION_Y, ABS_MT_BLOB_ID, ABS_MT_TRACKING_ID, ABS_MT_PRESSURE - }; -const std::vector RELBITS {}; -} - -VirtualTouchScreen::VirtualTouchScreen(const InputDevice& event, LocalAbsInfo& absInfo, uint32_t phyWidth, - uint32_t phyHeight) : VirtualDevice(event) -{ - dev_.absmin[ABS_X] = 0; - dev_.absmax[ABS_X] = static_cast(phyWidth); - dev_.absfuzz[ABS_X] = 0; - dev_.absflat[ABS_X] = 0; - - dev_.absmin[ABS_Y] = 0; - dev_.absmax[ABS_Y] = static_cast(phyHeight); - dev_.absfuzz[ABS_Y] = 0; - dev_.absflat[ABS_Y] = 0; - - dev_.absmin[ABS_PRESSURE] = absInfo.absPressureMin; - dev_.absmax[ABS_PRESSURE] = absInfo.absPressureMax; - dev_.absfuzz[ABS_PRESSURE] = 0; - dev_.absflat[ABS_PRESSURE] = 0; - - dev_.absmin[ABS_MT_TOUCH_MAJOR] = absInfo.absMtTouchMajorMin; - dev_.absmax[ABS_MT_TOUCH_MAJOR] = absInfo.absMtTouchMajorMax; - dev_.absfuzz[ABS_MT_TOUCH_MAJOR] = 0; - dev_.absflat[ABS_MT_TOUCH_MAJOR] = 0; - - dev_.absmin[ABS_MT_TOUCH_MINOR] = absInfo.absMtTouchMinorMin; - dev_.absmax[ABS_MT_TOUCH_MINOR] = absInfo.absMtTouchMinorMax; - dev_.absfuzz[ABS_MT_TOUCH_MINOR] = 0; - dev_.absflat[ABS_MT_TOUCH_MINOR] = 0; - - dev_.absmin[ABS_MT_ORIENTATION] = absInfo.absMtOrientationMin; - dev_.absmax[ABS_MT_ORIENTATION] = absInfo.absMtOrientationMax; - dev_.absfuzz[ABS_MT_ORIENTATION] = 0; - dev_.absflat[ABS_MT_ORIENTATION] = 0; - - dev_.absmin[ABS_MT_POSITION_X] = 0; - dev_.absmax[ABS_MT_POSITION_X] = static_cast(phyWidth); - dev_.absfuzz[ABS_MT_POSITION_X] = 0; - dev_.absflat[ABS_MT_POSITION_X] = 0; - - dev_.absmin[ABS_MT_POSITION_Y] = 0; - dev_.absmax[ABS_MT_POSITION_Y] = static_cast(phyHeight); - dev_.absfuzz[ABS_MT_POSITION_Y] = 0; - dev_.absflat[ABS_MT_POSITION_Y] = 0; - - dev_.absmin[ABS_MT_BLOB_ID] = absInfo.absMtBlobIdMin; - dev_.absmax[ABS_MT_BLOB_ID] = absInfo.absMtBlobIdMax; - dev_.absfuzz[ABS_MT_BLOB_ID] = 0; - dev_.absflat[ABS_MT_BLOB_ID] = 0; - - dev_.absmin[ABS_MT_TRACKING_ID] = absInfo.absMtTrackingIdMin; - dev_.absmax[ABS_MT_TRACKING_ID] = absInfo.absMtTrackingIdMax; - dev_.absfuzz[ABS_MT_TRACKING_ID] = 0; - dev_.absflat[ABS_MT_TRACKING_ID] = 0; - - dev_.absmin[ABS_MT_PRESSURE] = absInfo.absMtPressureMin; - dev_.absmax[ABS_MT_PRESSURE] = absInfo.absMtPressureMax; - dev_.absfuzz[ABS_MT_PRESSURE] = 0; - dev_.absflat[ABS_MT_PRESSURE] = 0; -} - -VirtualTouchScreen::~VirtualTouchScreen() {} - -const std::vector& VirtualTouchScreen::GetEventTypes() const -{ - return EVT_TYPES; -} - -const std::vector& VirtualTouchScreen::GetKeys() const -{ - return KEYS; -} - -const std::vector& VirtualTouchScreen::GetProperties() const -{ - return PROPERTIES; -} - -const std::vector& VirtualTouchScreen::GetAbs() const -{ - return ABS; -} - -const std::vector& VirtualTouchScreen::GetRelBits() const -{ - return RELBITS; -} -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - diff --git a/services/source/inputinject/test/sourceinjectunittest/BUILD.gn b/services/source/inputinject/test/sourceinjectunittest/BUILD.gn index dd7ab0b..7a3a097 100755 --- a/services/source/inputinject/test/sourceinjectunittest/BUILD.gn +++ b/services/source/inputinject/test/sourceinjectunittest/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("distributed_input_inner_sourceinject_test") { "${fwk_utils_path}/include", "${service_common}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "//foundation/communication/dsoftbus/interfaces/kits/transport", "${dfx_utils_path}/include", "${utils_path}/include", @@ -62,10 +63,6 @@ ohos_unittest("distributed_input_inner_sourceinject_test") { "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/distributed_input_inject.cpp", "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/distributed_input_node_manager.cpp", "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_device.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_keyboard.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_mouse.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_touchpad.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_touchscreen.cpp", "distributed_input_sourceinject_test.cpp", "${common_path}/test/mock/softbus_bus_center_mock.cpp", ] @@ -89,6 +86,7 @@ ohos_unittest("distributed_input_inner_sourceinject_test") { "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", "//third_party/openssl:libcrypto_static", + "//third_party/libevdev:libevdev", "${distributedinput_path}/interfaces/inner_kits:libdinput_sdk", ] diff --git a/services/source/sourcemanager/BUILD.gn b/services/source/sourcemanager/BUILD.gn index 848e1b0..f87f93b 100644 --- a/services/source/sourcemanager/BUILD.gn +++ b/services/source/sourcemanager/BUILD.gn @@ -37,6 +37,7 @@ ohos_shared_library("libdinput_source") { "${dfx_utils_path}/include", "${utils_path}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", "${fwk_interfaces_path}/include", "${fwk_interfaces_path}/include/ipc", @@ -101,6 +102,7 @@ ohos_shared_library("libdinput_source") { "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", "//foundation/distributedhardware/distributed_input/services/source/transport:libdinput_source_trans", "${fwk_interfaces_path}:libdhfwk_sdk", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 53d51a3..0c2b6d4 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -1036,11 +1036,11 @@ int32_t DistributedInputSourceManager::Release() bool DistributedInputSourceManager::CheckRegisterParam(const std::string &devId, const std::string &dhId, const std::string ¶meters, sptr callback) { - if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam devId is empty or devId size too long."); return false; } - if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam dhId is empty or dhId size too long."); return false; } @@ -1168,11 +1168,11 @@ int32_t DistributedInputSourceManager::UnregCallbackNotify(const std::string &de bool DistributedInputSourceManager::CheckUnregisterParam(const std::string &devId, const std::string &dhId, sptr callback) { - if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam devId is empty or devId size too long."); return false; } - if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) { DHLOGE("CheckParam dhId is empty or dhId size too long."); return false; } @@ -2660,17 +2660,17 @@ int32_t DistributedInputSourceManager::StartDScreenListener::ParseMessage(const return ERR_DH_INPUT_JSON_PARSE_FAIL; } sinkDevId = jsonObj[SINK_DEVICE_ID].get(); - if (!IsUint64(jsonObj, SOURCE_WINDOW_ID)) { + if (!IsUInt64(jsonObj, SOURCE_WINDOW_ID)) { DHLOGE("sourceWinId key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } srcScreenInfo.sourceWinId = jsonObj[SOURCE_WINDOW_ID].get(); - if (!IsUint32(jsonObj, SOURCE_WINDOW_WIDTH)) { + if (!IsUInt32(jsonObj, SOURCE_WINDOW_WIDTH)) { DHLOGE("sourceWinWidth key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } srcScreenInfo.sourceWinWidth = jsonObj[SOURCE_WINDOW_WIDTH].get(); - if (!IsUint32(jsonObj, SOURCE_WINDOW_HEIGHT)) { + if (!IsUInt32(jsonObj, SOURCE_WINDOW_HEIGHT)) { DHLOGE("sourceWinHeight key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } @@ -2770,7 +2770,7 @@ int32_t DistributedInputSourceManager::StopDScreenListener::ParseMessage(const s return ERR_DH_INPUT_JSON_PARSE_FAIL; } sinkDevId = jsonObj[SINK_DEVICE_ID].get(); - if (!IsUint64(jsonObj, SOURCE_WINDOW_ID)) { + if (!IsUInt64(jsonObj, SOURCE_WINDOW_ID)) { DHLOGE("sourceWinId key is invalid"); return ERR_DH_INPUT_JSON_PARSE_FAIL; } diff --git a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn index cf3362d..a8daee6 100755 --- a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn +++ b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn @@ -44,6 +44,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { "${fwk_utils_path}/include", "${service_common}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "//foundation/communication/dsoftbus/interfaces/kits/transport", "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", "${services_source_path}/inputinject/include", @@ -84,10 +85,6 @@ ohos_unittest("distributed_input_sourcemanager_test") { "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/distributed_input_inject.cpp", "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/distributed_input_node_manager.cpp", "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_device.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_keyboard.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_mouse.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_touchpad.cpp", - "//foundation/distributedhardware/distributed_input/services/source/inputinject/src/virtual_touchscreen.cpp", "//foundation/distributedhardware/distributed_input/services/source/sourcemanager/src/distributed_input_source_event_handler.cpp", "//foundation/distributedhardware/distributed_input/services/source/sourcemanager/src/distributed_input_source_manager.cpp", "//foundation/distributedhardware/distributed_input/services/source/sourcemanager/src/distributed_input_source_sa_cli_mgr.cpp", @@ -116,6 +113,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_static", "${fwk_interfaces_path}:libdhfwk_sdk", "${innerkits_path}:libdinput_sdk", diff --git a/services/source/transport/BUILD.gn b/services/source/transport/BUILD.gn index fca24a7..69982d0 100755 --- a/services/source/transport/BUILD.gn +++ b/services/source/transport/BUILD.gn @@ -38,6 +38,7 @@ ohos_shared_library("libdinput_source_trans") { "${distributedinput_path}/inputdevicehandler/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", + "//third_party/libevdev/libevdev/" ] sources = [ "src/distributed_input_source_transport.cpp" ] @@ -56,6 +57,7 @@ ohos_shared_library("libdinput_source_trans") { "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/communication/dsoftbus/sdk:softbus_client", "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/services/source/transport/test/sourcetransunittest/BUILD.gn b/services/source/transport/test/sourcetransunittest/BUILD.gn index 55b517b..b9703c7 100755 --- a/services/source/transport/test/sourcetransunittest/BUILD.gn +++ b/services/source/transport/test/sourcetransunittest/BUILD.gn @@ -41,6 +41,7 @@ ohos_unittest("distributed_input_sourcetrans_test") { "${fwk_interfaces_path}/include/ipc", "${service_common}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${services_source_path}/inputinject/include", "${services_source_path}/transport/include", "${services_source_path}/sourcemanager/include", @@ -84,6 +85,7 @@ ohos_unittest("distributed_input_sourcetrans_test") { "${utils_path}:libdinput_utils", "//foundation/distributedhardware/distributed_input/services/source/sourcemanager:libdinput_source", "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/sourcehandler/BUILD.gn b/sourcehandler/BUILD.gn index 6e61529..81189a8 100644 --- a/sourcehandler/BUILD.gn +++ b/sourcehandler/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("libdinput_source_handler") { "${services_source_path}/inputinject/include", "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", "//base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include", + "//third_party/libevdev/libevdev/" ] sources = [ @@ -58,6 +59,7 @@ ohos_shared_library("libdinput_source_handler") { "${innerkits_path}:libdinput_sdk", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "${dfx_utils_path}:libdinput_dfx_utils", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/sourcehandler/test/unittest/BUILD.gn b/sourcehandler/test/unittest/BUILD.gn index ba913b0..f50fab0 100755 --- a/sourcehandler/test/unittest/BUILD.gn +++ b/sourcehandler/test/unittest/BUILD.gn @@ -49,6 +49,7 @@ ohos_unittest("distributed_input_source_handler_test") { "${services_source_path}/inputinject/include", "${utils_path}/include", "//base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include", + "//third_party/libevdev/libevdev/", ] sources = [ @@ -77,6 +78,7 @@ ohos_unittest("distributed_input_source_handler_test") { "${fwk_utils_path}:distributedhardwareutils", "${innerkits_path}:libdinput_sdk", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn b/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn index 733e4ff..bc08f51 100755 --- a/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn @@ -39,6 +39,7 @@ ohos_fuzztest("DistributedInputClientFuzzTest") { "${service_common}/include", "${common_path}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "//foundation/distributedhardware/distributed_hardware_fwk/common/log/include", "//foundation/distributedhardware/distributed_hardware_fwk/common/utils/include", "//foundation/distributedhardware/distributed_hardware_fwk/utils/include/log", @@ -63,6 +64,7 @@ ohos_fuzztest("DistributedInputClientFuzzTest") { deps = [ "${distributedinput_path}/sourcehandler:libdinput_source_handler", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn b/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn index ec11d3b..156e6fd 100755 --- a/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn @@ -39,6 +39,7 @@ ohos_fuzztest("DistributedInputKitFuzzTest") { "${service_common}/include", "${common_path}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "//foundation/distributedhardware/distributed_hardware_fwk/common/log/include", "//foundation/distributedhardware/distributed_hardware_fwk/common/utils/include", "//foundation/distributedhardware/distributed_hardware_fwk/utils/include/log", @@ -61,6 +62,7 @@ ohos_fuzztest("DistributedInputKitFuzzTest") { deps = [ "${distributedinput_path}/interfaces/inner_kits:libdinput_sdk", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn b/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn index f5100f3..aeef73c 100755 --- a/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn @@ -36,6 +36,7 @@ ohos_fuzztest("DistributedInputSinkTransportFuzzTest") { "${fwk_utils_path}/include", "${service_common}/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${dfx_utils_path}/include", "${utils_path}/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", @@ -61,6 +62,7 @@ ohos_fuzztest("DistributedInputSinkTransportFuzzTest") { "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/communication/dsoftbus/sdk:softbus_client", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn b/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn index 934b380..a95fb49 100755 --- a/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn @@ -38,6 +38,7 @@ ohos_fuzztest("DistributedInputSourceTransportFuzzTest") { "${services_source_path}/inputinject/include", "${services_source_path}/transport/include", "//third_party/json/include", + "//third_party/libevdev/libevdev/", "${dfx_utils_path}/include", "${utils_path}/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", @@ -66,6 +67,7 @@ ohos_fuzztest("DistributedInputSourceTransportFuzzTest") { "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", + "//third_party/libevdev:libevdev", ] external_deps = [ diff --git a/utils/include/dinput_context.h b/utils/include/dinput_context.h index 78c44e1..0724515 100644 --- a/utils/include/dinput_context.h +++ b/utils/include/dinput_context.h @@ -37,7 +37,6 @@ #include "system_ability_definition.h" #include "i_dinput_context.h" -#include "input_hub.h" namespace OHOS { namespace DistributedHardware { diff --git a/utils/include/dinput_utils_tool.h b/utils/include/dinput_utils_tool.h index d9a1153..655bfe0 100644 --- a/utils/include/dinput_utils_tool.h +++ b/utils/include/dinput_utils_tool.h @@ -36,11 +36,13 @@ std::string GetLocalNetworkId(); std::string GetUUIDBySoftBus(const std::string &networkId); uint64_t GetCurrentTime(); std::string SetAnonyId(const std::string &message); -/* IsString, IsUint32 and IsUint64 are used to valid json key */ +/* IsString, IsUInt32 and IsUInt64 are used to valid json key */ bool IsString(const nlohmann::json& jsonObj, const std::string& key); bool IsInt32(const nlohmann::json& jsonObj, const std::string& key); -bool IsUint32(const nlohmann::json& jsonObj, const std::string& key); -bool IsUint64(const nlohmann::json& jsonObj, const std::string& key); +bool IsUInt16(const nlohmann::json& jsonObj, const std::string& key); +bool IsUInt32(const nlohmann::json& jsonObj, const std::string& key); +bool IsUInt64(const nlohmann::json& jsonObj, const std::string& key); +bool IsArray(const nlohmann::json& jsonObj, const std::string& key); std::string GetNodeDesc(std::string parameters); } // namespace DistributedInput } // namespace DistributedHardware diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index 4938a40..b380d69 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -32,10 +32,6 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -namespace { - const char *const DESCRIPTOR = "descriptor"; -} - DevInfo GetLocalDeviceInfo() { DevInfo devInfo{"", "", 0}; @@ -134,20 +130,35 @@ bool IsInt32(const nlohmann::json& jsonObj, const std::string& key) return res; } -bool IsUint32(const nlohmann::json& jsonObj, const std::string& key) + +bool IsUInt16(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT16_MAX; + DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); + return res; +} + +bool IsUInt32(const nlohmann::json& jsonObj, const std::string& key) { bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX; DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); return res; } -bool IsUint64(const nlohmann::json& jsonObj, const std::string& key) +bool IsUInt64(const nlohmann::json& jsonObj, const std::string& key) { bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT64_MAX; DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); return res; } +bool IsArray(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_array(); + DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); + return res; +} + std::string GetNodeDesc(std::string parameters) { nlohmann::json parObj = nlohmann::json::parse(parameters, nullptr, false); @@ -159,11 +170,11 @@ std::string GetNodeDesc(std::string parameters) std::string physicalPath = "N/A"; int32_t classes = -1; - if (parObj.find("name") != parObj.end() && parObj.find("physicalPath") != parObj.end() && - parObj.find("classes") != parObj.end()) { - nodeName = parObj.at("name").get(); - physicalPath = parObj.at("physicalPath").get(); - classes = parObj.at("classes").get(); + if (parObj.find(DEVICE_NAME) != parObj.end() && parObj.find(PHYSICAL_PATH) != parObj.end() && + parObj.find(CLASSES) != parObj.end()) { + nodeName = parObj.at(DEVICE_NAME).get(); + physicalPath = parObj.at(PHYSICAL_PATH).get(); + classes = parObj.at(CLASSES).get(); } return "{ nodeName: " + nodeName + ", physicalPath: " + physicalPath + ", classes: " + std::to_string(classes) + " }";