Class: Cosmos::Packet

Inherits:
Structure
  • Object
show all
Defined in:
lib/cosmos/packets/packet.rb,
lib/cosmos/utilities/simulated_target.rb,
ext/cosmos/ext/packet/packet.c

Overview

Update Packet for Simulated Targets

Direct Known Subclasses

CcsdsPacket, Table

Constant Summary collapse

VALUE_TYPES =

Valid format types

[:RAW, :CONVERTED, :FORMATTED, :WITH_UNITS]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

Creates a new packet by initalizing the attributes.

Parameters:

  • target_name (String)

    Name of the target this packet is associated with

  • packet_name (String)

    Name of the packet

  • default_endianness (Symbol)
  • description (String)

    Description of the packet

  • buffer (String)

    String buffer to hold the packet data

  • item_class (Class)

    Class used to instantiate items (Must be a subclass of PacketItem)



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/cosmos/ext/packet/packet.c', line 207

static VALUE packet_initialize(int argc, VALUE* argv, VALUE self) {
  VALUE target_name = Qnil;
  VALUE packet_name = Qnil;
  VALUE default_endianness = Qnil;
  VALUE description = Qnil;
  VALUE buffer = Qnil;
  VALUE item_class = Qnil;
  VALUE super_args[3] = {Qnil, Qnil, Qnil};

  switch (argc)
  {
    case 2:
      target_name = argv[0];
      packet_name = argv[1];
      default_endianness = symbol_BIG_ENDIAN;
      description = Qnil;
      buffer = rb_str_new2("");
      item_class = cPacketItem;
      break;
    case 3:
      target_name = argv[0];
      packet_name = argv[1];
      default_endianness = argv[2];
      description = Qnil;
      buffer = rb_str_new2("");
      item_class = cPacketItem;
      break;
    case 4:
      target_name = argv[0];
      packet_name = argv[1];
      default_endianness = argv[2];
      description = argv[3];
      buffer = rb_str_new2("");
      item_class = cPacketItem;
      break;
    case 5:
      target_name = argv[0];
      packet_name = argv[1];
      default_endianness = argv[2];
      description = argv[3];
      buffer = argv[4];
      item_class = cPacketItem;
      break;
    case 6:
      target_name = argv[0];
      packet_name = argv[1];
      default_endianness = argv[2];
      description = argv[3];
      buffer = argv[4];
      item_class = argv[5];
      break;
    default:
      /* Invalid number of arguments given */
      rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..6)", argc);
      break;
  };

  super_args[0] = default_endianness;
  super_args[1] = buffer;
  super_args[2] = item_class;
  rb_call_super(3, super_args);
  target_name_equals(self, target_name);
  packet_name_equals(self, packet_name);
  description_equals(self, description);
  rb_ivar_set(self, id_ivar_received_time, Qnil);
  rb_ivar_set(self, id_ivar_received_count, INT2FIX(0));
  rb_ivar_set(self, id_ivar_id_items, Qnil);
  rb_ivar_set(self, id_ivar_hazardous, Qfalse);
  rb_ivar_set(self, id_ivar_hazardous_description, Qnil);
  rb_ivar_set(self, id_ivar_given_values, Qnil);
  rb_ivar_set(self, id_ivar_limits_items, Qnil);
  rb_ivar_set(self, id_ivar_processors, Qnil);
  rb_ivar_set(self, id_ivar_stale, Qtrue);
  rb_ivar_set(self, id_ivar_limits_change_callback, Qnil);
  rb_ivar_set(self, id_ivar_read_conversion_cache, Qnil);
  rb_ivar_set(self, id_ivar_raw, Qnil);
  rb_ivar_set(self, id_ivar_messages_disabled, Qfalse);
  rb_ivar_set(self, id_ivar_meta, Qnil);
  rb_ivar_set(self, id_ivar_hidden, Qfalse);
  rb_ivar_set(self, id_ivar_disabled, Qfalse);

  return self;
}

Instance Attribute Details

#descriptionString (readonly)

Returns Description of the packet.

Returns:

  • (String)

    Description of the packet



