UASParameterCommsMgr.cc 21.3 KB
Newer Older
1 2
#include "UASParameterCommsMgr.h"

tstellanova's avatar
tstellanova committed
3 4
#include <QSettings>

5 6 7
#include "QGCUASParamManager.h"
#include "UASInterface.h"

8 9
#define RC_CAL_CHAN_MAX 8

10
UASParameterCommsMgr::UASParameterCommsMgr(QObject *parent) :
tstellanova's avatar
tstellanova committed
11
    QObject(parent),
12
    mav(NULL),
13
    paramDataModel(NULL),
tstellanova's avatar
tstellanova committed
14 15 16
    transmissionListMode(false),
    transmissionActive(false),
    transmissionTimeout(0),
17 18
    retransmissionTimeout(1000),
    rewriteTimeout(1000),
tstellanova's avatar
tstellanova committed
19
    retransmissionBurstRequestSize(5)
20
{
21 22 23 24 25 26 27


}

UASParameterCommsMgr* UASParameterCommsMgr::initWithUAS(UASInterface* uas)
{
    mav = uas;
28
    paramDataModel = mav->getParamManager()->dataModel();
tstellanova's avatar
tstellanova committed
29 30 31 32 33
    loadParamCommsSettings();

    //Requesting parameters one-by-one from mav
    connect(this, SIGNAL(parameterUpdateRequestedById(int,int)),
            mav, SLOT(requestParameter(int,int)));
34 35

    // Sending params to the UAS
36
    connect(this, SIGNAL(commitPendingParameter(int,QString,QVariant)),
37
            mav, SLOT(setParameter(int,QString,QVariant)));
38

39
    // Received parameter updates from UAS
40 41
    connect(mav, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)),
            this, SLOT(receivedParameterUpdate(int,int,int,int,QString,QVariant)));
42

43
    //connect to retransmissionTimer
44 45
    connect(&retransmissionTimer, SIGNAL(timeout()),
            this, SLOT(retransmissionGuardTick()));
tstellanova's avatar
tstellanova committed
46

47
    return this;
48 49 50
}


tstellanova's avatar
tstellanova committed
51 52 53 54 55

void UASParameterCommsMgr::loadParamCommsSettings()
{
    QSettings settings;
    settings.beginGroup("QGC_MAVLINK_PROTOCOL");
tstellanova's avatar
tstellanova committed
56
    bool ok;
tstellanova's avatar
tstellanova committed
57
    int val = settings.value("PARAMETER_RETRANSMISSION_TIMEOUT", retransmissionTimeout).toInt(&ok);
tstellanova's avatar
tstellanova committed
58 59
    if (ok) {
        retransmissionTimeout = val;
60
        qDebug() << "retransmissionTimeout" << retransmissionTimeout;
tstellanova's avatar
tstellanova committed
61 62
    }
    val = settings.value("PARAMETER_REWRITE_TIMEOUT", rewriteTimeout).toInt(&ok);
tstellanova's avatar
tstellanova committed
63 64
    if (ok) {
        rewriteTimeout = val;
tstellanova's avatar
tstellanova committed
65 66 67 68
    }
    settings.endGroup();
}

69 70


71 72 73 74 75 76 77 78 79 80
/**
 * Send a request to deliver the list of onboard parameters
 * from the MAV.
 */
void UASParameterCommsMgr::requestParameterList()
{
    if (!mav) {
        return;
    }

81 82 83 84 85 86
    if (!transmissionListMode) {
        // Clear transmission state
        receivedParamsList.clear();
        transmissionListSizeKnown.clear();

        transmissionListMode = true;
87 88
        foreach (int key, missingReadPackets.keys()) {
            missingReadPackets.value(key)->clear();
89 90 91 92
        }
        transmissionActive = true;

        setParameterStatusMsg(tr("Requested param list.. waiting"));
93
        listRecvTimeout = QGC::groundTimeMilliseconds() + 10000;
94
        mav->requestParameters();
95
        setRetransmissionGuardEnabled(true);
96 97 98
    }
    else {
        qDebug() << __FILE__ << __LINE__ << "Ignoring requestParameterList because we're receiving params list";
99 100 101 102 103
    }

}


