Method: OpenC3::CcsdsParser#unsegment_packet

Defined in:
lib/openc3/ccsds/ccsds_parser.rb

#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.

Parameters:

  • packet (Packet)

    A CCSDS packet

Returns:

  • (String|nil)

    The reconstituted CCSDS packet buffer or nil if the packet has not yet been fully assembled



67
68
69
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
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 67

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

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

  # Handle each segment
  case @ccsds_packet.read('CcsdsSeqflags')
  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

  end # case raw_sequence_flags
end