Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
801 - 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
                    emit (sig_MK_PPMData(PPM_in));
237
                }
238
            break;
239
 
240
            case 'Q' : // Settings lesen
241
                {
242
                    o_Input->stop_Resend(DATA_READ_SETTINGS);
243
 
244
                    s_MK_Settings MK_Set;
245
 
246
                    memcpy((unsigned char *)&MK_Set, (unsigned char *)&OutData, sizeof(MK_Set));
247
 
248
                    emit (sig_MK_ReadSettings(MK_Set));
249
                }
250
            break;
251
            case 'S' : // Settings geschrieben
252
                {
253
                    o_Input->stop_Resend(DATA_WRITE_SETTINGS);
254
 
255
                    emit (sig_MK_WriteSettings(OutData[0]));
256
                }
257
            break;
258
 
259
            case 'V' : // Versions-Info
260
                {
261
                    o_Input->stop_Resend(DATA_VERSION);
262
                    VersionInfo = HandlerMK::parse_Version(OutData, InData[1] - 'a');
263
 
264
                    if (VersionInfo.ID == ADDRESS_FC)
265
                    {
266
                        rb_FC->setChecked(true);
267
                    }
268
                    if (VersionInfo.ID == ADDRESS_NC)
269
                    {
270
                        rb_NC->setChecked(true);
271
                    }
272
                    if (VersionInfo.ID == ADDRESS_MK3MAG)
273
                    {
274
                        rb_MK3MAG->setChecked(true);
275
                    }
276
 
277
                    slot_sb_Intervall(sb_Intervall->value());
278
 
279
                    emit sig_MK_Version(VersionInfo);
280
                }
281
            break;
282
            case 'W' : // WayPoints
283
                {
284
                    o_Input->stop_Resend(DATA_WRITE_WAYPOINT);
285
 
286
                    emit(sig_MK_WayPoint(OutData[0]));
287
                }
288
            break;
289
        }
290
    }
291
}
292
 
293
// IP-Daten verarbeiten..
294
void wgt_Connection::parse_IP_Data(QString t_Data)
295
{
296
    QStringList Data;
297
    Data = t_Data.split(":");
298
 
299
    if (Data.count() > 1)
300
    {
301
        int CMD = Data[2].toInt();
302
 
303
        switch(CMD)
304
        {
305
            case 101 :
306
            {
307
                o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 101, gs_Client));
308
            }
309
            break;
310
            case 502 :
311
            {
312
                switch (Data[3].toInt())
313
                {
314
                    case 105 :
315
                    {
316
                        QString s_MD5PW;
317
                        QByteArray a_MD5PW;
318
 
319
                        a_MD5PW = QCryptographicHash::hash(le_Password->text().toAscii(),QCryptographicHash::Md5);
320
 
321
                        s_MD5PW = QString(a_MD5PW.toHex().data());
322
 
323
                        o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 105, s_MD5PW));
324
                    }
325
                    break;
326
                    case 106 :
327
                    {
328
                        o_Input->send_Data(HandlerIP::make_Frame(gi_ID, 106, gs_Fields));
329
                    }
330
                    break;
331
                }
332
            }
333
            break;
334
            case 505 :
335
            {
336
                if (Data[3] == "OK")
337
                {
338
                }
339
                else
340
                {
341
                    QMessageBox::warning(this, gs_Client, trUtf8("Authentifizierung fehlgeschlagen. <br />Daten senden zum Mikrokopter nicht möglich."), QMessageBox::Ok);
342
                }
343
            }
344
            break;
345
        }
346
    }
347
}
348
 
349
// Datenintervall geändert.
350
void wgt_Connection::slot_sb_Intervall(int t_Intervall)
351
{
352
    if (t_Intervall == 0)
353
    {
354
        c_Data[0] = 0;
355
    }
356
    else
357
    {
358
        c_Data[0] = t_Intervall / 10;
359
    }
360
 
361
//    if (wg_Interval->isVisible())
362
    {
363
        if (gi_Interval[0])
364
        {
365
           o_Input->send_Data(HandlerMK::make_Frame('d', ADDRESS_ALL, c_Data, 1).toLatin1().data());
366
        }
367
 
368
        if (gi_Interval[1])
369
        {
370
           o_Input->send_Data(HandlerMK::make_Frame('o', ADDRESS_ALL, c_Data, 1).toLatin1().data());
371
        }
372
    }
373
}
374
 
375
void wgt_Connection::slot_TimeOut_AboTimer()
376
{
377
    qDebug("Timer");
378
    slot_sb_Intervall(sb_Intervall->value());
379
}
380
 
