From dcc9934b81c440a5737c26448a08527e88bd8907 Mon Sep 17 00:00:00 2001
From: Alessio Igor Bogani <alessio.bogani@elettra.eu>
Date: Mon, 9 Dec 2024 15:13:02 +0000
Subject: [PATCH] Simplify code

---
 src/USB2.cpp | 72 ++++++++++++++--------------------------------------
 src/USB2.h   |  9 +++----
 2 files changed, 22 insertions(+), 59 deletions(-)

diff --git a/src/USB2.cpp b/src/USB2.cpp
index 8c1f535..6d19ed4 100644
--- a/src/USB2.cpp
+++ b/src/USB2.cpp
@@ -175,9 +175,8 @@ void USB2::init_device()
 
 	// Initialize device
 	if (init_error.empty()) {
-		resolve();
 		open();
-		check_state(true);
+		check_state();
 	}
 	/*----- PROTECTED REGION END -----*/	//	USB2::init_device
 }
@@ -388,10 +387,11 @@ void USB2::always_executed_hook()
 		set_state(Tango::FAULT);
 		set_status(init_error);
 		DEBUG_STREAM << init_error << endl;
-	} else {
-		check_state(true);
+		return;
 	}
 
+	check_state();
+
 	/*----- PROTECTED REGION END -----*/	//	USB2::always_executed_hook
 }
 
@@ -523,30 +523,21 @@ void USB2::write(const Tango::DevVarCharArray *argin)
 
 	if (max(output_queue_length(), 0) != 0) {
 		close();
-		resolve();
 		open();
 		reconnections += 1;
 		goto error;
 	}
-
 	while (bytes_total < bytes_to_write) {
-		int s = select(WRITE);
-		if (s == 0)
-			goto timeout;
-		else if (s < 0)
-			goto error;
-		else { /* s > 0 */ }
-
-		ssize_t bytes_written = _write(
+		ssize_t bytes_written;
+		conn_state = libusb_bulk_transfer(dev_handle, ep_down, 
 				argin_data.data() + bytes_total,
-				bytes_to_write - bytes_total);
-
+				bytes_to_write - bytes_total, &bytes_written, timeout);
+		if (conn_state == LIBUSB_ERROR_TIMEOUT)
+			goto timeout;
 		if ( bytes_written > 0) {
 			bytes_total += bytes_written;
 		} else if (bytes_written == 0) {
-			if (multiplexing == SELECT)
-				goto error;
-			/* Continue if multiplexing == SLEEP */
+			goto error;
 		} else { /* bytes_written < 0 */
 			goto error;
 		}
@@ -564,11 +555,12 @@ void USB2::write(const Tango::DevVarCharArray *argin)
 	return;
 
 error:
-	check_state(false);
+	check_state();
 	sleep(tout);
 timeout:
 	Tango::Except::throw_exception(
 			"", "Timeout expired", __PRETTY_FUNCTION__);
+
 	/*----- PROTECTED REGION END -----*/	//	USB2::write
 }
 //--------------------------------------------------------
@@ -760,14 +752,6 @@ void USB2::close()
 	data.clear();
 }
 
-ssize_t USB2::_write(unsigned char *buf, size_t count)
-{
-	int bytes_written;
-	conn_state = libusb_bulk_transfer(dev_handle,
-			ep_down, buf, count, &bytes_written, timeout);
-	return bytes_written;
-}
-
 void USB2::_read(size_t bytes_to_read)
 {
 	unsigned char buffer[10000];
@@ -775,40 +759,31 @@ void USB2::_read(size_t bytes_to_read)
 	int bytes_readed;
 
 	while (bytes_total < bytes_to_read) {
-		int s = select(READ);
-		if (s == 0)
-			goto timeout;
-		else if (s < 0)
-			goto error;
-		else { /* s > 0 */ }
-
-
 		conn_state = libusb_bulk_transfer(dev_handle, ep_up, buffer,
 				maxPacketSize, &bytes_readed, timeout);
-
+		if (conn_state == LIBUSB_ERROR_TIMEOUT)
+			goto timeout;
 		if (bytes_readed > 0) {
 			data.insert(data.end(), &buffer[0], &buffer[bytes_readed]);
 			bytes_total += bytes_readed;
 		} else if (bytes_readed == 0) {
-				goto error;
-		}	else { /* bytes_readed < 0 */
+			goto error;
+		} else { /* bytes_readed < 0 */
 			goto error;
 		}
 	}
 	return;
 error:
-	check_state(true);
+	check_state();
 	sleep(tout);
 timeout:
 	Tango::Except::throw_exception(
 			"", "Timeout expired", __PRETTY_FUNCTION__);
 }
 
-void USB2::check_state(bool forcing)
+void USB2::check_state()
 {
 	string mesg;
-	(void)forcing;
-
 	switch(conn_state)
 	{
 		case 0: /* Success */
@@ -816,8 +791,7 @@ void USB2::check_state(bool forcing)
 			set_status("Connected");
 			return;
 		case LIBUSB_ERROR_TIMEOUT:
-			mesg = "Timeout error";
-			break;
+			return;
 		case LIBUSB_ERROR_PIPE:
 			mesg = "Pipe error";
 			break;
@@ -845,17 +819,9 @@ void USB2::check_state(bool forcing)
 	DEBUG_STREAM << "Reconnecting due: " << mesg << endl;
 
 	close();
-	resolve();
 	open();
 	reconnections += 1;
 }
 
-int USB2::select(event_type et)
-{
-	return 1;
-}
-
-void USB2::resolve() {}
-
 /*----- PROTECTED REGION END -----*/	//	USB2::namespace_ending
 } //	namespace
diff --git a/src/USB2.h b/src/USB2.h
index 3ef058c..1b0fdbb 100644
--- a/src/USB2.h
+++ b/src/USB2.h
@@ -76,7 +76,7 @@ class USB2 : public TANGO_BASE_CLASS
 	string init_error;
 
 	libusb_context *ctx;
-	int vid, pid, ep_up, ep_down; // FIXME
+	int vid, pid, ep_up, ep_down;
 	libusb_device_handle *dev_handle;
 	int conn_state;
 
@@ -86,8 +86,6 @@ class USB2 : public TANGO_BASE_CLASS
 
 	int reconnections;
 
-	enum {SLEEP, SELECT} multiplexing;
-
 	timeval timeout_timeval, tout;
 /*----- PROTECTED REGION END -----*/	//	USB2::Data Members
 
@@ -268,9 +266,8 @@ public:
 	void close();
 	ssize_t _write(unsigned char*, size_t);
 	void _read(size_t);
-	void check_state(bool);
-	int select(event_type);
-	void resolve();
+	void check_state();
+
 /*----- PROTECTED REGION END -----*/	//	USB2::Additional Method prototypes
 };
 
-- 
GitLab