summaryrefslogtreecommitdiff
path: root/src/FIT.hpp
blob: 1ee4b3cd5f6b29cefb0f9e44c2b31414b1efbc04 (plain)
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
// -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*-
/***************************************************************************
 *   Copyright (C) 2010-2012 by Oleg Khudyakov                             *
 *   prcoder@gmail.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
// ***** BEGIN LICENSE BLOCK *****
////////////////////////////////////////////////////////////////////
// Copyright (c) 2012-2013 RALOVICH, Kristóf                      //
//                                                                //
// This program is free software; you can redistribute it and/or  //
// modify it under the terms of the GNU General Public License    //
// version 2 as published by the Free Software Foundation.        //
//                                                                //
////////////////////////////////////////////////////////////////////
// ***** END LICENSE BLOCK *****
#pragma once

#include "GPX.hpp"

#include <stdintfwd.hpp>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <boost/static_assert.hpp>
#include <ctime>


namespace antpm{


#pragma pack(1)
struct FITHeader
{
    uint8_t headerSize;
    uint8_t protocolVersion;
    uint16_t profileVersion;
    uint32_t dataSize;
    uint8_t signature[4];
    uint16_t headerCRC;
};

struct RecordNormalHeader
{
    uint8_t localMessageType:4;
    uint8_t reserved:2;
    uint8_t messageType:1;
    uint8_t headerType:1;
};

struct RecordCompressedTimeStampHeader
{
    uint8_t timeOffset:5;
    uint8_t localMessageType:2;
    uint8_t headerType:1;
};

union RecordHeader
{
    RecordNormalHeader normalHeader;
    RecordCompressedTimeStampHeader ctsHeader;
};

struct RecordFixed
{
    uint8_t reserved;
    uint8_t arch;
    uint16_t globalNum;
    uint8_t fieldsNum;
};

struct RecordField
{
    uint8_t definitionNum;
    uint8_t size;
    uint8_t baseType;
};

struct BaseTypeBits
{
    uint8_t baseTypeNum:5;
    uint8_t reserved:2;
    uint8_t endianAbility:1;
};

union BaseType
{
    BaseTypeBits bits;
    uint8_t byte;
};

struct ZeroFileRecord
{
    uint16_t index;
    uint8_t fileDataType;
    uint8_t recordType;
    uint16_t identifier;
    uint8_t fileDataTypeFlags;
    struct
    {
        uint8_t reserved:2;
        uint8_t crypto:1;
        uint8_t append:1;
        uint8_t archive:1;
        uint8_t erase:1;
        uint8_t write:1;
        uint8_t read:1;
    } generalFileFlags;
    uint32_t fileSize;
    uint32_t timeStamp;
};

struct DirectoryHeader
{
    uint8_t version;
    uint8_t structureLength;
    uint8_t timeFormat;
    uint8_t reserved[5];
    uint32_t currentSystemTime;
    uint32_t directoryModifiedTime;
};
BOOST_STATIC_ASSERT(sizeof(DirectoryHeader)==16);

#pragma pack()

enum BaseTypes
{
    BT_Enum = 0,
    BT_Int8,
    BT_UInt8,
    BT_Int16,
    BT_Uint16,
    BT_Int32,
    BT_UInt32,
    BT_String,
    BT_Float32,
    BT_Float64,
    BT_Uint8z,
    BT_Uint16z,
    BT_Uint32z,
    BT_ByteArray
};

struct RecordDef
{
    RecordFixed rfx;
    std::vector<RecordField> rf;
};

enum MessageFieldTypes
{
    MessageFieldTypeUnknown = 0,
    MessageFieldTypeCoord,
    MessageFieldTypeAltitude,
    MessageFieldTypeTimestamp,
    MessageFieldTypeTime,
    MessageFieldTypeWeight,
    MessageFieldTypeSpeed,
    MessageFieldTypeOdometr,
    MessageFieldTypeFileType,
    MessageFieldTypeManufacturer,
    MessageFieldTypeProduct,
    MessageFieldTypeGender,
    MessageFieldTypeLanguage,
    MessageFieldTypeSport,
    MessageFieldTypeEvent,
    MessageFieldTypeEventType
};

enum Manufacturers
{
    ManufacturerGarmin = 1,
    ManufacturerGarminFR405ANTFS,
    ManufacturerZephyr,
    ManufacturerDayton,
    ManufacturerIDT,
    ManufacturerSRM,
    ManufacturerQuarq,
    ManufacturerIBike,
    ManufacturerSaris,
    ManufacturerSparkHK,
    ManufacturerTanita,
    ManufacturerEchowell,
    ManufacturerDynastreamOEM,
    ManufacturerNautilus,
    ManufacturerDynastream,
    ManufacturerTimex,
    ManufacturerMetriGear,
    ManufacturerXelic,
    ManufacturerBeurer,
    ManufacturerCardioSport,
    ManufacturerAandD,
    ManufacturerHMM,
    ManufacturerSuunto,
    ManufacturerThitaElektronik,
    ManufacturerGPulse,
    ManufacturerCleanMobile,
    ManufacturerPedalBrain,
    ManufacturerPeaksware,
    ManufacturerSaxonar,
    ManufacturerLemondFitness,
    ManufacturerDexcom,
    ManufacturerWahooFitness,
    ManufacturerOctaneFitness,
    ManufacturerArchinoetics,
    ManufacturerTheHurtBox,
    ManufacturerCitizenSystems,
    ManufacturerUnknown1,
    ManufacturerOsynce,
    ManufacturerHolux,
    ManufacturerConcept2,
    ManufacturerUnknown2,
    ManufacturerOneGiantLeap,
    ManufacturerAceSensor,
    ManufacturerBrimBrothers,
    ManufacturerXplova,
    ManufacturerPerceptionDigital,
    ManufacturerBF1Systems,
    ManufacturerPioneer,
    ManufacturerSpantec,
    ManufacturerMetalogics,
    Manufacturer4IIIIS
};

enum GarminProducts
{
    GarminHRM1          = 1,
    GarminAXH01         = 2,
    GarminAXB01         = 3,
    GarminAXB02         = 4,
    GarminHRM2SS        = 5,
    GarminDsiAlf02      = 6,
    GarminFR405         = 717,
    GarminFR50          = 782,
    GarminFR60          = 988,
    GarminDsiAlf01      = 1011,
    GarminFR310XT       = 1018,
    GarminEDGE500       = 1036,
    GarminFR110         = 1124,
    GarminEDGE800       = 1169,
    GarminChirp         = 1253,
    GarminEDGE200       = 1325,
    GarminFR910XT       = 1328,
    GarminALF04         = 1341,
    GarminFR610         = 1345,
    GarminFR70          = 1436,
    GarminFR310XT4T     = 1446,
    GarminAMX           = 1461,
    GarminSDM4          = 10007,
    GarminTraningCenter = 20119,
    GarminConnect       = 65534
};

class ZeroFileContent
{
public:
    std::vector<ZeroFileRecord> zfRecords;
    std::vector<uint16_t> activityFiles;
    std::vector<uint16_t> waypointsFiles;
    std::vector<uint16_t> courseFiles;
    std::time_t getFitFileTime(const uint16_t idx); // represented in garmintime
};

class FIT
{
public:
    FIT();
    ~FIT();

    uint16_t CRC_byte(uint16_t crc, uint8_t byte);
    std::string getDataString(uint8_t *ptr, uint8_t size, uint8_t baseType, uint8_t messageType, uint8_t fieldNum);
    bool parse(std::vector<uint8_t> &fitData, GPX &gpx);
    bool parseZeroFile(std::vector<uint8_t> &data, ZeroFileContent &zeroFileContent);

    static bool getCreationDate(std::vector<uint8_t> &fitData, std::time_t& ct);
    std::time_t getCreationTimestamp() const { return mCreationTimestamp; }
    std::time_t getFirstTimestamp()    const { return mFirstTimestamp; }
    std::time_t getLastTimestamp()     const { return mLastTimestamp; }

private:
    std::map<uint8_t, std::string> messageTypeMap;
    std::map<uint8_t, std::map<uint8_t, std::string> > messageFieldNameMap;
    std::map<uint8_t, std::map<uint8_t, uint8_t> > messageFieldTypeMap;
    std::map<uint8_t, std::string> dataTypeMap;
    std::map<uint8_t, std::map<uint8_t, std::string> > enumMap;
    std::map<uint8_t, std::string> manufacturerMap;
    std::map<uint8_t, std::map<uint16_t, std::string> > productMap;
    int16_t manufacturer;

    std::time_t mCreationTimestamp; /// in garmin timestamp representation
    std::time_t mFirstTimestamp; /// in garmin timestamp representation
    std::time_t mLastTimestamp; /// in garmin timestamp representation
};

}