381
void wgt_Connection::slot_btn_Connect()
382
{
383
    if (!o_Input->IsOpen())
384
    {
385
        if (cb_Server->findText(cb_Server->currentText()) == -1)
386
        {
387
            cb_Server->addItem(cb_Server->currentText());
388
            cb_Server->setCurrentIndex(cb_Server->findText(cb_Server->currentText()));
389
        }
390
 
391
        cb_Server->setEnabled(false);
392
        le_Password->setEnabled(false);
393
 
394
        if (cb_Server->currentText().startsWith('/'))
395
        {
396
            o_Input = new Input_TTY();
397
            o_Input->Init();
398
 
399
            set_Input s_Input;
400
            s_Input.Main = cb_Server->currentText();
401
 
402
            if (o_Input->Open(s_Input) == true)
403
            {
404
                emit sig_Status(true);
405
                btn_Connect->setChecked(true);
406
                o_AboTimer->start();
407
 
408
                connect(o_Input, SIGNAL(sig_NewData(QString)), this, SLOT(slot_Input_Data(QString)));
409
 
410
                o_Input->send_Data(HandlerMK::make_Frame('v', 0, c_Data, 0).toLatin1().data(), DATA_VERSION);
411
            }
412
            else
413
            {
414
                cb_Server->setEnabled(true);
415
                le_Password->setEnabled(true);
416
            }
417
 
418
        }
419
        else
420
        {
421
            o_Input = new Input_TCP();
422
            o_Input->Init();
423
 
424
            set_Input s_Input;
425
 
426
            QStringList Server = cb_Server->currentText().split(":");
427
 
428
            s_Input.Main = Server[0];
429
            s_Input.Sub  = Server[1];
430
 
431
            if (o_Input->Open(s_Input) == true)
432
            {
433
                connect(o_Input, SIGNAL(sig_Disconnected(int)), this, SLOT(slot_Input_Disconnected(int)));
434
                connect(o_Input, SIGNAL(sig_Connected()), this, SLOT(slot_Input_Connected()));
435
            }
436
        }
437
    }
438
    else
439
    {
440
        cb_Server->setEnabled(true);
441
        le_Password->setEnabled(true);
442
 
443
        emit sig_Status(false);
444
        btn_Connect->setChecked(false);
445
        o_AboTimer->stop();
446
 
447
        o_Input->Close();
448
        disconnect(o_Input, SIGNAL(sig_NewData(QString)), 0, 0);
449
        if (o_Input->Mode() == TCP)
450
        {
451
            disconnect(o_Input, SIGNAL(sig_Disconnected(int)), 0, 0);
452
            disconnect(o_Input, SIGNAL(sig_Connected()), 0, 0);
453
        }
454
    }
455
}
456
 
457
// Neue Daten empfangen.
458
void wgt_Connection::slot_Input_Data(QString t_Data)
459
{
460
    if ((t_Data[0] == '#'))
461
    {
462
        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()))))
463
        {
464
            parse_MK_Data(t_Data);
465
        }
466
        else
467
        {
468
//            qDebug(QString("CRC-Error - " + t_Data).toLatin1().data());
469
        }
470
    }
471
    else if ((o_Input->Mode() == TCP) && (t_Data[0] == '$'))
472
    {
473
        parse_IP_Data(t_Data);
474
    }
475
}
476
 
477
// Neue Serververbindung.
478
void wgt_Connection::slot_Input_Connected()
479
{
480
    connect(o_Input, SIGNAL(sig_NewData(QString)), this, SLOT(slot_Input_Data(QString)));
481
 
482
    o_Input->send_Data(HandlerMK::make_Frame('v', 0, c_Data, 0).toLatin1().data(), DATA_VERSION);
483
    emit sig_Status(true);
484
    btn_Connect->setChecked(true);
485
    o_AboTimer->start();
486
}
487
 
488
// Serververbindung beendet
489
void wgt_Connection::slot_Input_Disconnected(int Error)
490
{
491
    cb_Server->setEnabled(true);
492
    le_Password->setEnabled(true);
493
 
494
    disconnect(o_Input, SIGNAL(sig_NewData(QString)), 0, 0);
495
    disconnect(o_Input, SIGNAL(sig_Disconnected(int)), 0, 0);
496
    disconnect(o_Input, SIGNAL(sig_Connected()), 0, 0);
497
 
498
    emit sig_Status(false);
499
    btn_Connect->setChecked(false);
500
    o_AboTimer->stop();
501
 
502
    switch (Error)
503
    {
504
        case REMOTECLOSED :
505
        {
506
//            lb_Status->setText(tr("Verbindung vom Server beendet."));
507
            QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Verbindung wurde vom Server beendet."), QMessageBox::Ok);
508
        }
509
        break;
510
        case REFUSED :
511
        {
512
//            lb_Status->setText(tr("Server nicht gefunden."));
513
            QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Kann nicht zum Server verbinden."), QMessageBox::Ok);
514
        }
515
        break;
516
        case 3 :
517
        {
518
//            lb_Status->setText(tr("Serververbindung getrennt. Logindaten falsch."));
519
            QMessageBox::warning(this, gs_Client,tr("QMK-Datenserver: Loginname oder Password falsch."), QMessageBox::Ok);
520
        }
521
        break;
522
        default :
523
        {
524
//            lb_Status->setText(tr("Getrennt vom QMK-Datenserver."));
525
        }
526
        break;
527
    }
528
 
529
}
530
 
531
wgt_Connection::~wgt_Connection()
532
{
533
    write_Settings();
534
}