OpalLink.cc 20.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL 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.

    QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of class OpalLink
 *   @author Bryan Godbolt <godbolt@ualberta.ca>
 */

30 31
#include "OpalLink.h"

32 33 34 35 36
OpalLink::OpalLink() :
        connectState(false),
        heartbeatTimer(new QTimer(this)),
        heartbeatRate(MAVLINK_HEARTBEAT_DEFAULT_RATE),
        m_heartbeatsEnabled(true),
37
        getSignalsTimer(new QTimer(this)),
Bryan Godbolt's avatar
Bryan Godbolt committed
38
        getSignalsPeriod(10),
39 40
        receiveBuffer(new QQueue<QByteArray>()),
        systemID(1),
41
        componentID(1),
42
        params(NULL),
43 44
        opalInstID(101),
        sendRCValues(false)
45
{
46
    start(QThread::LowPriority);
Bryan Godbolt's avatar
Bryan Godbolt committed
47 48 49 50 51

    // Set unique ID and add link to the list of links
    this->id = getNextLinkId();
    this->name = tr("OpalRT link ") + QString::number(getId());
    LinkManager::instance()->add(this);
52 53 54

    // Start heartbeat timer, emitting a heartbeat at the configured rate
    QObject::connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(heartbeat()));
55 56

    QObject::connect(getSignalsTimer, SIGNAL(timeout()), this, SLOT(getSignals()));
57 58
}

59 60 61 62 63 64 65 66

/*
 *
  Communication
 *
 */

qint64 OpalLink::bytesAvailable()
67
{
68
    return 0;
Bryan Godbolt's avatar
Bryan Godbolt committed
69 70
}

