SQEvent1.c -- Display SynqNet Events
/* SQEvent1.c */
/* Copyright(c) 1991-2006 by Motion Engineering, Inc. All rights reserved.
*
* This software contains proprietary and confidential information of
* Motion Engineering Inc., and its suppliers. Except as may be set forth
* in the license agreement under which this software is supplied, use,
* disclosure, or reproduction is prohibited without the prior express
* written consent of Motion Engineering, Inc.
*/
/*
:Display SynqNet Events
This is a simple program to demonstrate how to use the EventMgr to catch
SynqNet events, read the event message and display the event's information.
Warning! This is a sample program to assist in the integration of an
MEI motion controller with your application. It may not contain all
of the logic and safety features that your application requires.
The msgCHECK(...) macros used in the following sample code are intended
to convey our strong belief that ALL error return codes should be checked.
Actual application code should use specific error handling techniques (other
than msgCHECKs) best suited to your internal error recovery methods.
*/
#include <stdlib.h>
#include <stdio.h>
#include "stdmpi.h"
#include "stdmei.h"
#include "apputil.h"
#define WAIT_TIME (200) /* msec */
/* Parse the command line */
long parseCommandLine(int argc,
char *argv[],
long *synqNetNumber,
MPIControlType *controlType,
MPIControlAddress *controlAddress)
{
long argIndex;
/* Command line arguments and defaults */
Arg argList[] =
{
{ "-synqNet", ArgTypeLONG, &synqNetNumber, },
{ NULL, ArgTypeINVALID, NULL, }
};
/* Parse command line for Control type and address */
argIndex =
argControl(argc,
argv,
controlType,
controlAddress);
/* Parse command line for application-specific arguments */
while (argIndex < argc) {
long argIndexNew;
argIndexNew = argSet(argList, argIndex, argc, argv);
if (argIndexNew <= argIndex) {
argIndex = argIndexNew;
break;
}
else {
argIndex = argIndexNew;
}
}
/* Check for unknown/invalid command line arguments */
if (argIndex < argc) {
fprintf(stderr,"usage: %s %s\n"
"\t\t[-synqNet # (0 .. %d)]\n",
argv[0],
ArgUSAGE,
MEIXmpMaxSynqNets - 1);
exit(MPIMessageARG_INVALID);
}
return 0;
}
/* Create and initialize MPI objects */
void programInit(MPIControl *control,
MPIControlType controlType,
MPIControlAddress *controlAddress,
MEISynqNet *synqNet,
long synqNetNumber,
MPINotify *notify,
MPIEventMgr *eventMgr,
Service *service)
{
MPIEventMask eventMask;
long returnValue;
/* Create motion controller object */
*control =
mpiControlCreate(controlType,
controlAddress);
msgCHECK(mpiControlValidate(*control));
/* Initialize motion controller */
returnValue =
mpiControlInit(*control);
msgCHECK(returnValue);
/* Create SynqNet object */
*synqNet =
meiSynqNetCreate(*control,
synqNetNumber);
msgCHECK(meiSynqNetValidate(*synqNet));
/* Request notification of all events from SynqNet */
mpiEventMaskCLEAR(eventMask);
returnValue =
meiSynqNetEventNotifyGet(*synqNet,
&eventMask,
NULL);
msgCHECK(returnValue);
meiEventMaskSYNQNET(eventMask);
returnValue =
meiSynqNetEventNotifySet(*synqNet,
eventMask,
NULL);
msgCHECK(returnValue);
/* Create event notification object for NULL (all objects) */
*notify =
mpiNotifyCreate(eventMask,
NULL);
msgCHECK(mpiNotifyValidate(*notify));
/* Create event manager object */
*eventMgr = mpiEventMgrCreate(*control);
msgCHECK(mpiEventMgrValidate(*eventMgr));
/* Append notify to event manager */
returnValue =
mpiEventMgrNotifyAppend(*eventMgr,
*notify);
msgCHECK(returnValue);
/* Create service thread */
*service =
serviceCreate(*eventMgr,
-1, /* default (max) priority */
-1); /* -1 => enable interrupts */
if (*service == NULL) {
msgCHECK(MPIMessageHANDLE_INVALID);
}
}
/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl *control,
MEISynqNet *synqNet,
MPINotify *notify,
MPIEventMgr *eventMgr,
Service *service)
{
long returnValue;
/* Delete service thread */
returnValue =
serviceDelete(*service);
msgCHECK(returnValue);
*service = MPIHandleVOID;
/* Delete event manager object */
returnValue =
mpiEventMgrDelete(*eventMgr);
msgCHECK(returnValue);
*eventMgr = MPIHandleVOID;
/* Delete event notification object */
returnValue =
mpiNotifyDelete(*notify);
msgCHECK(returnValue);
*notify = MPIHandleVOID;
returnValue =
meiSynqNetDelete(*synqNet);
msgCHECK(returnValue);
*synqNet = MPIHandleVOID;
/* Delete control handle */
returnValue =
mpiControlDelete(*control);
msgCHECK(returnValue);
*control = MPIHandleVOID;
}
/* Display SynqNet events until the user presses a key */
void displaySynqNetEventsUntilKeyPressed(MEISynqNet synqNet,
MPINotify notify)
{
long returnValue;
printf("Waiting for SynqNet Events... <Press any key to exit>\n");
while (meiPlatformKey(MPIWaitPOLL) <= 0) {
MPIEventStatus eventStatus;
/* Wait for synqNet event */
returnValue =
mpiNotifyEventWait(notify,
&eventStatus,
WAIT_TIME);
if (returnValue == MPIMessageOK) {
switch (eventStatus.type) {
case MEIEventTypeSYNQNET_DEAD: {
printf("SynqNet Network Error.\n");
break;
}
case MEIEventTypeSYNQNET_RX_FAILURE: {
printf("SynqNet Rx Failure.\n");
break;
}
case MEIEventTypeSYNQNET_TX_FAILURE: {
printf("SynqNet Tx Failure.\n");
break;
}
case MEIEventTypeSYNQNET_NODE_FAILURE: {
MEISynqNetStatus synqNetStatus;
/* Read the network status */
returnValue =
meiSynqNetStatus(synqNet,
&synqNetStatus);
msgCHECK(returnValue);
printf("SynqNet Node Failure. Failed Node Mask:0x%X\n",
synqNetStatus.failedNodeMask[0]); /* First 32 nodes */
break;
}
case MEIEventTypeSYNQNET_RECOVERY: {
MEISynqNetCableList idleCable;
MPIEventMask eventMask;
printf("SynqNet Recovery.\n WARNING: Network cable failure, data packets redirected.\n");
/* Determine cable failure location. Packets redirected around the idle link. */
returnValue =
meiSynqNetIdleCableListGet(synqNet,
&idleCable);
msgCHECK(returnValue);
printf(" Idle link is cable %ld\n",
idleCable.cableNumber[0]);
/* Reset recovery event for re-trigger. */
mpiEventMaskCLEAR(eventMask);
mpiEventMaskSET(eventMask, MEIEventTypeSYNQNET_RECOVERY);
returnValue =
meiSynqNetEventReset(synqNet,
eventMask);
msgCHECK(returnValue);
break;
}
default: {
printf("Unexpected Event %ld\n", eventStatus.type);
break;
}
}
}
else if (returnValue == MPIMessageTIMEOUT) { /* ignore timeouts */
returnValue = MPIMessageOK;
}
msgCHECK(returnValue);
}
}
int main(int argc,
char *argv[])
{
MPIControl control;
MPIControlType controlType;
MPIControlAddress controlAddress;
MEISynqNet synqNet;
MPINotify notify;
MPIEventMgr eventMgr;
Service service;
long synqNetNumber = 0;
long returnValue;
/* Parse command line */
returnValue =
parseCommandLine(argc,
argv,
&synqNetNumber,
&controlType,
&controlAddress);
msgCHECK(returnValue);
/* Create and initialize MPI objects */
programInit(&control,
controlType,
&controlAddress,
&synqNet,
synqNetNumber,
¬ify,
&eventMgr,
&service);
/* Display SynqNet events until the user presses a key */
displaySynqNetEventsUntilKeyPressed(synqNet,
notify);
/* Perform certain cleanup actions and delete MPI objects */
programCleanup(&control,
&synqNet,
¬ify,
&eventMgr,
&service);
return ((int)returnValue);
}
|