Browse Source

RAWHID Support

Needed HID Parser to support Bidirectional Transfers

The HidParser code was setup such that the claim for a report, the caller could say I want to claim the whole thinig and allowed callback functions for processing of in buffer and out buffer.

Allow RawHID to contribute Transfer_t

Since RawHID may need more resources than most, maybe it should contribute the additional structures

The constructor for a RAWHID object allows you to specify the top usage
that it wishes to connect to.  I used this for example to be able to
connect to a Teensy with the RAWHID associated with emulating the
Serial object.

If a HID Input class says that it wants to claim the whole interface, I
reuse the buffer associated with holding the HID descriptor and use it
for output buffers.
main
Kurt Eckhardt 7 years ago
parent
commit
8aa67ff939
10 changed files with 305 additions and 30 deletions
  1. +55
    -5
      USBHost_t36.h
  2. +56
    -2
      examples/Mouse/Mouse.ino
  3. +0
    -0
      examples/Mouse/defs.h
  4. +0
    -0
      examples/SerialTest/defs.h
  5. +56
    -11
      hid.cpp
  6. +4
    -4
      joystick.cpp
  7. +4
    -4
      keyboardHIDExtras.cpp
  8. +6
    -0
      keywords.txt
  9. +4
    -4
      mouse.cpp
  10. +120
    -0
      rawhid.cpp

+ 55
- 5
USBHost_t36.h View File

typedef struct Device_struct Device_t; typedef struct Device_struct Device_t;
typedef struct Pipe_struct Pipe_t; typedef struct Pipe_struct Pipe_t;
typedef struct Transfer_struct Transfer_t; typedef struct Transfer_struct Transfer_t;
typedef enum { CLAIM_NO=0, CLAIM_REPORT, CLAIM_INTERFACE} hidclaim_t;


// All USB device drivers inherit use these classes. // All USB device drivers inherit use these classes.
// Drivers build user-visible functionality on top // Drivers build user-visible functionality on top
static void disconnect_Device(Device_t *dev); static void disconnect_Device(Device_t *dev);
static void enumeration(const Transfer_t *transfer); static void enumeration(const Transfer_t *transfer);
static void driver_ready_for_device(USBDriver *driver); static void driver_ready_for_device(USBDriver *driver);
static volatile bool enumeration_busy;
public: // Maybe others may want/need to contribute memory example HID devices may want to add transfers.
static void contribute_Devices(Device_t *devices, uint32_t num); static void contribute_Devices(Device_t *devices, uint32_t num);
static void contribute_Pipes(Pipe_t *pipes, uint32_t num); static void contribute_Pipes(Pipe_t *pipes, uint32_t num);
static void contribute_Transfers(Transfer_t *transfers, uint32_t num); static void contribute_Transfers(Transfer_t *transfers, uint32_t num);
static void contribute_String_Buffers(strbuf_t *strbuf, uint32_t num); static void contribute_String_Buffers(strbuf_t *strbuf, uint32_t num);
static volatile bool enumeration_busy;
private: private:
static void isr(); static void isr();
static void convertStringDescriptorToASCIIString(uint8_t string_index, Device_t *dev, const Transfer_t *transfer); static void convertStringDescriptorToASCIIString(uint8_t string_index, Device_t *dev, const Transfer_t *transfer);


// Device drivers may inherit from this base class, if they wish to receive // Device drivers may inherit from this base class, if they wish to receive
// HID input data fully decoded by the USBHIDParser driver // HID input data fully decoded by the USBHIDParser driver
class USBHIDParser;

class USBHIDInput { class USBHIDInput {
public: public:
operator bool() { return (mydevice != nullptr); } operator bool() { return (mydevice != nullptr); }
{ return ((mydevice == nullptr) || (mydevice->strbuf == nullptr)) ? nullptr : &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]]; } { return ((mydevice == nullptr) || (mydevice->strbuf == nullptr)) ? nullptr : &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]]; }


private: private:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual bool hid_process_in_data(const Transfer_t *transfer) {return false;}
virtual bool hid_process_out_data(const Transfer_t *transfer) {return false;}
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax); virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value); virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end(); virtual void hid_input_end();


