Class: OpenC3::Protocol

Inherits:
Object show all
Defined in:
lib/openc3/interfaces/protocols/protocol.rb,
ext/openc3/ext/burst_protocol/burst_protocol.c

Overview

Base class for all OpenC3 protocols which defines a framework which must be implemented by a subclass.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allow_empty_data = nil) ⇒ Protocol

to be passed down to later Protocols (instead of returning :STOP). Can be true, false, or nil, where nil is interpreted as true unless the Protocol is the last Protocol of the chain.

Parameters:

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

    Whether or not this protocol will allow an empty string



32
33
34
35
36
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 32

def initialize(allow_empty_data = nil)
  @interface = nil
  @allow_empty_data = ConfigParser.handle_true_false_nil(allow_empty_data)
  reset()
end

Instance Attribute Details

#allow_empty_data ⇒ Object

Returns the value of attribute allow_empty_data.



26
27
28
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 26

def allow_empty_data
  @allow_empty_data
end

#extra ⇒ Object

Returns the value of attribute extra.



27
28
29
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 27

def extra
  @extra
end

#interface ⇒ Object

Returns the value of attribute interface.



25
26
27
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 25

def interface
  @interface
end

Instance Method Details

#connect_reset ⇒ Object



50
51
52
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 50

def connect_reset
  reset()
end

#disconnect_reset ⇒ Object



54
55
56
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 54

def disconnect_reset
  reset()
end

#post_write_interface(packet, data, extra = nil) ⇒ Object



130
131
132
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 130

def post_write_interface(packet, data, extra = nil)
  return packet, data, extra
end

#protocol_cmd(cmd_name, *cmd_args) ⇒ Object



134
135
136
137
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 134

def protocol_cmd(cmd_name, *cmd_args)
  # Default do nothing - Implemented by subclasses
  return false
end

#read_data(data, extra = nil) ⇒ Object

Ensure we have some data in case this is the only protocol



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 103

def read_data(data, extra = nil)
  if data.length <= 0
    if @allow_empty_data.nil?
      if @interface and @interface.read_protocols[-1] == self
        # Last read interface in chain with auto @allow_empty_data
        return :STOP
      end
    elsif !@allow_empty_data
      # Don't @allow_empty_data means STOP
      return :STOP
    end
  end
  return data, extra
end

#read_details ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 156

def read_details
  result = {'name' => self.class.name.to_s.split("::")[-1]}
  if @read_data_input_time
    result['read_data_input_time'] = @read_data_input_time.getutc.iso8601(6)
  else
    result['read_data_input_time'] = nil
  end
  result['read_data_input'] = @read_data_input
  if @read_data_output_time
    result['read_data_output_time'] = @read_data_output_time.getutc.iso8601(6)
  else
    result['read_data_output_time'] = nil
  end
  result['read_data_output'] = @read_data_output
  return result
end

#read_packet(packet) ⇒ Object



118
119
120
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 118

def read_packet(packet)
  return packet
end

#read_protocol_input_base(data, _extra = nil) ⇒ Object

Called to provide insight into the protocol read_data for the input data



59
60
61
62
63
64
65
66
67
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 59

def read_protocol_input_base(data, _extra = nil)
  if @interface
    if @interface.save_raw_data
      @read_data_input_time = Time.now
      @read_data_input = data.clone
    end
    # Todo: @interface.stream_log_pair.read_log.write(data) if @interface.stream_log_pair
  end
end

#read_protocol_output_base(data, _extra = nil) ⇒ Object

Called to provide insight into the protocol read_data for the output data



70
71
72
73
74
75
76
77
78
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 70

def read_protocol_output_base(data, _extra = nil)
  if @interface
    if @interface.save_raw_data
      @read_data_output_time = Time.now
      @read_data_output = data.clone
    end
    # Todo: @interface.stream_log_pair.read_log.write(data) if @interface.stream_log_pair
  end
end

#reset ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 38

def reset
  @read_data_input_time = nil
  @read_data_input = ''
  @read_data_output_time = nil
  @read_data_output = ''
  @write_data_input_time = nil
  @write_data_input = ''
  @write_data_output_time = nil
  @write_data_output = ''
  @extra = nil
end

#write_data(data, extra = nil) ⇒ Object



126
127
128
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 126

def write_data(data, extra = nil)
  return data, extra
end

#write_details ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 139

def write_details
  result = {'name' => self.class.name.to_s.split("::")[-1]}
  if @write_data_input_time
    result['write_data_input_time'] = @write_data_input_time.getutc.iso8601(6)
  else
    result['write_data_input_time'] = nil
  end
  result['write_data_input'] = @write_data_input
  if @write_data_output_time
    result['write_data_output_time'] = @write_data_output_time.getutc.iso8601(6)
  else
    result['write_data_output_time'] = nil
  end
  result['write_data_output'] = @write_data_output
  return result
end

#write_packet(packet) ⇒ Object



122
123
124
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 122

def write_packet(packet)
  return packet
end

#write_protocol_input_base(data, _extra = nil) ⇒ Object

Called to provide insight into the protocol write_data for the input data



81
82
83
84
85
86
87
88
89
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 81

def write_protocol_input_base(data, _extra = nil)
  if @interface
    if @interface.save_raw_data
      @write_data_input_time = Time.now
      @write_data_input = data.clone
    end
    # Todo: @interface.stream_log_pair.write_log.write(data) if @interface.stream_log_pair
  end
end

#write_protocol_output_base(data, _extra = nil) ⇒ Object

Called to provide insight into the protocol write_data for the output data



92
93
94
95
96
97
98
99
100
# File 'lib/openc3/interfaces/protocols/protocol.rb', line 92

def write_protocol_output_base(data, _extra = nil)
  if @interface
    if @interface.save_raw_data
      @write_data_output_time = Time.now
      @write_data_output = data.clone
    end
    # Todo: @interface.stream_log_pair.write_log.write(data) if @interface.stream_log_pair
  end
end