From 7c3f4e864f2a11501653bb7594b5dd3c591cbf36 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 20 Feb 2009 12:11:11 +0000 Subject: [PATCH] Added basic support for the new event notification code. --- src/device-private.h | 13 +++++++++- src/device.c | 49 +++++++++++++++++++++++++++++++++++++ src/device.h | 13 ++++++++-- src/libdivecomputer.symbols | 1 + 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/device-private.h b/src/device-private.h index 9c8c7e8..a93c429 100644 --- a/src/device-private.h +++ b/src/device-private.h @@ -22,11 +22,15 @@ #ifndef DEVICE_PRIVATE_H #define DEVICE_PRIVATE_H +#include + +#include "device.h" + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -#include "device.h" +#define DEVICE_PROGRESS_INITIALIZER {0, UINT_MAX} struct device_t; struct device_backend_t; @@ -38,6 +42,10 @@ struct device_t { // Progress callback data. progress_callback_t progress; void *userdata; + // Event notifications. + unsigned int event_mask; + device_event_callback_t event_callback; + void *event_userdata; }; struct device_backend_t { @@ -78,6 +86,9 @@ progress_set_maximum (device_progress_state_t *progress, unsigned int value); void device_init (device_t *device, const device_backend_t *backend); +void +device_event_emit (device_t *device, device_event_t event, const void *data); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/src/device.c b/src/device.c index ee94f3a..181021c 100644 --- a/src/device.c +++ b/src/device.c @@ -31,6 +31,10 @@ device_init (device_t *device, const device_backend_t *backend) device->backend = backend; device->progress = NULL; device->userdata = NULL; + + device->event_mask = 0; + device->event_callback = NULL; + device->event_userdata = NULL; } @@ -57,6 +61,20 @@ device_set_progress (device_t *device, progress_callback_t callback, void *userd } +device_status_t +device_set_events (device_t *device, unsigned int events, device_event_callback_t callback, void *userdata) +{ + if (device == NULL) + return DEVICE_STATUS_UNSUPPORTED; + + device->event_mask = events; + device->event_callback = callback; + device->event_userdata = userdata; + + return DEVICE_STATUS_SUCCESS; +} + + device_status_t device_handshake (device_t *device, unsigned char data[], unsigned int size) { @@ -196,3 +214,34 @@ progress_set_maximum (device_progress_state_t *progress, unsigned int value) progress->maximum = value; } + + +void +device_event_emit (device_t *device, device_event_t event, const void *data) +{ + device_progress_t *progress = (device_progress_t *) data; + + // Check the event data for errors. + switch (event) { + case DEVICE_EVENT_WAITING: + assert (data == NULL); + break; + case DEVICE_EVENT_PROGRESS: + assert (progress != NULL); + assert (progress->maximum != 0); + assert (progress->maximum >= progress->current); + break; + default: + break; + } + + // Check if there is a callback function registered. + if (device == NULL || device->event_callback == NULL) + return; + + // Check the event mask. + if ((event & device->event_mask) == 0) + return; + + device->event_callback (device, event, data, device->event_userdata); +} diff --git a/src/device.h b/src/device.h index b39a0e1..2c20d91 100644 --- a/src/device.h +++ b/src/device.h @@ -57,12 +57,19 @@ typedef enum device_status_t { } device_status_t; typedef enum device_event_t { - DEVICE_EVENT_WAITING, - DEVICE_EVENT_PROGRESS + DEVICE_EVENT_WAITING = (1 << 0), + DEVICE_EVENT_PROGRESS = (1 << 1) } device_event_t; typedef struct device_t device_t; +typedef struct device_progress_t { + unsigned int current; + unsigned int maximum; +} device_progress_t; + +typedef void (*device_event_callback_t) (device_t *device, device_event_t event, const void *data, void *userdata); + typedef int (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata); typedef void (*progress_callback_t) (device_event_t event, unsigned int current, unsigned int maximum, void *userdata); @@ -70,6 +77,8 @@ device_type_t device_get_type (device_t *device); device_status_t device_set_progress (device_t *device, progress_callback_t callback, void *userdata); +device_status_t device_set_events (device_t *device, unsigned int events, device_event_callback_t callback, void *userdata); + 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); diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index a83543c..2e22ebc 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -24,6 +24,7 @@ device_get_type device_handshake device_read device_set_progress +device_set_events device_version device_write