Changeset b9f691a in subsurface


Ignore:
Timestamp:
Jul 11, 2017, 8:32:48 PM (12 days ago)
Author:
Dirk Hohndel <dirk@…>
Branches:
master
Children:
0c5f373e
Parents:
45f36e2 (diff), fbaaa64 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Dirk Hohndel <dirk@…> (07/11/17 20:31:40)
git-committer:
Dirk Hohndel <dirk@…> (07/11/17 20:32:48)
Message:

Merge branch 'hw-o-ble-2' of https://github.com/janmulder/subsurface

Doing a manual merge from the command line in order to retain Jan's SHAs
so that the commit message makes sense...

Signed-off-by: Dirk Hohndel <dirk@…>

Location:
core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • core/qt-ble.cpp

    r55df597 rfbaaa64  
    2828#define IS_HW(_d) same_string((_d)->vendor, "Heinrichs Weikamp")
    2929#define IS_SHEARWATER(_d) same_string((_d)->vendor, "Shearwater")
    30 #define IS_EON_STEEL(_d) same_string((_d)->product, "EON Steel")
    31 #define IS_G2(_d) same_string((_d)->product, "G2")
     30
     31#define MAXIMAL_HW_CREDIT       255
     32#define MINIMAL_HW_CREDIT       32
    3233
    3334extern "C" {
     
    6667        if (IS_HW(device)) {
    6768                if (c.uuid() == hwAllCharacteristics[HW_OSTC_BLE_DATA_TX]) {
     69                        hw_credit--;
    6870                        receivedPackets.append(value);
     71                        if (hw_credit == MINIMAL_HW_CREDIT)
     72                                setHwCredit(MAXIMAL_HW_CREDIT - MINIMAL_HW_CREDIT);
    6973                } else {
    7074                        qDebug() << "ignore packet from" << c.uuid() << value.toHex();
     
    7377                receivedPackets.append(value);
    7478        }
    75         //qDebug() << ".. incoming packet count" << receivedPackets.length();
    7679}
    7780
     
    8083        if (IS_HW(device)) {
    8184                if (c.uuid() == hwAllCharacteristics[HW_OSTC_BLE_CREDITS_RX]) {
    82                         qDebug() << "HW_OSTC_BLE_CREDITS_RX confirmed" << c.uuid() << value.toHex();
     85                        bool ok;
     86                        hw_credit += value.toHex().toInt(&ok, 16);
    8387                        isCharacteristicWritten = true;
    8488                }
     
    9296{
    9397        Q_UNUSED(value)
    94 
    95         qDebug() << "BLE write completed on" << d.name() <<  d.value();
     98        Q_UNUSED(d)
     99        qDebug() << "BLE write completed";
    96100}
    97101
     
    186190                return DC_STATUS_IO;
    187191
    188         int offset = 0;
    189         while (!receivedPackets.isEmpty()) {
    190                 /*
    191                  * Yes, to while loops with same condition seems strange. The inner one
    192                  * does the real work, but it prevents the QtEventloop to do its thing.
    193                  * As the incoming packets arrive based on signals and slots, that
    194                  * stuff is not handeled during the inner loop. So, add a short waitFor
    195                  * between the inner and outer while loop.
    196                  */
    197                 while (!receivedPackets.isEmpty()) {
    198                         QByteArray packet = receivedPackets.takeFirst();
    199 
    200                         if (IS_SHEARWATER(device))
    201                                 packet.remove(0,2);
    202 
    203                         //qDebug() << ".. read (packet.length, contents, size)" << packet.size() << packet.toHex() << size;
    204 
    205                         if ((offset + packet.size()) > size) {
    206                                 qDebug() << "BLE read trouble, receive buffer too small";
    207                                 return DC_STATUS_NOMEMORY;
    208                         }
    209 
    210                         memcpy((char *)data + offset, packet.data(), packet.size());
    211                         offset += packet.size();
    212                         *actual += packet.size();
    213                         // EON Steel wants to read only one packet at a time
    214                         if (IS_EON_STEEL(device) || IS_G2(device))
    215                                 goto we_are_done;
    216                 }
    217                 waitFor(50); // and process some Qt events to see if there is more data coming in.
    218         }
    219 we_are_done:
     192        QByteArray packet = receivedPackets.takeFirst();
     193
     194        if (IS_SHEARWATER(device))
     195                packet.remove(0,2);
     196
     197        if (packet.size() > size)
     198                return DC_STATUS_NOMEMORY;
     199
     200        memcpy((char *)data, packet.data(), packet.size());
     201        *actual += packet.size();
     202
     203        return DC_STATUS_SUCCESS;
     204}
     205
     206dc_status_t BLEObject::setHwCredit(unsigned int c)
     207{
     208        /* The Terminal I/O client transmits initial UART credits to the server (see 6.5).
     209         *
     210         * Notice that we have to write to the characteristic here, and not to its
     211         * descriptor as for the enabeling of notifications or indications.
     212         *
     213         * Futher notice that this function has the implicit effect of processing the
     214         * event loop (due to waiting for the confirmation of the credit request).
     215         * So, as characteristcStateChanged will be triggered, while receiving
     216         * data from the OSTC, these are processed too.
     217         */
     218
     219        QList<QLowEnergyCharacteristic> list = preferredService()->characteristics();
     220        isCharacteristicWritten = false;
     221        preferredService()->writeCharacteristic(list[HW_OSTC_BLE_CREDITS_RX],
     222                                                QByteArray(1, c),
     223                                                QLowEnergyService::WriteWithResponse);
     224
     225        /* And wait for the answer*/
     226        int msec = BLE_TIMEOUT;
     227        while (msec > 0 && !isCharacteristicWritten) {
     228                waitFor(100);
     229                msec -= 100;
     230        };
     231        if (!isCharacteristicWritten)
     232                return DC_STATUS_TIMEOUT;
    220233        return DC_STATUS_SUCCESS;
    221234}
     
    253266        preferredService()->writeDescriptor(d, QByteArray::fromHex("0100"));
    254267
    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;
     268        /* The Terminal I/O client transmits initial UART credits to the server (see 6.5). */
     269        return setHwCredit(MAXIMAL_HW_CREDIT);
    275270}
    276271
  • 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 = {
  • core/qtserialbluetooth.cpp

    rea8e300 r8911281  
    127127        Q_UNUSED(io)
    128128        size_t len;
     129        size_t received = 0;
    129130
    130131        if (buffer.in_pos >= buffer.in_bytes) {
     132                ble_serial_flush_write();
     133        }
     134
     135        /* There is still unused/unread data in the input steam.
     136         * So preseve it at the start of a new read.
     137         */
     138        if (buffer.in_pos > 0) {
     139                len = buffer.in_bytes - buffer.in_pos;
     140                memcpy(buffer.in, buffer.in + buffer.in_pos, len);
     141                buffer.in_pos = 0;
     142                buffer.in_bytes = len;
     143        }
     144
     145        /* Read a long as requested in the size parameter */
     146        while ((buffer.in_bytes - buffer.in_pos) < size) {
    131147                dc_status_t rc;
    132                 size_t received;
    133 
    134                 ble_serial_flush_write();
    135                 rc = ble_serial_ops.packet_read(&ble_serial_ops, buffer.in, sizeof(buffer.in), &received);
     148
     149                rc = ble_serial_ops.packet_read(&ble_serial_ops, buffer.in + buffer.in_bytes,
     150                                                sizeof(buffer.in) - buffer.in_bytes, &received);
    136151                if (rc != DC_STATUS_SUCCESS)
    137152                        return rc;
    138153                if (!received)
    139154                        return DC_STATUS_IO;
    140                 buffer.in_pos = 0;
    141                 buffer.in_bytes = received;
     155
     156                buffer.in_bytes += received;
    142157        }
    143158
Note: See TracChangeset for help on using the changeset viewer.