#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "MPSConfiguration.h" #include "HistoryCommunication.h" #include "MachineStateBuffer.h" #include "MachineRatesBuffer.h" #include "LinkNodeCommunication.h" #include "Timing.h" #include "Mitigation.h" #include "AlgorithmWrapper.h" #include /* This should be the last header */ static int mpsInitVerbosity = 1; extern struct seqProgram mpsEpicsFaultInputs; static epicsThreadId mpsEpicsFaultInputsThreadId = NULL; #if 0 #pragma mark - #pragma mark Configuration #endif epicsThreadId monitorEpicsFaults() { if (mpsEpicsFaultInputsThreadId != NULL) { seqStop(mpsEpicsFaultInputsThreadId); mpsEpicsFaultInputsThreadId = NULL; } mpsEpicsFaultInputsThreadId = seq((void *)&mpsEpicsFaultInputs, "", 0); return mpsEpicsFaultInputsThreadId; } #ifndef _GNU_SOURCE ssize_t getline(char **lineptr, size_t *n, FILE *stream) { char *buffer; size_t size; size_t length; char *c; if ((lineptr == NULL) || (n == NULL) || (stream == NULL)) { errno = EINVAL; return -1; } if (*lineptr == NULL) { buffer = malloc(BUFSIZ); size = BUFSIZ; if (buffer == NULL) { return -1; } } else { buffer = *lineptr; size = *n; } length = 1; while(1) { c = fgets(buffer + (length - 1), size, stream); if (c == NULL) { return -1; } length = strlen(buffer); if (buffer[length - 1] == '\n') { break; } if (feof(stream) != 0) { break; } size += BUFSIZ; buffer = realloc(buffer, size); if (buffer == NULL) { fseek(stream, (-1) * length, SEEK_CUR); return -1; } } *lineptr = buffer; *n = size; return length; } #endif static int configureLinkProcessor() { char *path = NULL; FILE *configFile = NULL; char *line = NULL; size_t size; ssize_t length; char *c; unsigned int faultNumber, linkNodeId, card, channel, autorecover, linkNodeType; /* * Configure Link Node Channel Faults */ path = getenv(MPS_ENV_CONFIG_LINK_NODE_CHANNEL_PATH); if (path == NULL) { return MPS_ERROR_ENVIRONMENT_VARIABLE_NOT_SET; } configFile = fopen(path, "r"); if (configFile == NULL) { errlogPrintf("configureLinkProcessor: %s, \"%s\"\n", strerror(errno), path); return MPS_ERROR; } while ((length = getline(&line, &size, configFile)) > 0) { c = line; /* Ignore leading spaces and comments starting with # */ while((c <= line + length) && (isspace(*c))) { c++; } if ((*c == '\0') || (*c == '#')) { /* Grab the next line if there is no data or the line is a comment */ continue; } sscanf(c, "%u,%u,%u,%u,%u", &faultNumber, &linkNodeId, &card, &channel, &autorecover); if ((linkNodeId < MPSLinkNodeIdMin ) || (linkNodeId > MPSLinkNodeIdMax ) || (card < MPSLinkNodeCardIdMin ) || (card > MPSLinkNodeCardIdMax ) || (channel < MPSLinkNodeChannelMin ) || (channel > MPSLinkNodeChannelMax ) ) { errlogPrintf("configureLinkProcessor: Bad data (Link Node ID %u, card %u, channel %u)", linkNodeId, card, channel); } setLinkNodeActive(linkNodeId, epicsTrue); setFaultNumberForLinkNodeCardAndChannel(faultNumber, linkNodeId, card, channel); if (autorecover) { setInputAutorecover(faultNumber, epicsTrue); } if (mpsInitVerbosity > 2) { printf("LNC Fault Number: %4u Link Node ID: %3u Card: %2u Channel: %2u Autorecover: %s\n", faultNumber, linkNodeId, card, channel, autorecover ? "Yes" : "No"); } } fclose(configFile); /* * Configure EPICS Faults */ path = getenv(MPS_ENV_CONFIG_EPICS_FAULT_PATH); if (path == NULL) { return MPS_ERROR_ENVIRONMENT_VARIABLE_NOT_SET; } configFile = fopen(path, "r"); if (configFile == NULL) { errlogPrintf("configureLinkProcessor: %s, \"%s\"\n", strerror(errno), path); return MPS_ERROR; } while ((length = getline(&line, &size, configFile)) > 0) { c = line; /* Ignore leading spaces and comments starting with # */ while((c <= line + length) && (isspace(*c))) { c++; } if ((*c == '\0') || (*c == '#')) { /* Grab the next line if there is no data or the line is a comment */ continue; } sscanf(c, "%u,%u", &faultNumber, &autorecover); if (autorecover) { setInputAutorecover(faultNumber, epicsTrue); } if (mpsInitVerbosity > 2) { printf("EPICS Fault Number: %4u Autorecover: %s\n", faultNumber, autorecover ? "Yes" : "No"); } } fclose(configFile); /* * Configure Link Node Faults */ path = getenv(MPS_ENV_CONFIG_LINK_NODE_PATH); if (path == NULL) { return MPS_ERROR_ENVIRONMENT_VARIABLE_NOT_SET; } configFile = fopen(path, "r"); if (configFile == NULL) { errlogPrintf("configureLinkProcessor: %s, \"%s\"\n", strerror(errno), path); return MPS_ERROR; } while ((length = getline(&line, &size, configFile)) > 0) { c = line; /* Ignore leading spaces and comments starting with # */ while((c <= line + length) && (isspace(*c))) { c++; } if ((*c == '\0') || (*c == '#')) { /* Grab the next line if there is no data or the line is a comment */ continue; } sscanf(c, "%u,%u,%u,%u", &faultNumber, &autorecover, &linkNodeId, &linkNodeType); if ((linkNodeId < MPSLinkNodeIdMin ) || (linkNodeId > MPSLinkNodeIdMax ) || (linkNodeType < MPSLinkNodeTypeMitigationControl ) || (linkNodeType > MPSLinkNodeTypePic ) ) { errlogPrintf("configureLinkProcessor: Bad data (Link Node ID %u, type %u)", linkNodeId, linkNodeType); } setLinkNodeType(linkNodeId, linkNodeType); setLinkNodeActive(linkNodeId, epicsTrue); if (autorecover) { setInputAutorecover(faultNumber, autorecover); } if (mpsInitVerbosity > 2) { printf("LN Fault Number: %4u LN ID: %3u LN Type: %2u Autorecover: %s\n", faultNumber, linkNodeId, linkNodeType, autorecover ? "Yes" : "No"); } } /* * Configure Link Processor Faults */ path = getenv(MPS_ENV_CONFIG_LINK_PROCESSOR_PATH); if (path == NULL) { return MPS_ERROR_ENVIRONMENT_VARIABLE_NOT_SET; } configFile = fopen(path, "r"); if (configFile == NULL) { errlogPrintf("configureLinkProcessor: %s, \"%s\"\n", strerror(errno), path); return MPS_ERROR; } while ((length = getline(&line, &size, configFile)) > 0) { c = line; /* Ignore leading spaces and comments starting with # */ while((c <= line + length) && (isspace(*c))) { c++; } if ((*c == '\0') || (*c == '#')) { /* Grab the next line if there is no data or the line is a comment */ continue; } sscanf(c, "%u,%u", &faultNumber, &autorecover); if (autorecover) { setInputAutorecover(faultNumber, epicsTrue); } if (mpsInitVerbosity > 2) { printf("Link Processor Fault Number: %4u Autorecover: %s\n", faultNumber, autorecover ? "Yes" : "No"); } } fclose(configFile); free(line); line = NULL; fclose(configFile); setLinkNodeNumbers(); return MPS_SUCCESS; } #if 0 #pragma mark - #pragma mark Initialization #endif void iocInitHook(initHookState state) { switch (state) { case initHookAfterCallbackInit: printf( "initHookAfterCallbackInit: "); break; case initHookAfterCaLinkInit: printf("Done.\n" "initHookAfterCaLinkInit: "); break; case initHookAfterInitDrvSup: printf("Done.\n" "initHookAfterInitDrvSup: "); break; case initHookAfterInitRecSup: printf("Done.\n" "initHookAfterInitRecSup: "); break; case initHookAfterInitDevSup: printf("Done.\n" "initHookAfterInitDevSup: "); break; case initHookAfterInitDatabase: printf("Done.\n" "initHookAfterInitDatabase: "); break; case initHookAfterFinishDevSup: printf("Done.\n" "initHookAfterFinishDevSup: "); break; case initHookAfterScanInit: printf("Done.\n" "initHookAfterScanInit: "); break; case initHookAfterInitialProcess: printf("Done.\n" "initHookAfterInitialProcess: "); break; case initHookAfterInterruptAccept: printf("Done.\n" "initHookAfterInterruptAccept: "); break; case initHookAtEnd: printf("Done.\n" "initHookAtEnd.\n"); break; default: break; } fflush(stdout); } void mpsEnvSetDatabaseVersion() { epicsEnvSet(MPS_ENV_CONFIG_DATABASE_VERSION, MPS_DATABASE_VERSION); printf("Setting Link Processor MPS Database Version: %s \n", MPS_DATABASE_VERSION); } /* wrapper to configure LanIpBasic for the mpsLinkProcessor. Called in st.cmd */ int mpsLanIpBscConfigWrapper(unsigned rxQueueDepth, unsigned rxRingSize, unsigned numRBufs, unsigned txRingSize ) { int status; LanIpBscConfigRec cfg; cfg.mask = LANIPCFG_RX_RING | LANIPCFG_TX_RING | LANIPCFG_N_RBUFS | LANIPCFG_SQDEPTH; cfg.rx_queue_depth = rxQueueDepth; cfg.rx_ring_size = rxRingSize; cfg.num_rbufs = numRBufs; cfg.tx_ring_size = txRingSize; status = lanIpBscConfig(&cfg,0); return status; } static void initializeLinkProcessorRegistrar(void) { int err; /* Register a hook to check on iocInit */ initHookRegister(iocInitHook); /* Initialize History Communication */ err = initializeHistoryCommunication(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not initialize history communication."); } /* Initialize Machine State Buffers */ err = initializeMachineStateBuffers(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not initialize machine state buffers."); } /* Initialize Machine Rate Buffer */ err = initializeMachineRatesBuffer(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not initialize machine rates buffer."); } /* Configuration */ err = configureLinkProcessor(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not configure the Link Processor."); } /* Initialize Mitigation Handling */ err = initializeMitigation(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not initialize mitigation handling."); } /* Initialize Algorithm */ err = initializeAlgorithm(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not initialize the alogrithm wrapper."); } /* Initialize Link Node Communication */ err = initializeLinkNodeCommunication(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not initialize Link Node communication."); } /* Initialize Timing */ err = intializeTiming(); if (err != MPS_SUCCESS) { cantProceed("initializeLinkProcessorRegistrar could not initialize timing."); } if(mpsInitVerbosity > 0) { printf("Link Processor Initialized\n"); } return; } epicsExportRegistrar(initializeLinkProcessorRegistrar);