Skip to content

Commit aba0760

Browse files
authored
Merge pull request #10684 from jarvte/fix_athandler_destr
Cellular: fix ATHandler destructor possible crash on delete
2 parents 34109f0 + 753ba8c commit aba0760

File tree

24 files changed

+254
-27
lines changed

24 files changed

+254
-27
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularbase/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ set(unittest-test-sources
2525
stubs/ATHandler_stub.cpp
2626
stubs/EventQueue_stub.cpp
2727
stubs/FileHandle_stub.cpp
28+
stubs/ConditionVariable_stub.cpp
29+
stubs/Mutex_stub.cpp
2830
)

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ set(unittest-test-sources
4242
stubs/CellularContext_stub.cpp
4343
stubs/CellularUtil_stub.cpp
4444
stubs/SocketAddress_stub.cpp
45+
stubs/ConditionVariable_stub.cpp
46+
stubs/Mutex_stub.cpp
4547
)

UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ set(unittest-test-sources
4343
stubs/SerialBase_stub.cpp
4444
stubs/CellularStateMachine_stub.cpp
4545
stubs/CellularContext_stub.cpp
46+
stubs/ConditionVariable_stub.cpp
47+
stubs/Mutex_stub.cpp
4648
)
4749

4850
# defines

UNITTESTS/features/cellular/framework/AT/at_cellularinformation/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ set(unittest-test-sources
2525
stubs/EventQueue_stub.cpp
2626
stubs/FileHandle_stub.cpp
2727
stubs/mbed_assert_stub.c
28+
stubs/ConditionVariable_stub.cpp
29+
stubs/Mutex_stub.cpp
2830
)

UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ set(unittest-test-sources
2828
stubs/mbed_assert_stub.c
2929
stubs/SocketAddress_stub.cpp
3030
stubs/randLIB_stub.cpp
31+
stubs/ConditionVariable_stub.cpp
32+
stubs/Mutex_stub.cpp
3133
)

UNITTESTS/features/cellular/framework/AT/at_cellularsms/unittest.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ set(unittest-test-sources
2727
stubs/us_ticker_stub.cpp
2828
stubs/mbed_assert_stub.c
2929
stubs/ThisThread_stub.cpp
30+
stubs/mbed_wait_api_stub.cpp
31+
stubs/ConditionVariable_stub.cpp
32+
stubs/Mutex_stub.cpp
3033
)

UNITTESTS/features/cellular/framework/AT/at_cellularstack/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ set(unittest-test-sources
2929
stubs/SocketAddress_stub.cpp
3030
stubs/mbed_assert_stub.c
3131
stubs/ThisThread_stub.cpp
32+
stubs/ConditionVariable_stub.cpp
33+
stubs/Mutex_stub.cpp
3234
)

UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TestATHandler : public testing::Test {
5151
urc_callback_count = 0;
5252
CellularUtil_stub::char_ptr = NULL;
5353
CellularUtil_stub::char_pos = 0;
54+
filehandle_stub_short_value_counter = 0;
5455
}
5556

