//Author: Timothy Pearson , (C) 2012 //Copyright: See COPYING file that comes with this distribution #include "debug.h" #include "define.h" #include "part.h" #include //::createAboutData() #include #include #include //::start() #include #include #include #include //encodeName() #include #include #include #include #include #include #include //access() #include #include "tracewidget.h" #include "floatspinbox.h" #include "layout.h" #define NETWORK_COMM_TIMEOUT_MS 15000 /* exception handling */ struct exit_exception { int c; exit_exception(int c):c(c) { } }; namespace RemoteLab { typedef KParts::GenericFactory Factory; #define CLIENT_LIBRARY "libremotelab_commanalyzer" K_EXPORT_COMPONENT_FACTORY( libremotelab_commanalyzer, RemoteLab::Factory ) CommAnalyzerPart::CommAnalyzerPart( TQWidget *parentWidget, const char *widgetName, TQObject *parent, const char *name, const TQStringList& ) : RemoteInstrumentPart( parent, name ), m_traceWidget(0), m_commHandlerState(-1), m_commHandlerMode(0), m_commHandlerCommandState(0), m_base(0), stopTraceUpdate(false) { // Initialize important base class variables m_clientLibraryName = CLIENT_LIBRARY; // Initialize mutex m_instrumentMutex = new TQMutex(false); // Initialize kpart setInstance(Factory::instance()); setWidget(new TQVBox(parentWidget, widgetName)); // Create timers m_updateTimeoutTimer = new TQTimer(this); connect(m_updateTimeoutTimer, SIGNAL(timeout()), this, SLOT(mainEventLoop())); // Create widgets m_base = new CommAnalyzerBase(widget()); m_traceWidget = m_base->traceWidget; m_base->traceWidget->setSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding)); m_base->traceWidget->setTraceEnabled(0, true); m_traceWidget->setTraceName(0, "Trace 1"); m_traceWidget->setTraceHorizontalUnits(0, "Hz"); m_traceWidget->setTraceVerticalUnits(0, "dBm"); m_base->saRefLevel->setFloatMin(-128); m_base->saRefLevel->setFloatMax(128); m_base->saRefLevel->setLineStep(1); connect(m_base->saRefLevel, SIGNAL(floatValueChanged(double)), this, SLOT(saRefLevelChanged(double))); TQTimer::singleShot(0, this, TQT_SLOT(postInit())); } CommAnalyzerPart::~CommAnalyzerPart() { if (m_instrumentMutex->locked()) { printf("[WARNING] Exiting when data transfer still in progress!\n\r"); fflush(stdout); } disconnectFromServer(); delete m_instrumentMutex; } void CommAnalyzerPart::postInit() { setUsingFixedSize(false); } bool CommAnalyzerPart::openURL(const KURL &url) { int ret; ret = connectToServer(url.url()); processLockouts(); return (ret != 0); } bool CommAnalyzerPart::closeURL() { disconnectFromServer(); m_url = KURL(); return true; } void CommAnalyzerPart::processLockouts() { } void CommAnalyzerPart::disconnectFromServerCallback() { m_updateTimeoutTimer->stop(); } void CommAnalyzerPart::connectionFinishedCallback() { connect(m_socket, SIGNAL(readyRead()), m_socket, SLOT(processPendingData())); m_socket->processPendingData(); connect(m_socket, SIGNAL(newDataReceived()), this, SLOT(mainEventLoop())); m_tickerState = 0; m_commHandlerState = 0; m_commHandlerMode = 0; m_socket->setDataTimeout(NETWORK_COMM_TIMEOUT_MS); m_updateTimeoutTimer->start(NETWORK_COMM_TIMEOUT_MS, TRUE); processLockouts(); mainEventLoop(); return; } #define UPDATEDISPLAY_TIMEOUT m_connectionActiveAndValid = false; \ m_tickerState = 0; \ m_commHandlerState = 2; \ m_commHandlerMode = 0; \ m_socket->clearIncomingData(); \ setStatusMessage(i18n("Server ping timeout. Please verify the status of your network connection.")); \ m_updateTimeoutTimer->start(NETWORK_COMM_TIMEOUT_MS, TRUE); \ m_instrumentMutex->unlock(); \ return; #define COMMUNICATIONS_FAILED m_connectionActiveAndValid = false; \ m_tickerState = 0; \ m_commHandlerState = 2; \ m_commHandlerMode = 0; \ m_socket->clearIncomingData(); \ setStatusMessage(i18n("Instrument communication failure. Please verify the status of your network connection.")); \ m_updateTimeoutTimer->start(NETWORK_COMM_TIMEOUT_MS, TRUE); \ m_instrumentMutex->unlock(); \ return; #define SET_WATCHDOG_TIMER if (!m_updateTimeoutTimer->isActive()) m_updateTimeoutTimer->start(NETWORK_COMM_TIMEOUT_MS, TRUE); #define PAT_WATCHDOG_TIMER m_updateTimeoutTimer->stop(); m_updateTimeoutTimer->start(NETWORK_COMM_TIMEOUT_MS, TRUE); #define SET_NEXT_STATE(x) if (m_commHandlerMode == 0) { \ m_commHandlerState = x; \ } \ else { \ m_commHandlerState = 255; \ } void CommAnalyzerPart::mainEventLoop() { TQDataStream ds(m_socket); ds.setPrintableData(true); if (!m_instrumentMutex->tryLock()) { TQTimer::singleShot(0, this, SLOT(mainEventLoop())); // Handle the concurrently received call immediately after current execution return; } if (m_socket) { if ((m_commHandlerMode == 0) || (m_commHandlerMode == 1)) { if (m_commHandlerState == 0) { // Request communications analyzer access ds << TQString("COMMUNICATIONS ANALYZER"); m_socket->writeEndOfFrame(); m_commHandlerState = 1; } else if (m_commHandlerState == 1) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get command status TQString result; ds >> result; m_socket->clearFrameTail(); if (result == "ACK") { SET_NEXT_STATE(2) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 2) { // Set spectrum analyzer mode ds << TQString("SETMODESPECTRUMANALYZER"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(3) } else if (m_commHandlerState == 3) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get command status TQString result; ds >> result; m_socket->clearFrameTail(); if (result == "ACK") { // Set spectrum analyzer mode ds << TQString("SETMODESPECTRUMANALYZER"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(4) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 4) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get command status TQString result; ds >> result; m_socket->clearFrameTail(); if (result == "ACK") { // Get number of samples in trace, step 1 ds << TQString("GETTRACESAMPLECOUNT"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(5) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 5) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get number of samples in trace, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_samplesInTrace; } m_socket->clearFrameTail(); if (result == "ACK") { // Get number of horizontal divisions, step 1 ds << TQString("GETHORIZONTALDIVCOUNT"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(6) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 6) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get number of horizontal divisions, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_hdivs; } m_socket->clearFrameTail(); if (result == "ACK") { // Get number of vertical divisions, step 1 ds << TQString("GETVERTICALDIVCOUNT"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(7) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 7) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get number of vertical divisions, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_vdivs; } m_socket->clearFrameTail(); if (result == "ACK") { // Get reference power level, step 1 ds << TQString("GETREFERENCEPOWERLEVEL"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(8) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 8) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get reference power level, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_rpower; } m_socket->clearFrameTail(); if (result == "ACK") { // Get vertical division scale, step 1 ds << TQString("GETVERTDIVSCALE"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(9) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 9) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get vertical division scale, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_vscale; } m_socket->clearFrameTail(); if (result == "ACK") { // Get center frequency, step 1 ds << TQString("GETCENTERFREQUENCY"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(10) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 10) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get center frequency, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_centerfreq; } m_socket->clearFrameTail(); if (result == "ACK") { // Get frequency span, step 1 ds << TQString("GETFREQUENCYSPAN"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(11) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 11) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get frequency span, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_spanfreq; } m_socket->clearFrameTail(); if (result == "ACK") { // Update display widget(s) updateGraticule(); } if (result == "ACK") { // Get trace, step 1 ds << TQString("GETSPECTRUMTRACE"); m_socket->writeEndOfFrame(); SET_NEXT_STATE(12) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 12) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get trace, step 2 TQDoubleArray trace; TQString result; ds >> result; if (result == "ACK") { ds >> trace; } m_socket->clearFrameTail(); if (result == "ACK") { // Update display widget(s) m_traceWidget->setSamples(0, trace); postProcessTrace(); m_traceWidget->repaint(); } if (result == "ACK") { SET_NEXT_STATE(2) } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerState == 255) { // Execute pending command m_commHandlerMode = 2; m_socket->clearIncomingData(); } SET_WATCHDOG_TIMER } else if (m_commHandlerMode == 2) { if (m_commHandlerCommandState == 0) { m_commHandlerMode = 0; m_commHandlerState = 2; } else if (m_commHandlerCommandState == 1) { // Set reference power level ds << TQString("SETREFERENCEPOWERLEVEL"); ds << m_rpower; m_socket->writeEndOfFrame(); m_commHandlerCommandState = 2; } else if (m_commHandlerCommandState == 2) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Set reference power level, step 2 TQString result; ds >> result; m_socket->clearFrameTail(); if (result == "ACK") { // Get reference power level, step 1 ds << TQString("GETREFERENCEPOWERLEVEL"); m_socket->writeEndOfFrame(); m_commHandlerCommandState = 3; } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } else if (m_commHandlerCommandState == 3) { // Get response data if (m_socket->canReadFrame()) { PAT_WATCHDOG_TIMER // Get reference power level, step 2 TQString result; ds >> result; if (result == "ACK") { ds >> m_rpower; } m_socket->clearFrameTail(); // Update display as needed updateGraticule(); if (result == "ACK") { m_commHandlerCommandState = 0; TQTimer::singleShot(0, this, SLOT(mainEventLoop())); } else { COMMUNICATIONS_FAILED } } else { if (!m_updateTimeoutTimer->isActive()) { UPDATEDISPLAY_TIMEOUT } } } } } else { m_commHandlerState = 0; m_commHandlerCommandState = 0; } m_instrumentMutex->unlock(); } void CommAnalyzerPart::postProcessTrace() { return; } void CommAnalyzerPart::updateGraticule() { m_traceWidget->setNumberOfSamples(0, m_samplesInTrace); m_traceWidget->setNumberOfHorizontalDivisions(m_hdivs); m_traceWidget->setNumberOfVerticalDivisions(m_vdivs); m_leftFrequency = m_centerfreq - (m_spanfreq/2.0); m_rightFrequency = m_centerfreq + (m_spanfreq/2.0); double freqincr = (m_rightFrequency-m_leftFrequency)/m_samplesInTrace; double freqpos = m_leftFrequency; TQDoubleArray tracepositions; tracepositions.resize(m_samplesInTrace); for (int i=0; isetPositions(0, tracepositions); m_traceWidget->setDisplayLimits(0, m_leftFrequency, m_rpower, m_rightFrequency, m_rpower-(m_vscale*m_hdivs)); // Also update controls m_base->saRefLevel->blockSignals(true); m_base->saRefLevel->setFloatValue(m_rpower); m_base->saRefLevel->blockSignals(false); } void CommAnalyzerPart::saRefLevelChanged(double newval) { m_rpower = newval; m_commHandlerMode = 1; m_commHandlerCommandState = 1; mainEventLoop(); } KAboutData* CommAnalyzerPart::createAboutData() { return new KAboutData( APP_NAME, I18N_NOOP( APP_PRETTYNAME ), APP_VERSION ); } } //namespace RemoteLab #include "part.moc"