MX Foundation 4
ar717_embedded_recorder.cs
/*******************************************************************************
//
// File:
// ar717_embedded_recorder.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example shows how to use the flash storage onboard the FlexMulti.
//
// - Format the onboard flash file(s) if necessary.
// - Automatic recording of ARINC 717 incoming RX data and write it on the flash.
// - Transmit ARINC 717 records.
// - Read the data stored on the flash.
//
// Hardware Requirements:
// - MAXT FlexMulti
// - loopback between first TX and RX ARINC 717 Enhanced channel when internal loopback is not used
//
*******************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar717_example
{
class ar717_embedded_recorder
{
const int FILE_SIZE = 256 * 1024 * 1024;// 256 MB
const int MAX_TX_SUBFRAMES_TO_TRANSMIT = 6; //minimum 4 to have at least one full frame.
const int SUBFRAMESIZE = 1024;
static uint BUFFER_SIZE = (uint)(MAX_TX_SUBFRAMES_TO_TRANSMIT * Marshal.SizeOf(typeof(MXF_A717_DATAREC)));
public static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
var tx717 = new UInt64[1];
var rx717 = new UInt64[1];
UInt64 txBuffer717 = 0;
UInt64 flash = 0;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 count = 0;
UInt64 fileIdx = 0, fileSize;
bool needUnmount = false;
UInt64 channelIdx = 0;
UInt64 deviceIdx = 0;
UInt64 moduleIdx = 0;
int nbFrames = MAX_TX_SUBFRAMES_TO_TRANSMIT;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
// Connects to services and initialize environment
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt64(false), out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", Convert.ToUInt64(false), out server);
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine();
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
// Initializes the server
Console.Write("Starting\n");
rc = mxfSystemInit(server);
// Gets handle of first channel of MULTI-EH module
if (rc == MAXT_SUCCESS)
rc = mxfChannelAllGet(server, MXF_CLASS_ALL, MXF_SCLASS_ALL, MXF_MODULE_MULTI_EH, 1, out count, tx717);
// Error if no A717 channel found
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Gets location of device and module for that channel
if (rc == MAXT_SUCCESS)
rc = mxfChannelLocationGet(tx717[0], out deviceIdx, out moduleIdx, out channelIdx);
// Releases resources to configure A717 channel
if (rc == MAXT_SUCCESS)
rc = mxfSystemResourcesRelease(server, 0);
// Initializes init callback handler to set first TX and RX channel to A717
if (rc == MAXT_SUCCESS)
// Initializes library
if (rc == MAXT_SUCCESS)
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, deviceIdx, out device);
// Gets first module A717 Enhanced
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MULTI_EH, 1, out count, module);
// Gets the first ARINC 717 TX channel
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A717, MXF_SCLASS_TX_CHANNEL, 1, out count, tx717);
// Gets the first ARINC 717 RX channel
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A717, MXF_SCLASS_RX_CHANNEL, 1, out count, rx717);
// Error if no A717 channel found
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Allocates TX Aperiodic static buffer for HIGH priority queue
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferAlloc(tx717[0], MXF_TXAPERIODIC_PRIORITY_HIGH, (UInt64)BUFFER_SIZE, out txBuffer717, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
{
try
{
txHostBuffer = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to 64-bit microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
//Activates loopback before transmission and reception
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rx717[0], KMXF_A717_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
if (nbFrames < 4)
{
if (rc == MAXT_SUCCESS)
Console.Write("\nNumber of subframes to transmit less than 4: synchronization will not occur.\n\n");
}
// Sets RX & TX channels subframe size
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rx717[0], KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(tx717[0], KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
// Sets RX & TX channels bit encoding to harvard
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rx717[0], KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(tx717[0], KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
// Sets RX & TX channels electrical selection to default
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rx717[0], KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(tx717[0], KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
/******************************************************************************/
// Initialize the flash
/******************************************************************************/
// Gets flash handle
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedNVStorageHandleGet(device, out flash);
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedNVStorageInfoGet(flash, out nvInfo);
if ((rc == MAXT_SUCCESS) && (nvInfo.type == MXF_EMBEDDED_NVSTORAGE_TYPE_EMMC))
{
needUnmount = true;
}
// Checks if a file is already create in flash
if (rc == MAXT_SUCCESS)
{
rc = mxfEmbeddedNVStorageFileCountGet(flash, out count);
if (rc == MAXT_ERROR_NVSTORAGE_NOT_FORMATTED)
{
Console.Write("Formatting...\n");
Console.Write("Done\n");
count = 0;
}
}
if (rc == MAXT_SUCCESS)
{
if (count != 0)
{
// Verifies if first file is right size
rc = mxfEmbeddedNVStorageFileSizeGet(flash, 0, out fileSize);
if (rc == MAXT_SUCCESS)
{
// This file is ok, resets it
if (fileSize == FILE_SIZE / nvInfo.sectorSize)
{
fileIdx = 0;
rc = mxfEmbeddedNVStorageFileReset(flash, fileIdx);
if (rc == MAXT_SUCCESS)
Console.Write("File #{0} reset\n", fileIdx);
}
// Formats and creates a new file
else
{
Console.Write("Formatting...\n");
Console.Write("Done\n");
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedNVStorageFileCreate(flash, FILE_SIZE / nvInfo.sectorSize, out fileIdx);
if (rc == MAXT_SUCCESS)
Console.Write("File #{0} allocated\n", fileIdx);
}
}
}
else
{
// Allocates file
rc = mxfEmbeddedNVStorageInfoGet(flash, out nvInfo);
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedNVStorageFileCreate(flash, FILE_SIZE / nvInfo.sectorSize, out fileIdx);
if (rc == MAXT_SUCCESS)
Console.Write("File #{0} allocated\n", fileIdx);
}
}
// Enables the recording of all messages
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedNVStorageFileMsgSelectEnableSet(rx717[0], fileIdx, 0, Convert.ToUInt64(true));
// Transmits
if (rc == MAXT_SUCCESS)
rc = writeMsgs(txBuffer717, txHostBuffer);
// Reads from memory
if (rc == MAXT_SUCCESS)
rc = read717RecordsFromEmbeddedFlash(flash, fileIdx);
// Disables recording
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedNVStorageFileMsgSelectEnableSet(rx717[0], fileIdx, 0, Convert.ToUInt64(false));
if (needUnmount)
// Catches any previous failing function
if (rc != MAXT_SUCCESS)
{
StringBuilder buffer = new StringBuilder(256);
if (mxfSystemErrorStringGet(server, rc, 256, buffer) != MAXT_SUCCESS)
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
Console.WriteLine(buffer);
}
//Frees all buffers
if (txBuffer717 != 0)
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
// Terminates
Console.Write("\nPress enter to terminate\n");
Console.Read();
return;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
private static UInt32 writeMsgs(UInt64 buffer, IntPtr hostBuffer)
{
UInt32 rc = 0;
UInt32 i;
IntPtr recPtr = hostBuffer;
UInt64 word;
{
data = new UInt16[8192]
};
for (i = 0; (rc == MAXT_SUCCESS) && (i < MAX_TX_SUBFRAMES_TO_TRANSMIT); i++)
{
rec.timeTag = 0;
rec.control = 0;
rec.dataSize = 2 * (UInt32)SUBFRAMESIZE; // 16 bits per word in subframe
rec.repeatCount = 1;
rec.reserved = 0;
for (word = 0; word < SUBFRAMESIZE; word++)
{
if (word == 0)
{
//1st word of each subframe has to be a sync word
// sync words are:
// - 0x247 for subframe #0
// - 0x5B8 for subframe #1
// - 0xA47 for subframe #2
// - 0xDB8 for subframe #3
switch (i % 4)
{
case 0:
rec.data[word] = 0x247;
break;
case 1:
rec.data[word] = 0x5B8;
break;
case 2:
rec.data[word] = 0xA47;
break;
case 3:
rec.data[word] = 0xDB8;
break;
default:
break;
}
}
else
rec.data[word] = (UInt16)(0x11 * word);
}
if (rc == MAXT_SUCCESS)
{
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfA717NextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Transmitting ...\n");
// Transmits strings on relative record time
rc = mxfA717TxAperiodicWrite(buffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_SUBFRAMES_TO_TRANSMIT, hostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep((MAX_TX_SUBFRAMES_TO_TRANSMIT + 2) * 1000); // 1 second per subframe + 2 seconds to be sure to exceed the minimum duration to wait.
if (rc != MAXT_SUCCESS)
Console.Write("Transmit failed; rc=0x{0:x8}\n", rc);
else
Console.Write("\nWriting {0} records\n", i);
return rc;
}
private static UInt32 read717RecordsFromEmbeddedFlash(UInt64 flash, UInt64 fileIdx)
{
IntPtr recPtr717 = IntPtr.Zero, p = IntPtr.Zero;
UInt64 msgReadCnt, byteReadCnt;
UInt64 word, data;
UInt32 rc = 0;
{
data = new UInt16[8192]
};
// Allocates host buffer
try
{
recPtr717 = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
if (rc == MAXT_SUCCESS)
{
try
{
p = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Reads all the messages on flash in the buffer allocated previously
Console.Write("Reading from flash\n\n");
if (rc == MAXT_SUCCESS)
{
do
{
rc = mxfA717EmbeddedNVStorageFileMsgRead(flash, fileIdx, 0, BUFFER_SIZE, out msgReadCnt, out byteReadCnt, p);
// Displays all records written by the embedded application
if (rc == MAXT_SUCCESS)
Console.Write("String received count = {0} \n", msgReadCnt);
if (rc == MAXT_SUCCESS)
{
// Displays received strings
recPtr717 = p;
for (data = 0; (data < msgReadCnt) && (rc == MAXT_SUCCESS); data++)
{
rec717 = (MXF_A717_DATAREC)Marshal.PtrToStructure(recPtr717, typeof(MXF_A717_DATAREC));
Console.Write("\n{0:D2}: Timetag={1:D12}, Size={2} words\n", data, rec717.timeTag, (rec717.dataSize) / 2);
for (word = 0; word < SUBFRAMESIZE; word++)
Console.Write("{0:X3} ", rec717.data[word]);
Console.Write("\n");
rc = mxfA717NextDataRecordPtrGet(recPtr717, out recPtr717);
}
}
if (rc != MAXT_SUCCESS)
Console.Write("Acquisition read failed; rc=0x{0:x8}\n", rc);
} while ((rc == MAXT_SUCCESS) && (msgReadCnt != 0));
}
if (recPtr717 != IntPtr.Zero)
Marshal.FreeHGlobal(recPtr717);
return rc;
}
private static UInt32 initHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
UInt64 device =0;
MXF_DEVICE_INFO deviceInfo = new MXF_DEVICE_INFO();
UInt32 rc;
if (attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceInfoGet(device, out deviceInfo);
if ((rc == MAXT_SUCCESS) && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI_EH))
{
// Sets MXF_MODULE_MULTI_EH first TX and RX channels to A717
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
value = MXF_CLASS_A717;
return Convert.ToUInt32(true);
}
}
}
return Convert.ToUInt32(false);
}
}
}
Updated 10/23/2023