all: Move the device serial number matching code into common lib.

This commit is contained in:
Solomon Peachy 2013-06-30 11:32:41 -04:00
parent 0e82760201
commit 310ae8c241
4 changed files with 80 additions and 142 deletions

View file

@ -141,32 +141,74 @@ static void sigterm_handler(int signum) {
INFO("Job Cancelled");
}
static void print_scan_output(struct libusb_device_handle *dev,
unsigned char *product, unsigned char *serial,
char *manuf, char *prefix)
static int print_scan_output(struct libusb_device *device,
struct libusb_device_descriptor *desc,
char *prefix, char *manuf2,
int found, int valid,
int scan_only, char *match_serno)
{
/* URL-ify model. */
char buf[128]; // XXX ugly..
int j = 0, k = 0;
char *ieee_id;
while (*(product + j + strlen(manuf))) {
buf[k] = *(product + j + strlen(manuf) + 1);
if(buf[k] == ' ') {
buf[k++] = '%';
buf[k++] = '2';
buf[k] = '0';
}
k++;
j++;
struct libusb_device_handle *dev;
unsigned char product[STR_LEN_MAX] = "";
unsigned char serial[STR_LEN_MAX] = "";
unsigned char manuf[STR_LEN_MAX] = "";
if (libusb_open(device, &dev)) {
ERROR("Could not open device %04x:%04x\n", desc->idVendor, desc->idProduct);
return -1;
}
ieee_id = get_device_id(dev);
fprintf(stdout, "direct %s%s/%s?serial=%s \"%s\" \"%s\" \"%s\" \"\"\n",
prefix, manuf,
buf, serial, product, product,
ieee_id);
/* Query detailed info */
if (desc->iManufacturer) {
libusb_get_string_descriptor_ascii(dev, desc->iManufacturer, manuf, STR_LEN_MAX);
}
if (desc->iProduct) {
libusb_get_string_descriptor_ascii(dev, desc->iProduct, product, STR_LEN_MAX);
}
if (desc->iSerialNumber) {
libusb_get_string_descriptor_ascii(dev, desc->iSerialNumber, serial, STR_LEN_MAX);
}
if (ieee_id)
free(ieee_id);
if (!strlen((char*)serial))
strcpy((char*)serial, "NONE");
DEBUG("%s%sPID: %04X Product: '%s' Serial: '%s'\n",
(!valid) ? "UNRECOGNIZED: " : "",
found ? "MATCH: " : "",
desc->idProduct, product, serial);
if (valid && scan_only) {
/* URL-ify model. */
char buf[128]; // XXX ugly..
int j = 0, k = 0;
char *ieee_id;
while (*(product + j + strlen(manuf2))) {
buf[k] = *(product + j + strlen(manuf2) + 1);
if(buf[k] == ' ') {
buf[k++] = '%';
buf[k++] = '2';
buf[k] = '0';
}
k++;
j++;
}
ieee_id = get_device_id(dev);
fprintf(stdout, "direct %s%s/%s?serial=%s \"%s\" \"%s\" \"%s\" \"\"\n",
prefix, manuf2,
buf, serial, product, product,
ieee_id);
if (ieee_id)
free(ieee_id);
}
/* If a serial number was passed down, use it. */
if (found && match_serno &&
strcmp(match_serno, (char*)serial)) {
found = -1;
}
libusb_close(dev);
return found;
}

View file

@ -35,7 +35,7 @@
#include <fcntl.h>
#include <signal.h>
#define VERSION "0.11"
#define VERSION "0.12"
#define URI_PREFIX "kodak1400://"
#include "backend_common.c"
@ -90,16 +90,11 @@ static int find_and_enumerate(struct libusb_context *ctx,
int i;
int found = -1;
struct libusb_device_handle *dev;
/* Enumerate and find suitable device */
num = libusb_get_device_list(ctx, list);
for (i = 0 ; i < num ; i++) {
struct libusb_device_descriptor desc;
unsigned char product[STR_LEN_MAX] = "";
unsigned char serial[STR_LEN_MAX] = "";
unsigned char manuf[STR_LEN_MAX] = "";
libusb_get_device_descriptor((*list)[i], &desc);
@ -119,38 +114,10 @@ static int find_and_enumerate(struct libusb_context *ctx,
continue;
}
if (libusb_open(((*list)[i]), &dev)) {
ERROR("Could not open device %04x:%04x\n", desc.idVendor, desc.idProduct);
found = -1;
continue;
}
/* Query detailed info */
if (desc.iManufacturer) {
libusb_get_string_descriptor_ascii(dev, desc.iManufacturer, manuf, STR_LEN_MAX);
}
if (desc.iProduct) {
libusb_get_string_descriptor_ascii(dev, desc.iProduct, product, STR_LEN_MAX);
}
if (desc.iSerialNumber) {
libusb_get_string_descriptor_ascii(dev, desc.iSerialNumber, serial, STR_LEN_MAX);
}
DEBUG("PID: %04X Product: '%s' Serial: '%s'\n",
desc.idProduct, product, serial);
if (scan_only) {
print_scan_output(dev, product, serial,
"Kodak", URI_PREFIX);
}
/* If a serial number was passed down, use it. */
if (found && match_serno &&
strcmp(match_serno, (char*)serial)) {
found = -1;
}
libusb_close(dev);
found = print_scan_output((*list)[i], &desc,
URI_PREFIX, "Kodak",
(found == i), 1,
scan_only, match_serno);
}
return found;

View file

@ -35,7 +35,7 @@
#include <fcntl.h>
#include <signal.h>
#define VERSION "0.06"
#define VERSION "0.07"
#define URI_PREFIX "kodak6800://"
#define STR_LEN_MAX 64
@ -77,16 +77,11 @@ static int find_and_enumerate(struct libusb_context *ctx,
int i;
int found = -1;
struct libusb_device_handle *dev;
/* Enumerate and find suitable device */
num = libusb_get_device_list(ctx, list);
for (i = 0 ; i < num ; i++) {
struct libusb_device_descriptor desc;
unsigned char product[STR_LEN_MAX] = "";
unsigned char serial[STR_LEN_MAX] = "";
unsigned char manuf[STR_LEN_MAX] = "";
libusb_get_device_descriptor((*list)[i], &desc);
@ -101,38 +96,10 @@ static int find_and_enumerate(struct libusb_context *ctx,
continue;
}
if (libusb_open(((*list)[i]), &dev)) {
ERROR("Could not open device %04x:%04x\n", desc.idVendor, desc.idProduct);
found = -1;
continue;
}
/* Query detailed info */
if (desc.iManufacturer) {
libusb_get_string_descriptor_ascii(dev, desc.iManufacturer, manuf, STR_LEN_MAX);
}
if (desc.iProduct) {
libusb_get_string_descriptor_ascii(dev, desc.iProduct, product, STR_LEN_MAX);
}
if (desc.iSerialNumber) {
libusb_get_string_descriptor_ascii(dev, desc.iSerialNumber, serial, STR_LEN_MAX);
}
DEBUG("PID: %04X Product: '%s' Serial: '%s'\n",
desc.idProduct, product, serial);
if (scan_only) {
print_scan_output(dev, product, serial,
"Kodak", URI_PREFIX);
}
/* If a serial number was passed down, use it. */
if (found && match_serno &&
strcmp(match_serno, (char*)serial)) {
found = -1;
}
libusb_close(dev);
found = print_scan_output((*list)[i], &desc,
URI_PREFIX, "Kodak",
(found == i), 1,
scan_only, match_serno);
}
return found;

View file

@ -35,7 +35,7 @@
#include <fcntl.h>
#include <signal.h>
#define VERSION "0.48"
#define VERSION "0.49"
#define URI_PREFIX "selphy://"
#include "backend_common.c"
@ -363,16 +363,11 @@ static int find_and_enumerate(struct libusb_context *ctx,
int i;
int found = -1;
struct libusb_device_handle *dev;
/* Enumerate and find suitable device */
num = libusb_get_device_list(ctx, list);
for (i = 0 ; i < num ; i++) {
struct libusb_device_descriptor desc;
unsigned char product[STR_LEN_MAX] = "";
unsigned char serial[STR_LEN_MAX] = "";
unsigned char manuf[STR_LEN_MAX] = "";
int valid = 0;
libusb_get_device_descriptor((*list)[i], &desc);
@ -449,43 +444,10 @@ static int find_and_enumerate(struct libusb_context *ctx,
break;
}
if (libusb_open(((*list)[i]), &dev)) {
ERROR("Could not open device %04x:%04x\n", desc.idVendor, desc.idProduct);
found = -1;
continue;
}
/* Query detailed info */
if (desc.iManufacturer) {
libusb_get_string_descriptor_ascii(dev, desc.iManufacturer, manuf, STR_LEN_MAX);
}
if (desc.iProduct) {
libusb_get_string_descriptor_ascii(dev, desc.iProduct, product, STR_LEN_MAX);
}
if (desc.iSerialNumber) {
libusb_get_string_descriptor_ascii(dev, desc.iSerialNumber, serial, STR_LEN_MAX);
}
if (!strlen((char*)serial))
strcpy((char*)serial, "NONE");
DEBUG("%s%sPID: %04X Product: '%s' Serial: '%s'\n",
(!valid) ? "UNRECOGNIZED: " : "",
(found == i) ? "MATCH: " : "",
desc.idProduct, product, serial);
if (valid && scan_only) {
print_scan_output(dev, product, serial,
"Canon", URI_PREFIX);
}
/* If a serial number was passed down, use it. */
if (found && match_serno &&
strcmp(match_serno, (char*)serial)) {
found = -1;
}
libusb_close(dev);
found = print_scan_output((*list)[i], &desc,
URI_PREFIX, "Canon",
(found == i), valid,
scan_only, match_serno);
}
return found;