Class: OpenC3::CcsdsParser

Inherits:
Object show all
Defined in:
lib/openc3/ccsds/ccsds_parser.rb

Overview

Unsegments CCSDS packets and perform other CCSDS processing tasks.

Defined Under Namespace

Classes: CcsdsSegmentationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCcsdsParser

Create a new OpenC3::CcsdsPacket and set the state to :READY



48
49
50
51
52
53
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 48

def initialize
  @ccsds_packet = CcsdsPacket.new
  @unsegmented_data = nil
  @sequence_count = nil
  reset()
end

Instance Attribute Details

#in_progress_dataString (readonly)



39
40
41
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 39

def in_progress_data
  @in_progress_data
end

#sequence_countInteger (readonly)



45
46
47
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 45

def sequence_count
  @sequence_count
end

#stateSymbol (readonly)



34
35
36
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 34

def state
  @state
end

#unsegmented_dataString (readonly)



42
43
44
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 42

def unsegmented_data
  @unsegmented_data
end

Instance Method Details

#resetObject

Resets internal state to :READY and clears the in_progress_data



56
57
58
59
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 56

def reset
  @state = :READY
  @in_progress_data = ''
end

#unsegment_packet(packet) ⇒ String|nil

Given a segment of a larger CCSDS packet, stores the data until a complete unsegmented packet can be created. Returns the unsegmented packet data once it is complete, or nil if still in progress. Raises CcsdsParser::CcsdsSegmentationError if a problem is encountered while unsegmenting.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 70

def unsegment_packet(packet)
  @ccsds_packet.buffer = packet.buffer

  previous_sequence_count = @sequence_count
  @sequence_count = @ccsds_packet.read('CcsdsSeqcnt')

  # Handle each segment
  seq_flags = @ccsds_packet.read('CcsdsSeqflags')
  case seq_flags
  when CcsdsPacket::CONTINUATION
    #####################################################
    # Continuation packet - only process if in progress
    #####################################################

    if @state == :IN_PROGRESS
      if @sequence_count == ((previous_sequence_count + 1) % 16384)
        @in_progress_data << @ccsds_packet.read('CcsdsData')
        return nil
      else
        reset()
        raise CcsdsSegmentationError, "Missing packet(s) before continuation packet detected. Current Sequence Count #{@sequence_count}, Previous Sequence Count #{previous_sequence_count}"
      end
    else
      reset()
      raise CcsdsSegmentationError, "Unexpected continuation packet"
    end

  when CcsdsPacket::FIRST
    #######################################################
    # First packet - always process
    #######################################################

    if @state == :IN_PROGRESS
      reset()
      @state = :IN_PROGRESS
      @in_progress_data << @ccsds_packet.buffer
      raise CcsdsSegmentationError, "Unexpected first packet"
    else
      @state = :IN_PROGRESS
      @in_progress_data << @ccsds_packet.buffer
      return nil
    end

  when CcsdsPacket::LAST
    ######################################################################
    # Last packet - only process if in progress
    ######################################################################

    if @state == :IN_PROGRESS
      if @sequence_count == ((previous_sequence_count + 1) % 16384)
        @in_progress_data << @ccsds_packet.read('CcsdsData')
        @unsegmented_data = @in_progress_data
        reset()
        return @unsegmented_data
      else
        reset()
        raise CcsdsSegmentationError, "Missing packet(s) before last packet detected. Current Sequence Count #{@sequence_count}, Previous Sequence Count #{previous_sequence_count}"
      end
    else
      reset()
      raise CcsdsSegmentationError, "Unexpected last packet"
    end

  when CcsdsPacket::STANDALONE
    ############################################################
    # Standalone packet - save and return its data
    ############################################################

    # Update most recent unsegmented data
    @unsegmented_data = @ccsds_packet.buffer

    if @state == :IN_PROGRESS
      reset()
      raise CcsdsSegmentationError, "Unexpected standalone packet"
    else
      reset()
      return @unsegmented_data
    end

  else
    reset()
    raise CcsdsSegmentationError, "Unknown sequence flags #{seq_flags}"
  end
end