reefnet_sensus_parser.c (8055B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2009 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> // malloc, free 23 24 #include <libdivecomputer/units.h> 25 26 #include "reefnet_sensus.h" 27 #include "context-private.h" 28 #include "parser-private.h" 29 #include "array.h" 30 31 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &reefnet_sensus_parser_vtable) 32 33 #define SAMPLE_DEPTH_ADJUST 13 34 35 typedef struct reefnet_sensus_parser_t reefnet_sensus_parser_t; 36 37 struct reefnet_sensus_parser_t { 38 dc_parser_t base; 39 // Depth calibration. 40 double atmospheric; 41 double hydrostatic; 42 // Clock synchronization. 43 unsigned int devtime; 44 dc_ticks_t systime; 45 // Cached fields. 46 unsigned int cached; 47 unsigned int divetime; 48 unsigned int maxdepth; 49 }; 50 51 static dc_status_t reefnet_sensus_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime); 52 static dc_status_t reefnet_sensus_parser_set_atmospheric (dc_parser_t *abstract, double atmospheric); 53 static dc_status_t reefnet_sensus_parser_set_density (dc_parser_t *abstract, double density); 54 static dc_status_t reefnet_sensus_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 55 static dc_status_t reefnet_sensus_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 56 static dc_status_t reefnet_sensus_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 57 58 static const dc_parser_vtable_t reefnet_sensus_parser_vtable = { 59 sizeof(reefnet_sensus_parser_t), 60 DC_FAMILY_REEFNET_SENSUS, 61 reefnet_sensus_parser_set_clock, /* set_clock */ 62 reefnet_sensus_parser_set_atmospheric, /* set_atmospheric */ 63 reefnet_sensus_parser_set_density, /* set_density */ 64 reefnet_sensus_parser_get_datetime, /* datetime */ 65 reefnet_sensus_parser_get_field, /* fields */ 66 reefnet_sensus_parser_samples_foreach, /* samples_foreach */ 67 NULL /* destroy */ 68 }; 69 70 71 dc_status_t 72 reefnet_sensus_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 73 { 74 reefnet_sensus_parser_t *parser = NULL; 75 76 if (out == NULL) 77 return DC_STATUS_INVALIDARGS; 78 79 // Allocate memory. 80 parser = (reefnet_sensus_parser_t *) dc_parser_allocate (context, &reefnet_sensus_parser_vtable, data, size); 81 if (parser == NULL) { 82 ERROR (context, "Failed to allocate memory."); 83 return DC_STATUS_NOMEMORY; 84 } 85 86 // Set the default values. 87 parser->atmospheric = DEF_ATMOSPHERIC; 88 parser->hydrostatic = DEF_DENSITY_SALT * GRAVITY; 89 parser->devtime = 0; 90 parser->systime = 0; 91 parser->cached = 0; 92 parser->divetime = 0; 93 parser->maxdepth = 0; 94 95 *out = (dc_parser_t*) parser; 96 97 return DC_STATUS_SUCCESS; 98 } 99 100 101 static dc_status_t 102 reefnet_sensus_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime) 103 { 104 reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t *) abstract; 105 106 parser->devtime = devtime; 107 parser->systime = systime; 108 109 return DC_STATUS_SUCCESS; 110 } 111 112 113 static dc_status_t 114 reefnet_sensus_parser_set_atmospheric (dc_parser_t *abstract, double atmospheric) 115 { 116 reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t *) abstract; 117 118 parser->atmospheric = atmospheric; 119 120 return DC_STATUS_SUCCESS; 121 } 122 123 124 static dc_status_t 125 reefnet_sensus_parser_set_density (dc_parser_t *abstract, double density) 126 { 127 reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t *) abstract; 128 129 parser->hydrostatic = density * GRAVITY; 130 131 return DC_STATUS_SUCCESS; 132 } 133 134 135 static dc_status_t 136 reefnet_sensus_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 137 { 138 reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t *) abstract; 139 140 if (abstract->size < 2 + 4) 141 return DC_STATUS_DATAFORMAT; 142 143 unsigned int timestamp = array_uint32_le (abstract->data + 2); 144 145 dc_ticks_t ticks = parser->systime; 146 if (timestamp < parser->devtime) { 147 ticks -= parser->devtime - timestamp; 148 } else { 149 ticks += timestamp - parser->devtime; 150 } 151 152 if (!dc_datetime_localtime (datetime, ticks)) 153 return DC_STATUS_DATAFORMAT; 154 155 return DC_STATUS_SUCCESS; 156 } 157 158 159 static dc_status_t 160 reefnet_sensus_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 161 { 162 reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t *) abstract; 163 164 if (abstract->size < 7) 165 return DC_STATUS_DATAFORMAT; 166 167 if (!parser->cached) { 168 const unsigned char *data = abstract->data; 169 unsigned int size = abstract->size; 170 171 unsigned int maxdepth = 0; 172 unsigned int interval = data[1]; 173 unsigned int nsamples = 0, count = 0; 174 175 unsigned int offset = 7; 176 while (offset + 1 <= size) { 177 // Depth. 178 unsigned int depth = data[offset++]; 179 if (depth > maxdepth) 180 maxdepth = depth; 181 182 // Skip temperature byte. 183 if ((nsamples % 6) == 0) 184 offset++; 185 186 // Current sample is complete. 187 nsamples++; 188 189 // The end of a dive is reached when 17 consecutive 190 // depth samples of less than 3 feet have been found. 191 if (depth < SAMPLE_DEPTH_ADJUST + 3) { 192 count++; 193 if (count == 17) { 194 break; 195 } 196 } else { 197 count = 0; 198 } 199 } 200 201 parser->cached = 1; 202 parser->divetime = nsamples * interval; 203 parser->maxdepth = maxdepth; 204 } 205 206 if (value) { 207 switch (type) { 208 case DC_FIELD_DIVETIME: 209 *((unsigned int *) value) = parser->divetime; 210 break; 211 case DC_FIELD_MAXDEPTH: 212 *((double *) value) = ((parser->maxdepth + 33.0 - (double) SAMPLE_DEPTH_ADJUST) * FSW - parser->atmospheric) / parser->hydrostatic; 213 break; 214 case DC_FIELD_GASMIX_COUNT: 215 *((unsigned int *) value) = 0; 216 break; 217 case DC_FIELD_DIVEMODE: 218 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 219 break; 220 default: 221 return DC_STATUS_UNSUPPORTED; 222 } 223 } 224 225 return DC_STATUS_SUCCESS; 226 } 227 228 229 static dc_status_t 230 reefnet_sensus_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 231 { 232 reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t*) abstract; 233 234 const unsigned char *data = abstract->data; 235 unsigned int size = abstract->size; 236 237 unsigned int offset = 0; 238 while (offset + 7 <= size) { 239 if (data[offset] == 0xFF && data[offset + 6] == 0xFE) { 240 241 unsigned int time = 0; 242 unsigned int interval = data[offset + 1]; 243 unsigned int nsamples = 0, count = 0; 244 245 offset += 7; 246 while (offset + 1 <= size) { 247 dc_sample_value_t sample = {0}; 248 249 // Time (seconds) 250 time += interval; 251 sample.time = time * 1000; 252 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 253 254 // Depth (adjusted feet of seawater). 255 unsigned int depth = data[offset++]; 256 sample.depth = ((depth + 33.0 - (double) SAMPLE_DEPTH_ADJUST) * FSW - parser->atmospheric) / parser->hydrostatic; 257 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 258 259 // Temperature (degrees Fahrenheit) 260 if ((nsamples % 6) == 0) { 261 if (offset + 1 > size) 262 return DC_STATUS_DATAFORMAT; 263 unsigned int temperature = data[offset++]; 264 sample.temperature = (temperature - 32.0) * (5.0 / 9.0); 265 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 266 } 267 268 // Current sample is complete. 269 nsamples++; 270 271 // The end of a dive is reached when 17 consecutive 272 // depth samples of less than 3 feet have been found. 273 if (depth < SAMPLE_DEPTH_ADJUST + 3) { 274 count++; 275 if (count == 17) { 276 break; 277 } 278 } else { 279 count = 0; 280 } 281 } 282 break; 283 } else { 284 offset++; 285 } 286 } 287 288 return DC_STATUS_SUCCESS; 289 }