Hello,

Here is a patch to switch to libusb 1.0, this aplies to the 0.6 version.

Nicolas.
>From 519acbb908c45e2354f38bab93a73ae382a91ff1 Mon Sep 17 00:00:00 2001
From: Nicolas Schodet <n...@ni.fr.eu.org>
Date: Wed, 6 Oct 2021 16:08:32 +0200
Subject: [PATCH] Migrate to libusb 1.0

---
 Makefile       |  16 +++---
 src/t2n.cc     |   3 +-
 src/usbmisc.cc | 141 +++++++++++++++++++++++++++++++------------------
 src/usbmisc.h  |  13 ++---
 src/usbnxt.cc  |  53 ++-----------------
 src/usbscan.c  |  54 +++++++++++--------
 6 files changed, 143 insertions(+), 137 deletions(-)

diff --git a/Makefile b/Makefile
index f3939e0..4987241 100644
--- a/Makefile
+++ b/Makefile
@@ -21,17 +21,19 @@ MAIN=t2n
 OBJDIR=./obj$(ARCHI)
 SRCDIR=./src
 
-all: dynamic static
+all: dynamic
 
 dynamic : $(OBJDIR) $(OBJDIR)/$(MAIN)  $(OBJDIR)/usbscan
-static : $(OBJDIR)/$(MAIN).static
+
+USB_CFLAGS = $(shell pkg-config libusb-1.0 --cflags)
+USB_LIBS = $(shell pkg-config libusb-1.0 --libs)
 
 CC=g++
 LK=g++
-CFLAGS+= -I$(SRCDIR) -I$(OBJDIR) -g -Wall
+CFLAGS+= -I$(SRCDIR) -I$(OBJDIR) -g -Wall $(USB_CFLAGS)
 LDFLAGS+=
 
-LIBS=-lusb -lstdc++
+LIBS=$(USB_LIBS)
 
 OBJS=$(OBJDIR)/$(MAIN).o \
      $(OBJDIR)/usbmisc.o \
@@ -46,9 +48,6 @@ $(OBJDIR)/usbscan: $(SRCDIR)/usbscan.c
 $(OBJDIR)/$(MAIN) : $(OBJS)
 	$(LK) $(LDFLAGS) $(OBJS) $(LIBS) -o $(OBJDIR)/$(MAIN)
 
-$(OBJDIR)/$(MAIN).static : $(OBJS)
-	$(LK) -static $(LDFLAGS) $(OBJS) $(LIBS) -o $(OBJDIR)/$(MAIN).static
-
 $(OBJDIR)/%.o : $(SRCDIR)/%.cc 
 	$(CC) -c $(CFLAGS) $(SRCDIR)/$*.cc -o $(OBJDIR)/$*.o
 
@@ -70,12 +69,11 @@ clean:
 distclean:
 	rm -rf $(OBJDIR)
 
-distrib: all static 
+distrib: all
 	ddir=t2n-`$(OBJDIR)/$(MAIN) -version`-$(ARCHI) ; \
 	echo $$ddir ; \
 	if [ ! -d $$ddir ] ; then mkdir $$ddir ; fi ; \
 	cp $(OBJDIR)/$(MAIN) $$ddir ; strip $(OBJDIR)/$(MAIN) ; \
-	cp $(OBJDIR)/$(MAIN).static $$ddir ; strip $(OBJDIR)/$(MAIN).static ; \
 #	cp -r hotplug $$ddir ; \
 #	cp -r udev $$ddir ; \
 	tar zcvf $$ddir.tgz $$ddir ; \
diff --git a/src/t2n.cc b/src/t2n.cc
index 8175b9a..1549194 100644
--- a/src/t2n.cc
+++ b/src/t2n.cc
@@ -30,11 +30,12 @@
 
 #include <string.h>
 #include <stdio.h>
+#include <libusb.h>
+
 #include "version.h"
 #include "errors.h"
 #include "ezargs.h"
 #include "errormng.h"
-#include "usb.h"
 #include "usbmisc.h"
 #include "usbnxt.h"
 
