nut-debian/tools/nut-scanner/scan_usb.c

275 lines
6.9 KiB
C
Raw Normal View History

2016-07-18 03:11:41 +03:00
/*
* Copyright (C) 2011 - EATON
2011-09-29 21:14:46 +03:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2016-07-18 03:11:41 +03:00
/*! \file scan_usb.c
\brief detect NUT supported USB devices
\author Frederic Bohe <fredericbohe@eaton.com>
*/
2011-09-29 21:14:46 +03:00
#include "common.h"
2012-01-24 12:22:33 +02:00
#include "nut-scan.h"
#ifdef WITH_USB
2011-09-29 21:14:46 +03:00
#include "upsclient.h"
#include "nutscan-usb.h"
#include <stdio.h>
#include <string.h>
2012-01-24 12:22:33 +02:00
#include <ltdl.h>
/* dynamic link library stuff */
static lt_dlhandle dl_handle = NULL;
static const char *dl_error = NULL;
static int (*nut_usb_close)(usb_dev_handle *dev);
static int (*nut_usb_find_busses)(void);
static char * (*nut_usb_strerror)(void);
static void (*nut_usb_init)(void);
static int (*nut_usb_get_string_simple)(usb_dev_handle *dev, int index,
char *buf, size_t buflen);
static struct usb_bus * (*nut_usb_busses);
static usb_dev_handle * (*nut_usb_open)(struct usb_device *dev);
static int (*nut_usb_find_devices)(void);
/* return 0 on error */
2016-07-18 03:11:41 +03:00
int nutscan_load_usb_library(const char *libname_path)
2012-01-24 12:22:33 +02:00
{
2016-07-18 03:11:41 +03:00
if( dl_handle != NULL ) {
/* if previous init failed */
if( dl_handle == (void *)1 ) {
return 0;
}
/* init has already been done */
return 1;
}
if (libname_path == NULL) {
fprintf(stderr, "USB library not found. USB search disabled.\n");
return 0;
}
2012-01-24 12:22:33 +02:00
if( lt_dlinit() != 0 ) {
fprintf(stderr, "Error initializing lt_init\n");
return 0;
}
2016-07-18 03:11:41 +03:00
dl_handle = lt_dlopen(libname_path);
if (!dl_handle) {
dl_error = lt_dlerror();
goto err;
}
lt_dlerror(); /* Clear any existing error */
*(void **) (&nut_usb_close) = lt_dlsym(dl_handle, "usb_close");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
*(void **) (&nut_usb_find_busses) = lt_dlsym(dl_handle, "usb_find_busses");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
*(void **) (&nut_usb_strerror) = lt_dlsym(dl_handle, "usb_strerror");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
*(void **) (&nut_usb_init) = lt_dlsym(dl_handle, "usb_init");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
*(void **) (&nut_usb_get_string_simple) = lt_dlsym(dl_handle,
"usb_get_string_simple");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
*(void **) (&nut_usb_busses) = lt_dlsym(dl_handle, "usb_busses");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
*(void **) (&nut_usb_open) = lt_dlsym(dl_handle, "usb_open");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
*(void **)(&nut_usb_find_devices) = lt_dlsym(dl_handle,"usb_find_devices");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
2012-01-24 12:22:33 +02:00
2016-07-18 03:11:41 +03:00
return 1;
2012-01-24 12:22:33 +02:00
err:
2016-07-18 03:11:41 +03:00
fprintf(stderr, "Cannot load USB library (%s) : %s. USB search disabled.\n", libname_path, dl_error);
dl_handle = (void *)1;
2012-06-01 16:55:19 +03:00
lt_dlexit();
2016-07-18 03:11:41 +03:00
return 0;
2012-01-24 12:22:33 +02:00
}
/* end of dynamic link library stuff */
2011-09-29 21:14:46 +03:00
static char* is_usb_device_supported(usb_device_id_t *usb_device_id_list,
int dev_VendorID, int dev_ProductID)
{
usb_device_id_t *usbdev;
for (usbdev=usb_device_id_list; usbdev->driver_name != NULL; usbdev++) {
if ( (usbdev->vendorID == dev_VendorID)
&& (usbdev->productID == dev_ProductID) ) {
return usbdev->driver_name;
}
}
return NULL;
}
/* return NULL if error */
nutscan_device_t * nutscan_scan_usb()
{
int ret;
char string[256];
char *driver_name = NULL;
char *serialnumber = NULL;
char *device_name = NULL;
char *vendor_name = NULL;
struct usb_device *dev;
struct usb_bus *bus;
usb_dev_handle *udev;
nutscan_device_t * nut_dev = NULL;
nutscan_device_t * current_nut_dev = NULL;
2012-01-24 12:22:33 +02:00
if( !nutscan_avail_usb ) {
return NULL;
}
2011-09-29 21:14:46 +03:00
/* libusb base init */
2012-01-24 12:22:33 +02:00
(*nut_usb_init)();
(*nut_usb_find_busses)();
(*nut_usb_find_devices)();
2011-09-29 21:14:46 +03:00
2012-01-24 12:22:33 +02:00
for (bus = (*nut_usb_busses); bus; bus = bus->next) {
2011-09-29 21:14:46 +03:00
for (dev = bus->devices; dev; dev = dev->next) {
if ((driver_name =
is_usb_device_supported(usb_device_table,
dev->descriptor.idVendor,
dev->descriptor.idProduct)) != NULL) {
/* open the device */
2012-01-24 12:22:33 +02:00
udev = (*nut_usb_open)(dev);
2011-09-29 21:14:46 +03:00
if (!udev) {
fprintf(stderr,"Failed to open device, \
skipping. (%s)\n",
2012-01-24 12:22:33 +02:00
(*nut_usb_strerror)());
2011-09-29 21:14:46 +03:00
continue;
}
/* get serial number */
if (dev->descriptor.iSerialNumber) {
2012-01-24 12:22:33 +02:00
ret = (*nut_usb_get_string_simple)(udev,
2011-09-29 21:14:46 +03:00
dev->descriptor.iSerialNumber,
string, sizeof(string));
if (ret > 0) {
2016-07-18 03:11:41 +03:00
serialnumber = strdup(str_rtrim(string, ' '));
2011-09-29 21:14:46 +03:00
}
}
/* get product name */
if (dev->descriptor.iProduct) {
2012-01-24 12:22:33 +02:00
ret = (*nut_usb_get_string_simple)(udev,
2011-09-29 21:14:46 +03:00
dev->descriptor.iProduct,
string, sizeof(string));
if (ret > 0) {
2016-07-18 03:11:41 +03:00
device_name = strdup(str_rtrim(string, ' '));
2011-09-29 21:14:46 +03:00
}
}
/* get vendor name */
if (dev->descriptor.iManufacturer) {
2012-01-24 12:22:33 +02:00
ret = (*nut_usb_get_string_simple)(udev,
2011-09-29 21:14:46 +03:00
dev->descriptor.iManufacturer,
string, sizeof(string));
if (ret > 0) {
2016-07-18 03:11:41 +03:00
vendor_name = strdup(str_rtrim(string, ' '));
2011-09-29 21:14:46 +03:00
}
}
nut_dev = nutscan_new_device();
if(nut_dev == NULL) {
fprintf(stderr,"Memory allocation \
error\n");
nutscan_free_device(current_nut_dev);
free(serialnumber);
free(device_name);
free(vendor_name);
return NULL;
}
nut_dev->type = TYPE_USB;
if(driver_name) {
nut_dev->driver = strdup(driver_name);
}
nut_dev->port = strdup("auto");
sprintf(string,"%04X",dev->descriptor.idVendor);
nutscan_add_option_to_device(nut_dev,"vendorid",
string);
sprintf(string,"%04X",
dev->descriptor.idProduct);
nutscan_add_option_to_device(nut_dev,"productid",
string);
if(device_name) {
nutscan_add_option_to_device(nut_dev,
"product",
device_name);
free(device_name);
}
if(serialnumber) {
nutscan_add_option_to_device(nut_dev,
"serial",
serialnumber);
free(serialnumber);
}
if(vendor_name) {
nutscan_add_option_to_device(nut_dev,
"vendor",
vendor_name);
free(vendor_name);
}
nutscan_add_option_to_device(nut_dev,"bus",
bus->dirname);
current_nut_dev = nutscan_add_device_to_device(
current_nut_dev,
nut_dev);
memset (string, 0, sizeof(string));
2012-01-24 12:22:33 +02:00
(*nut_usb_close)(udev);
2011-09-29 21:14:46 +03:00
}
}
}
2013-11-24 17:00:12 +02:00
return nutscan_rewind_device(current_nut_dev);
2011-09-29 21:14:46 +03:00
}
2012-01-24 12:22:33 +02:00
#else /* WITH_USB */
nutscan_device_t * nutscan_scan_usb()
{
return NULL;
}
#endif /* WITH_USB */
2011-09-29 21:14:46 +03:00