EtherScriptCommands
Edit this on GitLab
EtherScriptCommands Sample Application (SSK 1.x)
Overview
The EtherScriptCommands sample application demonstrates how to use Ethernet Script Commands on NAI boards using the NAI Software Support Kit (SSK 1.x). Scripts allow you to store a sequence of Ethernet commands (register reads, writes, mask operations) on the board under a numeric script ID, then execute the entire sequence with a single command. This offloads repetitive multi-step operations to the board, reducing Ethernet traffic and host-side complexity.
Scripts also enable the board’s safe-state feature: you can designate a script as the "safe state" script, which the board executes automatically when it detects that Ethernet communication has been lost (as determined by the NoCommsWatchdogTimer). This lets the board transition outputs to a known-safe configuration without host intervention — a critical capability for systems where a host failure must not leave outputs in an undefined state.
This is a board-level sample — it is not specific to any module type. It works with any NAI board that supports Generation 4 (or later) Ethernet commands.
Prerequisites
Before running this sample, make sure you have:
-
An NAI board connected via Ethernet that supports Gen4 Ethernet commands.
-
SSK 1.x installed on your development host.
-
The sample applications built. Refer to the SSK 1.x build instructions for your platform if you have not already compiled them.
How to Run
Launch the EtherScriptCommands executable from your build output directory. On startup the application looks for a configuration file (default_Ether_Script.txt). On the first run, this file will not exist — the application will present an interactive board menu where you configure a board connection and card index. You can save this configuration so that subsequent runs skip the menu and connect automatically. Once connected, a command menu lets you exercise each script operation.
Board Connection
|
Note
|
This startup sequence is common to all NAI sample applications. The board connection code shown here is not specific to any module type. For details on board connection configuration, see the First Time Setup Guide. |
The main() function follows the standard SSK 1.x startup flow. No module selection is needed because script commands operate at the board level.
-
Call
naiapp_RunBoardMenu()to load a saved configuration file (if one exists) or present the interactive board menu. The configuration file (default_Ether_Script.txt) is not included with the SSK — it is created when you save your connection settings from the board menu. On the first run, the menu will always appear. -
Query the user for a card index with
naiapp_query_CardIndex(). -
Check Gen4 support with
SupportsGen4Ether()and enter the command loop.
#if defined (__VXWORKS__)
int32_t EtherScriptCommands(void)
#else
int32_t main(void)
#endif
{
bool_t stop = FALSE;
int32_t cardIndex;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
if (naiapp_RunBoardMenu(CONFIG_FILE) == TRUE)
{
while (stop != TRUE)
{
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
if (stop != TRUE)
{
Run_EtherScriptCommands(cardIndex);
}
printf("\nType Q to quit or Enter key to restart application:\n");
stop = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
}
}
printf("\nType the Enter key to exit the program: ");
naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
naiapp_access_CloseAllOpenCards();
return 0;
}
|
Important
|
Common connection errors you may encounter at this stage:
|
Program Structure
Entry Point
On standard platforms the entry point is main(). On VxWorks the entry point is EtherScriptCommands() — the SSK 1.x build system selects the correct variant via a preprocessor guard:
#if defined (__VXWORKS__)
int32_t EtherScriptCommands(void)
#else
int32_t main(void)
#endif
The startup flow is the same in both cases:
-
Attempt to load the saved configuration file via
naiapp_RunBoardMenu(CONFIG_FILE). If the file does not yet exist, the interactive board menu is presented instead. -
Enter a loop that queries for card index.
-
Call
Run_EtherScriptCommands()to enter the interactive command loop. -
On exit, close all open board connections with
naiapp_access_CloseAllOpenCards().
Application Parameters
The Run_EtherScriptCommands() function populates an naiapp_AppParameters_t struct that is passed to every command handler. For script commands, only the card index is needed:
naiapp_AppParameters_t ether_params;
p_naiapp_AppParameters_t etherscript_params = ðer_params;
etherscript_params->cardIndex = cardIndex;
bGen4ScriptCommands = SupportsGen4Ether(cardIndex);
Command Loop
Run_EtherScriptCommands() drives the interactive command loop. On each iteration it displays the command menu and dispatches the user’s selection to the matching handler function:
| Command | Description |
|---|---|
Write |
Write a script configuration (sequence of Ethernet commands) to a script ID |
Read |
Read back the commands stored in a script |
Clear |
Remove a script configuration |
Run |
Run a script and return the output responses |
Execute |
Execute a script without returning output (fire-and-forget) |
SetID |
Designate a script ID as the safe state script |
GetID |
Query which script ID is currently set as the safe state script |
SetTimeout |
Set the Ethernet communication watchdog timeout |
GetTimeout |
Get the current Ethernet communication watchdog timeout |
Update |
Write test values to registers (for verifying script execution results) |
The menu-driven structure is a convenience of the sample application. In your own application, you would call the same underlying naibrd_Ether_*Script*() API functions directly.
Script Configuration
This section covers writing, reading, and clearing script configurations. A script is a stored sequence of one or more Ethernet commands identified by a numeric script ID. Once written to the board, the script can be executed repeatedly without resending the commands.
Write Script Configuration
To store a script on the board in your own application, construct the command sequence as a byte array and call naibrd_Ether_WriteScript(). The board stores the script internally and associates it with the specified script ID.
uint8_t commands[MAX_ETHER_SCRIPT_CMD_CNT * MAX_ETHER_SCRIPT_CMD_LEN];
uint16_t cmdcount = 0;
uint32_t cmdlen = 0;
/* Build the command sequence */
InitScriptCommands(scriptid, &cmdcount, &cmdlen, commands);
/* Write the script to the board */
nai_status_t status = naibrd_Ether_WriteScript(cardIndex, (uint16_t)scriptid,
cmdcount, cmdlen, commands);
-
scriptid— a numeric ID that identifies this script. -
cmdcount— the number of individual Ethernet commands in the script. -
cmdlen— the total byte length of all commands combined. -
commands[]— a byte array containing the serialized Ethernet commands.
The sample’s InitScriptCommands() function builds different command sequences depending on the script ID. Script ID 1 creates a single command that performs a soft reset (writes 1 to NAI_GEN5_REG_SOFT_RESET). All other script IDs create write-register commands that write test data to unused motherboard registers. The commands are constructed using the low-level Ethernet message API:
/* Build a WriteRegs command for the script */
msgIndex = (uint16_t)nai_ether_BeginWriteMessage(&commands[startIndex],
seqno, NAI_ETHER_GEN4, intf, regaddr, stride, count, NAI_REG32);
msgIndex = (uint16_t)nai_ether_WriteMessageData(&commands[startIndex],
msgIndex, NAI_REG32, data, NAI_REG32, count);
msgIndex = (uint16_t)nai_ether_FinishMessage(&commands[startIndex],
msgIndex, NAI_ETHER_GEN4);
These three functions — nai_ether_BeginWriteMessage(), nai_ether_WriteMessageData(), and nai_ether_FinishMessage() — construct a properly framed Gen4 Ethernet write command. In your own application, use the same sequence to build any Ethernet command you want to include in a script.
For multi-board systems, the sample iterates over all connected boards and creates separate write commands for each. Onboard commands use NAI_INTF_ONBOARD as the interface flag; offboard commands use the remote board’s base address from the system configuration (VME or PCI).
Read Script Configuration
To read back the commands stored in a script, call naibrd_Ether_ReadScript(). This retrieves the raw command bytes and the command count.
uint32_t arraysize = MAX_ETHER_SCRIPT_CMD_CNT * MAX_ETHER_SCRIPT_CMD_LEN;
uint8_t commands[MAX_ETHER_SCRIPT_CMD_CNT * MAX_ETHER_SCRIPT_CMD_LEN];
uint16_t cmdcount = 0;
nai_status_t status = naibrd_Ether_ReadScript(cardIndex, (uint16_t)scriptid,
&cmdcount, arraysize, commands);
The sample then parses and displays each command using ParseScriptCommand() and DisplayDecodedEthScriptCommand(). These functions locate individual commands within the byte stream by searching for Gen4 preamble and postamble markers, then decode the command type, flags (onboard/offboard, register size), address, count, stride, and data.
Clear Script Configuration
To remove a script from the board and free the script ID for reuse, call naibrd_Ether_ClearScript():
nai_status_t status = naibrd_Ether_ClearScript(cardIndex, (uint16_t)scriptid);
|
Important
|
Common Errors
|
Script Execution
Once a script is stored on the board, you can execute it in two ways: "Execute" (fire-and-forget) or "Run" (execute and return results).
Execute Script
To execute a script without waiting for or receiving output, call naibrd_Ether_ExecuteScript(). The board executes all commands in the script but does not return any response data. This is appropriate when the script only performs write operations and you do not need confirmation of each individual command result.
nai_status_t status = naibrd_Ether_ExecuteScript(cardIndex, (uint16_t)scriptid);
Run Script
To execute a script and receive the response data from each command, call naibrd_Ether_RunScript(). The board executes all commands and returns the combined responses in a buffer. This is useful when the script contains read operations and you need the resulting data.
uint8_t buffer[256];
int32_t recv_len = 0;
nai_status_t status = naibrd_Ether_RunScript(cardIndex, (uint16_t)scriptid,
1, 256, &recv_len, &buffer[0]);
-
The third parameter is the expected number of response commands.
-
The fourth parameter is the size of the receive buffer.
-
recv_len— on return, the actual number of bytes received.
The sample prints the raw hex output of the response buffer. In your own application, you would parse the response buffer to extract the individual command results.
|
Important
|
Common Errors
|
Safe State and Communication Timeout
The safe state feature ties scripts to the board’s communication watchdog. If the board does not receive any Ethernet operation messages within the configured timeout period, it automatically executes the designated safe state script. This is a critical safety mechanism for systems where loss of host communication must trigger a deterministic response.
Set Safe State Script ID
To designate a script as the safe state script in your own application, call naibrd_Ether_SetSafeStateScriptId():
nai_status_t status = naibrd_Ether_SetSafeStateScriptId(cardIndex, (uint16_t)scriptid);
The script must already be written to the board via naibrd_Ether_WriteScript() before designating it as the safe state script.
Get Safe State Script ID
To query which script ID is currently designated as the safe state script:
uint16_t scriptid;
nai_status_t status = naibrd_Ether_GetSafeStateScriptId(cardIndex, &scriptid);
Set Communication Timeout
To configure the NoCommsWatchdogTimer — the period the board waits without receiving Ethernet traffic before executing the safe state script — call naibrd_Ether_SetNoCommsWatchdogTimer():
uint32_t timeout;
nai_status_t status = naibrd_Ether_SetNoCommsWatchdogTimer(cardIndex, timeout);
-
timeout— the watchdog timeout value in seconds. A value of 0 disables the watchdog.
Get Communication Timeout
To read back the current watchdog timeout value:
uint32_t timeout = 0;
nai_status_t status = naibrd_Ether_GetNoCommsWatchdogTimer(cardIndex, &timeout);
|
Important
|
Common Errors
|
Test Register Operations
The sample includes an "Update" command that writes user-specified values to test registers via naibrd_WriteReg32(). This is a convenience for verifying that scripts executed correctly — you can manually set register values and then run a script that reads them, or vice versa.
uint32_t regAddr = UNUSED_REG_ADDR_FOR_TEST;
uint32_t regVal = 0;
for (i = 0; i < SCRIPT_TEST_REGISTER_COUNT; i++)
{
bQuit = QueryRegisterValue(regAddr, ®Val);
if (!bQuit)
{
naibrd_WriteReg32(cardIndex, 0, regAddr, regVal);
regAddr += TEST_REGISTER_STRIDE;
}
}
The sample uses addresses 0x0180 and 0x0188 (with a stride of 0x08) in the motherboard common area. These are unused registers chosen specifically for testing purposes. In your own application, you would not typically need this test functionality.
Troubleshooting Reference
This table summarizes common errors and symptoms covered in the sections above. For detailed context on each entry, refer to the relevant section.
| Error / Symptom | Possible Causes | Suggested Resolution |
|---|---|---|
No board found or connection timeout |
Board not powered, incorrect or missing configuration file, network issue |
Verify hardware is powered and connected via Ethernet. If |
Gen4 not supported |
Board uses pre-Generation 4 Ethernet protocol |
Script commands require Gen4 or later. Verify your board’s firmware version. |
Script execution has no effect |
Script ID not configured, or script contains only write commands to invalid addresses |
Write the script before executing. Verify that register addresses in the script are valid for your board. |
Run returns empty response |
Script contains only write commands (no reads), or receive buffer too small |
Include read commands in the script if you need response data. Increase buffer size. |
Safe state triggers during normal operation |
Communication watchdog timeout too short |
Increase the timeout value via |
Board resets unexpectedly |
Safe state script contains a soft reset command (e.g., script ID 1 in this sample) |
Do not assign a reset script as the safe state script unless reset is the intended safe-state behavior. |
Safe state does not trigger on communication loss |
Timeout set to 0 (disabled) or safe state script ID not assigned |
Set a nonzero timeout and assign a valid safe state script ID. |
"Ethernet Script Command Support Prior to Generation 4 Ethernet commands currently not supported" |
Board does not support Gen4 Ethernet |
Use a board with Gen4+ Ethernet support. |
Full Source
The complete source for this sample is provided below for reference. The sections above explain each part in detail.
Full Source — EtherScriptCommands.c (SSK 1.x)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
/* Common Sample Program include files */
#include "include/naiapp_boardaccess_menu.h"
#include "include/naiapp_boardaccess_query.h"
#include "include/naiapp_boardaccess_access.h"
#include "include/naiapp_boardaccess_display.h"
#include "include/naiapp_boardaccess_utils.h"
/* naibrd include files */
#include "nai.h"
#include "naibrd.h"
#include "naibrd_ether.h"
#include "advanced/nai_ether_adv.h"
#include "boards/naibrd_gen5.h"
/* Extern Functions or Variables*/
extern NAIAPPFUNC naiapp_syscfg_access_t g_NAISysCfgAccess[NAI_MAX_CARDS];
static const int8_t *CONFIG_FILE = (const int8_t *)"default_Ether_Script.txt";
/* Function prototypes */
static bool_t Run_EtherScriptCommands(int32_t cardIndex);
static bool_t QueryEthScriptID(int32_t *scriptid);
static void InitScriptCommands(int32_t scriptid, uint16_t *count, uint32_t *cmdlen, uint8_t command[]);
static void MakeWriteRegsCommandForScript(bool_t bGen4Ether, bool_t bOnboard, uint16_t startIndex, uint32_t regaddr, uint32_t count, uint32_t stride, uint32_t data[], uint8_t commands[], uint16_t *cmdlen);
static nai_status_t WriteEthScriptConfig(int32_t paramCount, int32_t* p_params);
static nai_status_t ReadEthScriptConfig(int32_t paramCount, int32_t* p_params);
static nai_status_t ClearEthScriptConfig(int32_t paramCount, int32_t* p_params);
static nai_status_t ExecuteEthScript(int32_t paramCount, int32_t* p_params);
static nai_status_t SetSafeStateScriptID(int32_t paramCount, int32_t* p_params);
static nai_status_t RunEthScript(int32_t paramCount, int32_t* p_params);
static nai_status_t GetSafeStateScriptID(int32_t paramCount, int32_t* p_params);
static bool_t QueryEthTimeOut(uint32_t *timeout);
static nai_status_t SetEtherCommTimeout(int32_t paramCount, int32_t* p_params);
static nai_status_t GetEtherCommTimeout(int32_t paramCount, int32_t* p_params);
static bool_t QueryRegisterValue(uint32_t regaddr, uint32_t *regval);
static nai_status_t UpdateTestRegister(int32_t paramCount, int32_t* p_params);
static bool_t ParseScriptCommand(int32_t startIndex, int32_t cmdarraysize, uint8_t command[], uint16_t *cmdlen, int32_t scriptcmdarraysize, uint8_t script_command[]);
static void DisplayDecodedEthScriptCommand(uint16_t cmdcount, uint16_t cmdlen, uint8_t script_command[]);
static void DecodedScriptCmdEthFlags(uint16_t flags, uint8_t *onoffbrd, uint8_t *regsize);
static bool_t bGen4ScriptCommands = FALSE;
#define SCRIPT_TEST_REGISTER_COUNT 2
static uint32_t UNUSED_REG_ADDR_FOR_TEST = 0x0180;
static uint32_t TEST_REGISTER_STRIDE = 0x08;
/****** Command Table *******/
enum eth_script_commands
{
ETH_SCRIPT_CMD_WRITE,
ETH_SCRIPT_CMD_READ,
ETH_SCRIPT_CMD_CLEAR,
ETH_SCRIPT_CMD_RUN,
ETH_SCRIPT_CMD_EXECUTE,
ETH_SCRIPT_CMD_SET_SAFE_ID,
ETH_SCRIPT_CMD_GET_SAFE_ID,
ETH_SCRIPT_CMD_SET_COMM_TIMEOUT,
ETH_SCRIPT_CMD_GET_COMM_TIMEOUT,
ETH_SCRIPT_CMD_UPDATE_TEST_REG,
ETH_SCRIPT_CMD_COUNT
};
naiapp_cmdtbl_params_t ETH_ScriptcmdMenuCmds[] = {
{"Write", "Write Script Configuration", ETH_SCRIPT_CMD_WRITE, WriteEthScriptConfig},
{"Read", "Read Script Configuration", ETH_SCRIPT_CMD_READ, ReadEthScriptConfig},
{"Clear", "Clear Script Configuration", ETH_SCRIPT_CMD_CLEAR, ClearEthScriptConfig},
{"Run", "Run Script with Output", ETH_SCRIPT_CMD_RUN, RunEthScript},
{"Execute", "Execute Script", ETH_SCRIPT_CMD_EXECUTE, ExecuteEthScript},
{"SetID", "Set Safe State Script ID", ETH_SCRIPT_CMD_SET_SAFE_ID, SetSafeStateScriptID},
{"GetID", "Get Safe State Script ID", ETH_SCRIPT_CMD_GET_SAFE_ID, GetSafeStateScriptID},
{"SetTimeout", "Set Ethernet Comm Timeout", ETH_SCRIPT_CMD_SET_COMM_TIMEOUT, SetEtherCommTimeout},
{"GetTimeout", "Get Ethernet Comm Timeout", ETH_SCRIPT_CMD_GET_COMM_TIMEOUT, GetEtherCommTimeout},
{"Update", "Update Test Registers", ETH_SCRIPT_CMD_UPDATE_TEST_REG, UpdateTestRegister},
};
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t EtherScriptCommands(void)
#else
int32_t main(void)
#endif
{
bool_t stop = FALSE;
int32_t cardIndex;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
if (naiapp_RunBoardMenu(CONFIG_FILE) == TRUE)
{
while (stop != TRUE)
{
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
if (stop != TRUE)
{
Run_EtherScriptCommands(cardIndex);
}
printf("\nType Q to quit or Enter key to restart application:\n");
stop = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
}
}
printf("\nType the Enter key to exit the program: ");
naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
naiapp_access_CloseAllOpenCards();
return 0;
}
/**************************************************************************************************************/
static bool_t Run_EtherScriptCommands(int32_t cardIndex)
{
bool_t bQuit = FALSE;
bool_t bContinue = TRUE;
bool_t bCmdFound = FALSE;
int32_t cmd;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiapp_AppParameters_t ether_params;
p_naiapp_AppParameters_t etherscript_params = ðer_params;
etherscript_params->cardIndex = cardIndex;
bGen4ScriptCommands = SupportsGen4Ether(cardIndex);
while (bContinue)
{
naiapp_utils_LoadParamMenuCommands(ETH_SCRIPT_CMD_COUNT, ETH_ScriptcmdMenuCmds);
naiapp_display_ParamMenuCommands((int8_t *)"Ethernet Script Command Menu");
printf("\nType Ethernet Script command or %c to quit : ", NAI_QUIT_CHAR);
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
bCmdFound = naiapp_utils_GetParamMenuCmdNum(inputResponseCnt, inputBuffer, &cmd);
if (bCmdFound)
{
switch (cmd)
{
case ETH_SCRIPT_CMD_WRITE:
case ETH_SCRIPT_CMD_READ:
case ETH_SCRIPT_CMD_CLEAR:
case ETH_SCRIPT_CMD_EXECUTE:
case ETH_SCRIPT_CMD_RUN:
case ETH_SCRIPT_CMD_SET_SAFE_ID:
case ETH_SCRIPT_CMD_GET_SAFE_ID:
case ETH_SCRIPT_CMD_SET_COMM_TIMEOUT:
case ETH_SCRIPT_CMD_GET_COMM_TIMEOUT:
case ETH_SCRIPT_CMD_UPDATE_TEST_REG:
ETH_ScriptcmdMenuCmds[cmd].func(APP_PARAM_COUNT, (int32_t*)etherscript_params);
break;
default:
printf("Invalid command entered\n");
break;
}
}
else
printf("Invalid command entered\n");
}
}
else
bContinue = FALSE;
}
return bQuit;
}
/**************************************************************************************************************/
static bool_t QueryEthScriptID(int32_t *scriptid)
{
bool_t bQuit = FALSE;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
printf("Enter the Script ID: (default: 1) > ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt == 0)
*scriptid = 1;
else
{
*scriptid = atol((const char *)inputBuffer);
}
}
return bQuit;
}
/**************************************************************************************************************/
static void InitScriptCommands(int32_t scriptid, uint16_t *count, uint32_t *cmdlen, uint8_t command[])
{
uint16_t msgIndex = 0;
uint16_t cmdcnt = 0;
uint16_t ethcmdlen = 0;
uint16_t scriptcmdlen = 0;
uint32_t data[SCRIPT_TEST_REGISTER_COUNT];
int i, boardIndex;
switch (scriptid)
{
case 1:
data[0] = 1;
MakeWriteRegsCommandForScript(bGen4ScriptCommands, TRUE, msgIndex, NAI_GEN5_REG_SOFT_RESET, 1, 0, data, command, ðcmdlen);
msgIndex += ethcmdlen;
scriptcmdlen += ethcmdlen;
cmdcnt++;
break;
default:
for (boardIndex = 0; boardIndex < naiapp_GetBoardCnt(); boardIndex++)
{
for (i = 0; i < SCRIPT_TEST_REGISTER_COUNT; i++)
{
if (i == 0)
data[i] = scriptid | 0xDEAD0000;
else if (i == 1)
data[i] = scriptid | 0xBEEF0000;
else
data[i] = scriptid | 0xABCD0000;
}
if (boardIndex == 0)
MakeWriteRegsCommandForScript(bGen4ScriptCommands, TRUE, msgIndex, UNUSED_REG_ADDR_FOR_TEST, SCRIPT_TEST_REGISTER_COUNT, TEST_REGISTER_STRIDE, data, command, ðcmdlen);
else
{
if (g_NAISysCfgAccess[boardIndex].comm == NAIBRD_COMM_VME)
MakeWriteRegsCommandForScript(bGen4ScriptCommands, FALSE, msgIndex, g_NAISysCfgAccess[boardIndex].vmeCfg.boardAddress + UNUSED_REG_ADDR_FOR_TEST, SCRIPT_TEST_REGISTER_COUNT, TEST_REGISTER_STRIDE, data, command, ðcmdlen);
else if (g_NAISysCfgAccess[boardIndex].comm == NAIBRD_COMM_PCI)
MakeWriteRegsCommandForScript(bGen4ScriptCommands, FALSE, msgIndex, g_NAISysCfgAccess[boardIndex].pciCfg.boardAddress + UNUSED_REG_ADDR_FOR_TEST, SCRIPT_TEST_REGISTER_COUNT, TEST_REGISTER_STRIDE, data, command, ðcmdlen);
}
msgIndex += ethcmdlen;
scriptcmdlen += ethcmdlen;
cmdcnt++;
}
break;
}
*count = cmdcnt;
*cmdlen = scriptcmdlen;
}
/**************************************************************************************************************/
static void MakeWriteRegsCommandForScript(bool_t bGen4Ether, bool_t bOnboard, uint16_t startIndex, uint32_t regaddr, uint32_t count, uint32_t stride, uint32_t data[], uint8_t commands[], uint16_t *cmdlen)
{
uint16_t msgIndex = startIndex;
uint16_t seqno;
nai_intf_t intf;
if (bGen4Ether)
{
seqno = 0;
if (bOnboard)
intf = NAI_INTF_ONBOARD;
else
intf = 0;
msgIndex = (uint16_t)nai_ether_BeginWriteMessage(&commands[startIndex],seqno,NAI_ETHER_GEN4,intf,regaddr,stride,count,NAI_REG32);
if (msgIndex >= 0)
{
msgIndex = (uint16_t)nai_ether_WriteMessageData(&commands[startIndex],msgIndex,NAI_REG32,data,NAI_REG32,count);
if (msgIndex >= 0)
{
msgIndex = (uint16_t)nai_ether_FinishMessage(&commands[startIndex],msgIndex,NAI_ETHER_GEN4);
}
}
*cmdlen = msgIndex;
}
}
/**************************************************************************************************************/
static nai_status_t WriteEthScriptConfig(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
uint8_t commands[MAX_ETHER_SCRIPT_CMD_CNT * MAX_ETHER_SCRIPT_CMD_LEN];
uint16_t cmdcount = 0;
uint32_t cmdlen = 0;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
int32_t scriptid;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
bQuit = QueryEthScriptID(&scriptid);
if (!bQuit)
{
InitScriptCommands(scriptid, &cmdcount, &cmdlen, commands);
status = check_status(naibrd_Ether_WriteScript(cardIndex, (uint16_t)scriptid, cmdcount, cmdlen, commands));
if (status == NAI_SUCCESS)
{
printf("Configured Script ID = %d.\n", scriptid);
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static nai_status_t ReadEthScriptConfig(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
int32_t scriptid;
uint16_t cmdcount = 0;
uint32_t arraysize = MAX_ETHER_SCRIPT_CMD_CNT * MAX_ETHER_SCRIPT_CMD_LEN;
uint8_t commands[MAX_ETHER_SCRIPT_CMD_CNT * MAX_ETHER_SCRIPT_CMD_LEN];
int32_t i, startIndex;
uint16_t cmdlen;
uint8_t script_command[MAX_ETHER_SCRIPT_CMD_LEN];
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
bQuit = QueryEthScriptID(&scriptid);
if (!bQuit)
{
status = check_status(naibrd_Ether_ReadScript(cardIndex, (uint16_t)scriptid, &cmdcount, arraysize, commands));
if (status == NAI_SUCCESS)
{
if (cmdcount > 0)
{
startIndex = 0;
for (i = 0; i < (int32_t)cmdcount; i++)
{
if (ParseScriptCommand(startIndex, arraysize, commands, &cmdlen, MAX_ETHER_SCRIPT_CMD_LEN, script_command))
{
startIndex += cmdlen;
DisplayDecodedEthScriptCommand((uint16_t)i+1, cmdlen, script_command);
}
}
}
else
printf("\n\nNo Commands configured for Script ID = %d\n", scriptid);
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static nai_status_t ClearEthScriptConfig(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
int32_t scriptid;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
bQuit = QueryEthScriptID(&scriptid);
if (!bQuit)
{
status = check_status(naibrd_Ether_ClearScript(cardIndex, (uint16_t)scriptid));
if (status == NAI_SUCCESS)
{
printf("Cleared Script ID = %d.\n", scriptid);
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static nai_status_t RunEthScript(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
uint8_t buffer[256];
int i;
int32_t recv_len = 0;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
int32_t scriptid;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
bQuit = QueryEthScriptID(&scriptid);
if (!bQuit)
{
status = check_status(naibrd_Ether_RunScript(cardIndex, (uint16_t)scriptid, 1, 256, &recv_len, &buffer[0]));
if (status == NAI_SUCCESS)
{
printf("Executed Script ID = %d.\n", scriptid);
printf("Hex Dump of Output:\n");
for (i = 0; i < recv_len; i++)
{
printf("%x", buffer[i]);
}
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static nai_status_t ExecuteEthScript(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
int32_t scriptid;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
bQuit = QueryEthScriptID(&scriptid);
if (!bQuit)
{
status = check_status(naibrd_Ether_ExecuteScript(cardIndex, (uint16_t)scriptid));
if (status == NAI_SUCCESS)
{
printf("Executed Script ID = %d.\n", scriptid);
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static nai_status_t SetSafeStateScriptID(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
int32_t scriptid;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
bQuit = QueryEthScriptID(&scriptid);
if (!bQuit)
{
status = check_status(naibrd_Ether_SetSafeStateScriptId(cardIndex, (uint16_t)scriptid));
if (status == NAI_SUCCESS)
{
printf("Set Script ID = %d as Safe State Script\n", scriptid);
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static nai_status_t GetSafeStateScriptID(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
uint16_t scriptid;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
status = check_status(naibrd_Ether_GetSafeStateScriptId(cardIndex, &scriptid));
if (status == NAI_SUCCESS)
{
printf("Set Script ID = %d as Safe State Script.\n", scriptid);
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static bool_t QueryEthTimeOut(uint32_t *timeout)
{
bool_t bQuit = FALSE;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
printf("Enter the Ethernet Comm Timeout: (default: 0) > ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt == 0)
*timeout = 0;
else
{
*timeout = (uint32_t)atol((const char *)inputBuffer);
}
}
return bQuit;
}
/**************************************************************************************************************/
static nai_status_t SetEtherCommTimeout(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
uint32_t timeout;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
bQuit = QueryEthTimeOut(&timeout);
if (!bQuit)
{
status = check_status(naibrd_Ether_SetNoCommsWatchdogTimer(cardIndex, timeout));
if (status == NAI_SUCCESS)
{
printf("Set Timeout = %d sec.\n", timeout);
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static nai_status_t GetEtherCommTimeout(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
uint32_t timeout = 0;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
status = check_status(naibrd_Ether_GetNoCommsWatchdogTimer(cardIndex, &timeout));
if (status == NAI_SUCCESS)
{
printf("Timeout = %d sec.\n", timeout);
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
static bool_t QueryRegisterValue(uint32_t regaddr, uint32_t *regval)
{
bool_t bQuit = FALSE;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
printf("Enter the Value (in hex) to set for Reg Addr: 0x%08X: (default: 0) > ", regaddr);
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt == 0)
*regval = 0;
else
{
*regval = naiapp_utils_HexStrToDecUInt32(inputBuffer);
}
}
return bQuit;
}
/**************************************************************************************************************/
static nai_status_t UpdateTestRegister(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
nai_status_t status;
uint32_t regAddr = UNUSED_REG_ADDR_FOR_TEST;
uint32_t regVal = 0;
int i;
p_naiapp_AppParameters_t etherscript_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = etherscript_params->cardIndex;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
for (i = 0; i < SCRIPT_TEST_REGISTER_COUNT; i++)
{
bQuit = QueryRegisterValue(regAddr, ®Val);
if (!bQuit)
{
status = check_status(naibrd_WriteReg32(cardIndex, 0, regAddr, regVal));
if (status == NAI_SUCCESS)
{
printf("Set RegAddr = 0x%08X to 0x%08X\n", regAddr, regVal);
}
regAddr += TEST_REGISTER_STRIDE;
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}