From f2b7c663b6ef48dc3e23a17ba210bf1c05a9ec73 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Thu, 29 Aug 2019 16:47:42 -0400 Subject: [PATCH] Cordio: prevent crashes if BLE.begin() is not called --- src/utility/HCICordioTransport.cpp | 13 +++++++++++-- src/utility/HCICordioTransport.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp index 125a6d20..80d2894f 100644 --- a/src/utility/HCICordioTransport.cpp +++ b/src/utility/HCICordioTransport.cpp @@ -150,7 +150,8 @@ static void bleLoop() rtos::Thread bleLoopThread; -HCICordioTransportClass::HCICordioTransportClass() +HCICordioTransportClass::HCICordioTransportClass() : + _begun(false) { } @@ -172,6 +173,8 @@ int HCICordioTransportClass::begin() CordioHCIHook::setDataReceivedHandler(HCICordioTransportClass::onDataReceived); + _begun = true; + return 1; } @@ -180,6 +183,8 @@ void HCICordioTransportClass::end() bleLoopThread.terminate(); CordioHCIHook::getDriver().terminate(); + + _begun = false; } void HCICordioTransportClass::wait(unsigned long timeout) @@ -208,12 +213,16 @@ int HCICordioTransportClass::read() size_t HCICordioTransportClass::write(const uint8_t* data, size_t length) { + if (!_begun) { + return 0; + } + uint8_t packetLength = length - 1; uint8_t packetType = data[0]; #if CORDIO_ZERO_COPY_HCI uint8_t* packet = (uint8_t*)WsfMsgAlloc(max(packetLength, MIN_WSF_ALLOC)); - + memcpy(packet, &data[1], packetLength); return CordioHCIHook::getTransportDriver().write(packetType, packetLength, packet); diff --git a/src/utility/HCICordioTransport.h b/src/utility/HCICordioTransport.h index 27ad492c..b8d0596a 100644 --- a/src/utility/HCICordioTransport.h +++ b/src/utility/HCICordioTransport.h @@ -47,6 +47,7 @@ class HCICordioTransportClass : public HCITransportInterface { void handleRxData(uint8_t* data, uint8_t len); private: + bool _begun; RingBufferN<256> _rxBuf; };