diff --git a/include/libdivecomputer/common.h b/include/libdivecomputer/common.h index 293de3b..c6c093a 100644 --- a/include/libdivecomputer/common.h +++ b/include/libdivecomputer/common.h @@ -41,6 +41,15 @@ typedef enum dc_status_t { DC_STATUS_CANCELLED = -10 } dc_status_t; +typedef enum dc_transport_t { + DC_TRANSPORT_NONE, + DC_TRANSPORT_SERIAL, + DC_TRANSPORT_USB, + DC_TRANSPORT_USBHID, + DC_TRANSPORT_IRDA, + DC_TRANSPORT_BLUETOOTH +} dc_transport_t; + typedef enum dc_family_t { DC_FAMILY_NULL = 0, /* Suunto */ diff --git a/include/libdivecomputer/descriptor.h b/include/libdivecomputer/descriptor.h index a78b600..12d9bab 100644 --- a/include/libdivecomputer/descriptor.h +++ b/include/libdivecomputer/descriptor.h @@ -29,15 +29,6 @@ extern "C" { #endif /* __cplusplus */ -typedef enum dc_transport_t { - DC_TRANSPORT_NONE, - DC_TRANSPORT_SERIAL, - DC_TRANSPORT_USB, - DC_TRANSPORT_USBHID, - DC_TRANSPORT_IRDA, - DC_TRANSPORT_BLUETOOTH -} dc_transport_t; - typedef struct dc_descriptor_t dc_descriptor_t; dc_status_t diff --git a/include/libdivecomputer/iostream.h b/include/libdivecomputer/iostream.h index 832680d..e2cb439 100644 --- a/include/libdivecomputer/iostream.h +++ b/include/libdivecomputer/iostream.h @@ -82,6 +82,15 @@ typedef enum dc_line_t { DC_LINE_RNG = 0x08, /**< Ring indicator */ } dc_line_t; +/** + * Get the transport type. + * + * @param[in] iostream A valid I/O stream. + * @returns The transport type of the I/O stream. + */ +dc_transport_t +dc_iostream_get_transport (dc_iostream_t *iostream); + /** * Set the read timeout. * diff --git a/src/bluetooth.c b/src/bluetooth.c index 5a37a5b..702782d 100644 --- a/src/bluetooth.c +++ b/src/bluetooth.c @@ -504,7 +504,7 @@ dc_bluetooth_open (dc_iostream_t **out, dc_context_t *context, dc_bluetooth_addr INFO (context, "Open: address=" DC_ADDRESS_FORMAT ", port=%u", address, port); // Allocate memory. - device = (dc_socket_t *) dc_iostream_allocate (context, &dc_bluetooth_vtable); + device = (dc_socket_t *) dc_iostream_allocate (context, &dc_bluetooth_vtable, DC_TRANSPORT_BLUETOOTH); if (device == NULL) { SYSERROR (context, S_ENOMEM); return DC_STATUS_NOMEMORY; diff --git a/src/custom.c b/src/custom.c index 519a39e..68aa881 100644 --- a/src/custom.c +++ b/src/custom.c @@ -69,17 +69,17 @@ static const dc_iostream_vtable_t dc_custom_vtable = { }; dc_status_t -dc_custom_open (dc_iostream_t **out, dc_context_t *context, const dc_custom_cbs_t *callbacks, void *userdata) +dc_custom_open (dc_iostream_t **out, dc_context_t *context, dc_transport_t transport, const dc_custom_cbs_t *callbacks, void *userdata) { dc_custom_t *custom = NULL; if (out == NULL || callbacks == NULL) return DC_STATUS_INVALIDARGS; - INFO (context, "Open: custom"); + INFO (context, "Open: transport=%u", transport); // Allocate memory. - custom = (dc_custom_t *) dc_iostream_allocate (context, &dc_custom_vtable); + custom = (dc_custom_t *) dc_iostream_allocate (context, &dc_custom_vtable, transport); if (custom == NULL) { ERROR (context, "Failed to allocate memory."); return DC_STATUS_NOMEMORY; diff --git a/src/custom.h b/src/custom.h index f1ab5da..9618dce 100644 --- a/src/custom.h +++ b/src/custom.h @@ -58,7 +58,7 @@ typedef struct dc_custom_cbs_t { * on failure. */ dc_status_t -dc_custom_open (dc_iostream_t **iostream, dc_context_t *context, const dc_custom_cbs_t *callbacks, void *userdata); +dc_custom_open (dc_iostream_t **iostream, dc_context_t *context, dc_transport_t transport, const dc_custom_cbs_t *callbacks, void *userdata); #ifdef __cplusplus } diff --git a/src/iostream-private.h b/src/iostream-private.h index b9bd4f1..f9c0883 100644 --- a/src/iostream-private.h +++ b/src/iostream-private.h @@ -35,6 +35,7 @@ typedef struct dc_iostream_vtable_t dc_iostream_vtable_t; struct dc_iostream_t { const dc_iostream_vtable_t *vtable; dc_context_t *context; + dc_transport_t transport; }; struct dc_iostream_vtable_t { @@ -70,7 +71,7 @@ struct dc_iostream_vtable_t { }; dc_iostream_t * -dc_iostream_allocate (dc_context_t *context, const dc_iostream_vtable_t *vtable); +dc_iostream_allocate (dc_context_t *context, const dc_iostream_vtable_t *vtable, dc_transport_t transport); void dc_iostream_deallocate (dc_iostream_t *iostream); diff --git a/src/iostream.c b/src/iostream.c index c1e3017..9681d02 100644 --- a/src/iostream.c +++ b/src/iostream.c @@ -27,7 +27,7 @@ #include "context-private.h" dc_iostream_t * -dc_iostream_allocate (dc_context_t *context, const dc_iostream_vtable_t *vtable) +dc_iostream_allocate (dc_context_t *context, const dc_iostream_vtable_t *vtable, dc_transport_t transport) { dc_iostream_t *iostream = NULL; @@ -44,6 +44,7 @@ dc_iostream_allocate (dc_context_t *context, const dc_iostream_vtable_t *vtable) // Initialize the base class. iostream->vtable = vtable; iostream->context = context; + iostream->transport = transport; return iostream; } @@ -63,6 +64,15 @@ dc_iostream_isinstance (dc_iostream_t *iostream, const dc_iostream_vtable_t *vta return iostream->vtable == vtable; } +dc_transport_t +dc_iostream_get_transport (dc_iostream_t *iostream) +{ + if (iostream == NULL) + return DC_TRANSPORT_NONE; + + return iostream->transport; +} + dc_status_t dc_iostream_set_timeout (dc_iostream_t *iostream, int timeout) { diff --git a/src/irda.c b/src/irda.c index d1aecfb..c8adb0d 100644 --- a/src/irda.c +++ b/src/irda.c @@ -300,7 +300,7 @@ dc_irda_open (dc_iostream_t **out, dc_context_t *context, unsigned int address, INFO (context, "Open: address=%08x, lsap=%u", address, lsap); // Allocate memory. - device = (dc_socket_t *) dc_iostream_allocate (context, &dc_irda_vtable); + device = (dc_socket_t *) dc_iostream_allocate (context, &dc_irda_vtable, DC_TRANSPORT_IRDA); if (device == NULL) { SYSERROR (context, S_ENOMEM); return DC_STATUS_NOMEMORY; diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index a0c3724..e5e55f0 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -33,6 +33,7 @@ dc_descriptor_get_type dc_descriptor_get_model dc_descriptor_get_transport +dc_iostream_get_transport dc_iostream_set_timeout dc_iostream_set_latency dc_iostream_set_break diff --git a/src/serial_posix.c b/src/serial_posix.c index 6f2ae71..e9dbff0 100644 --- a/src/serial_posix.c +++ b/src/serial_posix.c @@ -271,7 +271,7 @@ dc_serial_open (dc_iostream_t **out, dc_context_t *context, const char *name) INFO (context, "Open: name=%s", name); // Allocate memory. - device = (dc_serial_t *) dc_iostream_allocate (context, &dc_serial_vtable); + device = (dc_serial_t *) dc_iostream_allocate (context, &dc_serial_vtable, DC_TRANSPORT_SERIAL); if (device == NULL) { SYSERROR (context, ENOMEM); return DC_STATUS_NOMEMORY; diff --git a/src/serial_win32.c b/src/serial_win32.c index b0c6050..067bb72 100644 --- a/src/serial_win32.c +++ b/src/serial_win32.c @@ -276,7 +276,7 @@ dc_serial_open (dc_iostream_t **out, dc_context_t *context, const char *name) } // Allocate memory. - device = (dc_serial_t *) dc_iostream_allocate (context, &dc_serial_vtable); + device = (dc_serial_t *) dc_iostream_allocate (context, &dc_serial_vtable, DC_TRANSPORT_SERIAL); if (device == NULL) { SYSERROR (context, ERROR_OUTOFMEMORY); return DC_STATUS_NOMEMORY; diff --git a/src/usbhid.c b/src/usbhid.c index 244e175..1347423 100644 --- a/src/usbhid.c +++ b/src/usbhid.c @@ -473,7 +473,7 @@ dc_usbhid_open (dc_iostream_t **out, dc_context_t *context, unsigned int vid, un INFO (context, "Open: vid=%04x, pid=%04x", vid, pid); // Allocate memory. - usbhid = (dc_usbhid_t *) dc_iostream_allocate (context, &dc_usbhid_vtable); + usbhid = (dc_usbhid_t *) dc_iostream_allocate (context, &dc_usbhid_vtable, DC_TRANSPORT_USBHID); if (usbhid == NULL) { ERROR (context, "Out of memory."); return DC_STATUS_NOMEMORY;