MX Foundation 4
csdb_rx_sampling.cs
/****************************************************************************************************************
//
// File:
// csdb_rx_sampling.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 is a basic program for using the MX API sampling services
// for receiving data. In this example, message selection is used to
// discriminate data received.
//
// 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_sampling
{
const int BUFFER_SIZE = 4096; // 4KB
const int LABEL = 0x3;
const int SI = 0;
const int BLOCKCOUNT = 6;
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 rxBuffer = 0;
var txBuffer = new UInt64[2];
UInt64 count = 0;
IntPtr hostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
IntPtr recPtr = IntPtr.Zero;
UInt64 schedule = 0;
UInt64 msg = 0;
UInt64 i, j, dataIndex;
UInt64 msgsCount, bytesCount;
var label = new UInt64[] { 0xA5 };
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
{
data = new byte[12]
};
{
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;
}
// Initialize the server
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;
// Set the channels to low speed
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_SPEED, 12500);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_SPEED, 12500);
// Set the block count to 6
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 microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Alloc host buffer
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
try
{
txHostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Allocate RX sampling buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Filter out a specific label/si
if (rc == MAXT_SUCCESS)
rc = mxfCSDBRxSamplingMsgSelectSet(rxBuffer, MXF_MSG_DESELECT, 1, label);
// 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);
// Create the periodic scheduler
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleNew(txChannel[0], out schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000, 0, out msg);
// Define the number of buffer for the list and link to it
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Set record for the buffer, will be ignored by sampling
if (rc == MAXT_SUCCESS)
{
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.reserved = 0;
for (i = 0; i < BLOCKCOUNT; i++)
recCsdb.data[i] = 0xa5;
Marshal.StructureToPtr(recCsdb, txHostBuffer, false);
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, txHostBuffer);
}
// Set a record that will be received by sampling
if (rc == MAXT_SUCCESS)
{
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.reserved = 0;
recCsdb.data[0] = LABEL;
recCsdb.data[1] = 0x8 | SI;
for (i = 2; i < BLOCKCOUNT; i++)
recCsdb.data[i] = (byte)i;
Marshal.StructureToPtr(recCsdb, txHostBuffer, false);
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[1], 1, txHostBuffer);
}
// Run the scheduler, update records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Running periodic transmission, please wait...");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Read and display records for 5 seconds
for (i = 0; (i < 10) && (rc == MAXT_SUCCESS); i++)
{
rc = mxfCSDBRxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, BUFFER_SIZE, out msgsCount, out bytesCount, hostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Read {0} messages", msgsCount);
recPtr = hostBuffer;
for (j = 0; (rc == MAXT_SUCCESS) && (j < msgsCount); j++)
{
recSamp = (MXF_CSDB_SAMPREC)Marshal.PtrToStructure(recPtr, typeof(MXF_CSDB_SAMPREC));
Console.Write("{0:D2}: Timetag={1:D16}: CSDB data=[ ", j, recSamp.timeTag);
for (dataIndex = 0; dataIndex < BLOCKCOUNT; dataIndex++)
Console.Write("0x{0:X2} ", recSamp.data[dataIndex]);
Console.Write("]\n\r");
rc = mxfCSDBNextSamplingRecordPtrGet(recPtr, out recPtr);
}
}
if (rc != MAXT_SUCCESS)
Console.WriteLine("Sampling read failed; rc=0x{0:x8}", rc);
else
mxfSleep(500);
}
// Stop transmission
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleFree(schedule);
// Stop sampling
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingStop(rxBuffer);
// 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;
}
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