-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathgarmin_parser.c
More file actions
1788 lines (1575 loc) · 57 KB
/
garmin_parser.c
File metadata and controls
1788 lines (1575 loc) · 57 KB
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
/*
* Garmin Descent Mk1 parsing
*
* Copyright (C) 2018 Linus Torvalds
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "garmin.h"
#include "context-private.h"
#include "parser-private.h"
#include "array.h"
#include "field-cache.h"
#define MAXFIELDS 160
struct msg_desc;
// Local types
struct type_desc {
const char *msg_name;
const struct msg_desc *msg_desc;
unsigned char nrfields, devfields;
unsigned char fields[MAXFIELDS][3];
};
// Positions are signed 32-bit values, turning
// into 180 * val // 2**31 degrees.
struct pos {
int lat, lon;
};
#define MAX_SENSORS 6
struct garmin_sensor {
unsigned int sensor_id;
const char *sensor_name;
unsigned char sensor_enabled, sensor_units, sensor_used_for_gas_rate;
unsigned int sensor_rated_pressure, sensor_reserve_pressure, sensor_volume;
};
#define MAXTYPE 16
#define MAXGASES 16
#define MAXSTRINGS 32
// Some record data needs to be bunched up
// and sent together.
struct record_data {
unsigned int pending;
unsigned int time;
unsigned int timestamp;
// RECORD_DECO
int stop_time;
double ceiling;
// RECORD_GASMIX
int index, gas_status;
dc_gasmix_t gasmix;
// RECORD_EVENT
unsigned char event_type, event_nr, event_group;
unsigned int event_data, event_unknown;
// RECORD_DEVICE_INFO
unsigned int device_index, firmware, serial, product;
// RECORD_DECO_MODEL
unsigned char model, gf_low, gf_high;
// RECORD_SENSOR_PROFILE has no data, fills in dive.sensor[nr_sensor]
// RECORD_TANK_UPDATE
unsigned int sensor, pressure;
// RECORD_SETPOINT_CHANGE
unsigned int setpoint_actual_cbar;
};
#define RECORD_GASMIX 1
#define RECORD_DECO 2
#define RECORD_EVENT 4
#define RECORD_DEVICE_INFO 8
#define RECORD_DECO_MODEL 16
#define RECORD_SENSOR_PROFILE 32
#define RECORD_TANK_UPDATE 64
#define RECORD_SETPOINT_CHANGE 128
typedef struct garmin_parser_t {
dc_parser_t base;
dc_sample_callback_t callback;
void *userdata;
// Multi-value record data
struct record_data record_data;
struct type_desc type_desc[MAXTYPE];
// Field cache
struct {
unsigned int sub_sport;
unsigned int serial;
unsigned int product;
unsigned int firmware;
unsigned int protocol;
unsigned int profile;
unsigned int time;
int utc_offset, time_offset;
unsigned int nr_sensor;
struct garmin_sensor sensor[MAX_SENSORS];
unsigned int setpoint_low_cbar, setpoint_high_cbar;
unsigned int setpoint_low_switch_depth_mm, setpoint_high_switch_depth_mm;
unsigned int setpoint_low_switch_mode, setpoint_high_switch_mode;
} dive;
// I count nine (!) different GPS fields Hmm.
// Reporting all of them just to try to figure
// out what is what.
struct {
struct {
struct pos entry, exit;
struct pos NE, SW; // NE, SW corner
} SESSION;
struct {
struct pos entry, exit;
struct pos some, other;
} LAP;
struct pos RECORD;
} gps;
struct dc_field_cache cache;
unsigned char is_big_endian; // instead of bool
} garmin_parser_t;
typedef int (*garmin_data_cb_t)(unsigned char type, const unsigned char *data, int len, void *user);
static inline struct garmin_sensor *current_sensor(garmin_parser_t *garmin)
{
return garmin->dive.sensor + garmin->dive.nr_sensor;
}
static int find_tank_index(garmin_parser_t *garmin, unsigned int sensor_id)
{
for (int i = 0; i < garmin->dive.nr_sensor; i++) {
if (garmin->dive.sensor[i].sensor_id == sensor_id)
return i;
}
return 0;
}
/*
* Decode the event. Numbers from Wojtek's fit2subs python script
*/
static void garmin_event(struct garmin_parser_t *garmin,
unsigned char event, unsigned char type, unsigned char group,
unsigned int data, unsigned int unknown)
{
static const struct {
// 1 - state, 2 - notify, 3 - warning, 4 - alarm
int severity;
const char *name;
} event_desc[] = {
[0] = { 2, "Deco required" },
[1] = { 2, "Gas Switch prompted" },
[2] = { 1, "Surface" },
[3] = { 2, "Approaching NDL" },
[4] = { 3, "ppO2 warning" },
[5] = { 4, "ppO2 critical high" },
[6] = { 4, "ppO2 critical low" },
[7] = { 2, "Time alert" },
[8] = { 2, "Depth alert" },
[9] = { 3, "Deco ceiling broken" },
[10] = { 1, "Deco completed" },
[11] = { 3, "Safety stop ceiling broken" },
[12] = { 1, "Safety stop completed" },
[13] = { 3, "CNS warning" },
[14] = { 4, "CNS critical" },
[15] = { 3, "OTU warning" },
[16] = { 4, "OTU critical" },
[17] = { 3, "Ascent speed critical" },
[18] = { 1, "Alert dismissed" },
[19] = { 1, "Alert timed out" },
[20] = { 3, "Battry Low" },
[21] = { 3, "Battry Critical" },
[22] = { 1, "Safety stop begin" },
[23] = { 1, "Approaching deco stop" },
[24] = { 1, "Automatic switch to low setpoint" },
[25] = { 1, "Automatic switch to high setpoint" },
[26] = { 2, "Manual switch to low setpoint" },
[27] = { 2, "Manual switch to high setpoint" },
[32] = { 1, "Tank battery low" }, // No way to know which tank
};
dc_sample_value_t sample = {0};
switch (event) {
case 38:
break;
case 48:
break;
case 56:
if (data >= C_ARRAY_SIZE(event_desc))
return;
sample.event.type = SAMPLE_EVENT_STRING;
sample.event.name = event_desc[data].name;
sample.event.flags = event_desc[data].severity << SAMPLE_FLAGS_SEVERITY_SHIFT;
if (data == 24 || data == 25 || data == 26 || data == 27) {
// Update the actual setpoint used during the dive and report it
garmin->record_data.setpoint_actual_cbar = (data == 24 || data == 26) ? garmin->dive.setpoint_low_cbar : garmin->dive.setpoint_high_cbar;
garmin->record_data.pending |= RECORD_SETPOINT_CHANGE;
}
if (!sample.event.name)
return;
garmin->callback(DC_SAMPLE_EVENT, &sample, garmin->userdata);
return;
case 57:
sample.gasmix = data;
garmin->callback(DC_SAMPLE_GASMIX, &sample, garmin->userdata);
return;
}
}
/*
* Some data isn't just something we can save off directly: it's a record with
* multiple fields where one field describes another.
*
* The solution is to just batch it up in the "garmin->record_data", and then
* this function gets called at the end of a record.
*/
static void flush_pending_record(struct garmin_parser_t *garmin)
{
struct record_data *record = &garmin->record_data;
unsigned int pending = record->pending;
record->pending = 0;
if (!garmin->callback) {
if (pending & RECORD_GASMIX) {
// 0 - disabled, 1 - enabled, 2 - backup
int enabled = record->gas_status > 0;
int index = record->index;
if (enabled && index < MAXGASES) {
DC_ASSIGN_IDX(garmin->cache, GASMIX, index, record->gasmix);
DC_ASSIGN_FIELD(garmin->cache, GASMIX_COUNT, index + 1);
}
}
if (pending & RECORD_DEVICE_INFO && record->device_index == 0) {
garmin->dive.firmware = record->firmware;
garmin->dive.serial = record->serial;
garmin->dive.product = record->product;
}
if (pending & RECORD_DECO_MODEL)
dc_field_add_string_fmt(&garmin->cache, "Deco model", "Buhlmann ZHL-16C %u/%u", record->gf_low, record->gf_high);
// End of sensor record just increments nr_sensor,
// so that the next sensor record will start
// filling in the next one.
//
// NOTE! This only happens for tank pods, other
// sensors will just overwrite each other.
//
// Also note that the last sensor is just for
// scratch use, so that the sensor record can
// always fill in dive.sensor[nr_sensor] with
// no checking.
if (pending & RECORD_SENSOR_PROFILE) {
if (garmin->dive.nr_sensor < MAX_SENSORS-1)
garmin->dive.nr_sensor++;
}
return;
}
if (pending & RECORD_DECO) {
dc_sample_value_t sample = {0};
sample.deco.type = DC_DECO_DECOSTOP;
sample.deco.time = record->stop_time;
sample.deco.depth = record->ceiling;
sample.deco.tts = 0;
garmin->callback(DC_SAMPLE_DECO, &sample, garmin->userdata);
}
if (pending & RECORD_EVENT) {
garmin_event(garmin, record->event_nr, record->event_type,
record->event_group, record->event_data, record->event_unknown);
}
if (pending & RECORD_TANK_UPDATE) {
dc_sample_value_t sample = {0};
sample.pressure.tank = find_tank_index(garmin, record->sensor);
sample.pressure.value = record->pressure / 100.0;
garmin->callback(DC_SAMPLE_PRESSURE, &sample, garmin->userdata);
}
if (pending & RECORD_SETPOINT_CHANGE) {
dc_sample_value_t sample = {0};
sample.setpoint = record->setpoint_actual_cbar / 100.0;
garmin->callback(DC_SAMPLE_SETPOINT, &sample, garmin->userdata);
}
}
static dc_status_t garmin_parser_set_data (garmin_parser_t *garmin, const unsigned char *data, unsigned int size);
static dc_status_t garmin_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
static dc_status_t garmin_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
static dc_status_t garmin_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
static const dc_parser_vtable_t garmin_parser_vtable = {
sizeof(garmin_parser_t),
DC_FAMILY_GARMIN,
NULL, /* set_clock */
NULL, /* set_atmospheric */
NULL, /* set_density */
garmin_parser_get_datetime, /* datetime */
garmin_parser_get_field, /* fields */
garmin_parser_samples_foreach, /* samples_foreach */
NULL /* destroy */
};
dc_status_t
garmin_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size)
{
garmin_parser_t *parser = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
// Allocate memory.
parser = (garmin_parser_t *) dc_parser_allocate (context, &garmin_parser_vtable, data, size);
if (parser == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
garmin_parser_set_data(parser, data, size);
*out = (dc_parser_t *) parser;
return DC_STATUS_SUCCESS;
}
/*
* We really shouldn't use array_uint_be/le, since they
* can't deal with 64-bit types.
*
* But we've not actually seen any yet, so..
*/
static inline unsigned long long garmin_value(struct garmin_parser_t *g, const unsigned char *p, unsigned int type_size)
{
if (g->is_big_endian)
return array_uint_be(p, type_size);
else
return array_uint_le(p, type_size);
}
#define FMTSIZE 64
#define DECLARE_FIT_TYPE(name, ctype, inval, fmt) \
typedef ctype name; \
static const name name##_INVAL = inval; \
static name name##_VALUE(garmin_parser_t *g, const void *p) \
{ return (name) garmin_value(g, p, sizeof(name)); } \
static void name##_FORMAT(name val, char *buf) \
{ snprintf(buf, FMTSIZE, fmt, val); }
DECLARE_FIT_TYPE(ENUM, unsigned char, 0xff, "%u");
DECLARE_FIT_TYPE(UINT8, unsigned char, 0xff, "%u");
DECLARE_FIT_TYPE(UINT16, unsigned short, 0xffff, "%u");
DECLARE_FIT_TYPE(UINT32, unsigned int, 0xffffffff, "%u");
DECLARE_FIT_TYPE(UINT64, unsigned long long, 0xffffffffffffffffull, "%llu");
DECLARE_FIT_TYPE(UINT8Z, unsigned char, 0, "%u");
DECLARE_FIT_TYPE(UINT16Z, unsigned short, 0, "%u");
DECLARE_FIT_TYPE(UINT32Z, unsigned int, 0, "%u");
DECLARE_FIT_TYPE(SINT8, signed char, 0x7f, "%d");
DECLARE_FIT_TYPE(SINT16, signed short, 0x7fff, "%d");
DECLARE_FIT_TYPE(SINT32, signed int, 0x7fffffff, "%d");
DECLARE_FIT_TYPE(SINT64, signed long long, 0x7fffffffffffffffll, "%lld");
DECLARE_FIT_TYPE(FLOAT, unsigned int, 0xffffffff, "%u");
DECLARE_FIT_TYPE(DOUBLE, unsigned long long, 0xffffffffffffffffll, "%llu");
DECLARE_FIT_TYPE(STRING, char *, NULL, "\"%s\"");
// Override string value function - it's the pointer itself
#define STRING_VALUE(g, p) ((char *)(p))
static const struct {
const char *type_name;
int type_size;
unsigned long long type_inval;
} base_type_info[17] = {
{ "ENUM", 1, 0xff },
{ "SINT8", 1, 0x7f },
{ "UINT8", 1, 0xff },
{ "SINT16", 2, 0x7fff },
{ "UINT16", 2, 0xffff },
{ "SINT32", 4, 0x7fffffff },
{ "UINT32", 4, 0xffffffff },
{ "STRING", 1, 0 },
{ "FLOAT", 4, 0xffffffff },
{ "DOUBLE", 8, 0xfffffffffffffffful },
{ "UINT8Z", 1, 0x00 },
{ "UINT16Z", 2, 0x0000 },
{ "UINT32Z", 4, 0x00000000 },
{ "BYTE", 1, 0xff },
{ "SINT64", 8, 0x7fffffffffffffff },
{ "UINT64", 8, 0xffffffffffffffff },
{ "UINT64Z", 8, 0x0000000000000000 },
};
/*
* Garmin FIT events are described by tuples of "global mesg ID" and
* a "field number". There's lots of them, because you have events
* for pretty much anything ("cycling gear change") etc.
*
* There's a SDK that generates tables for you, but it looks nasty.
*
* So instead, we try to make sense of it manually.
*/
struct field_desc {
const char *name;
void (*parse)(struct garmin_parser_t *, unsigned char base_type, const unsigned char *data);
};
#define DECLARE_FIELD(msg, name, type) __DECLARE_FIELD(msg##_##name, type)
#define __DECLARE_FIELD(name, type) \
static void parse_##name(struct garmin_parser_t *, const type); \
static void parse_##name##_##type(struct garmin_parser_t *g, unsigned char base_type, const unsigned char *p) \
{ \
char fmtbuf[FMTSIZE]; \
if (strcmp(#type, base_type_info[base_type].type_name)) \
WARNING(g->base.context, "%s: %s should be %s\n", #name, #type, base_type_info[base_type].type_name); \
type val = type##_VALUE(g, p); \
if (val == type##_INVAL) return; \
type##_FORMAT(val, fmtbuf); \
DEBUG(g->base.context, "%s (%s): %s", #name, #type, fmtbuf); \
parse_##name(g, val); \
} \
static const struct field_desc name##_field_##type = { #name, parse_##name##_##type }; \
static void parse_##name(struct garmin_parser_t *garmin, type data)
// All msg formats can have a timestamp
// Garmin timestamps are in seconds since 00:00 Dec 31 1989 UTC
// Convert to "standard epoch time" by adding 631065600.
DECLARE_FIELD(ANY, timestamp, UINT32)
{
garmin->record_data.timestamp = data;
if (garmin->callback) {
// Turn the timestamp relative to the beginning of the dive
if (data < garmin->dive.time) {
DEBUG(garmin->base.context, "Timestamp before dive start: %d (dive start: %d)", data, garmin->dive.time);
return;
}
data -= garmin->dive.time - 1;
// Did we already do this?
if (data == garmin->record_data.time)
return;
garmin->record_data.time = data;
// Now we're ready to actually update the sample times
dc_sample_value_t sample = {0};
sample.time = data * 1000;
garmin->callback(DC_SAMPLE_TIME, &sample, garmin->userdata);
}
}
DECLARE_FIELD(ANY, message_index, UINT16) { garmin->record_data.index = data; }
DECLARE_FIELD(ANY, part_index, UINT32) { garmin->record_data.index = data; }
// FILE msg
DECLARE_FIELD(FILE, file_type, ENUM) { }
DECLARE_FIELD(FILE, manufacturer, UINT16) { }
DECLARE_FIELD(FILE, product, UINT16) { }
DECLARE_FIELD(FILE, serial, UINT32Z) { }
DECLARE_FIELD(FILE, creation_time, UINT32) { }
DECLARE_FIELD(FILE, number, UINT16) { }
DECLARE_FIELD(FILE, other_time, UINT32) { }
DECLARE_FIELD(FILE, product_name, STRING) { }
// SESSION msg
DECLARE_FIELD(SESSION, start_time, UINT32) { garmin->dive.time = data; }
DECLARE_FIELD(SESSION, start_pos_lat, SINT32) { garmin->gps.SESSION.entry.lat = data; }
DECLARE_FIELD(SESSION, start_pos_long, SINT32) { garmin->gps.SESSION.entry.lon = data; }
DECLARE_FIELD(SESSION, nec_pos_lat, SINT32) { garmin->gps.SESSION.NE.lat = data; }
DECLARE_FIELD(SESSION, nec_pos_long, SINT32) { garmin->gps.SESSION.NE.lon = data; }
DECLARE_FIELD(SESSION, swc_pos_lat, SINT32) { garmin->gps.SESSION.SW.lat = data; }
DECLARE_FIELD(SESSION, swc_pos_long, SINT32) { garmin->gps.SESSION.SW.lon = data; }
DECLARE_FIELD(SESSION, exit_pos_lat, SINT32) { garmin->gps.SESSION.exit.lat = data; }
DECLARE_FIELD(SESSION, exit_pos_long, SINT32) { garmin->gps.SESSION.exit.lon = data; }
// LAP msg
DECLARE_FIELD(LAP, start_time, UINT32) { }
DECLARE_FIELD(LAP, start_pos_lat, SINT32) { garmin->gps.LAP.entry.lat = data; }
DECLARE_FIELD(LAP, start_pos_long, SINT32) { garmin->gps.LAP.entry.lon = data; }
DECLARE_FIELD(LAP, end_pos_lat, SINT32) { garmin->gps.LAP.exit.lat = data; }
DECLARE_FIELD(LAP, end_pos_long, SINT32) { garmin->gps.LAP.exit.lon = data; }
DECLARE_FIELD(LAP, some_pos_lat, SINT32) { garmin->gps.LAP.some.lat = data; }
DECLARE_FIELD(LAP, some_pos_long, SINT32) { garmin->gps.LAP.some.lon = data; }
DECLARE_FIELD(LAP, other_pos_lat, SINT32) { garmin->gps.LAP.other.lat = data; }
DECLARE_FIELD(LAP, other_pos_long, SINT32) { garmin->gps.LAP.other.lon = data; }
// RECORD msg
DECLARE_FIELD(RECORD, position_lat, SINT32) { garmin->gps.RECORD.lat = data; }
DECLARE_FIELD(RECORD, position_long, SINT32) { garmin->gps.RECORD.lon = data; }
DECLARE_FIELD(RECORD, altitude, UINT16) { } // 5 *m + 500 ?
DECLARE_FIELD(RECORD, heart_rate, UINT8) // bpm
{
if (garmin->callback) {
dc_sample_value_t sample = {0};
sample.heartbeat = data;
garmin->callback(DC_SAMPLE_HEARTBEAT, &sample, garmin->userdata);
}
}
DECLARE_FIELD(RECORD, cadence, UINT8) { } // cadence
DECLARE_FIELD(RECORD, fract_cadence, UINT8) { } // fractional cadence
DECLARE_FIELD(RECORD, distance, UINT32) { } // Distance in 100 * m? WTF?
DECLARE_FIELD(RECORD, speed, UINT16) { } // Speed (m/s?)
DECLARE_FIELD(RECORD, temperature, SINT8) // degrees C
{
if (garmin->callback) {
dc_sample_value_t sample = {0};
sample.temperature = data;
garmin->callback(DC_SAMPLE_TEMPERATURE, &sample, garmin->userdata);
}
}
DECLARE_FIELD(RECORD, abs_pressure, UINT32) {} // Pascal
DECLARE_FIELD(RECORD, depth, UINT32) // mm
{
if (garmin->callback) {
dc_sample_value_t sample = {0};
sample.depth = data / 1000.0;
garmin->callback(DC_SAMPLE_DEPTH, &sample, garmin->userdata);
}
}
DECLARE_FIELD(RECORD, next_stop_depth, UINT32) // mm
{
garmin->record_data.pending |= RECORD_DECO;
garmin->record_data.ceiling = data / 1000.0;
}
DECLARE_FIELD(RECORD, next_stop_time, UINT32) // seconds
{
garmin->record_data.pending |= RECORD_DECO;
garmin->record_data.stop_time = data;
}
DECLARE_FIELD(RECORD, tts, UINT32)
{
if (garmin->callback) {
dc_sample_value_t sample = {0};
sample.time = data;
garmin->callback(DC_SAMPLE_TTS, &sample, garmin->userdata);
}
}
DECLARE_FIELD(RECORD, ndl, UINT32) // s
{
if (garmin->callback) {
dc_sample_value_t sample = {0};
sample.deco.type = DC_DECO_NDL;
sample.deco.time = data;
sample.deco.tts = 0;
garmin->callback(DC_SAMPLE_DECO, &sample, garmin->userdata);
}
}
DECLARE_FIELD(RECORD, cns_load, UINT8)
{
if (garmin->callback) {
dc_sample_value_t sample = {0};
sample.cns = data / 100.0;
garmin->callback(DC_SAMPLE_CNS, &sample, garmin->userdata);
}
}
DECLARE_FIELD(RECORD, n2_load, UINT16) { } // percent
DECLARE_FIELD(RECORD, air_time_remaining, UINT32) { } // seconds
DECLARE_FIELD(RECORD, pressure_sac, UINT16) { } // 100 * bar/min/pressure
DECLARE_FIELD(RECORD, volume_sac, UINT16) { } // 100 * l/min/pressure
DECLARE_FIELD(RECORD, rmv, UINT16) { } // 100 * l/min
DECLARE_FIELD(RECORD, ascent_rate, SINT32) { } // mm/s (negative is down)
// DEVICE_SETTINGS
DECLARE_FIELD(DEVICE_SETTINGS, utc_offset, UINT32)
{
garmin->dive.utc_offset = (SINT32) data; // wrong type in FIT
}
DECLARE_FIELD(DEVICE_SETTINGS, time_offset, UINT32)
{
/*
* Crazy FIT files have a zero time offset in DEVICE_SETTINGS,
* but then have a local_timestamp in ACTIVITY and/or in the
* TIME_CORRELATION messages.
*
* So I have no idea what this field means then.
*/
if (!data)
return;
garmin->dive.time_offset = (SINT32) data; // wrong type in FIT
}
// DEVICE_INFO
// collect the data and then use the record if it is for device_index 0
DECLARE_FIELD(DEVICE_INFO, device_index, UINT8)
{
garmin->record_data.device_index = data;
garmin->record_data.pending |= RECORD_DEVICE_INFO;
}
DECLARE_FIELD(DEVICE_INFO, product, UINT16)
{
garmin->record_data.product = data;
garmin->record_data.pending |= RECORD_DEVICE_INFO;
}
DECLARE_FIELD(DEVICE_INFO, serial_nr, UINT32Z)
{
garmin->record_data.serial = data;
garmin->record_data.pending |= RECORD_DEVICE_INFO;
}
DECLARE_FIELD(DEVICE_INFO, firmware, UINT16)
{
garmin->record_data.firmware = data;
garmin->record_data.pending |= RECORD_DEVICE_INFO;
}
// ACTIVITY
DECLARE_FIELD(ACTIVITY, total_timer_time, UINT32) { }
DECLARE_FIELD(ACTIVITY, num_sessions, UINT16) { }
DECLARE_FIELD(ACTIVITY, type, ENUM) { }
DECLARE_FIELD(ACTIVITY, event, ENUM) { }
DECLARE_FIELD(ACTIVITY, event_type, ENUM) { }
DECLARE_FIELD(ACTIVITY, local_timestamp, UINT32)
{
int time_offset = data - garmin->record_data.timestamp;
garmin->dive.time_offset = time_offset;
}
DECLARE_FIELD(ACTIVITY, event_group, UINT8) { }
// SPORT
DECLARE_FIELD(SPORT, sub_sport, ENUM) {
garmin->dive.sub_sport = (ENUM) data;
dc_divemode_t val;
switch (data) {
case 55: val = DC_DIVEMODE_GAUGE;
break;
case 56:
case 57: val = DC_DIVEMODE_FREEDIVE;
break;
case 63:
val = DC_DIVEMODE_CCR;
break;
default: val = DC_DIVEMODE_OC;
}
DC_ASSIGN_FIELD(garmin->cache, DIVEMODE, val);
}
/*
* What is the difference between 'system_timestamp' and the
* actual timestamp of the message itself? Who designs these
* crazy things? What is the meaning of it all? These are the
* kinds of unanswerable questions that keep me up at night.
*/
DECLARE_FIELD(TIMESTAMP_CORRELATION, system_timestamp, UINT32) { }
DECLARE_FIELD(TIMESTAMP_CORRELATION, local_timestamp, UINT32)
{
int time_offset = data - garmin->record_data.timestamp;
garmin->dive.time_offset = time_offset;
}
// DIVE_GAS - uses msg index
DECLARE_FIELD(DIVE_GAS, helium, UINT8)
{
garmin->record_data.gasmix.helium = data / 100.0;
garmin->record_data.pending |= RECORD_GASMIX;
}
DECLARE_FIELD(DIVE_GAS, oxygen, UINT8)
{
garmin->record_data.gasmix.oxygen = data / 100.0;
garmin->record_data.pending |= RECORD_GASMIX;
}
DECLARE_FIELD(DIVE_GAS, status, ENUM)
{
// 0 - disabled, 1 - enabled, 2 - backup
garmin->record_data.gas_status = data;
}
DECLARE_FIELD(DIVE_GAS, type, ENUM)
{
// 0 - open circuit, 1 - CCR diluent
if (data == 1)
garmin->record_data.gasmix.usage = DC_USAGE_DILUENT;
else
garmin->record_data.gasmix.usage = DC_USAGE_OPEN_CIRCUIT;
garmin->record_data.pending |= RECORD_GASMIX;
}
// DIVE_SUMMARY
DECLARE_FIELD(DIVE_SUMMARY, avg_depth, UINT32) { DC_ASSIGN_FIELD(garmin->cache, AVGDEPTH, data / 1000.0); }
DECLARE_FIELD(DIVE_SUMMARY, max_depth, UINT32) { DC_ASSIGN_FIELD(garmin->cache, MAXDEPTH, data / 1000.0); }
DECLARE_FIELD(DIVE_SUMMARY, surface_interval, UINT32) { } // sec
DECLARE_FIELD(DIVE_SUMMARY, start_cns, UINT8) { } // percent
DECLARE_FIELD(DIVE_SUMMARY, end_cns, UINT8) { } // percent
DECLARE_FIELD(DIVE_SUMMARY, start_n2, UINT16) { } // percent
DECLARE_FIELD(DIVE_SUMMARY, end_n2, UINT16) { } // percent
DECLARE_FIELD(DIVE_SUMMARY, o2_toxicity, UINT16) { } // OTUs
DECLARE_FIELD(DIVE_SUMMARY, dive_number, UINT32) { }
DECLARE_FIELD(DIVE_SUMMARY, bottom_time, UINT32) { DC_ASSIGN_FIELD(garmin->cache, DIVETIME, data / 1000); }
DECLARE_FIELD(DIVE_SUMMARY, avg_pressure_sac, UINT16) { } // 100 * bar/min/pressure
DECLARE_FIELD(DIVE_SUMMARY, avg_volume_sac, UINT16) { } // 100 * L/min/pressure
DECLARE_FIELD(DIVE_SUMMARY, avg_rmv, UINT16) { } // 100 * L/min
// DIVE_SETTINGS
DECLARE_FIELD(DIVE_SETTINGS, name, STRING) { }
DECLARE_FIELD(DIVE_SETTINGS, model, ENUM)
{
garmin->record_data.model = data;
garmin->record_data.pending |= RECORD_DECO_MODEL;
}
DECLARE_FIELD(DIVE_SETTINGS, gf_low, UINT8)
{
garmin->record_data.gf_low = data;
garmin->record_data.pending |= RECORD_DECO_MODEL;
}
DECLARE_FIELD(DIVE_SETTINGS, gf_high, UINT8)
{
garmin->record_data.gf_high = data;
garmin->record_data.pending |= RECORD_DECO_MODEL;
}
DECLARE_FIELD(DIVE_SETTINGS, water_type, ENUM)
{
garmin->cache.SALINITY.type = data ? DC_WATER_SALT : DC_WATER_FRESH;
garmin->cache.initialized |= 1 << DC_FIELD_SALINITY;
}
DECLARE_FIELD(DIVE_SETTINGS, water_density, FLOAT)
{
union { unsigned int binary; float actual; } val;
val.binary = data;
garmin->cache.SALINITY.density = val.actual;
garmin->cache.initialized |= 1 << DC_FIELD_SALINITY;
}
DECLARE_FIELD(DIVE_SETTINGS, po2_warn, UINT8) { }
DECLARE_FIELD(DIVE_SETTINGS, po2_critical, UINT8) { }
DECLARE_FIELD(DIVE_SETTINGS, po2_deco, UINT8) { }
DECLARE_FIELD(DIVE_SETTINGS, safety_stop_enabled, ENUM) { }
DECLARE_FIELD(DIVE_SETTINGS, bottom_depth, FLOAT) { }
DECLARE_FIELD(DIVE_SETTINGS, bottom_time, UINT32) { }
DECLARE_FIELD(DIVE_SETTINGS, apnea_countdown_enabled, ENUM) { }
DECLARE_FIELD(DIVE_SETTINGS, apnea_countdown_time, UINT32) { }
DECLARE_FIELD(DIVE_SETTINGS, backlight_mode, ENUM) { }
DECLARE_FIELD(DIVE_SETTINGS, backlight_brightness, UINT8) { }
DECLARE_FIELD(DIVE_SETTINGS, backlight_timeout, UINT8) { }
DECLARE_FIELD(DIVE_SETTINGS, repeat_dive_interval, UINT16) { }
DECLARE_FIELD(DIVE_SETTINGS, safety_stop_time, UINT16) { }
DECLARE_FIELD(DIVE_SETTINGS, heart_rate_source_type, ENUM) { }
DECLARE_FIELD(DIVE_SETTINGS, heart_rate_device_type, UINT8) { }
DECLARE_FIELD(DIVE_SETTINGS, setpoint_low_switch_mode, ENUM)
{
// 0 - manual, 1 - auto
garmin->dive.setpoint_low_switch_mode = data;
}
DECLARE_FIELD(DIVE_SETTINGS, setpoint_low_cbar, UINT8)
{
garmin->dive.setpoint_low_cbar = data;
// The initial setpoint at the start of the dive is the low setpoint
garmin->record_data.setpoint_actual_cbar = garmin->dive.setpoint_low_cbar;
garmin->record_data.pending |= RECORD_SETPOINT_CHANGE;
}
DECLARE_FIELD(DIVE_SETTINGS, setpoint_low_switch_depth_mm, UINT32)
{
garmin->dive.setpoint_low_switch_depth_mm = data;
}
DECLARE_FIELD(DIVE_SETTINGS, setpoint_high_switch_mode, ENUM)
{
// 0 - manual, 1 - auto
garmin->dive.setpoint_high_switch_mode = data;
}
DECLARE_FIELD(DIVE_SETTINGS, setpoint_high_cbar, UINT8)
{
garmin->dive.setpoint_high_cbar = data;
}
DECLARE_FIELD(DIVE_SETTINGS, setpoint_high_switch_depth_mm, UINT32)
{
garmin->dive.setpoint_high_switch_depth_mm = data;
}
// SENSOR_PROFILE record for each ANT/BLE sensor.
// We only care about sensor type 28 - Garmin tank pod.
DECLARE_FIELD(SENSOR_PROFILE, ant_channel_id, UINT32Z)
{
current_sensor(garmin)->sensor_id = data;
}
DECLARE_FIELD(SENSOR_PROFILE, name, STRING) { }
DECLARE_FIELD(SENSOR_PROFILE, enabled, ENUM)
{
current_sensor(garmin)->sensor_enabled = data;
}
DECLARE_FIELD(SENSOR_PROFILE, sensor_type, ENUM)
{
// 28 is tank pod
// start filling in next sensor after this record
if (data == 28)
garmin->record_data.pending |= RECORD_SENSOR_PROFILE;
}
DECLARE_FIELD(SENSOR_PROFILE, pressure_units, ENUM)
{
// 0 is PSI, 1 is KPA (unused), 2 is Bar
current_sensor(garmin)->sensor_units = data;
}
DECLARE_FIELD(SENSOR_PROFILE, rated_pressure, UINT16)
{
current_sensor(garmin)->sensor_rated_pressure = data;
}
DECLARE_FIELD(SENSOR_PROFILE, reserve_pressure, UINT16)
{
current_sensor(garmin)->sensor_reserve_pressure = data;
}
DECLARE_FIELD(SENSOR_PROFILE, volume, UINT16)
{
current_sensor(garmin)->sensor_volume = data;
}
DECLARE_FIELD(SENSOR_PROFILE, used_for_gas_rate, ENUM)
{
current_sensor(garmin)->sensor_used_for_gas_rate = data;
}
DECLARE_FIELD(TANK_UPDATE, sensor, UINT32Z)
{
garmin->record_data.sensor = data;
}
DECLARE_FIELD(TANK_UPDATE, pressure, UINT16)
{
garmin->record_data.pressure = data;
garmin->record_data.pending |= RECORD_TANK_UPDATE;
}
DECLARE_FIELD(TANK_SUMMARY, sensor, UINT32Z) { } // sensor ID
DECLARE_FIELD(TANK_SUMMARY, start_pressure, UINT16) { } // Bar * 100
DECLARE_FIELD(TANK_SUMMARY, end_pressure, UINT16) { } // Bar * 100
DECLARE_FIELD(TANK_SUMMARY, volume_used, UINT32) { } // L * 100
// EVENT
DECLARE_FIELD(EVENT, event, ENUM)
{
garmin->record_data.event_nr = data;
garmin->record_data.pending |= RECORD_EVENT;
}
DECLARE_FIELD(EVENT, type, ENUM)
{
garmin->record_data.event_type = data;
garmin->record_data.pending |= RECORD_EVENT;
}
DECLARE_FIELD(EVENT, data, UINT32)
{
garmin->record_data.event_data = data;
}
DECLARE_FIELD(EVENT, event_group, UINT8)
{
garmin->record_data.event_group = data;
}
DECLARE_FIELD(EVENT, unknown, UINT32)
{
garmin->record_data.event_unknown = data;
}
DECLARE_FIELD(EVENT, tank_pressure_reserve, UINT32Z) { } // sensor ID
DECLARE_FIELD(EVENT, tank_pressure_critical, UINT32Z) { } // sensor ID
DECLARE_FIELD(EVENT, tank_pressure_lost, UINT32Z) { } // sensor ID
// "Field description" (for developer fields)
DECLARE_FIELD(FIELD_DESCRIPTION, data_index, UINT8) { }
DECLARE_FIELD(FIELD_DESCRIPTION, field_definition, UINT8) { }
DECLARE_FIELD(FIELD_DESCRIPTION, base_type, UINT8) { }
DECLARE_FIELD(FIELD_DESCRIPTION, name, STRING) { } // "Depth"
DECLARE_FIELD(FIELD_DESCRIPTION, scale, UINT8) { }
DECLARE_FIELD(FIELD_DESCRIPTION, offset, SINT8) { }
DECLARE_FIELD(FIELD_DESCRIPTION, unit, STRING) { } // "feet"
DECLARE_FIELD(FIELD_DESCRIPTION, original_mesg, UINT16) { }
DECLARE_FIELD(FIELD_DESCRIPTION, original_field, UINT8) { }
struct msg_desc {
unsigned char maxfield;
const struct field_desc *field[];
};
#define SET_FIELD(msg, nr, name, type) \
[nr] = &msg##_##name##_field_##type
#define DECLARE_MESG(name) \
static const struct msg_desc name##_msg_desc
DECLARE_MESG(FILE) = {
.maxfield = 9,
.field = {
SET_FIELD(FILE, 0, file_type, ENUM),
SET_FIELD(FILE, 1, manufacturer, UINT16),
SET_FIELD(FILE, 2, product, UINT16),
SET_FIELD(FILE, 3, serial, UINT32Z),
SET_FIELD(FILE, 4, creation_time, UINT32),
SET_FIELD(FILE, 5, number, UINT16),
SET_FIELD(FILE, 7, other_time, UINT32),
SET_FIELD(FILE, 8, product_name, STRING),
}
};
DECLARE_MESG(DEVICE_SETTINGS) = {
.maxfield = 3,
.field = {
SET_FIELD(DEVICE_SETTINGS, 1, utc_offset, UINT32), // Convert to UTC
SET_FIELD(DEVICE_SETTINGS, 2, time_offset, UINT32), // Convert to local
}
};
DECLARE_MESG(USER_PROFILE) = { };
DECLARE_MESG(HRM_PROFILE) = { };
DECLARE_MESG(ZONES_TARGET) = { };
DECLARE_MESG(SPORT) = {
.maxfield = 2,
.field = {
SET_FIELD(SPORT, 1, sub_sport, ENUM), // 53 - 57 and 63 are dive activities
}
};
DECLARE_MESG(SESSION) = {
.maxfield = 40,
.field = {
SET_FIELD(SESSION, 2, start_time, UINT32),
SET_FIELD(SESSION, 3, start_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(SESSION, 4, start_pos_long, SINT32), // 180 deg / 2**31
SET_FIELD(SESSION, 29, nec_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(SESSION, 30, nec_pos_long, SINT32), // 180 deg / 2**31
SET_FIELD(SESSION, 31, swc_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(SESSION, 32, swc_pos_long, SINT32), // 180 deg / 2**31
SET_FIELD(SESSION, 38, exit_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(SESSION, 39, exit_pos_long, SINT32), // 180 deg / 2**31
}
};
DECLARE_MESG(LAP) = {
.maxfield = 31,
.field = {
SET_FIELD(LAP, 2, start_time, UINT32),
SET_FIELD(LAP, 3, start_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(LAP, 4, start_pos_long, SINT32), // 180 deg / 2**31
SET_FIELD(LAP, 5, end_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(LAP, 6, end_pos_long, SINT32), // 180 deg / 2**31
SET_FIELD(LAP, 27, some_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(LAP, 28, some_pos_long, SINT32), // 180 deg / 2**31
SET_FIELD(LAP, 29, other_pos_lat, SINT32), // 180 deg / 2**31
SET_FIELD(LAP, 30, other_pos_long, SINT32), // 180 deg / 2**31
}
};
DECLARE_MESG(RECORD) = {
.maxfield = 128,
.field = {
SET_FIELD(RECORD, 0, position_lat, SINT32), // 180 deg / 2**31
SET_FIELD(RECORD, 1, position_long, SINT32), // 180 deg / 2**31
SET_FIELD(RECORD, 2, altitude, UINT16), // 5 *m + 500 ?
SET_FIELD(RECORD, 3, heart_rate, UINT8), // bpm
SET_FIELD(RECORD, 4, cadence, UINT8), // cadence
SET_FIELD(RECORD, 5, distance, UINT32), // Distance in 100 * m? WTF?
SET_FIELD(RECORD, 6, speed, UINT16), // m/s? Who knows..
SET_FIELD(RECORD, 13, temperature, SINT8), // degrees C
SET_FIELD(RECORD, 53, fract_cadence, UINT8), // fractional cadence
SET_FIELD(RECORD, 91, abs_pressure, UINT32), // Pascal
SET_FIELD(RECORD, 92, depth, UINT32), // mm
SET_FIELD(RECORD, 93, next_stop_depth, UINT32), // mm
SET_FIELD(RECORD, 94, next_stop_time, UINT32), // seconds
SET_FIELD(RECORD, 95, tts, UINT32), // seconds
SET_FIELD(RECORD, 96, ndl, UINT32), // s
SET_FIELD(RECORD, 97, cns_load, UINT8), // percent
SET_FIELD(RECORD, 98, n2_load, UINT16), // percent
SET_FIELD(RECORD, 123, air_time_remaining, UINT32), // seconds
SET_FIELD(RECORD, 124, pressure_sac, UINT16), // 100 * bar/min/pressure
SET_FIELD(RECORD, 125, volume_sac, UINT16), // 100 * l/min/pressure
SET_FIELD(RECORD, 126, rmv, UINT16), // 100 * l/min
SET_FIELD(RECORD, 127, ascent_rate, SINT32), // mm/s (negative is down)
}
};