]> git.tdb.fi Git - ext/subsurface.git/blob - uemis.c
Merge branch 'uemis-integration' of git://github.com/dirkhh/subsurface
[ext/subsurface.git] / uemis.c
1 /*
2  * uemis.c
3  *
4  * UEMIS SDA file importer
5  * AUTHOR:  Dirk Hohndel - Copyright 2011
6  *
7  * Licensed under the MIT license.
8  */
9
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #define __USE_XOPEN
16 #include <time.h>
17 #include <regex.h>
18
19 #include "dive.h"
20 #include "uemis.h"
21
22 /*
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
27  *                 the MIT license.
28  */
29 /*
30  * Translation Table to decode (created by Bob Trower)
31  */
32 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
33
34 /*
35  * decodeblock -- decode 4 '6-bit' characters into 3 8-bit binary bytes
36  */
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]);
41 }
42
43 /*
44  * decode a base64 encoded stream discarding padding, line breaks and noise
45  */
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;
49
50         while (indx_in < inbuf_len) {
51                 for (len = 0, i = 0; i < 4 && (indx_in < inbuf_len); i++ ) {
52                         v = 0;
53                         while ((indx_in < inbuf_len) && v == 0) {
54                                 v = inbuf[indx_in++];
55                                 v = ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
56                                 if (v)
57                                         v = ((v == '$') ? 0 : v - 61);
58                         }
59                         if (indx_in < inbuf_len) {
60                                 len++;
61                                 if (v)
62                                         in[i] = (v - 1);
63                         }
64                         else
65                                 in[i] = 0;
66                 }
67                 if( len ) {
68                         decodeblock( in, out );
69                         for( i = 0; i < len - 1; i++ )
70                                 outbuf[indx_out++] = out[i];
71                 }
72         }
73 }
74 /* end code from Bob Trower */
75
76 /*
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.
82  *
83  * So I give water depths in "pressure depth", always assuming
84  * salt water. So one atmosphere per 10m.
85  */
86 static int pressure_to_depth(uint16_t value)
87 {
88         double atm, cm;
89
90         atm = (value / 100.0) / 1.01325;
91         cm = 100 * atm + 0.5;
92         return( (cm > 0) ? 10 * (long)cm : 0);
93 }
94
95 /*
96  * convert the base64 data blog
97  */
98 int uemis_convert_base64(char *base64, uint8_t **data) {
99         int len,datalen;
100
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");
106         }
107         *data = malloc(datalen);
108         if (! *data) {
109                 datalen = 0;
110                 fprintf(stderr,"Out of memory\n");
111                 goto bail;
112         }
113         decode(base64, *data, len);
114
115         if (memcmp(*data,"Dive\01\00\00",7))
116                 fprintf(stderr,"Missing Dive100 header\n");
117
118 bail:
119         return datalen;
120 }
121
122 /*
123  * parse uemis base64 data blob into struct dive
124  */
125 void uemis_parse_divelog_binary(char *base64, void *datap) {
126         int datalen;
127         int i;
128         uint8_t *data;
129         struct sample *sample;
130         struct dive **divep = datap;
131         struct dive *dive = *divep;
132         int template, gasoffset;
133
134         datalen = uemis_convert_base64(base64, &data);
135
136         /* dive template in use:
137            0 = air
138            1 = nitrox (B)
139            2 = nitrox (B+D)
140            3 = nitrox (B+T+D)
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
143          */
144         gasoffset = template = *(uint8_t *)(data+115);
145         if (template == 3)
146                 gasoffset = 4;
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 202.6bar (!?!?) - I first thought
150                  * it was 3000psi, but testing against all my dives gets me that strange number.
151                  * Still, that's of course completely bogus and shows they don't get how
152                  * cylinders are named in non-metric parts of the world...
153                  * we store the incorrect working pressure to get the SAC calculations "close"
154                  * but the user will have to correct this manually
155                  */
156                 dive->cylinder[i].type.size.mliter = volume;
157                 dive->cylinder[i].type.workingpressure.mbar = 202600;
158                 dive->cylinder[i].gasmix.o2.permille = *(uint8_t *)(data+120+25*(gasoffset + i)) * 10 + 0.5;
159                 dive->cylinder[i].gasmix.he.permille = 0;
160         }
161         /* first byte of divelog data is at offset 0x123 */
162         i = 0x123;
163         while ((i < datalen) && (*(uint16_t *)(data+i))) {
164                 /* it seems that a dive_time of 0 indicates the end of the valid readings */
165                 /* the SDA usually records more samples after the end of the dive --
166                  * we want to discard those, but not cut the dive short; sadly the dive
167                  * duration in the header is a) in minutes and b) up to 3 minutes short */
168                 if (*(uint16_t *)(data+i) > dive->duration.seconds + 180)
169                         break;
170                 sample = prepare_sample(divep);
171                 dive = *divep; /* prepare_sample might realloc the dive */
172                 sample->time.seconds = *(uint16_t *)(data+i);
173                 sample->depth.mm = pressure_to_depth(*(uint16_t *)(data+i+2));
174                 sample->temperature.mkelvin = (*(uint16_t *)(data+i+4) * 100) + 273150;
175                 sample->cylinderpressure.mbar= *(uint16_t *)(data+i+23) * 10;
176                 sample->cylinderindex = *(uint8_t *)(data+i+22);
177                 finish_sample(dive, sample);
178                 i += 0x25;
179         }
180         dive->duration.seconds = sample->time.seconds - 1;
181         return;
182 }