From e8b7b89d38c190f988e059fd64a23c6d9c051f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Inge=20Hanssen?= Date: Mon, 15 Jan 2018 10:17:28 +0100 Subject: [PATCH] Add fix for spurious condition variable wakeup. Fix typo in packet error encoding. --- src/common/ble_common.cpp | 4 ++-- src/common/transport/h5_transport.cpp | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/common/ble_common.cpp b/src/common/ble_common.cpp index bb86483f8..7453bdb15 100755 --- a/src/common/ble_common.cpp +++ b/src/common/ble_common.cpp @@ -60,8 +60,8 @@ uint32_t encode_decode(adapter_t *adapter, encode_function_t encode_function, de if (_adapter->isInternalError(err_code)) { - error_message << "Not able to decode packet received from target. Code #" << err_code; - _adapter->statusHandler(PKT_DECODE_ERROR, error_message.str().c_str()); + error_message << "Not able to encode packet received from target. Code #" << err_code; + _adapter->statusHandler(PKT_ENCODE_ERROR, error_message.str().c_str()); return NRF_ERROR_INTERNAL; } diff --git a/src/common/transport/h5_transport.cpp b/src/common/transport/h5_transport.cpp index 4c89472cf..816f757e7 100755 --- a/src/common/transport/h5_transport.cpp +++ b/src/common/transport/h5_transport.cpp @@ -227,9 +227,17 @@ uint32_t H5Transport::send(std::vector &data) logPacket(true, h5EncodedPacket); nextTransportLayer->send(lastPacket); + const uint8_t seqNumBefore = seqNum; + auto status = ackWaitCondition.wait_for(ackGuard, std::chrono::milliseconds(retransmissionInterval)); - if (status == std::cv_status::no_timeout) + // Checking for timeout. Also checking against spurios wakeup by making sure the sequence + // number has actually increased. If the sequence number has not increased, we have not + // received an ACK packet, and should not exit the loop (unless timeout). + // Ref. spurious wakeup: + // http://en.cppreference.com/w/cpp/thread/condition_variable + // https://en.wikipedia.org/wiki/Spurious_wakeup + if (status == std::cv_status::no_timeout && seqNum != seqNumBefore) { lastPacket.clear(); return NRF_SUCCESS;