OpalLink.cc 6.82 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 38
        getSignalsTimer(new QTimer(this)),
        getSignalsPeriod(1000),
39 40 41
        receiveBuffer(new QQueue<QByteArray>()),
        systemID(1),
        componentID(1)
42
{
43
    start(QThread::LowPriority);
Bryan Godbolt's avatar
Bryan Godbolt committed
44 45 46 47 48

    // 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);
49 50 51

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

    QObject::connect(getSignalsTimer, SIGNAL(timeout()), this, SLOT(getSignals()));
54 55
}

56 57 58 59 60 61 62 63

/*
 *
  Communication
 *
 */

qint64 OpalLink::bytesAvailable()
64
{
65
    return 0;
Bryan Godbolt's avatar
Bryan Godbolt committed
66 67
}

68
void OpalLink::writeBytes(const char *bytes, qint64 length)
Bryan Godbolt's avatar
Bryan Godbolt committed
69
{
70

Bryan Godbolt's avatar
Bryan Godbolt committed
71 72
}

73

74
void OpalLink::readBytes()
Bryan Godbolt's avatar
Bryan Godbolt committed
75
{
76
    receiveDataMutex.lock();
77 78
    const qint64 maxLength = 2048;
    char bytes[maxLength];
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    qDebug() << "OpalLink::readBytes(): Reading a message.  size of buffer: " << receiveBuffer->count();
    QByteArray message = receiveBuffer->dequeue();
    if (maxLength < message.size())
    {
        qDebug() << "OpalLink::readBytes:: Buffer Overflow";

        memcpy(bytes, message.data(), maxLength);
    }
    else
    {
        memcpy(bytes, message.data(), message.size());
    }

    emit bytesReceived(this, message);
    receiveDataMutex.unlock();

Bryan Godbolt's avatar
Bryan Godbolt committed
95 96
}

97
void OpalLink::receiveMessage(mavlink_message_t message)
Bryan Godbolt's avatar
Bryan Godbolt committed
98 99
{

100 101 102 103 104 105 106 107
    // 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())
    {
        receiveBuffer->enqueue(QByteArray(buffer, len));
108
        readBytes();
109 110
    }

Bryan Godbolt's avatar
Bryan Godbolt committed
111 112
}

113
void OpalLink::heartbeat()
Bryan Godbolt's avatar
Bryan Godbolt committed
114
{
115 116 117 118 119 120 121 122 123

    if (m_heartbeatsEnabled)
    {
        qDebug() << "OpalLink::heartbeat(): Generate a heartbeat";
        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
124 125
}

126
void OpalLink::getSignals()
Bryan Godbolt's avatar
Bryan Godbolt committed
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
//    qDebug() <<  "OpalLink::getSignals(): Attempting to acquire signals";
//
//
//    unsigned long  timeout = 0;
//    unsigned short acqGroup = 0; //this is actually group 1 in the model
//    unsigned short allocatedSignals = NUM_OUTPUT_SIGNALS;
//    unsigned short *numSignals = new unsigned short(0);
//    double *timestep = new double(0);
//    double values[NUM_OUTPUT_SIGNALS] = {};
//    unsigned short *lastValues = new unsigned short(false);
//    unsigned short *decimation = new unsigned short(0);
//
//    int returnVal = OpalGetSignals(timeout, acqGroup, allocatedSignals, numSignals, timestep,
//                                   values, lastValues, decimation);
//
//    if (returnVal == EOK )
//    {
//        qDebug() << "OpalLink::getSignals: Timestep=" << *timestep;// << ", Last? " << (bool)(*lastValues);
//    }
//    else if (returnVal == EAGAIN)
//    {
//        qDebug() << "OpalLink::getSignals: Data was not ready";
//    }
//    // if returnVal == EAGAIN => data just wasn't ready
//    else if (returnVal != EAGAIN)
//    {
//        getSignalsTimer->stop();
//        displayErrorMsg();
//    }
//    /* deallocate used memory */
//
//    delete timestep;
//    delete lastValues;
//    delete lastValues;
//    delete decimation;
163

Bryan Godbolt's avatar
Bryan Godbolt committed
164 165
}

