summaryrefslogtreecommitdiffstats
path: root/clients/tde/src/part/commanalyzer/part.cpp
blob: 3fd89eb16ac8498ac455eea769c99cbc8f4f435c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//Author:    Timothy Pearson <kb9vqf@pearsoncomputing.net>, (C) 2012
//Copyright: See COPYING file that comes with this distribution

#include "debug.h"
#include "define.h"
#include "part.h"

#include <kaboutdata.h>   //::createAboutData()
#include <kaction.h>
#include <klocale.h>
#include <kmessagebox.h>  //::start()
#include <kparts/genericfactory.h>
#include <kstatusbar.h>
#include <kstdaction.h>
#include <tqfile.h>        //encodeName()
#include <tqtimer.h>       //postInit() hack
#include <tqvbox.h>
#include <tqsocket.h>
#include <tqmutex.h>
#include <tqeventloop.h>
#include <tqapplication.h>
#include <unistd.h>       //access()
#include <stdint.h>

#include "tracewidget.h"
#include "floatspinbox.h"
#include "layout.h"

namespace RemoteLab {

typedef KParts::GenericFactory<RemoteLab::CommAnalyzerPart> Factory;
K_EXPORT_COMPONENT_FACTORY( libremotelab_commanalyzer, RemoteLab::Factory )


CommAnalyzerPart::CommAnalyzerPart( TQWidget *parentWidget, const char *widgetName, TQObject *parent, const char *name, const TQStringList& )
	: ReadOnlyPart( parent, name ), m_traceWidget(0), m_socket(0), m_base(0), stopTraceUpdate(false)
{
	// Initialize mutex
	m_instrumentMutex = new TQMutex(false);

	// Initialize kpart
	setInstance(Factory::instance());
	setWidget(new TQVBox(parentWidget, widgetName));

	// Create widgets
	m_base = new CommAnalyzerBase(widget());
	m_traceWidget = m_base->traceWidget;
	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_traceWidget) {
		delete m_traceWidget;
	}
	if (m_socket) {
		m_socket->close();
		while (m_socket->state() == TQSocket::Closing) {
			tqApp->processEvents();
		}
		delete m_socket;
	}

	delete m_instrumentMutex;
}

void CommAnalyzerPart::postInit() {
	m_updateTimer = new TQTimer(this);
	connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTrace()));
}

bool CommAnalyzerPart::openURL(const KURL &url) {
	connectToServer(url.url());
}

bool CommAnalyzerPart::closeURL() {
	m_socket->close();

	while (m_socket->state() != TQSocket::Idle) {
		tqApp->processEvents();
	}

	m_url = KURL();
	
	return true;
}

TQString CommAnalyzerPart::callServerMethod(int command) {
	if (m_instrumentMutex->locked() == true) {
		printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout);
		return TQString::null;
	}
	m_instrumentMutex->lock();
	if (m_socket->state() == TQSocket::Connected) {
		TQString cmd = TQChar(command);
		cmd.append('\r');
		m_socket->writeBlock(cmd.latin1(), cmd.length());
		// Read from the server
		TQString serverRet;
		while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) {
			char data[1];
			if( m_socket->readBlock(data, 1) > 0) {
				serverRet.append(data[0]);
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		m_instrumentMutex->unlock();
		return serverRet;
	}
	else {
		m_instrumentMutex->unlock();
		return TQString::null;
	}
}

