sporasub_sp2_parser.c (5747B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2021 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 "sporasub_sp2.h" 25 #include "context-private.h" 26 #include "parser-private.h" 27 #include "array.h" 28 29 #define ISINSTANCE(parser) dc_device_isinstance((parser), &sporasub_sp2_parser_vtable) 30 31 #define SZ_HEADER 0x20 32 #define SZ_SAMPLE 0x04 33 34 typedef struct sporasub_sp2_parser_t sporasub_sp2_parser_t; 35 36 struct sporasub_sp2_parser_t { 37 dc_parser_t base; 38 }; 39 40 static dc_status_t sporasub_sp2_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 41 static dc_status_t sporasub_sp2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 42 static dc_status_t sporasub_sp2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 43 44 static const dc_parser_vtable_t sporasub_sp2_parser_vtable = { 45 sizeof(sporasub_sp2_parser_t), 46 DC_FAMILY_SPORASUB_SP2, 47 NULL, /* set_clock */ 48 NULL, /* set_atmospheric */ 49 NULL, /* set_density */ 50 sporasub_sp2_parser_get_datetime, /* datetime */ 51 sporasub_sp2_parser_get_field, /* fields */ 52 sporasub_sp2_parser_samples_foreach, /* samples_foreach */ 53 NULL /* destroy */ 54 }; 55 56 57 dc_status_t 58 sporasub_sp2_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 59 { 60 sporasub_sp2_parser_t *parser = NULL; 61 62 if (out == NULL) 63 return DC_STATUS_INVALIDARGS; 64 65 // Allocate memory. 66 parser = (sporasub_sp2_parser_t *) dc_parser_allocate (context, &sporasub_sp2_parser_vtable, data, size); 67 if (parser == NULL) { 68 ERROR (context, "Failed to allocate memory."); 69 return DC_STATUS_NOMEMORY; 70 } 71 72 *out = (dc_parser_t *) parser; 73 74 return DC_STATUS_SUCCESS; 75 } 76 77 78 static dc_status_t 79 sporasub_sp2_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 80 { 81 const unsigned char *data = abstract->data; 82 unsigned int size = abstract->size; 83 84 if (size < SZ_HEADER) 85 return DC_STATUS_DATAFORMAT; 86 87 if (datetime) { 88 datetime->year = data[4] + 2000; 89 datetime->month = data[3]; 90 datetime->day = data[2]; 91 datetime->hour = data[7]; 92 datetime->minute = data[6]; 93 datetime->second = data[5]; 94 datetime->timezone = DC_TIMEZONE_NONE; 95 } 96 97 return DC_STATUS_SUCCESS; 98 } 99 100 101 static dc_status_t 102 sporasub_sp2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 103 { 104 const unsigned char *data = abstract->data; 105 unsigned int size = abstract->size; 106 107 dc_salinity_t *water = (dc_salinity_t *) value; 108 109 if (size < SZ_HEADER) 110 return DC_STATUS_DATAFORMAT; 111 112 unsigned int settings = data[0x1A]; 113 114 if (value) { 115 switch (type) { 116 case DC_FIELD_DIVETIME: 117 *((unsigned int *) value) = data[0x08] + data[0x09] * 60; 118 break; 119 case DC_FIELD_MAXDEPTH: 120 *((double *) value) = array_uint16_le (data + 0x14) / 100.0; 121 break; 122 case DC_FIELD_DIVEMODE: 123 *((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE; 124 break; 125 case DC_FIELD_TEMPERATURE_MINIMUM: 126 *((double *) value) = array_uint16_le (data + 0x18) / 10.0; 127 break; 128 case DC_FIELD_TEMPERATURE_MAXIMUM: 129 *((double *) value) = array_uint16_le (data + 0x16) / 10.0; 130 break; 131 case DC_FIELD_SALINITY: 132 water->type = settings & 0x08 ? DC_WATER_FRESH : DC_WATER_SALT; 133 water->density = 0.0; 134 break; 135 default: 136 return DC_STATUS_UNSUPPORTED; 137 } 138 } 139 140 return DC_STATUS_SUCCESS; 141 } 142 143 144 static dc_status_t 145 sporasub_sp2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 146 { 147 const unsigned char *data = abstract->data; 148 unsigned int size = abstract->size; 149 150 if (size < SZ_HEADER) 151 return DC_STATUS_DATAFORMAT; 152 153 unsigned int nsamples = array_uint16_le(data); 154 unsigned int settings = data[0x1A]; 155 156 // Get the sample interval. 157 unsigned int interval_idx = settings & 0x03; 158 const unsigned int intervals[] = {1, 2, 5, 10}; 159 if (interval_idx >= C_ARRAY_SIZE(intervals)) { 160 ERROR (abstract->context, "Invalid sample interval index %u", interval_idx); 161 return DC_STATUS_DATAFORMAT; 162 } 163 unsigned int interval = intervals[interval_idx]; 164 165 unsigned int time = 0; 166 unsigned int count = 0; 167 unsigned int offset = SZ_HEADER; 168 while (offset + SZ_SAMPLE <= size && count < nsamples) { 169 dc_sample_value_t sample = {0}; 170 171 unsigned int value = array_uint32_le (data + offset); 172 unsigned int heartrate = (value & 0xFF000000) >> 24; 173 unsigned int temperature = (value & 0x00FFC000) >> 14; 174 unsigned int depth = (value & 0x00003FFF) >> 0; 175 176 // Time (seconds) 177 time += interval; 178 sample.time = time * 1000; 179 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 180 181 // Depth (1/100 m) 182 sample.depth = depth / 100.0; 183 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 184 185 // Temperature (1/10 °C) 186 sample.temperature = temperature / 10.0 - 20.0; 187 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 188 189 // Heartrate 190 if (heartrate) { 191 sample.heartbeat = heartrate; 192 if (callback) callback (DC_SAMPLE_HEARTBEAT, &sample, userdata); 193 } 194 195 offset += SZ_SAMPLE; 196 count++; 197 } 198 199 return DC_STATUS_SUCCESS; 200 }