MX Foundation 4
ar664_raw_rx.c
/*******************************************************************************
//
// File:
// ar664_raw_rx.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 application send COM/UDP aperiodic data and monitor
// the data back (frame data) using a physical RAW data channel.
//
// Hardware Requirements:
// - FlexMulti with A664
//
*******************************************************************************/
#include "example.h"
// #define LOOPBACK
// #define LOCAL
#define TEST_DATASIZE 64
#define DATARECS_BUFFER_SIZE_MAX 64*1024
#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 readAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A664_DATAREC *rec664);
int main(void)
{
HMXF_SERVER server = 0;
HMXF_PORT portTx = 0;
HMXF_VL vlTx = 0;
HMXF_BUFFER rxBuffer = 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;
uint32 srcIpAdrs = 0, dstIpAdrs = 0;
uint64 status;
uint64 usedBytes, freeBytes;
uint64 msgCount = 0;
uint64 acqStopTime;
uint32 iRecord, iData;
uint32 maxTxRecords = 16;
uint32 vlid = 100;
uint32 rc;
uint64 count = 0;
#ifndef LOCAL
char szAddress[20];
#endif
// 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_RX_CHANNEL, 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
// 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;
rc = mxfA664VlCreate(phyChn, &vlParam, &vlTx);
}
// Create the transmission COM port
if (!rc)
{
inet_pton(AF_INET, DST_IP_ADRS, &srcIpAdrs);
inet_pton(AF_INET, SRC_IP_ADRS, &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 = ntohl(dstIpAdrs);
portParam.type.COM.srcAddress.port = SRC_UDP_PORT;
portParam.type.COM.srcAddress.version.IPv4.address = ntohl(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;
}
// In RAW mode the acquisition buffer get the data
// directly from the physical port. This port handle
// is used for acquisition and no COM/SAP port is required
// In this mode the application will read all the physical
// frames received on the port.
if (!rc)
rc = mxfRxAcqBufferAlloc(phyChn, 20000, &rxBuffer, NULL);
if (!rc)
{
rxRec = malloc(DATARECS_BUFFER_SIZE_MAX);
memset(rxRec, 0, DATARECS_BUFFER_SIZE_MAX);
if (!rxRec)
rc = MAXT_ERROR_MEM;
}
// 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 = mxfRxAcqBufferStatusGet(rxBuffer, &status, &msgCount, &usedBytes, &freeBytes);
if (!rc && (status & MXF_RXACQ_STATUS_RUNNING))
printf("Acquisition Running\n\n");
}
// Build the Transmit Records
for (iRecord = 0, data = txRec; iRecord < maxTxRecords && !rc; iRecord++)
{
data->control = 0;
data->dataSize = TEST_DATASIZE;
data->repeatCount = 1;
for (iData = 0; iData < TEST_DATASIZE; iData++)
data->data[iData] = (uint8)((iData + ((iRecord*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);
// Terminate Acquisition
if (!rc)
rc = mxfRxAcqStop(rxBuffer);
if (!rc)
rc = mxfRxAcqClear(rxBuffer);
// Make sure the acquisition if 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);
}
}
if (!rc)
rc = mxfRxAcqBufferFree(rxBuffer);
// Free COM ports
if (!rc)
rc = mxfA664PortRelease(portTx);
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 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("%llu 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