tecdiving_divecomputereu_parser.c (5456B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2018 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 "tecdiving_divecomputereu.h" 25 #include "context-private.h" 26 #include "parser-private.h" 27 #include "array.h" 28 29 #define ISINSTANCE(parser) dc_device_isinstance((parser), &tecdiving_divecomputereu_parser_vtable) 30 31 #define SZ_HEADER 100 32 33 typedef struct tecdiving_divecomputereu_parser_t tecdiving_divecomputereu_parser_t; 34 35 struct tecdiving_divecomputereu_parser_t { 36 dc_parser_t base; 37 }; 38 39 static dc_status_t tecdiving_divecomputereu_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 40 static dc_status_t tecdiving_divecomputereu_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 41 static dc_status_t tecdiving_divecomputereu_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 42 43 static const dc_parser_vtable_t tecdiving_divecomputereu_parser_vtable = { 44 sizeof(tecdiving_divecomputereu_parser_t), 45 DC_FAMILY_TECDIVING_DIVECOMPUTEREU, 46 NULL, /* set_clock */ 47 NULL, /* set_atmospheric */ 48 NULL, /* set_density */ 49 tecdiving_divecomputereu_parser_get_datetime, /* datetime */ 50 tecdiving_divecomputereu_parser_get_field, /* fields */ 51 tecdiving_divecomputereu_parser_samples_foreach, /* samples_foreach */ 52 NULL /* destroy */ 53 }; 54 55 56 dc_status_t 57 tecdiving_divecomputereu_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 58 { 59 tecdiving_divecomputereu_parser_t *parser = NULL; 60 61 if (out == NULL) 62 return DC_STATUS_INVALIDARGS; 63 64 // Allocate memory. 65 parser = (tecdiving_divecomputereu_parser_t *) dc_parser_allocate (context, &tecdiving_divecomputereu_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 tecdiving_divecomputereu_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 79 { 80 const unsigned char *data = abstract->data; 81 82 if (abstract->size < SZ_HEADER) 83 return DC_STATUS_DATAFORMAT; 84 85 if (datetime) { 86 datetime->year = data[2] + 2000; 87 datetime->month = data[3]; 88 datetime->day = data[4]; 89 datetime->hour = data[5]; 90 datetime->minute = data[6]; 91 datetime->second = data[7]; 92 datetime->timezone = DC_TIMEZONE_NONE; 93 } 94 95 return DC_STATUS_SUCCESS; 96 } 97 98 99 static dc_status_t 100 tecdiving_divecomputereu_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 101 { 102 const unsigned char *data = abstract->data; 103 104 if (abstract->size < SZ_HEADER) 105 return DC_STATUS_DATAFORMAT; 106 107 if (value) { 108 switch (type) { 109 case DC_FIELD_DIVETIME: 110 *((unsigned int *) value) = array_uint16_be (data + 23) * 60; 111 break; 112 case DC_FIELD_AVGDEPTH: 113 *((double *) value) = array_uint16_be (data + 27) / 100; 114 break; 115 case DC_FIELD_MAXDEPTH: 116 *((double *) value) = array_uint16_be (data + 29) / 10; 117 break; 118 case DC_FIELD_ATMOSPHERIC: 119 *((double *) value) = array_uint16_be (data + 14) / 1000.0; 120 break; 121 case DC_FIELD_TEMPERATURE_SURFACE: 122 *((double *) value) = (signed char) data[17]; 123 break; 124 case DC_FIELD_TEMPERATURE_MINIMUM: 125 *((double *) value) = (signed char) data[41]; 126 break; 127 case DC_FIELD_TEMPERATURE_MAXIMUM: 128 *((double *) value) = (signed char) data[42]; 129 break; 130 default: 131 return DC_STATUS_UNSUPPORTED; 132 } 133 } 134 135 return DC_STATUS_SUCCESS; 136 } 137 138 static dc_status_t 139 tecdiving_divecomputereu_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 140 { 141 const unsigned char *data = abstract->data; 142 unsigned int size = abstract->size; 143 144 unsigned int time = 0; 145 unsigned int interval = data[47]; 146 147 unsigned int offset = SZ_HEADER; 148 while (offset + 8 <= size) { 149 dc_sample_value_t sample = {0}; 150 151 // Time (seconds). 152 time += interval; 153 sample.time = time * 1000; 154 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 155 156 // Depth (1/10 m). 157 unsigned int depth = array_uint16_be (data + offset + 2); 158 sample.depth = depth / 10.0; 159 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 160 161 // Temperature (Celsius). 162 signed int temperature = (signed char) data[offset]; 163 sample.temperature = temperature; 164 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 165 166 // ppO2 167 unsigned int ppo2 = data[offset + 1]; 168 sample.ppo2.sensor = DC_SENSOR_NONE; 169 sample.ppo2.value = ppo2 / 10.0; 170 if (callback) callback (DC_SAMPLE_PPO2, &sample, userdata); 171 172 // Setpoint 173 unsigned int setpoint = data[offset + 4]; 174 sample.setpoint = setpoint / 10.0; 175 if (callback) callback (DC_SAMPLE_SETPOINT, &sample, userdata); 176 177 offset += 8; 178 } 179 180 return DC_STATUS_SUCCESS; 181 }