Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
750 | KeyOz | 1 | /*************************************************************************** |
2 | * Copyright (C) 2009 by Manuel Schrape * |
||
3 | * manuel.schrape@gmx.de * |
||
4 | * * |
||
5 | * This program is free software; you can redistribute it and/or modify * |
||
6 | * it under the terms of the GNU General Public License as published by * |
||
7 | * the Free Software Foundation; either version 2 of the License. * |
||
8 | * * |
||
9 | * This program is distributed in the hope that it will be useful, * |
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
12 | * GNU General Public License for more details. * |
||
13 | * * |
||
14 | * You should have received a copy of the GNU General Public License * |
||
15 | * along with this program; if not, write to the * |
||
16 | * Free Software Foundation, Inc., * |
||
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
18 | ***************************************************************************/ |
||
19 | |||
20 | #include <QSettings> |
||
21 | #include <QMessageBox> |
||
22 | #include <QCryptographicHash> |
||
23 | |||
24 | #include "wgt_Connection.h" |
||
25 | |||
26 | wgt_Connection::wgt_Connection(QWidget *parent) : QWidget(parent) |
||
27 | { |
||
28 | setupUi(this); |
||
29 | |||
30 | o_Input = new Input(); |
||
31 | |||
32 | o_AboTimer = new QTimer(); |
||
33 | o_AboTimer->setInterval(3500); |
||
34 | |||
35 | connect(btn_Connect, SIGNAL(clicked()), this, SLOT(slot_btn_Connect())); |
||
36 | connect(sb_Intervall, SIGNAL(valueChanged(int)), this, SLOT(slot_sb_Intervall(int))); |
||
37 | |||
38 | connect(o_AboTimer, SIGNAL(timeout()), this, SLOT(slot_TimeOut_AboTimer())); |
||
39 | } |
||
40 | |||
41 | void wgt_Connection::read_Settings() |
||
42 | { |
||
43 | QSettings Setting("QMK", "QMK-Connection"); |
||
44 | |||
45 | Setting.beginGroup("DATA"); |
||
46 | SERVER.Intervall = Setting.value(QString("Intervall_%1").arg(gi_ID), 500).toInt(); |
||
47 | Setting.endGroup(); |
||
48 | |||
49 | Setting.beginGroup("SERVER"); |
||
50 | SERVER.Password = Setting.value("Password", QString("")).toString(); |
||
51 | SERVER.IP_MAX = Setting.value("IP_MAX", 1).toInt(); |
||
52 | SERVER.IP_ID = Setting.value("IP_ID", 0).toInt(); |
||
53 | |||
54 | for (int z = 0; z < SERVER.IP_MAX; z++) |
||
55 | { |
||
56 | SERVER.IP[z] = Setting.value("IP_" + QString("%1").arg(z), QString("127.0.0.1:64400")).toString(); |
||
57 | } |
||
58 | Setting.endGroup(); |
||
59 | |||
60 | for(int z = 0; z < SERVER.IP_MAX; z++) |
||
61 | { |
||
62 | if (cb_Server->findText(SERVER.IP[z]) == -1) |
||
63 | { |
||
64 | cb_Server->addItem(SERVER.IP[z]); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | cb_Server->setCurrentIndex(SERVER.IP_ID); |
||
69 | |||
70 | le_Password->setText(SERVER.Password); |
||
71 | |||
72 | sb_Intervall->setValue(SERVER.Intervall); |
||
73 | |||
74 | } |
||
75 | |||
76 | void wgt_Connection::write_Settings() |
||
77 | { |
||
78 | SERVER.Intervall = sb_Intervall->value(); |
||
79 | |||
80 | SERVER.Password = le_Password->text(); |
||
81 | SERVER.IP_MAX = cb_Server->count(); |
||
82 | SERVER.IP_ID = cb_Server->currentIndex(); |
||
83 | |||
84 | for (int z = 0; z < cb_Server->count(); z++) |
||
85 | { |
||
86 | if (z < 10) |
||
87 | { |
||
88 | SERVER.IP[z] = cb_Server->itemText(z); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | QSettings Setting("QMK", "QMK-Connection"); |
||
93 | |||
94 | Setting.beginGroup("DATA"); |
||
95 | Setting.setValue(QString("Intervall_%1").arg(gi_ID), SERVER.Intervall); |
||
96 | Setting.endGroup(); |
||
97 | |||
98 | Setting.beginGroup("SERVER"); |
||
99 | Setting.setValue("Password", SERVER.Password); |
||
100 | Setting.setValue("IP_MAX", SERVER.IP_MAX); |
||
101 | Setting.setValue("IP_ID", SERVER.IP_ID); |
||
102 | |||
103 | for (int z = 0; z < SERVER.IP_MAX; z++) |
||
104 | { |
||
105 | Setting.setValue("IP_" + QString("%1").arg(z), SERVER.IP[z]); |
||
106 | } |
||
107 | Setting.endGroup(); |
||
108 | } |
||
109 | |||
110 | void wgt_Connection::set_Client(int li_ID, QString ls_Client, QString ls_Fields) |
||
111 | { |
||
112 | gi_Interval[0] = false; |
||
113 | gi_Interval[1] = false; |
||
114 | |||
115 | gs_Client = ls_Client; |
||
116 | gs_Fields = ls_Fields; |
||
117 | gi_ID = li_ID; |
||
118 | |||
119 | if (ls_Fields.contains('D')) |
||
120 | gi_Interval[0] = true; |
||
121 | |||
122 | if (ls_Fields.contains('O')) |
||
123 | gi_Interval[1] = true; |
||
124 | |||
125 | read_Settings(); |
||
126 | } |
||
127 | |||
128 | void wgt_Connection::set_SelectVisible(bool pi_Visible) |
||
129 | { |
||
130 | wg_Select->setVisible(pi_Visible); |
||
131 | } |
||
132 | |||
133 | void wgt_Connection::set_IntervalVisible(bool pi_Visible) |
||
134 | { |
||
135 | wg_Interval->setVisible(pi_Visible); |
||
136 | } |
||
137 | |||
138 | void wgt_Connection::set_ButtonVisible(bool pi_Visible) |
||
139 | { |
||
140 | btn_Connect->setVisible(pi_Visible); |
||
141 | } |
||
142 | |||
143 | void wgt_Connection::send_Data(QString ps_Data, int pi_ID) |
||
144 | { |
||
145 | o_Input->send_Data(ps_Data, pi_ID); |
||
146 | } |
||
147 | |||
148 | // MK-Eingangsdaten verarbeiten |
||
149 | void wgt_Connection::parse_MK_Data(QString t_Data) |
||
150 | { |
||
151 | unsigned char OutData[200]; |
||
152 | char *InData = t_Data.toLatin1().data(); |
||
153 | |||
154 | if (HandlerMK::Decode_64(InData, t_Data.length(), OutData) != 0) |
||
155 | { |
||
156 | |||
157 | switch(InData[2]) |
||
158 | { |
||
159 | case 'A' : // Analog-Labels |
||
160 | { |
||
161 | o_Input->stop_Resend(DATA_READ_LABEL); |
||
162 | |||
163 | s_MK_DebugLabels MK_DebugLabels; |
||
164 | |||
165 | MK_DebugLabels.Position = OutData[0]; |
||
166 | |||
167 | if (MK_DebugLabels.Position < 32) |
||
168 | { |
||
169 | MK_DebugLabels.Text = HandlerMK::Data2QString(OutData,1,17).trimmed(); |
||
170 | if (MK_DebugLabels.Text == "") |
||
171 | { |
||
172 | MK_DebugLabels.Text = "Debug-" + QString("%1").arg(MK_DebugLabels.Position); |
||
173 | } |
||
174 | |||
175 | if (((MK_DebugLabels.Position + 1) < 32))// && (get_Analoglabels == true)) |
||
176 | { |
||
177 | c_Data[0] = MK_DebugLabels.Position + 1; |
||
178 | o_Input->send_Data(HandlerMK::make_Frame('a', ADDRESS_ALL, c_Data, 1).toLatin1().data(), DATA_READ_LABEL); |
||
179 | } |
||
180 | |||
181 | emit(sig_MK_DebugLabels(MK_DebugLabels)); |
||
182 | } |
||
183 | } |
||
184 | break; |
||
185 | case 'D' : // Debug-Daten |
||
186 | { |
||
187 | s_MK_Debug MK_Debug; |
||
188 | |||
189 | memcpy((unsigned char *)&MK_Debug, (unsigned char *)&OutData, sizeof(MK_Debug)); |
||
190 | |||
191 | emit(sig_MK_Debug(MK_Debug)); |
||
192 | emit(sig_RawData(t_Data)); |
||
193 | } |
||
194 | break; |
||
195 | case 'N' : // MotorMixer lesen |
||
196 | { |
||
197 | o_Input->stop_Resend(DATA_READ_MIXER); |
||
198 | |||
199 | s_MK_Mixer MK_Mixer; |
||
200 | |||
201 | memcpy((unsigned char *)&MK_Mixer, (unsigned char *)&OutData, sizeof(MK_Mixer)); |
||
202 | |||
203 | emit (sig_MK_ReadMotorMixer(MK_Mixer)); |
||
204 | } |
||
205 | break; |
||
206 | case 'M' : // MotorMixer geschrieben |
||
207 | { |
||
208 | o_Input->stop_Resend(DATA_WRITE_MIXER); |
||
209 | |||
210 | emit (sig_MK_WriteMotorMixer(OutData[0])); |
||
211 | } |
||
212 | break; |
||
213 | |||
214 | case 'O' : // Navi-OSD-Data |
||
215 | { |
||
216 | if (InData[1] - 'a' == ADDRESS_NC) |
||
217 | { |
||
218 | s_MK_NaviData MK_NaviData; |
||
219 | |||
220 | memcpy((unsigned char *)&MK_NaviData, (unsigned char *)&OutData, sizeof(MK_NaviData)); |
||
221 | if (MK_NaviData.Version == MK_VERSION_NAVI) |
||
222 | { |
||
223 | emit(sig_MK_NaviData(MK_NaviData)); |
||
224 | emit(sig_RawData(t_Data)); |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | break; |
||
229 | |||
230 | case 'P' : // RC-Kanäle |
||
231 | { |
||
232 | s_MK_PPM_Data PPM_in; |
||
233 | |||
234 | memcpy((unsigned char *)&PPM_in, (unsigned char *)&OutData, sizeof(PPM_in)); |
||
235 | |||
236 | // qDebug() << PPM_in[0] << ", " << PPM_in[1] << ", " << PPM_in[2] << ", " << PPM_in[3]; |
||
237 | |||
238 | emit(sig_MK_PPMData(PPM_in)); |
||
239 | } |
||
240 | break; |
||
241 | |||
242 | case 'Q' : // Settings lesen |
||
243 | { |
||
244 | o_Input->stop_Resend(DATA_READ_SETTINGS); |
||
245 | |||
246 | s_MK_Settings MK_Set; |
||
247 | |||
248 | memcpy((unsigned char *)&MK_Set, (unsigned char *)&OutData, sizeof(MK_Set)); |
||
249 | |||
250 | emit (sig_MK_ReadSettings(MK_Set)); |
||
251 | } |
||
252 | break; |
||
253 | case 'S' : // Settings geschrieben |
||
254 | { |
||
255 | o_Input->stop_Resend(DATA_WRITE_SETTINGS); |
||
256 | |||
257 | emit (sig_MK_WriteSettings(OutData[0])); |
||
258 | } |
||
259 | break; |
||
260 | |||
261 | case 'V' : // Versions-Info |
||
262 | { |
||
263 | o_Input->stop_Resend(DATA_VERSION); |
||
264 | VersionInfo = HandlerMK::parse_Version(OutData, InData[1] - 'a'); |
||
265 | |||
266 | if (VersionInfo.ID == ADDRESS_FC) |
||
267 | { |
||
268 | rb_FC->setChecked(true); |
||
269 | } |
||
270 | if (VersionInfo.ID == ADDRESS_NC) |
||
271 | { |
||
272 | rb_NC->setChecked(true); |
||
273 | } |
||
274 | if (VersionInfo.ID == ADDRESS_MK3MAG) |
||
275 | { |
||
276 | rb_MK3MAG->setChecked(true); |
||
277 | } |
||
278 | |||
279 | slot_sb_Intervall(sb_Intervall->value()); |
||
280 | |||
281 | emit sig_MK_Version(VersionInfo); |
||
282 | } |
||
283 | break; |
||
284 | case 'W' : // WayPoints |
||
285 | { |
||
286 | o_Input->stop_Resend(DATA_WRITE_WAYPOINT); |
||
287 | |||
288 | emit(sig_MK_WayPoint(OutData[0])); |
||
289 | } |
||
290 | break; |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | |||
295 | // IP-Daten verarbeiten.. |
||
296 | void wgt_Connection::parse_IP_Data(QString t_Data) |
||
297 | { |
||
298 | QStringList Data; |
||
299 | Data = t_Data.split(":"); |
||
300 | |||
301 | if (Data.count() > 1) |
||
302 | { |
||
303 | int CMD = Data[2].toInt(); |
||
304 | |||
305 | switch(CMD) |
||
306 | { |
||
307 | case 101 : |
||
308 | { |
||
309 | o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 101, gs_Client)); |
||
310 | } |
||
311 | break; |
||
312 | case 502 : |
||
313 | { |
||
314 | switch (Data[3].toInt()) |
||
315 | { |
||
316 | case 105 : |
||
317 | { |
||
318 | QString s_MD5PW; |
||
319 | QByteArray a_MD5PW; |
||
320 | |||
321 | a_MD5PW = QCryptographicHash::hash(le_Password->text().toAscii(),QCryptographicHash::Md5); |
||
322 | |||
323 | s_MD5PW = QString(a_MD5PW.toHex().data()); |
||
324 | |||
325 | o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 105, s_MD5PW)); |
||
326 | } |
||
327 | break; |
||
328 | case 106 : |
||
329 | { |
||
330 | o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 106, gs_Fields)); |
||
331 | } |
||
332 | break; |
||
333 | } |
||
334 | } |
||
335 | break; |
||
336 | case 505 : |
||
337 | { |
||
338 | if (Data[3] == "OK") |
||
339 | { |
||
340 | } |
||
341 | else |
||
342 | { |
||
343 | QMessageBox::warning(this, gs_Client, trUtf8("Authentifizierung fehlgeschlagen. <br />Daten senden zum Mikrokopter nicht möglich."), QMessageBox::Ok); |
||
344 | } |
||
345 | } |
||
346 | break; |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | |||
351 | // Datenintervall geändert. |
||
352 | void wgt_Connection::slot_sb_Intervall(int t_Intervall) |
||
353 | { |
||
354 | if (t_Intervall == 0) |
||
355 | { |
||
356 | c_Data[0] = 0; |
||
357 | } |
||
358 | else |
||
359 | { |
||
360 | c_Data[0] = t_Intervall / 10; |
||
361 | } |
||
362 | |||
363 | // if (wg_Interval->isVisible()) |
||
364 | { |
||
365 | if (gi_Interval[0]) |
||
366 | { |
||
367 | o_Input->send_Data(HandlerMK::make_Frame('d', ADDRESS_ALL, c_Data, 1).toLatin1().data()); |
||
368 | } |
||
369 | |||
370 | if (gi_Interval[1]) |
||
371 | { |
||
372 | o_Input->send_Data(HandlerMK::make_Frame('o', ADDRESS_ALL, c_Data, 1).toLatin1().data()); |
||
373 | } |
||
374 | } |
||
375 | } |
||
376 | |||
377 | void wgt_Connection::slot_send_Data(QString ps_Data, int pi_ID) |
||
378 | { |
||
379 | send_Data(ps_Data, pi_ID); |
||
380 | } |
||
381 | |||
382 | void wgt_Connection::slot_TimeOut_AboTimer() |
||
383 | { |
||
384 | // qDebug("Timer"); |
||
385 | slot_sb_Intervall(sb_Intervall->value()); |
||
386 | } |
||
387 | |||
388 | void wgt_Connection::slot_btn_Connect() |
||
389 | { |
||
390 | if (!o_Input->IsOpen()) |
||
391 | { |
||
392 | if (cb_Server->findText(cb_Server->currentText()) == -1) |
||
393 | { |
||
394 | cb_Server->addItem(cb_Server->currentText()); |
||
395 | cb_Server->setCurrentIndex(cb_Server->findText(cb_Server->currentText())); |
||
396 | } |
||
397 | |||
398 | cb_Server->setEnabled(false); |
||
399 | le_Password->setEnabled(false); |
||
400 | |||
401 | if (cb_Server->currentText().startsWith('/')) |
||
402 | { |
||
403 | o_Input = new Input_TTY(); |
||
404 | o_Input->Init(); |
||
405 | |||
406 | set_Input s_Input; |
||
407 | s_Input.Main = cb_Server->currentText(); |
||
408 | |||
409 | if (o_Input->Open(s_Input) == true) |
||
410 | { |
||
411 | emit sig_Status(true); |
||
412 | btn_Connect->setChecked(true); |
||
413 | o_AboTimer->start(); |
||
414 | |||
415 | connect(o_Input, SIGNAL(sig_NewData(QString)), this, SLOT(slot_Input_Data(QString))); |
||
416 | |||
417 | o_Input->send_Data(HandlerMK::make_Frame('v', 0, c_Data, 0).toLatin1().data(), DATA_VERSION); |
||
418 | } |
||
419 | else |
||
420 | { |
||
421 | cb_Server->setEnabled(true); |
||
422 | le_Password->setEnabled(true); |
||
423 | } |
||
424 | |||
425 | } |
||
426 | else |
||
427 | { |
||
428 | o_Input = new Input_TCP(); |
||
429 | o_Input->Init(); |
||
430 | |||
431 | set_Input s_Input; |
||
432 | |||
433 | QStringList Server = cb_Server->currentText().split(":"); |
||
434 | |||
435 | s_Input.Main = Server[0]; |
||
436 | s_Input.Sub = Server[1]; |
||
437 | |||
438 | if (o_Input->Open(s_Input) == true) |
||
439 | { |
||
440 | connect(o_Input, SIGNAL(sig_Disconnected(int)), this, SLOT(slot_Input_Disconnected(int))); |
||
441 | connect(o_Input, SIGNAL(sig_Connected()), this, SLOT(slot_Input_Connected())); |
||
442 | } |
||
443 | } |
||
444 | } |
||
445 | else |
||
446 | { |
||
447 | cb_Server->setEnabled(true); |
||
448 | le_Password->setEnabled(true); |
||
449 | |||
450 | emit sig_Status(false); |
||
451 | btn_Connect->setChecked(false); |
||
452 | o_AboTimer->stop(); |
||
453 | |||
454 | o_Input->Close(); |
||
455 | disconnect(o_Input, SIGNAL(sig_NewData(QString)), 0, 0); |
||
456 | if (o_Input->Mode() == TCP) |
||
457 | { |
||
458 | disconnect(o_Input, SIGNAL(sig_Disconnected(int)), 0, 0); |
||
459 | disconnect(o_Input, SIGNAL(sig_Connected()), 0, 0); |
||
460 | } |
||
461 | } |
||
462 | } |
||
463 | |||
464 | // Neue Daten empfangen. |
||
465 | void wgt_Connection::slot_Input_Data(QString t_Data) |
||
466 | { |
||
467 | if ((t_Data[0] == '#')) |
||
468 | { |
||
469 | if ((HandlerMK::Check_CRC(t_Data.toLatin1().data(), t_Data.length() - 1)) || ((o_Input->Mode() == TTY) && (HandlerMK::Check_CRC(t_Data.toLatin1().data(), t_Data.length())))) |
||
470 | { |
||
471 | parse_MK_Data(t_Data); |
||
472 | } |
||
473 | else |
||
474 | { |
||
475 | // qDebug(QString("CRC-Error - " + t_Data).toLatin1().data()); |
||
476 | } |
||
477 | } |
||
478 | else if ((o_Input->Mode() == TCP) && (t_Data[0] == '$')) |
||
479 | { |
||
480 | parse_IP_Data(t_Data); |
||
481 | } |
||
482 | } |
||
483 | |||
484 | // Neue Serververbindung. |
||
485 | void wgt_Connection::slot_Input_Connected() |
||
486 | { |
||
487 | connect(o_Input, SIGNAL(sig_NewData(QString)), this, SLOT(slot_Input_Data(QString))); |
||
488 | |||
489 | o_Input->send_Data(HandlerMK::make_Frame('v', 0, c_Data, 0).toLatin1().data(), DATA_VERSION); |
||
490 | emit sig_Status(true); |
||
491 | btn_Connect->setChecked(true); |
||
492 | o_AboTimer->start(); |
||
493 | } |
||
494 | |||
495 | // Serververbindung beendet |
||
496 | void wgt_Connection::slot_Input_Disconnected(int Error) |
||
497 | { |
||
498 | cb_Server->setEnabled(true); |
||
499 | le_Password->setEnabled(true); |
||
500 | |||
501 | disconnect(o_Input, SIGNAL(sig_NewData(QString)), 0, 0); |
||
502 | disconnect(o_Input, SIGNAL(sig_Disconnected(int)), 0, 0); |
||
503 | disconnect(o_Input, SIGNAL(sig_Connected()), 0, 0); |
||
504 | |||
505 | emit sig_Status(false); |
||
506 | btn_Connect->setChecked(false); |
||
507 | o_AboTimer->stop(); |
||
508 | |||
509 | switch (Error) |
||
510 | { |
||
511 | case REMOTECLOSED : |
||
512 | { |
||
513 | // lb_Status->setText(tr("Verbindung vom Server beendet.")); |
||
514 | QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Verbindung wurde vom Server beendet."), QMessageBox::Ok); |
||
515 | } |
||
516 | break; |
||
517 | case REFUSED : |
||
518 | { |
||
519 | // lb_Status->setText(tr("Server nicht gefunden.")); |
||
520 | QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Kann nicht zum Server verbinden."), QMessageBox::Ok); |
||
521 | } |
||
522 | break; |
||
523 | case 3 : |
||
524 | { |
||
525 | // lb_Status->setText(tr("Serververbindung getrennt. Logindaten falsch.")); |
||
526 | QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Loginname oder Password falsch."), QMessageBox::Ok); |
||
527 | } |
||
528 | break; |
||
529 | default : |
||
530 | { |
||
531 | // lb_Status->setText(tr("Getrennt vom QMK-Datenserver.")); |
||
532 | } |
||
533 | break; |
||
534 | } |
||
535 | |||
536 | } |
||
537 | |||
538 | wgt_Connection::~wgt_Connection() |
||
539 | { |
||
540 | write_Settings(); |
||
541 | } |