Changeset b7057c4 in subsurface


Ignore:
Timestamp:
Jul 11, 2017, 4:17:00 AM (9 days ago)
Author:
Jan Mulder <jlmulder@…>
Branches:
master
Children:
b409e9f
Parents:
d233725
git-author:
Jan Mulder <jlmulder@…> (07/05/17 09:37:21)
git-committer:
Jan Mulder <jlmulder@…> (07/11/17 04:17:00)
Message:

OSTC over BLE: take care of credits

Handle credits. Do not just ask for maximum credits all the time as this
will stop the download. Also do not let the credits go back to 0 (while
this might work, this is not tested). Getting back the 0 credits stops
the download, and even when it can be restarted, it is less efficient
(and not needed). Notice also that it takes some time before a grant
request is honoured. During testing I saw reception of up to 25 packets
between request and grant. So a lower bound for the request of
32 packets seems resonable.

One aspect the Telit/Stollmann? TIO puzzeled me. Sections 4.1 and 4.2
both talk about credits, but my hyphothesis is that there are two
credits counters in play. One for traffic either way. This commit
only deals with credits granted by Subsurface to the OSTC to send
data. Credits granted by the OSTC to allow Subsurface to send new
commands is NOT part of this commit, and is seemingly not needed
in our scenario. As we only send new commands to the OSTC when
a previous one is finished (per HW's interface spec), the OSTC
does not run out of credits to receive commands.

Signed-off-by: Jan Mulder <jlmulder@…>

Location:
core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • core/qt-ble.cpp

    r55df597 rb7057c4  
    3131#define IS_G2(_d) same_string((_d)->product, "G2")
    3232
     33#define MAXIMAL_HW_CREDIT       255
     34#define MINIMAL_HW_CREDIT       32
     35
    3336extern "C" {
    3437
     
    6669        if (IS_HW(device)) {
    6770                if (c.uuid() == hwAllCharacteristics[HW_OSTC_BLE_DATA_TX]) {
     71                        hw_credit--;
    6872                        receivedPackets.append(value);
     73                        if (hw_credit == MINIMAL_HW_CREDIT)
     74                                setHwCredit(MAXIMAL_HW_CREDIT - hw_credit);
    6975                } else {
    7076                        qDebug() << "ignore packet from" << c.uuid() << value.toHex();
     
    7379                receivedPackets.append(value);
    7480        }
    75         //qDebug() << ".. incoming packet count" << receivedPackets.length();
    7681}
    7782
     
    8085        if (IS_HW(device)) {
    8186                if (c.uuid() == hwAllCharacteristics[HW_OSTC_BLE_CREDITS_RX]) {
    82                         qDebug() << "HW_OSTC_BLE_CREDITS_RX confirmed" << c.uuid() << value.toHex();
     87                        bool ok;
     88                        hw_credit += value.toHex().toInt(&ok, 16);
    8389                        isCharacteristicWritten = true;
    8490                }
     
    9197void BLEObject::writeCompleted(const QLowEnergyDescriptor &d, const QByteArray &value)
    9298{
    93         Q_UNUSED(value)
    94 
    9599        qDebug() << "BLE write completed on" << d.name() <<  d.value();
    96100}
     
    189193        while (!receivedPackets.isEmpty()) {
    190194                /*
    191                  * Yes, to while loops with same condition seems strange. The inner one
     195                 * Yes, two while loops with same condition seems strange. The inner one
    192196                 * does the real work, but it prevents the QtEventloop to do its thing.
    193197                 * As the incoming packets arrive based on signals and slots, that
     
    218222        }
    219223we_are_done:
     224        return DC_STATUS_SUCCESS;
     225}
     226
     227dc_status_t BLEObject::setHwCredit(unsigned int c)
     228{
     229        /* The Terminal I/O client transmits initial UART credits to the server (see 6.5).
     230         *
     231         * Notice that we have to write to the characteristic here, and not to its
     232         * descriptor as for the enabeling of notifications or indications.
     233         *
     234         * Futher notice that this function has the implicit effect of processing the
     235         * event loop (due to waiting for the confirmation of the credit request).
     236         * So, as characteristcStateChanged will be triggered, while receiving
     237         * data from the OSTC, these are processed too.
     238         */
     239
     240        QList<QLowEnergyCharacteristic> list = preferredService()->characteristics();
     241        isCharacteristicWritten = false;
     242        preferredService()->writeCharacteristic(list[HW_OSTC_BLE_CREDITS_RX],
     243                                                QByteArray(1, c),
     244                                                QLowEnergyService::WriteWithResponse);
     245
     246        /* And wait for the answer*/
     247        int msec = BLE_TIMEOUT;
     248        while (msec > 0 && !isCharacteristicWritten) {
     249                waitFor(100);
     250                msec -= 100;
     251        };
     252        if (!isCharacteristicWritten)
     253                return DC_STATUS_TIMEOUT;
    220254        return DC_STATUS_SUCCESS;
    221255}
     
    253287        preferredService()->writeDescriptor(d, QByteArray::fromHex("0100"));
    254288
    255         /* The Terminal I/O client transmits initial UART credits to the server (see 6.5).
    256          *
    257          * Notice that we have to write to the characteristic here, and not to its
    258          * descriptor as for the enabeling of notifications or indications.
    259          */
    260         isCharacteristicWritten = false;
    261         preferredService()->writeCharacteristic(allC[HW_OSTC_BLE_CREDITS_RX],
    262                                                 QByteArray(1, 255),
    263                                                 QLowEnergyService::WriteWithResponse);
    264 
    265         /* And give to OSTC some time to get initialized */
    266         int msec = BLE_TIMEOUT;
    267         while (msec > 0 && !isCharacteristicWritten) {
    268                 waitFor(100);
    269                 msec -= 100;
    270         };
    271         if (!isCharacteristicWritten)
    272                 return DC_STATUS_TIMEOUT;
    273 
    274         return DC_STATUS_SUCCESS;
     289        /* The Terminal I/O client transmits initial UART credits to the server (see 6.5). */
     290        return setHwCredit(MAXIMAL_HW_CREDIT);
    275291}
    276292
  • core/qt-ble.h

    r3cde4f5 rb7057c4  
    3333        void writeCompleted(const QLowEnergyDescriptor &d, const QByteArray &value);
    3434        dc_status_t setupHwTerminalIo(QList<QLowEnergyCharacteristic>);
     35        dc_status_t setHwCredit(unsigned int c);
    3536private:
    3637        QVector<QLowEnergyService *> services;
     
    4041        bool isCharacteristicWritten;
    4142        dc_user_device_t *device;
     43        unsigned int hw_credit = 0;
    4244
    4345        QList<QUuid> hwAllCharacteristics = {
Note: See TracChangeset for help on using the changeset viewer.