MX Foundation 4
csdb_rx_event_handler.cs
/******************************************************************************
//
// File:
// csdb_rx_event_handler.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo shows how to set a basic aperiodic transmission.
// The receiver handler get data on specific label/si.
//
// 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_rx_event_handler
{
const int BUFFER_SIZE = 4096;// 4KB
const int LABEL = 0x09;
const int SI = 0;
const int BLOCKCOUNT = 8;
private static UInt32 RXmsgTotal = 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 asyncEventInfo = new MXF_ASYNCEVENT_CONDITION[2];
UInt64 count = 0;
IntPtr hostBuffer = IntPtr.Zero;
var msgid = new MXF_MSGID_CSDB[1];
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
MXF_ASYNCEVENT_HANDLER _asyncEventHandler = asyncEventHandler;
// 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;
}
// Initialize the server
Console.WriteLine("Starting");
// initialize init callback handler to set 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;
// Set the block count to 8
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
// 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
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Set the event handler
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, hostBuffer, out asyncEvent);
// Allocate RX sampling buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Set the RX async event conditions
if (rc == MAXT_SUCCESS)
{
asyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_RX_MSG;
asyncEventInfo[0].condition.rxMsg.channel = rxChannel[0];
asyncEventInfo[1].condID = MXF_ASYNCEVENT_COND_RX_ERROR;
asyncEventInfo[1].condition.rxErr.channel = rxChannel[0];
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(true), 2, asyncEventInfo);
}
// Monitor a specific label/si
if (rc == MAXT_SUCCESS)
{
msgid[0].label = LABEL;
msgid[0].si = SI;
rc = mxfCSDBAsyncEventRxMsgSelectSet(asyncEvent, rxChannel[0], MXF_MSG_SELECT_ADD, 1, msgid);
}
// Start sampling
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingStart(rxBuffer);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Sampling started");
// Allocate TX Periodic Update buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], 0xA5, BUFFER_SIZE, out txBuffer[0], IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], LABEL, BUFFER_SIZE, out txBuffer[1], IntPtr.Zero);
// Set the Periodic Scheduler
if (rc == MAXT_SUCCESS)
rc = PeriodicScheduling(txChannel[0], txBuffer, hostBuffer);
// Stop sampling
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingStop(rxBuffer);
// Disable conditions
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(false), 2, asyncEventInfo);
// 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);
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
//****************************************************************************************************************
// RX asynchronous event Handler
//****************************************************************************************************************
private static UInt32 asyncEventHandler(UInt64 asyncEvent, IntPtr param)
{
var pendingList = new MXF_ASYNCEVENT_PENDING_INFO[64];
UInt64 channel;
UInt64 pendingCount;
UInt64 label, si, status;
UInt64 i, dataIdx;
UInt32 rc = MAXT_SUCCESS;
UInt64 dev, mod, port;
UInt64 sampBuffer;
IntPtr recPtr = param;
{
data = new byte[12]
};
// Build the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, 64, out pendingCount, pendingList);
for (i = 0; (rc == MAXT_SUCCESS) && (i < pendingCount); i++)
{
switch (pendingList[i].condID)
{
// An event was generated when the data on the specific (label/sdi) was received.
case MXF_ASYNCEVENT_COND_RX_MSG:
{
channel = pendingList[i].condition.rxMsg.channel;
label = pendingList[i].condition.rxMsg.msg.csdb.label;
si = pendingList[i].condition.rxMsg.msg.csdb.si;
// Get latest value
rc = mxfRxSamplingBufferGet(channel, out sampBuffer);
if (rc == MAXT_SUCCESS)
rc = mxfCSDBRxSamplingSingleRead(sampBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, label, si, recPtr);
if (rc == MAXT_SUCCESS)
{
rec = (MXF_CSDB_SAMPREC)Marshal.PtrToStructure(recPtr, typeof(MXF_CSDB_SAMPREC));
Console.Write("Msg {0:D2} - label={1:X2} si={2} ", RXmsgTotal++, label, si);
Console.Write("Timetag={0:D16}: CSDB data=[", rec.timeTag);
for (dataIdx = 0; dataIdx < BLOCKCOUNT; dataIdx++)
Console.Write("{0:X2}", rec.data[dataIdx]);
Console.Write("]\n\r");
}
if (rc != MAXT_SUCCESS)
Console.WriteLine("Error getting latest value rc = 0x{0:x8}", rc);
break;
}
case MXF_ASYNCEVENT_COND_RX_ERROR:
channel = pendingList[i].condition.rxErr.channel;
status = pendingList[i].condition.rxErr.status;
mxfChannelLocationGet(channel, out dev, out mod, out port);
Console.WriteLine("Status 0x{0:x8} received on channel {1}.{2}.{3}", status, dev, mod, port);
break;
default:
Console.Write("Unknown condID 0x{0:x16})", pendingList[i].condID);
break;
}
}
return rc;
}
//*****************************************************************************
// Periodic Transmission
//*****************************************************************************
private static UInt32 PeriodicScheduling(UInt64 txChannel, UInt64[] txBuffer, IntPtr recPtr)
{
UInt64 msg = 0;
UInt64 schedule = 0;
UInt32 rc;
byte i;
{
data = new byte[12]
};
// Set the SYNC block
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
for (i = 0; i < BLOCKCOUNT; i++)
recCsdb.data[i] = 0xa5;
recCsdb.reserved = 0;
Marshal.StructureToPtr(recCsdb, recPtr, false);
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, recPtr);
// Set the Message Block
if (rc == MAXT_SUCCESS)
{
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.data[0] = LABEL;
recCsdb.data[1] = SI;
for (i = 2; i < BLOCKCOUNT; i++)
recCsdb.data[i] = i;
recCsdb.reserved = 0;
Marshal.StructureToPtr(recCsdb, recPtr, false);
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recPtr);
}
//==========================================================================
// Run the scheduler, Update records
//==========================================================================
// Create and start the Periodic Scheduler
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleNew(txChannel, out schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000000, 0, out msg);
// Define two buffers in the list
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Run the schedule now
if (rc == MAXT_SUCCESS)
{
rc = mxfTxPeriodicScheduleRun(schedule);
if (rc != MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Schedule cannot be runned; rc=0x{0:x8}", rc);
}
else
{
// Let the scheduler run; send periodic sync/data for 5 seconds
Console.WriteLine("Running periodic transmission for 5 secs, please wait...");
mxfSleep(5000);
rc = mxfTxPeriodicScheduleFree(schedule);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmission stopped");
}
}
}
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 10/23/2023