166 167 168 169 170 171
/*
 *
  Administrative
 *
 */
void OpalLink::run()
Bryan Godbolt's avatar
Bryan Godbolt committed
172
{
173
    qDebug() << "OpalLink::run():: Starting the thread";
174 175
}

176 177 178
int OpalLink::getId()
{
    return id;
Bryan Godbolt's avatar
Bryan Godbolt committed
179 180
}

181
QString OpalLink::getName()
Bryan Godbolt's avatar
Bryan Godbolt committed
182
{
183
    return name;
Bryan Godbolt's avatar
Bryan Godbolt committed
184 185
}

186
void OpalLink::setName(QString name)
Bryan Godbolt's avatar
Bryan Godbolt committed
187
{
188 189
    this->name = name;
    emit nameChanged(this->name);
Bryan Godbolt's avatar
Bryan Godbolt committed
190 191
}

192 193 194
bool OpalLink::isConnected() {
    //qDebug() << "OpalLink::isConnected:: connectState: " << connectState;
    return connectState;
Bryan Godbolt's avatar
Bryan Godbolt committed
195 196 197 198
}



199 200 201

bool OpalLink::connect()
{
Bryan Godbolt's avatar
Bryan Godbolt committed
202
    short modelState;
203 204

    /// \todo allow configuration of instid in window
205 206
//    if (OpalConnect(101, false, &modelState) == EOK)
//    {
207
        connectState = true;
208 209 210
        emit connected();
        heartbeatTimer->start(1000/heartbeatRate);
        getSignalsTimer->start(getSignalsPeriod);
211 212 213 214 215 216
//    }
//    else
//    {
//        connectState = false;
//        displayErrorMsg();
//    }
217 218 219

    emit connected(connectState);
    return connectState;
220 221 222 223 224 225 226
}

bool OpalLink::disconnect()
{
    return false;
}

227 228 229 230 231 232 233 234 235
void OpalLink::displayErrorMsg()
{
    setLastErrorMsg();
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Critical);
    msgBox.setText(lastErrorMsg);
    msgBox.exec();
}

236 237
void OpalLink::setLastErrorMsg()
{
238 239 240 241 242
//    char buf[512];
//    unsigned short len;
//    OpalGetLastErrMsg(buf, sizeof(buf), &len);
//    lastErrorMsg.clear();
//    lastErrorMsg.append(buf);
243 244
}

245 246 247 248 249 250 251 252

/*
 *
  Statisctics
 *
 */

qint64 OpalLink::getNominalDataRate()
253
{
254
    return 0; //unknown
255
}
Bryan Godbolt's avatar
Bryan Godbolt committed
256

257
int OpalLink::getLinkQuality()
Bryan Godbolt's avatar
Bryan Godbolt committed
258
{
259
    return -1; //not supported
Bryan Godbolt's avatar
Bryan Godbolt committed
260 261
}

262
qint64 OpalLink::getTotalUpstream()
Bryan Godbolt's avatar
Bryan Godbolt committed
263
{
264 265 266 267 268
    statisticsMutex.lock();
    qint64 totalUpstream =  bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
    return totalUpstream;
}
Bryan Godbolt's avatar
Bryan Godbolt committed
269

270 271 272 273 274
qint64 OpalLink::getTotalDownstream() {
    statisticsMutex.lock();
    qint64 totalDownstream = bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
    return totalDownstream;
275 276
}

277
qint64 OpalLink::getCurrentUpstream()
278
{
279 280
    return 0; //unknown
}
281

282 283 284 285
qint64 OpalLink::getMaxUpstream()
{
    return 0; //unknown
}
286

287 288
qint64 OpalLink::getBitsSent() {
    return bitsSentTotal;
289 290
}

291 292 293
qint64 OpalLink::getBitsReceived() {
    return bitsReceivedTotal;
}
294 295


296 297 298
bool OpalLink::isFullDuplex()
{
    return false;
Bryan Godbolt's avatar
Bryan Godbolt committed
299
}