@@ -17,15 +17,17 @@ AdHocConnectionImpl::AdHocConnectionImpl() {
17
17
::memset (&mavlink_intermediate_status_, 0 , sizeof (mavlink_status_t ));
18
18
::memset (&mavlink_status_, 0 , sizeof (mavlink_status_t ));
19
19
}
20
+
20
21
std::string AdHocConnectionImpl::getName () { return name; }
21
22
22
23
AdHocConnectionImpl::~AdHocConnectionImpl () {
23
24
con_.reset ();
24
25
close ();
25
26
}
26
27
28
+ template <typename PortType>
27
29
std::shared_ptr<AdHocConnection> AdHocConnectionImpl::createConnection (const std::string &nodeName,
28
- std::shared_ptr<Port > port) {
30
+ std::shared_ptr<PortType > port) {
29
31
// std::shared_ptr<MavLinkCom> owner, const std::string& nodeName
30
32
std::shared_ptr<AdHocConnection> con = std::make_shared<AdHocConnection>();
31
33
con->startListening (nodeName, port);
@@ -35,10 +37,8 @@ std::shared_ptr<AdHocConnection> AdHocConnectionImpl::createConnection(const std
35
37
std::shared_ptr<AdHocConnection> AdHocConnectionImpl::connectLocalUdp (const std::string &nodeName,
36
38
std::string localAddr, int localPort) {
37
39
std::shared_ptr<UdpClientPort> socket = std::make_shared<UdpClientPort>();
38
-
39
40
socket->connect (localAddr, localPort, " " , 0 );
40
-
41
- return createConnection (nodeName, socket);
41
+ return createConnection<UdpClientPort>(nodeName, socket);
42
42
}
43
43
44
44
std::shared_ptr<AdHocConnection> AdHocConnectionImpl::connectRemoteUdp (const std::string &nodeName,
@@ -51,10 +51,8 @@ std::shared_ptr<AdHocConnection> AdHocConnectionImpl::connectRemoteUdp(const std
51
51
}
52
52
53
53
std::shared_ptr<UdpClientPort> socket = std::make_shared<UdpClientPort>();
54
-
55
54
socket->connect (local, 0 , remoteAddr, remotePort);
56
-
57
- return createConnection (nodeName, socket);
55
+ return createConnection<UdpClientPort>(nodeName, socket);
58
56
}
59
57
60
58
std::shared_ptr<AdHocConnection> AdHocConnectionImpl::connectTcp (const std::string &nodeName, std::string localAddr,
@@ -66,17 +64,15 @@ std::shared_ptr<AdHocConnection> AdHocConnectionImpl::connectTcp(const std::stri
66
64
}
67
65
68
66
std::shared_ptr<TcpClientPort> socket = std::make_shared<TcpClientPort>();
69
-
70
67
socket->connect (local, 0 , remoteIpAddr, remotePort);
71
-
72
- return createConnection (nodeName, socket);
68
+ return createConnection<TcpClientPort>(nodeName, socket);
73
69
}
74
70
75
71
std::shared_ptr<AdHocConnection> AdHocConnectionImpl::connectSerial (const std::string &nodeName, std::string name,
76
72
int baudRate, const std::string initString) {
77
73
std::shared_ptr<SerialPort> serial = std::make_shared<SerialPort>();
78
-
79
74
int hr = serial->connect (name.c_str (), baudRate);
75
+
80
76
if (hr != 0 )
81
77
throw std::runtime_error (Utils::stringf (" Could not open the serial port %s, error=%d" , name.c_str (), hr));
82
78
@@ -85,7 +81,7 @@ std::shared_ptr<AdHocConnection> AdHocConnectionImpl::connectSerial(const std::s
85
81
serial->write (reinterpret_cast <const uint8_t *>(initString.c_str ()), static_cast <int >(initString.size ()));
86
82
}
87
83
88
- return createConnection (nodeName, serial);
84
+ return createConnection<SerialPort> (nodeName, serial);
89
85
}
90
86
91
87
void AdHocConnectionImpl::startListening (std::shared_ptr<AdHocConnection> parent, const std::string &nodeName,
@@ -121,6 +117,7 @@ void AdHocConnectionImpl::close() {
121
117
bool AdHocConnectionImpl::isOpen () { return !closed; }
122
118
123
119
int AdHocConnectionImpl::getTargetComponentId () { return this ->other_component_id ; }
120
+
124
121
int AdHocConnectionImpl::getTargetSystemId () { return this ->other_system_id ; }
125
122
126
123
void AdHocConnectionImpl::sendMessage (const std::vector<uint8_t > &msg) {
@@ -143,6 +140,7 @@ int AdHocConnectionImpl::subscribe(AdHocMessageHandler handler) {
143
140
snapshot_stale = true ;
144
141
return entry.id ;
145
142
}
143
+
146
144
void AdHocConnectionImpl::unsubscribe (int id) {
147
145
std::lock_guard<std::mutex> guard (listener_mutex);
148
146
for (auto ptr = listeners.begin (), end = listeners.end (); ptr != end; ptr++) {
@@ -162,6 +160,7 @@ void AdHocConnectionImpl::readPackets() {
162
160
uint8_t *buffer = new uint8_t [MAXBUFFER];
163
161
int channel = 0 ;
164
162
int hr = 0 ;
163
+
165
164
while (hr == 0 && con_ != nullptr && !closed) {
166
165
int read = 0 ;
167
166
if (safePort->isClosed ()) {
0 commit comments