Implement libx52io read APIs

pull/26/head
nirenjan 2020-07-09 15:12:55 -07:00
parent 9ab3cce73e
commit 16cb1e4698
4 changed files with 74 additions and 8 deletions

View File

@ -27,5 +27,7 @@ struct libx52io_context {
void _x52io_set_axis_range(libx52io_context *ctx);
void _x52io_set_report_parser(libx52io_context *ctx);
int _x52io_parse_report(libx52io_context *ctx, libx52io_report *report,
unsigned char *data, int length);
#endif // !defined IO_COMMON_H

View File

@ -86,7 +86,10 @@ int libx52io_open(libx52io_context *ctx)
ctx->pid = cur_dev->product_id;
_x52io_set_axis_range(ctx);
/* _x52io_set_report_parser(ctx); */
_x52io_set_report_parser(ctx);
rc = LIBX52IO_SUCCESS;
goto finally;
break;

View File

@ -118,7 +118,7 @@ static int parse_x52(unsigned char *data, int length, libx52io_report *report)
};
if (length != 14) {
return 1;
return LIBX52IO_ERROR_IO;
}
axis = (data[3] << 24) |
@ -133,7 +133,7 @@ static int parse_x52(unsigned char *data, int length, libx52io_report *report)
map_buttons(data, button_map, report);
return 0;
return LIBX52IO_SUCCESS;
}
static int parse_x52pro(unsigned char *data, int length, libx52io_report *report)
@ -196,7 +196,7 @@ static int parse_x52pro(unsigned char *data, int length, libx52io_report *report
};
if (length != 15) {
return 1;
return LIBX52IO_ERROR_IO;
}
axis = (data[3] << 24) |
@ -211,7 +211,7 @@ static int parse_x52pro(unsigned char *data, int length, libx52io_report *report
map_buttons(data, button_map, report);
return 0;
return LIBX52IO_SUCCESS;
}
void _x52io_set_report_parser(libx52io_context *ctx)
@ -229,3 +229,41 @@ void _x52io_set_report_parser(libx52io_context *ctx)
break;
}
}
int _x52io_parse_report(libx52io_context *ctx, libx52io_report *report,
unsigned char *data, int length)
{
if (ctx->parser == NULL) {
return LIBX52IO_ERROR_NO_DEVICE;
}
return (ctx->parser)(data, length, report);
}
int libx52io_read(libx52io_context *ctx, libx52io_report *report)
{
return libx52io_read_timeout(ctx, report, -1);
}
int libx52io_read_timeout(libx52io_context *ctx, libx52io_report *report, int timeout)
{
int rc;
unsigned char data[16];
if (ctx == NULL || report == NULL) {
return LIBX52IO_ERROR_INVALID;
}
if (ctx->handle == NULL) {
return LIBX52IO_ERROR_NO_DEVICE;
}
rc = hid_read_timeout(ctx->handle, data, sizeof(data), timeout);
if (rc == 0) {
return LIBX52IO_ERROR_TIMEOUT;
} else if (rc < 0) {
return LIBX52IO_ERROR_IO;
}
return _x52io_parse_report(ctx, report, data, rc);
}

View File

@ -74,8 +74,8 @@ typedef enum {
/** Read error from device */
LIBX52IO_ERROR_IO,
/** Other (unknown) error */
LIBX52_ERROR_UNKNOWN
/** Timeout during read from device */
LIBX52IO_ERROR_TIMEOUT,
} libx52io_error_code;
/**
@ -336,7 +336,28 @@ int libx52io_close(libx52io_context *ctx);
* @brief Read and parse a HID report
*
* This function reads and parses a HID report from a connected joystick. This
* function will block until some data is available from the joystick.
* function will block until some data is available from the joystick, or the
* timeout is hit, whichever is first.
*
* @param[in] ctx Pointer to the device context
* @param[out] report Pointer to save the decoded HID report
* @param[in] timeout Timeout value in milliseconds
*
* @returns
* - \ref LIBX52IO_SUCCESS on read and parse success
* - \ref LIBX52IO_ERROR_INVALID if the context or report pointers are not valid
* - \ref LIBX52IO_ERROR_NO_DEVICE if the device is disconnected
* - \ref LIBX52IO_ERROR_IO if there was an error reading from the device,
* including if the device was disconnected during the read.
* - \ref LIBX52IO_ERROR_TIMEOUT if no report was read before timeout.
*/
int libx52io_read_timeout(libx52io_context *ctx, libx52io_report *report, int timeout);
/**
* @brief Read and parse a HID report
*
* This behaves the same as \ref libx52io_read_timeout with a timeout of \c -1.
* This function will block until some data is available from the joystick.
*
* @param[in] ctx Pointer to the device context
* @param[out] report Pointer to save the decoded HID report
@ -345,6 +366,8 @@ int libx52io_close(libx52io_context *ctx);
* - \ref LIBX52IO_SUCCESS on read and parse success
* - \ref LIBX52IO_ERROR_INVALID if the context or report pointers are not valid
* - \ref LIBX52IO_ERROR_NO_DEVICE if the device is disconnected
* - \ref LIBX52IO_ERROR_IO if there was an error reading from the device,
* including if the device was disconnected during the read.
*/
int libx52io_read(libx52io_context *ctx, libx52io_report *report);