From 0d73a3890043089405a9028e97f3a53571b9eb51 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 27 Jun 2018 13:20:35 +0200 Subject: [PATCH] Initialize the socket library for the bluetooth discovery On Windows, the WSAStartup() function needs to be called, to initialize the socket library, before using any of the other WSA socket functions. This includes the functions used for the bluetooth device discovery. --- src/bluetooth.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/bluetooth.c b/src/bluetooth.c index 922ea4e..4630523 100644 --- a/src/bluetooth.c +++ b/src/bluetooth.c @@ -312,6 +312,12 @@ dc_bluetooth_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descri return DC_STATUS_NOMEMORY; } + // Initialize the socket library. + status = dc_socket_init (context); + if (status != DC_STATUS_SUCCESS) { + goto error_free; + } + #ifdef _WIN32 WSAQUERYSET wsaq; memset(&wsaq, 0, sizeof (wsaq)); @@ -328,7 +334,7 @@ dc_bluetooth_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descri } else { SYSERROR (context, errcode); status = dc_socket_syserror(errcode); - goto error_free; + goto error_socket_exit; } } @@ -340,7 +346,7 @@ dc_bluetooth_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descri s_errcode_t errcode = S_ERRNO; SYSERROR (context, errcode); status = dc_socket_syserror(errcode); - goto error_free; + goto error_socket_exit; } // Open a socket to the bluetooth adapter. @@ -349,7 +355,7 @@ dc_bluetooth_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descri s_errcode_t errcode = S_ERRNO; SYSERROR (context, errcode); status = dc_socket_syserror(errcode); - goto error_free; + goto error_socket_exit; } // Perform the bluetooth device discovery. The inquiry lasts for at @@ -379,6 +385,8 @@ dc_bluetooth_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descri error_close: hci_close_dev(fd); #endif +error_socket_exit: + dc_socket_exit (context); error_free: dc_iterator_deallocate ((dc_iterator_t *) iterator); return status; @@ -486,6 +494,7 @@ dc_bluetooth_iterator_free (dc_iterator_t *abstract) bt_free(iterator->devices); hci_close_dev(iterator->fd); #endif + dc_socket_exit (abstract->context); return DC_STATUS_SUCCESS; }