71
void OpalLink::writeBytes(const char *bytes, qint64 length)
Bryan Godbolt's avatar
Bryan Godbolt committed
72
{
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    /* decode the message */
    mavlink_message_t msg;
    mavlink_status_t status;        
    int decodeSuccess = 0;
    for (int i=0; (!(decodeSuccess=mavlink_parse_char(this->getId(), bytes[i], &msg, &status))&& i<length); ++i);    

    /* perform the appropriate action */
    if (decodeSuccess)
    {
        switch(msg.msgid)
        {
        case MAVLINK_MSG_ID_PARAM_REQUEST_LIST:
            {
                qDebug() << "OpalLink::writeBytes(): request params";
87

88
                mavlink_message_t param;
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

                OpalRT::ParameterList::const_iterator paramIter;
                for (paramIter = params->begin(); paramIter != params->end(); ++paramIter)
                {
                    mavlink_msg_param_value_pack(systemID,
                                                 (*paramIter).getComponentID(),
                                                 &param,
                                                 (*paramIter).getParamID().toInt8_t(),
                                                 (static_cast<OpalRT::Parameter>(*paramIter)).getValue(),
                                                 params->count(),
                                                 params->indexOf(*paramIter));
                    receiveMessage(param);
                }


105 106 107
            }
        case MAVLINK_MSG_ID_PARAM_SET:
            {
108 109 110

//                qDebug() << "OpalLink::writeBytes(): Attempt to set a parameter";

111 112
                mavlink_param_set_t param;
                mavlink_msg_param_set_decode(&msg, &param);
113 114 115 116 117
                OpalRT::QGCParamID paramName((char*)param.param_id);

//                qDebug() << "OpalLink::writeBytes():paramName: " << paramName;

                if ((*params).contains(param.target_component, paramName))
118
                {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                    OpalRT::Parameter p = (*params)(param.target_component, paramName);
//                    qDebug() << __FILE__ << ":" << __LINE__ << ": "  << p;
                    // Set the param value in Opal-RT
                    p.setValue(param.param_value);

                    // Get the param value from Opal-RT to make sure it was set properly
                    mavlink_message_t paramMsg;
                    mavlink_msg_param_value_pack(systemID,
                                                 p.getComponentID(),
                                                 &paramMsg,
                                                 p.getParamID().toInt8_t(),
                                                 p.getValue(),
                                                 params->count(),
                                                 params->indexOf(p));
                    receiveMessage(paramMsg);
134 135 136
                }
            }
            break;
137 138 139
        case MAVLINK_MSG_ID_REQUEST_RC_CHANNELS:
        {
        	mavlink_request_rc_channels_t rc;
140
        	mavlink_msg_request_rc_channels_decode(&msg, &rc);        	
141 142 143
        	this->sendRCValues = static_cast<bool>(rc.enabled);
        }
        break;
144
#ifdef MAVLINK_ENABLED_UALBERTA_MESSAGES
145 146 147 148
        case MAVLINK_MSG_ID_RADIO_CALIBRATION:
        {
            mavlink_radio_calibration_t radio;
            mavlink_msg_radio_calibration_decode(&msg, &radio);
149 150 151 152 153 154 155 156 157
//            qDebug() << "RADIO CALIBRATION RECEIVED";
//            qDebug() << "AILERON: " << radio.aileron[0] << " " << radio.aileron[1] << " " << radio.aileron[2];
//            qDebug() << "ELEVATOR: " << radio.elevator[0] << " " << radio.elevator[1] << " " << radio.elevator[2];
//            qDebug() << "RUDDER: " << radio.rudder[0] << " " << radio.rudder[1] << " " << radio.rudder[2];
//            qDebug() << "GYRO: " << radio.gyro[0] << " " << radio.gyro[1];
//            qDebug() << "PITCH: " << radio.pitch[0] << radio.pitch[1] << radio.pitch[2] << radio.pitch[3] << radio.pitch[4];
//            qDebug() << "THROTTLE: " << radio.throttle[0] << radio.throttle[1] << radio.throttle[2] << radio.throttle[3] << radio.throttle[4];

            /* AILERON SERVO */
158
            if (params->contains(OpalRT::SERVO_INPUTS, "AIL_RIGHT_IN"))
159
                params->getParameter(OpalRT::SERVO_INPUTS, "AIL_RIGHT_IN").setValue(((radio.aileron[0]>900 /*in us?*/)?radio.aileron[0]/1000:radio.aileron[0]));
160
            if (params->contains(OpalRT::SERVO_INPUTS, "AIL_CENTER_IN"))
161
                params->getParameter(OpalRT::SERVO_INPUTS, "AIL_CENTER_IN").setValue(((radio.aileron[1]>900 /*in us?*/)?radio.aileron[1]/1000:radio.aileron[1]));
162
            if (params->contains(OpalRT::SERVO_INPUTS, "AIL_LEFT_IN"))
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
                params->getParameter(OpalRT::SERVO_INPUTS, "AIL_LEFT_IN").setValue(((radio.aileron[2]>900 /*in us?*/)?radio.aileron[2]/1000:radio.aileron[2]));
            /* ELEVATOR SERVO */
            if (params->contains(OpalRT::SERVO_INPUTS, "ELE_DOWN_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "ELE_DOWN_IN").setValue(((radio.elevator[0]>900 /*in us?*/)?radio.elevator[0]/1000:radio.elevator[0]));
            if (params->contains(OpalRT::SERVO_INPUTS, "ELE_CENTER_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "ELE_CENTER_IN").setValue(((radio.elevator[1]>900 /*in us?*/)?radio.elevator[1]/1000:radio.elevator[1]));
            if (params->contains(OpalRT::SERVO_INPUTS, "ELE_UP_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "ELE_UP_IN").setValue(((radio.elevator[2]>900 /*in us?*/)?radio.elevator[2]/1000:radio.elevator[2]));
            /* THROTTLE SERVO */
            if (params->contains(OpalRT::SERVO_INPUTS, "THR_SET0_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "THR_SET0_IN").setValue(((radio.throttle[0]>900 /*in us?*/)?radio.throttle[0]/1000:radio.throttle[0]));
            if (params->contains(OpalRT::SERVO_INPUTS, "THR_SET1_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "THR_SET1_IN").setValue(((radio.throttle[1]>900 /*in us?*/)?radio.throttle[1]/1000:radio.throttle[1]));
            if (params->contains(OpalRT::SERVO_INPUTS, "THR_SET2_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "THR_SET2_IN").setValue(((radio.throttle[2]>900 /*in us?*/)?radio.throttle[2]/1000:radio.throttle[2]));
            if (params->contains(OpalRT::SERVO_INPUTS, "THR_SET3_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "THR_SET3_IN").setValue(((radio.throttle[3]>900 /*in us?*/)?radio.throttle[3]/1000:radio.throttle[3]));
            if (params->contains(OpalRT::SERVO_INPUTS, "THR_SET4_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "THR_SET4_IN").setValue(((radio.throttle[4]>900 /*in us?*/)?radio.throttle[4]/1000:radio.throttle[4]));
            /* RUDDER SERVO */
            if (params->contains(OpalRT::SERVO_INPUTS, "RUD_LEFT_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "RUD_LEFT_IN").setValue(((radio.rudder[0]>900 /*in us?*/)?radio.rudder[0]/1000:radio.rudder[0]));
            if (params->contains(OpalRT::SERVO_INPUTS, "RUD_CENTER_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "RUD_CENTER_IN").setValue(((radio.rudder[1]>900 /*in us?*/)?radio.rudder[1]/1000:radio.rudder[1]));
            if (params->contains(OpalRT::SERVO_INPUTS, "RUD_RIGHT_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "RUD_RIGHT_IN").setValue(((radio.rudder[2]>900 /*in us?*/)?radio.rudder[2]/1000:radio.rudder[2]));
            /* GYRO MODE/GAIN SWITCH */
            if (params->contains(OpalRT::SERVO_INPUTS, "GYRO_DEF_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "GYRO_DEF_IN").setValue(((radio.gyro[0]>900 /*in us?*/)?radio.gyro[0]/1000:radio.gyro[0]));
            if (params->contains(OpalRT::SERVO_INPUTS, "GYRO_TOG_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "GYRO_TOG_IN").setValue(((radio.gyro[1]>900 /*in us?*/)?radio.gyro[1]/1000:radio.gyro[1]));
            /* PITCH SERVO */
            if (params->contains(OpalRT::SERVO_INPUTS, "PIT_SET0_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "PIT_SET0_IN").setValue(((radio.pitch[0]>900 /*in us?*/)?radio.pitch[0]/1000:radio.pitch[0]));
            if (params->contains(OpalRT::SERVO_INPUTS, "PIT_SET1_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "PIT_SET1_IN").setValue(((radio.pitch[1]>900 /*in us?*/)?radio.pitch[1]/1000:radio.pitch[1]));
            if (params->contains(OpalRT::SERVO_INPUTS, "PIT_SET2_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "PIT_SET2_IN").setValue(((radio.pitch[2]>900 /*in us?*/)?radio.pitch[2]/1000:radio.pitch[2]));
            if (params->contains(OpalRT::SERVO_INPUTS, "PIT_SET3_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "PIT_SET3_IN").setValue(((radio.pitch[3]>900 /*in us?*/)?radio.pitch[3]/1000:radio.pitch[3]));
            if (params->contains(OpalRT::SERVO_INPUTS, "PIT_SET4_IN"))
                params->getParameter(OpalRT::SERVO_INPUTS, "PIT_SET4_IN").setValue(((radio.pitch[4]>900 /*in us?*/)?radio.pitch[4]/1000:radio.pitch[4]));
205 206 207
        }
        break;
#endif
208 209 210 211 212 213
        default:
            {
                qDebug() << "OpalLink::writeBytes(): Unknown mavlink packet";
            }
        }
    }
Bryan Godbolt's avatar
Bryan Godbolt committed
214 215
}

216

217
void OpalLink::readBytes()
Bryan Godbolt's avatar
Bryan Godbolt committed
218
{
219
    receiveDataMutex.lock();
Bryan Godbolt's avatar
Bryan Godbolt committed
220
    emit bytesReceived(this, receiveBuffer->dequeue());
221 222
    receiveDataMutex.unlock();

Bryan Godbolt's avatar
Bryan Godbolt committed
223 224
}

225
void OpalLink::receiveMessage(mavlink_message_t message)
Bryan Godbolt's avatar
Bryan Godbolt committed
226 227
{

228 229 230 231 232 233 234
    // Create buffer
    char buffer[MAVLINK_MAX_PACKET_LEN];
    // Write message into buffer, prepending start sign
    int len = mavlink_msg_to_send_buffer((uint8_t*)(buffer), &message);
    // If link is connected
    if (isConnected())
    {
Bryan Godbolt's avatar
Bryan Godbolt committed
235
        receiveDataMutex.lock();
236
        receiveBuffer->enqueue(QByteArray(buffer, len));
Bryan Godbolt's avatar
Bryan Godbolt committed
237
        receiveDataMutex.unlock();
238
        readBytes();
239 240
    }

Bryan Godbolt's avatar
Bryan Godbolt committed
241 242
}

243
void OpalLink::heartbeat()
Bryan Godbolt's avatar
Bryan Godbolt committed
244
{
245 246 247 248 249 250 251 252

    if (m_heartbeatsEnabled)
    {
        mavlink_message_t beat;
        mavlink_msg_heartbeat_pack(systemID, componentID,&beat, MAV_HELICOPTER, MAV_AUTOPILOT_GENERIC);
        receiveMessage(beat);
    }

Bryan Godbolt's avatar
Bryan Godbolt committed
253
}
254 255
void OpalLink::setSignals(double *values)
{
Bryan Godbolt's avatar
Bryan Godbolt committed
256
    unsigned short numSignals = 2;
257 258 259 260 261 262 263
    unsigned short logicalId = 1;
    unsigned short signalIndex[] = {0,1};

    int returnValue;
    returnValue =  OpalSetSignals( numSignals, logicalId, signalIndex, values);
    if (returnValue != EOK)
    {
264
        OpalRT::OpalErrorMsg::displayLastErrorMsg();
265 266
    }
}
267
void OpalLink::getSignals()
Bryan Godbolt's avatar
Bryan Godbolt committed
268
{
Bryan Godbolt's avatar
Bryan Godbolt committed
269 270 271 272
    unsigned long  timeout = 0;
    unsigned short acqGroup = 0; //this is actually group 1 in the model
    unsigned short *numSignals = new unsigned short(0);
    double *timestep = new double(0);
273
    double values[OpalRT::NUM_OUTPUT_SIGNALS] = {};
Bryan Godbolt's avatar
Bryan Godbolt committed
274 275 276 277 278
    unsigned short *lastValues = new unsigned short(false);
    unsigned short *decimation = new unsigned short(0);

    while (!(*lastValues))
    {
279
        int returnVal = OpalGetSignals(timeout, acqGroup, OpalRT::NUM_OUTPUT_SIGNALS, numSignals, timestep,
Bryan Godbolt's avatar
Bryan Godbolt committed
280 281 282
                                       values, lastValues, decimation);

        if (returnVal == EOK )
283
        {            
284
            /* Send position info to qgroundcontrol */
Bryan Godbolt's avatar
Bryan Godbolt committed
285 286 287 288 289 290 291 292 293 294
            mavlink_message_t local_position;
            mavlink_msg_local_position_pack(systemID, componentID, &local_position,
                                            (*timestep)*1000000,
                                            values[OpalRT::X_POS],
                                            values[OpalRT::Y_POS],
                                            values[OpalRT::Z_POS],
                                            values[OpalRT::X_VEL],
                                            values[OpalRT::Y_VEL],
                                            values[OpalRT::Z_VEL]);
            receiveMessage(local_position);
295 296 297 298 299 300 301 302

            /* send attitude info to qgroundcontrol */
            mavlink_message_t attitude;
            mavlink_msg_attitude_pack(systemID, componentID, &attitude,
                                      (*timestep)*1000000,
                                      values[OpalRT::ROLL],
                                      values[OpalRT::PITCH],
                                      values[OpalRT::YAW],
303 304 305
                                      values[OpalRT::ROLL_SPEED],
                                      values[OpalRT::PITCH_SPEED],
                                      values[OpalRT::YAW_SPEED]
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                                      );
            receiveMessage(attitude);

            /* send bias info to qgroundcontrol */
            mavlink_message_t bias;
            mavlink_msg_nav_filter_bias_pack(systemID, componentID, &bias,
                                             (*timestep)*1000000,
                                             values[OpalRT::B_F_0],
                                             values[OpalRT::B_F_1],
                                             values[OpalRT::B_F_2],
                                             values[OpalRT::B_W_0],
                                             values[OpalRT::B_W_1],
                                             values[OpalRT::B_W_2]
                                             );
            receiveMessage(bias);
Bryan Godbolt's avatar
Bryan Godbolt committed
321 322

            /* send radio outputs */
323 324 325 326
            if (sendRCValues)
            {
                mavlink_message_t rc;
                mavlink_msg_rc_channels_pack(systemID, componentID, &rc,
Bryan Godbolt's avatar
Bryan Godbolt committed
327 328 329 330 331 332 333 334
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_1]),
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_2]),
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_3]),
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_4]),
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_5]),
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_6]),
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_7]),
                                             duty2PulseMicros(values[OpalRT::RAW_CHANNEL_8]),
335 336 337 338 339 340 341 342
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_1], OpalRT::NORM_CHANNEL_1),
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_2], OpalRT::NORM_CHANNEL_2),
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_3], OpalRT::NORM_CHANNEL_3),
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_4], OpalRT::NORM_CHANNEL_4),
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_5], OpalRT::NORM_CHANNEL_5),
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_6], OpalRT::NORM_CHANNEL_6),
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_7], OpalRT::NORM_CHANNEL_7),
                                             rescaleNorm(values[OpalRT::NORM_CHANNEL_8], OpalRT::NORM_CHANNEL_8),
Bryan Godbolt's avatar
Bryan Godbolt committed
343 344
                                             0 //rssi unused
                                             );
345 346
                receiveMessage(rc);
            }
347 348
        }        
        else if (returnVal != EAGAIN) // if returnVal == EAGAIN => data just wasn't ready
Bryan Godbolt's avatar
Bryan Godbolt committed
349 350
        {
            getSignalsTimer->stop();
351
            OpalRT::OpalErrorMsg::displayLastErrorMsg();
Bryan Godbolt's avatar
Bryan Godbolt committed
352 353 354 355 356 357 358 359 360
        }
    }

    /* deallocate used memory */

    delete numSignals;
    delete timestep;
    delete lastValues;
    delete decimation;
361

Bryan Godbolt's avatar
Bryan Godbolt committed
362 363
}

364

365 366 367 368 369 370
/*
 *
  Administrative
 *
 */
void OpalLink::run()
Bryan Godbolt's avatar
Bryan Godbolt committed
371
{
Bryan Godbolt's avatar
Bryan Godbolt committed
372
//    qDebug() << "OpalLink::run():: Starting the thread";
373 374
}

375 376 377
int OpalLink::getId()
{
    return id;
Bryan Godbolt's avatar
Bryan Godbolt committed
378 379
}

380
QString OpalLink::getName()
Bryan Godbolt's avatar
Bryan Godbolt committed
381
{
382
    return name;
Bryan Godbolt's avatar
Bryan Godbolt committed
383 384
}

385
void OpalLink::setName(QString name)
Bryan Godbolt's avatar
Bryan Godbolt committed
386
{
387 388
    this->name = name;
    emit nameChanged(this->name);
Bryan Godbolt's avatar
Bryan Godbolt committed
389 390
}

391
bool OpalLink::isConnected() {    
392
    return connectState;
Bryan Godbolt's avatar
Bryan Godbolt committed
393 394
}

Bryan Godbolt's avatar
Bryan Godbolt committed
395 396 397 398 399 400
uint16_t OpalLink::duty2PulseMicros(double duty)
{
    /* duty cycle assumed to be of a signal at 70 Hz */
    return static_cast<uint16_t>(duty/70*1000000);
}

401
uint8_t OpalLink::rescaleNorm(double norm, int ch)
402
{
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    switch(ch)
    {
    case OpalRT::NORM_CHANNEL_1:
    case OpalRT::NORM_CHANNEL_2:
    case OpalRT::NORM_CHANNEL_4:
    default:
        // three setpoints
        return static_cast<uint8_t>((norm+1)/2*255);
        break;
    case OpalRT::NORM_CHANNEL_5:
        //two setpoints
    case OpalRT::NORM_CHANNEL_3:
    case OpalRT::NORM_CHANNEL_6:        
        return static_cast<uint8_t>(norm*255);
        break;
    }


421
}
Bryan Godbolt's avatar
Bryan Godbolt committed
422

423 424 425

bool OpalLink::connect()
{
Bryan Godbolt's avatar
Bryan Godbolt committed
426
    short modelState;
427

428
    if ((OpalConnect(opalInstID, false, &modelState) == EOK)
429 430
        && (OpalGetSignalControl(0, true) == EOK)
        && (OpalGetParameterControl(true) == EOK))
Bryan Godbolt's avatar
Bryan Godbolt committed
431
    {
432
        connectState = true;
433 434
        if (params)
            delete params;
435
        params = new OpalRT::ParameterList();
436 437 438
        emit connected();
        heartbeatTimer->start(1000/heartbeatRate);
        getSignalsTimer->start(getSignalsPeriod);
Bryan Godbolt's avatar
Bryan Godbolt committed
439 440 441 442
    }
    else
    {
        connectState = false;
443
        OpalRT::OpalErrorMsg::displayLastErrorMsg();
Bryan Godbolt's avatar
Bryan Godbolt committed
444
    }
445 446 447

    emit connected(connectState);
    return connectState;
448 449 450 451
}

bool OpalLink::disconnect()
{
452 453 454 455
    // OpalDisconnect returns void so its success or failure cannot be tested
    OpalDisconnect();
    heartbeatTimer->stop();
    getSignalsTimer->stop();
456 457
    connectState = false;
    emit connected(connectState);
458
    return true;
459 460
}

461

462

463 464 465 466 467 468 469 470

/*
 *
  Statisctics
 *
 */

qint64 OpalLink::getNominalDataRate()
471
{
472
    return 0; //unknown
473
}
Bryan Godbolt's avatar
Bryan Godbolt committed
474

475
int OpalLink::getLinkQuality()
Bryan Godbolt's avatar
Bryan Godbolt committed
476
{
477
    return -1; //not supported
Bryan Godbolt's avatar
Bryan Godbolt committed
478 479
}

480
qint64 OpalLink::getTotalUpstream()
Bryan Godbolt's avatar
Bryan Godbolt committed
481
{
482 483 484 485 486
    statisticsMutex.lock();
    qint64 totalUpstream =  bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
    return totalUpstream;
}
Bryan Godbolt's avatar
Bryan Godbolt committed
487

488 489 490 491 492
qint64 OpalLink::getTotalDownstream() {
    statisticsMutex.lock();
    qint64 totalDownstream = bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
    return totalDownstream;
493 494
}

495
qint64 OpalLink::getCurrentUpstream()
496
{
497 498
    return 0; //unknown
}
499

500 501 502 503
qint64 OpalLink::getMaxUpstream()
{
    return 0; //unknown
}
504

505 506
qint64 OpalLink::getBitsSent() {
    return bitsSentTotal;
507 508
}

509 510 511
qint64 OpalLink::getBitsReceived() {
    return bitsReceivedTotal;
}
512 513


514 515 516
bool OpalLink::isFullDuplex()
{
    return false;
Bryan Godbolt's avatar
Bryan Godbolt committed
517
}