Class: OpenC3::PacketParser

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

Direct Known Subclasses

TableParser

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ PacketParser

Returns a new instance of PacketParser.

Parameters:



66
67
68
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 66

def initialize(parser)
  @parser = parser
end

Class Method Details

.check_for_duplicate(type, list, packet) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 101

def self.check_for_duplicate(type, list, packet)
  msg = nil
  if list[packet.target_name]
    if list[packet.target_name][packet.packet_name]
      msg = "#{type} Packet #{packet.target_name} #{packet.packet_name} redefined."
      Logger.instance.warn msg
    end
  end
  msg
end

.check_item_data_types(packet) ⇒ Object

Parameters:

  • packet (Packet)

    Packet to check all default and range items for appropriate data types. Only applicable to COMMAND packets.



55
56
57
58
59
60
61
62
63
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 55

def self.check_item_data_types(packet)
  packet.sorted_items.each do |item|
    item.check_default_and_range_data_types()
  end
rescue
  # Add the target name and packet name to the error message so the user
  # can debug where the error occurred
  raise $!, "#{packet.target_name} #{packet.packet_name} #{$!}", $!.backtrace
end

.finish_create_command(packet, commands, warnings) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 112

def self.finish_create_command(packet, commands, warnings)
  warning = PacketParser.check_for_duplicate('Command', commands, packet)
  warnings << warning if warning
  packet.define_reserved_items()
  commands[packet.target_name] ||= {}
  packet
end

.finish_create_telemetry(packet, telemetry, latest_data, warnings) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 120

def self.finish_create_telemetry(packet, telemetry, latest_data, warnings)
  warning = PacketParser.check_for_duplicate('Telemetry', telemetry, packet)
  warnings << warning if warning
  packet.define_reserved_items()

  unless telemetry[packet.target_name]
    telemetry[packet.target_name] = {}
    latest_data[packet.target_name] = {}
  end
  packet
end

.parse_command(parser, target_name, commands, warnings) ⇒ Object

Parameters:

  • parser (ConfigParser)

    Configuration parser

  • target_name (String)

    The name of the target to create the packet under. If the target name is 'SYSTEM' the keyword parameter will be used instead of this parameter.

  • commands (Hash)

    Hash of the currently defined commands

  • warnings (Array<String>)

    Any warning strings generated while parsing this command will be appended to this array



29
30
31
32
33
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 29

def self.parse_command(parser, target_name, commands, warnings)
  parser = PacketParser.new(parser)
  parser.verify_parameters()
  parser.create_command(target_name, commands, warnings)
end

.parse_telemetry(parser, target_name, telemetry, latest_data, warnings) ⇒ Object

Parameters:

  • parser (ConfigParser)

    Configuration parser

  • target_name (String)

    The name of the target to create the packet under. If the target name is 'SYSTEM' the keyword parameter will be used instead of this parameter.

  • telemetry (Hash)

    Hash of the currently defined telemetry packets

  • latest_data (Hash<String=>Hash<String=>Array(Packet)>>)

    Hash of hashes keyed first by the target name and then by the item name. This results in an array of packets containing that target and item. This structure is used to perform lookups when the packet and item are known but the packet is not.

  • warnings (Array<String>)

    Any warning strings generated while parsing this command will be appended to this array



47
48
49
50
51
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 47

def self.parse_telemetry(parser, target_name, telemetry, latest_data, warnings)
  parser = PacketParser.new(parser)
  parser.verify_parameters()
  parser.create_telemetry(target_name, telemetry, latest_data, warnings)
end

Instance Method Details

#create_command(target_name, commands, warnings) ⇒ Object



76
77
78
79
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 76

def create_command(target_name, commands, warnings)
  packet = create_packet(target_name)
  PacketParser.finish_create_command(packet, commands, warnings)
end

#create_packet(target_name) ⇒ Object

private



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 88

def create_packet(target_name)
  params = @parser.parameters
  target_name = params[0].to_s.upcase if target_name == 'SYSTEM'
  packet_name = params[1].to_s.upcase
  endianness = params[2].to_s.upcase.to_sym
  description = params[3].to_s
  if endianness != :BIG_ENDIAN and endianness != :LITTLE_ENDIAN
    raise @parser.error("Invalid endianness #{params[2]}. Must be BIG_ENDIAN or LITTLE_ENDIAN.", @usage)
  end

  Packet.new(target_name, packet_name, endianness, description)
end

#create_telemetry(target_name, telemetry, latest_data, warnings) ⇒ Object



81
82
83
84
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 81

def create_telemetry(target_name, telemetry, latest_data, warnings)
  packet = create_packet(target_name)
  PacketParser.finish_create_telemetry(packet, telemetry, latest_data, warnings)
end

#verify_parametersObject



70
71
72
73
74
# File 'lib/openc3/packets/parsers/packet_parser.rb', line 70

def verify_parameters
  @usage = "#{@parser.keyword} <TARGET NAME> <PACKET NAME> <ENDIANNESS: BIG_ENDIAN/LITTLE_ENDIAN> <DESCRIPTION (Optional)>"
  @parser.verify_num_parameters(3, 4, @usage)
  @parser.verify_parameter_naming(2) # Packet name is the 2nd parameter
end