* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
+#define _GNU_SOURCE
#include "cc.h"
-
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
return i;
}
+static int
+load_dec(char *dir, char *file)
+{
+ char *line;
+ char *end;
+ long i;
+
+ line = load_string(dir, file);
+ if (!line)
+ return -1;
+ i = strtol(line, &end, 10);
+ free(line);
+ if (end == line)
+ return -1;
+ return i;
+}
+
static int
dir_filter_tty_colon(const struct dirent *d)
{
struct dirent **namelist;
int interface;
int num_interfaces;
- char endpoint_base[20];
+ char endpoint_base[64];
char *endpoint_full;
char *tty_dir;
int ntty;
/* Check for tty/ttyACMx style names
*/
tty_dir = cc_fullname(endpoint_full, "tty");
- free(endpoint_full);
ntty = scandir(tty_dir, &namelist,
dir_filter_tty,
alphasort);
free (tty_dir);
+ if (ntty > 0) {
+ tty = cc_fullname("/dev", namelist[0]->d_name);
+ free(endpoint_full);
+ free(namelist);
+ return tty;
+ }
+
+ /* Check for ttyACMx style names
+ */
+ ntty = scandir(endpoint_full, &namelist,
+ dir_filter_tty,
+ alphasort);
+ free(endpoint_full);
if (ntty > 0) {
tty = cc_fullname("/dev", namelist[0]->d_name);
free(namelist);
usbdev->sys = strdup(sys);
usbdev->manufacturer = load_string(sys, "manufacturer");
usbdev->product = load_string(sys, "product");
- usbdev->serial = load_string(sys, "serial");
+ usbdev->serial = load_dec(sys, "serial");
usbdev->idProduct = load_hex(sys, "idProduct");
usbdev->idVendor = load_hex(sys, "idVendor");
usbdev->tty = usb_tty(sys);
free(usbdev->sys);
free(usbdev->manufacturer);
free(usbdev->product);
- free(usbdev->serial);
- free(usbdev->tty);
+ /* this can get used as a return value */
+ if (usbdev->tty)
+ free(usbdev->tty);
free(usbdev);
}
return 1;
}
+static int
+is_am(int idVendor, int idProduct) {
+ if (idVendor == 0xfffe)
+ return 1;
+ if (idVendor == 0x0403 && idProduct == 0x6015)
+ return 1;
+ return 0;
+}
+
struct cc_usbdevs *
-cc_usbdevs_scan(void)
+cc_usbdevs_scan(int non_tty)
{
int e;
struct dirent **ents;
dir = cc_fullname(USB_DEVICES, ents[e]->d_name);
dev = usb_scan_device(dir);
free(dir);
- if (dev->idVendor == 0xfffe && dev->tty) {
+ if (is_am(dev->idVendor, dev->idProduct) && (non_tty || dev->tty)) {
if (devs->dev)
devs->dev = realloc(devs->dev,
- devs->ndev + 1 * sizeof (struct usbdev *));
+ (devs->ndev + 1) * sizeof (struct usbdev *));
else
devs->dev = malloc (sizeof (struct usbdev *));
devs->dev[devs->ndev++] = dev;
usbdev_free(usbdevs->dev[i]);
free(usbdevs);
}
+
+static char *
+match_dev(char *product, int serial)
+{
+ struct cc_usbdevs *devs;
+ struct cc_usbdev *dev;
+ int i;
+ char *tty = NULL;
+
+ devs = cc_usbdevs_scan(FALSE);
+ if (!devs)
+ return NULL;
+ for (i = 0; i < devs->ndev; i++) {
+ dev = devs->dev[i];
+ if (product && strncmp (product, dev->product, strlen(product)) != 0)
+ continue;
+ if (serial && serial != dev->serial)
+ continue;
+ break;
+ }
+ if (i < devs->ndev) {
+ tty = devs->dev[i]->tty;
+ devs->dev[i]->tty = NULL;
+ }
+ cc_usbdevs_free(devs);
+ return tty;
+}
+
+char *
+cc_usbdevs_find_by_arg(char *arg, char *default_product)
+{
+ char *product;
+ int serial;
+ char *end;
+ char *colon;
+ char *tty;
+
+ if (arg)
+ {
+ /* check for <serial> */
+ serial = strtol(arg, &end, 0);
+ if (end != arg) {
+ if (*end != '\0')
+ return NULL;
+ product = NULL;
+ } else {
+ /* check for <product>:<serial> */
+ colon = strchr(arg, ':');
+ if (colon) {
+ product = strndup(arg, colon - arg);
+ serial = strtol(colon + 1, &end, 0);
+ if (*end != '\0')
+ return NULL;
+ } else {
+ product = arg;
+ serial = 0;
+ }
+ }
+ } else {
+ product = NULL;
+ serial = 0;
+ }
+ tty = NULL;
+ if (!product && default_product)
+ tty = match_dev(default_product, serial);
+ if (!tty)
+ tty = match_dev(product, serial);
+ if (product && product != arg)
+ free(product);
+ return tty;
+}