Class: F4R::Definition::Record

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

Overview

Record

| Byte            | Description           | Length     | Value         |
|-----------------+-----------------------+------------+---------------|
| 0               | Reserved              | 1 Byte     | 0             |
| 1               | Architecture          | 1 Byte     | Arch Type:    |
|                 |                       |            | 0: Little     |
|                 |                       |            | 1: Big        |
| 2-3             | Global Message #      | 2 Bytes    | 0: 65535      |
| 4               | Fields                | 1 Byte     | # of fields   |
| 5-              | Field Definition      | 3 Bytes    | Field content |
| 4 + Fields * 3  |                       |  per field |               |
| 5 + Fields * 3  | # of Developer Fields | 1 Byte     | # of Fields   |
| 6 + Fields * 3- | Developer Field Def.  | 3 Bytes    |               |
| END             |                       |  per feld  | Field content |

Instance Method Summary collapse

Instance Method Details

#endianSymbol

Helper for getting the architecture

Returns:

  • (Symbol)


1125
1126
1127
# File 'lib/f4r.rb', line 1125

def endian
  @endion ||= architecture.zero? ? :little : :big
end

#global_messageHash

Helper for getting the message global message

Returns:

  • (Hash)

    @global_message



1134
1135
1136
1137
1138
# File 'lib/f4r.rb', line 1134

def global_message
  @global_message ||= GlobalFit.messages.find do |m|
    m[:number] == global_message_number.snapshot
  end
end

#read(io) ⇒ Object

Serves as first place for validating data.

Parameters:

  • io (IO)


1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/f4r.rb', line 1110

def read(io)
  super

  unless global_message
    Log.error <<~ERROR
      Undefined global message: "#{global_message_number.snapshot}".
    ERROR
  end
end

#read_data(io) ⇒ BinData::Struct

Read data belonging to the same definition.

Parameters:

  • io (IO)

Returns:

  • (BinData::Struct)

    data



1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'lib/f4r.rb', line 1146

def read_data(io)
  data = to_bindata_struct.read(io)

  Log.decode [self.class, __method__],
    pos: io.pos, record: to_log_s

  data_fields.each do |df|
    Log.decode [self.class, __method__],
      field: df.to_log_s(data[df.number].snapshot)
  end

  data
end

#to_bindata_structBinData::Struct

Create [BinData::Struct] to contain and read and write the data belonging to the same definition.

Returns:

  • (BinData::Struct)


1186
1187
1188
1189
1190
1191
1192
# File 'lib/f4r.rb', line 1186

def to_bindata_struct
  opts = {
    endian: endian,
    fields: data_fields.map(&:to_bindata_struct)
  }
  BinData::Struct.new(opts)
end

#write_data(io, record) ⇒ Object

Write data belonging to the same definition.

Parameters:



1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/f4r.rb', line 1166

def write_data(io, record)
  struct = to_bindata_struct

  record[:fields].each do |name, field|
    struct[field[:definition].number] = field[:value]

    Log.encode [self.class, __method__],
      pos: io.pos,
      field: field[:definition].to_log_s(field[:value])
  end

  struct.write(io)
end