diff --git a/src/usbmisc.cc b/src/usbmisc.cc
index a34b4d1..795d335 100644
--- a/src/usbmisc.cc
+++ b/src/usbmisc.cc
@@ -31,9 +31,10 @@
 #include <string.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <libusb.h>
+
 #include "errors.h"
 #include "errormng.h"
-#include "usb.h"
 #include "usbmisc.h"
 
 //toujours vrai ???
@@ -54,47 +55,51 @@ void UsbHandle::_init(){
 	_bbuff = NULL;
 }
 
-//extern int print_device(struct usb_device *dev, int level);
-
 int UsbHandle::init(u_int16_t vendor, u_int16_t product){
+	int err;
 	_dev = UsbHandle::_find_usb_dev(vendor, product);
 	if(_dev == NULL) {
-		_usbmisc_error.set("no usb device found (vendor=0x%04x product=0x%04x)",
-			vendor, product
-		);
 		return -1;
 	}
 
-//print_device(_dev, 0);
-
-   _handle = usb_open(_dev);
-	if (!_handle) {
-		_usbmisc_error.set("fails to open device handle (vendor=0x%04x product=0x%04x)",
-			vendor, product
+	err = libusb_open(_dev, &_handle);
+	if(err < 0){
+		_usbmisc_error.set(
+			"fails to open device handle, %s (vendor=0x%04x product=0x%04x)",
+			libusb_strerror(err), vendor, product
 		);
 		return -1;
 	}
-	//reset ?
-	usb_reset(_handle);
 
-	int err;
+	err = libusb_reset_device(_handle);
+	if(err < 0){
+		_usbmisc_error.set(
+			"fails to reset device, %s (vendor=0x%04x product=0x%04x)",
+			libusb_strerror(err), vendor, product
+                );
+		return -1;
+	}
+
 	//set config 
-	err = usb_set_configuration(_handle, USB_CONFIG);
+	err = libusb_set_configuration(_handle, USB_CONFIG);
 	if(err != 0){
-//printf( "usb_strerror = %s\n", usb_strerror( )); 
 		_usbmisc_error.set(
-			"fails to set config, errno=%d, usberr=\"%s\" (cf=%d vendor=0x%04x product=0x%04x)",
-			err, usb_strerror(), USB_CONFIG, vendor, product
+			"fails to set config, %s (cf=%d vendor=0x%04x product=0x%04x)",
+			libusb_strerror(err), USB_CONFIG, vendor, product
 		);
+		libusb_close(_handle);
+		_handle = NULL;
 		return -1;
 	}
 	//demande d'interface ...
-	err = usb_claim_interface(_handle, USB_INTERFACE);
+	err = libusb_claim_interface(_handle, USB_INTERFACE);
 	if(err != 0){
 		_usbmisc_error.set(
-			"fails to optain usb interface (erno=%d id=%d vendor=0x%04x product=0x%04x)",
-			err, USB_INTERFACE, vendor, product
+			"fails to obtain usb interface, %s (id=%d vendor=0x%04x product=0x%04x)",
+			libusb_strerror(err), USB_INTERFACE, vendor, product
 		);
+		libusb_close(_handle);
+		_handle = NULL;
 		return -1;
 	}
 	return 0;
@@ -102,34 +107,55 @@ int UsbHandle::init(u_int16_t vendor, u_int16_t product){
 
 UsbHandle::~UsbHandle(){
 	//fermeture propre ...
-	if(_handle) usb_close(_handle);
+	if(_handle){
+		libusb_release_interface(_handle, USB_INTERFACE);
+		libusb_close(_handle);
+	}
+	if(_dev) libusb_unref_device(_dev);
 }
 
 
-struct usb_device* UsbHandle::_find_usb_dev(
+libusb_device* UsbHandle::_find_usb_dev(
 	u_int16_t vendor,
 	u_int16_t product
 ){
-	struct usb_bus *busses, *bus;
-	struct usb_device *devs, *d;
+	libusb_device **list;
 	u_int16_t v, p;
+	int ret;
 
-	usb_init();
-	usb_find_busses();
-	usb_find_devices();
-	busses = usb_get_busses();
-	for(bus = busses; bus;  bus = bus->next){
-		Verbose(2, "(usb scan) bus dirname=%s\n", bus->dirname);
-		devs = bus->devices;
-		for(d=devs; d; d=d->next){
-			v = d->descriptor.idVendor;
-			p = d->descriptor.idProduct;
-			Verbose(2, "(usb scan) dev filename=%s, vendor=0x%04x, product=0x%04x\n",
-				d->filename, v, p
+	ret = libusb_init(NULL);
+	if(ret < 0){
+                _usbmisc_error.set("libusb_init error: %s\n", libusb_strerror(ret));
+		return NULL;
+	}
+
+	ssize_t cnt = libusb_get_device_list(NULL, &list);
+	if(cnt < 0){
+		_usbmisc_error.set("libusb_get_device_list: %s\n",
+			libusb_strerror(cnt));
+		return NULL;
+	}
+	for(ssize_t i = 0; i < cnt; i++){
+		libusb_device *dev = list[i];
+		struct libusb_device_descriptor desc;
+		int ret = libusb_get_device_descriptor(dev, &desc);
+		if (ret == 0){
+			v = desc.idVendor;
+			p = desc.idProduct;
+			Verbose(2, "(usb scan) vendor=0x%04x, product=0x%04x\n",
+				v, p
 			);
-			if((v == vendor) && (p==product)) return d;
+			if((v == vendor) && (p==product)){
+				libusb_ref_device(dev);
+				libusb_free_device_list(list, 1);
+				return dev;
+			}
 		}
 	}
+	libusb_free_device_list(list, 1);
+	_usbmisc_error.set("no usb device found (vendor=0x%04x product=0x%04x)",
+		vendor, product
+        );
 	return NULL;
 }
 
@@ -235,6 +261,24 @@ void Buff::dump(FILE* os){
 	fprintf(os,"\n"); 
 }
 
+int UsbBulkBuff::transfer(unsigned int ep, unsigned char *data, int len, unsigned int to)
+{
+	int transfered;
+	int ret;
+	int total = 0;
+
+	do{
+		ret = libusb_bulk_transfer(_devh, ep, data, len, &transfered, to);
+		if(ret < 0)
+			return ret;
+		total += transfered;
+		data += transfered;
+		len -= transfered;
+	} while(len && !(ep & 0x80));
+
+	return total;
+}
+
 int UsbBulkBuff::write_read(
 const char* vprolog
 ){
@@ -244,24 +288,22 @@ const char* vprolog
 		return 0;
 	}
 	if(Verbose() >= 3){
-		Verbose(3,"UsbBulkBuff::usb_bulk_write(0x%02x, 0x%02x, 0x%x, %d, %d)\n",
+		Verbose(3,"UsbBulkBuff::write(0x%02x, 0x%02x, 0x%x, %d, %d)\n",
 				_devh, _write_ep, _write_buff.data(), _write_buff.len(), _write_to);
 		_write_buff.dump(stderr);
 	}
-	r = usb_bulk_write(_devh, _write_ep, _write_buff.data(), _write_buff.len(), _write_to);
-	Verbose(2, "%s usb_bulk_write %d bytes on %d bytes\n",
+	r = transfer(_write_ep, (unsigned char *)_write_buff.data(), _write_buff.len(), _write_to);
+	Verbose(2, "%s write %d bytes on %d bytes\n",
 		(vprolog)?vprolog:"", r, _write_buff.len());
-	if(r != _write_buff.len()) {
-		_usbmisc_error.set("usb_bulk_write failed (%d bytes instead of %d)\n",
-			r, _write_buff.len()
-		);
+	if(r < 0) {
+		_usbmisc_error.set("write failed, %s\n", libusb_strerror(r));
 		return -1;
 	}
-	r = usb_bulk_read(_devh, _read_ep, _read_buff.data(), _read_buff.maxsz(), _read_to);
-	Verbose(2, "%s usb_bulk_read -> %d bytes\n",
+	r = transfer(_read_ep, (unsigned char *)_read_buff.data(), _read_buff.maxsz(), _read_to);
+	Verbose(2, "%s read -> %d bytes\n",
 		(vprolog)?vprolog:"", r);
 	if(r < 0) {
-		_usbmisc_error.set("usb_bulk_read failed\n");
+		_usbmisc_error.set("read failed, %s\n", libusb_strerror(r));
 		return -1;
 	}
 	_read_buff.set_len(r);
@@ -319,7 +361,6 @@ int UsbHandle::receive(
 	return 0;
 
 	BUFF_ERROR :
-		printf( "usb_strerror = %s\n", usb_strerror( ));
 		_usbmisc_error.set("internal buffer error");
 		return -1;	
 } 
diff --git a/src/usbmisc.h b/src/usbmisc.h
index 2f21be7..f30e8f5 100644
--- a/src/usbmisc.h
+++ b/src/usbmisc.h
@@ -71,11 +71,11 @@ extern const char* usbmisc_error();
 class UsbBulkBuff; 
 
 class UsbHandle {
-	struct usb_device* _dev;
-	struct usb_dev_handle* _handle;
+	libusb_device* _dev;
+	libusb_device_handle* _handle;
 	UsbBulkBuff* _bbuff;
 
-	static struct usb_device* _find_usb_dev(
+	libusb_device* _find_usb_dev(
 		u_int16_t vendor, u_int16_t product	
 	);
 
@@ -153,7 +153,7 @@ public:
 
 class UsbBulkBuff {
 
-	struct usb_dev_handle* _devh;
+	libusb_device_handle* _devh;
 	unsigned int _write_ep;
 	unsigned int _write_to;
 	unsigned int _read_ep;
@@ -162,13 +162,14 @@ class UsbBulkBuff {
 	Buff _read_buff;
 	Buff _write_buff;
 	void _init();
+	int transfer(unsigned int ep, unsigned char *buf, int len, unsigned int to);
 
 friend class UsbHandle;
 protected:
 
 	//Création, initialisations
 	UsbBulkBuff();
-	inline void init(struct usb_dev_handle* dh, int wep, int wto, int rep, int rto);
+	inline void init(libusb_device_handle* dh, int wep, int wto, int rep, int rto);
 
 	//reset des buffers
 	void reset();
@@ -201,7 +202,7 @@ inline void UsbBulkBuff::_init(){
 	reset();
 }
 
-inline void UsbBulkBuff::init(struct usb_dev_handle* dh, int wep, int wto, int rep, int rto){
+inline void UsbBulkBuff::init(libusb_device_handle* dh, int wep, int wto, int rep, int rto){
 	_devh = dh;
 	_write_ep = wep; 
 	_write_to = wto; 
diff --git a/src/usbnxt.cc b/src/usbnxt.cc
index b2ce109..ba62a0c 100644
--- a/src/usbnxt.cc
+++ b/src/usbnxt.cc
@@ -31,9 +31,11 @@
 #include <stdio.h>
 #include <libgen.h>
 #include <errno.h>
+#include <unistd.h>
+#include <libusb.h>
+
 #include "errors.h"
 #include "errormng.h"
-#include "usb.h"
 #include "usbmisc.h"
 #include "usbnxt.h"
 
@@ -570,7 +572,6 @@ int UsbNxt::upload(const char* fname){
 	);
 	if (res) return _fails(fmsg);
 	if (status){
-		printf( "usb_strerror = %s\n", usb_strerror( ));
 		_usbnxt_error.set(
 			"upload: can't initiate upload (reply=0x%02x,status=0x%02x,error=0x%04x)",
 				reply, status, res);
@@ -629,51 +630,3 @@ int UsbNxt::upload(const char* fname){
 
 	return 0;
 }
-
-/*
-int nxt_battery(usb_dev_handle* dev){
-	int r;
-	buffer com;
-	buffer res;
-	char reply_c;
-	char command_c;
-	char status_c;
-	unsigned int mV;
-
-	com[0] = DIRECT_COMMAND;
-	com[1] = GET_BATTERY_LEVEL;
-
-	r = do_nxt_command(dev, &com[0], 2, &res[0]);
-
-	// retour : byte, byte, byte, short (le)
-	if (r != 5) {
-		fprintf(stderr, "nxt_battery: bad response (%d bytes)\n", r);
-		return -1;
-	}
-	reply_c = res[0];		
-	command_c = res[1];		
-	status_c = res[2];		
-	mV = ((unsigned int)res[3] << 8) & (unsigned int)res[4];
-
-	return (int)mV;
-}
-
-int main(){
-	struct usb_dev_handle *nxt;
-
-	nxt = find_nxt();
-	if(nxt) {
-		printf ("TROUVÉ !!!\n");
-	} else {
-		printf ("pas trouvé ...\n");
-		return 1;
-	}
-	usb_reset(nxt);
-
-CleanReturn:
-
-	if(nxt) {
-		usb_close(nxt);
-	}
-}
-*/
diff --git a/src/usbscan.c b/src/usbscan.c
index 31212bc..1ac7870 100644
--- a/src/usbscan.c
+++ b/src/usbscan.c
@@ -26,40 +26,52 @@ VERIMAG - Synchrone TOOLBOX
 ----------------------------------------------------------------------------
 test de scan sur un port usb ...
 --------------------------------------------------------------------------*/
-#include "usb.h"
 #include <string.h>
 #include <stdio.h>
+#include <libusb.h>
 
 // USB Générique : recherche d'un device particulier
-struct usb_device* find_usb_dev(
+struct libusb_device *find_usb_dev(
 	u_int16_t vendor,
 	u_int16_t product
 ){
-	struct usb_bus *busses, *bus;
-	struct usb_device *devs, *d;
+	libusb_device **list;
 	u_int16_t v, p;
+	int ret;
 
-	usb_init();
-	usb_find_busses();
-	usb_find_devices();
-	busses = usb_get_busses();
-	for(bus = busses; bus;  bus = bus->next){
-		printf("bus dirname=%s\n", bus->dirname);
-		devs = bus->devices;
-		for(d=devs; d; d=d->next){
-			v = d->descriptor.idVendor;
-			p = d->descriptor.idProduct;
-			printf("  dev filename=%s, vendor=0x%04x, product=0x%04x\n",
-				d->filename, v, p
-			);
-			if((v == vendor) && (p==product)) return d;
+	ret = libusb_init(NULL);
+	if(ret){
+		fprintf(stderr, "libusb_init: %s\n", libusb_strerror(ret));
+		return NULL;
+	}
+	ssize_t cnt = libusb_get_device_list(NULL, &list);
+	if(cnt < 0){
+		fprintf(stderr, "libusb_get_device_list: %s\n", libusb_strerror(cnt));
+		return NULL;
+	}
+	for(ssize_t i = 0; i < cnt; i++){
+		libusb_device *dev = list[i];
+		struct libusb_device_descriptor desc;
+		int ret = libusb_get_device_descriptor(dev, &desc);
+		if (ret == 0){
+			v = desc.idVendor;
+			p = desc.idProduct;
+			printf("  dev vendor=0x%04x, product=0x%04x\n", v, p);
+			if((v == vendor) && (p==product)){
+				libusb_ref_device(dev);
+				libusb_free_device_list(list, 1);
+				return dev;
+			}
 		}
 	}
+	libusb_free_device_list(list, 1);
 	return NULL;
 }
 
 int main(){
-	struct usb_device *d;
-	d = find_usb_dev(-1,-1);
-	return 0;
+	libusb_device *dev;
+	dev = find_usb_dev(-1, -1);
+	if(dev)
+		libusb_unref_device(dev);
+	return dev ? 0 : 1;
 }
-- 
2.30.2

Reply via email to