4 * UEMIS SDA file importer
5 * AUTHOR: Dirk Hohndel - Copyright 2011
7 * Licensed under the MIT license.
23 * following code is based on code found in at base64.sourceforge.net/b64.c
24 * AUTHOR: Bob Trower 08/04/01
25 * COPYRIGHT: Copyright (c) Trantor Standard Systems Inc., 2001
26 * NOTE: This source code may be used as you wish, subject to
30 * Translation Table to decode (created by Bob Trower)
32 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
35 * decodeblock -- decode 4 '6-bit' characters into 3 8-bit binary bytes
37 static void decodeblock( unsigned char in[4], unsigned char out[3] ) {
38 out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
39 out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
40 out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
44 * decode a base64 encoded stream discarding padding, line breaks and noise
46 static void decode( uint8_t *inbuf, uint8_t *outbuf, int inbuf_len ) {
47 uint8_t in[4], out[3], v;
48 int i,len,indx_in=0,indx_out=0;
50 while (indx_in < inbuf_len) {
51 for (len = 0, i = 0; i < 4 && (indx_in < inbuf_len); i++ ) {
53 while ((indx_in < inbuf_len) && v == 0) {
55 v = ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
57 v = ((v == '$') ? 0 : v - 61);
59 if (indx_in < inbuf_len) {
68 decodeblock( in, out );
69 for( i = 0; i < len - 1; i++ )
70 outbuf[indx_out++] = out[i];
74 /* end code from Bob Trower */
77 * pressure_to_depth: In centibar. And when converting to
78 * depth, I'm just going to always use saltwater, because I
79 * think "true depth" is just stupid. From a diving standpoint,
80 * "true depth" is pretty much completely pointless, unless
81 * you're doing some kind of underwater surveying work.
83 * So I give water depths in "pressure depth", always assuming
84 * salt water. So one atmosphere per 10m.
86 static int pressure_to_depth(uint16_t value)
90 atm = (value / 100.0) / 1.01325;
92 return( (cm > 0) ? 10 * (long)cm : 0);
96 * convert the base64 data blog
98 int uemis_convert_base64(char *base64, uint8_t **data) {
101 len = strlen(base64);
102 datalen = (len/4 + 1)*3;
103 if (datalen < 0x123+0x25) {
104 /* less than header + 1 sample??? */
105 fprintf(stderr,"suspiciously short data block\n");
107 *data = malloc(datalen);
110 fprintf(stderr,"Out of memory\n");
113 decode(base64, *data, len);
115 if (memcmp(*data,"Dive\01\00\00",7))
116 fprintf(stderr,"Missing Dive100 header\n");
122 /* Create events from the flag bits;
123 * These bits basically represent what is displayed on screen at sample time.
124 * Many of these 'warnings' are way hyper-active and seriously clutter the
125 * profile plot - so these are disabled by default
127 void uemis_event(struct dive *dive, struct sample *sample, uemis_sample_t *u_sample)
129 uint8_t *flags = u_sample->flags;
132 add_event(dive, sample->time.seconds, 0, 0, 0, "Safety Stop Violation");
134 add_event(dive, sample->time.seconds, 0, 0, 0, "Speed Alarm");
135 #if WANT_CRAZY_WARNINGS
136 if (flags[1] & 0x06) /* both bits 1 and 2 are a warning */
137 add_event(dive, sample->time.seconds, 0, 0, 0, "Speed Warning");
139 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Green Warning");
142 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Ascend Warning");
144 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Ascend Alarm");
145 /* flags[2] reflects the deco / time bar
146 * flags[3] reflects more display details on deco and pO2 */
148 add_event(dive, sample->time.seconds, 0, 0, 0, "Tank Pressure Info");
150 add_event(dive, sample->time.seconds, 0, 0, 0, "RGT Warning");
152 add_event(dive, sample->time.seconds, 0, 0, 0, "RGT Alert");
154 add_event(dive, sample->time.seconds, 0, 0, 0, "Tank Change Suggested");
156 add_event(dive, sample->time.seconds, 0, 0, 0, "Depth Limit Exceeded");
158 add_event(dive, sample->time.seconds, 0, 0, 0, "Max Deco Time Warning");
160 add_event(dive, sample->time.seconds, 0, 0, 0, "Dive Time Info");
162 add_event(dive, sample->time.seconds, 0, 0, 0, "Dive Time Alert");
164 add_event(dive, sample->time.seconds, 0, 0, 0, "Marker");
166 add_event(dive, sample->time.seconds, 0, 0, 0, "No Tank Data");
168 add_event(dive, sample->time.seconds, 0, 0, 0, "Low Battery Warning");
170 add_event(dive, sample->time.seconds, 0, 0, 0, "Low Battery Alert");
171 /* flags[7] reflects the little on screen icons that remind of previous
172 * warnings / alerts - not useful for events */
176 * parse uemis base64 data blob into struct dive
178 void uemis_parse_divelog_binary(char *base64, void *datap) {
182 struct sample *sample;
183 uemis_sample_t *u_sample;
184 struct dive **divep = datap;
185 struct dive *dive = *divep;
186 int template, gasoffset;
188 datalen = uemis_convert_base64(base64, &data);
190 /* dive template in use:
195 uemis cylinder data is insane - it stores seven tank settings in a block
196 and the template tells us which of the four groups of tanks we need to look at
198 gasoffset = template = *(uint8_t *)(data+115);
201 for (i = 0; i < template; i++) {
202 float volume = *(float *)(data+116+25*(gasoffset + i)) * 1000.0;
203 /* uemis always assumes a working pressure of 202.6bar (!?!?) - I first thought
204 * it was 3000psi, but testing against all my dives gets me that strange number.
205 * Still, that's of course completely bogus and shows they don't get how
206 * cylinders are named in non-metric parts of the world...
207 * we store the incorrect working pressure to get the SAC calculations "close"
208 * but the user will have to correct this manually
210 dive->cylinder[i].type.size.mliter = volume;
211 dive->cylinder[i].type.workingpressure.mbar = 202600;
212 dive->cylinder[i].gasmix.o2.permille = *(uint8_t *)(data+120+25*(gasoffset + i)) * 10 + 0.5;
213 dive->cylinder[i].gasmix.he.permille = 0;
215 /* first byte of divelog data is at offset 0x123 */
217 u_sample = (uemis_sample_t *)(data + i);
218 while ((i < datalen) && (u_sample->dive_time)) {
219 /* it seems that a dive_time of 0 indicates the end of the valid readings */
220 /* the SDA usually records more samples after the end of the dive --
221 * we want to discard those, but not cut the dive short; sadly the dive
222 * duration in the header is a) in minutes and b) up to 3 minutes short */
223 if (u_sample->dive_time > dive->duration.seconds + 180)
225 sample = prepare_sample(divep);
226 dive = *divep; /* prepare_sample might realloc the dive */
227 sample->time.seconds = u_sample->dive_time;
228 sample->depth.mm = pressure_to_depth(u_sample->water_pressure);
229 sample->temperature.mkelvin = (u_sample->dive_temperature * 100) + 273150;
230 sample->cylinderindex = u_sample->active_tank;
231 sample->cylinderpressure.mbar= u_sample->tank_pressure * 10;
232 uemis_event(dive, sample, u_sample);
233 finish_sample(dive, sample);
237 dive->duration.seconds = sample->time.seconds - 1;