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 */
76 /* small helper functions */
77 /* simpleregex allocates (and reallocates) the found buffer
78 * don't forget to free it when you are done
80 static int simpleregex(char *buffer, char *regex, char **found) {
85 if (regcomp(&re, regex, 0) !=0) {
86 fprintf(stderr,"internal error, regex failed!\n");
89 status = regexec(&re,buffer,5,match,0);
91 *found = realloc(*found,match[1].rm_eo-match[1].rm_so + 1);
92 strncpy(*found,buffer+match[1].rm_so,match[1].rm_eo-match[1].rm_so);
93 (*found)[match[1].rm_eo-match[1].rm_so] = '\0';
98 /* read in line of arbitrary length (important for SDA files that can
99 * have lines that are tens of kB long
100 * don't forget to free it when you are done
102 #define MYGETL_INCR 1024
103 static char * mygetline(FILE * f) {
110 if ((buf = realloc(buf,size)) == NULL)
112 fgets(buf+len,MYGETL_INCR,f);
114 } while (!feof(f) && buf[len-1]!='\n');
118 /* text matching, used to build very poor man's XML parser */
119 int matchit(FILE *infd, char *regex, char *typeregex, char **found) {
122 while (!feof(infd)) {
123 buffer = mygetline(infd);
124 if (buffer && simpleregex(buffer,regex,found)) {
125 buffer = mygetline(infd);
126 if (buffer && simpleregex(buffer,typeregex,found)) {
135 * pressure_to_depth: In centibar. And when converting to
136 * depth, I'm just going to always use saltwater, because I
137 * think "true depth" is just stupid. From a diving standpoint,
138 * "true depth" is pretty much completely pointless, unless
139 * you're doing some kind of underwater surveying work.
141 * So I give water depths in "pressure depth", always assuming
142 * salt water. So one atmosphere per 10m.
144 static int pressure_to_depth(uint16_t value)
148 atm = (value / 100.0) / 1.01325;
149 cm = 100 * atm + 0.5;
150 return( (cm > 0) ? 10 * (long)cm : 0);
154 * convert the base64 data blog
156 int uemis_convert_base64(char *base64, uint8_t **data) {
159 len = strlen(base64);
160 datalen = (len/4 + 1)*3;
161 if (datalen < 0x123+0x25) {
162 /* less than header + 1 sample??? */
163 fprintf(stderr,"suspiciously short data block\n");
165 *data = malloc(datalen);
168 fprintf(stderr,"Out of memory\n");
171 decode(base64, *data, len);
173 if (memcmp(*data,"Dive\01\00\00",7))
174 fprintf(stderr,"Missing Dive100 header\n");
181 * parse uemis base64 data blob into struct dive
183 void uemis_parse_divelog_binary(char *base64, struct dive **divep) {
187 struct sample *sample;
188 struct dive *dive = *divep;
189 int template, gasoffset;
191 datalen = uemis_convert_base64(base64, &data);
193 /* dive template in use:
198 uemis cylinder data is insane - it stores seven tank settings in a block
199 and the template tells us which of the four groups of tanks we need to look at
201 gasoffset = template = *(uint8_t *)(data+115);
204 for (i = 0; i < template; i++) {
205 float volume = *(float *)(data+116+25*(gasoffset + i)) * 1000.0;
206 /* uemis always assumes a working pressure of 3000psi / 206bar - even though that's wrong */
207 /* I also think that the unit that it stores (cuft for me) might change with SDA settings */
208 // dive->cylinder[i].type.size.mliter = volume * 206.84 / 28.317;
209 dive->cylinder[i].type.size.mliter = volume * 200 / 28.317;
210 dive->cylinder[i].gasmix.o2.permille = *(uint8_t *)(data+120+25*(gasoffset + i)) * 10 + 0.5;
211 dive->cylinder[i].gasmix.he.permille = 0;
213 /* first byte of divelog data is at offset 0x123 */
215 while ((i < datalen) && (*(uint16_t *)(data+i))) {
216 /* it seems that a dive_time of 0 indicates the end of the valid readings */
217 /* the SDA usually records more samples after the end of the dive --
218 * we want to discard those, but not cut the dive short; sadly the dive
219 * duration in the header is a) in minutes and b) up to 3 minutes short */
220 if (*(uint16_t *)(data+i) > dive->duration.seconds + 180)
222 sample = prepare_sample(divep);
223 dive = *divep; /* prepare_sample might realloc the dive */
224 sample->time.seconds = *(uint16_t *)(data+i);
225 sample->depth.mm = pressure_to_depth(*(uint16_t *)(data+i+2));
226 sample->temperature.mkelvin = (*(uint16_t *)(data+i+4) * 100) + 273150;
227 sample->cylinderpressure.mbar= *(uint16_t *)(data+i+23) * 10;
228 sample->cylinderindex = *(uint8_t *)(data+i+22);
229 finish_sample(dive, sample);
232 dive->duration.seconds = sample->time.seconds - 1;
236 /* parse a single file
237 * TODO: we don't report any errors when the parse fails - we simply don't add them to the list
240 parse_uemis_file(char *divelogfilename) {
245 FILE *divelogfile = fopen(divelogfilename,"r");
249 if (! matchit(divelogfile,"val key=\"date\"","<ts>\\([^<]*\\)</ts>",&found)) {
250 /* some error handling */
253 strptime(found, "%Y-%m-%dT%H:%M:%S", &tm);
254 dive->when = utc_mktime(&tm);
255 if (! matchit(divelogfile,"<val key=\"duration\">",
256 "<float>\\([0-9.]*\\)</float>", &found)) {
257 /* some error handling */
260 dive->duration.seconds = 60.0 * atof(found);
262 if (! matchit(divelogfile,"<val key=\"depth\">",
263 "<int>\\([0-9.]*\\)</int>", &found)) {
264 /* some error handling */
267 dive->maxdepth.mm = pressure_to_depth(atoi(found));
269 if (! matchit(divelogfile,"<val key=\"file_content\">",
270 ">\\([a-zA-Z0-9+/=]*\\)<", &found)) {
271 /* some error handling */
274 uemis_parse_divelog_binary(found,&dive);
282 * parse the two files extracted from the SDA
286 if (open_import_file_dialog("*.SDA","uemis Zurich SDA files",