Program Listing for File UDPHeader.h

Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/ska-telescope-ska-pst-recv/checkouts/latest/src/ska/pst/recv/formats/UDPHeader.h)

/*
 * Copyright 2023 Square Kilometre Array Observatory
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <cinttypes>
#include <string>

#include "ska/pst/common/utils/Time.h"
#include "ska/pst/recv/formats/PacketStructure.h"
#include "ska/pst/recv/formats/UDPFormat.h"

#ifndef SKA_PST_RECV_FORMATS_UDPHeader_h
#define SKA_PST_RECV_FORMATS_UDPHeader_h

namespace ska::pst::recv {

  enum FailureType {
    None,
    BadMagicWord,
    BadPacketSize,
    BadTransmit,
    MisorderedPacketSequenceNumber,
    BadScanID,
    BadChannelNumber,
    BadTimestamp,
    BadDataRate
  };

  static FailureType get_failure_type(const std::string& name)
  {
    if (name == "MagicWord") { return BadMagicWord; }
    if (name == "PacketSize") { return BadPacketSize; }
    if (name == "Transmit") { return BadTransmit; }
    if (name == "MisorderedPSN") { return MisorderedPacketSequenceNumber; }
    if (name == "ScanID") { return BadScanID; }
    if (name == "ChannelNumber") { return BadChannelNumber; }
    if (name == "Timestamp") { return BadTimestamp; }
    if (name == "DataRate") { return BadDataRate; }
    if (name == "None") { return None; }
    return None;
  }

  class UDPHeader
  {
    public:

      UDPHeader();

      ~UDPHeader() = default;

      void configure(const ska::pst::common::AsciiHeader& config, const ska::pst::recv::UDPFormat& format);

      void set_timestamp(ska::pst::common::Time& start_timestamp);

      void set_packet_sequence_number(uint64_t psn);

      void set_scan_id(uint64_t scan_id);

      void set_beam_id(uint64_t beam_id);

      void encode_header(char * buf);

      void increment_packet();

      size_t gen_packet(char * buf);

      size_t gen_packet_failure(char * buf, FailureType failure_type);

    private:

      cbf_psr_header_t header;

      uint32_t start_channel{0};

      uint32_t end_channel{0};

      double attoseconds_per_packet{0};

      uint32_t nchan_per_packet{0};

      uint32_t packet_size{0};

      bool increment_psn_every_packet{false};

      bool configured{false};

  };

} // namespace ska::pst::recv

#endif // SKA_PST_RECV_FORMATS_UDPHeader_h