int16_t CommAnalyzerPart::callServerMethodInt16(int command) {
	if (m_instrumentMutex->locked() == true) {
		printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout);
		return 0;
	}
	m_instrumentMutex->lock();
	if (m_socket->state() == TQSocket::Connected) {
		TQString cmd = TQChar(command);
		cmd.append('\r');
		m_socket->writeBlock(cmd.latin1(), cmd.length());
		// Read from the server
		int bytesread = 0;
		int16_t data[1];
		while ((bytesread < 2) && (m_socket->state() == TQSocket::Connected)) {
			int ret = m_socket->readBlock(((char*)data)+bytesread, 1);
			if (ret > 0) {
				bytesread += ret;
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		TQString serverRet;
		while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) {
			char data[1];
			if( m_socket->readBlock(data, 1) > 0) {
				serverRet.append(data[0]);
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		m_instrumentMutex->unlock();
		return data[0];
	}
	else {
		m_instrumentMutex->unlock();
		return 0;
	}
}

double CommAnalyzerPart::callServerMethodDouble(int command) {
	if (m_instrumentMutex->locked() == true) {
		printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout);
		return 0;
	}
	m_instrumentMutex->lock();
	if (m_socket->state() == TQSocket::Connected) {
		TQString cmd = TQChar(command);
		cmd.append('\r');
		m_socket->writeBlock(cmd.latin1(), cmd.length());
		// Read from the server
		unsigned int bytesread = 0;
		double data[1];
		while ((bytesread < sizeof(double)) && (m_socket->state() == TQSocket::Connected)) {
			int ret = m_socket->readBlock(((char*)data)+bytesread, 1);
			if (ret > 0) {
				bytesread += ret;
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		TQString serverRet;
		while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) {
			char data[1];
			if( m_socket->readBlock(data, 1) > 0) {
				serverRet.append(data[0]);
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		m_instrumentMutex->unlock();
		return data[0];
	}
	else {
		m_instrumentMutex->unlock();
		return 0;
	}
}

void CommAnalyzerPart::sendServerCommandWithParameter(int command, TQString param) {
	if (m_instrumentMutex->locked() == true) {
		printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout);
		return;
	}
	m_instrumentMutex->lock();
	if (m_socket->state() == TQSocket::Connected) {
		TQString cmd = TQChar(command);
		param = TQString("%1%2%3").arg(param).arg(TQChar('°')).arg(TQChar('\r'));
		cmd += param;
		m_socket->writeBlock(cmd.ascii(), cmd.length());
		// Read from the server
		TQString serverRet;
		while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) {
			char data[1];
			if( m_socket->readBlock(data, 1) > 0) {
				serverRet.append(data[0]);
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
	}
	m_instrumentMutex->unlock();
}

void CommAnalyzerPart::sendServerCommand(int command) {
	if (m_instrumentMutex->locked() == true) {
		printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout);
		return;
	}
	m_instrumentMutex->lock();
	if (m_socket->state() == TQSocket::Connected) {
		TQString cmd = TQChar(command);
		cmd.append('\r');
		m_socket->writeBlock(cmd.latin1(), cmd.length());
		// Read from the server
		TQString serverRet;
		while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) {
			char data[1];
			if( m_socket->readBlock(data, 1) > 0) {
				serverRet.append(data[0]);
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
	}
	m_instrumentMutex->unlock();
}

void CommAnalyzerPart::callServerMethodDoubleArray(int command, double * array, int arrayLen) {
	if (m_instrumentMutex->locked() == true) {
		printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout);
		return;
	}
	m_instrumentMutex->lock();
	if (m_socket->state() == TQSocket::Connected) {
		TQString cmd = TQChar(command);
		cmd.append('\r');
		m_socket->writeBlock(cmd.latin1(), cmd.length());
		// Read from the server
		TQString serverRet;
		while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) {
			char data[1];
			if( m_socket->readBlock(data, 1) > 0) {
				serverRet.append(data[0]);
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		unsigned int bytesread = 0;
		int16_t data[1];
		while ((bytesread < 2) && (m_socket->state() == TQSocket::Connected)) {
			int ret = m_socket->readBlock(((char*)data)+bytesread, 1);
			if (ret > 0) {
				bytesread += ret;
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		serverRet = "";
		while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) {
			char data[1];
			if( m_socket->readBlock(data, 1) > 0) {
				serverRet.append(data[0]);
			}
			tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
		}
		bytesread = 0;
		int elementsread = 0;
		for (elementsread=0;elementsread<arrayLen;elementsread++) {
			bytesread = 0;
			while ((bytesread < sizeof(double)) && (m_socket->state() == TQSocket::Connected)) {
				if (m_socket->size() < 1) {
					tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
				}
				int ret = m_socket->readBlock(((char*)array)+bytesread+(elementsread*sizeof(double)), 1);
				if (ret > 0) {
					bytesread += ret;
				}
			}
		}
	}
	m_instrumentMutex->unlock();
}

int CommAnalyzerPart::connectToServer(TQString server) {
	if (!m_socket) {
		m_socket = new TQSocket(this);
// 		connect(m_socket, SIGNAL(connected()), SLOT(socketConnected()));
// 		connect(m_socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed()));
// 		connect(m_socket, SIGNAL(readyRead()), SLOT(socketReadyRead()));
// 		connect(m_socket, SIGNAL(error(int)), SLOT(socketError(int)));
	}
	m_socket->connectToHost(server, 4002);
	while ((m_socket->state() != TQSocket::Connected) && (m_socket->state() != TQSocket::Idle)) {
		tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents);
	}
	if (m_socket->state() != TQSocket::Connected) {
		return -1;
	}

	// Gather information from the server
	if (callServerMethod(41) == "NCK") {
		// FIXME
		// Display message and exit
		return -1;
	}
	sendServerCommand(40);					// Set spectrum analyzer mode
	m_samplesInTrace = callServerMethodInt16(63);		// Get number of samples in trace
	m_traceWidget->setNumberOfSamples(m_samplesInTrace);
	m_hdivs = callServerMethodInt16(62);			// Get number of horizontal divisions
	m_traceWidget->setNumberOfHorizontalDivisions(m_hdivs);
	m_vdivs = callServerMethodInt16(64);			// Get number of vertical divisions
	m_traceWidget->setNumberOfVerticalDivisions(m_vdivs);

	m_rpower = callServerMethodDouble(65);			// Get reference power level
	m_vscale = callServerMethodDouble(66);			// Get vertical division scale

	m_centerfreq = callServerMethodDouble(67);		// Get center frequency
	m_spanfreq = callServerMethodDouble(68);		// Get frequency span

	updateGraticule();

	// Start trace update timer
	m_updateTimer->start(10, FALSE);
}

void CommAnalyzerPart::postProcessTrace() {
	return;
}

void CommAnalyzerPart::updateTrace() {
	m_updateTimer->stop();
	callServerMethodDoubleArray(42, m_traceWidget->samples(), m_samplesInTrace);
	postProcessTrace();
	m_traceWidget->repaint();
	if (m_socket->state() == TQSocket::Connected) {
		if (stopTraceUpdate == true) {
			stopTraceUpdate = false;
		}
		else {
			m_updateTimer->start(10, FALSE);
		}
	}
}

void CommAnalyzerPart::updateGraticule() {
	m_leftFrequency = m_centerfreq - (m_spanfreq/2.0);
	m_rightFrequency = m_centerfreq + (m_spanfreq/2.0);
	m_traceWidget->setDisplayLimits(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) {
	// We cannot directly send data to the remote instrument because the GUI event may have ocurred during a remote instrument transaction
	// This "flaw" is a direct result of maximizing performance by processing GUI events during network transfers, as well as the fact that this client is a multithreaded application
	m_rpower = newval;
	stopTraceUpdate = true;
	TQTimer::singleShot(0, this, SLOT(changeSaRefLevel()));
}

void CommAnalyzerPart::changeSaRefLevel() {
	// Keep trying to set the new power level
	if (m_instrumentMutex->locked() == false) {
		sendServerCommandWithParameter(61, TQString("%1").arg(m_rpower, 0, 'E'));	// Set reference power level
		m_rpower = callServerMethodDouble(65);						// Get reference power level
		updateGraticule();								// Update the display grid
		m_updateTimer->start(10, FALSE);						// Restart trace update timer
	}
	else {
		tqApp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput);
		TQTimer::singleShot(0, this, SLOT(changeSaRefLevel()));
	}
}

KAboutData* CommAnalyzerPart::createAboutData() {
	return new KAboutData( APP_NAME, I18N_NOOP( APP_PRETTYNAME ), APP_VERSION );
}

} //namespace RemoteLab

#include "part.moc"