30
31
32
# File 'lib/cosmos/packets/packet.rb', line 30

def description
  @description
end

#disabledBoolean

Returns Whether or not this is a ‘disabled’ packet.

Returns:

  • (Boolean)

    Whether or not this is a 'disabled' packet



60
61
62
# File 'lib/cosmos/packets/packet.rb', line 60

def disabled
  @disabled
end

#given_valuesHash<Item Name, Value>

Containst the values given by the user for a command (distinguished from defaults) These values should be used within command conversions if present because the order that values are written into the actual packet can vary

Returns:

  • (Hash<Item Name, Value>)

    Given values when constructing the packet



48
49
50
# File 'lib/cosmos/packets/packet.rb', line 48

def given_values
  @given_values
end

#hazardousBoolean

Returns Flag indicating if the packet is hazardous (typically for commands).

Returns:

  • (Boolean)

    Flag indicating if the packet is hazardous (typically for commands)



39
40
41
# File 'lib/cosmos/packets/packet.rb', line 39

def hazardous
  @hazardous
end

#hazardous_descriptionString

Returns Description of why the packet is hazardous.

Returns:

  • (String)

    Description of why the packet is hazardous



42
43
44
# File 'lib/cosmos/packets/packet.rb', line 42

def hazardous_description
  @hazardous_description
end

#hiddenBoolean

Returns Whether or not this is a ‘hidden’ packet.

Returns:

  • (Boolean)

    Whether or not this is a 'hidden' packet



57
58
59
# File 'lib/cosmos/packets/packet.rb', line 57

def hidden
  @hidden
end

#messages_disabledBoolean

Returns Whether or not messages should be printed for this packet.

Returns:

  • (Boolean)

    Whether or not messages should be printed for this packet



63
64
65
# File 'lib/cosmos/packets/packet.rb', line 63

def messages_disabled
  @messages_disabled
end

#packet_nameString (readonly)

Returns Name of the packet.

Returns:

  • (String)

    Name of the packet



27
28
29
# File 'lib/cosmos/packets/packet.rb', line 27

def packet_name
  @packet_name
end

#packet_rateObject

Returns the value of attribute packet_rate.



19
20
21
# File 'lib/cosmos/utilities/simulated_target.rb', line 19

def packet_rate
  @packet_rate
end

#rawBoolean

Returns Whether or not this is a ‘raw’ packet.

Returns:

  • (Boolean)

    Whether or not this is a 'raw' packet



54
55
56
# File 'lib/cosmos/packets/packet.rb', line 54

def raw
  @raw
end

#received_countInteger (readonly)

Returns Number of times the packet has been received.

Returns:

  • (Integer)

    Number of times the packet has been received



36
37
38
# File 'lib/cosmos/packets/packet.rb', line 36

def received_count
  @received_count
end

#received_timeTime (readonly)

Returns Time at which the packet was received.

Returns:

  • (Time)

    Time at which the packet was received



33
34
35
# File 'lib/cosmos/packets/packet.rb', line 33

def received_time
  @received_time
end

#staleBoolean (readonly)

Returns Flag indicating if the packet is stale (hasn’t been received recently).

