reefnet_sensusultra_parser.c (7991B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2008 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include <stdlib.h> 23 #include <string.h> // memcmp 24 25 #include <libdivecomputer/units.h> 26 27 #include "reefnet_sensusultra.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "array.h" 31 32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &reefnet_sensusultra_parser_vtable) 33 34 typedef struct reefnet_sensusultra_parser_t reefnet_sensusultra_parser_t; 35 36 struct reefnet_sensusultra_parser_t { 37 dc_parser_t base; 38 // Depth calibration. 39 double atmospheric; 40 double hydrostatic; 41 // Clock synchronization. 42 unsigned int devtime; 43 dc_ticks_t systime; 44 // Cached fields. 45 unsigned int cached; 46 unsigned int divetime; 47 unsigned int maxdepth; 48 }; 49 50 static dc_status_t reefnet_sensusultra_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime); 51 static dc_status_t reefnet_sensusultra_parser_set_atmospheric (dc_parser_t *abstract, double atmospheric); 52 static dc_status_t reefnet_sensusultra_parser_set_density (dc_parser_t *abstract, double density); 53 static dc_status_t reefnet_sensusultra_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 54 static dc_status_t reefnet_sensusultra_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 55 static dc_status_t reefnet_sensusultra_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 56 57 static const dc_parser_vtable_t reefnet_sensusultra_parser_vtable = { 58 sizeof(reefnet_sensusultra_parser_t), 59 DC_FAMILY_REEFNET_SENSUSULTRA, 60 reefnet_sensusultra_parser_set_clock, /* set_clock */ 61 reefnet_sensusultra_parser_set_atmospheric, /* set_atmospheric */ 62 reefnet_sensusultra_parser_set_density, /* set_density */ 63 reefnet_sensusultra_parser_get_datetime, /* datetime */ 64 reefnet_sensusultra_parser_get_field, /* fields */ 65 reefnet_sensusultra_parser_samples_foreach, /* samples_foreach */ 66 NULL /* destroy */ 67 }; 68 69 70 dc_status_t 71 reefnet_sensusultra_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 72 { 73 reefnet_sensusultra_parser_t *parser = NULL; 74 75 if (out == NULL) 76 return DC_STATUS_INVALIDARGS; 77 78 // Allocate memory. 79 parser = (reefnet_sensusultra_parser_t *) dc_parser_allocate (context, &reefnet_sensusultra_parser_vtable, data, size); 80 if (parser == NULL) { 81 ERROR (context, "Failed to allocate memory."); 82 return DC_STATUS_NOMEMORY; 83 } 84 85 // Set the default values. 86 parser->atmospheric = DEF_ATMOSPHERIC; 87 parser->hydrostatic = DEF_DENSITY_SALT * GRAVITY; 88 parser->devtime = 0; 89 parser->systime = 0; 90 parser->cached = 0; 91 parser->divetime = 0; 92 parser->maxdepth = 0; 93 94 *out = (dc_parser_t*) parser; 95 96 return DC_STATUS_SUCCESS; 97 } 98 99 100 static dc_status_t 101 reefnet_sensusultra_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime) 102 { 103 reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t *) abstract; 104 105 parser->devtime = devtime; 106 parser->systime = systime; 107 108 return DC_STATUS_SUCCESS; 109 } 110 111 112 static dc_status_t 113 reefnet_sensusultra_parser_set_atmospheric (dc_parser_t *abstract, double atmospheric) 114 { 115 reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t *) abstract; 116 117 parser->atmospheric = atmospheric; 118 119 return DC_STATUS_SUCCESS; 120 } 121 122 123 static dc_status_t 124 reefnet_sensusultra_parser_set_density (dc_parser_t *abstract, double density) 125 { 126 reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t *) abstract; 127 128 parser->hydrostatic = density * GRAVITY; 129 130 return DC_STATUS_SUCCESS; 131 } 132 133 134 static dc_status_t 135 reefnet_sensusultra_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 136 { 137 reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t *) abstract; 138 139 if (abstract->size < 4 + 4) 140 return DC_STATUS_DATAFORMAT; 141 142 unsigned int timestamp = array_uint32_le (abstract->data + 4); 143 144 dc_ticks_t ticks = parser->systime; 145 if (timestamp < parser->devtime) { 146 ticks -= parser->devtime - timestamp; 147 } else { 148 ticks += timestamp - parser->devtime; 149 } 150 151 if (!dc_datetime_localtime (datetime, ticks)) 152 return DC_STATUS_DATAFORMAT; 153 154 return DC_STATUS_SUCCESS; 155 } 156 157 158 static dc_status_t 159 reefnet_sensusultra_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 160 { 161 reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t *) abstract; 162 163 if (abstract->size < 20) 164 return DC_STATUS_DATAFORMAT; 165 166 if (!parser->cached) { 167 const unsigned char footer[4] = {0xFF, 0xFF, 0xFF, 0xFF}; 168 169 const unsigned char *data = abstract->data; 170 unsigned int size = abstract->size; 171 172 unsigned int interval = array_uint16_le (data + 8); 173 unsigned int threshold = array_uint16_le (data + 10); 174 175 unsigned int maxdepth = 0; 176 unsigned int nsamples = 0; 177 unsigned int offset = 16; 178 while (offset + sizeof (footer) <= size && 179 memcmp (data + offset, footer, sizeof (footer)) != 0) 180 { 181 unsigned int depth = array_uint16_le (data + offset + 2); 182 if (depth >= threshold) { 183 if (depth > maxdepth) 184 maxdepth = depth; 185 nsamples++; 186 } 187 188 offset += 4; 189 } 190 191 parser->cached = 1; 192 parser->divetime = nsamples * interval; 193 parser->maxdepth = maxdepth; 194 } 195 196 if (value) { 197 switch (type) { 198 case DC_FIELD_DIVETIME: 199 *((unsigned int *) value) = parser->divetime; 200 break; 201 case DC_FIELD_MAXDEPTH: 202 *((double *) value) = (parser->maxdepth * BAR / 1000.0 - parser->atmospheric) / parser->hydrostatic; 203 break; 204 case DC_FIELD_GASMIX_COUNT: 205 *((unsigned int *) value) = 0; 206 break; 207 case DC_FIELD_DIVEMODE: 208 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 209 break; 210 default: 211 return DC_STATUS_UNSUPPORTED; 212 } 213 } 214 215 return DC_STATUS_SUCCESS; 216 } 217 218 219 static dc_status_t 220 reefnet_sensusultra_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 221 { 222 reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t*) abstract; 223 224 const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; 225 const unsigned char footer[4] = {0xFF, 0xFF, 0xFF, 0xFF}; 226 227 const unsigned char *data = abstract->data; 228 unsigned int size = abstract->size; 229 230 unsigned int offset = 0; 231 while (offset + sizeof (header) <= size) { 232 if (memcmp (data + offset, header, sizeof (header)) == 0) { 233 if (offset + 16 > size) 234 return DC_STATUS_DATAFORMAT; 235 236 unsigned int time = 0; 237 unsigned int interval = array_uint16_le (data + offset + 8); 238 239 offset += 16; 240 while (offset + sizeof (footer) <= size && 241 memcmp (data + offset, footer, sizeof (footer)) != 0) 242 { 243 dc_sample_value_t sample = {0}; 244 245 // Time (seconds) 246 time += interval; 247 sample.time = time * 1000; 248 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 249 250 // Temperature (0.01 °K) 251 unsigned int temperature = array_uint16_le (data + offset); 252 sample.temperature = temperature / 100.0 - 273.15; 253 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 254 255 // Depth (absolute pressure in millibar) 256 unsigned int depth = array_uint16_le (data + offset + 2); 257 sample.depth = (depth * BAR / 1000.0 - parser->atmospheric) / parser->hydrostatic; 258 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 259 260 offset += 4; 261 } 262 break; 263 } else { 264 offset++; 265 } 266 } 267 268 return DC_STATUS_SUCCESS; 269 }