wrote quick'n'dirty FIT file parser in PHP without SDK

Inspired by Bram2473 java code to examine the exact vo2max (metmax) in FIT files I studied the FIT format over the holiday and wrote some code in PHP to take apart FIT files without the SDK so I could get higher error tolerance and other interesting fields.

It is ugly as hell but if there is any interest I can clean it up and eventually throw it on github or something to help others.

Since it doesn't have the SDK it doesn't know things like field names or scaling which you'd have to lookup yourself but easy enough with the CSV or fit dump utilities.

FIT is rather nasty and messy but it's still only about 100 lines. Have to finish some work on developer fields.

It also occurs to me it could be eventually converted to javascript. I don't think anyone has written a javascript client-side FIT parser? The only complexity is that "unpack" in javascript is like 300 lines of code but could be done via a library (defeating the small code size idea unfortunately).

basically FIT is just

file header -> block header (compressed vs uncompressed) -> definition vs data blocks -> definition/data fields (eventually developer fields with indexes)

the messy part is stepping byte by byte through the file and caching local definition tables because the data that uses them can be out of order

FIT was obviously written in an ancient time with low cpu power devices that just had to slap down data quickly

What's interesting is that when a garmin watch has to append stored and forwarded heartrate data from a HRM-TRI/SWIM/PRO. it apparently must take apart and put together a FIT file on the fly right on the watch in that limited memory and cpu power so that code must be really fascinating and tiny, Garmin might consider showing that off

  • Thank you for sharing!

    There are a few open source PHP (and even JavaScript) parsers in Github that you might consider contributing to.

    In the FIT SDK, the file Profile.xlsx lists all the messages, fields, scales, offsets, types etc. If you save each worksheet as a CSV file, ie messages.csv and types.csv, then you will have the information needed to generate whatever lookup tables or dictionaries that you need to convert the ids to more meaningful names or objects; and apply the appropriate scales and offsets. Most projects implement a tool to parse the CSV files and generate the code, since hand coding that is way to error prone,

    As for the store and forward HR data. The devices append the FIT file from the HRM to the FIT file created on the device. This is known as chained fit files, which are explained here https://developer.garmin.com/fit/protocol/ The SDK comes with plugins for merging the data, which makes it transparent for the clients that consume the files. The plugin framework is covered in the FIT Protocol doc (link above), and how to use the HrToRecordMesgBroadcast Plugin is covered here: https://developer.garmin.com/fit/cookbook/decoding-activity-files/