/****************************************************************************** * * Title : * Version 0.0 * * Description: * Related files: * * Author: Lukas Tomasek, tomasekl@fzu.cz * ******************************************************************************/ /****************************************************************************** * Header files * ******************************************************************************/ #include #include "RccExtControl.h" #include /****************************************************************************** * Global Variables * ******************************************************************************/ struct SHARED_MEM *sharedMemPtr; HANDLE hMapFile; LPVOID lpMapAddress; /*----------------------------------------------------------------------------*/ static int executeList(double timeoutInSec); /*----------------------------------------------------------------------------*/ int sharedMemoryOpen(void){ #ifdef I_AM_TEST_STAND /* i.e. TestStand server */ hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // Current file handle (shared memory). NULL, // Default security. PAGE_READWRITE, // Read/write permission. 0, // Max. object size. sizeof(struct SHARED_MEM), // Size of hFile. SHARED_MEMORY_NAME); // Name of mapping object. if (hMapFile == NULL){ printf("CreateFileMapping() error!\n"); return(-1); } #else /* client, i.e. external client application */ hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission. FALSE, // Do not inherit the name SHARED_MEMORY_NAME); // of the mapping object. if (hMapFile == NULL){ printf("OpenFileMapping() error!\n"); return(-1); } #endif lpMapAddress = MapViewOfFile( hMapFile, // Handle to mapping object. FILE_MAP_ALL_ACCESS, // Read/write permission 0, // Max. object size. 0, // Size of hFile. 0); // Map entire file. if (lpMapAddress == NULL){ printf("MapViewOfFile() error!\n"); CloseHandle(hMapFile); return(-1); } sharedMemPtr= (struct SHARED_MEM*) lpMapAddress; //!! return(0); } /*----------------------------------------------------------------------------*/ int sharedMemoryClose(void){ int status; status=UnmapViewOfFile(lpMapAddress); if (status == 0){ printf("UnmapViewOfFile() error!\n"); } status=CloseHandle(hMapFile); if (status == 0){ printf("CloseHandle() error!\n"); return(-1); } return(0); } #ifndef I_AM_TEST_STAND /*----------------------------------------------------------------------------*/ int executeCmdListFile(int slotNumber, char cmdListFileName[], int numRepetitions, double timeoutInSec){ int status; if(sharedMemPtr->executingFlag){ printf("executeCmdListFile() synchronization error!!\n"); return(-1); } /* copy params to shared memory */ sharedMemPtr->slotNumber=slotNumber; sharedMemPtr->listType=CMD_LIST_FILE; strcpy(sharedMemPtr->listFileName, cmdListFileName); sharedMemPtr->numRepetitions=numRepetitions; status=executeList(timeoutInSec); switch(status){ case 0: break;//success case -2: printf("executeCmdListFile() timeout!!\n"); break; case -3: printf("executeCmdListFile() execution failed!!\n"); break; default: printf("executeCmdListFile() error!!\n"); break; } return(0); } /*----------------------------------------------------------------------------*/ int executeMlistFile(int slotNumber, char mlistFileName[], int numRepetitions, double timeoutInSec){ int status; if(sharedMemPtr->executingFlag){ printf("executeMlistFile() synchronization error!!\n"); return(-1); } /* copy params to shared memory */ sharedMemPtr->slotNumber=slotNumber; sharedMemPtr->listType=MLIST_FILE; strcpy(sharedMemPtr->listFileName, mlistFileName); sharedMemPtr->numRepetitions=numRepetitions; status=executeList(timeoutInSec); switch(status){ case 0: break;//success case -2: printf("executeMlistFile() timeout!!\n"); break; case -3: printf("executeMlistFile() execution failed!!\n"); break; default: printf("executeMlistFile() error!!\n"); break; } return(0); } /*----------------------------------------------------------------------------*/ int executeRawPrimList(int slotNumber, int numRepetitions, double timeoutInSec){ // not ready yet int status; if(sharedMemPtr->executingFlag){ printf("executeRawPrimList() synchronization error!!\n"); return(-1); } /* copy params to shared memory */ // copy raw prim list to sharedMemPtr->primBuff before calling this routine!!! sharedMemPtr->slotNumber=slotNumber; sharedMemPtr->listType=RAW_PRIM_LIST; memset(sharedMemPtr->listFileName, 0, FILE_NAME_LEN); sharedMemPtr->numRepetitions=numRepetitions; /* clear reply buff for sure */ memset(sharedMemPtr->replyBuff, 0, DSP_BUFF_SIZE_BYTES); status=executeList(timeoutInSec); switch(status){ case 0: //success break; case -2: printf("executeRawPrimList() timeout!!\n"); break; case -3: printf("executeRawPrimList() execution failed!!\n"); break; default: printf("executeRawPrimList() error!!\n"); break; } //process reply data outside return(status); } /*----------------------------------------------------------------------------*/ static int executeList(double timeoutInSec){ double timeStart=Timer(); sharedMemPtr->returnStatus=0; // clear returnStatus sharedMemPtr->executingFlag=1; // set executingFlag flag /* wait until server processes the list and clears executingFlag */ while(sharedMemPtr->executingFlag){ if((Timer() - timeStart)>timeoutInSec){ return(-2); // timeout error } Sleep(10); // give the server some CPU time!!! } if(sharedMemPtr->returnStatus!=0){ return(-3); //execution failed } return(0); } #endif /* !I_AM_TEST_STAND */ /******************************************************************************/