Returns:

  • (Boolean)

    Flag indicating if the packet is stale (hasn't been received recently)



51
52
53
# File 'lib/cosmos/packets/packet.rb', line 51

def stale
  @stale
end

#target_nameString (readonly)

Returns Name of the target this packet is associated with.

Returns:

  • (String)

    Name of the target this packet is associated with



24
25
26
# File 'lib/cosmos/packets/packet.rb', line 24

def target_name
  @target_name
end

Instance Method Details

#append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ Object

Define an item at the end of the packet. This creates a new instance of the item_class as given in the constructor and adds it to the items hash. It also resizes the buffer to accomodate the new item.

Parameters:

  • name (String)

    Name of the item. Used by the items hash to retrieve the item.

  • bit_size (Integer)

    Bit size of the item in the raw buffer

  • data_type (Symbol)

    Type of data contained by the item. This is dependant on the item_class but by default see StructureItem.

  • array_size (Integer) (defaults to: nil)

    Set to a non nil value if the item is to represented as an array.

  • endianness (Symbol) (defaults to: @default_endianness)

    Endianness of this item. By default the endianness as set in the constructure is used.

  • overflow (Symbol) (defaults to: :ERROR)

    How to handle value overflows. This is dependant on the item_class but by default see StructureItem.

  • format_string (String) (defaults to: nil)

    String to pass to Kernel#sprintf

  • read_conversion (Conversion) (defaults to: nil)

    Conversion to apply when reading the item from the packet buffer

  • write_conversion (Conversion) (defaults to: nil)

    Conversion to apply before writing the item to the packet buffer

  • id_value (Object) (defaults to: nil)

    Set to something other than nil to indicate that this item should be used to identify a buffer as this packet. The id_value should make sense according to the data_type.



240
241
242
243
# File 'lib/cosmos/packets/packet.rb', line 240

def append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil)
  item = super(name, bit_size, data_type, array_size, endianness, overflow)
  packet_define_item(item, format_string, read_conversion, write_conversion, id_value)
end

#buffer=(buffer) ⇒ Object

Set the buffer to be used by the structure. The buffer is copied and thus further modifications to the buffer have no effect on the structure items.

Parameters:

  • buffer (String)

    Buffer of data to back the stucture items



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cosmos/packets/packet.rb', line 80

def buffer=(buffer)
  synchronize() do
    begin
      internal_buffer_equals(buffer)
    rescue RuntimeError
      Logger.instance.error "#{@target_name} #{@packet_name} received with actual packet length of #{buffer.length} but defined length of #{@defined_length}"
    end
    @read_conversion_cache.clear if @read_conversion_cache
    process()
  end
end

#check_limits(limits_set = :DEFAULT, ignore_persistence = false) ⇒ Object

Check all the items in the packet against their defined limits. Update their internal limits state and persistence and call the limits_change_callback as necessary.

Parameters:

  • limits_set (Symbol) (defaults to: :DEFAULT)

    Which limits set to check the item values against.

  • ignore_persistence (Boolean) (defaults to: false)

    Whether to ignore persistence when checking for out of limits



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/cosmos/packets/packet.rb', line 517

def check_limits(limits_set = :DEFAULT, ignore_persistence = false)
  # If check_limits is being called, then a new packet has arrived and
  # this packet is no longer stale
  if @stale
    @stale = false
    set_all_limits_states(nil)
  end

  return unless @limits_items
  @limits_items.each do |item|
    # Verify limits monitoring is enabled for this item
    if item.limits.enabled
      value = read_item(item)

      # Handle state monitoring and value monitoring differently
      if item.states
        handle_limits_states(item, value)
      elsif item.limits.values
        handle_limits_values(item, value, limits_set, ignore_persistence)
      end
    end
  end
end

#clonePacket Also known as: dup

Make a light weight clone of this packet. This only creates a new buffer of data and clones the processors. The defined packet items are the same.

Returns:

  • (Packet)

    A copy of the current packet with a new underlying buffer of data and processors



569
570
571
572
573
574
575
576
577
# File 'lib/cosmos/packets/packet.rb', line 569

def clone
  packet = super()
  if packet.instance_variable_get("@processors")
    packet.processors.each do |processor_name, processor|
      packet.processors[processor_name] = processor.clone
    end
  end
  packet
end

#define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ Object

Define an item in the packet. This creates a new instance of the item_class as given in the constructor and adds it to the items hash. It also resizes the buffer to accomodate the new item.