104 105 106 107
/*
 Empty read retransmission list
 Empty write retransmission list
*/
tstellanova's avatar
tstellanova committed
108
void UASParameterCommsMgr::clearRetransmissionLists(int& missingReadCount, int& missingWriteCount )
109 110 111
{
    qDebug() << __FILE__ << __LINE__ << "clearRetransmissionLists";

tstellanova's avatar
tstellanova committed
112
    missingReadCount = 0;
113
    QList<int> readKeys = missingReadPackets.keys();
114
    foreach (int compId, readKeys) {
115 116
        missingReadCount += missingReadPackets.value(compId)->count();
        missingReadPackets.value(compId)->clear();
117 118
    }

tstellanova's avatar
tstellanova committed
119
    missingWriteCount = 0;
120
    QList<int> writeKeys = missingWriteAckPackets.keys();
121
    foreach (int compId, writeKeys) {
122 123
        missingWriteCount += missingWriteAckPackets.value(compId)->count();
        missingWriteAckPackets.value(compId)->clear();
124 125 126 127 128
    }

}


129
void UASParameterCommsMgr::emitPendingParameterCommit(int compId, const QString& key, QVariant& value)
130 131 132 133 134 135 136
{
    int paramType = (int)value.type();
    switch (paramType)
    {
    case QVariant::Char:
    {
        QVariant fixedValue(QChar((unsigned char)value.toInt()));
137
        emit commitPendingParameter(compId, key, fixedValue);
138 139 140 141 142
    }
        break;
    case QVariant::Int:
    {
        QVariant fixedValue(value.toInt());
143
        emit commitPendingParameter(compId, key, fixedValue);
144 145 146 147 148
    }
        break;
    case QVariant::UInt:
    {
        QVariant fixedValue(value.toUInt());
149
        emit commitPendingParameter(compId, key, fixedValue);
150 151 152 153 154
    }
        break;
    case QMetaType::Float:
    {
        QVariant fixedValue(value.toFloat());
155
        emit commitPendingParameter(compId, key, fixedValue);
156 157 158
    }
        break;
    default:
159
        qCritical() << "ABORTED PARAM SEND, INVALID QVARIANT TYPE" << paramType;
160 161 162
        return;
    }

163
    setParameterStatusMsg(tr("Writing %1: %2 for comp. %3").arg(key).arg(value.toDouble()).arg(compId));
164 165 166 167 168 169 170 171 172 173 174 175

}


void UASParameterCommsMgr::resendReadWriteRequests()
{
    int compId;
    QList<int> compIds;

    // Re-request at maximum retransmissionBurstRequestSize parameters at once
    // to prevent link flooding'
    int requestedReadCount = 0;
176
    compIds = missingReadPackets.keys();
177 178
    foreach (compId, compIds) {
        // Request n parameters from this component (at maximum)
179 180
        QList<int>* missingReadParams = missingReadPackets.value(compId, NULL);
        qDebug() << "missingReadParams:" << missingReadParams->count();
181 182
        foreach (int paramId, *missingReadParams) {
            if (requestedReadCount < retransmissionBurstRequestSize) {
183
                //qDebug() << __FILE__ << __LINE__ << "RETRANSMISSION GUARD REQUESTS RETRANSMISSION OF PARAM #" << paramId << "FROM COMPONENT #" << compId;
184 185 186 187 188 189 190 191 192 193 194 195 196 197
                emit parameterUpdateRequestedById(compId, paramId);
                setParameterStatusMsg(tr("Requested retransmission of #%1").arg(paramId+1));
                requestedReadCount++;
            }
            else {
                qDebug() << "Throttling read retransmit requests at" << requestedReadCount;
                break;
            }
        }
    }

    // Re-request at maximum retransmissionBurstRequestSize parameters at once
    // to prevent write-request link flooding
    int requestedWriteCount = 0;
198
    compIds = missingWriteAckPackets.keys();
199
    foreach (compId, compIds) {
200
        QMap <QString, QVariant>* missingParams = missingWriteAckPackets.value(compId);
201 202 203 204
        foreach (QString key, missingParams->keys()) {
            if (requestedWriteCount < retransmissionBurstRequestSize) {
                // Re-request write operation
                QVariant value = missingParams->value(key);
205
                emitPendingParameterCommit(compId, key, value);
206 207 208 209 210 211 212 213 214 215
                requestedWriteCount++;
            }
            else {
                qDebug() << "Throttling write retransmit requests at" << requestedWriteCount;
                break;
            }
        }
    }

    if ((0 == requestedWriteCount) && (0 == requestedReadCount) ) {
216
        qDebug() << __FILE__ << __LINE__ << "No pending re-read or rewrite requests";
217 218 219 220
        if (!transmissionListMode) {
            setRetransmissionGuardEnabled(false);
            transmissionActive = false;
        }
221
    }
222 223 224 225
    else {
        //restart the timer now that we've sent
        setRetransmissionGuardEnabled(true);
    }
226 227
}

