Class: F4R::Definition::RecordField

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

Overview

Record Field

| Bit | Name             | Description                         |
|-----+------------------+-------------------------------------|
|   7 | Endian Ability   | 0 - for single byte data            |
|     |                  | 1 - if base type has endianness     |
|     |                  | (i.e. base type is 2 or more bytes) |
| 5-6 | Reserved         | Reserved                            |
| 0-4 | Base Type Number | Number assigned to Base Type        |

Instance Method Summary collapse

Instance Method Details

#base_type_definitionHash

Base type definitions for field

Returns:

  • (Hash)


1018
1019
1020
# File 'lib/f4r.rb', line 1018

def base_type_definition
  @base_type_definition ||= get_base_type_definition
end

#global_messageHash

Global message for field.

Returns:

  • (Hash)


1009
1010
1011
# File 'lib/f4r.rb', line 1009

def global_message
  @global_message ||= parent.parent.global_message
end

#global_message_fieldHash

Global message field with all its properties

Returns:

  • (Hash)


999
1000
1001
1002
# File 'lib/f4r.rb', line 999

def global_message_field
  @global_message_field ||= global_message[:fields].
    find { |f| f[:field_def] ==  field_definition_number.snapshot }
end

#nameString

Returns:

  • (String)


950
951
952
# File 'lib/f4r.rb', line 950

def name
  global_message_field[:field_name]
end

#numberInteger

Returns:

  • (Integer)


957
958
959
# File 'lib/f4r.rb', line 957

def number
  global_message_field[:field_def]
end

#to_bindata_structArray

Returns field in [BinData::Struct] format. Field identifier is its number since some field names (e.g., ‘type’) are reserved [BinData::Struct] keywords.

Examples:

[:uint8, '1']
[:string, '2', {length: 8}]
[:array, '3', {type: uint8, initial_length: 4}]

Returns:

  • (Array)


973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
# File 'lib/f4r.rb', line 973

def to_bindata_struct
  type = base_type_definition[:bindata]
  bytes = base_type_definition[:bytes]

  case
  when type == :string
    [type, number.to_s, {length: byte_count.snapshot}]
  when byte_count.snapshot > bytes # array
    if byte_count.snapshot % bytes != 0
      Log.error <<~ERROR
        Total bytes ("#{total_bytes}") must be multiple of base type
        bytes ("#{bytes}") of type "#{type}" in global FIT message "#{name}".
      ERROR
    end
    length = byte_count.snapshot / bytes
    [:array, number.to_s, {type: type, initial_length: length}]
  else
    [type, number.to_s]
  end
end

#to_log_s(value) ⇒ String

Field log output

@example:

FDN:2 BC: 4 EA: 1 R: 0 BTN:4 uint16 message_# field_#:  0 65535

Parameters:

  • value (String, Integer)

Returns:

  • (String)


1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'lib/f4r.rb', line 1031

def to_log_s(value)
  [
    ('%-8s' % "FDN:#{field_definition_number.snapshot}"),
    ('%-8s' % "BC: #{byte_count.snapshot}"),
    ('%-8s' % "EA: #{endian_ability.snapshot}"),
    ('%-8s' % "R:  #{reserved.snapshot}"),
    ('%-8s' % "BTN:#{base_type_number.snapshot}"),
    ('%-8s' % (base_type_definition[:fit])),
    global_message[:name],
    " #{name}: ",
    value,
  ].join(' ')
end