Parameters:

  • name (String)

    Name of the item. Used by the items hash to retrieve the item.

  • bit_offset (Integer)

    Bit offset of the item in the raw buffer

  • bit_size (Integer)

    Bit size of the item in the raw buffer

  • data_type (Symbol)

    Type of data contained by the item. This is dependant on the item_class but by default see StructureItem.

  • array_size (Integer) (defaults to: nil)

    Set to a non nil value if the item is to represented as an array.

  • endianness (Symbol) (defaults to: @default_endianness)

    Endianness of this item. By default the endianness as set in the constructure is used.

  • overflow (Symbol) (defaults to: :ERROR)

    How to handle value overflows. This is dependant on the item_class but by default see StructureItem.

  • format_string (String) (defaults to: nil)

    String to pass to Kernel#sprintf

  • read_conversion (Conversion) (defaults to: nil)

    Conversion to apply when reading the item from the packet buffer

  • write_conversion (Conversion) (defaults to: nil)

    Conversion to apply before writing the item to the packet buffer

  • id_value (Object) (defaults to: nil)

    Set to something other than nil to indicate that this item should be used to identify a buffer as this packet. The id_value should make sense according to the data_type.



221
222
223
224
# File 'lib/cosmos/packets/packet.rb', line 221

def define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil)
  item = super(name, bit_offset, bit_size, data_type, array_size, endianness, overflow)
  packet_define_item(item, format_string, read_conversion, write_conversion, id_value)
end

#description=(description) ⇒ Object (readonly)

Sets the description of the packet

Parameters:

  • description (String)

    Description of the packet



158
159
160
161
162
163
164
165
166
167
168
# File 'ext/cosmos/ext/packet/packet.c', line 158

static VALUE description_equals(VALUE self, VALUE description) {
  if (RTEST(description)) {
    if (rb_funcall(description, id_method_class, 0) != rb_cString) {
      rb_raise(rb_eArgError, "description must be a String but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(description, id_method_class, 0), id_method_to_s, 0)));
    }
    rb_ivar_set(self, id_ivar_description, rb_funcall(rb_funcall(description, id_method_clone, 0), id_method_freeze, 0));
  } else {
    rb_ivar_set(self, id_ivar_description, Qnil);
  }
  return rb_ivar_get(self, id_ivar_description);
}

#disable_limits(name) ⇒ Object

Disable limits on an item by name

Parameters:

  • name (String)

    Name of the item to disable limits



464
465
466
467
468
469
470
471
472
# File 'lib/cosmos/packets/packet.rb', line 464

def disable_limits(name)
  item = get_item(name)
  item.limits.enabled = false
  unless item.limits.state == :STALE
    old_limits_state = item.limits.state
    item.limits.state = nil
    @limits_change_callback.call(self, item, old_limits_state, nil, false) if @limits_change_callback
  end
end

#enable_limits(name) ⇒ Object

Enable limits on an item by name

Parameters:

  • name (String)

    Name of the item to enable limits



457
458
459
# File 'lib/cosmos/packets/packet.rb', line 457

def enable_limits(name)
  get_item(name).limits.enabled = true
end

#formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer) ⇒ String

Create a string that shows the name and value of each item in the packet

Parameters:

  • value_type (Symbol) (defaults to: :CONVERTED)

    How to convert the item before returning it. Must be one of VALUE_TYPES

  • indent (Integer) (defaults to: 0)

    Amount to indent before printing the item name

  • buffer (String) (defaults to: @buffer)

    The binary buffer to write the value to

Returns:

  • (String)

    String formatted with all the item names and values



427
428
429
# File 'lib/cosmos/packets/packet.rb', line 427

def formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer)
  return super(value_type, indent, buffer)
end

#get_item(name) ⇒ StructureItem

Returns StructureItem or one of its subclasses.

Parameters:

  • name (String)

    Name of the item to look up in the items Hash

Returns:



246
247
248
249
250
# File 'lib/cosmos/packets/packet.rb', line 246

def get_item(name)
  super(name)
rescue ArgumentError
  raise "Packet item '#{@target_name} #{@packet_name} #{name.upcase}' does not exist"
end

#id_itemsArray<PacketItem>

Id items are used by the identify? method to determine if a raw buffer of data represents this packet.

Returns:



171
172
173
# File 'lib/cosmos/packets/packet.rb', line 171

def id_items
  @id_items ||= []
end

#identified?TrueClass or FalseClass

Indicates if the packet has been identified

Returns:



193
194
195
# File 'lib/cosmos/packets/packet.rb', line 193

def identified?
  !@target_name.nil? && !@packet_name.nil?
end

