//============================================================================
/// Summary  : Reson 6046 Commands
/// Filename : Reson6046Commands.h
/// Author   : Reed Christeson, reed@bluefinrobotics.com
/// Project  : Reson 6046 Driver
/// Revision : 0.0
/// Created  : 2002.04.15
/// Modified : 2002.04.15
//============================================================================
// From RESON SeaBat 7k DATA FORMAT INTERFACE CONTROL DOCUMENT 
//      Draft Version 0.32


#ifndef _RESON6046PAYLOADCOMMANDS_H
#define _RESON6046PAYLOADCOMMANDS_H

// System Includes
#include <string.h>

// Reson Includes
#include "Reson7KDataTypes.h"

const U32 MESSAGE_WRAPPER_SYNC_PATTERN = 0xFFFF0000;
const U16 PLC_PROTOCOL_VERSION = 4;
const U16 PLC_RECORD_VERSION = 0;

const U16 MB7K_PROTOCOL_VERSION = 5;
const U16 MB7K_RECORD_VERSION = 5;

const U32 DATA_RECORD_FRAME_SYNC_PATTERN = 0x0000FFFF;

const I64 I64_NEG_1 = {0xFFFFFFFF,0xFFFFFFFF};
const I64 I64_0 = {0x00000000, 0x00000000};

// --- A3.8 Sensor ID ---
const U32 MultibeamSonar = 0x10;
const U32 SideScanImagingSonar = 0x20;
const U32 SubbottomProfiler = 0x30;
const U32 ForwardLookingImagingSonar = 0x40;
const U32 SingleBeamEchoSounder = 0x50;
const U32 VideoCamera = 0x60;
const U32 AttitudeSensor = 0x70;

// RGH March 12, 2004 - Assume Phase 2 and later, uncomment fields
//

// TCP and UDP Network Frame (NF)
#pragma pack(1)
struct Reson6046NetworkFrame
{
    U16 version; // 
    U16 offset; // fixed, size of NF
    U32 totalPackets;
    // Uncomment totalRecords for Phase 2 6046 on Payload Computer
    U16 totalRecords;
    // Uncomment transmissionIdentifier for Phase 2 6046 on Payload Computer
    U16 transmissionIdentifier;
    U32 packetSize; // size of bytes in this packet, including header and data.
    // Uncomment totalSize for Phase 2 6046 on Payload Computer
    U32 totalSize; // total size of bytes of all packets in transmission excluding network frame(s).
    U32 sequenceNumber; // sequential packet number (0 to N-1).

  // New fields introduced in version 0.53
  U32 destDeviceIdentifier; // 0=unspecified, 0xFFFFFF=Not used
  U16 destEnumerator; // Invalid if dest identifer id unspecified of not used
  U16 srcEnumerator; // Invalid if src identifer id unspecified of not used
  U32 srcDeviceIdentifier; // 0=unspecified, 0xFFFFFF=Not used
    
};
// DRF follows...

// Data Record Frame (DRF)
#pragma pack(1)
struct Reson6046DataRecordFrame
{
    U16 version; // protocol version of this frame
    U16 offset; // offset in bytes from the start of the sync pattern to the data section.
    U32 syncPattern; // 0x0000FFFF
    U32 size; // Size in bytes of this record from start of version to end of checksum, including data.
    U32 optionalDataOffset; // offset in bytes to optional data from start of record (0 = no data).
    U32 optionalDataIdentifier; // identifier for optional field (0 = no data).
    T7K time7k; // Reson 7K time tag.
    U16 recordVersion; // 0.54
    U32 recordTypeIdentifier; // identifier for record type of embedded data.
    U32 deviceIdentifier; // identifier of the device that this data pertains.

  // New structure introduced in version 0.53
  U32 systemEnumerator; // system enumerator for identical systems in one installation (0 - N).
  U32 sequentialRecordCounter; // sequential record counter.
  U16 flags; // bit field, bit 1 = valid checksum.
             //            bit 2 = fragmentation (0=no, 1=yes)
  U16 reserved2; // reserved.
  U32 reserved3; // reserved. used as 7500 remote control command id

  U32 totalRecords; // Total records in fragmented record set if flag is set
  U32 fragmentNumber; // Fragment number if flag is set

    // DYNAMIC DATA SECTION
  U32 checksum; // As off 2002.04.17, not used by reson.
};

// Record Type # 11000
struct Reson6046PayloadControllerCommand
{
    I32 sensorIndex; // ICD says U32 but also says value of -1 means PLC
    U32 commandID;
    U32 action; // this is U8 in the ICD V0.27
    U32 commandBytesToFollow;
    //Ascii Command Data Follows;
};

// Record Type # 11001
struct Reson6046PayloadControllerCommandAcknowledge
{
    U32 commandID;
    I32 sensorIndex;  // Matches sensorIndex in 11000
    U32 commandStatus;
};

// Record Type # 11002
struct Reson6046PayloadControllerAlarmOrStatus
{
    I32 messageType;
    I32 mode;
    I32 messageID;
    U32 messageBytesToFollow;
    // Ascii Message Follows
};

// --- A3.5 Message Wrapper Definition ---   
struct Reson6046MessageWrapper
{
    U32 startSync;    // 0xFFFF0000
    U32 messageType;  // Describes the type of message.
    U32 messageID;    // Identification numbers.  Explicit numbers for each message type.
    U32 messageSize;
    // message N bytes, N must be a multiple of 8 bytes
    U32 checksum;
    U32 spare; // Spare field to maintain byte alignment.
};

// --- A3.7 Acknowledge Message ---
struct Reson6046AcknowledgeMessage
{
    F64 timeTag;     // Indicates when message was received.
    U32 eventNumber; // 0 = invalid message. Event numbers > 0 indicates valid messages.
    U32 spare;       // Spare field to maintain byte alignment.
};

// ### A4 Payload Setup ###

// --- A4.1 Payload Setup Message ---


// --- A4.2 Payload Setup Answer Message ---

// ### A5 Payload Mode / Status / BIT ###

// --- A5.1 Payload Mode Message
// Message ID = 0
struct PayloadModeMessage
{
    U32 mode;
    U32 spare;
};

// --- A5.2 Request Payload Status Message ---
// Message ID = 1
// !!! Empty Message !!!

// --- A5.3 Payload Status Message ---
// Message ID = 2
struct PayloadStatusMessages
{
    F64 timeTag;
    U32 eventNumber;
    U32 mode;
    BYTE bit[8];
};



struct Reson6046CommandStructure
{
    struct Reson6046NetworkFrame networkFrame;
    U32 sizeNetworkFrame;
    U32 sizeNetworkFrameAndData;
    struct Reson6046DataRecordFrame dataRecordFrame;
    U32 sizeDataRecordFrame;
    U32 sizeDataRecordFrameAndData;
    struct Reson6046PayloadControllerCommand plcCommandFrame;
    U32 sizePlcCommandFrame;
    U32 sizePlcCommandFrameAndData;
    unsigned char *nfPtr;
    unsigned char *drfPtr;
    unsigned char *plccfPtr;
    unsigned char *responsePtr;
};

/// ******* Multibeam Remote Control
typedef struct tMBRemoteControl
{
  U32 rc_id;
  U32 ticket;
  U64 tracking[2];
} MBRemoteControl;

/// ******* RESON 6046 COMMANDS *******
class Reson6046Commands
{
 public:
    // ***** Enumerations *****
    enum ShowRawDataTypes
    {
	None = 0,
	ShowFrameOnly = 1,
	ShowFrameAndData = 2,
	ShowDataOnly = 3
    };

    // --- A3.6 Message Types ---
    enum Reson6046MessageTypes 
    {
	AcknowledgeMessage = 0,
	PayloadSetup = 1,
	PayloadModeAndStatus = 2,
	SensorControl = 3,
	PayloadTime = 4,
	QualityControl = 5,
	PayloadAlarms = 6,
	PayloadTriggers = 7,
	SensorData = 0x10
    };

    enum Reson6046PayloadMessageIDs 
    {
	PayloadModeMessage = 0,
	RequestPayloadStatusMessage = 1,
	PayloadStatusMessage = 2 
    };
    
    // Page 10-11 ICD Reson 7K v0.35.
    enum Reson6046RecordTypeIdentifiers 
    {
	//...
	PayloadControllerCommand = 11000,
	PayloadControllerCommandAck = 11001,
	PayloadControllerAlarmAndStatus = 11002 
	//...
    };

    // Page 7 of 19 6046 Software Spec. 2002.05.01
    enum Reson6046PLCRemoteCommands
    {
	PLC_Command_Generic = 0, // %s
	PLC_Command_Logging = 1, // %d
	PLC_Command_Switch = 2,  // %s
	PLC_Command_Path = 3,
	PLC_Command_Version = 4,
	PLC_Command_Load = 5,
	PLC_Command_Save = 6,
	PLC_Command_Subscribe = 7,
	PLC_Command_Unsubscribe = 8,
	PLC_Command_Alarm = 9,
	PLC_Command_Status = 10,
	PLC_Command_Verbose = 11,
	PLC_Command_Reset = 12,
	PLC_Command_Shutdown = 13,
	PLC_Command_Modules = 14,
	PLC_Command_RunList = 15,
	PLC_Command_Add = 16,
	PLC_Command_Remove = 17,
	PLC_Command_Reserved1 = 18,
	PLC_Command_Reserved2 = 19,
	PLC_Command_TimeSync = 20,
	PLC_Command_Health = 21
    };

    enum Reson6046PLCRemoteCommandActions
    {
	PLC_Action_Unspecified = 0,
	PLC_Action_Set = 1,
	PLC_Action_Get = 2
    };

    // Page 63 ICD Reson 7K v0.35
    enum Reson6046DeviceIdentifiers
    {
	//...
	BluefinVehicleController = 11100
	//...
    };

    enum Reson6046SensorIndex
    {
	SensorIndexPLC = -1,
	//...
	SensorIndexEdgeTech = 11000,
	SensorIndexBluefin = 11100
	//...
    };


 private:
};

#endif
