Class: OpenC3::PacketItemParser

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/packets/parsers/packet_item_parser.rb

Overview

Parses a packet item definition and creates a new PacketItem

Direct Known Subclasses

TableItemParser

Constant Summary collapse

BIG_ARRAY_SIZE =

This number is a little arbitrary but there are definitely issues at 1 million and you really shouldn’t be doing anything this big anyway

100_000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, warnings) ⇒ PacketItemParser

Returns a new instance of PacketItemParser.

Parameters:

  • parser (ConfigParser)

    Configuration parser

  • warnings (Array<String>)

    Array of warning strings from PacketConfig



44
45
46
47
48
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 44

def initialize(parser, warnings)
  @parser = parser
  @warnings = warnings
  @usage = get_usage()
end

Class Method Details

.parse(parser, packet, cmd_or_tlm, warnings) ⇒ Object

Parameters:

  • parser (ConfigParser)

    Configuration parser

  • packet (Packet)

    The packet the item should be added to

  • cmd_or_tlm (String)

    Whether this is a command or telemetry packet

  • warnings (Array<String>)

    Array of warning strings from PacketConfig



36
37
38
39
40
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 36

def self.parse(parser, packet, cmd_or_tlm, warnings)
  parser = PacketItemParser.new(parser, warnings)
  parser.verify_parameters(cmd_or_tlm)
  parser.create_packet_item(packet, cmd_or_tlm)
end

Instance Method Details

#create_packet_item(packet, cmd_or_tlm) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 65

def create_packet_item(packet, cmd_or_tlm)
  item_name = @parser.parameters[0].upcase
  if packet.items[item_name]
    msg = "#{packet.target_name} #{packet.packet_name} #{item_name} redefined."
    Logger.instance.warn msg
    @warnings << msg
  end
  item = PacketItem.new(item_name,
                        get_bit_offset(),
                        get_bit_size(),
                        get_data_type(),
                        get_endianness(packet),
                        get_array_size(),
                        :ERROR) # overflow
  if cmd_or_tlm == PacketConfig::COMMAND
    item.range = get_range()
    item.default = get_default()
  end
  item.id_value = get_id_value()
  item.description = get_description()
  if append?
    item = packet.append(item)
  else
    item = packet.define(item)
  end
  item
end

#verify_parameters(cmd_or_tlm) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 50

def verify_parameters(cmd_or_tlm)
  if @parser.keyword.include?('ITEM') && cmd_or_tlm == PacketConfig::COMMAND
    raise @parser.error("ITEM types are only valid with TELEMETRY", @usage)
  elsif @parser.keyword.include?('PARAMETER') && cmd_or_tlm == PacketConfig::TELEMETRY
    raise @parser.error("PARAMETER types are only valid with COMMAND", @usage)
  end

  # The usage is formatted with brackets <XXX> around each option so
  # count the number of open brackets to determine the number of options
  max_options = @usage.count("<")
  # The last two options (description and endianness) are optional
  @parser.verify_num_parameters(max_options - 2, max_options, @usage)
  @parser.verify_parameter_naming(1) # Item name is the 1st parameter
end