228 229 230 231 232
void UASParameterCommsMgr::resetAfterListReceive()
{
    transmissionListMode = false;
    transmissionListSizeKnown.clear();

233
    //We shouldn't clear missingPackets because other transactions might be using them?
234 235 236

}

237 238
void UASParameterCommsMgr::retransmissionGuardTick()
{
239 240 241
    quint64 curTime = QGC::groundTimeMilliseconds();

    //Workaround for an apparent Qt bug that causes retransmission guard timer to fire prematurely (350ms)
242
    int elapsed = (int)(curTime - lastTimerReset);
tstellanova's avatar
tstellanova committed
243
    if (elapsed < retransmissionTimeout) {
244 245 246 247 248
        qDebug() << "retransmissionGuardTick elapsed:" << (curTime - lastTimerReset);
        //reset the guard timer: it fired prematurely
        setRetransmissionGuardEnabled(true);
        return;
    }
249 250
    qDebug() << __FILE__ << __LINE__ << "RETRANSMISSION GUARD ACTIVE after" << elapsed;

251

252
    if (transmissionActive) {
253 254 255

        if (transmissionListMode && transmissionListSizeKnown.isEmpty() ) {
            //we are still waitin for the first parameter list response
256
            if (curTime > this->listRecvTimeout) {
257 258
                //re-request parameters
                setParameterStatusMsg(tr("TIMEOUT: Re-requesting param list"),ParamCommsStatusLevel_Warning);
259
                listRecvTimeout = curTime + 10000;
260
                mav->requestParameters();
261 262
                //reset the timer
                setRetransmissionGuardEnabled(true);
263 264 265 266
            }
            return;
        }

267 268
        // Check for timeout
        // stop retransmission attempts on timeout
269
        if (curTime > transmissionTimeout) {
270
            setRetransmissionGuardEnabled(false);
271 272
            resetAfterListReceive();

tstellanova's avatar
tstellanova committed
273 274 275 276 277 278 279
            int missingReadCount, missingWriteCount;
            clearRetransmissionLists(missingReadCount,missingWriteCount);
            if ((missingReadCount > 0) || (missingWriteCount > 0)) {
                setParameterStatusMsg(tr("TIMEOUT! MISSING: %1 read, %2 write.").arg(missingReadCount).arg(missingWriteCount),
                                      ParamCommsStatusLevel_Warning);
            }

280
            return;
281 282
        }

283 284 285
        resendReadWriteRequests();
    }
    else {
286
        qDebug() << __FILE__ << __LINE__ << "STOPPING RETRANSMISSION GUARD GRACEFULLY";
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        setRetransmissionGuardEnabled(false);
    }
}



/**
 * Enabling the retransmission guard enables the parameter widget to track
 * dropped parameters and to re-request them. This works for both individual
 * parameter reads as well for whole list requests.
 *
 * @param enabled True if retransmission checking should be enabled, false else
 */

void UASParameterCommsMgr::setRetransmissionGuardEnabled(bool enabled)
{
    if (enabled) {
        retransmissionTimer.start(retransmissionTimeout);
305
        lastTimerReset = QGC::groundTimeMilliseconds() ;
306 307 308 309 310 311 312 313 314 315 316 317
    } else {
        retransmissionTimer.stop();
    }
}

void UASParameterCommsMgr::requestParameterUpdate(int component, const QString& parameter)
{
    if (mav) {
        mav->requestParameter(component, parameter);
    }
}

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
void UASParameterCommsMgr::requestRcCalibrationParamsUpdate()
{
    if (!transmissionListMode) {
        QString minTpl("RC%1_MIN");
        QString maxTpl("RC%1_MAX");
        QString trimTpl("RC%1_TRIM");
        QString revTpl("RC%1_REV");

        // Do not request the RC type, as these values depend on this
        // active onboard parameter

        for (unsigned int i = 1; i < (RC_CAL_CHAN_MAX+1); ++i)  {
            qDebug() << "Request RC " << i;
            mav->requestParameter(0, minTpl.arg(i));
            QGC::SLEEP::usleep(5000);
            mav->requestParameter(0, trimTpl.arg(i));
            QGC::SLEEP::usleep(5000);
            mav->requestParameter(0, maxTpl.arg(i));
            QGC::SLEEP::usleep(5000);
            mav->requestParameter(0, revTpl.arg(i));
            QGC::SLEEP::usleep(5000);
        }
    }
    else {
        qDebug() << __FILE__ << __LINE__ << "Ignoring requestRcCalibrationParamsUpdate because we're receiving params list";
    }
}


347 348 349 350 351 352 353 354 355 356 357 358 359 360
/**
 * @param component the subsystem which has the parameter
 * @param parameterName name of the parameter, as delivered by the system
 * @param value value of the parameter
 */
void UASParameterCommsMgr::setParameter(int component, QString parameterName, QVariant value)
{
    if (parameterName.isEmpty()) {
        return;
    }

    double dblValue = value.toDouble();

    if (paramDataModel->isValueLessThanParamMin(parameterName,dblValue)) {
tstellanova's avatar
tstellanova committed
361 362 363
        setParameterStatusMsg(tr("REJ. %1, %2 < min").arg(parameterName).arg(dblValue),
                              ParamCommsStatusLevel_Error
                              );
364 365 366
        return;
    }
    if (paramDataModel->isValueGreaterThanParamMax(parameterName,dblValue)) {
tstellanova's avatar
tstellanova committed
367 368 369
        setParameterStatusMsg(tr("REJ. %1, %2 > max").arg(parameterName).arg(dblValue),
                              ParamCommsStatusLevel_Error
                              );
370 371
        return;
    }
372

373
    QVariant onboardVal;
374
    paramDataModel->getOnboardParamValue(component,parameterName,onboardVal);
375
    if (onboardVal == value) {
tstellanova's avatar
tstellanova committed
376 377 378
        setParameterStatusMsg(tr("REJ. %1 already %2").arg(parameterName).arg(dblValue),
                              ParamCommsStatusLevel_Warning
                              );
379 380 381
        return;
    }

382
    emitPendingParameterCommit(component, parameterName, value);
383 384 385

    // Wait for parameter to be written back
    // mark it therefore as missing
386 387
    if (!missingWriteAckPackets.contains(component)) {
        missingWriteAckPackets.insert(component, new QMap<QString, QVariant>());
388 389 390
    }

    // Insert it in missing write ACK list
391
    missingWriteAckPackets.value(component)->insert(parameterName, value);
392 393

    // Set timeouts
394
    if (transmissionActive) {
395 396
        transmissionTimeout += rewriteTimeout;
    }
397
    else {
398
        quint64 newTransmissionTimeout = QGC::groundTimeMilliseconds() + rewriteTimeout;
399
        if (newTransmissionTimeout > transmissionTimeout) {
400 401 402 403 404 405 406 407 408
            transmissionTimeout = newTransmissionTimeout;
        }
        transmissionActive = true;
    }

    // Enable guard / reset timeouts
    setRetransmissionGuardEnabled(true);
}

tstellanova's avatar
tstellanova committed
409
void UASParameterCommsMgr::setParameterStatusMsg(const QString& msg, ParamCommsStatusLevel_t level)
410 411 412
{
    qDebug() << "parameterStatusMsg: " << msg;
    parameterStatusMsg = msg;
tstellanova's avatar
tstellanova committed
413

tstellanova's avatar
tstellanova committed
414
    emit parameterStatusMsgUpdated(msg,level);
415
}
tstellanova's avatar
tstellanova committed
416 417 418 419 420 421 422 423 424


/**
 * @param uas System which has the component
 * @param component id of the component
 * @param parameterName human friendly name of the parameter
 */
void UASParameterCommsMgr::receivedParameterUpdate(int uas, int compId, int paramCount, int paramId, QString paramName, QVariant value)
{
425 426
    Q_UNUSED(uas); //this object is assigned to one UAS only

427 428
    qDebug() << "compId" << compId << "receivedParameterUpdate:" << paramName;

429
    //notify the data model that we have an updated param
430
    paramDataModel->handleParamUpdate(compId,paramName,value);
tstellanova's avatar
tstellanova committed
431 432

    // Missing packets list has to be instantiated for all components
433 434
    if (!missingReadPackets.contains(compId)) {
        missingReadPackets.insert(compId, new QList<int>());
tstellanova's avatar
tstellanova committed
435 436
    }

437
    QList<int>* compMissReadPackets =  missingReadPackets.value(compId);
438

tstellanova's avatar
tstellanova committed
439 440 441 442 443 444 445 446
    // List mode is different from single parameter transfers
    if (transmissionListMode) {
        // Only accept the list size once on the first packet from
        // each component
        if (!transmissionListSizeKnown.contains(compId)) {
            // Mark list size as known
            transmissionListSizeKnown.insert(compId, true);

447
            qDebug() << "Mark all parameters as missing: " << paramCount;
448
            for (int i = 1; i < paramCount; ++i) { //TODO check: param Id 0 is  "all parameters" and not valid ?
449 450
                if (!compMissReadPackets->contains(i)) {
                    compMissReadPackets->append(i);
tstellanova's avatar
tstellanova committed
451 452 453 454 455 456
                }
            }

            // There is only one transmission timeout for all components
            // since components do not manage their transmission,
            // the longest timeout is safe for all components.
457
            quint64 thisTransmissionTimeout = QGC::groundTimeMilliseconds() + (paramCount*retransmissionTimeout);
tstellanova's avatar
tstellanova committed
458 459 460 461 462 463 464 465
            if (thisTransmissionTimeout > transmissionTimeout) {
                transmissionTimeout = thisTransmissionTimeout;
            }
        }

    }

    // Mark this parameter as received in read list
466
    int index = compMissReadPackets->indexOf(paramId);
tstellanova's avatar
tstellanova committed
467
    if (index != -1) {
468
        compMissReadPackets->removeAt(index);
tstellanova's avatar
tstellanova committed
469 470 471 472
    }

    bool justWritten = false;
    bool writeMismatch = false;
473

tstellanova's avatar
tstellanova committed
474
    // Mark this parameter as received in write ACK list
475
    QMap<QString, QVariant>* compMissWritePackets = missingWriteAckPackets.value(compId);
476 477 478 479
    if (!compMissWritePackets) {
        //we sometimes send a write request on compId 0 and get a response on a nonzero compId eg 50
        compMissWritePackets = missingWriteAckPackets.value(0);
    }
480
    if (compMissWritePackets && compMissWritePackets->contains(paramName)) {
tstellanova's avatar
tstellanova committed
481
        justWritten = true;
482
        if (compMissWritePackets->value(paramName) != value) {
tstellanova's avatar
tstellanova committed
483 484
            writeMismatch = true;
        }
485
        compMissWritePackets->remove(paramName);
tstellanova's avatar
tstellanova committed
486 487
    }

488
    int missReadCount = 0;
489 490
    foreach (int key, missingReadPackets.keys()) {
        missReadCount +=  missingReadPackets.value(key)->count();
tstellanova's avatar
tstellanova committed
491 492 493
    }

    int missWriteCount = 0;
494 495
    foreach (int key, missingWriteAckPackets.keys()) {
        missWriteCount += missingWriteAckPackets.value(key)->count();
tstellanova's avatar
tstellanova committed
496 497 498
    }

    //TODO simplify this if-else tree
499 500
    if (justWritten) {
        if (!writeMismatch) {
501
            setParameterStatusMsg(tr("SUCCESS: Wrote %2 (#%1/%4): %3 [%5]").arg(paramId+1).arg(paramName).arg(value.toDouble()).arg(paramCount).arg(missWriteCount));
502 503
            if (0 == missWriteCount) {
                setParameterStatusMsg(tr("SUCCESS: WROTE ALL PARAMETERS"));
504 505 506 507
                if (persistParamsAfterSend) {
                    writeParamsToPersistentStorage();
                    persistParamsAfterSend = false;
                }
508 509 510 511
            }
        }
        else  {
            // Mismatch, tell user
512
            setParameterStatusMsg(tr("FAILURE: Wrote %1: sent %2 != onboard %3").arg(paramName).arg(compMissWritePackets->value(paramName).toDouble()).arg(value.toDouble()),
513 514
                                  ParamCommsStatusLevel_Warning);
        }
tstellanova's avatar
tstellanova committed
515 516
    }
    else {
517
        if (missReadCount == 0) {
tstellanova's avatar
tstellanova committed
518 519 520 521 522 523 524
            // Transmission done
            QTime time = QTime::currentTime();
            QString timeString = time.toString();
            setParameterStatusMsg(tr("All received. (updated at %1)").arg(timeString));
        }
        else {
            // Transmission in progress
525 526
            QString val = QString("%1").arg(value.toFloat(), 5, 'f', 1, QChar(' '));
            setParameterStatusMsg(tr("OK: %1 %2 (%3/%4)").arg(paramName).arg(val).arg(paramCount-missReadCount).arg(paramCount),
tstellanova's avatar
tstellanova committed
527
                                  ParamCommsStatusLevel_Warning);
528
            //transmissionMissingPackets
tstellanova's avatar
tstellanova committed
529 530 531 532
        }
    }

    // Check if last parameter was received
533
    if (missReadCount == 0 && missWriteCount == 0) {
534
        resetAfterListReceive();
535
        setRetransmissionGuardEnabled(false);
tstellanova's avatar
tstellanova committed
536 537 538
        //all parameters have been received, broadcast to UI
        emit parameterListUpToDate();
    }
539
    else {
540 541
        //reset the timeout timer since we received one
        setRetransmissionGuardEnabled(true);
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
        //qDebug() << "missCount:" << missCount << "missWriteCount:" << missWriteCount;
        //if (missCount < 4) {
        //    foreach (int key, transmissionMissingPackets.keys()) {
        //        QList<int>* list = transmissionMissingPackets.value(key);

        //        if (list && list->count()) {
        //            QString yazza = QString().sprintf("Component %d missing %d: ",key,list->count());

        //            for (int i = 0; i < list->count(); i++) {
        //                int val = list->at(i);
        //                yazza.append( QString().sprintf("%d,",val) );
        //            }

        //            qDebug() << yazza;
        //        }
        //        else {
        //            //very suspicious...no actual missing items??
        //            transmissionMissingPackets.remove(key);
        //            break;
        //        }
        //    }
        //}
565
    }
tstellanova's avatar
tstellanova committed
566 567 568 569 570 571 572
}


void UASParameterCommsMgr::writeParamsToPersistentStorage()
{
    if (mav) {
        mav->writeParametersToStorage(); //TODO track timeout, retransmit etc?
573
        persistParamsAfterSend = false; //done
tstellanova's avatar
tstellanova committed
574 575 576 577
    }
}


578
void UASParameterCommsMgr::sendPendingParameters(bool copyToPersistent)
tstellanova's avatar
tstellanova committed
579
{
580 581
    persistParamsAfterSend |= copyToPersistent;

tstellanova's avatar
tstellanova committed
582 583
    // Iterate through all components, through all pending parameters and send them to UAS
    int parametersSent = 0;
584
    QMap<int, QMap<QString, QVariant>*>* changedValues = paramDataModel->getAllPendingParams();
tstellanova's avatar
tstellanova committed
585
    QMap<int, QMap<QString, QVariant>*>::iterator i;
586
    for (i = changedValues->begin(); i != changedValues->end(); ++i) {
tstellanova's avatar
tstellanova committed
587
        // Iterate through the parameters of the component
tstellanova's avatar
tstellanova committed
588 589 590 591 592 593 594 595 596
        int compId = i.key();
        QMap<QString, QVariant>* paramList = i.value();
        QMap<QString, QVariant>::iterator j;
        setParameterStatusMsg(tr("%1 pending params for component %2").arg(paramList->count()).arg(compId));

        for (j = paramList->begin(); j != paramList->end(); ++j) {
            //TODO mavlink command for "set parameter list" ?
            setParameter(compId, j.key(), j.value());
            parametersSent++;
tstellanova's avatar
tstellanova committed
597 598 599 600 601 602
        }
    }

    // Change transmission status if necessary
    if (parametersSent == 0) {
        setParameterStatusMsg(tr("No transmission: No changed values."),ParamCommsStatusLevel_Warning);
603 604 605 606 607
        if (persistParamsAfterSend) {
            writeParamsToPersistentStorage();
        }
    }
    else {
tstellanova's avatar
tstellanova committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621
        setParameterStatusMsg(tr("Transmitting %1 parameters.").arg(parametersSent));
        // Set timeouts
        if (transmissionActive) {
            transmissionTimeout += parametersSent*rewriteTimeout;
        }
        else {
            transmissionActive = true;
            quint64 newTransmissionTimeout = QGC::groundTimeMilliseconds() + parametersSent*rewriteTimeout;
            if (newTransmissionTimeout > transmissionTimeout) {
                transmissionTimeout = newTransmissionTimeout;
            }
        }
        // Enable guard
        setRetransmissionGuardEnabled(true);
622 623

        qDebug() << "Pending parameters now:" << paramDataModel->countPendingParams();
tstellanova's avatar
tstellanova committed
624 625 626
    }
}

627 628 629
UASParameterCommsMgr::~UASParameterCommsMgr()
{
    setRetransmissionGuardEnabled(false);
630 631 632 633 634

    QString ptrStr;
    ptrStr.sprintf("%8p", this);
    qDebug() <<  "UASParameterCommsMgr destructor: " << ptrStr ;

635 636
}