Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Fix issue with open close open without transport teardown (#188)
Browse files Browse the repository at this point in the history
* Fix issue with open close open without transport teardown
  • Loading branch information
kenr authored Jan 15, 2019
1 parent 2ee416a commit 75202cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
12 changes: 8 additions & 4 deletions src/common/transport/h5_transport.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016 Nordic Semiconductor ASA
* Copyright (c) 2016-2019 Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -92,7 +92,6 @@ H5Transport::H5Transport(Transport *_nextTransportLayer, const uint32_t retransm
, stateMachineReady(false)
, isOpen(false)
{
setupStateMachine();
}

H5Transport::~H5Transport() noexcept
Expand All @@ -119,14 +118,17 @@ uint32_t H5Transport::open(const status_cb_t &status_callback, const data_cb_t &
return errorCode;
}

if (currentState != STATE_START)
if (!(currentState == STATE_START || currentState == STATE_CLOSED))
{
log(SD_RPC_LOG_FATAL, std::string("Not able to open, current state is not valid"));
std::stringstream ss;
ss << "Not able to open, current state is not valid (" << stateToString(currentState) << ")";
log(SD_RPC_LOG_FATAL, ss.str());
return NRF_ERROR_SD_RPC_H5_TRANSPORT_STATE;
}

// State machine starts in a separate thread.
// Wait for the state machine to be ready
setupStateMachine();
startStateMachine();

lastPacket.clear();
Expand Down Expand Up @@ -885,6 +887,8 @@ void H5Transport::stateMachineWorker()
// Inform interested parties that new current state is set and ready
stateWaitCondition.notify_all();
}

stateMachineReady = false;
}

bool H5Transport::waitForState(h5_state_t state, std::chrono::milliseconds timeout)
Expand Down
18 changes: 9 additions & 9 deletions src/common/transport/uart_boost.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016 Nordic Semiconductor ASA
* Copyright (c) 2016-2019 Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -57,14 +57,7 @@ constexpr uint32_t DUMMY_BAUD_RATE = 9600;

UartBoost::UartBoost(const UartCommunicationParameters &communicationParameters)
: Transport()
, readBuffer()
, writeBufferVector()
, writeQueue()
, queueMutex()
, isOpen(false)
, callbackReadHandle()
, callbackWriteHandle()
, uartSettingsBoost(communicationParameters)
, readBuffer(), isOpen(false), uartSettingsBoost(communicationParameters)
, asyncWriteInProgress(false)
, ioServiceThread(nullptr)
{
Expand Down Expand Up @@ -225,6 +218,12 @@ uint32_t UartBoost::open(const status_cb_t &status_callback, const data_cb_t &da
const auto asioWorker = [&]() {
try
{
// If ioService has ran before it needs to be restarted
if (ioService->stopped())
{
ioService->restart();
}

const auto count = ioService->run();
std::stringstream message;
message << "serial io_context executed " << count << " handlers.";
Expand Down Expand Up @@ -318,6 +317,7 @@ uint32_t UartBoost::close()
ioServiceThread->join();
}

delete ioServiceThread;
ioServiceThread = nullptr;
}

Expand Down
29 changes: 27 additions & 2 deletions test/softdevice_api/testcase_driver_open_close.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
* Copyright (c) 2018-2019 Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -94,7 +94,7 @@ TEST_CASE(CREATE_TEST_NAME_AND_TAGS(driver_open_close,
CHECK(c->close() == NRF_ERROR_INVALID_STATE);
}

SECTION("open_close_open_iterations")
SECTION("open_close_open_with_teardown_iterations")
{
for (uint32_t i = 0; i < numberOfIterations; i++)
{
Expand Down Expand Up @@ -146,4 +146,29 @@ TEST_CASE(CREATE_TEST_NAME_AND_TAGS(driver_open_close,
<< numberOfIterations << " complete.");
}
}

SECTION("open_close_open_iterations")
{
auto c = std::make_shared<testutil::AdapterWrapper>(
testutil::Central, serialPort.port, env.baudRate, env.mtu,
env.retransmissionInterval, env.responseTimeout);

REQUIRE(sd_rpc_log_handler_severity_filter_set(c->unwrap(), env.driverLogLevel) ==
NRF_SUCCESS);

for (uint32_t i = 0; i < numberOfIterations; i++)
{
NRF_LOG("Starting iteration #" << std::dec << static_cast<uint32_t>(i + 1) << " of "
<< numberOfIterations);

REQUIRE(c->open() == NRF_SUCCESS);
REQUIRE(c->configure() == NRF_SUCCESS);
CHECK(c->close() == NRF_SUCCESS);

NRF_LOG("Iteration #" << std::dec << static_cast<uint32_t>(i + 1) << " of "
<< numberOfIterations << " complete.");
}

sd_rpc_adapter_delete(c->unwrap());
}
}

0 comments on commit 75202cf

Please sign in to comment.