#identify?(buffer) ⇒ Boolean

Tries to identify if a buffer represents the currently defined packet. It does this by iterating over all the packet items that were created with an ID value and checking whether that ID value is present at the correct location in the buffer.

Incorrectly sized buffers will still positively identify if there is enough data to match the ID values. This is to allow incorrectly sized packets to still be processed as well as possible given the incorrectly sized data.

Parameters:

  • buffer (String)

    Raw buffer of binary data

Returns:

  • (Boolean)

    Whether or not the buffer of data is this packet



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
119
120
121
# File 'ext/cosmos/ext/packet/packet.c', line 92

static VALUE identify(VALUE self, VALUE buffer)
{
  VALUE id_items = rb_ivar_get(self, id_ivar_id_items);
  VALUE item = Qnil;
  VALUE id_value = Qnil;
  VALUE raw_value = Qnil;
  long id_items_length = 0;
  int index = 0;

  if (!RTEST(buffer)) {
    return Qfalse;
  }

  if (!RTEST(id_items)) {
    return Qtrue;
  }

  id_items_length = RARRAY_LEN(id_items);

  for (index = 0; index < id_items_length; index++) {
    item = rb_ary_entry(id_items, index);
    id_value = rb_ivar_get(item, id_ivar_id_value);
    raw_value = protected_read_item_internal(self, item, buffer);
    if (!rb_eql(id_value, raw_value)) {
      return Qfalse;
    }
  }

  return Qtrue;
}

#limits_change_callback=(limits_change_callback) ⇒ Object

Sets the callback object called when a limits state changes

