Add openBy function

This commit is contained in:
Raphael Assenat 2015-10-24 12:14:31 -04:00
parent ae74a95c22
commit 41f2d359b8
2 changed files with 58 additions and 0 deletions

View File

@ -74,6 +74,7 @@ void gcn64_freeListCtx(struct gcn64_list_ctx *ctx)
/**
* \brief List instances of our rgbleds device on the USB busses.
* \param info Pointer to gcn64_info structure to store data
* \param dst Destination buffer for device serial number/id.
* \param dstbuf_size Destination buffer size.
*/
@ -140,6 +141,50 @@ gcn64_hdl_t gcn64_openDevice(struct gcn64_info *dev)
return hdev;
}
gcn64_hdl_t gcn64_openBy(struct gcn64_info *dev, unsigned char flags)
{
struct gcn64_list_ctx *ctx;
struct gcn64_info inf;
gcn64_hdl_t h;
printf("gcn64_openBy, flags=0x%02x\n", flags);
ctx = gcn64_allocListCtx();
if (!ctx)
return NULL;
while (gcn64_listDevices(&inf, ctx)) {
printf("Considering '%s'\n", inf.str_path);
if (flags & GCN64_FLG_OPEN_BY_SERIAL) {
if (wcscmp(inf.str_serial, dev->str_serial))
continue;
}
if (flags & GCN64_FLG_OPEN_BY_PATH) {
if (strcmp(inf.str_path, dev->str_path))
continue;
}
if (flags & GCN64_FLG_OPEN_BY_VID) {
if (inf.usb_vid != dev->usb_vid)
continue;
}
if (flags & GCN64_FLG_OPEN_BY_PID) {
if (inf.usb_pid != dev->usb_pid)
continue;
}
printf("Found device. opening...\n");
h = gcn64_openDevice(&inf);
gcn64_freeListCtx(ctx);
return h;
}
gcn64_freeListCtx(ctx);
return NULL;
}
void gcn64_closeDevice(gcn64_hdl_t hdl)
{
hid_device *hdev = (hid_device*)hdl;

View File

@ -29,6 +29,19 @@ void gcn64_freeListCtx(struct gcn64_list_ctx *ctx);
struct gcn64_info *gcn64_listDevices(struct gcn64_info *info, struct gcn64_list_ctx *ctx);
gcn64_hdl_t gcn64_openDevice(struct gcn64_info *dev);
#define GCN64_FLG_OPEN_BY_SERIAL 1 /** Serial must match */
#define GCN64_FLG_OPEN_BY_PATH 2 /** Path must match */
#define GCN64_FLG_OPEN_BY_VID 4 /** USB VID must match */
#define GCN64_FLG_OPEN_BY_PID 8 /** USB PID MUST match */
/**
* \brief Open a device matching a serial number
* \param dev The device structure
* \param flags Flags
* \return A handle to the opened device, or NULL if not found
**/
gcn64_hdl_t gcn64_openBy(struct gcn64_info *dev, unsigned char flags);
void gcn64_closeDevice(gcn64_hdl_t hdl);
int gcn64_send_cmd(gcn64_hdl_t hdl, const unsigned char *cmd, int len);