MX Foundation 4
ar664_error_injection_detection.c
/*******************************************************************************
//
// File:
// ar664_error_injection_detection.c
//
// Copyright (c) MAX Technologies Inc. 1988-2017, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo shows how to modify A664 frame for generating error
// injection errors on specific frame headers or data and register
// the A664 stack for reporting the errors using the async/event API.
//
// Hardware Requirements:
// - FlexMulti A664
//
*******************************************************************************/
#include "example.h"
//#define LOOPBACK
//#define LOCAL
#define TEST_DATASIZE 64
#define DATARECS_BUFFER_SIZE_MAX 64*1024
#define LOOPBACK_MODE
#define SRC_IP_ADRS "10.0.0.111"
#define SRC_UDP_PORT 1234
#define DST_IP_ADRS "10.0.0.222"
#define DST_UDP_PORT 5678
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param);
uint32 readAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A664_DATAREC *rec664);
int main(void)
{
HMXF_SERVER server = 0;
HMXF_PORT portTx = 0;
HMXF_PORT portRx = 0;
HMXF_VL vlTx = 0, vlRx = 0;
HMXF_BUFFER rxBuffer = 0, rxBufferRaw=0;
MXF_A664_DATAREC* data = NULL;
HMXF_MODULE module = 0;
HMXF_DEVICE device = 0;
HMXF_CHANNEL phyChn = 0;
MXF_A664_DATAREC* rxRec = NULL;
MXF_A664_DATAREC* txRec = NULL;
MXF_ASYNCEVENT_CONDITION asyncEventInfoRx;
MXF_A664_ERROR_INJ errorInjectionTx;
HMXF_ASYNCEVENT asyncEvent = 0;
uint32 srcIpAdrs = 0, dstIpAdrs = 0;
uint64 status;
uint64 usedBytes, freeBytes;
uint64 msgCount = 0;
uint64 acqStopTime;
uint32 recordIdx, dataIdx;
uint32 maxTxRecords = 4;
uint32 vlid = 100;
uint32 rc;
uint64 count = 0;
#ifndef LOCAL
char szAddress[20];
#endif
printf("\nARINC664 Error Injection / Detection Demo\n\n");
// Connect to server
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
printf("Enter address of remote MXF4 server (IP or USB)\n\n");
printf(" xxx.xxx.xxx.xxx or usb\n\n");
fgets(szAddress, sizeof(szAddress), stdin);
szAddress[strcspn(szAddress, "\n")] = '\0';
if (!strcmp(szAddress, "usb"))
{
rc = mxfSystemUSBDetect(&count);
if (!rc && count)
rc = mxfSystemUSBServerConnect(0, &server);
// If device not found, return an error
if (!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
if (rc)
printf("No USB device detected; rc=0x%08x\n", rc);
}
else
{
rc = mxfServerConnect(szAddress, "admin", "admin", FALSE, &server);
if (rc)
printf("Error connecting to %s; rc=0x%08x\n", szAddress, rc);
}
#endif
if (rc)
{
printf("Failed to connect; rc=0x%08x\n", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initialize server, allocate base resources
rc = mxfSystemInit(server);
// Get the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Get the first A664 module
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A664, 1, &count, &module);
// Obtain the first ARINC 664 Phy channel (Phy logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A664, MXF_SCLASS_ALL, 1, &count, &phyChn);
// If module or channel not found, return an error
if (!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(phyChn, KMXF_A664_PHY_LOOPBACK, VMXF_A664_PHY_LOOPBACK_EXTERNAL);
#endif
// Raw port will receive erroneous frames
if (!rc)
rc = mxfAttributeUint64Set(phyChn, KMXF_A664_RAW_RX_ERROR_ONLY, TRUE);
// Set Timebase
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Create the transmission (TX) virtual link
if (!rc)
{
memset(&vlParam, 0, sizeof(MXF_A664_VL_PARAM));
vlParam.direction = MXF_A664_VL_DIR_TX;
vlParam.VLId = vlid;
vlParam.frameType = MXF_A664_FRAME_TYPE_ARINC664;
vlParam.frameSizeMax = 512;
vlParam.dir.Tx.subVlNumber = 1;
vlParam.dir.Tx.bag = 1 * 1000 * 1000; // 1ms
vlParam.dir.Tx.netSelection = MXF_A664_NETSELECT_ALL;
vlParam.dir.Tx.Ede.enable = FALSE;
vlParam.dir.Tx.subVl[0].bufferSize = vlParam.frameSizeMax;
vlParam.dir.Tx.subVl[0].maxBuffers = maxTxRecords+1;
rc = mxfA664VlCreate(phyChn, &vlParam, &vlTx);
}
// Create the transmission COM port
if (!rc)
{
inet_pton(AF_INET, SRC_IP_ADRS, &srcIpAdrs);
srcIpAdrs = ntohl(srcIpAdrs);
inet_pton(AF_INET, DST_IP_ADRS, &dstIpAdrs);
dstIpAdrs = ntohl(dstIpAdrs);
memset(&portParam, 0, sizeof(MXF_A664_PORT_PARAM));
portParam.portType = MXF_A664_PORT_TYPE_COM;
portParam.family = MXF_A664_PORT_FAMILY_IPV4;
portParam.mode = MXF_A664_PORT_MODE_QUEUING;
portParam.type.COM.destAddress.port = DST_UDP_PORT;
portParam.type.COM.destAddress.version.IPv4.address = dstIpAdrs;
portParam.type.COM.srcAddress.port = SRC_UDP_PORT;
portParam.type.COM.srcAddress.version.IPv4.address = srcIpAdrs;
rc = mxfA664PortCreate(vlTx, &portParam, &portTx);
}
if (!rc)
{
txRec = malloc(DATARECS_BUFFER_SIZE_MAX);
memset(txRec, 0, DATARECS_BUFFER_SIZE_MAX);
if (!txRec)
rc = MAXT_ERROR_MEM;
}
// Create the reception (RX) virtual link
if (!rc)
{
memset(&vlParam, 0, sizeof(MXF_A664_VL_PARAM));
vlParam.VLId = vlid;
vlParam.frameType = MXF_A664_FRAME_TYPE_ARINC664;
vlParam.direction = MXF_A664_VL_DIR_RX;
vlParam.frameSizeMax = 512;
vlParam.dir.Rx.Mac.skewMax = 4 * 1000 * 1000;
vlParam.dir.Rx.Mac.rmPSNRange = 2;
rc = mxfA664VlCreate(phyChn, &vlParam, &vlRx);
}
// Create COM RX port
if (!rc)
{
memset(&portParam, 0, sizeof(MXF_A664_PORT_PARAM));
portParam.portType = MXF_A664_PORT_TYPE_COM;
portParam.family = MXF_A664_PORT_FAMILY_IPV4;
portParam.mode = MXF_A664_PORT_MODE_QUEUING;
portParam.dir.Rx.network = MXF_A664_NETSELECT_ALL;
portParam.dir.Rx.maxBuffers = 16;
portParam.dir.Rx.bufferSize = 1518;
portParam.type.COM.destAddress.port = DST_UDP_PORT;
portParam.type.COM.destAddress.version.IPv4.address = dstIpAdrs;
portParam.type.COM.srcAddress.port = SRC_UDP_PORT;
portParam.type.COM.srcAddress.version.IPv4.address = srcIpAdrs;
rc = mxfA664PortCreate(vlRx, &portParam, &portRx);
}
// Get buffer handle automatically allocated for the port
if (!rc)
rc = mxfRxAcqBufferGet(portRx, &rxBuffer);
if (!rc)
{
rxRec = malloc(DATARECS_BUFFER_SIZE_MAX);
memset(rxRec, 0, DATARECS_BUFFER_SIZE_MAX);
if (!rxRec)
rc = MAXT_ERROR_MEM;
}
if (!rc)
rc = mxfRxAcqBufferAlloc(phyChn, 20000, &rxBufferRaw, NULL);
// Set TX Error Injection rule(s) to apply
if (!rc)
{
memset(&errorInjectionTx, 0, sizeof(errorInjectionTx));
errorInjectionTx.port = MXF_A664_NETSELECT_ALL;
errorInjectionTx.fragmentOffset = -1;
// IP header, offset 0, Version-Length
errorInjectionTx.error[0].id = MXF_A664_ERRORID_VL_FRAME_MASK_AND;
errorInjectionTx.error[0].offset = MXF_A664_FRAME_HEADER_OFFSET_IP;
errorInjectionTx.error[0].mask = 0x0f;
rc = mxfA664ErrorInjectionSet(portTx, 0, &errorInjectionTx);
}
// Define RX Error(s) detection conditions to trap; register the handler.
if (!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, NULL, &asyncEvent);
if (!rc)
{
memset(&asyncEventInfoRx, 0, sizeof(asyncEventInfoRx));
asyncEventInfoRx.condID = MXF_ASYNCEVENT_COND_RX_ERROR;
asyncEventInfoRx.condition.rxErr.channel = portRx;
rc = mxfAsyncEventConditionsSet(asyncEvent, VMXF_ENABLE, 1, &asyncEventInfoRx);
}
// Set acquisition mode
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Start acquisition, check status
if (!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
rc = mxfRxAcqStart(rxBufferRaw, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
{
rc = mxfRxAcqBufferStatusGet(rxBuffer, &status, &msgCount, &usedBytes, &freeBytes);
if (!rc && (status & MXF_RXACQ_STATUS_RUNNING))
printf("Acquisition Running\n\n");
}
// Build the Transmit Records.
for (recordIdx = 0, data = txRec; recordIdx < maxTxRecords && !rc; recordIdx++)
{
// Every two records inject one invalid record
data->control = recordIdx % 2 ? 0 : MXF_A664_TX_REC_CTRL_ERROR_INJ;
data->dataSize = TEST_DATASIZE;
data->repeatCount = 1;
for (dataIdx = 0; dataIdx < TEST_DATASIZE; dataIdx++)
data->data[dataIdx] = (uint8)((dataIdx + ((recordIdx*TEST_DATASIZE) & 0xff)));
rc = mxfA664NextDataRecordPtrGet(data, &data);
}
// Transmit the data
if (!rc)
{
rc = mxfA664TxAperiodicWrite(portTx, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, maxTxRecords, txRec, &count);
printf("%llu frames transmitted\n\n", count);
// Wait for complete transmission
mxfSleep(500);
}
// Receive the data
if (!rc)
rc = readAcquisitionData(rxBuffer, rxRec);
if (!rc)
rc = readAcquisitionData(rxBufferRaw, rxRec);
// All events should occurs before the end of the sleep
mxfSleep(1000);
// Terminate the handler
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, VMXF_DISABLE, 1, &asyncEventInfoRx);
if (!rc)
// Terminate Acquisition
if (!rc)
rc = mxfRxAcqStop(rxBuffer);
if (!rc)
rc = mxfRxAcqClear(rxBuffer);
// Make sure the acquisition is stopped
if (!rc)
{
rc = mxfRxAcqBufferStatusGet(rxBuffer, &status, &msgCount, &usedBytes, &freeBytes);
if (!rc && status & MXF_RXACQ_STATUS_STOPPED)
{
rc = mxfRxAcqStopTimeGet(rxBuffer, &acqStopTime);
if (!rc)
printf("\nAcquisition stopped at %llu\n", acqStopTime);
}
}
// Free Ports Resources
if (!rc)
rc = mxfRxAcqBufferFree(rxBuffer);
// Free COM ports
if (!rc)
rc = mxfA664PortRelease(portTx);
if (!rc)
rc = mxfA664PortRelease(portRx);
// Free VL(s)
if (!rc)
rc = mxfA664VlRelease(vlRx);
if (!rc)
rc = mxfA664VlRelease(vlTx);
// Free datarec buffers
if (txRec)
free(txRec);
if (rxRec)
free(rxRec);
printf("\n\nPress a key to terminate rc=0x%08x\n", rc);
getchar();
return rc;
}
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param)
{
HMXF_HANDLE handle;
MXF_ASYNCEVENT_PENDING_INFO pendingList[256];
uint64 status;
uint64 index, pendingCnt;
uint32 rc = MAXT_SUCCESS;
( void )param;
rc = mxfAsyncEventPendingGet(asyncEvent, sizeof(pendingList) / sizeof(pendingList[0]), &pendingCnt, pendingList);
for (index = 0; index<pendingCnt && !rc; index++)
{
switch (pendingList[index].condID)
{
case MXF_ASYNCEVENT_COND_RX_ERROR:
{
status = pendingList[index].condition.rxErr.status;
printf("RX ERR -> status (0x%llx)\n", status);
handle = pendingList[index].condition.rxErr.channel;
printf(" PORT Handle=0x%llx\n\n", handle);
break;
}
default:
printf("Unexpected Event Condition received\n");
break;
}
}
fflush(stdout);
return rc;
}
uint32 readAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A664_DATAREC *rec664)
{
MXF_A664_DATAREC* recPtr = rec664;
uint64 recordIdx, dataIdx;
uint64 status, msgsCount, bytesCount;
uint32 dataSize;
uint32 rc;
// Read and display records
rc = mxfA664RxAcqRead(rxBuffer, 0, DATARECS_BUFFER_SIZE_MAX, &status, &msgsCount, &bytesCount, rec664);
if (!rc)
printf("%lld records received\n\n", msgsCount);
for (recordIdx = 0; recordIdx < msgsCount && !rc; recordIdx++)
{
printf(" %03llu: timeTag=%012llu, Size=%u", recordIdx, recPtr->timeTag, recPtr->dataSize);
printf("\n data=");
dataSize = recPtr->dataSize;
for (dataIdx = 0; dataIdx < dataSize; dataIdx++)
{
printf("%02X ", recPtr->data[dataIdx]);
if (!((dataIdx + 1) % 8) && (dataIdx + 1 < recPtr->dataSize))
printf("\n ");
}
printf("\n\n");
mxfA664NextDataRecordPtrGet(recPtr, &recPtr);
}
return rc;
}
Updated 10/23/2023