-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinkManager1.cc
627 lines (576 loc) · 18.6 KB
/
LinkManager1.cc
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*===================================================================
APM_PLANNER Open Source Ground Control Station
(c) 2014 APM_PLANNER PROJECT <http://www.diydrones.com>
This file is part of the APM_PLANNER project
APM_PLANNER is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
APM_PLANNER is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with APM_PLANNER. If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
/**
* @file
* @brief LinkManager
*
* @author Michael Carpenter <[email protected]>
* @author QGROUNDCONTROL PROJECT - This code has GPLv3+ snippets from QGROUNDCONTROL, (c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
*/
#include "LinkManager1.h"
#include "PxQuadMAV1.h"
#include "SlugsMAV1.h"
#include "ArduPilotMegaMAV1.h"
#include "UASManager1.h"
#include "UDPLink1.h"
#include "TCPLink1.h"
#include <QSettings>
#include "UASObject.h"
LinkManager::LinkManager(QObject *parent) :
QObject(parent)
{
m_mavlinkLoggingEnabled = true;
m_mavlinkDecoder = new MAVLinkDecoder(this);
m_mavlinkProtocol = new MAVLinkProtocol();
m_mavlinkProtocol->setConnectionManager(this);
connect(m_mavlinkProtocol,SIGNAL(messageReceived(LinkInterface*,mavlink_message_t)),m_mavlinkDecoder,SLOT(receiveMessage(LinkInterface*,mavlink_message_t)));
connect(m_mavlinkProtocol,SIGNAL(protocolStatusMessage(QString,QString)),this,SLOT(protocolStatusMessageRec(QString,QString)));
loadSettings();
//Check to see if we have a single UDP connection, since they are the defaults
bool foundudp = false;
for (QMap<int,LinkInterface*>::const_iterator i= m_connectionMap.constBegin();i!=m_connectionMap.constEnd();i++)
{
if (i.value()->getLinkType() == LinkInterface::UDP_LINK)
{
foundudp = true;
}
}
if (!foundudp)
{
addUdpConnection(QHostAddress::Any,14550);
}
}
void LinkManager::stopLogging()
{
if (!m_mavlinkLoggingEnabled)
{
return;
}
m_mavlinkProtocol->stopLogging();
}
LinkManager::~LinkManager()
{
m_mavlinkProtocol->setConnectionManager(NULL);
delete m_mavlinkProtocol;
m_mavlinkProtocol = NULL;
saveSettings();
}
void LinkManager::loadSettings()
{
QSettings settings;
settings.beginGroup("LINKMANAGER");
m_mavlinkLoggingEnabled = settings.value("LOGGING",true).toBool();
int linkssize = settings.beginReadArray("LINKS");
for (int i=0;i<linkssize;i++)
{
settings.setArrayIndex(i);
QString type = settings.value("type").toString();
if (type == "UDP_LINK")
{
int port = settings.value("port").toInt();
int linkid = addUdpConnection(QHostAddress::Any,port);
UDPLink *iface = qobject_cast<UDPLink*>(m_connectionMap.value(linkid));
int hostcount = settings.beginReadArray("HOSTS");
for (int j=0;j<hostcount;++j)
{
settings.setArrayIndex(j);
QString host = settings.value("host").toString();
int port = settings.value("port").toInt();
iface->addHost(tr("%1:%2").arg(host, port));
}
settings.endArray();
}
else if (type == "TCP_LINK")
{
QString host = settings.value("host").toString();
int port = settings.value("port").toInt();
bool asServer = settings.value("asServer").toBool();
addTcpConnection(QHostAddress(host),port,asServer);
}
}
int portsize = settings.beginReadArray("PORTBAUDPAIRS");
for (int i=0;i<portsize;i++)
{
settings.setArrayIndex(i);
m_portToBaudMap[settings.value("port").toString()] = settings.value("baud").toInt();
}
settings.endArray();
settings.endArray();
}
void LinkManager::saveSettings()
{
QSettings settings;
settings.beginGroup("LINKMANAGER");
settings.setValue("LOGGING",m_mavlinkLoggingEnabled);
settings.beginWriteArray("LINKS");
int index = 0;
for (QMap<int,LinkInterface*>::const_iterator i= m_connectionMap.constBegin();i!=m_connectionMap.constEnd();i++)
{
settings.setArrayIndex(index++);
settings.setValue("linkid",i.value()->getId());
if (i.value()->getLinkType() == LinkInterface::UDP_LINK)
{
UDPLink *link = qobject_cast<UDPLink*>(i.value());
settings.setValue("type","UDP_LINK");
settings.beginWriteArray("HOSTS");
for (int j=0;j<link->getHosts().size();j++)
{
settings.setArrayIndex(j);
settings.setValue("host",link->getHosts().at(j).toString());
settings.setValue("port",link->getPorts().at(j));
}
settings.endArray();
settings.setValue("port",link->getPort());
}
else if (i.value()->getLinkType() == LinkInterface::TCP_LINK)
{
TCPLink *link = qobject_cast<TCPLink*>(i.value());
settings.setValue("type","TCP_LINK");
settings.setValue("host",link->getHostAddress().toString());
settings.setValue("port",link->getPort());
settings.setValue("asServer",link->isServer());
}
}
settings.endArray();
settings.beginWriteArray("PORTBAUDPAIRS");
index = 0;
for (QMap<QString,int>::const_iterator i=m_portToBaudMap.constBegin();i!=m_portToBaudMap.constEnd();i++)
{
settings.setArrayIndex(index++);
settings.setValue("port",i.key());
settings.setValue("baud",i.value());
}
settings.endArray();
settings.endGroup();
settings.sync();
}
void LinkManager::setLogSubDirectory(QString dir)
{
if (!dir.startsWith("/"))
{
m_logSubDir = "/" + dir;
}
else
{
m_logSubDir = dir;
}
if (!m_logSubDir.endsWith("/"))
{
m_logSubDir += "/";
}
QDir logdir(QGC::MAVLinkLogDirectory());
if (!logdir.cd(m_logSubDir.mid(1)))
{
logdir.mkdir(m_logSubDir.mid(1));
}
}
void LinkManager::enableLogging(bool enabled)
{
if (enabled)
{
m_mavlinkLoggingEnabled = enabled;
startLogging();
}
else
{
stopLogging();
m_mavlinkLoggingEnabled = enabled;
}
}
bool LinkManager::loggingEnabled()
{
return m_mavlinkLoggingEnabled;
}
void LinkManager::startLogging()
{
if (!m_mavlinkLoggingEnabled)
{
return;
}
QString logFileName = QGC::MAVLinkLogDirectory() + m_logSubDir + QGC::fileNameAsTime();
QLOG_DEBUG() << "LinkManger::startLogging()" << logFileName;
m_mavlinkProtocol->startLogging(logFileName);
}
LinkInterface::LinkType LinkManager::getLinkType(int linkid)
{
if (!m_connectionMap.contains(linkid))
{
return LinkInterface::UNKNOWN_LINK;
}
return m_connectionMap.value(linkid)->getLinkType();
}
int LinkManager::addUdpConnection(QHostAddress addr,int port)
{
UDPLink* udpLink = new UDPLink(addr,port);
connect(udpLink,SIGNAL(bytesReceived(LinkInterface*,QByteArray)),m_mavlinkProtocol,SLOT(receiveBytes(LinkInterface*,QByteArray)));
connect(udpLink,SIGNAL(connected(LinkInterface*)),this,SLOT(linkConnected(LinkInterface*)));
connect(udpLink,SIGNAL(disconnected(LinkInterface*)),this,SLOT(linkDisonnected(LinkInterface*)));
connect(udpLink,SIGNAL(error(LinkInterface*,QString)),this,SLOT(linkErrorRec(LinkInterface*,QString)));
m_connectionMap.insert(udpLink->getId(),udpLink);
emit newLink(udpLink->getId());
saveSettings();
udpLink->connect();
return udpLink->getId();
}
int LinkManager::addTcpConnection(QHostAddress addr,int port,bool asServer)
{
TCPLink *tcplink = new TCPLink(addr,port,asServer);
connect(tcplink,SIGNAL(bytesReceived(LinkInterface*,QByteArray)),m_mavlinkProtocol,SLOT(receiveBytes(LinkInterface*,QByteArray)));
connect(tcplink,SIGNAL(connected(LinkInterface*)),this,SLOT(linkConnected(LinkInterface*)));
connect(tcplink,SIGNAL(disconnected(LinkInterface*)),this,SLOT(linkDisonnected(LinkInterface*)));
m_connectionMap.insert(tcplink->getId(),tcplink);
emit newLink(tcplink->getId());
saveSettings();
if (asServer)
{
tcplink->connect();
}
return tcplink->getId();
}
void LinkManager::addLink(LinkInterface *link)
{
m_connectionMap.insert(link->getId(),link);
}
void LinkManager::removeLink(LinkInterface*)
{
// This is called with a LINK_ID not an interface. needs mor rework
//This function is not yet supported, it will be once we support multiple MAVs
}
void LinkManager::removeLink(int linkId)
{
if (m_connectionMap.contains(linkId))
{
if (m_connectionMap.value(linkId)->isConnected())
{
m_connectionMap.value(linkId)->disconnect();
}
delete m_connectionMap.value(linkId);
m_connectionMap.remove(linkId);
saveSettings();
}
}
bool LinkManager::connectLink(int index)
{
if (m_connectionMap.contains(index))
{
return m_connectionMap.value(index)->connect();
}
return false;
}
void LinkManager::disconnectLink(int index)
{
if (m_connectionMap.contains(index))
{
m_connectionMap.value(index)->disconnect();
}
}
void LinkManager::modifyTcpConnection(int index,QHostAddress addr,int port,bool asServer)
{
if (!m_connectionMap.contains(index))
{
return;
}
TCPLink *iface = qobject_cast<TCPLink*>(m_connectionMap.value(index));
if (!iface)
{
return;
}
iface->setHostAddress(addr);
iface->setPort(port);
iface->setAsServer(asServer);
emit linkChanged(index);
saveSettings();
}
QString LinkManager::getLinkName(int linkid)
{
if (!m_connectionMap.contains(linkid))
{
return "";
}
return m_connectionMap.value(linkid)->getName();
}
bool LinkManager::getLinkConnected(int linkid)
{
if (!m_connectionMap.contains(linkid))
{
return "";
}
return m_connectionMap.value(linkid)->isConnected();
}
int LinkManager::getUdpLinkPort(int linkid)
{
if (!m_connectionMap.contains(linkid))
{
return 0;
}
UDPLink *iface = qobject_cast<UDPLink*>(m_connectionMap.value(linkid));
if (!iface)
{
return 0;
}
return iface->getPort();
}
int LinkManager::getTcpLinkPort(int linkid)
{
if (!m_connectionMap.contains(linkid))
{
return 0;
}
TCPLink *iface = qobject_cast<TCPLink*>(m_connectionMap.value(linkid));
if (!iface)
{
return 0;
}
return iface->getPort();
}
QHostAddress LinkManager::getTcpLinkHost(int linkid)
{
if (!m_connectionMap.contains(linkid))
{
return QHostAddress::Null;
}
TCPLink *iface = qobject_cast<TCPLink*>(m_connectionMap.value(linkid));
if (!iface)
{
return QHostAddress::Null;
}
return iface->getHostAddress();
}
bool LinkManager::isTcpServer(int linkid)
{
if (!m_connectionMap.contains(linkid))
return false;
TCPLink *iface = qobject_cast<TCPLink*>(m_connectionMap.value(linkid));
if (!iface)
return false;
return iface->isServer();
}
void LinkManager::setUdpLinkPort(int linkid, int port)
{
if (!m_connectionMap.contains(linkid))
{
return;
}
UDPLink *iface = qobject_cast<UDPLink*>(m_connectionMap.value(linkid));
if (!iface)
{
return;
}
iface->setPort(port);
emit linkChanged(linkid);
saveSettings();
}
void LinkManager::addUdpHost(int linkid,QString hostname)
{
if (!m_connectionMap.contains(linkid))
{
return;
}
UDPLink *iface = qobject_cast<UDPLink*>(m_connectionMap.value(linkid));
if (!iface)
{
return;
}
iface->addHost(hostname);
saveSettings();
}
void LinkManager::messageReceived(LinkInterface* ,mavlink_message_t )
{
}
UASInterface* LinkManager::getUas(int id)
{
if (m_uasMap.contains(id))
{
return m_uasMap.value(id);
}
return 0;
}
QList<int> LinkManager::getLinks()
{
QList<int> links;
for (int i=0;i<m_connectionMap.keys().size();i++)
{
links.append(m_connectionMap.value(m_connectionMap.keys().at(i))->getId());
}
return links;
}
UASInterface* LinkManager::createUAS(MAVLinkProtocol* mavlink, LinkInterface* link, int sysid, mavlink_heartbeat_t* heartbeat, QObject* parent)
{
QPointer<QObject> p;
if (parent != NULL)
{
p = parent;
}
else
{
p = mavlink;
}
UASInterface* uas;
switch (heartbeat->autopilot)
{
case MAV_AUTOPILOT_GENERIC:
{
UAS* mav = new UAS(0, sysid);
// Set the system type
mav->setSystemType((int)heartbeat->type);
// Connect this robot to the UAS object
connect(mavlink, SIGNAL(messageReceived(LinkInterface*, mavlink_message_t)), mav, SLOT(receiveMessage(LinkInterface*, mavlink_message_t)));
#ifdef QGC_PROTOBUF_ENABLED
connect(mavlink, SIGNAL(extendedMessageReceived(LinkInterface*, std::tr1::shared_ptr<google::protobuf::Message>)), mav, SLOT(receiveExtendedMessage(LinkInterface*, std::tr1::shared_ptr<google::protobuf::Message>)));
#endif
uas = mav;
}
break;
case MAV_AUTOPILOT_PIXHAWK:
{
PxQuadMAV* mav = new PxQuadMAV(0, sysid);
// Set the system type
mav->setSystemType((int)heartbeat->type);
// Connect this robot to the UAS object
// it is IMPORTANT here to use the right object type,
// else the slot of the parent object is called (and thus the special
// packets never reach their goal)
connect(mavlink, SIGNAL(messageReceived(LinkInterface*, mavlink_message_t)), mav, SLOT(receiveMessage(LinkInterface*, mavlink_message_t)));
#ifdef QGC_PROTOBUF_ENABLED
connect(mavlink, SIGNAL(extendedMessageReceived(LinkInterface*, std::tr1::shared_ptr<google::protobuf::Message>)), mav, SLOT(receiveExtendedMessage(LinkInterface*, std::tr1::shared_ptr<google::protobuf::Message>)));
#endif
uas = mav;
}
break;
case MAV_AUTOPILOT_SLUGS:
{
SlugsMAV* mav = new SlugsMAV(0, sysid);
// Set the system type
mav->setSystemType((int)heartbeat->type);
// Connect this robot to the UAS object
// it is IMPORTANT here to use the right object type,
// else the slot of the parent object is called (and thus the special
// packets never reach their goal)
connect(mavlink, SIGNAL(messageReceived(LinkInterface*, mavlink_message_t)), mav, SLOT(receiveMessage(LinkInterface*, mavlink_message_t)));
uas = mav;
}
break;
case MAV_AUTOPILOT_ARDUPILOTMEGA:
{
ArduPilotMegaMAV* mav = new ArduPilotMegaMAV(0, sysid);
UASObject *obj = new UASObject();
connect(mavlink,SIGNAL(messageReceived(LinkInterface*,mavlink_message_t)),obj,SLOT(messageReceived(LinkInterface*,mavlink_message_t)));
m_uasObjectMap[sysid] = obj;
// Set the system type
mav->setSystemType((int)heartbeat->type);
// Connect this robot to the UAS object
// it is IMPORTANT here to use the right object type,
// else the slot of the parent object is called (and thus the special
// packets never reach their goal)
connect(mavlink, SIGNAL(messageReceived(LinkInterface*, mavlink_message_t)), mav, SLOT(receiveMessage(LinkInterface*, mavlink_message_t)));
uas = mav;
}
break;
#ifdef QGC_USE_SENSESOAR_MESSAGES
case MAV_AUTOPILOT_SENSESOAR:
{
senseSoarMAV* mav = new senseSoarMAV(0,sysid);
mav->setSystemType((int)heartbeat->type);
connect(mavlink, SIGNAL(messageReceived(LinkInterface*, mavlink_message_t)), mav, SLOT(receiveMessage(LinkInterface*, mavlink_message_t)));
uas = mav;
break;
}
#endif
default:
{
UAS* mav = new UAS(0, sysid);
mav->setSystemType((int)heartbeat->type);
// Connect this robot to the UAS object
// it is IMPORTANT here to use the right object type,
// else the slot of the parent object is called (and thus the special
// packets never reach their goal)
connect(mavlink, SIGNAL(messageReceived(LinkInterface*, mavlink_message_t)), mav, SLOT(receiveMessage(LinkInterface*, mavlink_message_t)));
uas = mav;
}
break;
}
m_uasMap.insert(sysid,uas);
// Set the autopilot type
uas->setAutopilotType((int)heartbeat->autopilot);
// Make UAS aware that this link can be used to communicate with the actual robot
uas->addLink(link);
// Now add UAS to "official" list, which makes the whole application aware of it
UASManager::instance()->addUAS(uas);
return uas;
}
UASObject *LinkManager::getUasObject(int uasid)
{
if (m_uasObjectMap.contains(uasid))
{
return m_uasObjectMap.value(uasid);
}
return 0;
}
void LinkManager::protocolStatusMessageRec(QString title,QString text)
{
emit protocolStatusMessage(title,text);
QLOG_DEBUG() << "Protocol Status Message:" << title << text;
}
void LinkManager::linkConnected(LinkInterface* link)
{
emit linkChanged(link->getId());
}
void LinkManager::linkDisonnected(LinkInterface* link)
{
emit linkChanged(link->getId());
}
void LinkManager::linkErrorRec(LinkInterface *link,QString errorstring)
{
emit linkError(link->getId(),errorstring);
}
void LinkManager::linkTimeoutTriggered(LinkInterface *)
{
//Link has had a timeout
//Disabled until it is fixed and more more robust - MLC
//emit linkError(link->getId(),"Connected to link, but unable to receive any mavlink packets, (link is silent). Disconnecting");
//link->disconnect();
}
void LinkManager::disableTimeouts(int index)
{
if (!m_connectionMap.contains(index))
{
return;
}
m_connectionMap.value(index)->disableTimeouts();
}
void LinkManager::enableTimeouts(int index)
{
if (!m_connectionMap.contains(index))
{
return;
}
m_connectionMap.value(index)->enableTimeouts();
}
void LinkManager::disableAllTimeouts()
{
for (QMap<int,LinkInterface*>::const_iterator i = m_connectionMap.constBegin(); i != m_connectionMap.constEnd();i++)
{
i.value()->disableTimeouts();
}
}
void LinkManager::enableAllTimeouts()
{
for (QMap<int,LinkInterface*>::const_iterator i = m_connectionMap.constBegin(); i != m_connectionMap.constEnd();i++)
{
i.value()->disableTimeouts();
}
}