Parameters:

  • limits_change_callback (#call)

    Object must respond to the call method and take the following arguments: packet (Packet), item (PacketItem), old_limits_state (Symbol), item_value (Object), log_change (Boolean). The current item state can be found by querying the item object: item.limits.state.



159
160
161
162
163
164
165
166
# File 'lib/cosmos/packets/packet.rb', line 159

def limits_change_callback=(limits_change_callback)
  if limits_change_callback
    raise ArgumentError, "limits_change_callback must respond to call" unless limits_change_callback.respond_to?(:call)
    @limits_change_callback = limits_change_callback
  else
    @limits_change_callback = nil
  end
end

#limits_itemsArray<PacketItem>

Returns All items with defined limits.

Returns:



176
177
178
# File 'lib/cosmos/packets/packet.rb', line 176

def limits_items
  @limits_items ||= []
end

#metaHash<Meta Name, Meta Values>

Returns packet specific metadata

Returns:

  • (Hash<Meta Name, Meta Values>)


187
188
189
# File 'lib/cosmos/packets/packet.rb', line 187

def meta
  @meta ||= {}
end

#out_of_limitsArray<Array<String, String, String, Symbol>>

Return an array of arrays indicating all items in the packet that are out of limits

[[target name, packet name, item name, item limits state], ...]

Returns:



490
491
492
493
494
495
496
497
498
499
500
# File 'lib/cosmos/packets/packet.rb', line 490

def out_of_limits
  items = []
  return items unless @limits_items
  @limits_items.each do |item|
    if (item.limits.enabled && item.limits.state &&
      PacketItemLimits::OUT_OF_LIMITS_STATES.include?(item.limits.state))
      items << [@target_name, @packet_name, item.name, item.limits.state]
    end
  end
  return items
end

#packet_name=(packet_name) ⇒ Object (readonly)

Sets the packet name. Unidentified packets will have packet name set to nil.

Parameters:

  • packet_name (String)

    Name of the packet



143
144
145
146
147
148
149
150
151
152
153
# File 'ext/cosmos/ext/packet/packet.c', line 143

static VALUE packet_name_equals(VALUE self, VALUE packet_name) {
  if (RTEST(packet_name)) {
    if (rb_funcall(packet_name, id_method_class, 0) != rb_cString) {
      rb_raise(rb_eArgError, "packet_name must be a String but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(packet_name, id_method_class, 0), id_method_to_s, 0)));
    }
    rb_ivar_set(self, id_ivar_packet_name, rb_funcall(rb_funcall(rb_funcall(packet_name, id_method_upcase, 0), id_method_clone, 0), id_method_freeze, 0));
  } else {
    rb_ivar_set(self, id_ivar_packet_name, Qnil);
  }
  return rb_ivar_get(self, id_ivar_packet_name);
}

#processorsHash

Returns Hash of processors associated with this packet.

Returns:

  • (Hash)

    Hash of processors associated with this packet



181
182
183
# File 'lib/cosmos/packets/packet.rb', line 181

def processors
  @processors ||= {}
end

#read(name, value_type = :CONVERTED, buffer = @buffer) ⇒ Object

Read an item in the packet by name

Parameters:

  • name (String)

    Name of the item to read

  • value_type (Symbol) (defaults to: :CONVERTED)

    How to convert the item before returning it. Must be one of VALUE_TYPES

  • buffer (String) (defaults to: @buffer)

    The binary buffer to read the item from

Returns:

  • The value. :FORMATTED and :WITH_UNITS values are always returned as Strings. :RAW values will match their data_type. :CONVERTED values can be any type.



378
379
380
# File 'lib/cosmos/packets/packet.rb', line 378

def read(name, value_type = :CONVERTED, buffer = @buffer)
  return super(name, value_type, buffer)
end

#read_all(value_type = :CONVERTED, buffer = @buffer, top = true) ⇒ Array<Array>

Read all items in the packet into an array of arrays

[[item name, item value], ...]

Parameters:

  • top (See Structure#read_all) (defaults to: true)
  • value_type (Symbol) (defaults to: :CONVERTED)

    How to convert the item before returning it. Must be one of VALUE_TYPES

  • buffer (String) (defaults to: @buffer)

    The binary buffer to write the value to

Returns:

  • (Array<Array>)

    Array of two element arrays containing the item name as element 0 and item value as element 1.



399
400
401
# File 'lib/cosmos/packets/packet.rb', line 399

def read_all(value_type = :CONVERTED, buffer = @buffer, top = true)
  return super(value_type, buffer, top)
end

#read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer) ⇒ Array<String, Object, Symbol|nil>

Read all items in the packet into an array of arrays

[[item name, item value], [item limits state], ...]

Parameters:

  • value_type (Symbol) (defaults to: :CONVERTED)

    How to convert the item before returning it. Must be one of VALUE_TYPES

  • buffer (String) (defaults to: @buffer)

    The binary buffer to write the value to

Returns:

  • (Array<String, Object, Symbol|nil>)

    Returns an Array consisting of [item name, item value, item limits state] where the item limits state can be one of Limits::LIMITS_STATES



411
412
413
414
415
416
417
418
419
# File 'lib/cosmos/packets/packet.rb', line 411

def read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer)
  result = nil
  synchronize_allow_reads(true) do
    result = read_all(value_type, buffer, false).map! do |array|
      array << @items[array[0]].limits.state
    end
  end
  return result
end

#read_item(item, value_type = :CONVERTED, buffer = @buffer) ⇒ Object

Read an item in the packet

Parameters:

  • item (PacketItem)

    Instance of PacketItem or one of its subclasses

  • value_type (Symbol) (defaults to: :CONVERTED)

    How to convert the item before returning it. Must be one of VALUE_TYPES

  • buffer (String) (defaults to: @buffer)

    The binary buffer to read the item from

Returns:

  • The value. :FORMATTED and :WITH_UNITS values are always returned as Strings. :RAW values will match their data_type. :CONVERTED values can be any type.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/cosmos/packets/packet.rb', line 261

def read_item(item, value_type = :CONVERTED, buffer = @buffer)
  value = super(item, :RAW, buffer)
  case value_type
  when :RAW
    # Done above
  when :CONVERTED, :FORMATTED, :WITH_UNITS
    if item.read_conversion
      using_cached_value = false

      check_cache = buffer.equal?(@buffer)
      if check_cache and @read_conversion_cache
        synchronize_allow_reads() do
          if @read_conversion_cache[item]
            value = @read_conversion_cache[item]
            using_cached_value = true
          end
        end
      end

      unless using_cached_value
        if item.array_size
          value.map! do |val, index|
            item.read_conversion.call(val, self, buffer)
          end
        else
          value = item.read_conversion.call(value, self, buffer)
        end
        if check_cache
          synchronize_allow_reads() do
            @read_conversion_cache ||= {}
            @read_conversion_cache[item] = value
          end
        end
      end
    end

    # Convert from value to state if possible
    if item.states
      if item.array_size
        value = value.map do |val, index|
          if item.states.key(val)
            item.states.key(val)
          else
            apply_format_string_and_units(item, val, value_type)
          end
        end
      else
        state_value = item.states.key(value)
        if state_value
          value = state_value
        else
          value = apply_format_string_and_units(item, value, value_type)
        end
      end
    else
      if item.array_size
        value = value.map do |val, index|
          apply_format_string_and_units(item, val, value_type)
        end
      else
        value = apply_format_string_and_units(item, value, value_type)
      end
    end
  else
    raise ArgumentError, "Unknown value type on read: #{value_type}"
  end
  return value
end

#received_count=(received_count) ⇒ Object (readonly)

Sets the received count of the packet

Parameters:

  • received_count (Integer)

    Number of times this packet has been received



189
190
191
192
193
194
195
# File 'ext/cosmos/ext/packet/packet.c', line 189

static VALUE received_count_equals(VALUE self, VALUE received_count) {
  if (rb_funcall(received_count, id_method_class, 0) != rb_cFixnum) {
    rb_raise(rb_eArgError, "received_count must be a Fixnum but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(received_count, id_method_class, 0), id_method_to_s, 0)));
  }
  rb_ivar_set(self, id_ivar_received_count, received_count);
  return rb_ivar_get(self, id_ivar_received_count);
}

#received_time=(received_time) ⇒ Object (readonly)

Sets the received time of the packet

Parameters:

  • received_time (Time)

    Time this packet was received



173
174
175
176
177
178
179
180
181
182
183
# File 'ext/cosmos/ext/packet/packet.c', line 173

static VALUE received_time_equals(VALUE self, VALUE received_time) {
  if (RTEST(received_time)) {
    if (rb_funcall(received_time, id_method_class, 0) != rb_cTime) {
      rb_raise(rb_eArgError, "received_time must be a Time but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(received_time, id_method_class, 0), id_method_to_s, 0)));
    }
    rb_ivar_set(self, id_ivar_received_time, rb_funcall(rb_funcall(received_time, id_method_clone, 0), id_method_freeze, 0));
  } else {
    rb_ivar_set(self, id_ivar_received_time, Qnil);
  }
  return rb_ivar_get(self, id_ivar_received_time);
}

#resetObject

Reset temporary packet data This includes packet received time, received count, and processor state



550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/cosmos/packets/packet.rb', line 550

def reset
  @received_time = nil
  @received_count = 0
  if @read_conversion_cache
    synchronize() do
      @read_conversion_cache.clear
    end
  end
  return unless @processors
  @processors.each do |processor_name, processor|
    processor.reset
  end
end

#restore_defaults(buffer = @buffer) ⇒ Object

Restore all items in the packet to their default value

Parameters:

  • buffer (String) (defaults to: @buffer)

    Raw buffer of binary data



448
449
450
451
452
# File 'lib/cosmos/packets/packet.rb', line 448

def restore_defaults(buffer = @buffer)
  @sorted_items.each do |item|
    write_item(item, item.default, :CONVERTED, buffer)
  end
end

#set_all_limits_states(state) ⇒ Object

Set the limits state for all items to the given state

Parameters:

  • state (Symbol)

    Must be one of PacketItemLimits::LIMITS_STATES



505
506
507
# File 'lib/cosmos/packets/packet.rb', line 505

def set_all_limits_states(state)
  @sorted_items.each {|item| item.limits.state = state}
end

#set_received_time_fast(received_time) ⇒ Object

Sets the received time of the packet (without cloning)

Parameters:

  • received_time (Time)

    Time this packet was received



117
118
119
120
# File 'lib/cosmos/packets/packet.rb', line 117

def set_received_time_fast(received_time)
  @received_time = received_time
  @received_time.freeze if @received_time
end

#set_staleObject

Sets the overall packet stale state to true and sets each packet item limits state to :STALE.



543
544
545
546
# File 'lib/cosmos/packets/packet.rb', line 543

def set_stale
  @stale = true
  set_all_limits_states(:STALE)
end

#target_name=(target_name) ⇒ Object (readonly)

Sets the target name this packet is associated with. Unidentified packets will have target name set to nil.

Parameters:

  • target_name (String)

    Name of the target this packet is associated with



127
128
129
130
131
132
133
134
135
136
137
# File 'ext/cosmos/ext/packet/packet.c', line 127

static VALUE target_name_equals(VALUE self, VALUE target_name) {
  if (RTEST(target_name)) {
    if (rb_funcall(target_name, id_method_class, 0) != rb_cString) {
      rb_raise(rb_eArgError, "target_name must be a String but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(target_name, id_method_class, 0), id_method_to_s, 0)));
    }
    rb_ivar_set(self, id_ivar_target_name, rb_funcall(rb_funcall(rb_funcall(target_name, id_method_upcase, 0), id_method_clone, 0), id_method_freeze, 0));
  } else {
    rb_ivar_set(self, id_ivar_target_name, Qnil);
  }
  return rb_ivar_get(self, id_ivar_target_name);
}

#update_limits_items_cacheObject

Force the packet to update its knowledge of items with limits. This is an optimization so we don’t have to iterate through all the items when checking for limits.



477
478
479
480
481
482
483
484
# File 'lib/cosmos/packets/packet.rb', line 477

def update_limits_items_cache
  # Collect only the items who have limits values or states and then
  # compact the array to remove all the nil values
  @limits_items = @sorted_items.collect do |item|
    item if item.limits.values || item.state_colors
  end
  @limits_items.compact!
end

#write(name, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object

Write an item in the packet by name

Parameters:

  • name (String)

    Name of the item to write

  • value (Object)

    Value based on the item definition. This could be a string, integer, float, or array of values.

  • value_type (Symbol) (defaults to: :CONVERTED)

    How to convert the item before returning it. Must be one of VALUE_TYPES

  • buffer (String) (defaults to: @buffer)

    The binary buffer to write the value to



388
389
390
# File 'lib/cosmos/packets/packet.rb', line 388

def write(name, value, value_type = :CONVERTED, buffer = @buffer)
  super(name, value, value_type, buffer)
end

#write_item(item, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object

Write an item in the packet

Parameters:

  • item (PacketItem)

    Instance of PacketItem or one of its subclasses

  • value (Object)

    Value based on the item definition. This could be a string, integer, float, or array of values.

  • value_type (Symbol) (defaults to: :CONVERTED)

    How to convert the item before returning it. Must be one of VALUE_TYPES

  • buffer (String) (defaults to: @buffer)

    The binary buffer to write the value to



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/cosmos/packets/packet.rb', line 336

def write_item(item, value, value_type = :CONVERTED, buffer = @buffer)
  if @read_conversion_cache
    synchronize() do
      @read_conversion_cache.clear
    end
  end
  case value_type
  when :RAW
    super(item, value, value_type, buffer)
  when :CONVERTED
    if item.states
      # Convert from state to value if possible
      state_value = item.states[value.to_s.upcase]
      value = state_value if state_value
    end
    if item.write_conversion
      value = item.write_conversion.call(value, self, buffer)
    else
      raise "Cannot write DERIVED item without a write conversion" if item.data_type == :DERIVED
    end
    begin
      super(item, value, :RAW, buffer) unless item.data_type == :DERIVED
    rescue ArgumentError => err
      if item.states and String === value and err.message =~ /invalid value for/
        raise "Unknown state #{value} for #{item.name}"
      else
        raise err
      end
    end
  when :FORMATTED, :WITH_UNITS
    raise ArgumentError, "Invalid value type on write: #{value_type}"
  else
    raise ArgumentError, "Unknown value type on write: #{value_type}"
  end
end