MX Foundation 4
csdb_rx_sampling.c
/****************************************************************************************************************
//
// File:
// csdb_rx_sampling.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, 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.
//
*****************************************************************************************************************/
#include "example.h"
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
#define LABEL 0x3
#define SI 0
#define BLOCKCOUNT 6
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0, txChannel=0;
HMXF_BUFFER rxBuffer=0, txBuffer[2];
uint64 count=0;
MXF_CSDB_SAMPREC *hostBuffer=NULL;
HMXF_SCHED schedule=0;
HMXF_SCHED_MSG msg=0;
uint64 i, j, dataIndex;
uint64 msgsCount, bytesCount;
uint64 label=0xA5;
// Connect to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
if(rc!=MAXT_SUCCESS)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initialize the server
printf("\nStarting\n");
// initialize init callback handler to set IPM-ASYNC-EH first TX and RX channel to CSDB
rc = mxfSystemInitAttributeUint64CallbackHandler(server, &initHandler);
if(!rc)
rc = mxfSystemInit(server);
// Get the device handle
if(!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Get the module handle
if(!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, &count, &module);
// Get the first CSDB Protocol RX channel (RX logical #0)
if(!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_CSDB, MXF_SCLASS_RX_CHANNEL, 1, &count, &rxChannel);
// Get the first CSDB Protocol TX channel (TX logical #0)
if(!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_CSDB, MXF_SCLASS_TX_CHANNEL, 1, &count, &txChannel);
// If channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Set the channels to low speed
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_SPEED, 12500);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_SPEED, 12500);
// Set the block count to 6
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
// Enable loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_TX_RX_TEST_LB , VMXF_ENABLE);
#endif
// Set timebase to 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Alloc host buffer
if(!rc)
{
hostBuffer = (MXF_CSDB_SAMPREC *)malloc(BUFFER_SIZE);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Allocate RX sampling buffer
if(!rc)
rc = mxfRxSamplingBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Filter out a specific label/si
if (!rc)
rc = mxfCSDBRxSamplingMsgSelectSet(rxBuffer, MXF_MSG_DESELECT, 1, &label);
// Start sampling
if(!rc)
rc = mxfRxSamplingStart(rxBuffer);
if(!rc)
printf("Sampling started\n\r");
// Allocate TX Periodic Update buffer
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, 0xA5, BUFFER_SIZE, &txBuffer[0], NULL);
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, LABEL, BUFFER_SIZE, &txBuffer[1], NULL);
// Create the periodic scheduler
if(!rc)
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if(!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000LL, 0, &msg);
// Define the number of buffer for the list and link to it
if(!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Set record for the buffer, will be ignored by sampling
if(!rc)
{
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.reserved = 0;
for (i=0; i<BLOCKCOUNT; i++)
recCsdb.data[i] = 0xa5;
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, &recCsdb);
}
// Set a record that will be received by sampling
if(!rc)
{
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]= (uint8)i;
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[1], 1, &recCsdb);
}
// Run the scheduler, update records
if(!rc)
{
printf("Running periodic transmission, please wait...\n\r");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Read and display records for 5 seconds
for(i=0; i<10 && !rc; i++)
{
rc = mxfCSDBRxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, BUFFER_SIZE, &msgsCount, &bytesCount, hostBuffer);
if(!rc)
{
printf("Read %"PRIu64" messages\n\r", msgsCount);
recSamp = (MXF_CSDB_SAMPREC*)hostBuffer;
for (j=0; !rc && j<msgsCount; j++)
{
printf("%02"PRIu64": Timetag=%016"PRIu64": CSDB data=[ ", j, recSamp->timeTag);
for (dataIndex=0; dataIndex<BLOCKCOUNT; dataIndex++)
printf("0x%02X ", recSamp->data[dataIndex]);
printf("]\n\r");
mxfCSDBNextSamplingRecordPtrGet(recSamp, &recSamp);
}
}
if(rc)
printf("Sampling read failed; rc=0x%08x\n", rc);
else
mxfSleep(500);
}
// Stop transmission
if(!rc)
rc = mxfTxPeriodicScheduleFree(schedule);
// Stop sampling
if(!rc)
rc = mxfRxSamplingStop(rxBuffer);
// Catch any previous failing function
if(rc)
{
char buffer[256];
if(mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer))
sprintf (buffer,"ERROR # 0x%08X", rc);
printf("%s\n\r", buffer);
}
// Terminate
if(hostBuffer)
free(hostBuffer);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
HMXF_DEVICE device;
MXF_DEVICE_INFO deviceInfo;
uint32 rc;
server=server;
deviceIndex=deviceIndex;
if(attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, &device);
if (!rc)
rc = mxfDeviceInfoGet(device, &deviceInfo);
if(!rc && (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 TRUE;
}
}
}
return FALSE;
}
Updated 10/23/2023