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:
parent
7c95581826
commit
3230387fff
@ -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 */
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
*
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user