5657
void TearDown()
@@ -352,6 +353,7 @@ TEST_F(TestATHandler, test_ATHandler_cmd_start)
352353
{
353354
EventQueue que;
354355
FileHandle_stub fh1;
356+
fh1.short_value = 0;
355357

356358
ATHandler at(&fh1, que, 0, ",");
357359
mbed_poll_stub::revents_value = POLLOUT;

UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
# Add test specific include paths
77
set(unittest-includes ${unittest-includes}
8-
features/cellular/framework/common/util
8+
../platform
9+
../features/cellular/framework/common/util
910
../features/cellular/framework/common
1011
../features/cellular/framework/AT
1112
../features/frameworks/mbed-client-randlib/mbed-client-randlib
13+
1214
)
1315

1416
# Source files
@@ -31,6 +33,10 @@ set(unittest-test-sources
3133
stubs/ThisThread_stub.cpp
3234
stubs/randLIB_stub.cpp
3335
stubs/CellularUtil_stub.cpp
36+
stubs/ConditionVariable_stub.cpp
37+
stubs/Mutex_stub.cpp
38+
stubs/mbed_rtos_rtx_stub.c
39+
stubs/rtx_mutex_stub.c
3440
)
3541

3642
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_CELLULAR_DEBUG_AT=true -DOS_STACK_SIZE=2048")

UNITTESTS/features/cellular/framework/device/cellularcontext/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ set(unittest-test-sources
3535
stubs/CellularDevice_stub.cpp
3636
stubs/equeue_stub.c
3737
stubs/ThisThread_stub.cpp
38+
stubs/ConditionVariable_stub.cpp
39+
stubs/Mutex_stub.cpp
3840
)
3941

4042
# defines

UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ set(unittest-test-sources
3333
stubs/NetworkInterface_stub.cpp
3434
stubs/NetworkInterfaceDefaults_stub.cpp
3535
stubs/CellularContext_stub.cpp
36+
stubs/ConditionVariable_stub.cpp
37+
stubs/Mutex_stub.cpp
3638
)
3739

3840
# defines

UNITTESTS/features/cellular/framework/device/cellularstatemachine/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ set(unittest-test-sources
3838
stubs/EventQueue_stub.cpp
3939
stubs/equeue_stub.c
4040
stubs/CellularContext_stub.cpp
41+
stubs/ConditionVariable_stub.cpp
42+
stubs/Mutex_stub.cpp
4143
)
4244

4345
# defines

UNITTESTS/features/lorawan/loramac/unittest.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ set(unittest-test-sources
4040
stubs/LoRaWANTimer_stub.cpp
4141
stubs/LoRaMacCommand_stub.cpp
4242
stubs/EventQueue_stub.cpp
43-
43+
stubs/Mutex_stub.cpp
4444
)
4545

4646
# defines
47+
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_RTOS_PRESENT=1")
48+
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_RTOS_PRESENT=1")
49+
4750
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_ADR_ON=true -DMBED_CONF_LORA_PUBLIC_NETWORK=true -DMBED_CONF_LORA_NB_TRIALS=2 -DMBED_CONF_LORA_DOWNLINK_PREAMBLE_LENGTH=5")
4851
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_ADR_ON=true -DMBED_CONF_LORA_PUBLIC_NETWORK=true -DMBED_CONF_LORA_NB_TRIALS=2 -DMBED_CONF_LORA_DOWNLINK_PREAMBLE_LENGTH=5")
4952

UNITTESTS/features/lorawan/lorawaninterface/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(unittest-test-sources
4141
stubs/LoRaWANTimer_stub.cpp
4242
stubs/LoRaMacCommand_stub.cpp
4343
stubs/LoRaPHYEU868_stub.cpp
44+
stubs/Mutex_stub.cpp
4445
)
4546

4647
# defines

UNITTESTS/features/lorawan/lorawanstack/unittest.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ set(unittest-test-sources
4242
stubs/LoRaMacCommand_stub.cpp
4343
stubs/EventQueue_stub.cpp
4444
stubs/equeue_stub.c
45-
45+
stubs/Mutex_stub.cpp
4646
)
4747

4848
# defines

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ void ATHandler_stub::debug_call_count_clear()
8383

8484
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay) :
8585
_nextATHandler(0),
86+
#if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT
87+
_oobCv(_fileHandleMutex),
88+
#endif
8689
_fileHandle(fh),
8790
_queue(queue),
8891
_ref_count(1),
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef MBED_OS_UNITTESTS_STUBS_CONDITIONVARIABLE_STUB_CPP_
18+
#define MBED_OS_UNITTESTS_STUBS_CONDITIONVARIABLE_STUB_CPP_
19+
20+
#include "ConditionVariable_stub.h"
21+
#include "mbed_error.h"
22+
#include "mbed_assert.h"
23+
24+
bool ConditionVariable_stub::time_out = false;
25+
#ifdef MBED_CONF_RTOS_PRESENT
26+
using namespace rtos;
27+
28+
ConditionVariable::ConditionVariable(Mutex &mutex): _mutex(mutex), _wait_list(0)
29+
{
30+
// No initialization to do
31+
}
32+
33+
ConditionVariable::~ConditionVariable()
34+
{
35+
}
36+
37+
void ConditionVariable::notify_one()
38+
{
39+
40+
}
41+
42+
void ConditionVariable::wait()
43+
{
44+
}
45+
46+
bool ConditionVariable::wait_for(uint32_t millisec)
47+
{
48+
return ConditionVariable_stub::time_out;
49+
}
50+
51+
void ConditionVariable::notify_all()
52+
{
53+
}
54+
#endif
55+
#endif /* MBED_OS_UNITTESTS_STUBS_CONDITIONVARIABLE_STUB_CPP_ */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef MBED_OS_UNITTESTS_STUBS_CONDITIONVARIABLE_STUB_H_
18+
#define MBED_OS_UNITTESTS_STUBS_CONDITIONVARIABLE_STUB_H_
19+
20+
#include "rtos/ConditionVariable.h"
21+
22+
namespace ConditionVariable_stub {
23+
extern bool time_out;
24+
}
25+
26+
27+
#endif /* MBED_OS_UNITTESTS_STUBS_CONDITIONVARIABLE_STUB_H_ */

