Skip to content

hidraw: Implement hid_error() - #125

Open
nh2 wants to merge 3 commits into
signal11:masterfrom
nh2:hidraw-error-messages
Open

nh2 wants to merge 3 commits into
signal11:masterfrom
nh2:hidraw-error-messages

Conversation

@nh2

@nh2 nh2 commented Jun 17, 2013

Copy link
Copy Markdown

Here is my first try at implementing error messages for the Linux/hidraw implementation.

  • I used a thread-local variable for non-device error messages.
  • Non-device errors can be obtained by passing NULL into hid_error as I suggested in What is hid_error good for? #123.
  • I belive to have covered all system calls that could fail; please check this.

A few extra questions:

  • I am not sure why in utf8_to_wchar_t the input is called utf8. As far as I know, the encoding used in mbstowcs depends on the current locale, if if I haven't set my locale to use UTF8, then the input is not UTF8.
  • The register_error equivalent in windows/hid.c doesn't use its argument const char *op. Is that intended?
  • In the Windows implementation, do functions like HidD_GetPreparsedData actually set the error code for GetLastError()?

@nh2

nh2 commented Jun 18, 2013

Copy link
Copy Markdown
Author

I have attached two commits to not crash on Windows as you mentioned (unfortunately I cannot test this) and to update the docs.

In the docs of hid_error, I wrote:

            Whether a function sets the last error is noted in its
            documentation. These functions will reset the last error
            to NULL before their execution.

It might be nicer to write and implement this instead:

            All hid_* functions (apart from hid_error() itself)
            update the value of hid_error().

In this case, all hid_* function would have to set the error to NULL at their beginning. This is probably more consistent and unsurprising (the fact that some functions (re)set the error while others just leave it unchanged, as they do now, can be surprising), but it would require changing the Windows implementation, which I cannot do reliably myself.

What do you think about this?

I'll take a look at this, but it may be a few days.

No problem.

@nh2

nh2 commented Aug 12, 2013

Copy link
Copy Markdown
Author

Any update on this?

@ynezz

ynezz commented Aug 19, 2013

Copy link
Copy Markdown

Hi Alan,

would you mind to change the API little bit? So there's some shared context amongst all API functions calls and there's no global variable used (there's currently libusb_context shared). This would make better error handling easier and library thread safe.

As nh2 already suggests, there's currently no way of knowing where did for example hid_enumerate() failed as there's no context and so hid_error() couldn't be used properly. I'm working on Lua bindings and I think, that it might be a support nightmare if I don't know where did hid_enumerate() failed and why.

The changes would mean something along this lines:

struct hid_device dev;
uint16_t vendor = 0x1234;
uint16_t product = 0x5678;

if (hid_enumerate(&dev, vendor, product) == NULL)
   wprintf(L"%s() error: %ls\n", hid_error(dev));

...
int hid_init(struct hid_device *dev);
int hid_open(struct hid_device *dev, uint16_t vendor, ...);
...

Thanks.

@signal11

Copy link
Copy Markdown
Owner

That seems a very awkward design choice, to pass in a device just to use the error string/code. Also, hid_device is opaque (you can use a pointer only), because it's different between platforms.

I welcome a solution to the design issue you state, but let's come up with a better way.

Alan.

@ynezz

ynezz commented Aug 27, 2013

Copy link
Copy Markdown

Well, it was just an example :) The context concept is simply needed for other useful stuff like logging for example, here struct hidapi is of course opaque also.

typedef void (*hid_log_fn)(struct hid_api *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args);

struct hidapi {
     int last_error;
     char *last_error_str;
     hid_log_fn log_fn;
     int log_priority;
     struct libusb_context *usb_ctx;
};

int hid_init(struct hidapi **ctx);
int hid_open(struct hidapi *ctx, struct hid_device *dev, uint16_t vendor, uint16_t product);
const char* hid_strerror(struct hidapi *ctx);
void hid_set_log_priority(struct hidapi *ctx, int priority);
void hid_set_log_fn(struct hidapi *ctx, void (*log_fn)(struct hidapi *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args));
int hid_finish(struct hidapi *ctx);

void main()
{
       struct hidapi context;

       if (hid_init(&context) < 0)
               return -1;

       ...

       hid_finish(context);
}

@signal11

signal11 commented Sep 9, 2013

Copy link
Copy Markdown
Owner

The patch attached to this pull request makes some assertions in the documentation but doesn't actually implement any functionality.

To answer your last post, I'd like users to not have to deal with contexts if they don't need to. What you propose addresses a few different issues at once. Maybe if we take one at a time. API changes are tricky and you want to get them right the first time. Also, I'd like it if we could just add to the API rather than change.

What about the possibility of adding an error parameter to hid_open() and hid_enumerate() the way winpcap does (pcap_open_live())? Maybe we could have a struct that has an error code and a string in it?

@nh2

nh2 commented Sep 9, 2013

Copy link
Copy Markdown
Author

The patch attached to this pull request makes some assertions in the documentation but doesn't actually implement any functionality.

Not sure what you meant - it does implement hid_error to give you the last global error message.

@signal11

signal11 commented Sep 9, 2013

Copy link
Copy Markdown
Owner

ok, there are three patches and I only saw the one. Nevermind. I'll look closer.

@phrohdoh

phrohdoh commented Dec 30, 2017 •

Copy link
Copy Markdown

Has this been abandoned?

flit pushed a commit to flit/hidapi that referenced this pull request Feb 6, 2020
* macOS support for multiple usage/usage-pages
Update of @fengji2006 PR#65, second try

* apply requested changes and test

* handle possible nullptr
erikolofsson pushed a commit to Malterlib/hidapi that referenced this pull request Nov 25, 2020
NOTE: This commit does not handle composite HID descriptors
I am interested in adding support for composite descriptors though I still need to find a
device with a composite descriptor to test it correctly.

The implementation idea is similar, as in signal11#125 for macOS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants