5#include <QtCore/QStringList>
10#ifndef QGC_NO_SERIAL_LINK
20 , _configuration(configuration)
21 , _scheduler(scheduler)
26 const bool connectionChanged = !previous.
sameConnection(_configuration);
27 const bool turnedOn = !previous.
autoConnect && _configuration.autoConnect;
28 const bool turnedOff = previous.
autoConnect && !_configuration.autoConnect;
29 if (connectionChanged || turnedOff) {
36 _keepConnected = _owner == Owner::Saved && !connectionChanged;
40void GPSReceiverConnectionPolicy::_cancelRetries()
43 qCDebug(GPSReceiverConnectionPolicyLog) <<
"Automatic reconnect cancelled";
46 if ((_keepConnected || _retryDeadlineUs) && !_receiver.hasReceiver()) {
47 _receiver.setConnectionError({});
49 _keepConnected =
false;
50 _waitingForPort =
false;
52#ifndef QGC_NO_SERIAL_LINK
53 _waitingPorts.clear();
55 _retryDeadlineUs.reset();
56 _retryBackoff.reset();
63 _startPending =
false;
69 if (_receiver.hasReceiver()) {
70 _receiver.setConnectionError(tr(
"Disconnect the current receiver before connecting another."));
75 if (!_connectConfigured(allowPersistentChanges)) {
78 _owner = Owner::Saved;
86 _receiver.endConnection(
true);
91 if (_owner == Owner::Saved && _configuration.autoConnect && !std::exchange(_keepConnected,
true)) {
92 qCDebug(GPSReceiverConnectionPolicyLog) <<
"Automatic reconnect armed for the saved receiver";
94 _waitingForPort =
false;
95 _retryDeadlineUs.reset();
96 _retryBackoff.reset();
101 if (_owner == Owner::Discovered && _discoveryEnabled()) {
104 _owner = Owner::None;
111 if (_owner == Owner::Saved && _keepConnected) {
113 _waitingForPort =
true;
119 _owner = Owner::None;
124void GPSReceiverConnectionPolicy::_scheduleRetry()
126 const auto delay = _retryBackoff.
next();
127 qCDebug(GPSReceiverConnectionPolicyLog) <<
"Retrying the receiver connection in" << delay.count() <<
"ms";
128 _retryDeadlineUs = _scheduler->
nowUs() +
static_cast<quint64
>(std::chrono::microseconds(delay).count());
133 if (!_configuration.autoConnect || _paused || _receiver.hasReceiver()) {
136 if (std::exchange(_startPending,
false) && _hasSavedReceiver()) {
137 _owner = Owner::Saved;
138 _keepConnected =
true;
140 if (_keepConnected) {
142 }
else if (_discoveryEnabled()) {
147bool GPSReceiverConnectionPolicy::_hasSavedReceiver()
const
151 return !_configuration.
serialDevice.trimmed().isEmpty();
153 return !_configuration.
tcpHost.trimmed().isEmpty() && _configuration.
tcpPort != 0;
161bool GPSReceiverConnectionPolicy::_discoveryEnabled()
const
163#ifdef QGC_NO_SERIAL_LINK
169 _configuration.serialDevice.trimmed().isEmpty();
173void GPSReceiverConnectionPolicy::_updateSaved()
176 const QString
device = _configuration.serialDevice.trimmed();
177 if (!_serialDeviceAvailable(device)) {
178 if (!std::exchange(_waitingForPort,
true)) {
179 const QString label = _serialDeviceLabel(device);
180 if (label.isEmpty()) {
181 _receiver.setConnectionError(tr(
"Waiting for the receiver on USB."));
184 _receiver.setConnectionError(tr(
"Waiting for the receiver on %1.").arg(label));
189 if (std::exchange(_waitingForPort,
false)) {
195 if (!_retryDeadlineUs || _scheduler->nowUs() >= *_retryDeadlineUs) {
200void GPSReceiverConnectionPolicy::_attemptSaved()
202 _retryDeadlineUs.reset();
204 if (!_connectConfigured(
false)) {
209bool GPSReceiverConnectionPolicy::_serialDeviceAvailable([[maybe_unused]]
const QString& device)
const
211#ifdef QGC_NO_SERIAL_LINK
214 return _receiver.serialPortAvailable(device);
218QString GPSReceiverConnectionPolicy::_serialDeviceLabel(
const QString& device)
const
220#ifdef QGC_NO_SERIAL_LINK
223 return _receiver.serialPortLabel(device);
227bool GPSReceiverConnectionPolicy::_connectConfigured(
bool allowPersistentChanges)
233 _receiver.setConnectionError(tr(
"Select a receiver type before connecting."));
236 const auto connection = _configuration.effectiveConnectionType();
238 const QString host = _configuration.tcpHost.trimmed();
239 const uint tcpPort = _configuration.tcpPort;
240 if (host.isEmpty() || tcpPort == 0 || tcpPort > 65535) {
241 _receiver.setConnectionError(tr(
"Enter the receiver's TCP host and port."));
244 return _receiver.connectTcp(host,
static_cast<quint16
>(tcpPort), *type, allowPersistentChanges);
247 const uint udpPort = _configuration.udpPort;
248 if (configuredBase) {
249 _receiver.setConnectionError(
250 tr(
"A configured base needs a serial or TCP connection. UDP only receives data."));
253 if (udpPort == 0 || udpPort > 65535) {
254 _receiver.setConnectionError(tr(
"Enter the UDP port that receives the data."));
257 return _receiver.connectUdp(
static_cast<quint16
>(udpPort), *type);
259#ifndef QGC_NO_SERIAL_LINK
260 const QString
device = _configuration.serialDevice.trimmed();
261 if (!_serialDeviceAvailable(device)) {
262 _receiver.setConnectionError(tr(
"Select an available serial device that is not a bootloader."));
266 const auto baud = _configuration.serialBaudRate;
271 auto claim = _receiver.claimSerialPort(device);
273 _receiver.setConnectionError(tr(
"The selected serial device is already in use."));
276 return _receiver.connectSerial(device, *type, baud, allowPersistentChanges, std::move(claim));
282void GPSReceiverConnectionPolicy::_updateDiscovery()
284#ifndef QGC_NO_SERIAL_LINK
285 const QStringList receivers = _receiver.rtkReceiverPorts();
289 for (
const QString& device : receivers) {
290 if (_receiver.serialPortAllowed(device)) {
291 allowed.append(device);
294 if (_owner == Owner::Discovered) {
295 const bool passedOver = !allowed.isEmpty() && !_receiver.serialPortAllowed(_autoPort);
296 if (_serialDeviceAvailable(_autoPort) && !passedOver) {
297 if (!_retryDeadlineUs || _scheduler->nowUs() >= *_retryDeadlineUs) {
298 _connectDiscovered(_autoPort);
303 _owner = Owner::None;
306 _waitingPorts.removeIf([&receivers](
const QString& device) {
return !receivers.contains(device); });
307 const QStringList& candidates = allowed.isEmpty() ? receivers : allowed;
308 const quint64
nowUs = _scheduler->nowUs();
309 for (
const QString& device : candidates) {
310 if (_waitingPorts.settled(device, nowUs)) {
311 _connectDiscovered(device);
318#ifndef QGC_NO_SERIAL_LINK
319void GPSReceiverConnectionPolicy::_connectDiscovered(
const QString& device)
321 auto claim = _receiver.claimSerialPort(device);
325 _owner = Owner::Discovered;
327 _waitingPorts.clear();
328 _retryDeadlineUs.reset();
332 _receiver.setConnectionError(tr(
"Select a receiver type before connecting."));
334 if (!type || !_receiver.connectSerial(device, *type, 0,
false, std::move(claim))) {
QString gpsReceiverConfigErrorText(GPSReceiverConfigError error)
Translated diagnostic; empty for GPSReceiverConfigError::None.
constexpr bool gpsValidBaudRate(uint32_t baudRate, bool passive)
GPSReceiverSessionOutcome
What the policy does after a receiver session ends; the receiver words the message for it.
@ Unplugged
A discovered receiver was unplugged; discovery connects it again when it returns.
@ None
Nothing reconnects; the receiver's own message for the error applies.
@ AutoRetrying
A discovered receiver failed or was lost; discovery retries it.
@ WaitingForPort
The saved receiver was unplugged; it reconnects when its port returns.
@ Retrying
The saved receiver's connection was lost; a retry is scheduled.
std::optional< GPSType > gpsReceiverTypeForManufacturer(int manufacturer)
The family a persisted manufacturer selects, GPSType::automatic for GPS_AUTOMATIC_MANUFACTURER; empty...
GPSTypes::GPSType GPSType
#define QGC_LOGGING_CATEGORY(name, categoryStr)
constexpr Delay next(Delay retryAfter=Delay::zero())
Returns peek(retryAfter) and counts the attempt.
void disconnectConfigured()
Disconnects at the user's request and pauses automatic connection.
GPSReceiverConnectionPolicy(GPSReceiverConnector &receiver, const GPSReceiverConfiguration &configuration, RuntimeScheduler *scheduler)
GPSReceiverSessionOutcome sessionEnded(bool portRemoved)
void reset()
Forgets connection ownership and pending retries, before another connection replaces it.
void update()
Periodic tick: due automatic connections and USB discovery.
void configurationChanged(const GPSReceiverConfiguration &previous)
The receiver's configuration changed from previous.
bool connectConfigured(bool allowPersistentChanges)
Connects the saved receiver at the user's request and resumes automatic connection.
virtual quint64 nowUs() const =0
QCameraDevice device(const QString &description)
The attached camera of description description; a null device when none is.
The receiver configuration the application's settings binding supplies as values.
RTKSettings::ReceiverRole receiverRole
Only a configured base is written to by QGroundControl.
bool autoConnect
Keep the saved receiver connected, or discover a configured base on USB when no device is saved.
bool sameConnection(const GPSReceiverConfiguration &other) const
RTKSettings::ConnectionType effectiveConnectionType() const
The connection this configuration uses; builds without serial links connect a serial selection over T...