|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// The following FIT Protocol software provided may be used with FIT protocol |
| 3 | +// devices only and remains the copyrighted property of Garmin Canada Inc. |
| 4 | +// The software is being provided on an "as-is" basis and as an accommodation, |
| 5 | +// and therefore all warranties, representations, or guarantees of any kind |
| 6 | +// (whether express, implied or statutory) including, without limitation, |
| 7 | +// warranties of merchantability, non-infringement, or fitness for a particular |
| 8 | +// purpose, are specifically disclaimed. |
| 9 | +// |
| 10 | +// Copyright 2008 Garmin Canada Inc. |
| 11 | +//////////////////////////////////////////////////////////////////////////////// |
| 12 | + |
| 13 | +#include <fstream> |
| 14 | +#include <iostream> |
| 15 | + |
| 16 | +#include "fit_decode.hpp" |
| 17 | +#include "fit_mesg_broadcaster.hpp" |
| 18 | +#include "fit_developer_field_description.hpp" |
| 19 | + |
| 20 | +class Listener |
| 21 | + : public fit::FileIdMesgListener |
| 22 | + , public fit::UserProfileMesgListener |
| 23 | + , public fit::MonitoringMesgListener |
| 24 | + , public fit::DeviceInfoMesgListener |
| 25 | + , public fit::MesgListener |
| 26 | + , public fit::DeveloperFieldDescriptionListener |
| 27 | + , public fit::RecordMesgListener |
| 28 | +{ |
| 29 | +public: |
| 30 | + |
| 31 | + static void PrintValues(const fit::FieldBase& field) |
| 32 | + { |
| 33 | + for (FIT_UINT8 j=0; j< (FIT_UINT8)field.GetNumValues(); j++) |
| 34 | + { |
| 35 | + std::wcout << L" Val" << j << L": "; |
| 36 | + switch (field.GetType()) |
| 37 | + { |
| 38 | + // Get float 64 values for numeric types to receive values that have |
| 39 | + // their scale and offset properly applied. |
| 40 | + case FIT_BASE_TYPE_ENUM: |
| 41 | + case FIT_BASE_TYPE_BYTE: |
| 42 | + case FIT_BASE_TYPE_SINT8: |
| 43 | + case FIT_BASE_TYPE_UINT8: |
| 44 | + case FIT_BASE_TYPE_SINT16: |
| 45 | + case FIT_BASE_TYPE_UINT16: |
| 46 | + case FIT_BASE_TYPE_SINT32: |
| 47 | + case FIT_BASE_TYPE_UINT32: |
| 48 | + case FIT_BASE_TYPE_SINT64: |
| 49 | + case FIT_BASE_TYPE_UINT64: |
| 50 | + case FIT_BASE_TYPE_UINT8Z: |
| 51 | + case FIT_BASE_TYPE_UINT16Z: |
| 52 | + case FIT_BASE_TYPE_UINT32Z: |
| 53 | + case FIT_BASE_TYPE_UINT64Z: |
| 54 | + case FIT_BASE_TYPE_FLOAT32: |
| 55 | + case FIT_BASE_TYPE_FLOAT64: |
| 56 | + std::wcout << field.GetFLOAT64Value(j); |
| 57 | + break; |
| 58 | + case FIT_BASE_TYPE_STRING: |
| 59 | + std::wcout << field.GetSTRINGValue(j); |
| 60 | + break; |
| 61 | + default: |
| 62 | + break; |
| 63 | + } |
| 64 | + std::wcout << L" " << field.GetUnits().c_str() << L"\n";; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + void OnMesg(fit::Mesg& mesg) override |
| 69 | + { |
| 70 | + printf("On Mesg:\n"); |
| 71 | + std::wcout << L" New Mesg: " << mesg.GetName().c_str() << L". It has " << mesg.GetNumFields() << L" field(s) and " << mesg.GetNumDevFields() << " developer field(s).\n"; |
| 72 | + |
| 73 | + for (FIT_UINT16 i = 0; i < (FIT_UINT16)mesg.GetNumFields(); i++) |
| 74 | + { |
| 75 | + fit::Field* field = mesg.GetFieldByIndex(i); |
| 76 | + std::wcout << L" Field" << i << " (" << field->GetName().c_str() << ") has " << field->GetNumValues() << L" value(s)\n"; |
| 77 | + PrintValues(*field); |
| 78 | + } |
| 79 | + |
| 80 | + for (auto devField : mesg.GetDeveloperFields()) |
| 81 | + { |
| 82 | + std::wcout << L" Developer Field(" << devField.GetName().c_str() << ") has " << devField.GetNumValues() << L" value(s)\n"; |
| 83 | + PrintValues(devField); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void OnMesg(fit::FileIdMesg& mesg) override |
| 88 | + { |
| 89 | + printf("File ID:\n"); |
| 90 | + if (mesg.IsTypeValid()) |
| 91 | + printf(" Type: %d\n", mesg.GetType()); |
| 92 | + if (mesg.IsManufacturerValid()) |
| 93 | + printf(" Manufacturer: %d\n", mesg.GetManufacturer()); |
| 94 | + if (mesg.IsProductValid()) |
| 95 | + printf(" Product: %d\n", mesg.GetProduct()); |
| 96 | + if (mesg.IsSerialNumberValid()) |
| 97 | + printf(" Serial Number: %u\n", mesg.GetSerialNumber()); |
| 98 | + if (mesg.IsNumberValid()) |
| 99 | + printf(" Number: %d\n", mesg.GetNumber()); |
| 100 | + } |
| 101 | + |
| 102 | + void OnMesg(fit::UserProfileMesg& mesg) override |
| 103 | + { |
| 104 | + printf("User profile:\n"); |
| 105 | + if (mesg.IsFriendlyNameValid()) |
| 106 | + std::wcout << L" Friendly Name: " << mesg.GetFriendlyName().c_str() << L"\n"; |
| 107 | + if (mesg.GetGender() == FIT_GENDER_MALE) |
| 108 | + printf(" Gender: Male\n"); |
| 109 | + if (mesg.GetGender() == FIT_GENDER_FEMALE) |
| 110 | + printf(" Gender: Female\n"); |
| 111 | + if (mesg.IsAgeValid()) |
| 112 | + printf(" Age [years]: %d\n", mesg.GetAge()); |
| 113 | + if ( mesg.IsWeightValid() ) |
| 114 | + printf(" Weight [kg]: %0.2f\n", mesg.GetWeight()); |
| 115 | + } |
| 116 | + |
| 117 | + void OnMesg(fit::DeviceInfoMesg& mesg) override |
| 118 | + { |
| 119 | + printf("Device info:\n"); |
| 120 | + |
| 121 | + if (mesg.IsTimestampValid()) |
| 122 | + printf(" Timestamp: %d\n", mesg.GetTimestamp()); |
| 123 | + |
| 124 | + switch(mesg.GetBatteryStatus()) |
| 125 | + { |
| 126 | + case FIT_BATTERY_STATUS_CRITICAL: |
| 127 | + printf(" Battery status: Critical\n"); |
| 128 | + break; |
| 129 | + case FIT_BATTERY_STATUS_GOOD: |
| 130 | + printf(" Battery status: Good\n"); |
| 131 | + break; |
| 132 | + case FIT_BATTERY_STATUS_LOW: |
| 133 | + printf(" Battery status: Low\n"); |
| 134 | + break; |
| 135 | + case FIT_BATTERY_STATUS_NEW: |
| 136 | + printf(" Battery status: New\n"); |
| 137 | + break; |
| 138 | + case FIT_BATTERY_STATUS_OK: |
| 139 | + printf(" Battery status: OK\n"); |
| 140 | + break; |
| 141 | + default: |
| 142 | + printf(" Battery status: Invalid\n"); |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + void OnMesg(fit::MonitoringMesg& mesg) override |
| 148 | + { |
| 149 | + printf("Monitoring:\n"); |
| 150 | + |
| 151 | + if (mesg.IsTimestampValid()) |
| 152 | + { |
| 153 | + printf(" Timestamp: %d\n", mesg.GetTimestamp()); |
| 154 | + } |
| 155 | + |
| 156 | + if(mesg.IsActivityTypeValid()) |
| 157 | + { |
| 158 | + printf(" Activity type: %d\n", mesg.GetActivityType()); |
| 159 | + } |
| 160 | + |
| 161 | + switch(mesg.GetActivityType()) // The Cycling field is dynamic |
| 162 | + { |
| 163 | + case FIT_ACTIVITY_TYPE_WALKING: |
| 164 | + case FIT_ACTIVITY_TYPE_RUNNING: // Intentional fallthrough |
| 165 | + if(mesg.IsStepsValid()) |
| 166 | + { |
| 167 | + printf(" Steps: %d\n", mesg.GetSteps()); |
| 168 | + } |
| 169 | + break; |
| 170 | + case FIT_ACTIVITY_TYPE_CYCLING: |
| 171 | + case FIT_ACTIVITY_TYPE_SWIMMING: // Intentional fallthrough |
| 172 | + if( mesg.IsStrokesValid() ) |
| 173 | + { |
| 174 | + printf( "Strokes: %f\n", mesg.GetStrokes()); |
| 175 | + } |
| 176 | + break; |
| 177 | + default: |
| 178 | + if(mesg.IsCyclesValid() ) |
| 179 | + { |
| 180 | + printf( "Cycles: %f\n", mesg.GetCycles()); |
| 181 | + } |
| 182 | + break; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + static void PrintOverrideValues( const fit::Mesg& mesg, FIT_UINT8 fieldNum ) |
| 187 | + { |
| 188 | + std::vector<const fit::FieldBase*> fields = mesg.GetOverrideFields( fieldNum ); |
| 189 | + const fit::Profile::FIELD * profileField = fit::Profile::GetField( mesg.GetNum(), fieldNum ); |
| 190 | + FIT_BOOL namePrinted = FIT_FALSE; |
| 191 | + |
| 192 | + for ( const fit::FieldBase* field : fields ) |
| 193 | + { |
| 194 | + if ( !namePrinted ) |
| 195 | + { |
| 196 | + printf( " %s:\n", profileField->name.c_str() ); |
| 197 | + namePrinted = FIT_TRUE; |
| 198 | + } |
| 199 | + |
| 200 | + if ( FIT_NULL != dynamic_cast<const fit::Field*>( field ) ) |
| 201 | + { |
| 202 | + // Native Field |
| 203 | + printf( " native: " ); |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + // Developer Field |
| 208 | + printf( " override: " ); |
| 209 | + } |
| 210 | + |
| 211 | + switch (field->GetType()) |
| 212 | + { |
| 213 | + // Get float 64 values for numeric types to receive values that have |
| 214 | + // their scale and offset properly applied. |
| 215 | + case FIT_BASE_TYPE_ENUM: |
| 216 | + case FIT_BASE_TYPE_BYTE: |
| 217 | + case FIT_BASE_TYPE_SINT8: |
| 218 | + case FIT_BASE_TYPE_UINT8: |
| 219 | + case FIT_BASE_TYPE_SINT16: |
| 220 | + case FIT_BASE_TYPE_UINT16: |
| 221 | + case FIT_BASE_TYPE_SINT32: |
| 222 | + case FIT_BASE_TYPE_UINT32: |
| 223 | + case FIT_BASE_TYPE_SINT64: |
| 224 | + case FIT_BASE_TYPE_UINT64: |
| 225 | + case FIT_BASE_TYPE_UINT8Z: |
| 226 | + case FIT_BASE_TYPE_UINT16Z: |
| 227 | + case FIT_BASE_TYPE_UINT32Z: |
| 228 | + case FIT_BASE_TYPE_UINT64Z: |
| 229 | + case FIT_BASE_TYPE_FLOAT32: |
| 230 | + case FIT_BASE_TYPE_FLOAT64: |
| 231 | + printf("%f\n", field->GetFLOAT64Value()); |
| 232 | + break; |
| 233 | + case FIT_BASE_TYPE_STRING: |
| 234 | + printf("%ls\n", field->GetSTRINGValue().c_str()); |
| 235 | + break; |
| 236 | + default: |
| 237 | + break; |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + void OnMesg( fit::RecordMesg& record ) override |
| 243 | + { |
| 244 | + printf( "Record:\n" ); |
| 245 | + PrintOverrideValues( record, fit::RecordMesg::FieldDefNum::HeartRate); |
| 246 | + PrintOverrideValues( record, fit::RecordMesg::FieldDefNum::Cadence ); |
| 247 | + PrintOverrideValues( record, fit::RecordMesg::FieldDefNum::Distance ); |
| 248 | + PrintOverrideValues( record, fit::RecordMesg::FieldDefNum::Speed ); |
| 249 | + } |
| 250 | + |
| 251 | + void OnDeveloperFieldDescription( const fit::DeveloperFieldDescription& desc ) override |
| 252 | + { |
| 253 | + printf( "New Developer Field Description\n" ); |
| 254 | + printf( " App Version: %d\n", desc.GetApplicationVersion() ); |
| 255 | + printf( " Field Number: %d\n", desc.GetFieldDefinitionNumber() ); |
| 256 | + } |
| 257 | +}; |
| 258 | + |
| 259 | +int main(int argc, char* argv[]) |
| 260 | +{ |
| 261 | + fit::Decode decode; |
| 262 | + // decode.SkipHeader(); // Use on streams with no header and footer (stream contains FIT defn and data messages only) |
| 263 | + // decode.IncompleteStream(); // This suppresses exceptions with unexpected eof (also incorrect crc) |
| 264 | + fit::MesgBroadcaster mesgBroadcaster; |
| 265 | + Listener listener; |
| 266 | + std::fstream file; |
| 267 | + |
| 268 | + printf("FIT Decode Example Application\n"); |
| 269 | + |
| 270 | + if (argc != 2) |
| 271 | + { |
| 272 | + printf("Usage: decode.exe <filename>\n"); |
| 273 | + return -1; |
| 274 | + } |
| 275 | + |
| 276 | + file.open(argv[1], std::ios::in | std::ios::binary); |
| 277 | + |
| 278 | + if (!file.is_open()) |
| 279 | + { |
| 280 | + printf("Error opening file %s\n", argv[1]); |
| 281 | + return -1; |
| 282 | + } |
| 283 | + |
| 284 | + if (!decode.CheckIntegrity(file)) |
| 285 | + { |
| 286 | + printf("FIT file integrity failed.\nAttempting to decode...\n"); |
| 287 | + } |
| 288 | + |
| 289 | + mesgBroadcaster.AddListener((fit::FileIdMesgListener &)listener); |
| 290 | + mesgBroadcaster.AddListener((fit::UserProfileMesgListener &)listener); |
| 291 | + mesgBroadcaster.AddListener((fit::MonitoringMesgListener &)listener); |
| 292 | + mesgBroadcaster.AddListener((fit::DeviceInfoMesgListener &)listener); |
| 293 | + mesgBroadcaster.AddListener((fit::RecordMesgListener&)listener); |
| 294 | + mesgBroadcaster.AddListener((fit::MesgListener &)listener); |
| 295 | + |
| 296 | + try |
| 297 | + { |
| 298 | + decode.Read(&file, &mesgBroadcaster, &mesgBroadcaster, &listener); |
| 299 | + } |
| 300 | + catch (const fit::RuntimeException& e) |
| 301 | + { |
| 302 | + printf("Exception decoding file: %s\n", e.what()); |
| 303 | + return -1; |
| 304 | + } |
| 305 | + catch (...) |
| 306 | + { |
| 307 | + printf("Exception decoding file"); |
| 308 | + return -1; |
| 309 | + } |
| 310 | + |
| 311 | + printf("Decoded FIT file %s.\n", argv[1]); |
| 312 | + |
| 313 | + return 0; |
| 314 | +} |
| 315 | + |
0 commit comments