diff --git a/include/libdivecomputer/suunto_eonsteel.h b/include/libdivecomputer/suunto_eonsteel.h index d12387c..68e2986 100644 --- a/include/libdivecomputer/suunto_eonsteel.h +++ b/include/libdivecomputer/suunto_eonsteel.h @@ -19,8 +19,8 @@ * MA 02110-1301 USA */ -#ifndef EON_STEEL_H -#define EON_STEEL_H +#ifndef SUUNTO_EONSTEEL_H +#define SUUNTO_EONSTEEL_H #include "context.h" #include "device.h" @@ -36,4 +36,4 @@ dc_status_t suunto_eonsteel_parser_create(dc_parser_t **parser, dc_context_t *co #ifdef __cplusplus } #endif /* __cplusplus */ -#endif /* EON_STEEL_H */ +#endif /* SUUNTO_EONSTEEL_H */ diff --git a/src/suunto_eonsteel.c b/src/suunto_eonsteel.c index aeff089..9260950 100644 --- a/src/suunto_eonsteel.c +++ b/src/suunto_eonsteel.c @@ -43,14 +43,14 @@ #include -struct eonsteel { +typedef struct suunto_eonsteel_device_t { dc_device_t base; libusb_context *ctx; libusb_device_handle *handle; unsigned int magic; unsigned short seq; -}; +} suunto_eonsteel_device_t; // The EON Steel implements a small filesystem #define DIRTYPE_FILE 0x0001 @@ -81,6 +81,19 @@ struct directory_entry { #define READDIR_CMD 0x0910 #define DIR_CLOSE_CMD 0x0a10 +static dc_status_t suunto_eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); +static dc_status_t suunto_eonsteel_device_close(dc_device_t *abstract); + +static const dc_device_vtable_t suunto_eonsteel_device_vtable = { + DC_FAMILY_SUUNTO_EONSTEEL, + NULL, /* set_fingerprint */ + NULL, /* read */ + NULL, /* write */ + NULL, /* dump */ + suunto_eonsteel_device_foreach, /* foreach */ + suunto_eonsteel_device_close /* close */ +}; + static const char *dive_directory = "0:/dives"; static struct directory_entry *alloc_dirent(int type, int len, const char *name) @@ -159,7 +172,7 @@ static void debug_text(const char *name, const char *buf, int len) printf("\nend of text\n"); } -static int receive_data(struct eonsteel *eon, unsigned char *buffer, int size) +static int receive_data(suunto_eonsteel_device_t *eon, unsigned char *buffer, int size) { const int InEndpoint = 0x82; unsigned char buf[64]; @@ -190,7 +203,7 @@ static int receive_data(struct eonsteel *eon, unsigned char *buffer, int size) return ret; } -static int send_cmd(struct eonsteel *eon, +static int send_cmd(suunto_eonsteel_device_t *eon, unsigned short cmd, unsigned int len, const unsigned char *buffer) @@ -248,7 +261,7 @@ static int send_cmd(struct eonsteel *eon, * send_cmd() side. The offsets are the same in the actual raw * packet. */ -static int send_receive(struct eonsteel *eon, +static int send_receive(suunto_eonsteel_device_t *eon, unsigned short cmd, unsigned int len_out, const unsigned char *out, unsigned int len_in, unsigned char *in) @@ -279,7 +292,7 @@ static int send_receive(struct eonsteel *eon, return actual; } -static int read_file(struct eonsteel *eon, const char *filename, dc_buffer_t *buf) +static int read_file(suunto_eonsteel_device_t *eon, const char *filename, dc_buffer_t *buf) { unsigned char result[2560]; unsigned char cmdbuf[64]; @@ -380,7 +393,7 @@ static struct directory_entry *parse_dirent(int nr, const unsigned char *p, int return old; } -static int get_file_list(struct eonsteel *eon, struct directory_entry **res) +static int get_file_list(suunto_eonsteel_device_t *eon, struct directory_entry **res) { struct directory_entry *de = NULL; unsigned char cmd[64]; @@ -428,21 +441,7 @@ static int get_file_list(struct eonsteel *eon, struct directory_entry **res) return 0; } - -static dc_status_t eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); -static dc_status_t eonsteel_device_close(dc_device_t *abstract); - -static const dc_device_vtable_t eonsteel_device_vtable = { - DC_FAMILY_SUUNTO_EONSTEEL, - NULL, /* set_fingerprint */ - NULL, /* read */ - NULL, /* write */ - NULL, /* dump */ - eonsteel_device_foreach, /* foreach */ - eonsteel_device_close /* close */ -}; - -static int initialize_eonsteel(struct eonsteel *eon) +static int initialize_eonsteel(suunto_eonsteel_device_t *eon) { const int InEndpoint = 0x82; unsigned char buf[64]; @@ -470,14 +469,15 @@ static int initialize_eonsteel(struct eonsteel *eon) return 0; } -dc_status_t suunto_eonsteel_device_open(dc_device_t **out, dc_context_t *context, const char *name, unsigned int model) +dc_status_t +suunto_eonsteel_device_open(dc_device_t **out, dc_context_t *context, const char *name, unsigned int model) { - struct eonsteel *eon; + suunto_eonsteel_device_t *eon; if (out == NULL) return DC_STATUS_INVALIDARGS; - eon = calloc(1, sizeof(struct eonsteel)); + eon = calloc(1, sizeof(suunto_eonsteel_device_t)); if (!eon) return DC_STATUS_NOMEMORY; @@ -486,7 +486,7 @@ dc_status_t suunto_eonsteel_device_open(dc_device_t **out, dc_context_t *context eon->seq = INIT_SEQ; // Set up the libdivecomputer interfaces - device_init(&eon->base, context, &eonsteel_device_vtable); + device_init(&eon->base, context, &suunto_eonsteel_device_vtable); if (libusb_init(&eon->ctx)) { ERROR(context, "libusb_init() failed"); @@ -524,11 +524,12 @@ static int count_dir_entries(struct directory_entry *de) return count; } -static dc_status_t eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) +static dc_status_t +suunto_eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { int skip = 0, rc; struct directory_entry *de; - struct eonsteel *eon = (struct eonsteel *) abstract; + suunto_eonsteel_device_t *eon = (suunto_eonsteel_device_t *) abstract; dc_buffer_t *file; char pathname[64]; unsigned int time; @@ -591,9 +592,10 @@ static dc_status_t eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callba return device_is_cancelled(abstract) ? DC_STATUS_CANCELLED : DC_STATUS_SUCCESS; } -static dc_status_t eonsteel_device_close(dc_device_t *abstract) +static dc_status_t +suunto_eonsteel_device_close(dc_device_t *abstract) { - struct eonsteel *eon = (struct eonsteel *) abstract; + suunto_eonsteel_device_t *eon = (suunto_eonsteel_device_t *) abstract; libusb_close(eon->handle); libusb_exit(eon->ctx); @@ -604,7 +606,8 @@ static dc_status_t eonsteel_device_close(dc_device_t *abstract) #else // no LIBUSB support -dc_status_t suunto_eonsteel_device_open(dc_device_t **out, dc_context_t *context, const char *name, unsigned int model) +dc_status_t +suunto_eonsteel_device_open(dc_device_t **out, dc_context_t *context, const char *name, unsigned int model) { ERROR(context, "The Suunto EON Steel backend needs libusb-1.0"); return DC_STATUS_UNSUPPORTED; diff --git a/src/suunto_eonsteel_parser.c b/src/suunto_eonsteel_parser.c index 1f75395..b15b6ea 100644 --- a/src/suunto_eonsteel_parser.c +++ b/src/suunto_eonsteel_parser.c @@ -41,7 +41,7 @@ struct type_desc { #define MAXTYPE 512 #define MAXGASES 16 -struct eon_parser { +typedef struct suunto_eonsteel_parser_t { dc_parser_t base; struct type_desc type_desc[MAXTYPE]; // field cache @@ -55,7 +55,7 @@ struct eon_parser { dc_salinity_t salinity; double surface_pressure; } cache; -}; +} suunto_eonsteel_parser_t; static int report_error(const char *fmt, ...) @@ -110,7 +110,7 @@ static void debug_text(const char *name, const char *buf, int len) typedef int (*eon_data_cb_t)(unsigned short type, const struct type_desc *desc, const void *data, int len, void *user); -static int record_type(struct eon_parser *eon, unsigned short type, const char *name, int namelen) +static int record_type(suunto_eonsteel_parser_t *eon, unsigned short type, const char *name, int namelen) { struct type_desc desc; const char *next; @@ -161,7 +161,7 @@ static int record_type(struct eon_parser *eon, unsigned short type, const char * return 0; } -static int traverse_entry(struct eon_parser *eon, const unsigned char *p, int len, eon_data_cb_t callback, void *user) +static int traverse_entry(suunto_eonsteel_parser_t *eon, const unsigned char *p, int len, eon_data_cb_t callback, void *user) { const unsigned char *name, *data, *end, *last, *one_past_end = p + len; int textlen, type; @@ -229,7 +229,7 @@ static int traverse_entry(struct eon_parser *eon, const unsigned char *p, int le return end - p; } -static int traverse_data(struct eon_parser *eon, eon_data_cb_t callback, void *user) +static int traverse_data(suunto_eonsteel_parser_t *eon, eon_data_cb_t callback, void *user) { const unsigned char *data = eon->base.data; int len = eon->base.size; @@ -254,7 +254,7 @@ static int traverse_data(struct eon_parser *eon, eon_data_cb_t callback, void *u } struct sample_data { - struct eon_parser *eon; + suunto_eonsteel_parser_t *eon; dc_sample_callback_t callback; void *userdata; unsigned int time; @@ -345,9 +345,10 @@ static int traverse_samples(unsigned short type, const struct type_desc *desc, c return 0; } -static dc_status_t eonsteel_parser_samples_foreach(dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) +static dc_status_t +suunto_eonsteel_parser_samples_foreach(dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) { - struct eon_parser *eon = (void *)abstract; + suunto_eonsteel_parser_t *eon = (void *)abstract; struct sample_data data = { eon, callback, userdata, 0 }; traverse_data(eon, traverse_samples, &data); @@ -359,9 +360,10 @@ static dc_status_t eonsteel_parser_samples_foreach(dc_parser_t *abstract, dc_sam #define field_value(p, set) \ memcpy((p), &(set), sizeof(set)) -static dc_status_t eonsteel_parser_get_field(dc_parser_t *parser, dc_field_type_t type, unsigned int flags, void *value) +static dc_status_t +suunto_eonsteel_parser_get_field(dc_parser_t *parser, dc_field_type_t type, unsigned int flags, void *value) { - struct eon_parser *eon = (struct eon_parser *)parser; + suunto_eonsteel_parser_t *eon = (suunto_eonsteel_parser_t *)parser; if (!(eon->cache.initialized >> type)) return DC_STATUS_UNSUPPORTED; @@ -399,7 +401,8 @@ static dc_status_t eonsteel_parser_get_field(dc_parser_t *parser, dc_field_type_ * and we've saved it off as the four first bytes * of the dive data (in little-endian format). */ -static dc_status_t eonsteel_parser_get_datetime(dc_parser_t *parser, dc_datetime_t *datetime) +static dc_status_t +suunto_eonsteel_parser_get_datetime(dc_parser_t *parser, dc_datetime_t *datetime) { if (parser->size < 4) return DC_STATUS_UNSUPPORTED; @@ -409,13 +412,13 @@ static dc_status_t eonsteel_parser_get_datetime(dc_parser_t *parser, dc_datetime } // time in ms -static void add_time_field(struct eon_parser *eon, unsigned short time_delta_ms) +static void add_time_field(suunto_eonsteel_parser_t *eon, unsigned short time_delta_ms) { eon->cache.divetime += time_delta_ms; } // depth in cm -static void set_depth_field(struct eon_parser *eon, unsigned short d) +static void set_depth_field(suunto_eonsteel_parser_t *eon, unsigned short d) { if (d != 0xffff) { double depth = d / 100.0; @@ -426,7 +429,7 @@ static void set_depth_field(struct eon_parser *eon, unsigned short d) } // gas type: 0=Off,1=Primary,2=?,3=Diluent -static void add_gas_type(struct eon_parser *eon, unsigned char type) +static void add_gas_type(suunto_eonsteel_parser_t *eon, unsigned char type) { if (eon->cache.ngases < MAXGASES) eon->cache.ngases++; @@ -434,7 +437,7 @@ static void add_gas_type(struct eon_parser *eon, unsigned char type) } // O2 percentage as a byte -static void add_gas_o2(struct eon_parser *eon, unsigned char o2) +static void add_gas_o2(suunto_eonsteel_parser_t *eon, unsigned char o2) { int idx = eon->cache.ngases-1; if (idx >= 0) @@ -443,7 +446,7 @@ static void add_gas_o2(struct eon_parser *eon, unsigned char o2) } // He percentage as a byte -static void add_gas_he(struct eon_parser *eon, unsigned char he) +static void add_gas_he(suunto_eonsteel_parser_t *eon, unsigned char he) { int idx = eon->cache.ngases-1; if (idx >= 0) @@ -453,7 +456,7 @@ static void add_gas_he(struct eon_parser *eon, unsigned char he) static int traverse_fields(unsigned short type, const struct type_desc *desc, const void *data, int len, void *user) { - struct eon_parser *eon = user; + suunto_eonsteel_parser_t *eon = user; switch (type) { case 0x0001: // group: time in first word, depth in second @@ -480,7 +483,7 @@ static int traverse_fields(unsigned short type, const struct type_desc *desc, co } -static void initialize_field_caches(struct eon_parser *eon) +static void initialize_field_caches(suunto_eonsteel_parser_t *eon) { memset(&eon->cache, 0, sizeof(eon->cache)); eon->cache.initialized = 1 << DC_FIELD_DIVETIME; @@ -492,39 +495,42 @@ static void initialize_field_caches(struct eon_parser *eon) eon->cache.divetime /= 1000; } -static dc_status_t eonsteel_parser_set_data(dc_parser_t *parser, const unsigned char *data, unsigned int size) +static dc_status_t +suunto_eonsteel_parser_set_data(dc_parser_t *parser, const unsigned char *data, unsigned int size) { - struct eon_parser *eon = (void *)parser; + suunto_eonsteel_parser_t *eon = (void *)parser; memset(eon->type_desc, 0, sizeof(eon->type_desc)); initialize_field_caches(eon); return DC_STATUS_SUCCESS; } -static dc_status_t eonsteel_parser_destroy(dc_parser_t *parser) +static dc_status_t +suunto_eonsteel_parser_destroy(dc_parser_t *parser) { free(parser); return DC_STATUS_SUCCESS; } -static const dc_parser_vtable_t eonsteel_parser_vtable = { +static const dc_parser_vtable_t suunto_eonsteel_parser_vtable = { DC_FAMILY_SUUNTO_EONSTEEL, - eonsteel_parser_set_data, /* set_data */ - eonsteel_parser_get_datetime, /* datetime */ - eonsteel_parser_get_field, /* fields */ - eonsteel_parser_samples_foreach, /* samples_foreach */ - eonsteel_parser_destroy /* destroy */ + suunto_eonsteel_parser_set_data, /* set_data */ + suunto_eonsteel_parser_get_datetime, /* datetime */ + suunto_eonsteel_parser_get_field, /* fields */ + suunto_eonsteel_parser_samples_foreach, /* samples_foreach */ + suunto_eonsteel_parser_destroy /* destroy */ }; -dc_status_t suunto_eonsteel_parser_create(dc_parser_t **out, dc_context_t *context, unsigned int model) +dc_status_t +suunto_eonsteel_parser_create(dc_parser_t **out, dc_context_t *context, unsigned int model) { - struct eon_parser *eon; + suunto_eonsteel_parser_t *eon; if (out == NULL) return DC_STATUS_INVALIDARGS; eon = calloc(1, sizeof(*eon)); - parser_init(&eon->base, context, &eonsteel_parser_vtable); + parser_init(&eon->base, context, &suunto_eonsteel_parser_vtable); *out = (dc_parser_t *) eon;