MX Foundation 4
csdb_rx_event_handler.c
/******************************************************************************
//
// File:
// csdb_rx_event_handler.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 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.
//
*******************************************************************************/
#include <mxfapi.h>
//#define LOOPBACK
//#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
#define LABEL 0x09
#define SI 0
#define BLOCKCOUNT 8
uint32 RXmsgTotal=0;
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, pvoid pParam);
uint32 PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER *txBuffer, MXF_CSDB_DATAREC *recCsdb);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0, txChannel=0;
HMXF_ASYNCEVENT asyncEvent=0;
HMXF_BUFFER rxBuffer=0, txBuffer[2];
MXF_ASYNCEVENT_CONDITION asyncEventInfo[2];
uint64 count=0;
MXF_CSDB_DATAREC *hostBuffer=NULL;
// 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 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 block count to 8
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 nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Alloc host buffer
if(!rc)
{
hostBuffer = (MXF_CSDB_DATAREC *)malloc(BUFFER_SIZE);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Set the event handler
if(!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, hostBuffer, &asyncEvent);
// Allocate RX sampling buffer
if(!rc)
rc = mxfRxSamplingBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Set the RX async event conditions
if(!rc)
{
asyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_RX_MSG;
asyncEventInfo[0].condition.rxMsg.channel = rxChannel;
asyncEventInfo[1].condID = MXF_ASYNCEVENT_COND_RX_ERROR;
asyncEventInfo[1].condition.rxErr.channel = rxChannel;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 2, &asyncEventInfo[0]);
}
// Monitor a specific label/si
if (!rc)
{
msgid.label = LABEL;
msgid.si = SI;
msgid.reserved[0] = 0;
msgid.reserved[1] = 0;
rc = mxfCSDBAsyncEventRxMsgSelectSet(asyncEvent, rxChannel, MXF_MSG_SELECT_ADD, 1, &msgid);
}
// 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);
// Set the Periodic Scheduler
if(!rc)
rc = PeriodicScheduling(txChannel, txBuffer, hostBuffer);
// Stop sampling
if(!rc)
rc = mxfRxSamplingStop(rxBuffer);
// Disable conditions
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 2, asyncEventInfo);
// Terminate async event handler
if(!rc)
// 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;
}
//****************************************************************************************************************
// RX asynchronous event Handler
//****************************************************************************************************************
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param)
{
HMXF_CHANNEL channel;
uint64 pendingCount;
uint64 label, si, status;
uint64 i, dataIdx;
uint32 rc=MAXT_SUCCESS;
uint64 dev, mod, port;
HMXF_BUFFER sampBuffer;
(void)param;
// Build the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, 64, &pendingCount, pendingList);
for (i=0; !rc && 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, &sampBuffer);
if(!rc)
rc = mxfCSDBRxSamplingSingleRead(sampBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, label, si, &rec);
if(!rc)
{
printf("Msg %02d - label=%02llX si=%llu ", RXmsgTotal++, label, si);
printf("Timetag=%016llu: CSDB data=[", rec.timeTag);
for (dataIdx=0; dataIdx<BLOCKCOUNT; dataIdx++)
printf("%02X", rec.data[dataIdx]);
printf("]\n\r");
}
if(rc)
printf("Error getting latest value rc = 0x%08x\n", rc);
break;
}
case MXF_ASYNCEVENT_COND_RX_ERROR:
channel = pendingList[i].condition.rxErr.channel;
status = pendingList[i].condition.rxErr.status;
mxfChannelLocationGet(channel, &dev, &mod, &port);
printf("Status 0x%08llx received on channel %llu.%llu.%llu\n", status, dev, mod, port);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
//*****************************************************************************
// Periodic Transmission
//*****************************************************************************
uint32 PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER *txBuffer, MXF_CSDB_DATAREC *recCsdb)
{
HMXF_SCHED_MSG msg=0;
HMXF_SCHED schedule=0;
uint32 rc;
uint8 i;
// 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;
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, recCsdb);
// Set the Message Block
if(!rc)
{
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;
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recCsdb);
}
//==========================================================================
// Run the scheduler, Update records
//==========================================================================
// Create and start the Periodic Scheduler
if(!rc)
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if (!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000000, 0, &msg);
// Define two buffers in the list
if(!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Run the schedule now
if(!rc)
{
rc = mxfTxPeriodicScheduleRun(schedule);
if (rc)
printf("\n\rSchedule cannot be runned; rc=0x%08x\n\r", rc);
else
{
// Let the scheduler run; send periodic sync/data for 5 seconds
printf("Running periodic transmission for 5 secs, please wait...\n\r");
mxfSleep(5000);
rc = mxfTxPeriodicScheduleFree(schedule);
if(!rc)
printf("\n\rTransmission stopped\n\r");
}
}
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 03/30/2022