From 85eef19f8f9b1510eb3ae6e79f96ee77b7b1a8b1 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 19 Dec 2017 20:25:07 +0100 Subject: [PATCH] Integrate the connect step into the open function There is no need to expose the two step connection setup of the underlying socket interface in the public api. Doing so may complicate the implementation on platforms where the native api is not based on the socket interface (e.g. Mac OS X). Note that the function to connect based on the IrDA service name is removed. It's not used anywhere in libdivecomputer and since IrDA is an outdated technology nowadays, it's unlikely we'll need it in the future. --- src/bluetooth.c | 48 +++++++++++--------------- src/bluetooth.h | 12 +------ src/irda.c | 84 ++++++++++------------------------------------ src/irda.h | 24 +------------ src/uwatec_smart.c | 9 +---- 5 files changed, 40 insertions(+), 137 deletions(-) diff --git a/src/bluetooth.c b/src/bluetooth.c index 4581cd7..e6dc09d 100644 --- a/src/bluetooth.c +++ b/src/bluetooth.c @@ -443,7 +443,7 @@ dc_bluetooth_iterator_free (dc_iterator_t *abstract) #endif dc_status_t -dc_bluetooth_open (dc_iostream_t **out, dc_context_t *context) +dc_bluetooth_open (dc_iostream_t **out, dc_context_t *context, dc_bluetooth_address_t address, unsigned int port) { #ifdef BLUETOOTH dc_status_t status = DC_STATUS_SUCCESS; @@ -452,6 +452,8 @@ dc_bluetooth_open (dc_iostream_t **out, dc_context_t *context) if (out == NULL) return DC_STATUS_INVALIDARGS; + INFO (context, "Open: address=" DC_ADDRESS_FORMAT ", port=%u", address, port); + // Allocate memory. device = (dc_socket_t *) dc_iostream_allocate (context, &dc_bluetooth_vtable); if (device == NULL) { @@ -469,29 +471,6 @@ dc_bluetooth_open (dc_iostream_t **out, dc_context_t *context) goto error_free; } - *out = (dc_iostream_t *) device; - - return DC_STATUS_SUCCESS; - -error_free: - dc_iostream_deallocate ((dc_iostream_t *) device); - return status; -#else - return DC_STATUS_UNSUPPORTED; -#endif -} - -dc_status_t -dc_bluetooth_connect (dc_iostream_t *abstract, dc_bluetooth_address_t address, unsigned int port) -{ -#ifdef BLUETOOTH - dc_socket_t *device = (dc_socket_t *) abstract; - - if (!ISINSTANCE (abstract)) - return DC_STATUS_INVALIDARGS; - - INFO (abstract->context, "Connect: address=" DC_ADDRESS_FORMAT ", port=%d", address, port); - #ifdef _WIN32 SOCKADDR_BTH sa; sa.addressFamily = AF_BTH; @@ -507,16 +486,29 @@ dc_bluetooth_connect (dc_iostream_t *abstract, dc_bluetooth_address_t address, u sa.rc_family = AF_BLUETOOTH; dc_address_set (&sa.rc_bdaddr, address); if (port == 0) { - dc_status_t rc = dc_bluetooth_sdp (&sa.rc_channel, abstract->context, &sa.rc_bdaddr); - if (rc != DC_STATUS_SUCCESS) { - return rc; + status = dc_bluetooth_sdp (&sa.rc_channel, context, &sa.rc_bdaddr); + if (status != DC_STATUS_SUCCESS) { + goto error_close; } } else { sa.rc_channel = port; } #endif - return dc_socket_connect (&device->base, (struct sockaddr *) &sa, sizeof (sa)); + status = dc_socket_connect (&device->base, (struct sockaddr *) &sa, sizeof (sa)); + if (status != DC_STATUS_SUCCESS) { + goto error_close; + } + + *out = (dc_iostream_t *) device; + + return DC_STATUS_SUCCESS; + +error_close: + dc_socket_close (&device->base); +error_free: + dc_iostream_deallocate ((dc_iostream_t *) device); + return status; #else return DC_STATUS_UNSUPPORTED; #endif diff --git a/src/bluetooth.h b/src/bluetooth.h index 264e833..bc5bb2a 100644 --- a/src/bluetooth.h +++ b/src/bluetooth.h @@ -87,23 +87,13 @@ dc_bluetooth_iterator_new (dc_iterator_t **iterator, dc_context_t *context, dc_d * * @param[out] iostream A location to store the bluetooth connection. * @param[in] context A valid context object. - * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code - * on failure. - */ -dc_status_t -dc_bluetooth_open (dc_iostream_t **iostream, dc_context_t *context); - -/** - * Connect to an bluetooth device. - * - * @param[in] iostream A valid bluetooth connection. * @param[in] address The bluetooth device address. * @param[in] port The bluetooth port number. * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code * on failure. */ dc_status_t -dc_bluetooth_connect (dc_iostream_t *iostream, dc_bluetooth_address_t address, unsigned int port); +dc_bluetooth_open (dc_iostream_t **iostream, dc_context_t *context, dc_bluetooth_address_t address, unsigned int port); #ifdef __cplusplus } diff --git a/src/irda.c b/src/irda.c index da5773f..1ac754b 100644 --- a/src/irda.c +++ b/src/irda.c @@ -289,7 +289,7 @@ dc_irda_iterator_next (dc_iterator_t *abstract, void *out) #endif dc_status_t -dc_irda_open (dc_iostream_t **out, dc_context_t *context) +dc_irda_open (dc_iostream_t **out, dc_context_t *context, unsigned int address, unsigned int lsap) { #ifdef IRDA dc_status_t status = DC_STATUS_SUCCESS; @@ -298,6 +298,8 @@ dc_irda_open (dc_iostream_t **out, dc_context_t *context) if (out == NULL) return DC_STATUS_INVALIDARGS; + INFO (context, "Open: address=%08x, lsap=%u", address, lsap); + // Allocate memory. device = (dc_socket_t *) dc_iostream_allocate (context, &dc_irda_vtable); if (device == NULL) { @@ -311,71 +313,6 @@ dc_irda_open (dc_iostream_t **out, dc_context_t *context) goto error_free; } - *out = (dc_iostream_t *) device; - - return DC_STATUS_SUCCESS; - -error_free: - dc_iostream_deallocate ((dc_iostream_t *) device); - return status; -#else - return DC_STATUS_UNSUPPORTED; -#endif -} - -dc_status_t -dc_irda_connect_name (dc_iostream_t *abstract, unsigned int address, const char *name) -{ -#ifdef IRDA - dc_socket_t *device = (dc_socket_t *) abstract; - - if (!ISINSTANCE (abstract)) - return DC_STATUS_INVALIDARGS; - - INFO (abstract->context, "Connect: address=%08x, name=%s", address, name ? name : ""); - -#ifdef _WIN32 - SOCKADDR_IRDA peer; - peer.irdaAddressFamily = AF_IRDA; - peer.irdaDeviceID[0] = (address ) & 0xFF; - peer.irdaDeviceID[1] = (address >> 8) & 0xFF; - peer.irdaDeviceID[2] = (address >> 16) & 0xFF; - peer.irdaDeviceID[3] = (address >> 24) & 0xFF; - if (name) { - strncpy (peer.irdaServiceName, name, sizeof(peer.irdaServiceName) - 1); - peer.irdaServiceName[sizeof(peer.irdaServiceName) - 1] = '\0'; - } else { - memset (peer.irdaServiceName, 0x00, sizeof(peer.irdaServiceName)); - } -#else - struct sockaddr_irda peer; - peer.sir_family = AF_IRDA; - peer.sir_addr = address; - if (name) { - strncpy (peer.sir_name, name, sizeof(peer.sir_name) - 1); - peer.sir_name[sizeof(peer.sir_name) - 1] = '\0'; - } else { - memset (peer.sir_name, 0x00, sizeof(peer.sir_name)); - } -#endif - - return dc_socket_connect (&device->base, (struct sockaddr *) &peer, sizeof (peer)); -#else - return DC_STATUS_UNSUPPORTED; -#endif -} - -dc_status_t -dc_irda_connect_lsap (dc_iostream_t *abstract, unsigned int address, unsigned int lsap) -{ -#ifdef IRDA - dc_socket_t *device = (dc_socket_t *) abstract; - - if (!ISINSTANCE (abstract)) - return DC_STATUS_INVALIDARGS; - - INFO (abstract->context, "Connect: address=%08x, lsap=%u", address, lsap); - #ifdef _WIN32 SOCKADDR_IRDA peer; peer.irdaAddressFamily = AF_IRDA; @@ -392,7 +329,20 @@ dc_irda_connect_lsap (dc_iostream_t *abstract, unsigned int address, unsigned in memset (peer.sir_name, 0x00, sizeof(peer.sir_name)); #endif - return dc_socket_connect (&device->base, (struct sockaddr *) &peer, sizeof (peer)); + status = dc_socket_connect (&device->base, (struct sockaddr *) &peer, sizeof (peer)); + if (status != DC_STATUS_SUCCESS) { + goto error_close; + } + + *out = (dc_iostream_t *) device; + + return DC_STATUS_SUCCESS; + +error_close: + dc_socket_close (&device->base); +error_free: + dc_iostream_deallocate ((dc_iostream_t *) device); + return status; #else return DC_STATUS_UNSUPPORTED; #endif diff --git a/src/irda.h b/src/irda.h index c0ab8d6..92e9557 100644 --- a/src/irda.h +++ b/src/irda.h @@ -78,35 +78,13 @@ dc_irda_iterator_new (dc_iterator_t **iterator, dc_context_t *context, dc_descri * * @param[out] iostream A location to store the IrDA connection. * @param[in] context A valid context object. - * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code - * on failure. - */ -dc_status_t -dc_irda_open (dc_iostream_t **iostream, dc_context_t *context); - -/** - * Connect to an IrDA device. - * - * @param[in] iostream A valid IrDA connection. - * @param[in] address The IrDA device address. - * @param[in] name The IrDA service name. - * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code - * on failure. - */ -dc_status_t -dc_irda_connect_name (dc_iostream_t *iostream, unsigned int address, const char *name); - -/** - * Connect to an IrDA device. - * - * @param[in] iostream A valid IrDA connection. * @param[in] address The IrDA device address. * @param[in] lsap The IrDA LSAP number. * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code * on failure. */ dc_status_t -dc_irda_connect_lsap (dc_iostream_t *iostream, unsigned int address, unsigned int lsap); +dc_irda_open (dc_iostream_t **iostream, dc_context_t *context, unsigned int address, unsigned int lsap); #ifdef __cplusplus } diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index f82f3e1..a3be17d 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -200,19 +200,12 @@ uwatec_smart_device_open (dc_device_t **out, dc_context_t *context) } // Open the irda socket. - status = dc_irda_open (&device->iostream, context); + status = dc_irda_open (&device->iostream, context, dc_irda_device_get_address (dev), 1); if (status != DC_STATUS_SUCCESS) { ERROR (context, "Failed to open the irda socket."); goto error_device_free; } - // Connect the device. - status = dc_irda_connect_lsap (device->iostream, dc_irda_device_get_address (dev), 1); - if (status != DC_STATUS_SUCCESS) { - ERROR (context, "Failed to connect the device."); - goto error_close; - } - // Perform the handshaking. status = uwatec_smart_handshake (device); if (status != DC_STATUS_SUCCESS) {