From 842592fb55b7877c975f2a8556e6c4c4c6b80722 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 13 Feb 2020 11:58:39 -0800 Subject: [PATCH] Extend field-cache infrastructure to support the Suunto EON Steel This adds a few misc fields that the EON Steel wants, and changes the string insertion interface to return a 'dc_status_t', which will be used by that back-end. The existing deepblu users don't care. Signed-off-by: Linus Torvalds --- src/field-cache.c | 20 +++++++++++++++----- src/field-cache.h | 18 ++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/field-cache.c b/src/field-cache.c index ee25f39..890320a 100644 --- a/src/field-cache.c +++ b/src/field-cache.c @@ -13,7 +13,7 @@ * dynamically on the stack or whatever without having to * worry about it. */ -void dc_field_add_string(dc_field_cache_t *cache, const char *desc, const char *value) +dc_status_t dc_field_add_string(dc_field_cache_t *cache, const char *desc, const char *value) { int i; @@ -22,23 +22,33 @@ void dc_field_add_string(dc_field_cache_t *cache, const char *desc, const char * dc_field_string_t *str = cache->strings+i; if (str->desc) continue; - str->desc = desc; str->value = strdup(value); - break; + if (!str->value) + return DC_STATUS_NOMEMORY; + str->desc = desc; + return DC_STATUS_SUCCESS; } + return DC_STATUS_INVALIDARGS; } -void dc_field_add_string_fmt(dc_field_cache_t *cache, const char *desc, const char *fmt, ...) +dc_status_t dc_field_add_string_fmt(dc_field_cache_t *cache, const char *desc, const char *fmt, ...) { char buffer[256]; va_list ap; + /* + * We ignore the return value from vsnprintf, and we + * always NUL-terminate the destination buffer ourselves. + * + * That way we don't have to worry about random bad legacy + * implementations. + */ va_start(ap, fmt); buffer[sizeof(buffer)-1] = 0; (void) vsnprintf(buffer, sizeof(buffer)-1, fmt, ap); va_end(ap); - dc_field_add_string(cache, desc, buffer); + return dc_field_add_string(cache, desc, buffer); } dc_status_t dc_field_get_string(dc_field_cache_t *cache, unsigned idx, dc_field_string_t *value) diff --git a/src/field-cache.h b/src/field-cache.h index 87a4c6c..0fc21d5 100644 --- a/src/field-cache.h +++ b/src/field-cache.h @@ -5,6 +5,7 @@ typedef struct dc_field_cache { unsigned int initialized; + // DC_GET_FIELD_xyz unsigned int DIVETIME; double MAXDEPTH; double AVGDEPTH; @@ -14,11 +15,24 @@ typedef struct dc_field_cache { dc_salinity_t SALINITY; dc_gasmix_t GASMIX[MAXGASES]; + // misc - clean me up! + double lowsetpoint; + double highsetpoint; + double customsetpoint; + + // This (slong with GASMIX) should be something like + // dc_tank_t TANK[MAXGASES] + // but that's for later + dc_tankinfo_t tankinfo[MAXGASES]; + double tanksize[MAXGASES]; + double tankworkingpressure[MAXGASES]; + + // DC_GET_FIELD_STRING dc_field_string_t strings[MAXSTRINGS]; } dc_field_cache_t; -void dc_field_add_string(dc_field_cache_t *, const char *desc, const char *data); -void dc_field_add_string_fmt(dc_field_cache_t *, const char *desc, const char *fmt, ...); +dc_status_t dc_field_add_string(dc_field_cache_t *, const char *desc, const char *data); +dc_status_t dc_field_add_string_fmt(dc_field_cache_t *, const char *desc, const char *fmt, ...); dc_status_t dc_field_get_string(dc_field_cache_t *, unsigned idx, dc_field_string_t *value); /*