]> git.tdb.fi Git - ext/subsurface.git/blob - uemis.c
Fix depth calculations in SDA import
[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 <gtk/gtk.h>
20
21 #include "dive.h"
22 #include "display.h"
23 #include "uemis.h"
24
25 /*
26  * following code is based on code found in at base64.sourceforge.net/b64.c
27  * AUTHOR:         Bob Trower 08/04/01
28  * COPYRIGHT:      Copyright (c) Trantor Standard Systems Inc., 2001
29  * NOTE:           This source code may be used as you wish, subject to
30  *                 the MIT license.
31  */
32 /*
33  * Translation Table to decode (created by Bob Trower)
34  */
35 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
36
37 /*
38  * decodeblock -- decode 4 '6-bit' characters into 3 8-bit binary bytes
39  */
40 static void decodeblock( unsigned char in[4], unsigned char out[3] ) {
41         out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
42         out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
43         out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
44 }
45
46 /*
47  * decode a base64 encoded stream discarding padding, line breaks and noise
48  */
49 static void decode( uint8_t *inbuf, uint8_t *outbuf, int inbuf_len ) {
50         uint8_t in[4], out[3], v;
51         int i,len,indx_in=0,indx_out=0;
52
53         while (indx_in < inbuf_len) {
54                 for (len = 0, i = 0; i < 4 && (indx_in < inbuf_len); i++ ) {
55                         v = 0;
56                         while ((indx_in < inbuf_len) && v == 0) {
57                                 v = inbuf[indx_in++];
58                                 v = ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
59                                 if (v)
60                                         v = ((v == '$') ? 0 : v - 61);
61                         }
62                         if (indx_in < inbuf_len) {
63                                 len++;
64                                 if (v)
65                                         in[i] = (v - 1);
66                         }
67                         else
68                                 in[i] = 0;
69                 }
70                 if( len ) {
71                         decodeblock( in, out );
72                         for( i = 0; i < len - 1; i++ )
73                                 outbuf[indx_out++] = out[i];
74                 }
75         }
76 }
77 /* end code from Bob Trower */
78
79 /* small helper functions */
80 /* simpleregex allocates (and reallocates) the found buffer
81  * don't forget to free it when you are done
82  */
83 static int simpleregex(char *buffer, char *regex, char **found) {
84         int status;
85         regex_t re;
86         regmatch_t match[5];
87
88         if (regcomp(&re, regex, 0) !=0) {
89                 fprintf(stderr,"internal error, regex failed!\n");
90                 exit(1);
91         }
92         status = regexec(&re,buffer,5,match,0);
93         if(status == 0) {
94                 *found = realloc(*found,match[1].rm_eo-match[1].rm_so + 1);
95                 strncpy(*found,buffer+match[1].rm_so,match[1].rm_eo-match[1].rm_so);
96                 (*found)[match[1].rm_eo-match[1].rm_so] = '\0';
97         }
98         return(status == 0);
99 }
100
101 /* read in line of arbitrary length (important for SDA files that can
102  * have lines that are tens of kB long
103  * don't forget to free it when you are done
104  */
105 #define MYGETL_INCR 1024
106 static char * mygetline(FILE * f) {
107         size_t size = 0;
108         size_t len  = 0;
109         char * buf  = NULL;
110
111         do {
112                 size += MYGETL_INCR;
113                 if ((buf = realloc(buf,size)) == NULL)
114                         break;
115                 fgets(buf+len,MYGETL_INCR,f);
116                 len = strlen(buf);
117         } while (!feof(f) && buf[len-1]!='\n');
118         return buf;
119 }
120
121 /* text matching, used to build very poor man's XML parser */
122 int matchit(FILE *infd, char *regex, char *typeregex, char **found) {
123         char *buffer;
124
125         while (!feof(infd)) {
126                 buffer = mygetline(infd);
127                 if (buffer && simpleregex(buffer,regex,found)) {
128                         buffer = mygetline(infd);
129                         if (buffer && simpleregex(buffer,typeregex,found)) {
130                                 return 1;
131                         }
132                 }
133         }
134         return 0;
135 }
136
137 /*
138  * pressure_to_depth: In centibar. And when converting to
139  * depth, I'm just going to always use saltwater, because I
140  * think "true depth" is just stupid. From a diving standpoint,
141  * "true depth" is pretty much completely pointless, unless
142  * you're doing some kind of underwater surveying work.
143  *
144  * So I give water depths in "pressure depth", always assuming
145  * salt water. So one atmosphere per 10m.
146  */
147 static int pressure_to_depth(uint16_t value)
148 {
149         double atm, cm;
150
151         atm = (value / 100.0) / 1.01325;
152         cm = 100 * atm + 0.5;
153         return( (cm > 0) ? 10 * (long)cm : 0);
154 }
155
156 /*
157  * convert the base64 data blog
158  */
159 int uemis_convert_base64(char *base64, uint8_t **data) {
160         int len,datalen;
161
162         len = strlen(base64);
163         datalen = (len/4 + 1)*3;
164         if (datalen < 0x123+0x25) {
165                 /* less than header + 1 sample??? */
166                 fprintf(stderr,"suspiciously short data block\n");
167         }
168         *data = malloc(datalen);
169         if (! *data) {
170                 datalen = 0;
171                 fprintf(stderr,"Out of memory\n");
172                 goto bail;
173         }
174         decode(base64, *data, len);
175
176         if (memcmp(*data,"Dive\01\00\00",7))
177                 fprintf(stderr,"Missing Dive100 header\n");
178
179 bail:
180         return datalen;
181 }
182
183 /*
184  * parse uemis base64 data blob into struct dive
185  */
186 static void parse_divelog_binary(char *base64, struct dive **divep) {
187         int datalen;
188         int i;
189         uint8_t *data;
190         struct sample *sample;
191         struct dive *dive = *divep;
192
193         datalen = uemis_convert_base64(base64, &data);
194
195         /* first byte of divelog data is at offset 0x123 */
196         i = 0x123;
197         while ((i < datalen) && (*(uint16_t *)(data+i))) {
198                 /* it seems that a dive_time of 0 indicates the end of the valid readings */
199                 /* the SDA usually records more samples after the end of the dive --
200                  * we want to discard those, but not cut the dive short; sadly the dive
201                  * duration in the header is a) in minutes and b) up to 3 minutes short */
202                 if (*(uint16_t *)(data+i) > dive->duration.seconds + 180)
203                         break;
204                 sample = prepare_sample(divep);
205                 sample->time.seconds = *(uint16_t *)(data+i);
206                 sample->depth.mm = pressure_to_depth(*(uint16_t *)(data+i+2));
207                 sample->temperature.mkelvin = (*(uint16_t *)(data+i+4) * 100) + 273150;
208                 sample->cylinderpressure.mbar= *(uint16_t *)(data+i+23) * 10;
209                 sample->cylinderindex = *(uint8_t *)(data+i+22);
210                 finish_sample(*divep, sample);
211                 i += 0x25;
212         }
213         dive->duration.seconds = sample->time.seconds - 1;
214         return;
215 }
216
217 void
218 parse_uemis_file(char *divelogfilename,GError **error) {
219         char *found=NULL;
220         struct tm tm;
221         struct dive *dive;
222
223         FILE *divelogfile = fopen(divelogfilename,"r");
224
225         dive = alloc_dive();
226
227         if (! matchit(divelogfile,"val key=\"date\"","<ts>\\([^<]*\\)</ts>",&found)) {
228                 /* some error handling */
229                 goto bail;
230         }
231         strptime(found, "%Y-%m-%dT%H:%M:%S", &tm);
232         dive->when = utc_mktime(&tm);
233         if (! matchit(divelogfile,"<val key=\"duration\">",
234                         "<float>\\([0-9.]*\\)</float>", &found)) {
235                 /* some error handling */
236                 goto bail;
237         }
238         dive->duration.seconds = 60.0 * atof(found);
239
240         if (! matchit(divelogfile,"<val key=\"depth\">",
241                         "<int>\\([0-9.]*\\)</int>", &found)) {
242                 /* some error handling */
243                 goto bail;
244         }
245         dive->maxdepth.mm = pressure_to_depth(atoi(found));
246
247         if (! matchit(divelogfile,"<val key=\"file_content\">",
248                         ">\\([a-zA-Z0-9+/]*\\)<", &found)) {
249                 /* some error handling */
250                 goto bail;
251         }
252         parse_divelog_binary(found,&dive);
253         record_dive(dive);
254 bail:
255         if (found)
256                 free(found);
257 }
258
259 /*
260  * parse the two files extracted from the SDA
261  */
262 void
263 uemis_import() {
264         GtkWidget *dialog;
265         GtkFileFilter *filter = gtk_file_filter_new ();
266         gtk_file_filter_add_pattern (filter, "*.SDA");
267         gtk_file_filter_set_name(filter, "uemis Zurich SDA files");
268         dialog = gtk_file_chooser_dialog_new("Open File",
269                                         GTK_WINDOW(main_window),
270                                         GTK_FILE_CHOOSER_ACTION_OPEN,
271                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
272                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
273                                         NULL);
274         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
275         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
276
277         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
278                 GSList *filenames;
279                 char *filename;
280                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
281
282                 GError *error = NULL;
283                 while(filenames != NULL) {
284                         filename = (char *)filenames->data;
285                         parse_uemis_file(filename, &error);
286                         if (error != NULL)
287                         {
288                                 report_error(error);
289                                 g_error_free(error);
290                                 error = NULL;
291                         }
292
293                         g_free(filename);
294                         filenames = g_slist_next(filenames);
295                 }
296                 g_slist_free(filenames);
297                 report_dives();
298         }
299         gtk_widget_destroy(dialog);
300 }