MX Foundation 4
ar708_event_handler.c
/*****************************************************************************
//
// File:
// ar708_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 example demonstrates the usage of ARINC 708 channel class
// for transmission and reception and shows how to configure and
// use asynchronous events.
//
// Hardware requirements:
// - MAXT Flex with A708 option.
// - Loopback between TX and RX channels if internal loopback is not used.
//
*****************************************************************************/
#include "example.h"
#define MAX_TX_RECORDS_TO_TRANSMIT 6
// #define LOCAL
//#define LOOPBACK
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
void DisplayDataArray(uint64 recNum, MXF_A708_DATAREC* rec);
int main(void)
{
uint32 rc;
HMXF_SERVER server=0;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_BUFFER txBuffer=0;
HMXF_ASYNCEVENT asyncEvent=0;
uint64 moduleCount=0;
uint64 channelCount=0;
size_t txBufferSize=0;
MXF_A708_DATAREC* txHostBuffer=0;
uint64 data;
uint64 word;
// Connects 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
// Initializes MX Foundation library
if (!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets the device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Gets handle of first ARINC 708 module
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A708_EH, 1, &moduleCount, &module);
// If module not found, return an error
if(!rc && !moduleCount)
rc = MAXT_ERROR_NOT_FOUND;
// Activates the ARINC 708 module (because the MIL-STD-1553 is by default activated, except on FM1553-2)
if (!rc)
{
rc = mxfAttributeUint64Set(module, KMXF_A708_MODULE_ACTIVE, TRUE);
if (rc == MAXT_ERROR_NOT_SUPPORTED) // Attribute not supported on FM1553-2, simply ignore
rc = MAXT_SUCCESS;
}
// Gets handle of first ARINC 708 RX channel
if(!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A708, MXF_SCLASS_RX_CHANNEL, 1, &channelCount, &rxChannel);
// Gets handle of first ARINC 708 TX channel
if (!rc && channelCount)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A708, MXF_SCLASS_TX_CHANNEL, 1, &channelCount, &txChannel);
// Enables loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A708_TX_RX_TEST_LB , VMXF_ENABLE);
#endif
// Allocates 4 KB buffer for tx data
if(!rc)
{
txBufferSize = 4*1024;
// Allocates TX Aperiodic static buffer for HIGH priority queue
rc=mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, &txBuffer, NULL);
// Host buffer allocation
if(!rc)
{
txHostBuffer = (MXF_A708_DATAREC*) calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Registers the callback handler service function
if (!rc)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, NULL, &asyncEvent);
memset(&eventInfo, 0, sizeof(eventInfo));
// Receive message in error; the callback get called when a data word in error is detected
eventInfo[0].condID = MXF_ASYNCEVENT_COND_RX_ERROR;
eventInfo[0].condition.rxErr.channel = rxChannel;
// Enables RX error asynchronous event
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, eventInfo);
if(!rc)
{
rec = txHostBuffer;
//Prepare records to send with an error injection in transmission for the 3rd record
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
if (data==2)
{
rec->control = MXF_A708_TX_REC_CTRL_MANCHESTERBIT_ERROR; //The 3rd record contains manchester error at bit 1000
rec->manchesterBitErr = 1000;
}
else
{
rec->control = 0;
rec->manchesterBitErr = 0;
}
rec->repeatCount = 1;
rec->dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
for(word=0; word < rec->dataSize/2; word++) //The first 8 bits of each message have to be 055
{
switch(word)
{
case 0:
rec->data[word] = 055; // Label
break;
case 1:
case 2:
case 3:
case 4:
rec->data[word] = 0x0000;
break;
default:
rec->data[word] = (uint16)(0x0101*word);
}
}
rc = mxfA708NextDataRecordPtrGet(rec, &rec);
}
}
//if(!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT,txHostBuffer);
if(!rc)
{
printf("Transmitting ...\n\n");
// Transmits strings on relative record time
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(2000);
// Disable conditions
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, eventInfo);
if (asyncEvent)
// Frees device and host buffers
if (txBuffer)
if(txHostBuffer)
free(txHostBuffer);
if(rc)
{
char errorString[200];
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%X", rc);
printf("%s\n\r", errorString);
}
printf("Terminating ...\n");
// Unloads MX Foundation library
// Disconnects from MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param)
{
HMXF_CHANNEL channel;
uint64 i, maxCount=64, pendingCount, status;
uint32 rc=MAXT_SUCCESS;
uint64 dev, mod, port;
(void)param;
// Builds the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, maxCount, &pendingCount, pendingList);
for (i=0; i < pendingCount && !rc; i++)
{
switch(pendingList[i].condID)
{
// A receive error was detected
case MXF_ASYNCEVENT_COND_RX_ERROR:
channel = pendingList[i].condition.rxErr.channel;
status = pendingList[i].condition.rxErr.status;
rc = mxfChannelLocationGet(channel, &dev, &mod, &port);
if(!rc)
{
printf("Status 0x%08llx received on channel %llu.%llu.%llu: ", status, dev, mod, port);
if(status & MXF_A708_RX_REC_CTRL_MANCHESTER_ERROR)
printf("manchester error ");
if(status & MXF_A708_RX_REC_CTRL_BITSCNT_ERROR)
printf("bit count error ");
if(status & MXF_A708_RX_REC_CTRL_TOOLONGWORD_ERROR)
printf("message too long error ");
if(status & MXF_A708_RX_REC_CTRL_BOWSYNC_ERROR)
printf("Beginning sync error ");
if(status & MXF_A708_RX_REC_CTRL_EOWSYNC_ERROR)
printf("Ending sync error ");
if(status & MXF_A708_RX_REC_CTRL_NOTENOUGHBITS_ERROR)
printf("less than 200 bytes error ");
if(status & MXF_A708_RX_REC_CTRL_TOOMANYBITS_ERROR)
printf("more than 200 bytes error ");
printf("\n");
}
break;
default:
printf("Unknown condID 0x%llx\n", pendingList[i].condID);
}
}
return rc;
}
void DisplayDataArray(uint64 recNum, MXF_A708_DATAREC* rec)
{
uint64 iRec,
iData;
printf("\n");
for(iRec=0; iRec < recNum; iRec++)
{
printf("%03llu %010llu 0x%08x %02u ", iRec, p->timeTag, p->control, p->dataSize);
for(iData=0; iData < p->dataSize/2; iData++)
{
printf("%04x ", p->data[iData]);
if(!((iData+1)%8) && (iData+1 < p->dataSize/2))
printf("\n ");
}
printf("\n");
}
}
Updated 10/23/2023