class F4R::Definition::Header

Main header for FIT files.

| Byte | Parameter           | Description             | Size (Bytes) |
|------+---------------------+-------------------------+--------------|
|    0 | Header Size         | Length of file header   |            1 |
|    1 | Protocol Version    | Provided by SDK         |            1 |
|    2 | Profile Version LSB | Provided by SDK         |            2 |
|    3 | Profile Version MSB | Provided by SDK         |              |
|    4 | Data Size LSB       | Length of data records  |            4 |
|    5 | Data Size           | Minus header or CRC     |              |
|    6 | Data Size           |                         |              |
|    7 | Data Size MSB       |                         |              |
|    8 | Data Type Byte [0]  | ASCII values for ".FIT" |            4 |
|    9 | Data Type Byte [1]  |                         |              |
|   10 | Data Type Byte [2]  |                         |              |
|   11 | Data Type Byte [3]  |                         |              |
|   12 | CRC LSB             | CRC                     |            2 |
|   13 | CRC MSB             |                         |              |

Public Instance Methods

crc_mismatch?(io) click to toggle source

CRC validations

@param [IO] io @return [Boolean]

# File lib/f4r.rb, line 777
def crc_mismatch?(io)
  unless crc.snapshot.zero?
    io.rewind
    crc_16 = CRC16.crc(io.read(header_size.snapshot - 2))
    unless crc_16 == crc.snapshot
      Log.error "CRC mismatch: Computed #{crc_16} instead of #{crc.snapshot}."
    end
  end

  start_pos = header_size.snapshot == 14 ? header_size : 0

  crc_16 = CRC16.crc(IO.binread(io, file_size, start_pos))
  crc_ref = io.readbyte.to_i | (io.readbyte.to_i << 8)

  unless crc_16 = crc_ref
    Log.error "crc mismatch: computed #{crc_16} instead of #{crc_ref}."
  end

  io.seek(header_size)
end
file_size() click to toggle source

@return [Integer]

# File lib/f4r.rb, line 801
def file_size
  header_size.snapshot + data_size.snapshot
end
read(io) click to toggle source

Data validation should happen as soon as possible.

@param [IO] io

Calls superclass method
# File lib/f4r.rb, line 735
def read(io)
  super

  case
  when !supported_header?
    Log.error  "Unsupported header size: #{header_size.snapshot}."
  when data_type.snapshot != '.FIT'
    Log.error "Unknown file type: #{data_type.snapshot}."
  end

  crc_mismatch?(io)

  Log.decode [self.class, __method__], to_log_s
end
supported_header?() click to toggle source

@return [Boolean]

# File lib/f4r.rb, line 767
def supported_header?
  [12, 14].include? header_size.snapshot
end
to_log_s() click to toggle source

Header format for log output

Example:

HS: 14   PlV: 32   PeV: 1012 DS: 1106 DT: .FIT CRC:0

@return [String]

# File lib/f4r.rb, line 813
def to_log_s
  {
    file_header: [
      ('%-8s' % "HS: #{header_size.snapshot}"),
      ('%-8s' % "PlV:#{protocol_version.snapshot}"),
      ('%-8s' % "PeV:#{profile_version.snapshot}"),
      ('%-8s' % "DS: #{data_size.snapshot}"),
      ('%-8s' % "DT: #{data_type.snapshot}"),
      ('%-8s' % "CRC:#{crc.snapshot}"),
    ].join(' ')
  }
end
write(io) click to toggle source

Write header and its CRC to IO

@param [IO] io

Calls superclass method
# File lib/f4r.rb, line 755
def write(io)
  super
  io.rewind
  crc_16 = CRC16.crc(io.read(header_size.snapshot - 2))
  BinData::Uint16le.new(crc_16).write(io)

  Log.encode [self.class, __method__], to_log_s
end