Factor out opendev2/opendev3.
This commit is contained in:
parent
5e59f47615
commit
9a5c6dcf49
|
@ -30,6 +30,7 @@ libsigrok_la_SOURCES = \
|
||||||
hwplugin.c \
|
hwplugin.c \
|
||||||
filter.c \
|
filter.c \
|
||||||
hardware/common/ezusb.c \
|
hardware/common/ezusb.c \
|
||||||
|
hardware/common/misc.c \
|
||||||
hardware/common/serial.c \
|
hardware/common/serial.c \
|
||||||
hardware/openbench-logic-sniffer/ols.c \
|
hardware/openbench-logic-sniffer/ols.c \
|
||||||
hardware/saleae-logic/saleae-logic.c \
|
hardware/saleae-logic/saleae-logic.c \
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* This file is part of the sigrok project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
|
||||||
|
*
|
||||||
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <glib.h>
|
||||||
|
#include <libusb.h>
|
||||||
|
#include <sigrok.h>
|
||||||
|
|
||||||
|
int opendev2(int device_index, struct sigrok_device_instance **sdi,
|
||||||
|
libusb_device *dev, struct libusb_device_descriptor *des,
|
||||||
|
int *skip, uint16_t vid, uint16_t pid, int interface)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if ((err = libusb_get_device_descriptor(dev, des))) {
|
||||||
|
g_warning("failed to get device descriptor: %d", err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (des->idVendor != vid || des->idProduct != pid)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (*skip != device_index) {
|
||||||
|
/* Skip devices of this type that aren't the one we want. */
|
||||||
|
*skip += 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Should check the bus here, since we know that already. But what are
|
||||||
|
* we going to do if it doesn't match after the right number of skips?
|
||||||
|
*/
|
||||||
|
if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
|
||||||
|
(*sdi)->usb->address = libusb_get_device_address(dev);
|
||||||
|
(*sdi)->status = ST_ACTIVE;
|
||||||
|
g_message("opened device %d on %d.%d interface %d",
|
||||||
|
(*sdi)->index, (*sdi)->usb->bus,
|
||||||
|
(*sdi)->usb->address, interface);
|
||||||
|
} else {
|
||||||
|
g_warning("failed to open device: %d", err);
|
||||||
|
*sdi = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
|
||||||
|
struct libusb_device_descriptor *des,
|
||||||
|
uint16_t vid, uint16_t pid, int interface)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if ((err = libusb_get_device_descriptor(dev, des))) {
|
||||||
|
g_warning("failed to get device descriptor: %d", err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (des->idVendor != vid || des->idProduct != pid)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
|
||||||
|
&& libusb_get_device_address(dev) == (*sdi)->usb->address) {
|
||||||
|
/* Found it. */
|
||||||
|
if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
|
||||||
|
(*sdi)->status = ST_ACTIVE;
|
||||||
|
g_message("opened device %d on %d.%d interface %d",
|
||||||
|
(*sdi)->index, (*sdi)->usb->bus,
|
||||||
|
(*sdi)->usb->address, interface);
|
||||||
|
} else {
|
||||||
|
g_warning("failed to open device: %d", err);
|
||||||
|
*sdi = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -164,75 +164,6 @@ int check_conf_profile(libusb_device *dev)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int opendev2(int device_index, struct sigrok_device_instance **sdi,
|
|
||||||
libusb_device *dev, struct libusb_device_descriptor *des,
|
|
||||||
int *skip, uint16_t vid, uint16_t pid, int interface)
|
|
||||||
{
|
|
||||||
int err;
|
|
||||||
|
|
||||||
if ((err = libusb_get_device_descriptor(dev, des))) {
|
|
||||||
g_warning("failed to get device descriptor: %d", err);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (des->idVendor != vid || des->idProduct != pid)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (*skip != device_index) {
|
|
||||||
/* Skip devices of this type that aren't the one we want. */
|
|
||||||
*skip += 1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Should check the bus here, since we know that already. But what are
|
|
||||||
* we going to do if it doesn't match after the right number of skips?
|
|
||||||
*/
|
|
||||||
if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
|
|
||||||
(*sdi)->usb->address = libusb_get_device_address(dev);
|
|
||||||
(*sdi)->status = ST_ACTIVE;
|
|
||||||
g_message("opened device %d on %d.%d interface %d",
|
|
||||||
(*sdi)->index, (*sdi)->usb->bus,
|
|
||||||
(*sdi)->usb->address, interface);
|
|
||||||
} else {
|
|
||||||
g_warning("failed to open device: %d", err);
|
|
||||||
*sdi = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
|
|
||||||
struct libusb_device_descriptor *des,
|
|
||||||
uint16_t vid, uint16_t pid, int interface)
|
|
||||||
{
|
|
||||||
int err;
|
|
||||||
|
|
||||||
if ((err = libusb_get_device_descriptor(dev, des))) {
|
|
||||||
g_warning("failed to get device descriptor: %d", err);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (des->idVendor != vid || des->idProduct != pid)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
|
|
||||||
&& libusb_get_device_address(dev) == (*sdi)->usb->address) {
|
|
||||||
/* Found it. */
|
|
||||||
if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
|
|
||||||
(*sdi)->status = ST_ACTIVE;
|
|
||||||
g_message("opened device %d on %d.%d interface %d",
|
|
||||||
(*sdi)->index, (*sdi)->usb->bus,
|
|
||||||
(*sdi)->usb->address, interface);
|
|
||||||
} else {
|
|
||||||
g_warning("failed to open device: %d", err);
|
|
||||||
*sdi = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sigrok_device_instance *sl_open_device(int device_index)
|
struct sigrok_device_instance *sl_open_device(int device_index)
|
||||||
{
|
{
|
||||||
struct sigrok_device_instance *sdi;
|
struct sigrok_device_instance *sdi;
|
||||||
|
|
|
@ -140,7 +140,7 @@ static unsigned int get_memory_size(int type)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
|
static int opendev4(struct sigrok_device_instance **sdi, libusb_device *dev,
|
||||||
struct libusb_device_descriptor *des)
|
struct libusb_device_descriptor *des)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
@ -205,7 +205,7 @@ struct sigrok_device_instance *zp_open_device(int device_index)
|
||||||
libusb_get_device_list(usb_context, &devlist);
|
libusb_get_device_list(usb_context, &devlist);
|
||||||
for (i = 0; devlist[i]; i++) {
|
for (i = 0; devlist[i]; i++) {
|
||||||
/* TODO: Error handling. */
|
/* TODO: Error handling. */
|
||||||
err = opendev3(&sdi, devlist[i], &des);
|
err = opendev4(&sdi, devlist[i], &des);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* Status must be ST_ACTIVE, i.e. already in use... */
|
/* Status must be ST_ACTIVE, i.e. already in use... */
|
||||||
|
|
9
sigrok.h
9
sigrok.h
|
@ -433,4 +433,13 @@ void serial_restore_params(int fd, void *backup);
|
||||||
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
||||||
int flowcontrol);
|
int flowcontrol);
|
||||||
|
|
||||||
|
/* libsigrok/hardware/common/misc.c */
|
||||||
|
/* TODO: Should not be public. */
|
||||||
|
int opendev2(int device_index, struct sigrok_device_instance **sdi,
|
||||||
|
libusb_device *dev, struct libusb_device_descriptor *des,
|
||||||
|
int *skip, uint16_t vid, uint16_t pid, int interface);
|
||||||
|
int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
|
||||||
|
struct libusb_device_descriptor *des,
|
||||||
|
uint16_t vid, uint16_t pid, int interface);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue