支持非标外设扩展驱动-HID DDK增强

Signed-off-by: limabiao <limabiao1@h-partners.com>
Change-Id: Ia759e8f3872136fda864dc0d594f687d5a238a21
This commit is contained in:
limabiao
2024-12-27 11:33:43 +08:00
parent ce54bde693
commit e826e83bd5
92 changed files with 3884 additions and 41 deletions
+1
View File
@@ -29,6 +29,7 @@
"hilog",
"hilog_lite",
"drivers_interface_input",
"drivers_peripheral_usb",
"access_token"
],
"third_party": [
+13 -4
View File
@@ -1,4 +1,4 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Copyright (c) 2023-2025 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
@@ -15,15 +15,23 @@ import("//build/config/components/hdi/hdi.gni")
import("//build/ohos.gni")
import("./../input.gni")
ohos_shared_library("libhid_ddk_service_1.0") {
ohos_shared_library("libhid_ddk_service_1.1") {
sources = [
"src/hid_ddk_permission.cpp",
"src/hid_ddk_service.cpp",
]
if (is_standard_system) {
sources += [ "src/hid_linux_adapter.cpp" ]
} else if (defined(ohos_lite)) {
sources += [ "src/hid_liteos_adapter.cpp" ]
}
include_dirs = [
"include/",
"include/emit_event_manager/",
"${input_driver_path}/utils/include",
"//drivers/hdf_core/framework/model/usb/include",
"//drivers/peripheral/usb/ddk/host/include",
]
deps = [ "${input_driver_path}/ddk_service/src/emit_event_manager:emit_event_manager" ]
@@ -32,6 +40,7 @@ ohos_shared_library("libhid_ddk_service_1.0") {
"access_token:libaccesstoken_sdk",
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_peripheral_usb:libusb_pnp_manager",
"hdf_core:libhdf_utils",
"hilog:libhilog",
"ipc:ipc_single",
@@ -55,7 +64,7 @@ ohos_shared_library("libhid_ddk_driver") {
external_deps = [
"c_utils:utils",
"drivers_interface_input:libhid_ddk_stub_1.0",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_host",
"hdf_core:libhdf_ipc_adapter",
"hdf_core:libhdf_utils",
@@ -73,6 +82,6 @@ ohos_shared_library("libhid_ddk_driver") {
group("hid_ddk_target") {
deps = [
":libhid_ddk_driver",
":libhid_ddk_service_1.0",
":libhid_ddk_service_1.1",
]
}
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025 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_ADAPTER_H
#define HID_ADAPTER_H
#include <stdint.h>
#include <vector>
#include "v1_1/ihid_ddk.h"
namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_1 {
class HidOsAdapter {
public:
virtual int32_t GetRawInfo(int32_t fd, HidRawDevInfo& rawDevInfo) = 0;
virtual int32_t GetRawName(int32_t fd, std::vector<uint8_t>& data) = 0;
virtual int32_t GetPhysicalAddress(int32_t fd, std::vector<uint8_t>& data) = 0;
virtual int32_t GetRawUniqueId(int32_t fd, std::vector<uint8_t>& data) = 0;
virtual int32_t SendReport(int32_t fd, HidReportType reportType, const std::vector<uint8_t>& data) = 0;
virtual int32_t GetReport(int32_t fd, HidReportType reportType, std::vector<uint8_t>& data) = 0;
virtual int32_t GetReportDescriptor(int32_t fd, std::vector<uint8_t>& data, uint32_t& bytesRead) = 0;
};
} // V1_1
} // Ddk
} // Input
} // HDI
} // OHOS
#endif // HID_ADAPTER_H
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2025 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
@@ -22,12 +22,12 @@ namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_0 {
namespace V1_1 {
class DdkPermissionManager {
public:
static bool VerifyPermission(std::string permissionName);
};
} // namespace V1_0
} // namespace V1_1
} // namespace Ddk
} // namespace Input
} // namespace HDI
+42 -6
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 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
@@ -16,25 +16,61 @@
#ifndef HID_DDK_SERVICE_H
#define HID_DDK_SERVICE_H
#include "v1_0/ihid_ddk.h"
#include "hid_adapter.h"
#include "v1_1/ihid_ddk.h"
namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_0 {
namespace V1_1 {
class HidDdkService : public IHidDdk {
public:
HidDdkService() = default;
HidDdkService(std::shared_ptr<HidOsAdapter>& osAdapter) : osAdapter_(osAdapter) {}
virtual ~HidDdkService() = default;
int32_t CreateDevice(const Hid_Device& hidDevice, const Hid_EventProperties& hidEventProperties) override;
int32_t CreateDevice(const Hid_Device& hidDevice,
const Hid_EventProperties& hidEventProperties, uint32_t& deviceId) override;
int32_t EmitEvent(uint32_t deviceId, const std::vector<Hid_EmitItem>& items) override;
int32_t DestroyDevice(uint32_t deviceId) override;
int32_t Init() override;
int32_t Release() override;
int32_t Open(uint64_t deviceId, uint8_t interfaceIndex, HidDeviceHandle& dev) override;
int32_t Close(const HidDeviceHandle& dev) override;
int32_t Write(const HidDeviceHandle& dev, const std::vector<uint8_t>& data, uint32_t& bytesWritten) override;
int32_t ReadTimeout(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize, int32_t timeout,
uint32_t& bytesRead) override;
int32_t SetNonBlocking(const HidDeviceHandle& dev, int32_t nonBlock) override;
int32_t GetRawInfo(const HidDeviceHandle& dev, HidRawDevInfo& rawDevInfo) override;
int32_t GetRawName(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize) override;
int32_t GetPhysicalAddress(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize) override;
int32_t GetRawUniqueId(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize) override;
int32_t SendReport(const HidDeviceHandle& dev, HidReportType reportType, const std::vector<uint8_t>& data) override;
int32_t GetReport(const HidDeviceHandle& dev, HidReportType reportType, uint8_t reportNumber,
std::vector<uint8_t>& data, uint32_t buffSize) override;
int32_t GetReportDescriptor(const HidDeviceHandle& dev, std::vector<uint8_t>& buf, uint32_t buffSize,
uint32_t& bytesRead) override;
private:
std::shared_ptr<HidOsAdapter> osAdapter_;
};
} // V1_0
} // V1_1
} // Ddk
} // Input
} // HDI
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025 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_LINUX_ADAPTER_H
#define HID_LINUX_ADAPTER_H
#include <stdint.h>
#include <vector>
#include "hid_adapter.h"
#include "v1_0/ihid_ddk.h"
namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_1 {
class LinuxHidOsAdapter : public HidOsAdapter {
public:
int32_t GetRawInfo(int32_t fd, HidRawDevInfo& rawDevInfo) override;
int32_t GetRawName(int32_t fd, std::vector<uint8_t>& data) override;
int32_t GetPhysicalAddress(int32_t fd, std::vector<uint8_t>& data) override;
int32_t GetRawUniqueId(int32_t fd, std::vector<uint8_t>& data) override;
int32_t SendReport(int32_t fd, HidReportType reportType, const std::vector<uint8_t>& data) override;
int32_t GetReport(int32_t fd, HidReportType reportType, std::vector<uint8_t>& data) override;
int32_t GetReportDescriptor(int32_t fd, std::vector<uint8_t>& data, uint32_t& bytesRead) override;
virtual ~LinuxHidOsAdapter() = default;
};
} // V1_1
} // Ddk
} // Input
} // HDI
} // OHOS
#endif // HID_LINUX_ADAPTER_H
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025 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_LITEOS_ADAPTER_H
#define HID_LITEOS_ADAPTER_H
#include <stdint.h>
#include <vector>
#include "hid_adapter.h"
#include "v1_0/ihid_ddk.h"
namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_1 {
class LiteosHidOsAdapter : public HidOsAdapter {
public:
int32_t GetRawInfo(int32_t fd, HidRawDevInfo& rawDevInfo) override;
int32_t GetRawName(int32_t fd, std::vector<uint8_t>& data) override;
int32_t GetPhysicalAddress(int32_t fd, std::vector<uint8_t>& data) override;
int32_t GetRawUniqueId(int32_t fd, std::vector<uint8_t>& data) override;
int32_t SendReport(int32_t fd, HidReportType reportType, const std::vector<uint8_t>& data) override;
int32_t GetReport(int32_t fd, HidReportType reportType, std::vector<uint8_t>& data) override;
int32_t GetReportDescriptor(int32_t fd, std::vector<uint8_t>& data, uint32_t& bytesRead) override;
virtual ~LiteosHidOsAdapter() = default;
};
} // V1_1
} // Ddk
} // Input
} // HDI
} // OHOS
#endif // HID_LITEOS_ADAPTER_H
+5 -5
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 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
@@ -16,13 +16,13 @@
#include <hdf_base.h>
#include <hdf_device_desc.h>
#include <hdf_sbuf_ipc.h>
#include "v1_0/hid_ddk_stub.h"
#include "v1_1/hid_ddk_stub.h"
#include "emit_event_manager.h"
#include "input_uhdf_log.h"
#define HDF_LOG_TAG hid_ddk_driver
using namespace OHOS::HDI::Input::Ddk::V1_0;
using namespace OHOS::HDI::Input::Ddk::V1_1;
struct HdfHidDdkHost {
struct IDeviceIoService ioService;
@@ -69,7 +69,7 @@ static int HdfHidDdkDriverBind(struct HdfDeviceObject *deviceObject)
hdfHidDdkHost->ioService.Open = NULL;
hdfHidDdkHost->ioService.Release = NULL;
auto serviceImpl = OHOS::HDI::Input::Ddk::V1_0::IHidDdk::Get(true);
auto serviceImpl = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(true);
if (serviceImpl == nullptr) {
HDF_LOGE("%{public}s: failed to get of implement service", __func__);
delete hdfHidDdkHost;
@@ -77,7 +77,7 @@ static int HdfHidDdkDriverBind(struct HdfDeviceObject *deviceObject)
}
hdfHidDdkHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
OHOS::HDI::Input::Ddk::V1_0::IHidDdk::GetDescriptor());
OHOS::HDI::Input::Ddk::V1_1::IHidDdk::GetDescriptor());
if (hdfHidDdkHost->stub == nullptr) {
HDF_LOGE("%{public}s: failed to get stub object", __func__);
delete hdfHidDdkHost;
+3 -3
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Copyright (c) 2024-2025 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
@@ -22,7 +22,7 @@ namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_0 {
namespace V1_1 {
using namespace OHOS::Security::AccessToken;
bool DdkPermissionManager::VerifyPermission(std::string permissionName)
@@ -32,7 +32,7 @@ bool DdkPermissionManager::VerifyPermission(std::string permissionName)
HDF_LOGI("%{public}s VerifyAccessToken: %{public}d", __func__, result);
return result == PERMISSION_GRANTED;
}
} // namespace V1_0
} // namespace V1_1
} // namespace Ddk
} // namespace Input
} // namespace HDI
+301 -5
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 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
@@ -13,11 +13,26 @@
* limitations under the License.
*/
#include "v1_0/hid_ddk_service.h"
#include "hid_ddk_service.h"
#include <hdf_base.h>
#include "emit_event_manager.h"
#include "hid_ddk_permission.h"
#include "input_uhdf_log.h"
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <sys/ioctl.h>
#include <linux/hiddev.h>
#include <linux/hidraw.h>
#include <poll.h>
#include <memory.h>
#include <securec.h>
#include "ddk_sysfs_device.h"
#ifdef __LITEOS__
#include "hid_liteos_adapter.h"
#else
#include "hid_linux_adapter.h"
#endif
#define HDF_LOG_TAG hid_ddk_service
@@ -25,12 +40,32 @@ namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_0 {
namespace V1_1 {
const uint8_t DEVFS_PATH_LEN = 128;
const uint8_t THIRTY_TWO_BIT = 32;
const uint32_t MAX_REPORT_BUFFER_SIZE = 16 * 1024 - 1;
inline uint32_t GetBusNum(uint64_t devHandle)
{
return static_cast<uint32_t>(devHandle >> THIRTY_TWO_BIT);
}
inline uint32_t GetDevNum(uint64_t devHandle)
{
return static_cast<uint32_t>(devHandle & 0xFFFFFFFF);
}
static const std::string PERMISSION_NAME = "ohos.permission.ACCESS_DDK_HID";
extern "C" IHidDdk *HidDdkImplGetInstance(void)
{
return new (std::nothrow) HidDdkService();
std::shared_ptr<HidOsAdapter> osAdapter;
#ifdef __LITEOS__
osAdapter = std::make_shared<LiteosHidOsAdapter>();
#else
osAdapter = std::make_shared<LinuxHidOsAdapter>();
#endif
return new (std::nothrow) HidDdkService(osAdapter);
}
int32_t HidDdkService::CreateDevice(const Hid_Device &hidDevice,
@@ -75,7 +110,268 @@ int32_t HidDdkService::DestroyDevice(uint32_t deviceId)
return OHOS::ExternalDeviceManager::EmitEventManager::GetInstance().DestroyDevice(deviceId);
}
} // V1_0
int32_t HidDdkService::Init()
{
HDF_LOGD("%{public}s init enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
return HID_DDK_SUCCESS;
}
int32_t HidDdkService::Release()
{
HDF_LOGD("%{public}s release enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
return HID_DDK_SUCCESS;
}
int32_t HidDdkService::Open(uint64_t deviceId, uint8_t interfaceIndex, HidDeviceHandle& dev)
{
HDF_LOGD("%{public}s open enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
DevInterfaceInfo devInfo;
devInfo.busNum = GetBusNum(deviceId);
devInfo.devNum = GetDevNum(deviceId);
devInfo.intfNum = interfaceIndex;
char buff[DEVFS_PATH_LEN] = {0};
int32_t ret = DdkSysfsGetDevNodePath(&devInfo, "hidraw", buff, sizeof(buff));
if (ret != HID_DDK_SUCCESS) {
HDF_LOGE("%{public}s device not found", __func__);
return HID_DDK_DEVICE_NOT_FOUND;
}
int32_t fd = open(buff, O_RDWR);
if (fd < 0) {
HDF_LOGE("%{public}s open failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
dev.fd = fd;
return HID_DDK_SUCCESS;
}
int32_t HidDdkService::Close(const HidDeviceHandle& dev)
{
HDF_LOGD("%{public}s close enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
int32_t ret = close(dev.fd);
if (ret == -1) {
HDF_LOGE("%{public}s close failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
return HID_DDK_SUCCESS;
}
int32_t HidDdkService::Write(const HidDeviceHandle& dev, const std::vector<uint8_t>& data, uint32_t& bytesWritten)
{
HDF_LOGD("%{public}s write enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
int32_t ret = write(dev.fd, data.data(), data.size());
if (ret < 0) {
HDF_LOGE("%{public}s write failed, errno=%{public}d", __func__, errno);
bytesWritten = 0;
return HID_DDK_IO_ERROR;
}
bytesWritten = ret;
return HID_DDK_SUCCESS;
}
int32_t HidDdkService::ReadTimeout(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize,
int32_t timeout, uint32_t& bytesRead)
{
HDF_LOGD("%{public}s read timeout enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
if (buffSize > MAX_REPORT_BUFFER_SIZE) {
HDF_LOGE("%{public}s: invalid parameter", __func__);
return HID_DDK_INVALID_PARAMETER;
}
if (timeout >= 0) {
int32_t ret;
struct pollfd fds;
fds.fd = dev.fd;
fds.events = POLLIN;
fds.revents = 0;
ret = poll(&fds, 1, timeout);
if (ret == 0) {
return HID_DDK_TIMEOUT;
} else if (ret == -1) {
HDF_LOGE("%{public}s poll failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) {
HDF_LOGE("%{public}s poll failed, revents=%{public}d", __func__, fds.revents);
return HID_DDK_IO_ERROR;
}
}
data.resize(buffSize);
int32_t readRet = read(dev.fd, data.data(), data.size());
if (readRet < 0) {
HDF_LOGE("%{public}s read failed, errno=%{public}d", __func__, errno);
bytesRead = 0;
return HID_DDK_IO_ERROR;
}
bytesRead = readRet;
return HID_DDK_SUCCESS;
}
int32_t HidDdkService::SetNonBlocking(const HidDeviceHandle& dev, int32_t nonBlock)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
return HID_DDK_SUCCESS;
}
int32_t HidDdkService::GetRawInfo(const HidDeviceHandle& dev, HidRawDevInfo& rawDevInfo)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
return osAdapter_->GetRawInfo(dev.fd, rawDevInfo);
}
int32_t HidDdkService::GetRawName(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
if (buffSize > MAX_REPORT_BUFFER_SIZE) {
HDF_LOGE("%{public}s: invalid parameter", __func__);
return HID_DDK_INVALID_PARAMETER;
}
data.resize(buffSize);
return osAdapter_->GetRawName(dev.fd, data);
}
int32_t HidDdkService::GetPhysicalAddress(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
if (buffSize > MAX_REPORT_BUFFER_SIZE) {
HDF_LOGE("%{public}s: invalid parameter", __func__);
return HID_DDK_INVALID_PARAMETER;
}
data.resize(buffSize);
return osAdapter_->GetPhysicalAddress(dev.fd, data);
}
int32_t HidDdkService::GetRawUniqueId(const HidDeviceHandle& dev, std::vector<uint8_t>& data, uint32_t buffSize)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
if (buffSize > MAX_REPORT_BUFFER_SIZE) {
HDF_LOGE("%{public}s: invalid parameter", __func__);
return HID_DDK_INVALID_PARAMETER;
}
data.resize(buffSize);
return osAdapter_->GetRawUniqueId(dev.fd, data);
}
int32_t HidDdkService::SendReport(const HidDeviceHandle& dev, HidReportType reportType,
const std::vector<uint8_t>& data)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
return osAdapter_->SendReport(dev.fd, reportType, data);
}
int32_t HidDdkService::GetReport(const HidDeviceHandle& dev, HidReportType reportType, uint8_t reportNumber,
std::vector<uint8_t>& data, uint32_t buffSize)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
if (buffSize > MAX_REPORT_BUFFER_SIZE) {
HDF_LOGE("%{public}s: invalid parameter", __func__);
return HID_DDK_INVALID_PARAMETER;
}
data.resize(buffSize);
data[0] = reportNumber;
return osAdapter_->GetReport(dev.fd, reportType, data);
}
int32_t HidDdkService::GetReportDescriptor(const HidDeviceHandle& dev, std::vector<uint8_t>& buf, uint32_t buffSize,
uint32_t& bytesRead)
{
HDF_LOGD("%{public}s enter", __func__);
if (!DdkPermissionManager::VerifyPermission(PERMISSION_NAME)) {
HDF_LOGE("%{public}s: no permission", __func__);
return HID_DDK_NO_PERM;
}
if (buffSize > MAX_REPORT_BUFFER_SIZE) {
HDF_LOGE("%{public}s: invalid parameter", __func__);
return HID_DDK_INVALID_PARAMETER;
}
buf.resize(buffSize);
return osAdapter_->GetReportDescriptor(dev.fd, buf, bytesRead);
}
} // V1_1
} // Ddk
} // Input
} // HDI
+193
View File
@@ -0,0 +1,193 @@
/*
* Copyright (c) 2025 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 "hid_linux_adapter.h"
#include <hdf_base.h>
#include "input_uhdf_log.h"
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <sys/ioctl.h>
#include <linux/hiddev.h>
#include <linux/hidraw.h>
#include <poll.h>
#include <memory.h>
#include <securec.h>
#define HDF_LOG_TAG hid_linux_adapter
namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_1 {
#ifndef HIDIOCGINPUT
#define HIDIOCGINPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x0A, len)
#endif
#ifndef HIDIOCSOUTPUT
#define HIDIOCSOUTPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x0B, len)
#endif
#ifndef HIDIOCGOUTPUT
#define HIDIOCGOUTPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x0C, len)
#endif
#ifndef HIDIOCSINPUT
#define HIDIOCSINPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x09, len)
#endif
int32_t LinuxHidOsAdapter::GetRawInfo(int32_t fd, HidRawDevInfo& rawDevInfo)
{
struct hidraw_devinfo info;
(void)memset_s(&info, sizeof(info), 0x0, sizeof(info));
int32_t ret = ioctl(fd, HIDIOCGRAWINFO, &info);
if (ret < 0) {
HDF_LOGE("%{public}s ioctl failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
rawDevInfo.busType = info.bustype;
rawDevInfo.vendor = info.vendor;
rawDevInfo.product = info.product;
return HID_DDK_SUCCESS;
}
int32_t LinuxHidOsAdapter::GetRawName(int32_t fd, std::vector<uint8_t>& data)
{
int32_t ret = ioctl(fd, HIDIOCGRAWNAME(data.size()), data.data());
if (ret < 0) {
HDF_LOGE("%{public}s ioctl failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
return HID_DDK_SUCCESS;
}
int32_t LinuxHidOsAdapter::GetPhysicalAddress(int32_t fd, std::vector<uint8_t>& data)
{
int32_t ret = ioctl(fd, HIDIOCGRAWPHYS(data.size()), data.data());
if (ret < 0) {
HDF_LOGE("%{public}s ioctl failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
return HID_DDK_SUCCESS;
}
int32_t LinuxHidOsAdapter::GetRawUniqueId(int32_t fd, std::vector<uint8_t>& data)
{
int32_t ret = ioctl(fd, HIDIOCGRAWUNIQ(data.size()), data.data());
if (ret < 0) {
HDF_LOGE("%{public}s ioctl failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
return HID_DDK_SUCCESS;
}
int32_t LinuxHidOsAdapter::SendReport(int32_t fd, HidReportType reportType, const std::vector<uint8_t>& data)
{
unsigned long int req = 0;
switch (reportType) {
case HID_INPUT_REPORT:
req = HIDIOCSINPUT(data.size());
break;
case HID_OUTPUT_REPORT:
req = HIDIOCSOUTPUT(data.size());
break;
case HID_FEATURE_REPORT:
req = HIDIOCSFEATURE(data.size());
break;
default:
HDF_LOGE("%{public}s: invalid report type", __func__);
return HID_DDK_INVALID_PARAMETER;
}
int32_t res = ioctl(fd, req, data.data());
if (res < 0) {
HDF_LOGE("%{public}s ioctl failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
return HID_DDK_SUCCESS;
}
int32_t LinuxHidOsAdapter::GetReport(int32_t fd, HidReportType reportType, std::vector<uint8_t>& data)
{
unsigned long int req = 0;
switch (reportType) {
case HID_INPUT_REPORT:
req = HIDIOCGINPUT(data.size());
break;
case HID_OUTPUT_REPORT:
req = HIDIOCGOUTPUT(data.size());
break;
case HID_FEATURE_REPORT:
req = HIDIOCGFEATURE(data.size());
break;
default:
HDF_LOGE("%{public}s: invalid report type", __func__);
return HID_DDK_INVALID_PARAMETER;
}
int32_t res = ioctl(fd, req, data.data());
if (res < 0) {
HDF_LOGE("%{public}s ioctl failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
return HID_DDK_SUCCESS;
}
int32_t LinuxHidOsAdapter::GetReportDescriptor(int32_t fd, std::vector<uint8_t>& data, uint32_t& bytesRead)
{
bytesRead = 0;
uint32_t descSize = 0;
int32_t ret = ioctl(fd, HIDIOCGRDESCSIZE, &descSize);
if (ret < 0) {
HDF_LOGE("%{public}s ioctl(HIDIOCGRDESCSIZE) failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
struct hidraw_report_descriptor desc;
(void)memset_s(&desc, sizeof(desc), 0x0, sizeof(desc));
desc.size = descSize;
ret = ioctl(fd, HIDIOCGRDESC, &desc);
if (ret < 0) {
HDF_LOGE("%{public}s ioctl(HIDIOCGRDESC) failed, errno=%{public}d", __func__, errno);
return HID_DDK_IO_ERROR;
}
uint32_t tempSize = desc.size < data.size() ? desc.size : data.size();
ret = memcpy_s(data.data(), data.size(), desc.value, tempSize);
if (ret < 0) {
HDF_LOGE("%{public}s memcpy_s failed, ret=%{public}d, data.size=%{public}d, tempSize=%{public}d", __func__,
ret, data.size(), tempSize);
return HID_DDK_MEMORY_ERROR;
}
bytesRead = tempSize;
return HID_DDK_SUCCESS;
}
} // V1_1
} // Ddk
} // Input
} // HDI
} // OHOS
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2025 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 "hid_liteos_adapter.h"
#include <hdf_base.h>
#include "input_uhdf_log.h"
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <sys/ioctl.h>
#include <poll.h>
#include <memory.h>
#include <securec.h>
#define HDF_LOG_TAG hid_liteos_adapter
namespace OHOS {
namespace HDI {
namespace Input {
namespace Ddk {
namespace V1_1 {
int32_t LiteosHidOsAdapter::GetRawInfo(int32_t fd, HidRawDevInfo& rawDevInfo)
{
return HID_DDK_INVALID_OPERATION;
}
int32_t LiteosHidOsAdapter::GetRawName(int32_t fd, std::vector<uint8_t>& data)
{
return HID_DDK_INVALID_OPERATION;
}
int32_t LinuxHidOsAdapter::GetPhysicalAddress(int32_t fd, std::vector<uint8_t>& data)
{
return HID_DDK_INVALID_OPERATION;
}
int32_t LinuxHidOsAdapter::GetRawUniqueId(int32_t fd, std::vector<uint8_t>& data)
{
return HID_DDK_INVALID_OPERATION;
}
int32_t SendReport(int32_t fd, HidReportType reportType, const std::vector<uint8_t>& data)
{
return HID_DDK_INVALID_OPERATION;
}
int32_t GetReport(int32_t fd, HidReportType reportType, std::vector<uint8_t>& data)
{
return HID_DDK_INVALID_OPERATION;
}
int32_t GetReportDescriptor(int32_t fd, std::vector<uint8_t>& data, uint32_t& bytesRead)
{
return HID_DDK_INVALID_OPERATION;
}
} // V1_1
} // Ddk
} // Input
} // HDI
} // OHOS
+18 -2
View File
@@ -1,4 +1,4 @@
# Copyright (c) 2022-2023 Huawei Device Co., Ltd.
# Copyright (c) 2022-2025 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
@@ -43,7 +43,23 @@ if (defined(ohos_lite)) {
deps += [ "unittest/udriver:input_udriver_unittest" ]
}
if (drivers_peripheral_input_feature_support_ddk) {
deps += [ "fuzztest/hidddk_fuzzer:fuzztest" ]
deps += [
"fuzztest/hidclose_fuzzer:fuzztest",
"fuzztest/hidddk_fuzzer:fuzztest",
"fuzztest/hidgetphysicaladdress_fuzzer:fuzztest",
"fuzztest/hidgetrawinfo_fuzzer:fuzztest",
"fuzztest/hidgetrawname_fuzzer:fuzztest",
"fuzztest/hidgetrawuniqueid_fuzzer:fuzztest",
"fuzztest/hidgetreport_fuzzer:fuzztest",
"fuzztest/hidgetreportdescriptor_fuzzer:fuzztest",
"fuzztest/hidinit_fuzzer:fuzztest",
"fuzztest/hidopen_fuzzer:fuzztest",
"fuzztest/hidreadtimeout_fuzzer:fuzztest",
"fuzztest/hidrelease_fuzzer:fuzztest",
"fuzztest/hidsendreport_fuzzer:fuzztest",
"fuzztest/hidsetnonblocking_fuzzer:fuzztest",
"fuzztest/hidwrite_fuzzer:fuzztest",
]
}
}
}
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidCloseFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidclose_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidclose_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidCloseFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidclose_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidCloseFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_CLOSE, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidCloseFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* 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_CLOSE_FUZZER_H
#define HID_CLOSE_FUZZER_H
#define FUZZ_PROJECT_NAME "hidclose_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
+3 -3
View File
@@ -1,4 +1,4 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Copyright (c) 2023-2025 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
@@ -31,8 +31,8 @@ ohos_fuzztest("HidDdkFuzzTest") {
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.0",
"drivers_interface_input:libhid_ddk_stub_1.0",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 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
@@ -17,10 +17,10 @@
#include <cstdint>
#include "hdf_log.h"
#include "hidddk_fuzzer.h"
#include "v1_0/hid_ddk_stub.h"
#include "v1_0/ihid_ddk.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_0;
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
constexpr size_t THRESHOLD = 10;
@@ -32,7 +32,7 @@ constexpr int32_t THIRD_BIT = 3;
constexpr int32_t ZERO_MOVE_LEN = 24;
constexpr int32_t FIRST_MOVE_LEN = 16;
constexpr int32_t SECOND_MOVE_LEN = 8;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_0.IHidDdk";
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
uint32_t Convert2Uint32(const uint8_t *ptr)
{
@@ -64,12 +64,12 @@ bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<IHidDdk> hidDdkInterface = IHidDdk::Get(false);
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<HidDdkStub> hidDdk = new HidDdkStub(hidDdkInterface);
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidGetPhysicalAddressFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidgetphysicaladdress_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidgetphysicaladdress_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidGetPhysicalAddressFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 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 "hidgetphysicaladdress_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidGetPhysicalAddressFuzzTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_GET_PHYSICAL_ADDRESS, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidGetPhysicalAddressFuzzTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_GETPHYSICALADDRESS_FUZZER_H
#define HID_GETPHYSICALADDRESS_FUZZER_H
#define FUZZ_PROJECT_NAME "hidgetphysicaladdress_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidGetRawInfoFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidgetrawinfo_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidgetrawinfo_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidGetRawInfoFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 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 "hidgetrawinfo_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidGetRawInfoFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_GET_RAW_INFO, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidGetRawInfoFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_GETRAWINFO_FUZZER_H
#define HID_GETRAWINFO_FUZZER_H
#define FUZZ_PROJECT_NAME "hidgetrawinfo_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidGetRawNameFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidgetrawname_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidgetrawname_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidGetRawNameFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 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 "hidgetrawname_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidGetRawNameFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_GET_RAW_NAME, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidGetRawNameFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_GETRAWNAME_FUZZER_H
#define HID_GETRAWNAME_FUZZER_H
#define FUZZ_PROJECT_NAME "hidgetrawname_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidGetRawUniqueIdFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidgetrawuniqueid_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidgetrawuniqueid_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidGetRawUniqueIdFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 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 "hidgetrawuniqueid_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidGetRawUniqueIdFuzzTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_GET_RAW_UNIQUE_ID, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidGetRawUniqueIdFuzzTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_GETRAWUNIQUEID_FUZZER_H
#define HID_GETRAWUNIQUEID_FUZZER_H
#define FUZZ_PROJECT_NAME "hidgetrawuniqueid_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidGetReportFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidgetreport_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidgetreport_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidGetReportFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 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 "hidgetreport_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidGetReportFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_GET_REPORT, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidGetReportFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_GETREPORT_FUZZER_H
#define HID_GETREPORT_FUZZER_H
#define FUZZ_PROJECT_NAME "hidgetreport_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidGetReportDescriptorFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidgetreportdescriptor_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidgetreportdescriptor_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidGetReportDescriptorFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 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 "hidgetreportdescriptor_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidGetReportDescriptorFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_GET_REPORT_DESCRIPTOR, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidGetReportDescriptorFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_GETREPORTDESCRIPTOR_FUZZER_H
#define HID_GETREPORTDESCRIPTOR_FUZZER_H
#define FUZZ_PROJECT_NAME "hidgetreportdescriptor_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidInitFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidinit_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidinit_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidInitFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidinit_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidInitFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_INIT, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidInitFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* 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_INIT_FUZZER_H
#define HID_INIT_FUZZER_H
#define FUZZ_PROJECT_NAME "hidinit_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidOpenFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidopen_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidopen_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidOpenFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidopen_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidOpenFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_OPEN, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidOpenFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* 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_OPEN_FUZZER_H
#define HID_OPEN_FUZZER_H
#define FUZZ_PROJECT_NAME "hidopen_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidReadTimeoutFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidreadtimeout_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidreadtimeout_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidReadTimeoutFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidreadtimeout_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidReadTimeoutFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_READ_TIMEOUT, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidReadTimeoutFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* 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_READTIMEOUT_FUZZER_H
#define HID_READTIMEOUT_FUZZER_H
#define FUZZ_PROJECT_NAME "hidreadtimeout_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidReleaseFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidrelease_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidrelease_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidReleaseFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidrelease_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidReleaseFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_RELEASE, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidReleaseFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* 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_RELEASE_FUZZER_H
#define HID_RELEASE_FUZZER_H
#define FUZZ_PROJECT_NAME "hidrelease_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidSendReportFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidsendreport_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidsendreport_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidSendReportFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidsendreport_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidSendReportFuzzTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_SEND_REPORT, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidSendReportFuzzTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_SENDREPORT_FUZZER_H
#define HID_SENDREPORT_FUZZER_H
#define FUZZ_PROJECT_NAME "hidsendreport_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidSetNonBlockingFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidsetnonblocking_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidsetnonblocking_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidSetNonBlockingFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidsetnonblocking_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidSetNonBlockingFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_SET_NON_BLOCKING, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidSetNonBlockingFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2025 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_SETNONBLOCKING_FUZZER_H
#define HID_SETNONBLOCKING_FUZZER_H
#define FUZZ_PROJECT_NAME "hidsetnonblocking_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
@@ -0,0 +1,47 @@
# Copyright (c) 2025 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/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "drivers_peripheral_input/drivers_peripheral_input"
ohos_fuzztest("HidWriteFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "./../hidwrite_fuzzer"
include_dirs = [ "./" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "hidwrite_fuzzer.cpp" ]
external_deps = [
"c_utils:utils",
"drivers_interface_input:hid_ddk_idl_headers",
"drivers_interface_input:libhid_ddk_proxy_1.1",
"drivers_interface_input:libhid_ddk_stub_1.1",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":HidWriteFuzzTest" ]
}
@@ -0,0 +1,14 @@
# Copyright (c) 2025 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.
FUZZ
@@ -0,0 +1,73 @@
/*
* 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 "hidwrite_fuzzer.h"
#include <cstddef>
#include <cstdint>
#include "hdf_log.h"
#include "v1_1/hid_ddk_stub.h"
#include "v1_1/ihid_ddk.h"
using namespace OHOS::HDI::Input::Ddk::V1_1;
namespace OHOS {
namespace HID {
constexpr size_t THRESHOLD = 10;
constexpr int32_t OFFSET = 4;
const std::u16string HID_INTERFACE_TOKEN = u"ohos.hdi.input.ddk.v1_1.IHidDdk";
bool HidWriteFuzzerTest(const uint8_t *rawData, size_t size)
{
if (rawData == nullptr) {
HDF_LOGE("%{public}s: rawData is null", __func__);
return false;
}
rawData = rawData + OFFSET;
size = size - OFFSET;
MessageParcel data;
data.WriteInterfaceToken(HID_INTERFACE_TOKEN);
data.WriteBuffer(rawData, size);
data.RewindRead(0);
MessageParcel reply;
MessageOption option;
sptr<OHOS::HDI::Input::Ddk::V1_1::IHidDdk> hidDdkInterface = OHOS::HDI::Input::Ddk::V1_1::IHidDdk::Get(false);
if (hidDdkInterface == nullptr) {
HDF_LOGE("%{public}s: get hidDdkInterface failed", __func__);
return false;
}
sptr<OHOS::HDI::Input::Ddk::V1_1::HidDdkStub> hidDdk = new OHOS::HDI::Input::Ddk::V1_1::HidDdkStub(hidDdkInterface);
if (hidDdk == nullptr) {
HDF_LOGE("%{public}s: new hidDdk failed", __func__);
return false;
}
hidDdk->OnRemoteRequest(CMD_HID_DDK_WRITE, data, reply, option);
return true;
}
} // namespace HID
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size < OHOS::HID::THRESHOLD) {
return 0;
}
OHOS::HID::HidWriteFuzzerTest(data, size);
return 0;
}
@@ -0,0 +1,21 @@
/*
* 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_WRITE_FUZZER_H
#define HID_WRITE_FUZZER_H
#define FUZZ_PROJECT_NAME "hidwrite_fuzzer"
#endif
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2025 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.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>
+3
View File
@@ -53,6 +53,9 @@
"inner_kits": [
{
"name":"//drivers/peripheral/usb/hdi_service:libusb_interface_service_1.2"
},
{
"name":"//drivers/peripheral/usb/ddk:libusb_pnp_manager"
}
]
}
+8
View File
@@ -21,8 +21,16 @@
extern "C" {
#endif
#define SYSFS_DEVICES_DIR "/sys/bus/usb/devices/"
typedef struct DevInterfaceInfo {
uint32_t busNum;
uint32_t devNum;
uint8_t intfNum;
} DevInterfaceInfo;
uint64_t DdkSysfsMakeDevAddr(uint32_t busNum, uint32_t devNum);
int32_t DdkSysfsGetDevice(const char *deviceDir, struct UsbPnpNotifyMatchInfoTable *device);
int32_t DdkSysfsGetDevNodePath(DevInterfaceInfo *devInfo, const char *prefix, char *buff, uint32_t buffSize);
#ifdef __cplusplus
}
#endif
+209 -3
View File
@@ -20,6 +20,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <regex.h>
#include "hdf_log.h"
#include "securec.h"
@@ -29,12 +30,17 @@
#define PROPERTY_MAX_LEN 128
#define HDF_LOG_TAG usb_ddk_sysfs_dev
const int DEC_BASE = 10;
const int HEX_BASE = 16;
static inline int32_t DdkSysfsGetBase(const char *propName)
{
if (strcmp(propName, "idProduct") == 0 || strcmp(propName, "idVendor") == 0) {
return 16; // 16 means hexadecimal
if (strcmp(propName, "idProduct") == 0 || strcmp(propName, "idVendor") == 0 ||
strcmp(propName, "bInterfaceNumber") == 0 || strcmp(propName, "bInterfaceProtocol") == 0 ||
strcmp(propName, "bInterfaceClass") == 0 || strcmp(propName, "bInterfaceSubClass") == 0) {
return HEX_BASE;
}
return 10; // 10 means decimal
return DEC_BASE;
}
inline uint64_t DdkSysfsMakeDevAddr(uint32_t busNum, uint32_t devNum)
@@ -227,5 +233,205 @@ int32_t DdkSysfsGetDevice(const char *deviceDir, struct UsbPnpNotifyMatchInfoTab
return ret;
}
return HDF_SUCCESS;
}
static int32_t DdkSysfsFindDevPath(uint32_t busNum, uint32_t devNum, char *buff, uint32_t buffSize)
{
if (buff == NULL || buffSize == 0) {
HDF_LOGE("%{public}s: invalid param", __func__);
return HDF_ERR_INVALID_PARAM;
}
int32_t ret = HDF_ERR_OUT_OF_RANGE;
DIR *dir = opendir(SYSFS_DEVICES_DIR);
if (dir == NULL) {
HDF_LOGE("%{public}s: opendir failed sysfsDevDir:%{public}s", __func__, SYSFS_DEVICES_DIR);
return HDF_ERR_BAD_FD;
}
struct dirent *devHandle;
while ((devHandle = readdir(dir)) != NULL) {
if ((uint32_t)strtol(devHandle->d_name, NULL, DEC_BASE) != busNum || strchr(devHandle->d_name, ':')) {
continue;
}
int64_t value = 0;
ret = DdkSysfsReadProperty(devHandle->d_name, "devnum", &value, INT32_MAX);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: retrieve devnum failed:%{public}d", __func__, ret);
break;
}
if ((uint32_t)value != devNum) {
continue;
}
errno_t err = strncpy_s(buff, buffSize, devHandle->d_name, buffSize - 1);
if (err != 0) {
HDF_LOGE("%{public}s: strncpy_s error for devHandle->d_name:%{public}s", __func__, devHandle->d_name);
break;
}
break;
}
closedir(dir);
return ret;
}
static int32_t DdkSysfsFindIntfPath(char *deviceDir, uint8_t intfNum, char *buff, uint32_t buffSize)
{
if (deviceDir == NULL || buff == NULL || buffSize == 0) {
HDF_LOGE("%{public}s: invalid param", __func__);
return HDF_ERR_INVALID_PARAM;
}
int32_t ret = HDF_ERR_OUT_OF_RANGE;
int32_t num = sprintf_s(buff, buffSize, "%s%s/", SYSFS_DEVICES_DIR, deviceDir);
if (num <= 0) {
HDF_LOGE("%{public}s: sprintf_s error deviceDir:%{public}s", __func__, deviceDir);
return HDF_FAILURE;
}
DIR *dir = opendir(buff);
if (dir == NULL) {
HDF_LOGE("%{public}s: opendir failed buff:%{public}s", __func__, buff);
return HDF_ERR_BAD_FD;
}
struct dirent *devHandle;
while ((devHandle = readdir(dir)) != NULL) {
if (strncmp(devHandle->d_name, deviceDir, strlen(deviceDir)) != 0) {
continue;
}
struct UsbPnpNotifyInterfaceInfo intf;
ret = DdkSysfsGetInterface(deviceDir, devHandle->d_name, &intf);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: DdkSysfsGetInterface failed:%{public}d", __func__, ret);
break;
}
if (intf.interfaceNumber != intfNum) {
continue;
}
errno_t err = strncat_s(buff, buffSize, devHandle->d_name, buffSize - strlen(buff) - 1);
if (err == 0) {
ret = HDF_SUCCESS;
break;
}
}
closedir(dir);
return ret;
}
static bool DdkCheckProductDir(char *inputString)
{
if (inputString == NULL) {
HDF_LOGE("%{public}s: invalid param", __func__);
return false;
}
regex_t regex;
const char *pattern = "^[0-9A-F]{4}:[0-9A-F]{4}:[0-9A-F]{4}\\.[0-9A-F]{4}$";
int32_t ret = regcomp(&regex, pattern, REG_EXTENDED);
if (ret != 0) {
HDF_LOGE("%{public}s: Could not compile regex", __func__);
return false;
}
ret = regexec(&regex, inputString, 0, NULL, 0);
regfree(&regex);
return ret == 0;
}
static int32_t DdkSysfsFindDevNodeName(char *path, const char *prefix, char *buff, uint32_t buffSize)
{
if (path == NULL || prefix == NULL || buff == NULL || buffSize == 0) {
HDF_LOGE("%{public}s: invalid param", __func__);
return HDF_ERR_INVALID_PARAM;
}
int32_t ret = HDF_ERR_OUT_OF_RANGE;
DIR *dir = opendir(path);
if (dir == NULL) {
HDF_LOGE("%{public}s: opendir failed path:%{public}s", __func__, path);
return HDF_ERR_BAD_FD;
}
struct dirent *devHandle;
while ((devHandle = readdir(dir)) != NULL) {
char *pSubStrOffset = strstr(devHandle->d_name, prefix);
if (pSubStrOffset != NULL && strlen(devHandle->d_name) > strlen(prefix)) {
errno_t err = strncpy_s(buff, buffSize, devHandle->d_name, buffSize - 1);
if (err != 0) {
HDF_LOGE("%{public}s: strncpy_s error for devHandle->d_name:%{public}s", __func__, devHandle->d_name);
ret = HDF_FAILURE;
break;
}
ret = HDF_SUCCESS;
break;
}
if (!DdkCheckProductDir(devHandle->d_name) && strcmp(devHandle->d_name, prefix) != 0) {
continue;
}
char subPath[SYSFS_PATH_LEN] = { 0x00 };
int32_t num = sprintf_s(subPath, sizeof(subPath), "%s/%s", path, devHandle->d_name);
if (num <= 0) {
HDF_LOGE("%{public}s: sprintf_s error devHandle->d_name:%{public}s", __func__, devHandle->d_name);
ret = HDF_FAILURE;
break;
}
ret = DdkSysfsFindDevNodeName(subPath, prefix, buff, buffSize);
if (ret != HDF_ERR_OUT_OF_RANGE) {
HDF_LOGI("%{public}s: subPath:%{public}s", __func__, subPath);
break;
}
}
closedir(dir);
return ret;
}
int32_t DdkSysfsGetDevNodePath(DevInterfaceInfo *devInfo, const char *prefix, char *buff, uint32_t buffSize)
{
if (devInfo == NULL || buff == NULL || buffSize == 0) {
HDF_LOGE("%{public}s: invalid param", __func__);
return HDF_ERR_INVALID_PARAM;
}
char devicePath[SYSFS_PATH_LEN] = { 0x00 };
int32_t ret = DdkSysfsFindDevPath(devInfo->busNum, devInfo->devNum, devicePath, sizeof(devicePath));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: retrieve device path failed, ret:%{public}d", __func__, ret);
return ret;
}
char fullPath[SYSFS_PATH_LEN] = { 0x00 };
ret = DdkSysfsFindIntfPath(devicePath, devInfo->intfNum, fullPath, sizeof(fullPath));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: retrieve interface path failed, ret:%{public}d", __func__, ret);
return ret;
}
HDF_LOGI("%{public}s: fullPath [%{public}s]", __func__, fullPath);
char nodeName[SYSFS_PATH_LEN] = { 0x00 };
ret = DdkSysfsFindDevNodeName(fullPath, prefix, nodeName, sizeof(nodeName));
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: retrieve device file path failed, ret:%{public}d", __func__, ret);
return ret;
}
int32_t num = sprintf_s(buff, buffSize, "/dev/%s", nodeName);
if (num <= 0) {
HDF_LOGE("%{public}s: sprintf_s error nodeName:%{public}s", __func__, nodeName);
return HDF_FAILURE;
}
HDF_LOGI("%{public}s: devPath [%{public}s]", __func__, buff);
return HDF_SUCCESS;
}
+44
View File
@@ -17,6 +17,7 @@ if (defined(ohos_lite)) {
import("//build/config/sanitizers/sanitizers.gni")
import("//build/test.gni")
import("./../../../../../hdf_core/adapter/uhdf2/uhdf.gni")
import("./../../../usb.gni")
}
if (defined(ohos_lite)) {
@@ -254,9 +255,52 @@ if (defined(ohos_lite)) {
external_deps = [ "hilog:libhilog" ]
}
}
ohos_unittest("ddk_sysfs_device_test") {
module_out_path = module_output_path
include_dirs = [
"./../../../ddk/common/include",
"./../../../ddk/device/include",
"./../../../ddk/host/include",
"./../../../hdf_usb/include",
"./../../../interfaces/ddk/common",
"./../../../interfaces/ddk/host",
"./../../../interfaces/ddk/device",
"./../host_sdk",
"./../../../utils/include",
]
sources = [
"./ddk_sysfs_device_test.cpp",
"./usb_device_info_parser.cpp",
]
deps = [ "./../../../ddk:libusb_core" ]
if (is_standard_system) {
external_deps = [
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"c_utils:utils",
"hdf_core:libhdf_host",
"hdf_core:libhdf_ipc_adapter",
"hdf_core:libhdf_utils",
"hilog:libhilog",
"ipc:ipc_single",
]
if (usb_c_utils_enable) {
external_deps += [ "c_utils:utils" ]
}
} else {
external_deps = [ "hilog:libhilog" ]
}
}
group("hdf_unittest_usb_host") {
testonly = true
deps = [
":ddk_sysfs_device_test",
":usb_host_sdk_if_test",
":usb_host_sdk_if_test_io",
":usb_raw_sdk_if_test",
@@ -0,0 +1,191 @@
/*
* Copyright (c) 2025 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 <cstdio>
#include <cstring>
#include <gtest/gtest.h>
#include <unistd.h>
#include "usb_device_info_parser.h"
#include "accesstoken_kit.h"
#include "nativetoken_kit.h"
#include "token_setproc.h"
#include "hdf_base.h"
#include "hdf_log.h"
#include "osal_mem.h"
#include "osal_time.h"
#include "securec.h"
#include "usb_ddk_interface.h"
#include "ddk_sysfs_device.h"
using namespace std;
using namespace testing::ext;
namespace {
class DdkSysfsDeviceTest : public testing::Test {
};
/**
* @tc.number : DdkSysfsGetDevNameTestForHidraw001
* @tc.name :
* @tc.type : PERF
* @tc.level : Level 1
*/
HWTEST_F(DdkSysfsDeviceTest, DdkSysfsGetDevNameTestForHidraw001, TestSize.Level1)
{
UsbDeviceInfoParser parser;
std::optional<UsbDeviceInfo> dev = parser.Find("USB Optical Mouse");
if (!dev.has_value()) {
std::cout << "DdkGetDevInfos failed!" << std::endl;
return;
}
std::cout << "busNum:" << dev.value().busNum << " devNum:" << dev.value().devNum << std::endl;
DevInterfaceInfo devInfo;
devInfo.busNum = dev.value().busNum;
devInfo.devNum = dev.value().devNum;
devInfo.intfNum = 0;
char devNodePath[NAME_MAX] = { 0x00 };
int32_t ret = DdkSysfsGetDevNodePath(&devInfo, "hidraw", devNodePath, sizeof(devNodePath));
EXPECT_EQ(HDF_SUCCESS, ret);
EXPECT_EQ("/dev/hidraw0", std::string(devNodePath));
}
/**
* @tc.number : DdkSysfsGetDevNameTestForHidraw002
* @tc.name :
* @tc.type : PERF
* @tc.level : Level 1
*/
HWTEST_F(DdkSysfsDeviceTest, DdkSysfsGetDevNameTestForHidraw002, TestSize.Level1)
{
UsbDeviceInfoParser parser;
std::optional<UsbDeviceInfo> dev = parser.Find("USB (Serial|UART)");
if (!dev.has_value()) {
std::cout << "DdkGetDevInfos failed!" << std::endl;
return;
}
std::cout << "busNum:" << dev.value().busNum << " devNum:" << dev.value().devNum << std::endl;
DevInterfaceInfo devInfo;
devInfo.busNum = dev.value().busNum;
devInfo.devNum = dev.value().devNum;
devInfo.intfNum = 0;
char devNodePath[NAME_MAX] = { 0x00 };
int32_t ret = DdkSysfsGetDevNodePath(&devInfo, "hidraw", devNodePath, sizeof(devNodePath));
EXPECT_EQ(HDF_ERR_OUT_OF_RANGE, ret);
}
/**
* @tc.number : DdkSysfsGetDevNameTestForHidraw003
* @tc.name :
* @tc.type : PERF
* @tc.level : Level 1
*/
HWTEST_F(DdkSysfsDeviceTest, DdkSysfsGetDevNameTestForHidraw003, TestSize.Level1)
{
UsbDeviceInfoParser parser;
std::optional<UsbDeviceInfo> dev = parser.Find("USB Optical Mouse");
if (!dev.has_value()) {
std::cout << "DdkGetDevInfos failed!" << std::endl;
return;
}
std::cout << "busNum:" << dev.value().busNum << " devNum:" << dev.value().devNum << std::endl;
DevInterfaceInfo devInfo;
devInfo.busNum = dev.value().busNum;
devInfo.devNum = dev.value().devNum;
devInfo.intfNum = 2;
char devNodePath[NAME_MAX] = { 0x00 };
int32_t ret = DdkSysfsGetDevNodePath(&devInfo, "hidraw", devNodePath, sizeof(devNodePath));
EXPECT_EQ(HDF_ERR_OUT_OF_RANGE, ret);
}
/**
* @tc.number : DdkSysfsGetDevNameTestForUsbSerial001
* @tc.name :
* @tc.type : PERF
* @tc.level : Level 1
*/
HWTEST_F(DdkSysfsDeviceTest, DdkSysfsGetDevNameTestForUsbSerial001, TestSize.Level1)
{
UsbDeviceInfoParser parser;
std::optional<UsbDeviceInfo> dev = parser.Find("USB Serial");
if (!dev.has_value()) {
std::cout << "DdkGetDevInfos failed!" << std::endl;
return;
}
std::cout << "busNum:" << dev.value().busNum << " devNum:" << dev.value().devNum << std::endl;
DevInterfaceInfo devInfo;
devInfo.busNum = dev.value().busNum;
devInfo.devNum = dev.value().devNum;
devInfo.intfNum = 0;
char devNodePath[NAME_MAX] = { 0x00 };
int32_t ret = DdkSysfsGetDevNodePath(&devInfo, "ttyUSB", devNodePath, sizeof(devNodePath));
EXPECT_EQ(HDF_SUCCESS, ret);
EXPECT_EQ("/dev/ttyUSB0", std::string(devNodePath));
}
/**
* @tc.number : DdkSysfsGetDevNameTestForUsbSerial002
* @tc.name :
* @tc.type : PERF
* @tc.level : Level 1
*/
HWTEST_F(DdkSysfsDeviceTest, DdkSysfsGetDevNameTestForUsbSerial002, TestSize.Level1)
{
UsbDeviceInfoParser parser;
std::optional<UsbDeviceInfo> dev = parser.Find("USB Optical Mouse");
if (!dev.has_value()) {
std::cout << "DdkGetDevInfos failed!" << std::endl;
return;
}
std::cout << "busNum:" << dev.value().busNum << " devNum:" << dev.value().devNum << std::endl;
DevInterfaceInfo devInfo;
devInfo.busNum = dev.value().busNum;
devInfo.devNum = dev.value().devNum;
devInfo.intfNum = 0;
char devNodePath[NAME_MAX] = { 0x00 };
int32_t ret = DdkSysfsGetDevNodePath(&devInfo, "ttyUSB", devNodePath, sizeof(devNodePath));
EXPECT_EQ(HDF_ERR_OUT_OF_RANGE, ret);
}
/**
* @tc.number : DdkSysfsGetDevNameTestForUsbSerial003
* @tc.name :
* @tc.type : PERF
* @tc.level : Level 1
*/
HWTEST_F(DdkSysfsDeviceTest, DdkSysfsGetDevNameTestForUsbSerial003, TestSize.Level1)
{
UsbDeviceInfoParser parser;
std::optional<UsbDeviceInfo> dev = parser.Find("USB Serial");
if (!dev.has_value()) {
std::cout << "DdkGetDevInfos failed!" << std::endl;
return;
}
std::cout << "busNum:" << dev.value().busNum << " devNum:" << dev.value().devNum << std::endl;
DevInterfaceInfo devInfo;
devInfo.busNum = dev.value().busNum;
devInfo.devNum = dev.value().devNum;
devInfo.intfNum = 2;
char devNodePath[NAME_MAX] = { 0x00 };
int32_t ret = DdkSysfsGetDevNodePath(&devInfo, "ttyUSB", devNodePath, sizeof(devNodePath));
EXPECT_EQ(HDF_ERR_OUT_OF_RANGE, ret);
}
} // namespace
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2025 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 <iostream>
#include <sstream>
#include "usb_device_info_parser.h"
typedef enum {
POSTION_ONE = 1,
POSTION_TWO = 2,
} POSITION;
UsbDeviceInfoParser::UsbDeviceInfoParser()
: devInfoRegex_(".*Bus=([0-9]+).*Dev#=\\s*([0-9]+).*")
{
}
std::optional<UsbDeviceInfo> UsbDeviceInfoParser::Find(const std::string& productRegex)
{
std::ifstream filePath("/sys/kernel/debug/usb/devices");
std::string line;
UsbDeviceInfo deviceInfo;
const std::string prefix = "S: Product=";
while (std::getline(filePath, line)) {
if (line.empty()) {
continue;
}
if (line[0] == 'T') {
std::smatch match;
if (!std::regex_match(line, match, devInfoRegex_)) {
std::cout << __func__ << ":" << __LINE__ << " regex_match failed" << std::endl;
return std::nullopt;
}
deviceInfo.busNum = std::stoi(match.str(POSTION_ONE));
deviceInfo.devNum = std::stoi(match.str(POSTION_TWO));
}
if (line.find(prefix) == std::string::npos) {
continue;
}
auto product = line.substr(prefix.size(), line.size());
std::smatch match;
if (!std::regex_match(product, match, std::regex(productRegex))) {
std::cout << __func__ << ":" << __LINE__ << " regex_match [" << product << "] with ["
<< productRegex << "] failed" << std::endl;
continue;
}
deviceInfo.productNum = product;
return std::optional<UsbDeviceInfo>(deviceInfo);
}
return std::nullopt;
}
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2025 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 USB_DEVICE_INFO_PARSER_H
#define USB_DEVICE_INFO_PARSER_H
#include <fstream>
#include <regex>
#include <optional>
#include <string>
struct UsbDeviceInfo {
uint32_t busNum;
uint32_t devNum;
std::string productNum;
};
class UsbDeviceInfoParser {
public:
UsbDeviceInfoParser();
std::optional<UsbDeviceInfo> Find(const std::string& productRegex);
private:
std::regex devInfoRegex_;
};
#endif