Added the first steps towards the implementation of the new api.

For this new api, each device will be implemented as a separate backend 
for a common interface. This will make it easier to support multiple 
devices in a single application.
This commit is contained in:
Jef Driesen 2008-07-04 12:49:47 +00:00
parent 3ec8bb025c
commit ea01b66dec
4 changed files with 191 additions and 0 deletions

View File

@ -5,6 +5,7 @@
libdivecomputerdir = $(includedir)/libdivecomputer
libdivecomputer_HEADERS = \
utils.h \
device.h \
suunto.h \
suunto_eon.h \
suunto_vyper.h \
@ -29,6 +30,7 @@ lib_LTLIBRARIES = libdivecomputer.la
libdivecomputer_la_LDFLAGS = -no-undefined
libdivecomputer_la_SOURCES = \
device.h device-private.h device.c \
suunto.h \
suunto_common.h suunto_common.c \
suunto_eon.h suunto_eon.c \

40
src/device-private.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef DEVICE_PRIVATE_H
#define DEVICE_PRIVATE_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include "device.h"
struct device_t;
struct device_backend_t;
typedef struct device_backend_t device_backend_t;
struct device_t {
const device_backend_t *backend;
};
struct device_backend_t {
device_type_t type;
device_status_t (*handshake) (device_t *device, unsigned char data[], unsigned int size);
device_status_t (*version) (device_t *device, unsigned char data[], unsigned int size);
device_status_t (*read) (device_t *device, unsigned int address, unsigned char data[], unsigned int size);
device_status_t (*write) (device_t *device, unsigned int address, const unsigned char data[], unsigned int size);
device_status_t (*download) (device_t *device, unsigned char data[], unsigned int size);
device_status_t (*foreach) (device_t *device, dive_callback_t callback, void *userdata);
device_status_t (*close) (device_t *device);
};
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* DEVICE_PRIVATE_H */

103
src/device.c Normal file
View File

@ -0,0 +1,103 @@
#include "device-private.h"
#define NULL 0
device_type_t
device_get_type (device_t *device)
{
if (device == NULL)
return DEVICE_TYPE_NULL;
return device->backend->type;
}
device_status_t
device_handshake (device_t *device, unsigned char data[], unsigned int size)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
if (device->backend->handshake == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->handshake (device, data, size);
}
device_status_t
device_version (device_t *device, unsigned char data[], unsigned int size)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
if (device->backend->version == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->version (device, data, size);
}
device_status_t
device_read (device_t *device, unsigned int address, unsigned char data[], unsigned int size)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
if (device->backend->read == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->read (device, address, data, size);
}
device_status_t
device_write (device_t *device, unsigned int address, const unsigned char data[], unsigned int size)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
if (device->backend->write == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->write (device, address, data, size);
}
device_status_t
device_download (device_t *device, unsigned char data[], unsigned int size)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
if (device->backend->download == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->download (device, data, size);
}
device_status_t
device_foreach (device_t *device, dive_callback_t callback, void *userdata)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
if (device->backend->foreach == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->foreach (device, callback, userdata);
}
device_status_t
device_close (device_t *device)
{
if (device == NULL)
return DEVICE_STATUS_SUCCESS;
if (device->backend->close == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->close (device);
}

46
src/device.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef DEVICE_H
#define DEVICE_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef enum device_type_t {
DEVICE_TYPE_NULL = 0
} device_type_t;
typedef enum device_status_t {
DEVICE_STATUS_SUCCESS = 0,
DEVICE_STATUS_UNSUPPORTED = -1,
DEVICE_STATUS_TYPE_MISMATCH = -2,
DEVICE_STATUS_ERROR = -3,
DEVICE_STATUS_IO = -4,
DEVICE_STATUS_TIMEOUT = -5,
DEVICE_STATUS_PROTOCOL = -6,
DEVICE_STATUS_MEMORY = -7
} device_status_t;
typedef struct device_t device_t;
typedef void (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata);
device_type_t device_get_type (device_t *device);
device_status_t device_handshake (device_t *device, unsigned char data[], unsigned int size);
device_status_t device_version (device_t *device, unsigned char data[], unsigned int size);
device_status_t device_read (device_t *device, unsigned int address, unsigned char data[], unsigned int size);
device_status_t device_write (device_t *device, unsigned int address, const unsigned char data[], unsigned int size);
device_status_t device_download (device_t *device, unsigned char data[], unsigned int size);
device_status_t device_foreach (device_t *device, dive_callback_t callback, void *userdata);
device_status_t device_close (device_t *device);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* DEVICE_H */