//-------------------------------------------------------------------------- //--------------------------------------------------------------------------



class USBHIDParser : public USBDriver { class USBHIDParser : public USBDriver {
public: public:
USBHIDParser(USBHost &host) { init(); } USBHIDParser(USBHost &host) { init(); }
static void driver_ready_for_hid_collection(USBHIDInput *driver); static void driver_ready_for_hid_collection(USBHIDInput *driver);
bool sendPacket(const uint8_t *buffer);
protected: protected:
enum { TOPUSAGE_LIST_LEN = 4 }; enum { TOPUSAGE_LIST_LEN = 4 };
enum { USAGE_LIST_LEN = 24 }; enum { USAGE_LIST_LEN = 24 };
USBHIDInput * find_driver(uint32_t topusage); USBHIDInput * find_driver(uint32_t topusage);
void parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len); void parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len);
void init(); void init();

// Atempt for RAWhid to take over processing of data
//
uint16_t inSize(void) {return in_size;}
uint16_t outSize(void) {return out_size;}

uint8_t activeSendMask(void) {return txstate;}

private: private:
Pipe_t *in_pipe; Pipe_t *in_pipe;
Pipe_t *out_pipe; Pipe_t *out_pipe;
Pipe_t mypipes[3] __attribute__ ((aligned(32))); Pipe_t mypipes[3] __attribute__ ((aligned(32)));
Transfer_t mytransfers[4] __attribute__ ((aligned(32))); Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
strbuf_t mystring_bufs[1]; strbuf_t mystring_bufs[1];
uint8_t txstate = 0;
uint8_t *tx1 = nullptr;
uint8_t *tx2 = nullptr;
bool hid_driver_claimed_control_ = false;
}; };


//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
enum {MAX_KEYS_DOWN=4}; enum {MAX_KEYS_DOWN=4};
// uint32_t buttons() { return buttons_; } // uint32_t buttons() { return buttons_; }
protected: protected:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax); virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value); virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end(); virtual void hid_input_end();
int getWheel() { return wheel; } int getWheel() { return wheel; }
int getWheelH() { return wheelH; } int getWheelH() { return wheelH; }
protected: protected:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax); virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value); virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end(); virtual void hid_input_end();
uint32_t getButtons() { return buttons; } uint32_t getButtons() { return buttons; }
int getAxis(uint32_t index) { return (index < (sizeof(axis)/sizeof(axis[0]))) ? axis[index] : 0; } int getAxis(uint32_t index) { return (index < (sizeof(axis)/sizeof(axis[0]))) ? axis[index] : 0; }
protected: protected:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax); virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value); virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end(); virtual void hid_input_end();
uint16_t wheelCircumference; // default is WHEEL_CIRCUMFERENCE (2122cm) uint16_t wheelCircumference; // default is WHEEL_CIRCUMFERENCE (2122cm)
}; };


//--------------------------------------------------------------------------

class RawHIDController : public USBHIDInput {
public:
RawHIDController(USBHost &host, uint32_t usage = 0) : fixed_usage_(usage) { init(); }
uint32_t usage(void) {return usage_;}
void attachReceive(bool (*f)(uint32_t usage, const uint8_t *data, uint32_t len)) {receiveCB = f;}
bool sendPacket(const uint8_t *buffer);
protected:
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual bool hid_process_in_data(const Transfer_t *transfer);
virtual bool hid_process_out_data(const Transfer_t *transfer);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end();
virtual void disconnect_collection(Device_t *dev);
private:
void init();
USBHIDParser *driver_;
enum { MAX_PACKET_SIZE = 64 };
bool (*receiveCB)(uint32_t usage, const uint8_t *data, uint32_t len) = nullptr;
uint8_t collections_claimed = 0;
//volatile bool hid_input_begin_ = false;
uint32_t fixed_usage_;
uint32_t usage_ = 0;

// See if we can contribute transfers
Transfer_t mytransfers[2] __attribute__ ((aligned(32)));

};


