MX Foundation 4
ar664_playback.c
/*******************************************************************************
//
// File:
// ar664_playback.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 playback the ARINC 664 data previously recorded
// (by ar664_recorder.c). Based on the records timetag they are replayed with
// the same timing than the data received on the ports.
//
// Hardware Requirements:
// - FlexMulti with A664
//
*******************************************************************************/
#include "example.h"
//#define LOOPBACK
//#define LOCAL
#define DATARECS_BUFFER_SIZE_MAX 64*1024
uint32 playback(uint64 startTime, HMXF_BUFFER txBuffer);
uint32 readAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A664_DATAREC *rec664);
int main(void)
{
HMXF_SERVER server = 0;
HMXF_BUFFER txBuffer = 0, rxBuffer = 0;
HMXF_MODULE module = 0;
HMXF_DEVICE device = 0;
HMXF_CHANNEL phyChn = 0;
MXF_A664_DATAREC* rxRec = NULL;
uint64 allocated;
uint64 status;
uint64 usedBytes, freeBytes;
uint64 msgCount = 0;
uint64 acqStopTime;
uint32 rc;
uint64 count = 0;
uint64 currentTime=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);
// Allocate the TX buffer for the port
if(!rc)
rc = mxfA664TxAperiodicBufferAlloc(phyChn, MXF_TXAPERIODIC_PRIORITY_HIGH, 1000, 1024, &txBuffer, &allocated);
// In RAW mode the acquisition buffer get the data
// directly from the physical port. A buffer must
// be however allocated for 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");
}
// Playback
if(!rc)
rc = mxfDeviceTimerGet(device, &currentTime);
if(!rc)
{
currentTime += 500 * 1000 * 1000; // 500ms later
rc = playback(currentTime, txBuffer);
}
// Receive the data
if (!rc)
{
mxfSleep(1000);
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);
}
}
// Free Ports Resources
if (!rc)
rc = mxfTxAperiodicBufferFree(txBuffer);
if (!rc)
rc = mxfRxAcqBufferFree(rxBuffer);
// Free datarec buffers
if (rxRec)
free(rxRec);
printf("\n\nPress a key to terminate rc=0x%08x\n", rc);
getchar();
return rc;
}
uint32 playback(uint64 startTime, HMXF_BUFFER txBuffer)
{
FILE* fp = NULL;
uint64 baseTimetag = 0;
uint32 datarecSize;
uint32 iRecord = 0;
size_t length;
uint32 rc = MAXT_SUCCESS;
uint64 writtenCount;
char *filename = "recorder.raw";
fp = fopen(filename, "rb");
if (!fp)
return MAXT_ERROR_FILE_OPEN;
printf("Starting Playback\n\n");
// File format is as follow [length][data][length][data] ... [length][data]
// where length = size of the following data and data can be UDP or RAW data.
do
{
length = fread(&datarecSize, sizeof(datarecSize), 1, fp);
if (length)
{
memset(&txRec, 0, sizeof(MXF_A664_DATAREC));
length = fread(&txRec, datarecSize, 1, fp);
if (length)
{
if (++iRecord == 1)
baseTimetag = txRec.timeTag;
txRec.timeTag = startTime + (txRec.timeTag - baseTimetag);
txRec.control = MXF_A664_CTRL_TX_RAW_IP_CHECKSUM_INCLUDED | MXF_A664_CTRL_TX_RAW_MAC_CRC32_INCLUDED | MXF_A664_CTRL_TX_RAW_MAC_NETID_INCLUDED;
// Now play the recorded records with an ajusted timestamp
rc = mxfA664TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 1, &txRec, &writtenCount);
}
}
printf(".");
} while (length && !rc);
if(fp)
fclose(fp);
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("\n%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