Skip to content

Commit 2eed8a5

Browse files
committed
Cordio: prevent crashes if BLE.begin() is not called
1 parent 177f387 commit 2eed8a5

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/utility/HCICordioTransport.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ static void bleLoop()
150150
rtos::Thread bleLoopThread;
151151

152152

153-
HCICordioTransportClass::HCICordioTransportClass()
153+
HCICordioTransportClass::HCICordioTransportClass() :
154+
_begun(false)
154155
{
155156
}
156157

@@ -172,6 +173,8 @@ int HCICordioTransportClass::begin()
172173

173174
CordioHCIHook::setDataReceivedHandler(HCICordioTransportClass::onDataReceived);
174175

176+
_begun = true;
177+
175178
return 1;
176179
}
177180

@@ -180,6 +183,8 @@ void HCICordioTransportClass::end()
180183
bleLoopThread.terminate();
181184

182185
CordioHCIHook::getDriver().terminate();
186+
187+
_begun = false;
183188
}
184189

185190
void HCICordioTransportClass::wait(unsigned long timeout)
@@ -208,12 +213,16 @@ int HCICordioTransportClass::read()
208213

209214
size_t HCICordioTransportClass::write(const uint8_t* data, size_t length)
210215
{
216+
if (!_begun) {
217+
return 0;
218+
}
219+
211220
uint8_t packetLength = length - 1;
212221
uint8_t packetType = data[0];
213222

214223
#if CORDIO_ZERO_COPY_HCI
215224
uint8_t* packet = (uint8_t*)WsfMsgAlloc(max(packetLength, MIN_WSF_ALLOC));
216-
225+
217226
memcpy(packet, &data[1], packetLength);
218227

219228
return CordioHCIHook::getTransportDriver().write(packetType, packetLength, packet);

src/utility/HCICordioTransport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class HCICordioTransportClass : public HCITransportInterface {
4747
void handleRxData(uint8_t* data, uint8_t len);
4848

4949
private:
50+
bool _begun;
5051
RingBufferN<256> _rxBuf;
5152
};
5253

0 commit comments

Comments
 (0)