1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
|
// SPDX-License-Identifier: GPL-2.0
#ifdef __clang__
// Clang has a bug on zero-initialization of C structs.
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#endif
#include "dive.h"
#include "parse.h"
#include "divelist.h"
#include "device.h"
#include "membuffer.h"
#include "gettext.h"
extern int dm4_events(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
event_start();
if (data[1])
cur_event.time.seconds = atoi(data[1]);
if (data[2]) {
switch (atoi(data[2])) {
case 1:
/* 1 Mandatory Safety Stop */
strcpy(cur_event.name, "safety stop (mandatory)");
break;
case 3:
/* 3 Deco */
/* What is Subsurface's term for going to
* deco? */
strcpy(cur_event.name, "deco");
break;
case 4:
/* 4 Ascent warning */
strcpy(cur_event.name, "ascent");
break;
case 5:
/* 5 Ceiling broken */
strcpy(cur_event.name, "violation");
break;
case 6:
/* 6 Mandatory safety stop ceiling error */
strcpy(cur_event.name, "violation");
break;
case 7:
/* 7 Below deco floor */
strcpy(cur_event.name, "below floor");
break;
case 8:
/* 8 Dive time alarm */
strcpy(cur_event.name, "divetime");
break;
case 9:
/* 9 Depth alarm */
strcpy(cur_event.name, "maxdepth");
break;
case 10:
/* 10 OLF 80% */
case 11:
/* 11 OLF 100% */
strcpy(cur_event.name, "OLF");
break;
case 12:
/* 12 High pO₂ */
strcpy(cur_event.name, "PO2");
break;
case 13:
/* 13 Air time */
strcpy(cur_event.name, "airtime");
break;
case 17:
/* 17 Ascent warning */
strcpy(cur_event.name, "ascent");
break;
case 18:
/* 18 Ceiling error */
strcpy(cur_event.name, "ceiling");
break;
case 19:
/* 19 Surfaced */
strcpy(cur_event.name, "surface");
break;
case 20:
/* 20 Deco */
strcpy(cur_event.name, "deco");
break;
case 22:
case 32:
/* 22 Mandatory safety stop violation */
/* 32 Deep stop violation */
strcpy(cur_event.name, "violation");
break;
case 30:
/* Tissue level warning */
strcpy(cur_event.name, "tissue warning");
break;
case 37:
/* Tank pressure alarm */
strcpy(cur_event.name, "tank pressure");
break;
case 257:
/* 257 Dive active */
/* This seems to be given after surface when
* descending again. */
strcpy(cur_event.name, "surface");
break;
case 258:
/* 258 Bookmark */
if (data[3]) {
strcpy(cur_event.name, "heading");
cur_event.value = atoi(data[3]);
} else {
strcpy(cur_event.name, "bookmark");
}
break;
case 259:
/* Deep stop */
strcpy(cur_event.name, "Deep stop");
break;
case 260:
/* Deep stop */
strcpy(cur_event.name, "Deep stop cleared");
break;
case 266:
/* Mandatory safety stop activated */
strcpy(cur_event.name, "safety stop (mandatory)");
break;
case 267:
/* Mandatory safety stop deactivated */
/* DM5 shows this only on event list, not on the
* profile so skipping as well for now */
break;
default:
strcpy(cur_event.name, "unknown");
cur_event.value = atoi(data[2]);
break;
}
}
event_end();
return 0;
}
extern int dm5_cylinders(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
cylinder_start();
if (data[7] && atoi(data[7]) > 0 && atoi(data[7]) < 350000)
cur_dive->cylinder[cur_cylinder_index].start.mbar = atoi(data[7]);
if (data[8] && atoi(data[8]) > 0 && atoi(data[8]) < 350000)
cur_dive->cylinder[cur_cylinder_index].end.mbar = (atoi(data[8]));
if (data[6]) {
/* DM5 shows tank size of 12 liters when the actual
* value is 0 (and using metric units). So we just use
* the same 12 liters when size is not available */
if (strtod_flags(data[6], NULL, 0) == 0.0 && cur_dive->cylinder[cur_cylinder_index].start.mbar)
cur_dive->cylinder[cur_cylinder_index].type.size.mliter = 12000;
else
cur_dive->cylinder[cur_cylinder_index].type.size.mliter = lrint((strtod_flags(data[6], NULL, 0)) * 1000);
}
if (data[2])
cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = atoi(data[2]) * 10;
if (data[3])
cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = atoi(data[3]) * 10;
cylinder_end();
return 0;
}
extern int dm5_gaschange(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
event_start();
if (data[0])
cur_event.time.seconds = atoi(data[0]);
if (data[1]) {
strcpy(cur_event.name, "gaschange");
cur_event.value = lrint(strtod_flags(data[1], NULL, 0));
}
/* He part of the mix */
if (data[2])
cur_event.value += lrint(strtod_flags(data[2], NULL, 0)) << 16;
event_end();
return 0;
}
extern int dm4_tags(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
if (data[0])
taglist_add_tag(&cur_dive->tag_list, data[0]);
return 0;
}
extern int dm4_dive(void *param, int columns, char **data, char **column)
{
(void) columns;
(void) column;
unsigned int i;
int interval, retval = 0;
sqlite3 *handle = (sqlite3 *)param;
float *profileBlob;
unsigned char *tempBlob;
int *pressureBlob;
char *err = NULL;
char get_events_template[] = "select * from Mark where DiveId = %d";
char get_tags_template[] = "select Text from DiveTag where DiveId = %d";
char get_events[64];
dive_start();
cur_dive->number = atoi(data[0]);
cur_dive->when = (time_t)(atol(data[1]));
if (data[2])
utf8_string(data[2], &cur_dive->notes);
/*
* DM4 stores Duration and DiveTime. It looks like DiveTime is
* 10 to 60 seconds shorter than Duration. However, I have no
* idea what is the difference and which one should be used.
* Duration = data[3]
* DiveTime = data[15]
*/
if (data[3])
cur_dive->duration.seconds = atoi(data[3]);
if (data[15])
cur_dive->dc.duration.seconds = atoi(data[15]);
/*
* TODO: the deviceid hash should be calculated here.
*/
settings_start();
dc_settings_start();
if (data[4])
utf8_string(data[4], &cur_settings.dc.serial_nr);
if (data[5])
utf8_string(data[5], &cur_settings.dc.model);
cur_settings.dc.deviceid = 0xffffffff;
dc_settings_end();
settings_end();
if (data[6])
cur_dive->dc.maxdepth.mm = lrint(strtod_flags(data[6], NULL, 0) * 1000);
if (data[8])
cur_dive->dc.airtemp.mkelvin = C_to_mkelvin(atoi(data[8]));
if (data[9])
cur_dive->dc.watertemp.mkelvin = C_to_mkelvin(atoi(data[9]));
/*
* TODO: handle multiple cylinders
*/
cylinder_start();
if (data[22] && atoi(data[22]) > 0)
cur_dive->cylinder[cur_cylinder_index].start.mbar = atoi(data[22]);
else if (data[10] && atoi(data[10]) > 0)
cur_dive->cylinder[cur_cylinder_index].start.mbar = atoi(data[10]);
if (data[23] && atoi(data[23]) > 0)
cur_dive->cylinder[cur_cylinder_index].end.mbar = (atoi(data[23]));
if (data[11] && atoi(data[11]) > 0)
cur_dive->cylinder[cur_cylinder_index].end.mbar = (atoi(data[11]));
if (data[12])
cur_dive->cylinder[cur_cylinder_index].type.size.mliter = lrint((strtod_flags(data[12], NULL, 0)) * 1000);
if (data[13])
cur_dive->cylinder[cur_cylinder_index].type.workingpressure.mbar = (atoi(data[13]));
if (data[20])
cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = atoi(data[20]) * 10;
if (data[21])
cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = atoi(data[21]) * 10;
cylinder_end();
if (data[14])
cur_dive->dc.surface_pressure.mbar = (atoi(data[14]) * 1000);
interval = data[16] ? atoi(data[16]) : 0;
profileBlob = (float *)data[17];
tempBlob = (unsigned char *)data[18];
pressureBlob = (int *)data[19];
for (i = 0; interval && i * interval < cur_dive->duration.seconds; i++) {
sample_start();
cur_sample->time.seconds = i * interval;
if (profileBlob)
cur_sample->depth.mm = lrintf(profileBlob[i] * 1000.0f);
else
cur_sample->depth.mm = cur_dive->dc.maxdepth.mm;
if (data[18] && data[18][0])
cur_sample->temperature.mkelvin = C_to_mkelvin(tempBlob[i]);
if (data[19] && data[19][0])
cur_sample->pressure[0].mbar = pressureBlob[i];
sample_end();
}
snprintf(get_events, sizeof(get_events) - 1, get_events_template, cur_dive->number);
retval = sqlite3_exec(handle, get_events, &dm4_events, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query dm4_events failed.\n");
return 1;
}
snprintf(get_events, sizeof(get_events) - 1, get_tags_template, cur_dive->number);
retval = sqlite3_exec(handle, get_events, &dm4_tags, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query dm4_tags failed.\n");
return 1;
}
dive_end();
/*
for (i=0; i<columns;++i) {
fprintf(stderr, "%s\t", column[i]);
}
fprintf(stderr, "\n");
for (i=0; i<columns;++i) {
fprintf(stderr, "%s\t", data[i]);
}
fprintf(stderr, "\n");
//exit(0);
*/
return SQLITE_OK;
}
extern int dm5_dive(void *param, int columns, char **data, char **column)
{
(void) columns;
(void) column;
unsigned int i;
int interval, retval = 0, block_size;
sqlite3 *handle = (sqlite3 *)param;
unsigned const char *sampleBlob;
char *err = NULL;
char get_events_template[] = "select * from Mark where DiveId = %d";
char get_tags_template[] = "select Text from DiveTag where DiveId = %d";
char get_cylinders_template[] = "select * from DiveMixture where DiveId = %d";
char get_gaschange_template[] = "select GasChangeTime,Oxygen,Helium from DiveGasChange join DiveMixture on DiveGasChange.DiveMixtureId=DiveMixture.DiveMixtureId where DiveId = %d";
char get_events[512];
dive_start();
cur_dive->number = atoi(data[0]);
cur_dive->when = (time_t)(atol(data[1]));
if (data[2])
utf8_string(data[2], &cur_dive->notes);
if (data[3])
cur_dive->duration.seconds = atoi(data[3]);
if (data[15])
cur_dive->dc.duration.seconds = atoi(data[15]);
/*
* TODO: the deviceid hash should be calculated here.
*/
settings_start();
dc_settings_start();
if (data[4]) {
utf8_string(data[4], &cur_settings.dc.serial_nr);
cur_settings.dc.deviceid = atoi(data[4]);
}
if (data[5])
utf8_string(data[5], &cur_settings.dc.model);
dc_settings_end();
settings_end();
if (data[6])
cur_dive->dc.maxdepth.mm = lrint(strtod_flags(data[6], NULL, 0) * 1000);
if (data[8])
cur_dive->dc.airtemp.mkelvin = C_to_mkelvin(atoi(data[8]));
if (data[9])
cur_dive->dc.watertemp.mkelvin = C_to_mkelvin(atoi(data[9]));
if (data[4]) {
cur_dive->dc.deviceid = atoi(data[4]);
}
if (data[5])
utf8_string(data[5], &cur_dive->dc.model);
snprintf(get_events, sizeof(get_events) - 1, get_cylinders_template, cur_dive->number);
retval = sqlite3_exec(handle, get_events, &dm5_cylinders, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query dm5_cylinders failed.\n");
return 1;
}
if (data[14])
cur_dive->dc.surface_pressure.mbar = (atoi(data[14]) / 100);
interval = data[16] ? atoi(data[16]) : 0;
sampleBlob = (unsigned const char *)data[24];
if (sampleBlob) {
switch (sampleBlob[0]) {
case 2:
block_size = 19;
break;
case 3:
block_size = 23;
break;
case 4:
block_size = 26;
break;
default:
block_size = 16;
break;
}
}
for (i = 0; interval && sampleBlob && i * interval < cur_dive->duration.seconds; i++) {
float *depth = (float *)&sampleBlob[i * block_size + 3];
int32_t temp = (sampleBlob[i * block_size + 10] << 8) + sampleBlob[i * block_size + 11];
int32_t pressure = (sampleBlob[i * block_size + 9] << 16) + (sampleBlob[i * block_size + 8] << 8) + sampleBlob[i * block_size + 7];
sample_start();
cur_sample->time.seconds = i * interval;
cur_sample->depth.mm = lrintf(depth[0] * 1000.0f);
/*
* Limit temperatures and cylinder pressures to somewhat
* sensible values
*/
if (temp >= -10 && temp < 50)
cur_sample->temperature.mkelvin = C_to_mkelvin(temp);
if (pressure >= 0 && pressure < 350000)
cur_sample->pressure[0].mbar = pressure;
sample_end();
}
/*
* Log was converted from DM4, thus we need to parse the profile
* from DM4 format
*/
if (i == 0) {
float *profileBlob;
unsigned char *tempBlob;
int *pressureBlob;
profileBlob = (float *)data[17];
tempBlob = (unsigned char *)data[18];
pressureBlob = (int *)data[19];
for (i = 0; interval && i * interval < cur_dive->duration.seconds; i++) {
sample_start();
cur_sample->time.seconds = i * interval;
if (profileBlob)
cur_sample->depth.mm = lrintf(profileBlob[i] * 1000.0f);
else
cur_sample->depth.mm = cur_dive->dc.maxdepth.mm;
if (data[18] && data[18][0])
cur_sample->temperature.mkelvin = C_to_mkelvin(tempBlob[i]);
if (data[19] && data[19][0])
cur_sample->pressure[0].mbar = pressureBlob[i];
sample_end();
}
}
snprintf(get_events, sizeof(get_events) - 1, get_gaschange_template, cur_dive->number);
retval = sqlite3_exec(handle, get_events, &dm5_gaschange, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query dm5_gaschange failed.\n");
return 1;
}
snprintf(get_events, sizeof(get_events) - 1, get_events_template, cur_dive->number);
retval = sqlite3_exec(handle, get_events, &dm4_events, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query dm4_events failed.\n");
return 1;
}
snprintf(get_events, sizeof(get_events) - 1, get_tags_template, cur_dive->number);
retval = sqlite3_exec(handle, get_events, &dm4_tags, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query dm4_tags failed.\n");
return 1;
}
dive_end();
return SQLITE_OK;
}
int parse_dm4_buffer(sqlite3 *handle, const char *url, const char *buffer, int size,
struct dive_table *table)
{
(void) buffer;
(void) size;
int retval;
char *err = NULL;
target_table = table;
/* StartTime is converted from Suunto's nano seconds to standard
* time. We also need epoch, not seconds since year 1. */
char get_dives[] = "select D.DiveId,StartTime/10000000-62135596800,Note,Duration,SourceSerialNumber,Source,MaxDepth,SampleInterval,StartTemperature,BottomTemperature,D.StartPressure,D.EndPressure,Size,CylinderWorkPressure,SurfacePressure,DiveTime,SampleInterval,ProfileBlob,TemperatureBlob,PressureBlob,Oxygen,Helium,MIX.StartPressure,MIX.EndPressure FROM Dive AS D JOIN DiveMixture AS MIX ON D.DiveId=MIX.DiveId";
retval = sqlite3_exec(handle, get_dives, &dm4_dive, handle, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "Database query failed '%s'.\n", url);
return 1;
}
return 0;
}
int parse_dm5_buffer(sqlite3 *handle, const char *url, const char *buffer, int size,
struct dive_table *table)
{
(void) buffer;
(void) size;
int retval;
char *err = NULL;
target_table = table;
/* StartTime is converted from Suunto's nano seconds to standard
* time. We also need epoch, not seconds since year 1. */
char get_dives[] = "select DiveId,StartTime/10000000-62135596800,Note,Duration,coalesce(SourceSerialNumber,SerialNumber),Source,MaxDepth,SampleInterval,StartTemperature,BottomTemperature,StartPressure,EndPressure,'','',SurfacePressure,DiveTime,SampleInterval,ProfileBlob,TemperatureBlob,PressureBlob,'','','','',SampleBlob FROM Dive where Deleted is null";
retval = sqlite3_exec(handle, get_dives, &dm5_dive, handle, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "Database query failed '%s'.\n", url);
return 1;
}
return 0;
}
extern int shearwater_cylinders(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
int o2 = lrint(strtod_flags(data[0], NULL, 0) * 1000);
int he = lrint(strtod_flags(data[1], NULL, 0) * 1000);
/* Shearwater allows entering only 99%, not 100%
* so assume 99% to be pure oxygen */
if (o2 == 990 && he == 0)
o2 = 1000;
cylinder_start();
cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = o2;
cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = he;
cylinder_end();
return 0;
}
extern int shearwater_changes(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
if (columns != 3) {
return 1;
}
if (!data[0] || !data[1] || !data[2]) {
return 2;
}
int o2 = lrint(strtod_flags(data[1], NULL, 0) * 1000);
int he = lrint(strtod_flags(data[2], NULL, 0) * 1000);
/* Shearwater allows entering only 99%, not 100%
* so assume 99% to be pure oxygen */
if (o2 == 990 && he == 0)
o2 = 1000;
// Find the cylinder index
int i;
bool found = false;
for (i = 0; i < cur_cylinder_index; ++i) {
if (cur_dive->cylinder[i].gasmix.o2.permille == o2 && cur_dive->cylinder[i].gasmix.he.permille == he) {
found = true;
break;
}
}
if (!found) {
// Cylinder not found, creating a new one
cylinder_start();
cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = o2;
cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = he;
cylinder_end();
i = cur_cylinder_index;
}
add_gas_switch_event(cur_dive, get_dc(), atoi(data[0]), i);
return 0;
}
extern int shearwater_profile_sample(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
sample_start();
if (data[0])
cur_sample->time.seconds = atoi(data[0]);
if (data[1])
cur_sample->depth.mm = metric ? lrint(strtod_flags(data[1], NULL, 0) * 1000) : feet_to_mm(strtod_flags(data[1], NULL, 0));
if (data[2])
cur_sample->temperature.mkelvin = metric ? C_to_mkelvin(strtod_flags(data[2], NULL, 0)) : F_to_mkelvin(strtod_flags(data[2], NULL, 0));
if (data[3]) {
cur_sample->setpoint.mbar = lrint(strtod_flags(data[3], NULL, 0) * 1000);
}
if (data[4])
cur_sample->ndl.seconds = atoi(data[4]) * 60;
if (data[5])
cur_sample->cns = atoi(data[5]);
if (data[6])
cur_sample->stopdepth.mm = metric ? atoi(data[6]) * 1000 : feet_to_mm(atoi(data[6]));
/* We don't actually have data[3], but it should appear in the
* SQL query at some point.
if (data[3])
cur_sample->pressure[0].mbar = metric ? atoi(data[3]) * 1000 : psi_to_mbar(atoi(data[3]));
*/
sample_end();
return 0;
}
extern int shearwater_ai_profile_sample(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
sample_start();
if (data[0])
cur_sample->time.seconds = atoi(data[0]);
if (data[1])
cur_sample->depth.mm = metric ? lrint(strtod_flags(data[1], NULL, 0) * 1000) : feet_to_mm(strtod_flags(data[1], NULL, 0));
if (data[2])
cur_sample->temperature.mkelvin = metric ? C_to_mkelvin(strtod_flags(data[2], NULL, 0)) : F_to_mkelvin(strtod_flags(data[2], NULL, 0));
if (data[3]) {
cur_sample->setpoint.mbar = lrint(strtod_flags(data[3], NULL, 0) * 1000);
}
if (data[4])
cur_sample->ndl.seconds = atoi(data[4]) * 60;
if (data[5])
cur_sample->cns = atoi(data[5]);
if (data[6])
cur_sample->stopdepth.mm = metric ? atoi(data[6]) * 1000 : feet_to_mm(atoi(data[6]));
/* Weird unit conversion but seems to produce correct results.
* Also missing values seems to be reported as a 4092 (564 bar) */
if (data[7] && atoi(data[7]) != 4092) {
cur_sample->pressure[0].mbar = psi_to_mbar(atoi(data[7])) * 2;
}
if (data[8] && atoi(data[8]) != 4092)
cur_sample->pressure[1].mbar = psi_to_mbar(atoi(data[8])) * 2;
sample_end();
return 0;
}
extern int shearwater_mode(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
if (data[0])
cur_dive->dc.divemode = atoi(data[0]) == 0 ? CCR : OC;
return 0;
}
extern int shearwater_dive(void *param, int columns, char **data, char **column)
{
(void) columns;
(void) column;
int retval = 0;
sqlite3 *handle = (sqlite3 *)param;
char *err = NULL;
char get_profile_template[] = "select currentTime,currentDepth,waterTemp,averagePPO2,currentNdl,CNSPercent,decoCeiling from dive_log_records where diveLogId=%d";
char get_profile_template_ai[] = "select currentTime,currentDepth,waterTemp,averagePPO2,currentNdl,CNSPercent,decoCeiling,aiSensor0_PressurePSI,aiSensor1_PressurePSI from dive_log_records where diveLogId = %d";
char get_cylinder_template[] = "select fractionO2,fractionHe from dive_log_records where diveLogId = %d group by fractionO2,fractionHe";
char get_changes_template[] = "select a.currentTime,a.fractionO2,a.fractionHe from dive_log_records as a,dive_log_records as b where (a.id - 1) = b.id and (a.fractionO2 != b.fractionO2 or a.fractionHe != b.fractionHe) and a.diveLogId=b.divelogId and a.diveLogId = %d";
char get_mode_template[] = "select distinct currentCircuitSetting from dive_log_records where diveLogId = %d";
char get_buffer[1024];
dive_start();
cur_dive->number = atoi(data[0]);
cur_dive->when = (time_t)(atol(data[1]));
int dive_id = atoi(data[11]);
if (data[2])
add_dive_site(data[2], cur_dive);
if (data[3])
utf8_string(data[3], &cur_dive->buddy);
if (data[4])
utf8_string(data[4], &cur_dive->notes);
metric = atoi(data[5]) == 1 ? 0 : 1;
/* TODO: verify that metric calculation is correct */
if (data[6])
cur_dive->dc.maxdepth.mm = metric ? lrint(strtod_flags(data[6], NULL, 0) * 1000) : feet_to_mm(strtod_flags(data[6], NULL, 0));
if (data[7])
cur_dive->dc.duration.seconds = atoi(data[7]) * 60;
if (data[8])
cur_dive->dc.surface_pressure.mbar = atoi(data[8]);
/*
* TODO: the deviceid hash should be calculated here.
*/
settings_start();
dc_settings_start();
if (data[9])
utf8_string(data[9], &cur_settings.dc.serial_nr);
if (data[10]) {
switch (atoi(data[10])) {
case 2:
cur_settings.dc.model = strdup("Shearwater Petrel/Perdix");
break;
case 4:
cur_settings.dc.model = strdup("Shearwater Predator");
break;
default:
cur_settings.dc.model = strdup("Shearwater import");
break;
}
}
cur_settings.dc.deviceid = atoi(data[9]);
dc_settings_end();
settings_end();
if (data[10]) {
switch (atoi(data[10])) {
case 2:
cur_dive->dc.model = strdup("Shearwater Petrel/Perdix");
break;
case 4:
cur_dive->dc.model = strdup("Shearwater Predator");
break;
default:
cur_dive->dc.model = strdup("Shearwater import");
break;
}
}
if (data[11]) {
snprintf(get_buffer, sizeof(get_buffer) - 1, get_mode_template, dive_id);
retval = sqlite3_exec(handle, get_buffer, &shearwater_mode, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query shearwater_mode failed.\n");
return 1;
}
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder_template, dive_id);
retval = sqlite3_exec(handle, get_buffer, &shearwater_cylinders, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query shearwater_cylinders failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_changes_template, dive_id);
retval = sqlite3_exec(handle, get_buffer, &shearwater_changes, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query shearwater_changes failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template_ai, dive_id);
retval = sqlite3_exec(handle, get_buffer, &shearwater_ai_profile_sample, 0, &err);
if (retval != SQLITE_OK) {
snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template, dive_id);
retval = sqlite3_exec(handle, get_buffer, &shearwater_profile_sample, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query shearwater_profile_sample failed.\n");
return 1;
}
}
dive_end();
return SQLITE_OK;
}
int parse_shearwater_buffer(sqlite3 *handle, const char *url, const char *buffer, int size,
struct dive_table *table)
{
(void) buffer;
(void) size;
int retval;
char *err = NULL;
target_table = table;
char get_dives[] = "select l.number,timestamp,location||' / '||site,buddy,notes,imperialUnits,maxDepth,maxTime,startSurfacePressure,computerSerial,computerModel,i.diveId FROM dive_info AS i JOIN dive_logs AS l ON i.diveId=l.diveId";
retval = sqlite3_exec(handle, get_dives, &shearwater_dive, handle, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "Database query failed '%s'.\n", url);
return 1;
}
return 0;
}
extern int cobalt_profile_sample(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
sample_start();
if (data[0])
cur_sample->time.seconds = atoi(data[0]);
if (data[1])
cur_sample->depth.mm = atoi(data[1]);
if (data[2])
cur_sample->temperature.mkelvin = metric ? C_to_mkelvin(strtod_flags(data[2], NULL, 0)) : F_to_mkelvin(strtod_flags(data[2], NULL, 0));
sample_end();
return 0;
}
extern int cobalt_cylinders(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
cylinder_start();
if (data[0])
cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = atoi(data[0]) * 10;
if (data[1])
cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = atoi(data[1]) * 10;
if (data[2])
cur_dive->cylinder[cur_cylinder_index].start.mbar = psi_to_mbar(atoi(data[2]));
if (data[3])
cur_dive->cylinder[cur_cylinder_index].end.mbar = psi_to_mbar(atoi(data[3]));
if (data[4])
cur_dive->cylinder[cur_cylinder_index].type.size.mliter = atoi(data[4]) * 100;
if (data[5])
cur_dive->cylinder[cur_cylinder_index].gas_used.mliter = atoi(data[5]) * 1000;
cylinder_end();
return 0;
}
extern int cobalt_buddies(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
if (data[0])
utf8_string(data[0], &cur_dive->buddy);
return 0;
}
/*
* We still need to figure out how to map free text visibility to
* Subsurface star rating.
*/
extern int cobalt_visibility(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
(void) data;
return 0;
}
extern int cobalt_location(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
static char *location = NULL;
if (data[0]) {
if (location) {
char *tmp = malloc(strlen(location) + strlen(data[0]) + 4);
if (!tmp)
return -1;
sprintf(tmp, "%s / %s", location, data[0]);
free(location);
location = NULL;
cur_dive->dive_site_uuid = find_or_create_dive_site_with_name(tmp, cur_dive->when);
free(tmp);
} else {
location = strdup(data[0]);
}
}
return 0;
}
extern int cobalt_dive(void *param, int columns, char **data, char **column)
{
(void) columns;
(void) column;
int retval = 0;
sqlite3 *handle = (sqlite3 *)param;
char *err = NULL;
char get_profile_template[] = "select runtime*60,(DepthPressure*10000/SurfacePressure)-10000,p.Temperature from Dive AS d JOIN TrackPoints AS p ON d.Id=p.DiveId where d.Id=%d";
char get_cylinder_template[] = "select FO2,FHe,StartingPressure,EndingPressure,TankSize,TankPressure,TotalConsumption from GasMixes where DiveID=%d and StartingPressure>0 and EndingPressure > 0 group by FO2,FHe";
char get_buddy_template[] = "select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=4";
char get_visibility_template[] = "select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=3";
char get_location_template[] = "select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=0";
char get_site_template[] = "select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=1";
char get_buffer[1024];
dive_start();
cur_dive->number = atoi(data[0]);
cur_dive->when = (time_t)(atol(data[1]));
if (data[4])
utf8_string(data[4], &cur_dive->notes);
/* data[5] should have information on Units used, but I cannot
* parse it at all based on the sample log I have received. The
* temperatures in the samples are all Imperial, so let's go by
* that.
*/
metric = 0;
/* Cobalt stores the pressures, not the depth */
if (data[6])
cur_dive->dc.maxdepth.mm = atoi(data[6]);
if (data[7])
cur_dive->dc.duration.seconds = atoi(data[7]);
if (data[8])
cur_dive->dc.surface_pressure.mbar = atoi(data[8]);
/*
* TODO: the deviceid hash should be calculated here.
*/
settings_start();
dc_settings_start();
if (data[9]) {
utf8_string(data[9], &cur_settings.dc.serial_nr);
cur_settings.dc.deviceid = atoi(data[9]);
cur_settings.dc.model = strdup("Cobalt import");
}
dc_settings_end();
settings_end();
if (data[9]) {
cur_dive->dc.deviceid = atoi(data[9]);
cur_dive->dc.model = strdup("Cobalt import");
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_cylinders, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_cylinders failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_buddy_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_buddies, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_buddies failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_visibility_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_visibility, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_visibility failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_location_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_location, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_location failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_site_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_location, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_location (site) failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_profile_sample, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_profile_sample failed.\n");
return 1;
}
dive_end();
return SQLITE_OK;
}
int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *buffer, int size,
struct dive_table *table)
{
(void) buffer;
(void) size;
int retval;
char *err = NULL;
target_table = table;
char get_dives[] = "select Id,strftime('%s',DiveStartTime),LocationId,'buddy','notes',Units,(MaxDepthPressure*10000/SurfacePressure)-10000,DiveMinutes,SurfacePressure,SerialNumber,'model' from Dive where IsViewDeleted = 0";
retval = sqlite3_exec(handle, get_dives, &cobalt_dive, handle, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "Database query failed '%s'.\n", url);
return 1;
}
return 0;
}
extern int divinglog_cylinder(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
short dbl = 1;
//char get_cylinder_template[] = "select TankID,TankSize,PresS,PresE,PresW,O2,He,DblTank from Tank where LogID = %d";
/*
* Divinglog might have more cylinders than what we support. So
* better to ignore those.
*/
if (cur_cylinder_index >= MAX_CYLINDERS)
return 0;
if (data[7] && atoi(data[7]) > 0)
dbl = 2;
cylinder_start();
/*
* Assuming that we have to double the cylinder size, if double
* is set
*/
if (data[1] && atoi(data[1]) > 0)
cur_dive->cylinder[cur_cylinder_index].type.size.mliter = atol(data[1]) * 1000 * dbl;
if (data[2] && atoi(data[2]) > 0)
cur_dive->cylinder[cur_cylinder_index].start.mbar = atol(data[2]) * 1000;
if (data[3] && atoi(data[3]) > 0)
cur_dive->cylinder[cur_cylinder_index].end.mbar = atol(data[3]) * 1000;
if (data[4] && atoi(data[4]) > 0)
cur_dive->cylinder[cur_cylinder_index].type.workingpressure.mbar = atol(data[4]) * 1000;
if (data[5] && atoi(data[5]) > 0)
cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = atol(data[5]) * 10;
if (data[6] && atoi(data[6]) > 0)
cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = atol(data[6]) * 10;
cylinder_end();
return 0;
}
extern int divinglog_profile(void *handle, int columns, char **data, char **column)
{
(void) handle;
(void) columns;
(void) column;
int sinterval = 0;
unsigned long time;
int len1, len2, len3, len4, len5;
char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
short oldcyl = -1;
/* We do not have samples */
if (!data[1])
return 0;
if (data[0])
sinterval = atoi(data[0]);
/*
* Profile
*
* DDDDDCRASWEE
* D: Depth (in meter with two decimals)
* C: Deco (1 = yes, 0 = no)
* R: RBT (Remaining Bottom Time warning)
* A: Ascent warning
* S: Decostop ignored
* W: Work warning
* E: Extra info (different for every computer)
*
* Example: 004500010000
* 4.5 m, no deco, no RBT warning, ascanding too fast, no decostop ignored, no work, no extra info
*
*
* Profile2
*
* TTTFFFFIRRR
*
* T: Temperature (in °C with one decimal)
* F: Tank pressure 1 (in bar with one decimal)
* I: Tank ID (0, 1, 2 ... 9)
* R: RBT (in min)
*
* Example: 25518051099
* 25.5 °C, 180.5 bar, Tank 1, 99 min RBT
*
*/
ptr1 = data[1];
ptr2 = data[2];
ptr3 = data[3];
ptr4 = data[4];
ptr5 = data[5];
len1 = strlen(ptr1);
len2 = ptr2 ? strlen(ptr2) : 0;
len3 = ptr3 ? strlen(ptr3) : 0;
len4 = ptr4 ? strlen(ptr4) : 0;
len5 = ptr5 ? strlen(ptr5) : 0;
time = 0;
while (len1 >= 12) {
sample_start();
cur_sample->time.seconds = time;
cur_sample->in_deco = ptr1[5] - '0' ? true : false;
cur_sample->depth.mm = atoi_n(ptr1, 5) * 10;
if (len2 >= 11) {
int temp = atoi_n(ptr2, 3);
int pressure = atoi_n(ptr2+3, 4);
int tank = atoi_n(ptr2+7, 1);
int rbt = atoi_n(ptr2+8, 3) * 60;
cur_sample->temperature.mkelvin = C_to_mkelvin(temp / 10.0f);
cur_sample->pressure[0].mbar = pressure * 100;
cur_sample->rbt.seconds = rbt;
if (oldcyl != tank) {
struct gasmix *mix = &cur_dive->cylinder[tank].gasmix;
int o2 = get_o2(mix);
int he = get_he(mix);
event_start();
cur_event.time.seconds = time;
strcpy(cur_event.name, "gaschange");
o2 = (o2 + 5) / 10;
he = (he + 5) / 10;
cur_event.value = o2 + (he << 16);
event_end();
oldcyl = tank;
}
ptr2 += 11; len2 -= 11;
}
if (len3 >= 14) {
cur_sample->heartbeat = atoi_n(ptr3+8, 3);
ptr3 += 14; len3 -= 14;
}
if (len4 >= 9) {
/*
* Following value is NDL when not in deco, and
* either 0 or TTS when in deco.
*/
int val = atoi_n(ptr4, 3);
if (cur_sample->in_deco) {
cur_sample->ndl.seconds = 0;
if (val)
cur_sample->tts.seconds = val * 60;
} else {
cur_sample->ndl.seconds = val * 60;
}
cur_sample->stoptime.seconds = atoi_n(ptr4+3, 3) * 60;
cur_sample->stopdepth.mm = atoi_n(ptr4+6, 3) * 1000;
ptr4 += 9; len4 -= 9;
}
/*
* AAABBBCCCOOOONNNNSS
*
* A = ppO2 cell 1 (measured)
* B = ppO2 cell 2 (measured)
* C = ppO2 cell 3 (measured)
* O = OTU
* N = CNS
* S = Setpoint
*
* Example: 1121131141548026411
* 1.12 bar, 1.13 bar, 1.14 bar, OTU = 154.8, CNS = 26.4, Setpoint = 1.1
*/
if (len5 >= 19) {
int ppo2_1 = atoi_n(ptr5 + 0, 3);
int ppo2_2 = atoi_n(ptr5 + 3, 3);
int ppo2_3 = atoi_n(ptr5 + 6, 3);
int otu = atoi_n(ptr5 + 9, 4);
(void) otu; // we seem to not store this? Do we understand its format?
int cns = atoi_n(ptr5 + 13, 4);
int setpoint = atoi_n(ptr5 + 17, 2);
if (ppo2_1 > 0)
cur_sample->o2sensor[0].mbar = ppo2_1 * 100;
if (ppo2_2 > 0)
cur_sample->o2sensor[1].mbar = ppo2_2 * 100;
if (ppo2_3 > 0)
cur_sample->o2sensor[2].mbar = ppo2_3 * 100;
if (cns > 0)
cur_sample->cns = lrintf(cns / 10.0f);
if (setpoint > 0)
cur_sample->setpoint.mbar = setpoint * 100;
ptr5 += 19; len5 -= 19;
}
/*
* Count the number of o2 sensors
*/
if (!cur_dive->dc.no_o2sensors && (cur_sample->o2sensor[0].mbar || cur_sample->o2sensor[0].mbar || cur_sample->o2sensor[0].mbar)) {
cur_dive->dc.no_o2sensors = cur_sample->o2sensor[0].mbar ? 1 : 0 +
cur_sample->o2sensor[1].mbar ? 1 : 0 +
cur_sample->o2sensor[2].mbar ? 1 : 0;
}
sample_end();
/* Remaining bottom time warning */
if (ptr1[6] - '0') {
event_start();
cur_event.time.seconds = time;
strcpy(cur_event.name, "rbt");
event_end();
}
/* Ascent warning */
if (ptr1[7] - '0') {
event_start();
cur_event.time.seconds = time;
strcpy(cur_event.name, "ascent");
event_end();
}
/* Deco stop ignored */
if (ptr1[8] - '0') {
event_start();
cur_event.time.seconds = time;
strcpy(cur_event.name, "violation");
event_end();
}
/* Workload warning */
if (ptr1[9] - '0') {
event_start();
cur_event.time.seconds = time;
strcpy(cur_event.name, "workload");
event_end();
}
ptr1 += 12; len1 -= 12;
time += sinterval;
}
return 0;
}
extern int divinglog_dive(void *param, int columns, char **data, char **column)
{
(void) columns;
(void) column;
int retval = 0;
sqlite3 *handle = (sqlite3 *)param;
char *err = NULL;
char get_profile_template[] = "select ProfileInt,Profile,Profile2,Profile3,Profile4,Profile5 from Logbook where ID = %d";
char get_cylinder0_template[] = "select 0,TankSize,PresS,PresE,PresW,O2,He,DblTank from Logbook where ID = %d";
char get_cylinder_template[] = "select TankID,TankSize,PresS,PresE,PresW,O2,He,DblTank from Tank where LogID = %d order by TankID";
char get_buffer[1024];
dive_start();
diveid = atoi(data[13]);
cur_dive->number = atoi(data[0]);
cur_dive->when = (time_t)(atol(data[1]));
if (data[2])
cur_dive->dive_site_uuid = find_or_create_dive_site_with_name(data[2], cur_dive->when);
if (data[3])
utf8_string(data[3], &cur_dive->buddy);
if (data[4])
utf8_string(data[4], &cur_dive->notes);
if (data[5])
cur_dive->dc.maxdepth.mm = lrint(strtod_flags(data[5], NULL, 0) * 1000);
if (data[6])
cur_dive->dc.duration.seconds = atoi(data[6]) * 60;
if (data[7])
utf8_string(data[7], &cur_dive->divemaster);
if (data[8])
cur_dive->airtemp.mkelvin = C_to_mkelvin(atol(data[8]));
if (data[9])
cur_dive->watertemp.mkelvin = C_to_mkelvin(atol(data[9]));
if (data[10]) {
cur_dive->weightsystem[0].weight.grams = atol(data[10]) * 1000;
cur_dive->weightsystem[0].description = strdup(translate("gettextFromC", "unknown"));
}
if (data[11])
cur_dive->suit = strdup(data[11]);
/* Divinglog has following visibility options: good, medium, bad */
if (data[14]) {
switch(data[14][0]) {
case '0':
break;
case '1':
cur_dive->visibility = 5;
break;
case '2':
cur_dive->visibility = 3;
break;
case '3':
cur_dive->visibility = 1;
break;
default:
break;
}
}
settings_start();
dc_settings_start();
if (data[12]) {
cur_dive->dc.model = strdup(data[12]);
} else {
cur_settings.dc.model = strdup("Divinglog import");
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder0_template, diveid);
retval = sqlite3_exec(handle, get_buffer, &divinglog_cylinder, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query divinglog_cylinder0 failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder_template, diveid);
retval = sqlite3_exec(handle, get_buffer, &divinglog_cylinder, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query divinglog_cylinder failed.\n");
return 1;
}
if (data[15]) {
switch (data[15][0]) {
/* OC */
case '0':
break;
case '1':
cur_dive->dc.divemode = PSCR;
break;
case '2':
cur_dive->dc.divemode = CCR;
break;
}
}
dc_settings_end();
settings_end();
if (data[12]) {
cur_dive->dc.model = strdup(data[12]);
} else {
cur_dive->dc.model = strdup("Divinglog import");
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template, diveid);
retval = sqlite3_exec(handle, get_buffer, &divinglog_profile, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query divinglog_profile failed.\n");
return 1;
}
dive_end();
return SQLITE_OK;
}
int parse_divinglog_buffer(sqlite3 *handle, const char *url, const char *buffer, int size,
struct dive_table *table)
{
(void) buffer;
(void) size;
int retval;
char *err = NULL;
target_table = table;
char get_dives[] = "select Number,strftime('%s',Divedate || ' ' || ifnull(Entrytime,'00:00')),Country || ' - ' || City || ' - ' || Place,Buddy,Comments,Depth,Divetime,Divemaster,Airtemp,Watertemp,Weight,Divesuit,Computer,ID,Visibility,SupplyType from Logbook where UUID not in (select UUID from DeletedRecords)";
retval = sqlite3_exec(handle, get_dives, &divinglog_dive, handle, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "Database query failed '%s'.\n", url);
return 1;
}
return 0;
}
|