UNITTESTS/stubs/mbed_rtos_rtx_stub.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "cmsis_os2.h"
19+
20+
osMutexId_t singleton_mutex_id;

UNITTESTS/stubs/rtx_mutex_stub.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "cmsis_os2.h"
19+
20+
osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
21+
{
22+
return osOK;
23+
}
24+
25+
osStatus_t osMutexRelease(osMutexId_t mutex_id)
26+
{
27+
return osOK;
28+
}

UNITTESTS/target_h/cmsis_os2.h

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
#ifndef __CMSIS_OS2_H__
1919
#define __CMSIS_OS2_H__
2020

21+
#ifdef __cplusplus
22+
extern "C"
23+
{
24+
#endif
25+
2126
#include <inttypes.h>
2227

2328
//If conflicts, then remove these, copied from cmsis_os.h
2429
typedef int32_t osStatus;
2530

26-
#define osOK 0
27-
#define osErrorNoMemory -5
28-
2931

3032
//These are from cmsis_os2.h
3133
typedef void *osSemaphoreId_t;
@@ -37,13 +39,27 @@ typedef struct {
3739
uint32_t cb_size; ///< size of provided memory for control block
3840
} osSemaphoreAttr_t;
3941

42+
/// Status code values returned by CMSIS-RTOS functions.
43+
typedef enum {
44+
osOK = 0, ///< Operation completed successfully.
45+
osError = -1, ///< Unspecified RTOS error: run-time error but no other error message fits.
46+
osErrorTimeout = -2, ///< Operation not completed within the timeout period.
47+
osErrorResource = -3, ///< Resource not available.
48+
osErrorParameter = -4, ///< Parameter error.
49+
osErrorNoMemory = -5, ///< System is out of memory: it was impossible to allocate or reserve memory for the operation.
50+
osErrorISR = -6, ///< Not allowed in ISR context: the function cannot be called from interrupt service routines.
51+
osStatusReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
52+
} osStatus_t;
53+
4054
//Thread
4155
typedef enum {
4256
osPriorityNormal = 24 ///< Priority: normal
4357
} osPriority_t;
4458

4559
typedef void *osThreadId_t;
4660

61+
typedef void *osMutexId_t;
62+
4763
typedef void *osEventFlagsId_t;
4864

4965
/// Attributes structure for thread.
@@ -74,5 +90,19 @@ typedef struct {
7490
#define osMutexPrioInherit 0x00000002U ///< Priority inherit protocol.
7591
#define osMutexRobust 0x00000008U ///< Robust mutex.
7692

93+
/// Acquire a Mutex or timeout if it is locked.
94+
/// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
95+
/// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
96+
/// \return status code that indicates the execution status of the function.
97+
osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout);
98+
99+
/// Release a Mutex that was acquired by \ref osMutexAcquire.
100+
/// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
101+
/// \return status code that indicates the execution status of the function.
102+
osStatus_t osMutexRelease(osMutexId_t mutex_id);
103+
104+
#ifdef __cplusplus
105+
}
106+
#endif
77107

78108
#endif

0 commit comments

Comments
 (0)