/** \file
 *
 *  Contains the SendPacket struct definition.
 *
 *  Copyright (c) 2019 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#ifndef SOURCE_IO_SENDPACKET_H_
#define SOURCE_IO_SENDPACKET_H_

#include <stdint.h>
#include "logger/Logger.h"

#define SENDPACKET_MAX_SIZE 1032UL
#define SENDPACKET_HEADER_SIZE ( sizeof(uint32_t) + 2 * sizeof(uint16_t) )
#define SENDPACKET_MAX_DATA_SIZE ( SENDPACKET_MAX_SIZE - SENDPACKET_HEADER_SIZE )

/**
SendPacket holds data to be transmitted via modem (e.g. acoustic, satellite).
Includes a header that includes timestamp, sequence#, and packets remaining,
followed by payload data.
NOTE: Packet is serialized for transmission by writing the n bytes in
the object, where n = headerSize + payload data size.
DO NOT add class members before the data_ member unless you intend
them to be serialized.

 */
class SendPacket
{
public:
    /// ---------------------------------------
    /// DATA THAT IS SENT TO THE MODEM
    /// ---------------------------------------
    /// Timestamp of log folder from sendFilename_
    uint32_t timeT_;
    /// Current index of shore file from sendFilename_
    uint16_t index_;
    /// # of packets left to send
    uint16_t packetsLeft_;
    /// the payload
    uint8_t data_[ SENDPACKET_MAX_DATA_SIZE ];
    /// ---------------------------------------
    /// DATA THAT IS NOT SENT TO THE MODEM
    /// ---------------------------------------
    /// size of data
    size_t dataSize_;
    int debug_;
    /// Name of file currently being sent to shore
    Str sendFilename_;
    /// Size of file currently being sent to shore
    size_t sendFilesize_;

    SendPacket( unsigned int maxDataBytes, Logger *logger )
        : timeT_( 0 ),
          index_( 0 ),
          packetsLeft_( 0 ),
          dataSize_( 0 ),
          debug_( false ),
          sendFilename_( Str::EMPTY_STR ),
          sendFilesize_( 0 )
    {
        // Check that application won't overwrite SendPacket data buffer
        if( maxDataBytes > SENDPACKET_MAX_DATA_SIZE )
        {
            char buf[100];
            sprintf( buf, "maxDataBytes %u exceeds SENDPACKET_MAX_DATA_SIZE %lu",
                     maxDataBytes, SENDPACKET_MAX_DATA_SIZE );

            logger->syslog( Str( buf ), Syslog::CRITICAL );
        }
    }

};

#endif /* SOURCE_IO_SENDPACKET_H_ */
