citizen_aqualand_parser.c (7529B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2014 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 24 #include <libdivecomputer/units.h> 25 26 #include "citizen_aqualand.h" 27 #include "context-private.h" 28 #include "parser-private.h" 29 #include "array.h" 30 31 #define ISINSTANCE(parser) dc_device_isinstance((parser), &citizen_aqualand_parser_vtable) 32 33 #define SZ_HEADER 32 34 35 typedef struct citizen_aqualand_parser_t { 36 dc_parser_t base; 37 } citizen_aqualand_parser_t; 38 39 static dc_status_t citizen_aqualand_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 40 static dc_status_t citizen_aqualand_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 41 static dc_status_t citizen_aqualand_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 42 43 static const dc_parser_vtable_t citizen_aqualand_parser_vtable = { 44 sizeof(citizen_aqualand_parser_t), 45 DC_FAMILY_CITIZEN_AQUALAND, 46 NULL, /* set_clock */ 47 NULL, /* set_atmospheric */ 48 NULL, /* set_density */ 49 citizen_aqualand_parser_get_datetime, /* datetime */ 50 citizen_aqualand_parser_get_field, /* fields */ 51 citizen_aqualand_parser_samples_foreach, /* samples_foreach */ 52 NULL /* destroy */ 53 }; 54 55 56 dc_status_t 57 citizen_aqualand_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 58 { 59 citizen_aqualand_parser_t *parser = NULL; 60 61 if (out == NULL) 62 return DC_STATUS_INVALIDARGS; 63 64 // Allocate memory. 65 parser = (citizen_aqualand_parser_t *) dc_parser_allocate (context, &citizen_aqualand_parser_vtable, data, size); 66 if (parser == NULL) { 67 ERROR (context, "Failed to allocate memory."); 68 return DC_STATUS_NOMEMORY; 69 } 70 71 *out = (dc_parser_t*) parser; 72 73 return DC_STATUS_SUCCESS; 74 } 75 76 77 static dc_status_t 78 citizen_aqualand_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 79 { 80 if (abstract->size < SZ_HEADER) 81 return DC_STATUS_DATAFORMAT; 82 83 const unsigned char *p = abstract->data; 84 85 if (datetime) { 86 datetime->year = bcd2dec(p[0x05]) * 100 + bcd2dec(p[0x06]); 87 datetime->month = bcd2dec(p[0x07]); 88 datetime->day = bcd2dec(p[0x08]); 89 datetime->hour = bcd2dec(p[0x0A]); 90 datetime->minute = bcd2dec(p[0x0B]); 91 datetime->second = bcd2dec(p[0x0C]); 92 datetime->timezone = DC_TIMEZONE_NONE; 93 } 94 95 return DC_STATUS_SUCCESS; 96 } 97 98 99 static dc_status_t 100 citizen_aqualand_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 101 { 102 if (abstract->size < SZ_HEADER) 103 return DC_STATUS_DATAFORMAT; 104 105 const unsigned char *data = abstract->data; 106 107 unsigned int metric = (data[0x04] == 0xA6 ? 0 : 1); 108 unsigned int maxdepth = bcd2dec(data[0x12]) * 10 + ((data[0x13] >> 4) & 0x0F); 109 unsigned int divetime = (data[0x16] & 0x0F) * 100 + bcd2dec(data[0x17]); 110 111 if (value) { 112 switch (type) { 113 case DC_FIELD_DIVETIME: 114 *((unsigned int *) value) = divetime * 60; 115 break; 116 case DC_FIELD_MAXDEPTH: 117 if (metric) 118 *((double *) value) = maxdepth / 10.0; 119 else 120 *((double *) value) = maxdepth * FEET; 121 break; 122 case DC_FIELD_GASMIX_COUNT: 123 *((unsigned int *) value) = 0; 124 break; 125 case DC_FIELD_DIVEMODE: 126 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 127 break; 128 default: 129 return DC_STATUS_UNSUPPORTED; 130 } 131 } 132 133 return DC_STATUS_SUCCESS; 134 } 135 136 static dc_status_t 137 citizen_aqualand_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 138 { 139 const unsigned char *data = abstract->data; 140 unsigned int size = abstract->size; 141 142 if (size < SZ_HEADER) 143 return DC_STATUS_DATAFORMAT; 144 145 // Estimate the maximum number of samples. We calculate the number of 146 // 12 bit values that fit in the available profile data, and round the 147 // result upwards. The actual number of samples should always be smaller 148 // due to the presence of at least two end markers. 149 unsigned int maxcount = (2 * (size - SZ_HEADER) + 2) / 3; 150 151 // Allocate storage for the processed 16 bit samples. 152 unsigned short *samples = (unsigned short *) malloc(maxcount * sizeof(unsigned short)); 153 if (samples == NULL) { 154 return DC_STATUS_NOMEMORY; 155 } 156 157 // Pre-process the depth and temperature tables. The 12 bit BCD encoded 158 // values are converted into an array of 16 bit values, which is much 159 // more convenient to process in the second stage. 160 unsigned int nsamples = 0; 161 unsigned int count[2] = {0, 0}; 162 unsigned int offset = SZ_HEADER * 2; 163 unsigned int length = size * 2; 164 for (unsigned int i = 0; i < 2; ++i) { 165 const unsigned int marker = (i == 0 ? 0xEF : 0xFF); 166 167 while (offset + 3 <= length) { 168 unsigned int value = 0; 169 unsigned int octet = offset / 2; 170 unsigned int nibble = offset % 2; 171 unsigned int hi = data[octet]; 172 unsigned int lo = data[octet + 1]; 173 174 // Check for the end marker. 175 if (hi == marker || lo == marker) { 176 offset += nibble; 177 break; 178 } 179 180 // Convert 12 bit BCD to decimal. 181 if (nibble) { 182 value = ((hi ) & 0x0F) * 100 + 183 ((lo >> 4) & 0x0F) * 10 + 184 ((lo ) & 0x0F); 185 } else { 186 value = ((hi >> 4) & 0x0F) * 100 + 187 ((hi ) & 0x0F) * 10 + 188 ((lo >> 4) & 0x0F); 189 } 190 191 // Store the value. 192 samples[nsamples] = value; 193 count[i]++; 194 nsamples++; 195 196 offset += 3; 197 } 198 199 // Verify the end marker. 200 if (offset + 2 > length || data[offset / 2] != marker) { 201 ERROR (abstract->context, "No end marker found."); 202 free(samples); 203 return DC_STATUS_DATAFORMAT; 204 } 205 206 offset += 2; 207 } 208 209 unsigned int time = 0; 210 unsigned int interval = 5; 211 unsigned int metric = (data[0x04] == 0xA6 ? 0 : 1); 212 for (unsigned int i = 0; i < count[0]; ++i) { 213 dc_sample_value_t sample = {0}; 214 215 // Get the depth value. 216 unsigned int depth = samples[i]; 217 218 // Every 12th sample there is a strange sample that always contains 219 // the value 999. This is clearly not a valid depth, but when trying 220 // to skip these samples, the depth and temperatures go out of sync. 221 // Therefore we replace the bogus sample with an interpolated value. 222 if (depth == 999) { 223 depth = 0; 224 if (i > 0) { 225 depth += samples[i - 1]; 226 } 227 if (i < count[0] - 1) { 228 depth += samples[i + 1]; 229 } 230 depth /= 2; 231 } 232 233 // Time 234 time += interval; 235 sample.time = time * 1000; 236 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 237 238 // Depth 239 if (metric) 240 sample.depth = depth / 10.0; 241 else 242 sample.depth = depth * FEET; 243 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 244 245 // Temperature 246 if (time % 300 == 0) { 247 unsigned int idx = count[0] + time / 300; 248 if (idx < nsamples) { 249 unsigned int temperature = samples[idx]; 250 if (metric) 251 sample.temperature = temperature / 10.0; 252 else 253 sample.temperature = (temperature - 32.0) * (5.0 / 9.0); 254 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 255 } 256 } 257 } 258 259 free(samples); 260 261 return DC_STATUS_SUCCESS; 262 }