Class: F4R::Definition::Header

Inherits:
BinData::Record
  • Object
show all
Defined in:
lib/f4r.rb

Overview

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             |                         |              |

Instance Method Summary collapse

Instance Method Details

#crc_mismatch?(io) ⇒ Boolean

CRC validations

Parameters:

  • io (IO)

Returns:

  • (Boolean)


777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# 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_sizeInteger

Returns:

  • (Integer)


801
802
803
# File 'lib/f4r.rb', line 801

def file_size
  header_size.snapshot + data_size.snapshot
end

#read(io) ⇒ Object

Data validation should happen as soon as possible.

Parameters:

  • io (IO)


735
736
737
738
739
740
741
742
743
744
745
746
747
748
# 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?Boolean

Returns:

  • (Boolean)


767
768
769
# File 'lib/f4r.rb', line 767

def supported_header?
  [12, 14].include? header_size.snapshot
end

#to_log_sString

Header format for log output

Example:

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

Returns:

  • (String)


813
814
815
816
817
818
819
820
821
822
823
824
# 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) ⇒ Object

Write header and its CRC to IO

Parameters:

  • io (IO)


755
756
757
758
759
760
761
762
# 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