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



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

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

Instance Attribute Details

#in_progress_dataString (readonly)

Returns Binary data that has been collected so far towards the completely unsegmented packet. Note: This value is cleared once the entire packet is formed and also cleared due to errors.

Returns:

  • (String)

    Binary data that has been collected so far towards the completely unsegmented packet. Note: This value is cleared once the entire packet is formed and also cleared due to errors.



36
37
38
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 36

def in_progress_data
  @in_progress_data
end

#sequence_countInteger (readonly)

Returns Sequence count of the previously received packet.

Returns:

  • (Integer)

    Sequence count of the previously received packet



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

def sequence_count
  @sequence_count
end

#stateSymbol (readonly)

Returns Indicates if the parser is :READY to start a new series of packets or is :IN_PROGRESS.

Returns:

  • (Symbol)

    Indicates if the parser is :READY to start a new series of packets or is :IN_PROGRESS



31
32
33
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 31

def state
  @state
end

#unsegmented_dataString (readonly)

Returns The most recent successfully unsegmented packet’s data.

Returns:

  • (String)

    The most recent successfully unsegmented packet's data



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

def unsegmented_data
  @unsegmented_data
end

Instance Method Details

#resetObject

Resets internal state to :READY and clears the in_progress_data



53
54
55
56
# File 'lib/openc3/ccsds/ccsds_parser.rb', line 53

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.

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