Class: GdbFlasher::IHex::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/gdbflasher/ihex.rb

Constant Summary collapse

TYPES =
{
  0 => :data,
  1 => :eof,
  4 => :extended_address,
  5 => :start_address
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecord

Returns a new instance of Record.



13
14
15
16
17
18
# File 'lib/gdbflasher/ihex.rb', line 13

def initialize
  @length = nil
  @address = nil
  @type = nil
  @data = nil
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



4
5
6
# File 'lib/gdbflasher/ihex.rb', line 4

def address
  @address
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/gdbflasher/ihex.rb', line 4

def data
  @data
end

#lengthObject

Returns the value of attribute length.



4
5
6
# File 'lib/gdbflasher/ihex.rb', line 4

def length
  @length
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/gdbflasher/ihex.rb', line 4

def type
  @type
end

Class Method Details

.parse(line) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gdbflasher/ihex.rb', line 24

def self.parse(line)
  if line.match(/^:([0-9a-fA-F]{2})+$/).nil?
    raise "Malformed Intel Hex line: #{line}"
  end

  line.slice! 0

  bytes = Array.new(line.length / 2) { |i| line[i * 2..i * 2 + 1].hex }

  record = Record.new
  record.length = bytes[0]
  record.address = (bytes[1] << 8) | bytes[2]
  record.type = TYPES[bytes[3]]
  record.data = bytes[4..-2].pack("C*")

  checksum = (~bytes.reduce(:+) + 1) & 0xFF

  if !record.valid? || record.type.nil? || checksum != 0
    raise "Malformed Intel Hex line: #{line}"
  end

  record
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/gdbflasher/ihex.rb', line 20

def valid?
  @length == @data.length
end