MX Foundation 4
ar708_event_handler.cs
/************************************************************************************************
//
// File:
// ar708_buffer_threshold.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example illustrates how to manage buffer thresholds with asynchronous events.
// The demo will transmit and record the messages using asynchronous
// events.
//
// Hardware Requirements:
// - MAXT FlexMulti-1553 or FlexMulti-629 with A708 option.
// - Loopback between TX and RX channels if internal loopback is not used.
//
**************************************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar708_example
{
class ar708_event_handler
{
private const int MAX_TX_RECORDS_TO_TRANSMIT = 6;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
var txBuffer = new UInt64[1];
var rec = new MXF_A708_DATAREC();
UInt64 asyncEvent = 0;
MXF_ASYNCEVENT_HANDLER asyncEventHandler = AsyncEventHandler;
var eventInfo = new MXF_ASYNCEVENT_CONDITION[1];
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 moduleCount = 0, channelCount = 0;
uint txBufferSize;
UInt64 data, word;
// Connect to services and initialize environment
#if (LOCAL)
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt64(false), out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", Convert.ToUInt64(false), out server);
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine();
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
// Initialize the server
Console.WriteLine();
Console.WriteLine("Starting");
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
//Get Module handle
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A708_EH, 1, out moduleCount, module);
// If module not found, return an error
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Activates the ARINC 708 module (because the MIL-STD-1553 is by default activated, except on FM1553-2)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_A708_MODULE_ACTIVE, Convert.ToUInt64(true));
if (rc == MAXT_ERROR_NOT_SUPPORTED)
rc = MAXT_SUCCESS;
//Get handle of first ARINC 708 RX Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A708, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, rxChannel);
//Get handle of first A708 Tx Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A708, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, txChannel);
// Enable loopback
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A708_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
//Allocates 4KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 4 * 1024;
//Allocates Tx Aperiodic static buffer for HIGH priority queue
rc = mxfTxAperiodicBufferAlloc(txChannel[0], MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, out txBuffer[0], IntPtr.Zero);
//Host buffer allocation
if (rc == MAXT_SUCCESS)
{
try
{
txHostBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
//Sets timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
//Registers the callback handler service function
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, IntPtr.Zero, out asyncEvent);
if (rc == MAXT_SUCCESS)
{
eventInfo[0].condID = MXF_ASYNCEVENT_COND_RX_ERROR;
eventInfo[0].condition.rxErr.channel = rxChannel[0];
}
//Enables RX Error Asynchronous Event
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(true), 1, eventInfo);
if (rc == MAXT_SUCCESS)
{
IntPtr recPtr = txHostBuffer;
rec.data = new UInt16[128];
//Prepare records to send with an error injection in transmission for the 3rd record
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = 0;
if (data == 2)
{
rec.control = (uint)MXF_A708_TX_REC_CTRL_MANCHESTERBIT_ERROR; //The 3rd record contains manchester error at bit 1000
rec.manchesterBitErr = 1000;
}
else
{
rec.control = 0;
rec.manchesterBitErr = 0;
}
rec.repeatCount = 1;
rec.dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
for (word = 0; word < rec.dataSize / 2; word++)
{
switch (word)
{
case 0:
rec.data[word] = 055; // Label
break;
case 1:
rec.data[word] = (ushort)((data != 5) ? 0x0000 : 0x0004); // Cooling Fault detected will fire trigger
break;
case 2:
case 3:
rec.data[word] = 0x0000;
break;
default:
rec.data[word] = (UInt16)(0x0101 * data);
break;
}
}
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfA708NextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting...");
rc = mxfA708TxAperiodicWrite(txBuffer[0], MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(2000);
//Disable Conditions
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(false), 1, eventInfo);
if (asyncEvent != 0)
// Catch any previous failing function
if (rc != MAXT_SUCCESS)
{
StringBuilder buffer = new StringBuilder(256);
if (mxfSystemErrorStringGet(server, rc, 256, buffer) != MAXT_SUCCESS)
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
Console.WriteLine(buffer);
}
//Frees all buffers
if (txBuffer[0] != 0)
// Terminate
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
//****************************************************************************************************************
// Asynchronous Event Handler
//****************************************************************************************************************
private static UInt32 AsyncEventHandler(UInt64 asyncEvent, IntPtr context)
{
UInt64 channel;
UInt64 i, maxCount = 64, pendingCount, status;
UInt32 rc = MAXT_SUCCESS;
UInt64 dev, mod, port;
// Builds the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, maxCount, out pendingCount, pendingList);
for (i = 0; i < pendingCount && (rc == MAXT_SUCCESS); i++)
{
switch (pendingList[i].condID)
{
// A receive error was detected
case MXF_ASYNCEVENT_COND_RX_ERROR:
channel = pendingList[i].condition.rxErr.channel;
status = pendingList[i].condition.rxErr.status;
rc = mxfChannelLocationGet(channel, out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Status {0} received on channel {1}.{2}.{3}: ", status, dev, mod, port);
if (status != 0 && MXF_A708_RX_REC_CTRL_MANCHESTER_ERROR == Convert.ToUInt64(true))
Console.WriteLine("manchester error ");
if (status != 0 && MXF_A708_RX_REC_CTRL_BITSCNT_ERROR == Convert.ToUInt64(true))
Console.WriteLine("bit count error ");
if (status != 0 && MXF_A708_RX_REC_CTRL_TOOLONGWORD_ERROR == Convert.ToUInt64(true))
Console.WriteLine("message too long error ");
if (status != 0 && MXF_A708_RX_REC_CTRL_BOWSYNC_ERROR == Convert.ToUInt64(true))
Console.WriteLine("Beginning sync error ");
if (status != 0 && MXF_A708_RX_REC_CTRL_EOWSYNC_ERROR == Convert.ToUInt64(true))
Console.WriteLine("Ending sync error ");
if (status != 0 && MXF_A708_RX_REC_CTRL_NOTENOUGHBITS_ERROR == Convert.ToUInt64(true))
Console.WriteLine("less than 200 bytes error ");
if (status != 0 && MXF_A708_RX_REC_CTRL_TOOMANYBITS_ERROR == Convert.ToUInt64(true))
Console.WriteLine("more than 200 bytes error ");
Console.WriteLine();
}
break;
default:
Console.WriteLine("Unknown condID {0}", pendingList[i].condID);
break;
}
}
return rc;
}
/********************************************************************************************************************/
//DisplayDataArray
/********************************************************************************************************************/
private static void DisplayDataArray(UInt64 recNum, IntPtr rxHostBuffer)
{
UInt64 iRec, iData;
IntPtr p = rxHostBuffer;
Console.WriteLine();
for (iRec = 0; iRec < recNum; iRec++)
{
rec = (MXF_A708_DATAREC)Marshal.PtrToStructure(p, typeof(MXF_A708_DATAREC));
Console.WriteLine("{0}: Timetag: {1} Control: {2:x2} Size: {3}", iRec, rec.timeTag, rec.control, rec.dataSize);
Console.Write("Data: ");
for (iData = 0; iData < rec.dataSize / 2; iData++)
{
Console.WriteLine("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
}
}
}
}
Updated 10/23/2023