]> git.tdb.fi Git - ext/subsurface.git/blob - uemis.c
Much nicer implementation of uemis sample parsing - and add events, too
[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 /* 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
126  */
127 void uemis_event(struct dive *dive, struct sample *sample, uemis_sample_t *u_sample)
128 {
129         uint8_t *flags = u_sample->flags;
130
131         if (flags[1] & 0x01)
132                 add_event(dive, sample->time.seconds, 0, 0, 0, "Safety Stop Violation");
133         if (flags[1] & 0x08)
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");
138         if (flags[1] & 0x10)
139                 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Green Warning");
140 #endif
141         if (flags[1] & 0x20)
142                 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Ascend Warning");
143         if (flags[1] & 0x40)
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 */
147         if (flags[4] & 0x01)
148                 add_event(dive, sample->time.seconds, 0, 0, 0, "Tank Pressure Info");
149         if (flags[4] & 0x04)
150                 add_event(dive, sample->time.seconds, 0, 0, 0, "RGT Warning");
151         if (flags[4] & 0x08)
152                 add_event(dive, sample->time.seconds, 0, 0, 0, "RGT Alert");
153         if (flags[4] & 0x40)
154                 add_event(dive, sample->time.seconds, 0, 0, 0, "Tank Change Suggest");
155         if (flags[4] & 0x80)
156                 add_event(dive, sample->time.seconds, 0, 0, 0, "Depth Limit Exceeded");
157         if (flags[5] & 0x01)
158                 add_event(dive, sample->time.seconds, 0, 0, 0, "Max Deco Time Warning");
159         if (flags[5] & 0x04)
160                 add_event(dive, sample->time.seconds, 0, 0, 0, "Dive Time Info");
161         if (flags[5] & 0x08)
162                 add_event(dive, sample->time.seconds, 0, 0, 0, "Dive Time Alert");
163         if (flags[5] & 0x10)
164                 add_event(dive, sample->time.seconds, 0, 0, 0, "Marker");
165         if (flags[6] & 0x02)
166                 add_event(dive, sample->time.seconds, 0, 0, 0, "No Tank Data");
167         if (flags[6] & 0x04)
168                 add_event(dive, sample->time.seconds, 0, 0, 0, "Low Battery Warning");
169         if (flags[6] & 0x08)
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 */
173 }
174
175 /*
176  * parse uemis base64 data blob into struct dive
177  */
178 void uemis_parse_divelog_binary(char *base64, void *datap) {
179         int datalen;
180         int i;
181         uint8_t *data;
182         struct sample *sample;
183         uemis_sample_t *u_sample;
184         struct dive **divep = datap;
185         struct dive *dive = *divep;
186         int template, gasoffset;
187
188         datalen = uemis_convert_base64(base64, &data);
189
190         /* dive template in use:
191            0 = air
192            1 = nitrox (B)
193            2 = nitrox (B+D)
194            3 = nitrox (B+T+D)
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
197          */
198         gasoffset = template = *(uint8_t *)(data+115);
199         if (template == 3)
200                 gasoffset = 4;
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
209                  */
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;
214         }
215         /* first byte of divelog data is at offset 0x123 */
216         i = 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)
224                         break;
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);
234                 i += 0x25;
235                 u_sample++;
236         }
237         dive->duration.seconds = sample->time.seconds - 1;
238         return;
239 }