diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2012-07-21 21:38:03 -0500 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2012-07-21 21:38:03 -0500 |
commit | 3ecb5d9be7d2099cb75f4644bcf87441fd820fd2 (patch) | |
tree | 3ee96c4446bf79240d5017f293a56c69627d98bc /servers/sensor_monitor_server_lin/src | |
parent | 25abfd3c581c1098532d464ae303fd7e43fce46e (diff) | |
download | ulab-3ecb5d9be7d2099cb75f4644bcf87441fd820fd2.tar.gz ulab-3ecb5d9be7d2099cb75f4644bcf87441fd820fd2.zip |
Initial sensor monitor server client
Diffstat (limited to 'servers/sensor_monitor_server_lin/src')
-rw-r--r-- | servers/sensor_monitor_server_lin/src/Makefile.am | 8 | ||||
-rw-r--r-- | servers/sensor_monitor_server_lin/src/main.cpp | 2 | ||||
-rw-r--r-- | servers/sensor_monitor_server_lin/src/sensor_conn.cpp | 256 | ||||
-rw-r--r-- | servers/sensor_monitor_server_lin/src/sensor_conn.h | 36 |
4 files changed, 138 insertions, 164 deletions
diff --git a/servers/sensor_monitor_server_lin/src/Makefile.am b/servers/sensor_monitor_server_lin/src/Makefile.am index bd6b9ee..7b28ea9 100644 --- a/servers/sensor_monitor_server_lin/src/Makefile.am +++ b/servers/sensor_monitor_server_lin/src/Makefile.am @@ -1,11 +1,11 @@ INCLUDES= $(all_includes) $(KDE_INCLUDES)/tde -I/usr/include/sasl KDE_CXXFLAGS = $(USE_EXCEPTIONS) -bin_PROGRAMS = remotefpga_fpgaprogserver +bin_PROGRAMS = remotefpga_sensormonserver -remotefpga_fpgaprogserver_SOURCES = main.cpp fpga_conn.cpp +remotefpga_sensormonserver_SOURCES = main.cpp sensor_conn.cpp -remotefpga_fpgaprogserver_METASOURCES = AUTO -remotefpga_fpgaprogserver_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_TDECORE) $(LIB_TDEUI) -ltdefx $(LIB_KIO) -lktexteditor -ltdekrbsocket -ltqtrla +remotefpga_sensormonserver_METASOURCES = AUTO +remotefpga_sensormonserver_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_TDECORE) $(LIB_TDEUI) -ltdefx $(LIB_KIO) -lktexteditor -ltdekrbsocket -ltqtrla KDE_OPTIONS = nofinal diff --git a/servers/sensor_monitor_server_lin/src/main.cpp b/servers/sensor_monitor_server_lin/src/main.cpp index 3a5012b..763c6a2 100644 --- a/servers/sensor_monitor_server_lin/src/main.cpp +++ b/servers/sensor_monitor_server_lin/src/main.cpp @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) KSimpleConfig config(TQDir::currentDirPath() + "/remotefpga_sensormonserver.conf", false); config.setGroup("Server"); - SensorServer fpgasvr(0, config.readNumEntry("port", 4014), &config); + SensorServer sensorsvr(0, config.readNumEntry("port", 4014), &config); return app.exec(); } diff --git a/servers/sensor_monitor_server_lin/src/sensor_conn.cpp b/servers/sensor_monitor_server_lin/src/sensor_conn.cpp index b73e480..86b8db0 100644 --- a/servers/sensor_monitor_server_lin/src/sensor_conn.cpp +++ b/servers/sensor_monitor_server_lin/src/sensor_conn.cpp @@ -56,12 +56,7 @@ struct exit_exception { }; enum connectionStates { - StateIdle = 0, - StateGetFileSize = 1, - StateGetFileContents = 2, - StateStartProgramming = 3, - StateCheckProgrammingStatus = 4, - StateProgammingFinished = 5 + StateIdle = 0 }; /* @@ -70,24 +65,37 @@ enum connectionStates { instance of this class. */ SensorSocket::SensorSocket(int sock, TQObject *parent, const char *name) : - TDEKerberosServerSocket(parent, name), m_criticalSection(0), m_loopTimer(NULL), m_config(static_cast<SensorServer*>(parent)->m_config), m_commandLoopState(StateIdle), - m_progpipe(NULL), m_progpipefd(-1), m_progErrorFlag(false), m_progDoneFlag(false) + TDEKerberosServerSocket(parent, name), m_criticalSection(0), m_loopTimer(NULL), m_config(static_cast<SensorServer*>(parent)->m_config), m_commandLoopState(StateIdle) { // Initialize timers m_kerberosInitTimer = new TQTimer(); connect(m_kerberosInitTimer, SIGNAL(timeout()), this, SLOT(finishKerberosHandshake())); m_servClientTimeout = new TQTimer(); + + // Initialize data structures + int i; + for (i=0;i<MAX_SENSORS;i++) { + m_sensorMinIntervalTimers[i] = NULL; + } + + initializeSensors(); setServiceName("remotefpga"); - line = 0; connect(this, SIGNAL(connectionClosed()), SLOT(connectionClosedHandler())); connect(this, SIGNAL(connectionClosed()), parent, SLOT(remoteConnectionClosed())); setSocket(sock); } SensorSocket::~SensorSocket() { + for (int j=0;j<MAX_SENSORS;j++) { + if (m_sensorMinIntervalTimers[j]) { + m_sensorMinIntervalTimers[j]->stop(); + delete m_sensorMinIntervalTimers[j]; + m_sensorMinIntervalTimers[j] = NULL; + } + } if (m_servClientTimeout) { m_servClientTimeout->stop(); delete m_servClientTimeout; @@ -161,6 +169,49 @@ void SensorSocket::finishKerberosHandshake() { } } +void SensorSocket::initializeSensors() { + int i=0; + m_sensorList.clear(); + m_sensorExecInfo.clear(); + m_config->setGroup("Sensors"); + TQStringList sensorNameList = m_config->readListEntry("active"); + for (TQStringList::Iterator it = sensorNameList.begin(); it != sensorNameList.end(); ++it) { + TQString sensorName = *it; + if (m_config->hasGroup(TQString("Sensor %1").arg(sensorName))) { + SensorType st; + m_config->setGroup(TQString("Sensor %1").arg(sensorName)); + st.index = i; + st.name = sensorName; + st.description = m_config->readEntry("name", i18n("<unknown>")); + st.units = m_config->readEntry("units", i18n("<unknown>")); + st.min = m_config->readDoubleNumEntry("minvalue", 0.0); + st.max = m_config->readDoubleNumEntry("maxvalue", 1.0); + st.mininterval = m_config->readDoubleNumEntry("mininterval", 1.0); + st.nominalinterval = m_config->readDoubleNumEntry("nominalinterval", 10.0); + m_sensorList.append(st); + m_sensorExecInfo[i] = m_config->readEntry("exec", "echo 0 && exit"); + if (!m_sensorMinIntervalTimers[i]) m_sensorMinIntervalTimers[i] = new TQTimer(); + m_sensorMinIntervalTimers[i]->stop(); + printf("[DEBUG] Added new sensor %s at index %d\n\r", st.name.ascii(), st.index); + i++; + } + else { + printf("[WARNING] Unknown sensor %s specified in sensor list. Ignoring...\n\r", sensorName.ascii()); + } + if (i>=MAX_SENSORS) { + printf("[WARNING] MAX_SENSORS (%d) exceeded. Ignoring any additional sensor definitions...\n\r", MAX_SENSORS); + break; + } + } + for (int j=i;j<MAX_SENSORS;j++) { + if (m_sensorMinIntervalTimers[j]) { + m_sensorMinIntervalTimers[j]->stop(); + delete m_sensorMinIntervalTimers[j]; + m_sensorMinIntervalTimers[j] = NULL; + } + } +} + void SensorSocket::commandLoop() { bool transferred_data; @@ -168,8 +219,7 @@ void SensorSocket::commandLoop() { try { transferred_data = false; if (state() == TQSocket::Connected) { - if ((m_commandLoopState == StateIdle) || (m_commandLoopState == StateStartProgramming) || (m_commandLoopState == StateCheckProgrammingStatus) || (m_commandLoopState == StateProgammingFinished)) { - // Certain commands can come in at any time during some operations + if (m_commandLoopState == StateIdle) { if (canReadLine()) { processPendingData(); } @@ -178,145 +228,71 @@ void SensorSocket::commandLoop() { ds.setPrintableData(true); TQString command; ds >> command; - clearFrameTail(); - if (command == "STATUS") { - if (m_logMessages != "") { - ds << TQString("LOGMESSAGES"); - writeEndOfFrame(); - ds << m_logMessages; - writeEndOfFrame(); - m_logMessages = ""; - } - else if (m_progErrorFlag) { - ds << TQString("ERROR"); - m_progErrorFlag = false; - writeEndOfFrame(); - } - else if (m_progDoneFlag) { - ds << TQString("DONE"); - ds << m_progRetCode; - m_progDoneFlag = false; - writeEndOfFrame(); - } - else if (m_commandLoopState == StateIdle) { - ds << TQString("IDLE"); - writeEndOfFrame(); - } - else if ((m_commandLoopState == StateStartProgramming) || (m_commandLoopState == StateCheckProgrammingStatus) || (m_commandLoopState == StateProgammingFinished)) { - ds << TQString("PROGRAMMING"); - writeEndOfFrame(); - } - else { - ds << TQString("UNKNOWN"); - writeEndOfFrame(); - } + if (command == "SENSORS") { + clearFrameTail(); + ds << m_sensorList; + writeEndOfFrame(); } - else if (m_commandLoopState == StateIdle) { - if (command == "FILE") { - m_commandLoopState = StateGetFileSize; + else if (command == "SAMPLE") { + TQ_UINT32 sensorIndex; + ds >> sensorIndex; + clearFrameTail(); + printf("[DEBUG] Requested sample from sensor at index %d\n\r", sensorIndex); + if (sensorIndex >= m_sensorList.count()) { + ds << TQString("NCK"); } - else if (command == "PROGRAM") { - m_commandLoopState = StateStartProgramming; + else if (m_sensorMinIntervalTimers[sensorIndex]->isActive()) { + ds << TQString("DLY"); } else { - printf("[WARNING] Received unknown command '%s'\n\r", command.ascii()); + double sampleValue; + bool commandSuccess = true; + long long intervalMsec = (m_sensorList[sensorIndex].mininterval*1.0e3); + m_sensorMinIntervalTimers[sensorIndex]->start(intervalMsec, TRUE); + TQDateTime timestamp = TQDateTime::currentDateTime(TQt::UTC); + TQString command = m_sensorExecInfo[sensorIndex]; + FILE* pipe = popen(command.ascii(), "r"); + if (!pipe) { + commandSuccess = false; + } + else { + char buffer[1024]; + TQString result = ""; + while(!feof(pipe)) { + if (fgets(buffer, 1024, pipe) != NULL) { + result += buffer; + } + } + TQ_INT32 retcode = pclose(pipe); + sampleValue = result.toDouble(); + if (retcode != 0) { + commandSuccess = false; + } + } + if (commandSuccess) { + ds << TQString("ACK"); + ds << sampleValue; + ds << timestamp; + } + else { + ds << TQString("NCK"); + } } + writeEndOfFrame(); } - transferred_data = true; - } - } - if (m_commandLoopState == StateGetFileSize) { - if (canReadLine()) { - processPendingData(); - } - if (canReadFrame()) { - TQDataStream ds(this); - ds.setPrintableData(true); - ds >> m_programmingFileSize; - clearFrameTail(); - m_servClientTimeout->start(NETWORK_COMM_TIMEOUT_MS, TRUE); - m_commandLoopState = StateGetFileContents; - } - } - else if (m_commandLoopState == StateGetFileContents) { - if (canReadLine()) { - m_servClientTimeout->start(NETWORK_COMM_TIMEOUT_MS, TRUE); - processPendingData(); - } - if (bytesAvailable() >= m_programmingFileSize) { - TQByteArray fileContents(m_programmingFileSize); - readBlock(fileContents.data(), fileContents.size()); - m_programmingFileName = TQString("/tmp/%1#%2.dat").arg(m_remoteHost).arg(port()); - TQFile outputFile(m_programmingFileName); - if (outputFile.open(IO_ReadWrite)) { - outputFile.writeBlock(fileContents); - outputFile.flush(); - outputFile.close(); + else if (command == "PING") { + clearFrameTail(); + ds << TQString("PONG"); + writeEndOfFrame(); } - transferred_data = true; - m_commandLoopState = StateIdle; - } - else { - if (!m_servClientTimeout->isActive()) { - m_progErrorFlag = true; - transferred_data = true; - m_commandLoopState = StateIdle; + else { + clearFrameTail(); + printf("[WARNING] Received unknown command %s from host %s\n\r", command.ascii(), m_remoteHost.ascii()); fflush(stdout); + ds << TQString("NCK"); + writeEndOfFrame(); } - } - } - else if (m_commandLoopState == StateStartProgramming) { - // Start programming! - - // Open programming process - m_config->setGroup("Programming"); - TQString programmingScript = m_config->readEntry("script"); - programmingScript.replace("%f", m_programmingFileName); - if (!programmingScript.contains("2>&1")) { - programmingScript.append(" 2>&1"); - } - if ((m_progpipe = popen(programmingScript.ascii(), "r")) == NULL) { - m_logMessages.append(TQString("The system was unable to execute '%1'\nPlease contact your system administrator with this information").arg(programmingScript)); - m_progErrorFlag = true; transferred_data = true; - m_commandLoopState = StateIdle; - } - else { - m_progpipefd = fileno(m_progpipe); - fcntl(m_progpipefd, F_SETFL, O_NONBLOCK); - } - m_commandLoopState = StateCheckProgrammingStatus; - } - else if (m_commandLoopState == StateCheckProgrammingStatus) { - // Check programming status - TQCString buf; - buf.resize(8192); - ssize_t r = read(m_progpipefd, buf.data(), buf.size()); - if ((r == -1) && (errno == EAGAIN)) { - // No data available yet } - else if (r > 0) { - // Data was received - buf.data()[r] = 0; - m_logMessages.append(buf); - } - else { - // Process terminated - m_commandLoopState = StateProgammingFinished; - } - } - else if (m_commandLoopState == StateProgammingFinished) { - // Programming process terminated; get exit code and clean up - if (m_progpipe) { - m_progRetCode = pclose(m_progpipe); - } - else { - m_progRetCode = -1; - } - m_progpipe = NULL; - m_progpipefd = -1; - - m_progDoneFlag = true; - m_commandLoopState = StateIdle; } } m_criticalSection--; diff --git a/servers/sensor_monitor_server_lin/src/sensor_conn.h b/servers/sensor_monitor_server_lin/src/sensor_conn.h index 9486c6f..05c939a 100644 --- a/servers/sensor_monitor_server_lin/src/sensor_conn.h +++ b/servers/sensor_monitor_server_lin/src/sensor_conn.h @@ -38,13 +38,17 @@ #define MAGIC_NUMBER 1 #define PROTOCOL_VERSION 1 -class SensorServer : public TDEKerberosServerSocket +#define MAX_SENSORS 255 + +typedef TQMap<TQ_UINT32, TQString> SensorStringMap; + +class SensorSocket : public TDEKerberosServerSocket { Q_OBJECT public: - SensorServer(int sock, TQObject *parent=0, const char *name=0); - ~SensorServer(); + SensorSocket(int sock, TQObject *parent=0, const char *name=0); + ~SensorSocket(); public: void close(); @@ -55,9 +59,9 @@ class SensorServer : public TDEKerberosServerSocket void finishKerberosHandshake(); void connectionClosedHandler(); void commandLoop(); + void initializeSensors(); private: - int line; int m_criticalSection; TQString m_remoteHost; @@ -68,26 +72,20 @@ class SensorServer : public TDEKerberosServerSocket KSimpleConfig* m_config; int m_commandLoopState; - TQ_ULONG m_programmingFileSize; - TQString m_programmingFileName; - FILE *m_progpipe; - int m_progpipefd; - - bool m_progErrorFlag; - bool m_progDoneFlag; - TQ_INT32 m_progRetCode; - TQString m_logMessages; + SensorList m_sensorList; + SensorStringMap m_sensorExecInfo; + TQTimer* m_sensorMinIntervalTimers[MAX_SENSORS]; - friend class FPGAServer; + friend class SensorServer; }; -class FPGAServer : public TQServerSocket +class SensorServer : public TQServerSocket { Q_OBJECT public: - FPGAServer(TQObject* parent=0, int port=0, KSimpleConfig* config=0); - ~FPGAServer(); + SensorServer(TQObject* parent=0, int port=0, KSimpleConfig* config=0); + ~SensorServer(); void newConnection(int socket); @@ -95,12 +93,12 @@ class FPGAServer : public TQServerSocket void remoteConnectionClosed(); signals: - void newConnect(SensorServer*); + void newConnect(SensorSocket*); private: KSimpleConfig* m_config; int m_numberOfConnections; - friend class SensorServer; + friend class SensorSocket; };
\ No newline at end of file |