Rev 750 | Details | Compare with Previous | 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 | { |
||
801 | - | 145 | // qDebug(ps_Data.toLatin1().data()); |
750 | KeyOz | 146 | o_Input->send_Data(ps_Data, pi_ID); |
147 | } |
||
148 | |||
149 | // MK-Eingangsdaten verarbeiten |
||
150 | void wgt_Connection::parse_MK_Data(QString t_Data) |
||
151 | { |
||
152 | unsigned char OutData[200]; |
||
153 | char *InData = t_Data.toLatin1().data(); |
||
154 | |||
155 | if (HandlerMK::Decode_64(InData, t_Data.length(), OutData) != 0) |
||
156 | { |
||
157 | |||
158 | switch(InData[2]) |
||
159 | { |
||
160 | case 'A' : // Analog-Labels |
||
161 | { |
||
162 | o_Input->stop_Resend(DATA_READ_LABEL); |
||
163 | |||
164 | s_MK_DebugLabels MK_DebugLabels; |
||
165 | |||
166 | MK_DebugLabels.Position = OutData[0]; |
||
167 | |||
168 | if (MK_DebugLabels.Position < 32) |
||
169 | { |
||
170 | MK_DebugLabels.Text = HandlerMK::Data2QString(OutData,1,17).trimmed(); |
||
171 | if (MK_DebugLabels.Text == "") |
||
172 | { |
||
173 | MK_DebugLabels.Text = "Debug-" + QString("%1").arg(MK_DebugLabels.Position); |
||
174 | } |
||
175 | |||
176 | if (((MK_DebugLabels.Position + 1) < 32))// && (get_Analoglabels == true)) |
||
177 | { |
||
178 | c_Data[0] = MK_DebugLabels.Position + 1; |
||
179 | o_Input->send_Data(HandlerMK::make_Frame('a', ADDRESS_ALL, c_Data, 1).toLatin1().data(), DATA_READ_LABEL); |
||
180 | } |
||
181 | |||
182 | emit(sig_MK_DebugLabels(MK_DebugLabels)); |
||
183 | } |
||
184 | } |
||
185 | break; |
||
186 | case 'D' : // Debug-Daten |
||
187 | { |
||
188 | s_MK_Debug MK_Debug; |
||
189 | |||
190 | memcpy((unsigned char *)&MK_Debug, (unsigned char *)&OutData, sizeof(MK_Debug)); |
||
191 | |||
192 | emit(sig_MK_Debug(MK_Debug)); |
||
193 | emit(sig_RawData(t_Data)); |
||
194 | } |
||
195 | break; |
||
196 | case 'N' : // MotorMixer lesen |
||
197 | { |
||
198 | o_Input->stop_Resend(DATA_READ_MIXER); |
||
199 | |||
200 | s_MK_Mixer MK_Mixer; |
||
201 | |||
202 | memcpy((unsigned char *)&MK_Mixer, (unsigned char *)&OutData, sizeof(MK_Mixer)); |
||
203 | |||
204 | emit (sig_MK_ReadMotorMixer(MK_Mixer)); |
||
205 | } |
||
206 | break; |
||
207 | case 'M' : // MotorMixer geschrieben |
||
208 | { |
||
209 | o_Input->stop_Resend(DATA_WRITE_MIXER); |
||
210 | |||
211 | emit (sig_MK_WriteMotorMixer(OutData[0])); |
||
212 | } |
||
213 | break; |
||
214 | |||
215 | case 'O' : // Navi-OSD-Data |
||
216 | { |
||
217 | if (InData[1] - 'a' == ADDRESS_NC) |
||
218 | { |
||
219 | s_MK_NaviData MK_NaviData; |
||
220 | |||
221 | memcpy((unsigned char *)&MK_NaviData, (unsigned char *)&OutData, sizeof(MK_NaviData)); |
||
222 | if (MK_NaviData.Version == MK_VERSION_NAVI) |
||
223 | { |
||
224 | emit(sig_MK_NaviData(MK_NaviData)); |
||
225 | emit(sig_RawData(t_Data)); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | break; |
||
230 | |||
231 | case 'P' : // RC-Kanäle |
||
232 | { |
||
233 | s_MK_PPM_Data PPM_in; |
||
234 | |||
235 | memcpy((unsigned char *)&PPM_in, (unsigned char *)&OutData, sizeof(PPM_in)); |
||
236 | |||
237 | // qDebug() << PPM_in[0] << ", " << PPM_in[1] << ", " << PPM_in[2] << ", " << PPM_in[3]; |
||
238 | |||
239 | emit(sig_MK_PPMData(PPM_in)); |
||
240 | } |
||
241 | break; |
||
242 | |||
243 | case 'Q' : // Settings lesen |
||
244 | { |
||
245 | o_Input->stop_Resend(DATA_READ_SETTINGS); |
||
246 | |||
247 | s_MK_Settings MK_Set; |
||
248 | |||
249 | memcpy((unsigned char *)&MK_Set, (unsigned char *)&OutData, sizeof(MK_Set)); |
||
250 | |||
251 | emit (sig_MK_ReadSettings(MK_Set)); |
||
252 | } |
||
253 | break; |
||
254 | case 'S' : // Settings geschrieben |
||
255 | { |
||
256 | o_Input->stop_Resend(DATA_WRITE_SETTINGS); |
||
257 | |||
258 | emit (sig_MK_WriteSettings(OutData[0])); |
||
259 | } |
||
260 | break; |
||
261 | |||
262 | case 'V' : // Versions-Info |
||
263 | { |
||
264 | o_Input->stop_Resend(DATA_VERSION); |
||
265 | VersionInfo = HandlerMK::parse_Version(OutData, InData[1] - 'a'); |
||
266 | |||
267 | if (VersionInfo.ID == ADDRESS_FC) |
||
268 | { |
||
269 | rb_FC->setChecked(true); |
||
270 | } |
||
271 | if (VersionInfo.ID == ADDRESS_NC) |
||
272 | { |
||
273 | rb_NC->setChecked(true); |
||
274 | } |
||
275 | if (VersionInfo.ID == ADDRESS_MK3MAG) |
||
276 | { |
||
277 | rb_MK3MAG->setChecked(true); |
||
278 | } |
||
279 | |||
280 | slot_sb_Intervall(sb_Intervall->value()); |
||
281 | |||
282 | emit sig_MK_Version(VersionInfo); |
||
283 | } |
||
284 | break; |
||
285 | case 'W' : // WayPoints |
||
286 | { |
||
287 | o_Input->stop_Resend(DATA_WRITE_WAYPOINT); |
||
288 | |||
289 | emit(sig_MK_WayPoint(OutData[0])); |
||
290 | } |
||
291 | break; |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | // IP-Daten verarbeiten.. |
||
297 | void wgt_Connection::parse_IP_Data(QString t_Data) |
||
298 | { |
||
299 | QStringList Data; |
||
300 | Data = t_Data.split(":"); |
||
301 | |||
302 | if (Data.count() > 1) |
||
303 | { |
||
304 | int CMD = Data[2].toInt(); |
||
305 | |||
306 | switch(CMD) |
||
307 | { |
||
308 | case 101 : |
||
309 | { |
||
310 | o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 101, gs_Client)); |
||
311 | } |
||
312 | break; |
||
313 | case 502 : |
||
314 | { |
||
315 | switch (Data[3].toInt()) |
||
316 | { |
||
317 | case 105 : |
||
318 | { |
||
319 | QString s_MD5PW; |
||
320 | QByteArray a_MD5PW; |
||
321 | |||
322 | a_MD5PW = QCryptographicHash::hash(le_Password->text().toAscii(),QCryptographicHash::Md5); |
||
323 | |||
324 | s_MD5PW = QString(a_MD5PW.toHex().data()); |
||
325 | |||
326 | o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 105, s_MD5PW)); |
||
327 | } |
||
328 | break; |
||
329 | case 106 : |
||
330 | { |
||
331 | o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 106, gs_Fields)); |
||
332 | } |
||
333 | break; |
||
334 | } |
||
335 | } |
||
336 | break; |
||
337 | case 505 : |
||
338 | { |
||
339 | if (Data[3] == "OK") |
||
340 | { |
||
341 | } |
||
342 | else |
||
343 | { |
||
344 | QMessageBox::warning(this, gs_Client, trUtf8("Authentifizierung fehlgeschlagen. <br />Daten senden zum Mikrokopter nicht möglich."), QMessageBox::Ok); |
||
345 | } |
||
346 | } |
||
347 | break; |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | // Datenintervall geändert. |
||
353 | void wgt_Connection::slot_sb_Intervall(int t_Intervall) |
||
354 | { |
||
355 | if (t_Intervall == 0) |
||
356 | { |
||
357 | c_Data[0] = 0; |
||
358 | } |
||
359 | else |
||
360 | { |
||
361 | c_Data[0] = t_Intervall / 10; |
||
362 | } |
||
363 | |||
364 | // if (wg_Interval->isVisible()) |
||
365 | { |
||
366 | if (gi_Interval[0]) |
||
367 | { |
||
368 | o_Input->send_Data(HandlerMK::make_Frame('d', ADDRESS_ALL, c_Data, 1).toLatin1().data()); |
||
369 | } |
||
370 | |||
371 | if (gi_Interval[1]) |
||
372 | { |
||
373 | o_Input->send_Data(HandlerMK::make_Frame('o', ADDRESS_ALL, c_Data, 1).toLatin1().data()); |
||
374 | } |
||
375 | } |
||
376 | } |
||
377 | |||
378 | void wgt_Connection::slot_send_Data(QString ps_Data, int pi_ID) |
||
379 | { |
||
380 | send_Data(ps_Data, pi_ID); |
||
381 | } |
||
382 | |||
383 | void wgt_Connection::slot_TimeOut_AboTimer() |
||
384 | { |
||
385 | // qDebug("Timer"); |
||
386 | slot_sb_Intervall(sb_Intervall->value()); |
||
387 | } |
||
388 | |||
389 | void wgt_Connection::slot_btn_Connect() |
||
390 | { |
||
391 | if (!o_Input->IsOpen()) |
||
392 | { |
||
393 | if (cb_Server->findText(cb_Server->currentText()) == -1) |
||
394 | { |
||
395 | cb_Server->addItem(cb_Server->currentText()); |
||
396 | cb_Server->setCurrentIndex(cb_Server->findText(cb_Server->currentText())); |
||
397 | } |
||
398 | |||
399 | cb_Server->setEnabled(false); |
||
400 | le_Password->setEnabled(false); |
||
401 | |||
402 | if (cb_Server->currentText().startsWith('/')) |
||
403 | { |
||
404 | o_Input = new Input_TTY(); |
||
405 | o_Input->Init(); |
||
406 | |||
407 | set_Input s_Input; |
||
408 | s_Input.Main = cb_Server->currentText(); |
||
409 | |||
410 | if (o_Input->Open(s_Input) == true) |
||
411 | { |
||
412 | emit sig_Status(true); |
||
413 | btn_Connect->setChecked(true); |
||
414 | o_AboTimer->start(); |
||
415 | |||
416 | connect(o_Input, SIGNAL(sig_NewData(QString)), this, SLOT(slot_Input_Data(QString))); |
||
417 | |||
418 | o_Input->send_Data(HandlerMK::make_Frame('v', 0, c_Data, 0).toLatin1().data(), DATA_VERSION); |
||
419 | } |
||
420 | else |
||
421 | { |
||
422 | cb_Server->setEnabled(true); |
||
423 | le_Password->setEnabled(true); |
||
424 | } |
||
425 | |||
426 | } |
||
427 | else |
||
428 | { |
||
429 | o_Input = new Input_TCP(); |
||
430 | o_Input->Init(); |
||
431 | |||
432 | set_Input s_Input; |
||
433 | |||
434 | QStringList Server = cb_Server->currentText().split(":"); |
||
435 | |||
436 | s_Input.Main = Server[0]; |
||
437 | s_Input.Sub = Server[1]; |
||
438 | |||
439 | if (o_Input->Open(s_Input) == true) |
||
440 | { |
||
441 | connect(o_Input, SIGNAL(sig_Disconnected(int)), this, SLOT(slot_Input_Disconnected(int))); |
||
442 | connect(o_Input, SIGNAL(sig_Connected()), this, SLOT(slot_Input_Connected())); |
||
443 | } |
||
444 | } |
||
445 | } |
||
446 | else |
||
447 | { |
||
448 | cb_Server->setEnabled(true); |
||
449 | le_Password->setEnabled(true); |
||
450 | |||
451 | emit sig_Status(false); |
||
452 | btn_Connect->setChecked(false); |
||
453 | o_AboTimer->stop(); |
||
454 | |||
455 | o_Input->Close(); |
||
456 | disconnect(o_Input, SIGNAL(sig_NewData(QString)), 0, 0); |
||
457 | if (o_Input->Mode() == TCP) |
||
458 | { |
||
459 | disconnect(o_Input, SIGNAL(sig_Disconnected(int)), 0, 0); |
||
460 | disconnect(o_Input, SIGNAL(sig_Connected()), 0, 0); |
||
461 | } |
||
462 | } |
||
463 | } |
||
464 | |||
465 | // Neue Daten empfangen. |
||
466 | void wgt_Connection::slot_Input_Data(QString t_Data) |
||
467 | { |
||
468 | if ((t_Data[0] == '#')) |
||
469 | { |
||
470 | 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())))) |
||
471 | { |
||
472 | parse_MK_Data(t_Data); |
||
473 | } |
||
474 | else |
||
475 | { |
||
476 | // qDebug(QString("CRC-Error - " + t_Data).toLatin1().data()); |
||
477 | } |
||
478 | } |
||
479 | else if ((o_Input->Mode() == TCP) && (t_Data[0] == '$')) |
||
480 | { |
||
481 | parse_IP_Data(t_Data); |
||
482 | } |
||
483 | } |
||
484 | |||
485 | // Neue Serververbindung. |
||
486 | void wgt_Connection::slot_Input_Connected() |
||
487 | { |
||
488 | connect(o_Input, SIGNAL(sig_NewData(QString)), this, SLOT(slot_Input_Data(QString))); |
||
489 | |||
490 | o_Input->send_Data(HandlerMK::make_Frame('v', 0, c_Data, 0).toLatin1().data(), DATA_VERSION); |
||
491 | emit sig_Status(true); |
||
492 | btn_Connect->setChecked(true); |
||
493 | o_AboTimer->start(); |
||
494 | } |
||
495 | |||
496 | // Serververbindung beendet |
||
497 | void wgt_Connection::slot_Input_Disconnected(int Error) |
||
498 | { |
||
499 | cb_Server->setEnabled(true); |
||
500 | le_Password->setEnabled(true); |
||
501 | |||
502 | disconnect(o_Input, SIGNAL(sig_NewData(QString)), 0, 0); |
||
503 | disconnect(o_Input, SIGNAL(sig_Disconnected(int)), 0, 0); |
||
504 | disconnect(o_Input, SIGNAL(sig_Connected()), 0, 0); |
||
505 | |||
506 | emit sig_Status(false); |
||
507 | btn_Connect->setChecked(false); |
||
508 | o_AboTimer->stop(); |
||
509 | |||
510 | switch (Error) |
||
511 | { |
||
512 | case REMOTECLOSED : |
||
513 | { |
||
514 | // lb_Status->setText(tr("Verbindung vom Server beendet.")); |
||
515 | QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Verbindung wurde vom Server beendet."), QMessageBox::Ok); |
||
516 | } |
||
517 | break; |
||
518 | case REFUSED : |
||
519 | { |
||
520 | // lb_Status->setText(tr("Server nicht gefunden.")); |
||
521 | QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Kann nicht zum Server verbinden."), QMessageBox::Ok); |
||
522 | } |
||
523 | break; |
||
524 | case 3 : |
||
525 | { |
||
526 | // lb_Status->setText(tr("Serververbindung getrennt. Logindaten falsch.")); |
||
527 | QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Loginname oder Password falsch."), QMessageBox::Ok); |
||
528 | } |
||
529 | break; |
||
530 | default : |
||
531 | { |
||
532 | // lb_Status->setText(tr("Getrennt vom QMK-Datenserver.")); |
||
533 | } |
||
534 | break; |
||
535 | } |
||
536 | |||
537 | } |
||
538 | |||
539 | wgt_Connection::~wgt_Connection() |
||
540 | { |
||
541 | write_Settings(); |
||
542 | } |