From 384eec9bc3cb8c8bc0d9862b6110df017fea874f Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Tue, 26 Jan 2010 23:34:23 +0000 Subject: [PATCH] Add "hid_decode" which uses the usb_hid HID parser to decode a report descriptor (which is conveniently stored by usb_hid to /tmp for each device you plug in). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35306 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/bin/Jamfile | 1 + src/bin/hid_decode/Jamfile | 23 +++++++++++ src/bin/hid_decode/UserlandHID.h | 9 +++++ src/bin/hid_decode/hid_decode.cpp | 64 +++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/bin/hid_decode/Jamfile create mode 100644 src/bin/hid_decode/UserlandHID.h create mode 100644 src/bin/hid_decode/hid_decode.cpp diff --git a/src/bin/Jamfile b/src/bin/Jamfile index 355d01ae88..77a45c608b 100644 --- a/src/bin/Jamfile +++ b/src/bin/Jamfile @@ -205,6 +205,7 @@ SubInclude HAIKU_TOP src bin fwcontrol ; SubInclude HAIKU_TOP src bin gawk ; SubInclude HAIKU_TOP src bin gdb ; SubInclude HAIKU_TOP src bin grep ; +SubInclude HAIKU_TOP src bin hid_decode ; SubInclude HAIKU_TOP src bin iasl ; SubInclude HAIKU_TOP src bin ideinfo ; SubInclude HAIKU_TOP src bin keymap ; diff --git a/src/bin/hid_decode/Jamfile b/src/bin/hid_decode/Jamfile new file mode 100644 index 0000000000..dfe2abe1ca --- /dev/null +++ b/src/bin/hid_decode/Jamfile @@ -0,0 +1,23 @@ +SubDir HAIKU_TOP src bin hid_decode ; + +local defines = [ FDefines USERLAND_HID ] ; +local driverDir = [ FDirName $(HAIKU_TOP) src add-ons kernel drivers input + usb_hid ] ; + +SubDirC++Flags $(defines) ; + +UsePrivateHeaders drivers ; +UseHeaders $(driverDir) ; + +BinCommand hid_decode : + HIDCollection.cpp + HIDParser.cpp + HIDReport.cpp + HIDReportItem.cpp + + hid_decode.cpp + : be +; + +SEARCH on [ FGristFiles HIDCollection.cpp HIDParser.cpp HIDReport.cpp + HIDReportItem.cpp ] = $(driverDir) ; diff --git a/src/bin/hid_decode/UserlandHID.h b/src/bin/hid_decode/UserlandHID.h new file mode 100644 index 0000000000..4962670a3b --- /dev/null +++ b/src/bin/hid_decode/UserlandHID.h @@ -0,0 +1,9 @@ +#ifndef HID_USERLAND_H +#define HID_USERLAND_H + +#include + +#define TRACE(x...) /*printf(x)*/ +#define TRACE_ALWAYS(x...) printf(x) + +#endif diff --git a/src/bin/hid_decode/hid_decode.cpp b/src/bin/hid_decode/hid_decode.cpp new file mode 100644 index 0000000000..1dcb3ff12a --- /dev/null +++ b/src/bin/hid_decode/hid_decode.cpp @@ -0,0 +1,64 @@ +/* + * Copyright 2010, Michael Lotz, mmlr@mlotz.ch. + * Distributed under the terms of the MIT License. + */ + +#include "HIDCollection.h" +#include "HIDParser.h" +#include "HIDReport.h" + +#include + +#include +#include +#include + + +int +main(int argc, char *argv[]) +{ + if (argc < 2) { + printf("usage: %s \n", argv[0]); + return 1; + } + + BFile file(argv[1], B_READ_ONLY); + if (!file.IsReadable()) { + printf("can't open file \"%s\" for reading\n", argv[1]); + return 2; + } + + off_t descriptorLength; + file.GetSize(&descriptorLength); + + uint8 *reportDescriptor = (uint8 *)malloc(descriptorLength); + if (reportDescriptor == NULL) { + printf("failed to allocate buffer of %lld bytes\n", descriptorLength); + return 3; + } + + ssize_t read = file.Read(reportDescriptor, descriptorLength); + if (read != descriptorLength) { + printf("failed to read file of %lld bytes: %s\n", descriptorLength, + strerror(read)); + return 4; + } + + HIDParser parser(NULL); + status_t result = parser.ParseReportDescriptor(reportDescriptor, + descriptorLength); + + free(reportDescriptor); + if (result != B_OK) { + printf("failed to parse descriptor: %s\n", strerror(result)); + return 5; + } + + for (uint32 i = 0; i < parser.CountReports(HID_REPORT_TYPE_ANY); i++) + parser.ReportAt(HID_REPORT_TYPE_ANY, i)->PrintToStream(); + + if (parser.RootCollection() != NULL) + parser.RootCollection()->PrintToStream(); + + return 0; +}