MX Foundation 4
csdb_buffer_threshold.cs
/************************************************************************************************
//
// File:
// csdb_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 a ramp on one label and record the messages using asynchronous
// events.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX CSDB Enhanced channels.
//
**************************************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace csdb_example
{
class csdb_buffer_threshold
{
const int BUFFER_SIZE = 1 * 1024 * 1024; // 1 MiB
const int TXALMOSTFULL = 7;
const int TXALMOSTEMPTY = 3;
const int RXALMOSTFULL = 5;
const int RXALMOSTEMPTY = 2;
const int TX_MSG_LABEL = 0xA0;
const int TX_MSG_SI = 0x01;
private static UInt32 TXAsyncEvents = 0;
private static UInt16 data = 0;
public 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];
UInt64 asyncEvent = 0;
UInt64 rxBuffer = 0;
var txBuffer = new UInt64[2];
var txBufferEvent = new UInt64[1];
var rxAsyncEventInfo = new MXF_ASYNCEVENT_CONDITION[1];
var txAsyncEventInfo = new MXF_ASYNCEVENT_CONDITION[1];
UInt64 schedule = 0;
UInt64 msgSched = 0;
UInt64 count = 0;
IntPtr hostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
MXF_ASYNCEVENT_HANDLER _asyncEventHandler = asyncEventHandler;
{
data = new byte[12]
};
// Connects 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;
}
Console.WriteLine();
Console.WriteLine("Starting");
// initialize init callback handler to set IPM-ASYNC-EH first TX and RX channel to CSDB
if (rc == MAXT_SUCCESS)
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get the module handle
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, out count, module);
// Get the first CSDB Protocol RX channel (RX logical #0)
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_CSDB, MXF_SCLASS_RX_CHANNEL, 1, out count, rxChannel);
// Get the first CSDB Protocol TX channel (TX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_CSDB, MXF_SCLASS_TX_CHANNEL, 1, out count, txChannel);
// If channel not found, return an error
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Configure RX and TX channel to 12.5Kbps, blockcount 6, parity odd, one stop bit
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_SPEED, 12500);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_SPEED, 12500);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_BLOCKCOUNT, 6);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_BLOCKCOUNT, 6);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_PARITY, VMXF_CSDB_PARITY_ODD);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_PARITY, VMXF_CSDB_PARITY_ODD);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_STOPBITSIZE, 1);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_STOPBITSIZE, 1);
// Enable loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Set timebase to 64-bit nanoseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Alloc host buffer (1 MiB)
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
try
{
txHostBuffer = Marshal.AllocHGlobal(1*1024);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Set the event handler
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, hostBuffer, out asyncEvent);
// Allocate RX acquisition buffer (1 MiB)
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Set the RX async event condition
if (rc == MAXT_SUCCESS)
{
rxAsyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
rxAsyncEventInfo[0].condition.rxAcqBufferThreshold.buffer = rxBuffer;
rxAsyncEventInfo[0].condition.rxAcqBufferThreshold.almostFull = RXALMOSTFULL;
rxAsyncEventInfo[0].condition.rxAcqBufferThreshold.almostEmpty = RXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(true), 1, rxAsyncEventInfo);
}
// Set acquisition mode
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Start acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Acquisition started");
// Allocate TX sync block buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], 0xA5, 4096, out txBuffer[0], IntPtr.Zero);
// Allocate TX periodic update buffer (1MiB)
if (rc == MAXT_SUCCESS)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], TX_MSG_LABEL, BUFFER_SIZE, out txBuffer[1], IntPtr.Zero);
txBufferEvent[0] = txBuffer[1];
}
// Set the TX async event condition
if (rc == MAXT_SUCCESS)
{
txAsyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD;
txAsyncEventInfo[0].condition.txPeriodicUpdateMsgBufferThreshold.channel = txChannel[0];
txAsyncEventInfo[0].condition.txPeriodicUpdateMsgBufferThreshold.almostFull = TXALMOSTFULL;
txAsyncEventInfo[0].condition.txPeriodicUpdateMsgBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(true), 1, txAsyncEventInfo);
}
// Select message for async event condition
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, txChannel[0], MXF_MSG_SELECT_ONLY, 1, txBufferEvent);
// Set the Periodic Scheduler
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleNew(txChannel[0], out schedule);
// Add one message to scheduler, Rate=50 ms, Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 50000000, 0, out msgSched);
// Add two buffers to the message
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleBufferListAdd(msgSched, 2, 0, txBuffer);
// Set sync block, will not be updated later on
if (rc == MAXT_SUCCESS)
{
// SYNC block
recTXCsdb.timeTag = 0;
recTXCsdb.control = 0;
recTXCsdb.repeatCount = 1;
recTXCsdb.reserved = 0;
recTXCsdb.data[0] = 0xA5;
recTXCsdb.data[1] = 0xA5;
recTXCsdb.data[2] = 0xA5;
recTXCsdb.data[3] = 0xA5;
recTXCsdb.data[4] = 0xA5;
recTXCsdb.data[5] = 0xA5;
Marshal.StructureToPtr(recTXCsdb, txHostBuffer, false);
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, txHostBuffer);
}
// Run the schedule
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleRun(schedule);
// Send messages for 2 seconds
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Running periodic transmission...");
mxfSleep(2000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
// Disable conditions
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(false), 1, rxAsyncEventInfo);
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(false), 1, txAsyncEventInfo);
// Terminate async event handler
if (rc == MAXT_SUCCESS)
// 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);
}
// Terminate
if (hostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(hostBuffer);
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 param)
{
var pendingList = new MXF_ASYNCEVENT_PENDING_INFO[64];
UInt64 maxCount = 64, pendingCount;
UInt64 i;
UInt32 rc;
// Get the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, maxCount, out pendingCount, pendingList);
for (i = 0; (rc == MAXT_SUCCESS) && (i < pendingCount); i++)
{
switch (pendingList[i].condID)
{
case MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD:
// An almost empty condition was detected...
updateMsgs(pendingList[i].condition.txPeriodicUpdateMsgBufferThreshold.buffer, param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, param);
break;
default:
Console.Write("Unknown condID 0x{0:x16})", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Periodic Transmission
//****************************************************************************************************************
private static UInt32 updateMsgs(UInt64 buffer, IntPtr hostBuffer)
{
UInt32 rc = 0;
UInt32 i;
IntPtr recPtr = hostBuffer;
{
data = new byte[12]
};
// Refill the FIFO in order to produce a ramp
for (i = 0; (rc == MAXT_SUCCESS) && (i < TXALMOSTFULL); i++)
{
// Send a parity error on first record
rec.timeTag = 0;
rec.control = (i != 0) ? 0 : MXF_CSDB_TX_REC_CTRL_PARITY_ERROR;
rec.repeatCount = 1;
rec.reserved = 0;
rec.data[0] = TX_MSG_LABEL;
rec.data[1] = 0x40 | TX_MSG_SI;
rec.data[2] = (byte)data;
rec.data[3] = (byte)(data >> 8);
rec.data[4] = (byte)data;
rec.data[5] = (byte)(data >> 8);
data++;
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfCSDBNextDataRecordPtrGet(recPtr, out recPtr);
}
// Add more data to the buffer
if (rc == MAXT_SUCCESS)
rc = mxfCSDBTxPeriodicUpdateMsgWrite(buffer, i, hostBuffer);
if (rc != MAXT_SUCCESS)
Console.WriteLine("Periodic Update failed; rc=0x{0:x8}", rc);
else
{
Console.WriteLine();
Console.WriteLine("Async Event {0} - Writing {1} records", ++TXAsyncEvents, i);
}
return rc;
}
//****************************************************************************************************************
// Acquisition Reception
//****************************************************************************************************************
private static UInt32 readAcquisition(UInt64 buffer, IntPtr hostBuffer)
{
IntPtr recPtr = hostBuffer;
UInt64 status, msgsCount, bytesCount;
UInt64 j;
UInt32 rc, index;
{
data = new byte[12]
};
// Read and display records
rc = mxfCSDBRxAcqRead(buffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, hostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Read {0} messages", msgsCount);
for (j = 0; (rc == MAXT_SUCCESS) && (j < msgsCount); j++)
{
rec = (MXF_CSDB_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_CSDB_DATAREC));
Console.Write("{0:D2}: Timetag {1} - ", j, rec.timeTag);
for (index = 0; index < 6; index++)
Console.Write("{0:X2}", rec.data[index]);
if ((rec.control & MXF_CSDB_RX_REC_CTRL_PARITY_ERROR) == MXF_CSDB_RX_REC_CTRL_PARITY_ERROR)
Console.Write(" Parity error");
if ((rec.control & MXF_CSDB_RX_REC_CTRL_STOPBIT_ERROR) == MXF_CSDB_RX_REC_CTRL_STOPBIT_ERROR)
Console.Write(" Stop bit error");
Console.WriteLine();
rc = mxfCSDBNextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc != MAXT_SUCCESS)
Console.WriteLine("Acquisition read failed; rc=0x{0:x8}", rc);
return rc;
}
private static UInt32 initHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
UInt64 device;
MXF_DEVICE_INFO deviceInfo = new MXF_DEVICE_INFO();
UInt32 rc;
if (attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceInfoGet(device, out deviceInfo);
if ((rc == MAXT_SUCCESS) && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_ASYNC_EH))
{
// Sets IPM-ASYNC-EH first TX and RX channel to CSDB
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
value = MXF_CLASS_CSDB;
return Convert.ToUInt32(true);
}
}
}
return Convert.ToUInt32(false);
}
}
}
Updated 03/30/2022