Add the transport type to the I/O stream

Add a function to query the underlying transport type. This allows the
dive computer backends to implement transport specific behaviour where
necessary.

For the built-in I/O implementations, the transport type is obviously
always hardcoded, but for a custom I/O implementation the application
needs to provide the correct type. Hence the transport type can't be
hardcoded in the vtable and needs to be passed as a parameter.
This commit is contained in:
Jef Driesen 2017-12-16 13:50:25 +01:00
parent 7c95581826
commit 3230387fff
13 changed files with 41 additions and 20 deletions

View File

@ -41,6 +41,15 @@ typedef enum dc_status_t {
DC_STATUS_CANCELLED = -10 DC_STATUS_CANCELLED = -10
} dc_status_t; } 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 { typedef enum dc_family_t {
DC_FAMILY_NULL = 0, DC_FAMILY_NULL = 0,
/* Suunto */ /* Suunto */

View File

@ -29,15 +29,6 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #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; typedef struct dc_descriptor_t dc_descriptor_t;
dc_status_t dc_status_t

View File

@ -82,6 +82,15 @@ typedef enum dc_line_t {
DC_LINE_RNG = 0x08, /**< Ring indicator */ DC_LINE_RNG = 0x08, /**< Ring indicator */
} dc_line_t; } 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. * Set the read timeout.
* *

View File

@ -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); INFO (context, "Open: address=" DC_ADDRESS_FORMAT ", port=%u", address, port);
// Allocate memory. // 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) { if (device == NULL) {
SYSERROR (context, S_ENOMEM); SYSERROR (context, S_ENOMEM);
return DC_STATUS_NOMEMORY; return DC_STATUS_NOMEMORY;

View File

@ -69,17 +69,17 @@ static const dc_iostream_vtable_t dc_custom_vtable = {
}; };
dc_status_t 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; dc_custom_t *custom = NULL;
if (out == NULL || callbacks == NULL) if (out == NULL || callbacks == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
INFO (context, "Open: custom"); INFO (context, "Open: transport=%u", transport);
// Allocate memory. // 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) { if (custom == NULL) {
ERROR (context, "Failed to allocate memory."); ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY; return DC_STATUS_NOMEMORY;

View File

@ -58,7 +58,7 @@ typedef struct dc_custom_cbs_t {
* on failure. * on failure.
*/ */
dc_status_t 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 #ifdef __cplusplus
} }

View File

@ -35,6 +35,7 @@ typedef struct dc_iostream_vtable_t dc_iostream_vtable_t;
struct dc_iostream_t { struct dc_iostream_t {
const dc_iostream_vtable_t *vtable; const dc_iostream_vtable_t *vtable;
dc_context_t *context; dc_context_t *context;
dc_transport_t transport;
}; };
struct dc_iostream_vtable_t { struct dc_iostream_vtable_t {
@ -70,7 +71,7 @@ struct dc_iostream_vtable_t {
}; };
dc_iostream_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 void
dc_iostream_deallocate (dc_iostream_t *iostream); dc_iostream_deallocate (dc_iostream_t *iostream);

View File

@ -27,7 +27,7 @@
#include "context-private.h" #include "context-private.h"
dc_iostream_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)
{ {
dc_iostream_t *iostream = NULL; 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. // Initialize the base class.
iostream->vtable = vtable; iostream->vtable = vtable;
iostream->context = context; iostream->context = context;
iostream->transport = transport;
return iostream; return iostream;
} }
@ -63,6 +64,15 @@ dc_iostream_isinstance (dc_iostream_t *iostream, const dc_iostream_vtable_t *vta
return iostream->vtable == vtable; 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_status_t
dc_iostream_set_timeout (dc_iostream_t *iostream, int timeout) dc_iostream_set_timeout (dc_iostream_t *iostream, int timeout)
{ {

View File

@ -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); INFO (context, "Open: address=%08x, lsap=%u", address, lsap);
// Allocate memory. // 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) { if (device == NULL) {
SYSERROR (context, S_ENOMEM); SYSERROR (context, S_ENOMEM);
return DC_STATUS_NOMEMORY; return DC_STATUS_NOMEMORY;

View File

@ -33,6 +33,7 @@ dc_descriptor_get_type
dc_descriptor_get_model dc_descriptor_get_model
dc_descriptor_get_transport dc_descriptor_get_transport
dc_iostream_get_transport
dc_iostream_set_timeout dc_iostream_set_timeout
dc_iostream_set_latency dc_iostream_set_latency
dc_iostream_set_break dc_iostream_set_break

View File

@ -271,7 +271,7 @@ dc_serial_open (dc_iostream_t **out, dc_context_t *context, const char *name)
INFO (context, "Open: name=%s", name); INFO (context, "Open: name=%s", name);
// Allocate memory. // 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) { if (device == NULL) {
SYSERROR (context, ENOMEM); SYSERROR (context, ENOMEM);
return DC_STATUS_NOMEMORY; return DC_STATUS_NOMEMORY;

View File

@ -276,7 +276,7 @@ dc_serial_open (dc_iostream_t **out, dc_context_t *context, const char *name)
} }
// Allocate memory. // 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) { if (device == NULL) {
SYSERROR (context, ERROR_OUTOFMEMORY); SYSERROR (context, ERROR_OUTOFMEMORY);
return DC_STATUS_NOMEMORY; return DC_STATUS_NOMEMORY;

View File

@ -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); INFO (context, "Open: vid=%04x, pid=%04x", vid, pid);
// Allocate memory. // 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) { if (usbhid == NULL) {
ERROR (context, "Out of memory."); ERROR (context, "Out of memory.");
return DC_STATUS_NOMEMORY; return DC_STATUS_NOMEMORY;