]> git.tdb.fi Git - ext/subsurface.git/blob - uemis.c
Fix default size for scrollable notebook
[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
18 #include "dive.h"
19 #include "uemis.h"
20
21 /*
22  * following code is based on code found in at base64.sourceforge.net/b64.c
23  * AUTHOR:         Bob Trower 08/04/01
24  * COPYRIGHT:      Copyright (c) Trantor Standard Systems Inc., 2001
25  * NOTE:           This source code may be used as you wish, subject to
26  *                 the MIT license.
27  */
28 /*
29  * Translation Table to decode (created by Bob Trower)
30  */
31 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
32
33 /*
34  * decodeblock -- decode 4 '6-bit' characters into 3 8-bit binary bytes
35  */
36 static void decodeblock( unsigned char in[4], unsigned char out[3] ) {
37         out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
38         out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
39         out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
40 }
41
42 /*
43  * decode a base64 encoded stream discarding padding, line breaks and noise
44  */
45 static void decode( uint8_t *inbuf, uint8_t *outbuf, int inbuf_len ) {
46         uint8_t in[4], out[3], v;
47         int i,len,indx_in=0,indx_out=0;
48
49         while (indx_in < inbuf_len) {
50                 for (len = 0, i = 0; i < 4 && (indx_in < inbuf_len); i++ ) {
51                         v = 0;
52                         while ((indx_in < inbuf_len) && v == 0) {
53                                 v = inbuf[indx_in++];
54                                 v = ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
55                                 if (v)
56                                         v = ((v == '$') ? 0 : v - 61);
57                         }
58                         if (indx_in < inbuf_len) {
59                                 len++;
60                                 if (v)
61                                         in[i] = (v - 1);
62                         }
63                         else
64                                 in[i] = 0;
65                 }
66                 if( len ) {
67                         decodeblock( in, out );
68                         for( i = 0; i < len - 1; i++ )
69                                 outbuf[indx_out++] = out[i];
70                 }
71         }
72 }
73 /* end code from Bob Trower */
74
75 /*
76  * pressure_to_depth: In centibar. And when converting to
77  * depth, I'm just going to always use saltwater, because I
78  * think "true depth" is just stupid. From a diving standpoint,
79  * "true depth" is pretty much completely pointless, unless
80  * you're doing some kind of underwater surveying work.
81  *
82  * So I give water depths in "pressure depth", always assuming
83  * salt water. So one atmosphere per 10m.
84  */
85 static int pressure_to_depth(uint16_t value)
86 {
87         double atm, cm;
88
89         atm = bar_to_atm(value / 100.0);
90         cm = 100 * atm + 0.5;
91         return (cm > 0) ? 10 * (long)cm : 0;
92 }
93
94 /*
95  * convert the base64 data blog
96  */
97 int uemis_convert_base64(char *base64, uint8_t **data) {
98         int len,datalen;
99
100         len = strlen(base64);
101         datalen = (len/4 + 1)*3;
102         if (datalen < 0x123+0x25) {
103                 /* less than header + 1 sample??? */
104                 fprintf(stderr,"suspiciously short data block\n");
105         }
106         *data = malloc(datalen);
107         if (! *data) {
108                 datalen = 0;
109                 fprintf(stderr,"Out of memory\n");
110                 goto bail;
111         }
112         decode(base64, *data, len);
113
114         if (memcmp(*data,"Dive\01\00\00",7))
115                 fprintf(stderr,"Missing Dive100 header\n");
116
117 bail:
118         return datalen;
119 }
120
121 /* Create events from the flag bits;
122  * These bits basically represent what is displayed on screen at sample time.
123  * Many of these 'warnings' are way hyper-active and seriously clutter the
124  * profile plot - so these are disabled by default
125  */
126 void uemis_event(struct dive *dive, struct sample *sample, uemis_sample_t *u_sample)
127 {
128         uint8_t *flags = u_sample->flags;
129
130         if (flags[1] & 0x01)
131                 add_event(dive, sample->time.seconds, 0, 0, 0, "Safety Stop Violation");
132         if (flags[1] & 0x08)
133                 add_event(dive, sample->time.seconds, 0, 0, 0, "Speed Alarm");
134 #if WANT_CRAZY_WARNINGS
135         if (flags[1] & 0x06) /* both bits 1 and 2 are a warning */
136                 add_event(dive, sample->time.seconds, 0, 0, 0, "Speed Warning");
137         if (flags[1] & 0x10)
138                 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Green Warning");
139 #endif
140         if (flags[1] & 0x20)
141                 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Ascend Warning");
142         if (flags[1] & 0x40)
143                 add_event(dive, sample->time.seconds, 0, 0, 0, "PO2 Ascend Alarm");
144         /* flags[2] reflects the deco / time bar
145          * flags[3] reflects more display details on deco and pO2 */
146         if (flags[4] & 0x01)
147                 add_event(dive, sample->time.seconds, 0, 0, 0, "Tank Pressure Info");
148         if (flags[4] & 0x04)
149                 add_event(dive, sample->time.seconds, 0, 0, 0, "RGT Warning");
150         if (flags[4] & 0x08)
151                 add_event(dive, sample->time.seconds, 0, 0, 0, "RGT Alert");
152         if (flags[4] & 0x40)
153                 add_event(dive, sample->time.seconds, 0, 0, 0, "Tank Change Suggested");
154         if (flags[4] & 0x80)
155                 add_event(dive, sample->time.seconds, 0, 0, 0, "Depth Limit Exceeded");
156         if (flags[5] & 0x01)
157                 add_event(dive, sample->time.seconds, 0, 0, 0, "Max Deco Time Warning");
158         if (flags[5] & 0x04)
159                 add_event(dive, sample->time.seconds, 0, 0, 0, "Dive Time Info");
160         if (flags[5] & 0x08)
161                 add_event(dive, sample->time.seconds, 0, 0, 0, "Dive Time Alert");
162         if (flags[5] & 0x10)
163                 add_event(dive, sample->time.seconds, 0, 0, 0, "Marker");
164         if (flags[6] & 0x02)
165                 add_event(dive, sample->time.seconds, 0, 0, 0, "No Tank Data");
166         if (flags[6] & 0x04)
167                 add_event(dive, sample->time.seconds, 0, 0, 0, "Low Battery Warning");
168         if (flags[6] & 0x08)
169                 add_event(dive, sample->time.seconds, 0, 0, 0, "Low Battery Alert");
170         /* flags[7] reflects the little on screen icons that remind of previous
171          * warnings / alerts - not useful for events */
172 }
173
174 /*
175  * parse uemis base64 data blob into struct dive
176  */
177 void uemis_parse_divelog_binary(char *base64, void *datap) {
178         int datalen;
179         int i;
180         uint8_t *data;
181         struct sample *sample;
182         uemis_sample_t *u_sample;
183         struct dive **divep = datap;
184         struct dive *dive = *divep;
185         int template, gasoffset;
186
187         datalen = uemis_convert_base64(base64, &data);
188
189         /* dive template in use:
190            0 = air
191            1 = nitrox (B)
192            2 = nitrox (B+D)
193            3 = nitrox (B+T+D)
194            uemis cylinder data is insane - it stores seven tank settings in a block
195            and the template tells us which of the four groups of tanks we need to look at
196          */
197         gasoffset = template = *(uint8_t *)(data+115);
198         if (template == 3)
199                 gasoffset = 4;
200         if (template == 0)
201                 template = 1;
202         for (i = 0; i < template; i++) {
203                 float volume = *(float *)(data+116+25*(gasoffset + i)) * 1000.0;
204                 /* uemis always assumes a working pressure of 202.6bar (!?!?) - I first thought
205                  * it was 3000psi, but testing against all my dives gets me that strange number.
206                  * Still, that's of course completely bogus and shows they don't get how
207                  * cylinders are named in non-metric parts of the world...
208                  * we store the incorrect working pressure to get the SAC calculations "close"
209                  * but the user will have to correct this manually
210                  */
211                 dive->cylinder[i].type.size.mliter = volume;
212                 dive->cylinder[i].type.workingpressure.mbar = 202600;
213                 dive->cylinder[i].gasmix.o2.permille = *(uint8_t *)(data+120+25*(gasoffset + i)) * 10 + 0.5;
214                 dive->cylinder[i].gasmix.he.permille = 0;
215         }
216         /* first byte of divelog data is at offset 0x123 */
217         i = 0x123;
218         u_sample = (uemis_sample_t *)(data + i);
219         while ((i < datalen) && (u_sample->dive_time)) {
220                 /* it seems that a dive_time of 0 indicates the end of the valid readings */
221                 /* the SDA usually records more samples after the end of the dive --
222                  * we want to discard those, but not cut the dive short; sadly the dive
223                  * duration in the header is a) in minutes and b) up to 3 minutes short */
224                 if (u_sample->dive_time > dive->duration.seconds + 180)
225                         break;
226                 sample = prepare_sample(divep);
227                 dive = *divep; /* prepare_sample might realloc the dive */
228                 sample->time.seconds = u_sample->dive_time;
229                 sample->depth.mm = pressure_to_depth(u_sample->water_pressure);
230                 sample->temperature.mkelvin = (u_sample->dive_temperature * 100) + 273150;
231                 sample->cylinderindex = u_sample->active_tank;
232                 sample->cylinderpressure.mbar= u_sample->tank_pressure * 10;
233                 uemis_event(dive, sample, u_sample);
234                 finish_sample(dive);
235                 i += 0x25;
236                 u_sample++;
237         }
238         dive->duration.seconds = sample->time.seconds - 1;
239         return;
240 }