From ea01b66dec3de843f52d291e44d13b5c112fdf18 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 4 Jul 2008 12:49:47 +0000 Subject: [PATCH] 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. --- src/Makefile.am | 2 + src/device-private.h | 40 +++++++++++++++++ src/device.c | 103 +++++++++++++++++++++++++++++++++++++++++++ src/device.h | 46 +++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 src/device-private.h create mode 100644 src/device.c create mode 100644 src/device.h diff --git a/src/Makefile.am b/src/Makefile.am index e608b4f..afb00fd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/device-private.h b/src/device-private.h new file mode 100644 index 0000000..2881d33 --- /dev/null +++ b/src/device-private.h @@ -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 */ diff --git a/src/device.c b/src/device.c new file mode 100644 index 0000000..4dd036a --- /dev/null +++ b/src/device.c @@ -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); +} diff --git a/src/device.h b/src/device.h new file mode 100644 index 0000000..5f9141b --- /dev/null +++ b/src/device.h @@ -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 */