Class: OpenC3::SlipProtocol

Inherits:
TerminatedProtocol show all
Defined in:
lib/openc3/interfaces/protocols/slip_protocol.rb

Overview

Usage in plugin.txt:

INTERFACE …

PROTOCOL READ_WRITE SlipProtocol

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, #reset, #write_packet

Methods inherited from Protocol

#connect_reset, #disconnect_reset, #post_write_interface, #protocol_cmd, #read_packet, #reset, #write_packet

Constructor Details

#initialize(start_char = nil, read_strip_characters = true, read_enable_escaping = true, write_enable_escaping = true, end_char = 0xC0, esc_char = 0xDB, esc_end_char = 0xDC, esc_esc_char = 0xDD, allow_empty_data = nil) ⇒ SlipProtocol

Note: Characters are expected to be given as integers

Parameters:

  • start_char (Integer/nil) (defaults to: nil)

    Character to place at the start of frames (Defaults to nil)

  • read_strip_characters (true/false) (defaults to: true)

    Strip off start_char and end_char from reads

  • read_enable_escaping (true/false) (defaults to: true)

    Whether to enable or disable character escaping on reads

  • write_enable_escaping (true/false) (defaults to: true)

    Whether to enable or disable character escaping on writes

  • end_char (Integer) (defaults to: 0xC0)

    Character to place at the end of frames (Defaults to 0xC0)

  • esc_char (Integer) (defaults to: 0xDB)

    Escape character (Defaults to 0xDB)

  • esc_end_char (Integer) (defaults to: 0xDC)

    Character to Escape End character (Defaults to 0xDC)

  • esc_esc_char (Integer) (defaults to: 0xDD)

    Character to Escape Escape character (Defaults to 0xDD)

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

    See Protocol#initialize



46
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/openc3/interfaces/protocols/slip_protocol.rb', line 46

def initialize(
  start_char = nil,
  read_strip_characters = true,
  read_enable_escaping = true,
  write_enable_escaping = true,
  end_char = 0xC0,
  esc_char = 0xDB,
  esc_end_char = 0xDC,
  esc_esc_char = 0xDD,
  allow_empty_data = nil)

  @start_char = ConfigParser.handle_nil(start_char)
  @start_char = [Integer(start_char)].pack('C') if @start_char
  @end_char = [Integer(end_char)].pack('C')
  @esc_char = [Integer(esc_char)].pack('C')
  @esc_end_char = [Integer(esc_end_char)].pack('C')
  @esc_esc_char = [Integer(esc_esc_char)].pack('C')
  @replace_end = @esc_char + @esc_end_char
  @replace_esc = @esc_char + @esc_esc_char
  @read_strip_characters = ConfigParser.handle_true_false(read_strip_characters)
  raise "read_strip_characters must be true or false" if @read_strip_characters != true and @read_strip_characters != false
  @read_enable_escaping = ConfigParser.handle_true_false(read_enable_escaping)
  raise "read_enable_escaping must be true or false" if @read_enable_escaping != true and @read_enable_escaping != false
  @write_enable_escaping = ConfigParser.handle_true_false(write_enable_escaping)
  raise "write_enable_escaping must be true or false" if @write_enable_escaping != true and @write_enable_escaping != false

  strip_read_termination = false
  discard_leading_bytes = 0
  if @start_char
    sync_pattern = sprintf("%0X", Integer(start_char))
  else
    sync_pattern = nil
  end
  fill_fields = false # Handled in write_data below

  super(
    "", # Write termination handled in write_data below
    sprintf("%0X", Integer(end_char)), # Expects Hex Character String
    strip_read_termination,
    discard_leading_bytes,
    sync_pattern,
    fill_fields,
    allow_empty_data
  )
end

Instance Method Details

#read_data(data, extra = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/openc3/interfaces/protocols/slip_protocol.rb', line 92

def read_data(data, extra = nil)
  data, extra = super(data, extra)
  return data, extra if data.length <= 0 or Symbol === data

  if @read_strip_characters
    if @start_char
      data = data[1..-1]
    end
    data = data[0..-2]
  end

  if @read_enable_escaping
    data = data.gsub(@replace_end, @end_char).gsub(@replace_esc, @esc_char)
  end

  return data, extra
end

#reduce_to_single_packetObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/openc3/interfaces/protocols/slip_protocol.rb', line 126

def reduce_to_single_packet
  return :STOP if @data.length <= 0
  if @start_char
    index = @data[1..-1].index(@read_termination_characters)
    index = index + 1 if index
  else
    index = @data.index(@read_termination_characters)
  end

  # Reduce to packet data and setup current_data for next packet
  if index
    if index > 0
      packet_data = @data[0..(index + @read_termination_characters.length - 1)]
    else # @data begins with the termination characters
      packet_data = @data[0..(@read_termination_characters.length - 1)]
    end
    @data.replace(@data[(index + @read_termination_characters.length)..-1])
    return packet_data, @extra
  else
    return :STOP
  end
end

#write_data(data, extra = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/openc3/interfaces/protocols/slip_protocol.rb', line 110

def write_data(data, extra = nil)
  # Intentionally not calling super()

  if @write_enable_escaping
    data = data.gsub(@esc_char, @replace_esc).gsub(@end_char, @replace_end)
  end

  if @start_char
    data = @start_char + data
  end

  data << @end_char

  return data, extra
end