[PATCH v5 08/11] usbip: letting connection establishment replaceable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To introduce some application protocols like WebSocket, this patch 
allows to substitute connection establishment and termination. In 
combination with previous patch, both connection and transmission can 
be replaced.

usbip_connection_operations_t includes open and close operation. Open 
method returns usbip_sock_t which includes send, receive and close 
method. Then, transmission methods are replaced at the same time. 

Succeeding WebSocket patch uses this feature.

Signed-off-by: Nobuo Iwata <nobuo.iwata@xxxxxxxxxxxxxxx>
---
 tools/usb/usbip/libsrc/usbip_common.c  | 10 ++++++++++
 tools/usb/usbip/libsrc/usbip_common.h  | 11 +++++++++++
 tools/usb/usbip/src/usbip.c            |  1 +
 tools/usb/usbip/src/usbip_attach.c     |  6 +++---
 tools/usb/usbip/src/usbip_connect.c    |  6 +++---
 tools/usb/usbip/src/usbip_disconnect.c |  6 +++---
 tools/usb/usbip/src/usbip_list.c       |  6 +++---
 tools/usb/usbip/src/usbip_network.c    |  9 +++++++--
 tools/usb/usbip/src/usbip_network.h    |  3 +--
 9 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c b/tools/usb/usbip/libsrc/usbip_common.c
index dc0712c..54efa10 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -297,3 +297,13 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
 	sock->shutdown = shutdown;
 }
 
+usbip_connection_operations_t usbip_conn_ops = {NULL, NULL};
+
+void usbip_conn_init(
+	usbip_sock_t *(*open)(char *host, char *port),
+	void (*close)(usbip_sock_t *sock))
+{
+	usbip_conn_ops.open = open;
+	usbip_conn_ops.close = close;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h b/tools/usb/usbip/libsrc/usbip_common.h
index 0dcbd99..07c411f 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -148,4 +148,15 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
 	ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
 	void (*shutdown)(void *arg));
 
+typedef struct usbip_connection_operations {
+	usbip_sock_t *(*open)(char *host, char *port);
+	void (*close)(usbip_sock_t *sock);
+} usbip_connection_operations_t;
+
+extern usbip_connection_operations_t usbip_conn_ops;
+
+void usbip_conn_init(
+	usbip_sock_t *(*open)(char *host, char *port),
+	void (*close)(usbip_sock_t *sock));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index 9d1468f..505e384 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -202,6 +202,7 @@ int main(int argc, char *argv[])
 				argc -= optind;
 				argv += optind;
 				optind = 0;
+				usbip_net_tcp_conn_init();
 				rc = run_command(&cmds[i], argc, argv);
 				goto out;
 			}
diff --git a/tools/usb/usbip/src/usbip_attach.c b/tools/usb/usbip/src/usbip_attach.c
index ae0ca6e..c67e3d2 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -135,7 +135,7 @@ static int attach_device(char *host, char *busid)
 	int rc;
 	int rhport;
 
-	sock = usbip_net_tcp_connect(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, usbip_port_string);
 	if (!sock) {
 		err("tcp connect");
 		goto err_out;
@@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid)
 		usbip_ux_join(ux);
 	}
 	usbip_ux_cleanup(&ux);
-	usbip_net_tcp_close(sock);
+	usbip_conn_ops.close(sock);
 
 	return 0;
 err_cleanup_ux:
 	usbip_ux_cleanup(&ux);
 err_close_conn:
-	usbip_net_tcp_close(sock);
+	usbip_conn_ops.close(sock);
 err_out:
 	return -1;
 }
diff --git a/tools/usb/usbip/src/usbip_connect.c b/tools/usb/usbip/src/usbip_connect.c
index 5f9505c..8dabd0b 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -150,7 +150,7 @@ static int connect_device(char *host, char *busid)
 		goto err_out;
 	}
 
-	sock = usbip_net_tcp_connect(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, usbip_port_string);
 	if (!sock) {
 		err("tcp connect");
 		goto err_unbind_device;
@@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid)
 		usbip_unbind_device(busid);
 	}
 	usbip_ux_cleanup(&ux);
-	usbip_net_tcp_close(sock);
+	usbip_conn_ops.close(sock);
 
 	return 0;
 err_cleanup_ux:
 	usbip_ux_cleanup(&ux);
 err_close_conn:
-	usbip_net_tcp_close(sock);
+	usbip_conn_ops.close(sock);
 err_unbind_device:
 	usbip_unbind_device(busid);
 err_out:
diff --git a/tools/usb/usbip/src/usbip_disconnect.c b/tools/usb/usbip/src/usbip_disconnect.c
index 8da1717..1bda962 100644
--- a/tools/usb/usbip/src/usbip_disconnect.c
+++ b/tools/usb/usbip/src/usbip_disconnect.c
@@ -136,7 +136,7 @@ static int disconnect_device(char *host, char *busid)
 	usbip_sock_t *sock;
 	int rc;
 
-	sock = usbip_net_tcp_connect(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, usbip_port_string);
 	if (!sock) {
 		err("tcp connect");
 		return -1;
@@ -145,11 +145,11 @@ static int disconnect_device(char *host, char *busid)
 	rc = unexport_device(busid, sock);
 	if (rc < 0) {
 		err("unexport");
-		usbip_net_tcp_close(sock);
+		usbip_conn_ops.close(sock);
 		return -1;
 	}
 
-	usbip_net_tcp_close(sock);
+	usbip_conn_ops.close(sock);
 
 	if (!usbip_ux_installed()) {
 		rc = usbip_unbind_device(busid);
diff --git a/tools/usb/usbip/src/usbip_list.c b/tools/usb/usbip/src/usbip_list.c
index cc86132..64f143a 100644
--- a/tools/usb/usbip/src/usbip_list.c
+++ b/tools/usb/usbip/src/usbip_list.c
@@ -132,7 +132,7 @@ static int list_importable_devices(char *host)
 	int rc;
 	usbip_sock_t *sock;
 
-	sock = usbip_net_tcp_connect(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, usbip_port_string);
 	if (!sock) {
 		err("could not connect to %s:%s: %s", host,
 		    usbip_port_string, usbip_net_gai_strerror(sock->fd));
@@ -143,11 +143,11 @@ static int list_importable_devices(char *host)
 	rc = get_importable_devices(host, sock);
 	if (rc < 0) {
 		err("failed to get device list from %s", host);
-		usbip_net_tcp_close(sock);
+		usbip_conn_ops.close(sock);
 		return -1;
 	}
 
-	usbip_net_tcp_close(sock);
+	usbip_conn_ops.close(sock);
 
 	return 0;
 }
diff --git a/tools/usb/usbip/src/usbip_network.c b/tools/usb/usbip/src/usbip_network.c
index 7673e1a..8906d92 100644
--- a/tools/usb/usbip/src/usbip_network.c
+++ b/tools/usb/usbip/src/usbip_network.c
@@ -268,7 +268,7 @@ int usbip_net_set_v6only(int sockfd)
 /*
  * IPv6 Ready
  */
-usbip_sock_t *usbip_net_tcp_connect(char *hostname, char *service)
+static usbip_sock_t *net_tcp_open(char *hostname, char *service)
 {
 	struct addrinfo hints, *res, *rp;
 	int sockfd;
@@ -321,12 +321,17 @@ usbip_sock_t *usbip_net_tcp_connect(char *hostname, char *service)
 	return sock;
 }
 
-void usbip_net_tcp_close(usbip_sock_t *sock)
+static void net_tcp_close(usbip_sock_t *sock)
 {
 	close(sock->fd);
 	free(sock);
 }
 
+void usbip_net_tcp_conn_init(void)
+{
+	usbip_conn_init(net_tcp_open, net_tcp_close);
+}
+
 static const char *s_unknown_error = "?";
 
 const char *usbip_net_gai_strerror(int errcode)
diff --git a/tools/usb/usbip/src/usbip_network.h b/tools/usb/usbip/src/usbip_network.h
index f84775e..7d9327d 100644
--- a/tools/usb/usbip/src/usbip_network.h
+++ b/tools/usb/usbip/src/usbip_network.h
@@ -181,8 +181,7 @@ int usbip_net_set_reuseaddr(int sockfd);
 int usbip_net_set_nodelay(int sockfd);
 int usbip_net_set_keepalive(int sockfd);
 int usbip_net_set_v6only(int sockfd);
-usbip_sock_t *usbip_net_tcp_connect(char *hostname, char *port);
-void usbip_net_tcp_close(usbip_sock_t *sock);
+void usbip_net_tcp_conn_init(void);
 const char *usbip_net_gai_strerror(int errcode);
 
 #endif /* __USBIP_NETWORK_H */
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux