From c5105a37884888fac91a0113338816e379261be4 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 14 Jul 2012 16:16:36 +0200 Subject: [PATCH] Remove the IrDA init and cleanup functions. The Windows WSAStartup() and WSACleanup() functions are now called automatically when opening and closing IrDA sockets. This causes no problems because these functions are reference counted and can be called multiple times. In practice nothing changes because the Uwatec Smart backend already called these functions for every connection. --- src/irda.c | 76 ++++++++++++++++++++++------------------------ src/irda.h | 4 --- src/uwatec_smart.c | 9 ------ 3 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/irda.c b/src/irda.c index fb6076a..e8c7424 100644 --- a/src/irda.c +++ b/src/irda.c @@ -113,45 +113,6 @@ const char* irda_errmsg (void) } -int irda_init (void) -{ -#ifdef _WIN32 - WSADATA wsaData; - WORD wVersionRequested = MAKEWORD (2, 2); - if (WSAStartup (wVersionRequested, &wsaData) != 0) { - TRACE ("WSAStartup"); - return -1; - } - - // Confirm that the WinSock DLL supports 2.2. - // Note that if the DLL supports versions greater - // than 2.2 in addition to 2.2, it will still return - // 2.2 in wVersion since that is the version we requested. - if (LOBYTE (wsaData.wVersion) != 2 || - HIBYTE (wsaData.wVersion) != 2) { - TRACE ("wsaData.wVersion"); - WSACleanup (); - return -1; - } -#endif - - return 0; -} - - -int irda_cleanup (void) -{ -#ifdef _WIN32 - if (WSACleanup () != 0) { - TRACE ("WSACleanup"); - return -1; - } -#endif - - return 0; -} - - int irda_socket_open (irda_t **out) { @@ -168,6 +129,28 @@ irda_socket_open (irda_t **out) // Default to blocking reads. device->timeout = -1; +#ifdef _WIN32 + // Initialize the winsock dll. + WSADATA wsaData; + WORD wVersionRequested = MAKEWORD (2, 2); + if (WSAStartup (wVersionRequested, &wsaData) != 0) { + TRACE ("WSAStartup"); + free (device); + return -1; + } + + // Confirm that the winsock dll supports version 2.2. + // Note that if the dll supports versions greater than 2.2 in addition to + // 2.2, it will still return 2.2 since that is the version we requested. + if (LOBYTE (wsaData.wVersion) != 2 || + HIBYTE (wsaData.wVersion) != 2) { + TRACE ("wsaData.wVersion"); + WSACleanup (); + free (device); + return -1; + } +#endif + // Open the socket. device->fd = socket (AF_IRDA, SOCK_STREAM, 0); #ifdef _WIN32 @@ -176,6 +159,9 @@ irda_socket_open (irda_t **out) if (device->fd == -1) { #endif TRACE ("socket"); +#ifdef _WIN32 + WSACleanup (); +#endif free (device); return -1; } @@ -202,11 +188,23 @@ irda_socket_close (irda_t *device) #else if (close (device->fd) != 0) { TRACE ("close"); +#endif +#ifdef _WIN32 + WSACleanup (); #endif free (device); return -1; } +#ifdef _WIN32 + // Terminate the winsock dll. + if (WSACleanup () != 0) { + TRACE ("WSACleanup"); + free (device); + return -1; + } +#endif + // Free memory. free (device); diff --git a/src/irda.h b/src/irda.h index 7480e34..e2dd224 100644 --- a/src/irda.h +++ b/src/irda.h @@ -34,10 +34,6 @@ int irda_errcode (void); const char* irda_errmsg (void); -int irda_init (void); - -int irda_cleanup (void); - int irda_socket_open (irda_t **device); int irda_socket_close (irda_t *device); diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index 1ca24c6..cfc6caf 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -170,13 +170,10 @@ uwatec_smart_device_open (dc_device_t **out) device->systime = (dc_ticks_t) -1; device->devtime = 0; - irda_init (); - // Open the irda socket. int rc = irda_socket_open (&device->socket); if (rc == -1) { WARNING ("Failed to open the irda socket."); - irda_cleanup (); free (device); return DC_STATUS_IO; } @@ -186,7 +183,6 @@ uwatec_smart_device_open (dc_device_t **out) if (rc == -1) { WARNING ("Failed to discover the device."); irda_socket_close (device->socket); - irda_cleanup (); free (device); return DC_STATUS_IO; } @@ -194,7 +190,6 @@ uwatec_smart_device_open (dc_device_t **out) if (device->address == 0) { WARNING ("No dive computer found."); irda_socket_close (device->socket); - irda_cleanup (); free (device); return DC_STATUS_IO; } @@ -204,7 +199,6 @@ uwatec_smart_device_open (dc_device_t **out) if (rc == -1) { WARNING ("Failed to connect the device."); irda_socket_close (device->socket); - irda_cleanup (); free (device); return DC_STATUS_IO; } @@ -228,13 +222,10 @@ uwatec_smart_device_close (dc_device_t *abstract) // Close the device. if (irda_socket_close (device->socket) == -1) { - irda_cleanup (); free (device); return DC_STATUS_IO; } - irda_cleanup (); - // Free memory. free (device);