#endif #endif

+ 56
- 2
examples/Mouse/Mouse.ino View File

USBHIDParser hid5(myusb); USBHIDParser hid5(myusb);
MouseController mouse1(myusb); MouseController mouse1(myusb);
JoystickController joystick1(myusb); JoystickController joystick1(myusb);
RawHIDController rawhid1(myusb);
RawHIDController rawhid2(myusb, 0xffc90004);


USBDriver *drivers[] = {&hub1, &hub2, &hub3, &hub4, &keyboard1, &keyboard2, &hid1, &hid2, &hid3, &hid4, &hid5}; USBDriver *drivers[] = {&hub1, &hub2, &hub3, &hub4, &keyboard1, &keyboard2, &hid1, &hid2, &hid3, &hid4, &hid5};
#define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0])) #define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
bool driver_active[CNT_DEVICES] = {false, false, false, false}; bool driver_active[CNT_DEVICES] = {false, false, false, false};


// Lets also look at HID Input devices // Lets also look at HID Input devices
USBHIDInput *hiddrivers[] = {&mouse1, &joystick1};
USBHIDInput *hiddrivers[] = {&mouse1, &joystick1, &rawhid1, &rawhid2};
#define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0])) #define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
const char * hid_driver_names[CNT_DEVICES] = {"Mouse1","Joystick1"};
const char * hid_driver_names[CNT_DEVICES] = {"Mouse1","Joystick1", "RawHid1", "RawHid2"};
bool hid_driver_active[CNT_DEVICES] = {false, false}; bool hid_driver_active[CNT_DEVICES] = {false, false};




keyboard2.attachPress(OnPress); keyboard2.attachPress(OnPress);
hidextras.attachPress(OnHIDExtrasPress); hidextras.attachPress(OnHIDExtrasPress);
hidextras.attachRelease(OnHIDExtrasRelease); hidextras.attachRelease(OnHIDExtrasRelease);

rawhid1.attachReceive(OnReceiveHidData);
rawhid2.attachReceive(OnReceiveHidData);
} }




Serial.println(); Serial.println();
joystick1.joystickDataClear(); joystick1.joystickDataClear();
} }

// See if we have some RAW data
if (rawhid1) {
int ch;
uint8_t buffer[64];
uint8_t count_chars = 0;
memset(buffer, 0, sizeof(buffer));
if (Serial.available()) {
while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
buffer[count_chars++] = ch;
}
rawhid1.sendPacket(buffer);
}
}
} }




Serial.print(") key release:"); Serial.print(") key release:");
Serial.println(key, HEX); Serial.println(key, HEX);
} }

bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
// Called for maybe both HIDS for rawhid basic test. One is for the Teensy
// to output to Serial. while still having Raw Hid...
if (usage == 0xffc90004) {
// Lets trim off trailing null characters.
while ((len > 0) && (data[len-1] == 0)) {
len--;
}
if (len) {
Serial.print("RawHid Serial: ");
Serial.write(data, len);
}
} else {
Serial.print("RawHID data: ");
Serial.println(usage, HEX);
while (len) {
uint8_t cb = (len > 16)? 16 : len;
const uint8_t *p = data;
uint8_t i;
for (i = 0; i < cb; i++) {
Serial.printf("%02x ", *p++);
}
Serial.print(": ");
for (i = 0; i < cb; i++) {
Serial.write(((*data >= ' ')&&(*data <= '~'))? *data : '.');
data++;
}
len -= cb;
Serial.println();
}
}

return true;
}

+ 0
- 0
examples/Mouse/defs.h View File


+ 0
- 0
examples/SerialTest/defs.h View File


+ 56
- 11
hid.cpp View File

if (((endpoint1 & 0xF0) == 0x80) && ((endpoint2 & 0xF0) == 0)) { if (((endpoint1 & 0xF0) == 0x80) && ((endpoint2 & 0xF0) == 0)) {
// first endpoint is IN, second endpoint is OUT // first endpoint is IN, second endpoint is OUT
in_pipe = new_Pipe(dev, 3, endpoint1 & 0x0F, 1, size1, interval1); in_pipe = new_Pipe(dev, 3, endpoint1 & 0x0F, 1, size1, interval1);
//out_pipe = new_Pipe(dev, 3, endpoint2, 0, size2, interval2);
out_pipe = NULL; // TODO; fixme
out_pipe = new_Pipe(dev, 3, endpoint2, 0, size2, interval2);
in_size = size1; in_size = size1;
out_size = size2; out_size = size2;
} else if (((endpoint1 & 0xF0) == 0) && ((endpoint2 & 0xF0) == 0x80)) { } else if (((endpoint1 & 0xF0) == 0) && ((endpoint2 & 0xF0) == 0x80)) {
// first endpoint is OUT, second endpoint is IN // first endpoint is OUT, second endpoint is IN
in_pipe = new_Pipe(dev, 3, endpoint2 & 0x0F, 1, size2, interval2); in_pipe = new_Pipe(dev, 3, endpoint2 & 0x0F, 1, size2, interval2);
//out_pipe = new_Pipe(dev, 3, endpoint1, 0, size1, interval1);
out_pipe = NULL; // TODO; fixme
out_pipe = new_Pipe(dev, 3, endpoint1, 0, size1, interval1);
in_size = size2; in_size = size2;
out_size = size1; out_size = size1;
} else { } else {
return false; return false;
} }
//out_pipe->callback_function = out_callback;
out_pipe->callback_function = out_callback;
} }
in_pipe->callback_function = in_callback; in_pipe->callback_function = in_callback;
for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) { for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) {


void USBHIDParser::out_callback(const Transfer_t *transfer) void USBHIDParser::out_callback(const Transfer_t *transfer)
{ {
//println("USBHIDParser:: out_callback (static)");
if (transfer->driver) { if (transfer->driver) {
((USBHIDParser*)(transfer->driver))->out_data(transfer); ((USBHIDParser*)(transfer->driver))->out_data(transfer);
} }
Serial.println(); */ Serial.println(); */


print("HID: "); print("HID: ");
print(use_report_id);
print(" - ");
print_hexbytes(transfer->buffer, transfer->length); print_hexbytes(transfer->buffer, transfer->length);
const uint8_t *buf = (const uint8_t *)transfer->buffer; const uint8_t *buf = (const uint8_t *)transfer->buffer;
uint32_t len = transfer->length; uint32_t len = transfer->length;
if (use_report_id == false) {
parse(0x0100, buf, len);
} else {
if (len > 1) {
parse(0x0100 | buf[0], buf + 1, len - 1);

// See if the first top report wishes to bypass the
// parse...
if (!(topusage_drivers[0] && topusage_drivers[0]->hid_process_in_data(transfer))) {

if (use_report_id == false) {
parse(0x0100, buf, len);
} else {
if (len > 1) {
parse(0x0100 | buf[0], buf + 1, len - 1);
}
} }
} }
queue_Data_Transfer(in_pipe, report, in_size, this); queue_Data_Transfer(in_pipe, report, in_size, this);


void USBHIDParser::out_data(const Transfer_t *transfer) void USBHIDParser::out_data(const Transfer_t *transfer)
{ {
println("USBHIDParser:out_data called (instance)");
// A packet completed. lets mark it as done and call back
// to top reports handler. We unmark our checkmark to
// handle case where they may want to queue up another one.
if (transfer->buffer == tx1) txstate &= ~1;
if (transfer->buffer == tx2) txstate &= ~2;
if (topusage_drivers[0]) {
topusage_drivers[0]->hid_process_out_data(transfer);
}
} }


bool USBHIDParser::sendPacket(const uint8_t *buffer) {
if (!out_size || !out_pipe) return false;
if (!tx1) {
// Was not init before, for now lets put it at end of descriptor
// TODO: should verify that either don't exceed overlap descsize
// Or that we have taken over this device
tx1 = &descriptor[sizeof(descriptor) - out_size];
tx2 = tx1 - out_size;
}
if ((txstate & 3) == 3) return false; // both transmit buffers are full
uint8_t *p = tx1;
if ((txstate & 1) == 0) {
txstate |= 1;
} else {
txstate |= 2;
p = tx2;
}
// copy the users data into our out going buffer
memcpy(p, buffer, out_size);
println("USBHIDParser Send packet");
print_hexbytes(buffer, out_size);
queue_Data_Transfer(out_pipe, p, out_size, this);
println(" Queue_data transfer returned");
return true;
}


// This no-inputs parse is meant to be used when we first get the // This no-inputs parse is meant to be used when we first get the
// HID report descriptor. It finds all the top level collections // HID report descriptor. It finds all the top level collections
// and allows drivers to claim them. This is always where we // and allows drivers to claim them. This is always where we
{ {
println("find_driver"); println("find_driver");
USBHIDInput *driver = available_hid_drivers_list; USBHIDInput *driver = available_hid_drivers_list;
hidclaim_t claim_type;
while (driver) { while (driver) {
println(" driver ", (uint32_t)driver, HEX); println(" driver ", (uint32_t)driver, HEX);
if (driver->claim_collection(device, topusage)) {
if ((claim_type = driver->claim_collection(this, device, topusage)) != CLAIM_NO) {
if (claim_type == CLAIM_INTERFACE) hid_driver_claimed_control_ = true;
return driver; return driver;
} }
driver = driver->next; driver = driver->next;

+ 4
- 4
joystick.cpp View File







bool JoystickController::claim_collection(Device_t *dev, uint32_t topusage)
hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{ {
// only claim Desktop/Joystick and Desktop/Gamepad // only claim Desktop/Joystick and Desktop/Gamepad
if (topusage != 0x10004 && topusage != 0x10005) return false;
if (topusage != 0x10004 && topusage != 0x10005) return CLAIM_NO;
// only claim from one physical device // only claim from one physical device
if (mydevice != NULL && dev != mydevice) return false;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
mydevice = dev; mydevice = dev;
collections_claimed++; collections_claimed++;
anychange = true; // always report values on first read anychange = true; // always report values on first read
return true;
return CLAIM_REPORT;
} }


void JoystickController::disconnect_collection(Device_t *dev) void JoystickController::disconnect_collection(Device_t *dev)

+ 4
- 4
keyboardHIDExtras.cpp View File

#define TOPUSAGE_SYS_CONTROL 0x10080 #define TOPUSAGE_SYS_CONTROL 0x10080
#define TOPUSAGE_CONSUMER_CONTROL 0x0c0001 #define TOPUSAGE_CONSUMER_CONTROL 0x0c0001


bool KeyboardHIDExtrasController::claim_collection(Device_t *dev, uint32_t topusage)
hidclaim_t KeyboardHIDExtrasController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{ {
// Lets try to claim a few specific Keyboard related collection/reports // Lets try to claim a few specific Keyboard related collection/reports
//Serial.printf("KBH Claim %x\n", topusage); //Serial.printf("KBH Claim %x\n", topusage);
if ((topusage != TOPUSAGE_SYS_CONTROL) if ((topusage != TOPUSAGE_SYS_CONTROL)
&& (topusage != TOPUSAGE_CONSUMER_CONTROL) && (topusage != TOPUSAGE_CONSUMER_CONTROL)
) return false;
) return CLAIM_NO;
// only claim from one physical device // only claim from one physical device
//Serial.println("KeyboardHIDExtrasController claim collection"); //Serial.println("KeyboardHIDExtrasController claim collection");
if (mydevice != NULL && dev != mydevice) return false;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
mydevice = dev; mydevice = dev;
collections_claimed_++; collections_claimed_++;
return true;
return CLAIM_REPORT;
} }


void KeyboardHIDExtrasController::disconnect_collection(Device_t *dev) void KeyboardHIDExtrasController::disconnect_collection(Device_t *dev)

+ 6
- 0
keywords.txt View File

AntPlus KEYWORD1 AntPlus KEYWORD1
JoystickController KEYWORD1 JoystickController KEYWORD1
KeyboardHIDExtrasController KEYWORD1 KeyboardHIDExtrasController KEYWORD1
RawHIDController KEYWORD1


# Common Functions # Common Functions
Task KEYWORD2 Task KEYWORD2
USBHOST_SERIAL_8N2 LITERAL1 USBHOST_SERIAL_8N2 LITERAL1
USBHOST_SERIAL_8E1 LITERAL1 USBHOST_SERIAL_8E1 LITERAL1
USBHOST_SERIAL_8O1 LITERAL1 USBHOST_SERIAL_8O1 LITERAL1

# RAWHid
usage KEYWORD2
attachReceive KEYWORD2
sendPacket KEYWORD2

+ 4
- 4
mouse.cpp View File







bool MouseController::claim_collection(Device_t *dev, uint32_t topusage)
hidclaim_t MouseController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{ {
// only claim Desktop/Mouse // only claim Desktop/Mouse
if (topusage != 0x10002) return false;
if (topusage != 0x10002) return CLAIM_NO;
// only claim from one physical device // only claim from one physical device
if (mydevice != NULL && dev != mydevice) return false;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
mydevice = dev; mydevice = dev;
collections_claimed++; collections_claimed++;
return true;
return CLAIM_REPORT;
} }


void MouseController::disconnect_collection(Device_t *dev) void MouseController::disconnect_collection(Device_t *dev)

+ 120
- 0
rawhid.cpp View File

/* USB EHCI Host for Teensy 3.6
* Copyright 2017 Paul Stoffregen (paul@pjrc.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <Arduino.h>
#include "USBHost_t36.h" // Read this header first for key info

void RawHIDController::init()
{
USBHost::contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
USBHIDParser::driver_ready_for_hid_collection(this);
}

hidclaim_t RawHIDController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{
// only claim RAWHID devices currently: 16c0:0486
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("Rawhid Claim: %x:%x usage: %x\n", dev->idVendor, dev->idProduct, topusage);
#endif

if ((dev->idVendor != 0x16c0 || (dev->idProduct) != 0x486)) return CLAIM_NO;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
if (usage_) return CLAIM_NO; // Only claim one
if (fixed_usage_ && (fixed_usage_ != topusage)) return CLAIM_NO; // See if we want specific one and if so is it this one
mydevice = dev;
collections_claimed++;
usage_ = topusage;
driver_ = driver; // remember the driver.
return CLAIM_INTERFACE; // We wa
}

void RawHIDController::disconnect_collection(Device_t *dev)
{
if (--collections_claimed == 0) {
mydevice = NULL;
usage_ = 0;
}
}

bool RawHIDController::hid_process_in_data(const Transfer_t *transfer)
{
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHIDController::hid_process_in_data: %x\n", usage_);
#endif

if (receiveCB) {
return (*receiveCB)(usage_, (const uint8_t *)transfer->buffer, transfer->length);
}
return true;
}

bool RawHIDController::hid_process_out_data(const Transfer_t *transfer)
{
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHIDController::hid_process_out_data: %x\n", usage_);
#endif
return true;
}

bool RawHIDController::sendPacket(const uint8_t *buffer)
{
if (!driver_) return false;
return driver_->sendPacket(buffer);
}



void RawHIDController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
{
// These should not be called as we are claiming the whole interface and not
// allowing the parse to happen
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHID::hid_input_begin %x %x %x %x\n", topusage, type, lgmin, lgmax);
#endif
//hid_input_begin_ = true;
}

void RawHIDController::hid_input_data(uint32_t usage, int32_t value)
{
// These should not be called as we are claiming the whole interface and not
// allowing the parse to happen
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHID: usage=%X, value=%d", usage, value);
if ((value >= ' ') && (value <='~')) Serial.printf("(%c)", value);
Serial.println();
#endif
}

void RawHIDController::hid_input_end()
{
// These should not be called as we are claiming the whole interface and not
// allowing the parse to happen
#ifdef USBHOST_PRINT_DEBUG
Serial.println("RawHID::hid_input_end");
#endif
// if (hid_input_begin_) {
// hid_input_begin_ = false;
// }
}



Loading…
Cancel
Save