#include "iocsh.h" #include "Commands.h" #include "ExecThread.h" #include "ExecConfiguration.h" #include "PatternGenerator.h" #include "ChannelAccess.h" #include "CaChannel.h" #include #include #include #include #include #ifdef RTEMS #include #endif USING_FF_NAMESPACE /** ffHelp ********************************************************************/ static const iocshArg ffHelp_Arg0 = {"command", iocshArgString}; static const iocshArg * const ffHelp_Args[1] = {&ffHelp_Arg0}; static const iocshFuncDef ffHelp_FuncDef = {"ffHelp", 0, NULL}; typedef std::map HelpMap; static HelpMap *helpMap; static void Help(const iocshArgBuf *args) { std::cout << "FastFeedback available commands:" << std::endl; HelpMap::iterator it; for (it = helpMap->begin(); it != helpMap->end(); ++it) { std::cout << " " << it->first << std::endl; } std::cout << "Type a command: "; std::string command; std::cin >> command; it = helpMap->find(command); if (it == helpMap->end()) { std::cout << "Unknown command \"" << command << "\"" << std::endl; } else { std::cout << "----------------------------------------------------------"; std::cout << std::endl; std::cout << it->second; std::cout << "----------------------------------------------------------"; std::cout << std::endl; } } static void HelpCommand(std::string command) { HelpMap::iterator it; it = helpMap->find(command); if (it == helpMap->end()) { std::cout << "Unknown command \"" << command << "\"" << std::endl; } else { std::cout << "----------------------------------------------------------"; std::cout << std::endl; std::cout << it->second; std::cout << "----------------------------------------------------------"; std::cout << std::endl; } } void IocshRegister::registerHelp() { helpMap = new HelpMap(); std::pair command; command.first = "ffDebug"; command.second = "Usage: ffDebug (or ffd) \n"; command.second += " Show system debugging information.\n"; command.second += " Where:\n"; command.second += " LoopName - (optional parameter) name of the loop (see ffShow)\n"; helpMap->insert(command); command.first = "ffHelp"; command.second = "Usage: ffHelp"; command.second = " Describes available FastFeedback (ff) commands.\n"; helpMap->insert(command); command.first = "ffLogOff"; command.second = "Usage: ffLogOff\n"; command.second += " Disables error messages to the console.\n"; helpMap->insert(command); command.first = "ffLogOn"; command.second = "Usage: ffLogOn\n"; command.second += " Enables error messages to the console.\n"; helpMap->insert(command); command.first = "ffPeek"; command.second = "Usage: ffPeek (or ffp) \n"; command.second += " Prints the lastest value used as input or output for the loop.\n"; command.second += " Where:\n"; command.second += " LoopName - name of the loop (see ffShow)\n"; command.second += " DeviceName - (see ffShowDevices )\n"; command.second += " PatternIndex - pattern number for the loop (see ffShow)\n"; helpMap->insert(command); command.first = "ffReboot"; command.second = "Usage: ffReboot\n"; command.second += " Stops all running loops and reboots the IOC by\n"; command.second += " invoking bsp_reset().\n"; helpMap->insert(command); command.first = "ffShowDevices"; command.second = "Usage: ffShowDevices (or ffsd) \n"; command.second += " Display device information for the specified loop.\n"; helpMap->insert(command); command.first = "ffShow"; command.second = "Usage: ffShow (or ffs) \n"; command.second += " Shows general information about the FastFeedback system\n"; command.second += " or about the specified loop.\n"; command.second += " Where:\n"; command.second += " LoopName - (optional parameter) name of the loop (see ffShow)\n"; helpMap->insert(command); command.first = "ffShowLoopConfig"; command.second = "Usage: ffShowLoopConfig (or ffslc) \n"; command.second += " Shows detailed configuration information about the given loop.\n"; helpMap->insert(command); command.first = "ffShowMeas"; command.second = "Usage: ffShowMeas (or ffsm)\n"; command.second += " Shows detailed information about current collected\n"; command.second += " measurements.\n"; helpMap->insert(command); command.first = "ffStart"; command.second = "Usage: ffStart\n"; command.second += " Starts the FastFeedback system using configuration from.\n"; command.second += " loaded with the ffConfig command.\n"; helpMap->insert(command); command.first = "ffStopLoops"; command.second = "Usage: ffStopLoops (or ffsl)\n"; command.second += " Turns STATE=OFF to all loops. This releases all Fcom\n"; command.second += " and ChannelAccess connections.\n"; helpMap->insert(command); } /** ffReboot ******************************************************************/ static const iocshFuncDef ffReboot_FuncDef = {"ffReboot", 0, NULL}; static void ExecThreadReboot(const iocshArgBuf *args) { #ifdef RTEMS std::cout << "Stopping all feedback loops..." << std::flush; ExecThread::getInstance().stopLoops(); // Must wait until LoopConfiguration::_configured == false for all Loops while (!ExecThread::getInstance().areLoopsOff()) { epicsThreadSleep(1); std::cout << "." << std::flush; } std::cout << " done. Rebooting..." << std::endl << std::flush; epicsThreadSleep(1); bsp_reset(); #endif } /** Allow ffStart to be invoked from cexp shell */ extern "C" { void ffReboot() { ExecThreadReboot(0); } } /** ffStart *******************************************************************/ static const iocshFuncDef ffStart_FuncDef = {"ffStart", 0, NULL}; static void ExecThreadStart(const iocshArgBuf *args) { try { ExecThread::getInstance().start(); } catch (...) { std::cerr << "ERROR: failed to start FastFeedback system." << std::endl; } } /** Allow ffStart to be invoked from cexp shell */ extern "C" { void ffStart() { ExecThreadStart(0); } } /** ffStop ********************************************************************/ static const iocshFuncDef ffStopLoops_FuncDef = {"ffStopLoops", 0, NULL}; static const iocshFuncDef ffsl_FuncDef = {"ffsl", 0, NULL}; static void ExecThreadStopLoops(const iocshArgBuf *args) { ExecThread::getInstance().stopLoops(); } /** Allow ffStop to be invoked from cexp shell */ extern "C" { void ffStopLoops() { ExecThreadStopLoops(0); } } /** ffLogOn *******************************************************************/ static const iocshFuncDef ffLogOn_FuncDef = {"ffLogOn", 0, NULL}; static void ConsoleLogEnable(const iocshArgBuf *args) { Log::getInstance().setAllLogToConsole(true); } /** ffLogOff ******************************************************************/ static const iocshFuncDef ffLogOff_FuncDef = {"ffLogOff", 0, NULL}; static void ConsoleLogDisable(const iocshArgBuf *args) { Log::getInstance().setAllLogToConsole(false); } /** ffPeek ********************************************************************/ static const iocshArg ffPeek_Arg0 = {"LoopName", iocshArgString}; static const iocshArg ffPeek_Arg1 = {"DeviceName", iocshArgString}; static const iocshArg ffPeek_Arg2 = {"PatternIndex", iocshArgInt}; static const iocshArg * const ffPeek_Args[3] = {&ffPeek_Arg0, &ffPeek_Arg1, &ffPeek_Arg2}; static const iocshFuncDef ffPeek_FuncDef = {"ffPeek", 3, ffPeek_Args}; static const iocshFuncDef ffp_FuncDef = {"ffp", 3, ffPeek_Args}; static void LoopPeek(const iocshArgBuf *args) { if (args[0].sval == NULL || args[1].sval == NULL) { HelpCommand("ffPeek"); return; } std::string loopName(args[0].sval); std::transform(loopName.begin(), loopName.end(), loopName.begin(), toupper); std::string deviceName(args[1].sval); std::transform(deviceName.begin(), deviceName.end(), deviceName.begin(), toupper); int patternIndex = args[2].ival; LoopConfigurationMap::iterator it; it = ExecConfiguration::getInstance()._loopConfigurations.find(loopName); LoopConfiguration *loopConfig = it->second; if (it != ExecConfiguration::getInstance()._loopConfigurations.end()) { if (patternIndex >= (int) loopConfig->_patternMasks.size()) { std::cout << "ERROR: invalid PatternIndex, value must be between 0 and " << loopConfig->_patternMasks.size() - 1 << std::endl; return; } PatternMask pattern = loopConfig->_patternMasks[patternIndex]; // Look for the desired DeviceName/Pattern DataPoint dataPoint; if (loopConfig->peek(deviceName, pattern, dataPoint) != 0) { std::cout << "ERROR: invalid device." << std::endl; return; } char timeStr[200]; epicsTimeToStrftime(timeStr, 200, "%Y/%m/%d %H:%M:%S.%06f", &dataPoint._timestamp); std::cout << dataPoint._value << " at " << timeStr << ", pulse id: " << PULSEID(dataPoint._timestamp) << " (status=" << dataPoint._status << ")" << std::endl; } else { std::cout << "ERROR: unknown loop named \"" << args[0].sval << "\"" << std::endl; } } /** ffShow ********************************************************************/ /* static const iocshFuncDef ffShow_FuncDef = {"ffShow", 0, NULL}; static const iocshFuncDef ffs_FuncDef = {"ffs", 0, NULL}; static void ExecThreadShow(const iocshArgBuf *args) { ExecThread::getInstance().show(); } extern "C" { void ffShow() { ExecThreadShow(0); } } */ /** ffDebug *******************************************************************/ static const iocshArg ffDebug_Arg0 = {"LoopName", iocshArgString}; static const iocshArg * const ffDebug_Args[1] = {&ffDebug_Arg0}; static const iocshFuncDef ffDebug_FuncDef = {"ffDebug", 1, ffDebug_Args}; static const iocshFuncDef ffd_FuncDef = {"ffd", 1, ffDebug_Args}; static void ExecThreadShowDebug(const iocshArgBuf *args) { if (args[0].sval != NULL) { std::string loopName(args[0].sval); std::transform(loopName.begin(), loopName.end(), loopName.begin(), toupper); ExecThread::getInstance().showDebugLoop(loopName); } else { ExecThread::getInstance().showDebug(); } } extern "C" { void ffDebug() { ExecThreadShowDebug(0); } } /** ffShowLoop ****************************************************************/ static const iocshArg ffShowLoop_Arg0 = {"LoopName", iocshArgString}; static const iocshArg * const ffShowLoop_Args[1] = {&ffShowLoop_Arg0}; static const iocshFuncDef ffShow_FuncDef = {"ffShow", 1, ffShowLoop_Args}; static const iocshFuncDef ffs_FuncDef = {"ffs", 1, ffShowLoop_Args}; static void ExecThreadShowLoop(const iocshArgBuf *args) { if (args[0].sval != NULL) { std::string loopName(args[0].sval); std::transform(loopName.begin(), loopName.end(), loopName.begin(), toupper); ExecThread::getInstance().showLoop(loopName); } else { ExecThread::getInstance().show(); } } extern "C" { void ffShow(char *loopName) { iocshArgBuf args; args.sval = loopName; ExecThreadShowLoop(&args); } } /** ffShowLoopConfig **********************************************************/ static const iocshArg ffShowLoopConfig_Arg0 = {"LoopName", iocshArgString}; static const iocshArg * const ffShowLoopConfig_Args[1] = {&ffShowLoopConfig_Arg0}; static const iocshFuncDef ffShowLoopConfig_FuncDef = {"ffShowLoopConfig", 1, ffShowLoopConfig_Args}; static const iocshFuncDef ffslc_FuncDef = {"ffslc", 1, ffShowLoopConfig_Args}; static void ExecThreadShowLoopConfig(const iocshArgBuf *args) { if (args[0].sval != NULL) { std::string loopName(args[0].sval); std::transform(loopName.begin(), loopName.end(), loopName.begin(), toupper); ExecThread::getInstance().showLoopConfig(loopName); } else { HelpCommand("ffShowLoopConfig"); } } extern "C" { void ffShowLoopConfig(char *loopName) { iocshArgBuf args; args.sval = loopName; ExecThreadShowLoopConfig(&args); } } /** ffShowMeas ****************************************************************/ static const iocshFuncDef ffShowMeas_FuncDef = {"ffShowMeas", 0, NULL}; static const iocshFuncDef ffsm_FuncDef = {"ffsm", 0, NULL}; static void MeasurementCollectorShowMeas(const iocshArgBuf *args) { MeasurementCollector::getInstance().showMeasurements(); } /** ffShowDevices *************************************************************/ static const iocshArg ffShowDevices_Arg0 = {"LoopName", iocshArgString}; static const iocshArg * const ffShowDevices_Args[1] = {&ffShowDevices_Arg0}; static const iocshFuncDef ffShowDevices_FuncDef = {"ffShowDevices", 1, ffShowDevices_Args}; static const iocshFuncDef ffsd_FuncDef = {"ffsd", 1, ffShowDevices_Args}; static void LoopShowDevices(const iocshArgBuf *args) { std::string loopName; if (args[0].sval != NULL) { loopName = args[0].sval; std::transform(loopName.begin(), loopName.end(), loopName.begin(), toupper); } else { HelpCommand("ffShowDevices"); } LoopConfigurationMap::iterator it; it = ExecConfiguration::getInstance()._loopConfigurations.find(loopName); if (it != ExecConfiguration::getInstance()._loopConfigurations.end()) { it->second->showDevices(); } else { std::cout << "ERROR: unknown loop named \"" << loopName << "\"" << std::endl; } } /** Register all commands */ IocshRegister::IocshRegister() { registerHelp(); iocshRegister(&ffDebug_FuncDef, ExecThreadShowDebug); iocshRegister(&ffd_FuncDef, ExecThreadShowDebug); iocshRegister(&ffStart_FuncDef, ExecThreadStart); iocshRegister(&ffReboot_FuncDef, ExecThreadReboot); iocshRegister(&ffStopLoops_FuncDef, ExecThreadStopLoops); iocshRegister(&ffsl_FuncDef, ExecThreadStopLoops); iocshRegister(&ffLogOn_FuncDef, ConsoleLogEnable); iocshRegister(&ffLogOff_FuncDef, ConsoleLogDisable); iocshRegister(&ffPeek_FuncDef, LoopPeek); iocshRegister(&ffp_FuncDef, LoopPeek); iocshRegister(&ffShow_FuncDef, ExecThreadShowLoop); iocshRegister(&ffs_FuncDef, ExecThreadShowLoop); // iocshRegister(&ffShowLoop_FuncDef, ExecThreadShowLoop); // iocshRegister(&ffsl_FuncDef, ExecThreadShowLoop); iocshRegister(&ffShowLoopConfig_FuncDef, ExecThreadShowLoopConfig); iocshRegister(&ffslc_FuncDef, ExecThreadShowLoopConfig); iocshRegister(&ffShowMeas_FuncDef, MeasurementCollectorShowMeas); iocshRegister(&ffsm_FuncDef, MeasurementCollectorShowMeas); iocshRegister(&ffShowDevices_FuncDef, LoopShowDevices); iocshRegister(&ffsd_FuncDef, LoopShowDevices); iocshRegister(&ffHelp_FuncDef, Help); } static FastFeedback::IocshRegister iocshRegisterObj;