#define LOOPBACK
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace hdlc_example
{
    class hdlc_tx_error_injection
    {
        const int MAX_TX_RECORDS_TO_TRANSMIT = 8;
        public static void Main(string[] args)
        {
            UInt32 rc;
            UInt64 server = 0;
            UInt64 rxChannel = 0;
            UInt64 txChannel = 0;
            UInt64 rxBuffer = 0;
            UInt64 txBuffer = 0;
            uint txBufferSize = 0;
            uint rxBufferSize = 0;
            IntPtr rxHostBuffer = IntPtr.Zero;
            IntPtr txHostBuffer = IntPtr.Zero;
            IntPtr recPtr = IntPtr.Zero;
            UInt64 rxAcqStatus = 0;
            UInt64 msgCount = 0;
            UInt64 byteCount = 0;
            UInt64 data;
            UInt64 word;
            MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
            {
                data = new UInt16[2048]
            };
            
#if LOCAL
#else
            rc = 
mxfServerConnect(
"192.168.0.1", 
"admin", 
"admin", Convert.ToUInt64(
false), out server);
#endif
            
            if (rc == MAXT_SUCCESS)
            
            if (rc == MAXT_SUCCESS)
            {
                Console.WriteLine("Starting ...");
            }
            
            if (rc == MAXT_SUCCESS)
                rc = 
mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_MULTI_EH, 0, out rxChannel);
            
            if (rc == MAXT_SUCCESS)
                rc = 
mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_MULTI_EH, 0, out txChannel);
            
            if (rc == MAXT_SUCCESS)
            if (rc == MAXT_SUCCESS)
            if (rc == MAXT_SUCCESS)
            
            if (!rc)
            if (!rc)
            
#if LOOPBACK
            if (rc == MAXT_SUCCESS)
#endif
            
            if (rc == MAXT_SUCCESS)
            {
                txBufferSize = (uint)MAX_TX_RECORDS_TO_TRANSMIT * (uint)Marshal.SizeOf(typeof(
MXF_HDLC_DATAREC));
                
                
                if (rc == MAXT_SUCCESS)
                {
                    try
                    {
                        txHostBuffer = Marshal.AllocHGlobal((int)txBufferSize);
                    }
                    catch (OutOfMemoryException)
                    {
                        rc = MAXT_ERROR_MEM;
                    }
                }
            }
            
            if (rc == MAXT_SUCCESS)
            {
                rxBufferSize = (uint)MAX_TX_RECORDS_TO_TRANSMIT * (uint)Marshal.SizeOf(typeof(
MXF_HDLC_DATAREC));
                
                
                if (rc == MAXT_SUCCESS)
                {
                    try
                    {
                        rxHostBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
                    }
                    catch (OutOfMemoryException)
                    {
                        rc = MAXT_ERROR_MEM;
                    }
                }
            }
            
            if (rc == MAXT_SUCCESS)
            
            if (rc == MAXT_SUCCESS)
            if (rc == MAXT_SUCCESS)
            {
                recPtr = txHostBuffer;
                
                for (data = 0; (data < MAX_TX_RECORDS_TO_TRANSMIT) && (rc == MAXT_SUCCESS); data++)
                {
                    rec.
control = (data != 2) ? 0 : MXF_HDLC_TX_REC_CTRL_FCS_NOT_SEND;
                    for (word = 0; word < rec.
dataSize / 2; word++)
 
                    {
                        rec.
data[word] = (UInt16)(0x1111 * data);
                    }
                    Marshal.StructureToPtr(rec, recPtr, false);
                }
            }
            if (rc == MAXT_SUCCESS)
                DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
            if (rc == MAXT_SUCCESS)
            {
                Console.WriteLine("Transmitting ...");
                
            }
            if (rc == MAXT_SUCCESS)
            if (rc == MAXT_SUCCESS)
            {
                Console.WriteLine("Receiving ...");
                
                rc = 
mxfHDLCRxAcqRead(rxBuffer, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxHostBuffer);
            }
            if (rc == MAXT_SUCCESS)
                Console.WriteLine("String received count = {0} ", msgCount);
            if (rc == MAXT_SUCCESS)
            {
                
                DisplayDataArray(msgCount, rxHostBuffer);
            }
            
            if (rc == MAXT_SUCCESS)
            
            if (txChannel)
            if (rxChannel)
            
            if (txBuffer != 0)
            if (rxBuffer != 0)
            if (txHostBuffer != IntPtr.Zero)
                Marshal.FreeHGlobal(txHostBuffer);
            if (rxHostBuffer != IntPtr.Zero)
                Marshal.FreeHGlobal(rxHostBuffer);
            if (rc != MAXT_SUCCESS)
            {
                StringBuilder buffer = new StringBuilder(256);
                    buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
                Console.WriteLine(buffer);
            }
            Console.WriteLine("Terminating ...");
            
            
            Console.WriteLine();
            Console.WriteLine("Press a key to terminate");
            Console.Read();
            return;
        }
        private static UInt32 initHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
        {
            UInt64 device;
            UInt32 rc;
            if (attrib == KMXF_CHANNEL_CLASS)
            {
                if (rc == MAXT_SUCCESS)
                if ((rc == MAXT_SUCCESS) && ((deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI_EH) || (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI)))
                {
                    
                    if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
                    {
                        value = MXF_CLASS_HDLC;
                        return Convert.ToUInt32(true);
                    }
                    else if ((channelIndex == 4) || (channelIndex == deviceInfo.modules[moduleIndex].txCount + 4))
                    {
                        value = MXF_CLASS_CLOCK;
                        return Convert.ToUInt32(true);
                    }
                }
            }
            return Convert.ToUInt32(false);
        }
        private static void DisplayDataArray(UInt64 recNum, IntPtr rec)
        {
            UInt64 iRec,
                   iData;
            IntPtr recPtr = rec;
            UInt32 rc = MAXT_SUCCESS;
            {
                data = new UInt16[2048]
            };
            Console.WriteLine();
            for (iRec = 0; (iRec < recNum) && (rc == MAXT_SUCCESS); iRec++)
            {
                for (iData = 0; iData < p.
dataSize / 2; iData++)
 
                {
                        Console.Write(
"{0:x4} ", p.
data[iData]);
                    else
                        Console.Write(
"{0:x2} {1:x2} ", (byte)p.
data[iData], (byte)(p.
data[iData] >> 8));
                    if ((((iData + 1) % 8) == 0) && (iData + 1 < p.
dataSize / 2))
 
                        Console.Write("\n                               ");
                }
                Console.WriteLine();
                if ((p.
control & MXF_HDLC_RX_REC_CTRL_CLOSING_ERROR) == MXF_HDLC_RX_REC_CTRL_CLOSING_ERROR)
 
                    Console.Write("Closing error ");
                if ((p.
control & MXF_HDLC_RX_REC_CTRL_ABORT) == MXF_HDLC_RX_REC_CTRL_ABORT)
 
                    Console.Write("Abort error ");
                if ((p.
control & MXF_HDLC_RX_REC_CTRL_DECODING_ERROR) == MXF_HDLC_RX_REC_CTRL_DECODING_ERROR)
 
                    Console.Write("Decoding error ");
                if ((p.
control & MXF_HDLC_RX_REC_CTRL_FCS_ERROR) == MXF_HDLC_RX_REC_CTRL_FCS_ERROR)
 
                    Console.Write("FCS error ");
                if ((p.
control & MXF_HDLC_RX_REC_CTRL_FRAMESIZE_ERROR) == MXF_HDLC_RX_REC_CTRL_FRAMESIZE_ERROR)
 
                    Console.Write("Frame Size error ");
                Console.WriteLine();
            }
        }
    }
}