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, packet_config, warnings) ⇒ PacketItemParser

Returns a new instance of PacketItemParser.

Parameters:

  • parser (ConfigParser)

    Configuration parser

  • warnings (Array<String>)

    Array of warning strings from PacketConfig



40
41
42
43
44
45
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 40

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

Class Method Details

.parse(parser, packet_config, 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



32
33
34
35
36
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 32

def self.parse(parser, packet_config, packet, cmd_or_tlm, warnings)
  parser = PacketItemParser.new(parser, packet_config, 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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 75

def create_packet_item(packet, cmd_or_tlm)
  item_name = @parser.parameters[0].upcase
  if Packet::RESERVED_ITEM_NAMES.include?(item_name)
    raise @parser.error("#{item_name} is a reserved item name", @usage)
  end
  if packet.items[item_name]
    msg = "#{packet.target_name} #{packet.packet_name} #{item_name} redefined."
    Logger.instance.warn msg
    @warnings << msg
  end
  if @parser.keyword.include?("STRUCTURE")
    item = PacketItem.new(item_name,
                          get_bit_offset(),
                          get_bit_size(true),
                          :BLOCK,
                          :BIG_ENDIAN,
                          nil,
                          :ERROR) # overflow
  else
    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)
    item.description = get_description()
  end
  if append?
    item = packet.append(item)
  else
    item = packet.define(item)
  end
  if @parser.keyword.include?("STRUCTURE")
    structure = lookup_packet(get_cmd_or_tlm(), get_target_name(), get_packet_name())
    packet.structurize_item(item, structure)
  end
  item
end

#verify_parameters(cmd_or_tlm) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openc3/packets/parsers/packet_item_parser.rb', line 47

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("<")
  if @parser.keyword.include?('STRUCTURE')
    @parser.verify_num_parameters(max_options, max_options, @usage)
  else
    # The last two options (description and endianness) are optional
    @parser.verify_num_parameters(max_options - 2, max_options, @usage)
  end
  @parser.verify_parameter_naming(1) # Item name is the 1st parameter

  # ARRAY items cannot have brackets in their name because brackets are used
  # for array indexing in the UI and would cause confusion
  if @parser.keyword.include?('ARRAY')
    item_name = @parser.parameters[0]
    if item_name.include?('[') || item_name.include?(']')
      raise @parser.error("ARRAY items cannot have brackets in their name: #{item_name}", @usage)
    end
  end
end