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");
123 * parse uemis base64 data blob into struct dive
125 void uemis_parse_divelog_binary(char *base64, void *datap) {
129 struct sample *sample;
130 struct dive **divep = datap;
131 struct dive *dive = *divep;
132 int template, gasoffset;
134 datalen = uemis_convert_base64(base64, &data);
136 /* dive template in use:
141 uemis cylinder data is insane - it stores seven tank settings in a block
142 and the template tells us which of the four groups of tanks we need to look at
144 gasoffset = template = *(uint8_t *)(data+115);
147 for (i = 0; i < template; i++) {
148 float volume = *(float *)(data+116+25*(gasoffset + i)) * 1000.0;
149 /* uemis always assumes a working pressure of 3000psi / 206bar - even though that's wrong */
150 /* I also think that the unit that it stores (cuft for me) might change with SDA settings */
151 // dive->cylinder[i].type.size.mliter = volume * 206.84 / 28.317;
152 dive->cylinder[i].type.size.mliter = volume * 200 / 28.317;
153 dive->cylinder[i].gasmix.o2.permille = *(uint8_t *)(data+120+25*(gasoffset + i)) * 10 + 0.5;
154 dive->cylinder[i].gasmix.he.permille = 0;
156 /* first byte of divelog data is at offset 0x123 */
158 while ((i < datalen) && (*(uint16_t *)(data+i))) {
159 /* it seems that a dive_time of 0 indicates the end of the valid readings */
160 /* the SDA usually records more samples after the end of the dive --
161 * we want to discard those, but not cut the dive short; sadly the dive
162 * duration in the header is a) in minutes and b) up to 3 minutes short */
163 if (*(uint16_t *)(data+i) > dive->duration.seconds + 180)
165 sample = prepare_sample(divep);
166 dive = *divep; /* prepare_sample might realloc the dive */
167 sample->time.seconds = *(uint16_t *)(data+i);
168 sample->depth.mm = pressure_to_depth(*(uint16_t *)(data+i+2));
169 sample->temperature.mkelvin = (*(uint16_t *)(data+i+4) * 100) + 273150;
170 sample->cylinderpressure.mbar= *(uint16_t *)(data+i+23) * 10;
171 sample->cylinderindex = *(uint8_t *)(data+i+22);
172 finish_sample(dive, sample);
175 dive->duration.seconds = sample->time.seconds - 1;