Class: OpenC3::PreidentifiedProtocol

Inherits:
BurstProtocol show all
Defined in:
lib/openc3/interfaces/protocols/preidentified_protocol.rb

Overview

Delineates packets using the OpenC3 preidentification system

Constant Summary collapse

COSMOS4_STORED_FLAG_MASK =
0x80
COSMOS4_EXTRA_FLAG_MASK =
0x40

Instance Attribute Summary

Attributes inherited from Protocol

#allow_empty_data, #extra, #interface

Instance Method Summary collapse

Methods inherited from BurstProtocol

#handle_sync_pattern, #log_discard, #read_data

Methods inherited from Protocol

#connect_reset, #disconnect_reset, #post_write_interface, #protocol_cmd, #read_data

Constructor Details

#initialize(sync_pattern = nil, max_length = nil, mode = 4, allow_empty_data = nil) ⇒ PreidentifiedProtocol

Returns a new instance of PreidentifiedProtocol.

Parameters:

  • max_length (Integer) (defaults to: nil)

    The maximum allowed value of the length field

  • allow_empty_data (true/false/nil) (defaults to: nil)

    See Protocol#initialize

  • sync_pattern (String) (defaults to: nil)

    String representing a hex number ("0x1234") that will be searched for in the raw data. Bytes encountered before this pattern is found are discarded.



34
35
36
37
38
39
# File 'lib/openc3/interfaces/protocols/preidentified_protocol.rb', line 34

def initialize(sync_pattern = nil, max_length = nil, mode = 4, allow_empty_data = nil)
  super(0, sync_pattern, false, allow_empty_data)
  @max_length = ConfigParser.handle_nil(max_length)
  @max_length = Integer(@max_length) if @max_length
  @mode = Integer(mode)
end

Instance Method Details

#read_packet(packet) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openc3/interfaces/protocols/preidentified_protocol.rb', line 46

def read_packet(packet)
  packet.received_time = @read_received_time
  packet.target_name = @read_target_name
  packet.packet_name = @read_packet_name
  if @mode == 4 # COSMOS4.3+ Protocol
    packet.stored = @read_stored
    if packet.extra and @read_extra
      packet.extra.merge(@read_extra)
    else
      packet.extra = @read_extra
    end
  end
  return packet
end

#resetObject



41
42
43
44
# File 'lib/openc3/interfaces/protocols/preidentified_protocol.rb', line 41

def reset
  super()
  @reduction_state = :START
end

#write_data(data, extra = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/openc3/interfaces/protocols/preidentified_protocol.rb', line 82

def write_data(data, extra = nil)
  data_length = [data.length].pack('N') # UINT32
  data_to_send = ''
  data_to_send << @sync_pattern if @sync_pattern
  if @mode == 4 # COSMOS4.3+ Protocol
    data_to_send << @write_flags
    if @write_extra
      data_to_send << [@write_extra.length].pack('N')
      data_to_send << @write_extra
    end
  end
  data_to_send << @write_time_seconds
  data_to_send << @write_time_microseconds
  data_to_send << @write_target_name.length
  data_to_send << @write_target_name
  data_to_send << @write_packet_name.length
  data_to_send << @write_packet_name
  data_to_send << data_length
  data_to_send << data
  return data_to_send, extra
end

#write_packet(packet) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openc3/interfaces/protocols/preidentified_protocol.rb', line 61

def write_packet(packet)
  received_time = packet.received_time
  received_time = Time.now unless received_time
  @write_time_seconds = [received_time.tv_sec].pack('N') # UINT32
  @write_time_microseconds = [received_time.tv_usec].pack('N') # UINT32
  @write_target_name = packet.target_name
  @write_target_name = 'UNKNOWN' unless @write_target_name
  @write_packet_name = packet.packet_name
  @write_packet_name = 'UNKNOWN' unless @write_packet_name
  if @mode == 4 # COSMOS4.3+ Protocol
    @write_flags = 0
    @write_flags |= COSMOS4_STORED_FLAG_MASK if packet.stored
    @write_extra = nil
    if packet.extra
      @write_flags |= COSMOS4_EXTRA_FLAG_MASK
      @write_extra = packet.extra.as_json(:allow_nan => true).to_json(:allow_nan => true)
    end
  end
  return packet
end