3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
26 /*! @file td_port.c */
28 #include "queue_utest_common.h"
30 /* C runtime includes. */
37 #include "unity_memory.h"
40 #include "mock_task.h"
41 #include "mock_fake_port.h"
43 /* ============================ GLOBAL VARIABLES =========================== */
45 bool xInCriticalSection = false;
46 uint32_t ulNumEnterCriticalSection = 0;
47 uint32_t ulNumExitCriticalSection = 0;
49 UBaseType_t uxSavedInterruptStatusGlobal = 0;
50 uint32_t ulNumCallsSetInterruptMaskFromISR = 0;
51 uint32_t ulNumCallsClearInterruptMaskFromISR = 0;
53 /* ========================== CALLBACK FUNCTIONS =========================== */
55 static void enterCriticalSectionStub( int cmock_num_calls )
57 /* check that enterCriticalSectionStub was not called twice in a row */
58 TEST_ASSERT_FALSE_MESSAGE( xInCriticalSection, "vFakePortEnterCriticalSection was called twice in a row." );
59 xInCriticalSection = true;
60 ulNumEnterCriticalSection++;
63 static void exitCriticalSectionStub( int cmock_num_calls )
65 /* check that exitCriticalSectionStub was not called twice in a row */
66 TEST_ASSERT_TRUE_MESSAGE( xInCriticalSection, "vFakePortExitCriticalSection was called twice in a row." );
67 xInCriticalSection = false;
68 ulNumExitCriticalSection++;
71 static UBaseType_t portSetInterruptMaskFromISRStub( int cmock_num_calls )
73 ulNumCallsSetInterruptMaskFromISR++;
74 uxSavedInterruptStatusGlobal = getLastMonotonicTestValue() + 241235;
75 xInCriticalSection = true;
76 return uxSavedInterruptStatusGlobal;
79 static void portClearInterruptMaskFromISRStub( UBaseType_t uxSavedInterruptStatus,
82 ulNumCallsClearInterruptMaskFromISR++;
83 TEST_ASSERT_EQUAL_MESSAGE( uxSavedInterruptStatusGlobal, uxSavedInterruptStatus,
84 "Saved interrupt state from call to portClearInterruptMaskFromISR does not match last call to portSetInterruptMaskFromISR." );
85 xInCriticalSection = false;
86 uxSavedInterruptStatusGlobal = 0;
89 /* ============================= Unity Fixtures ============================= */
91 /* ========================== Helper functions =========================== */
93 void td_port_register_stubs( void )
95 /* Track critical section state */
96 xInCriticalSection = false;
97 ulNumEnterCriticalSection = 0;
98 ulNumExitCriticalSection = 0;
100 vFakePortEnterCriticalSection_Stub( &enterCriticalSectionStub );
101 vFakePortExitCriticalSection_Stub( &exitCriticalSectionStub );
103 uxSavedInterruptStatusGlobal = 0;
104 vFakePortClearInterruptMaskFromISR_Stub( &portClearInterruptMaskFromISRStub );
105 ulFakePortSetInterruptMaskFromISR_Stub( &portSetInterruptMaskFromISRStub );
108 BaseType_t td_port_isInCriticalSection( void )
110 return xInCriticalSection;
113 void td_port_teardown_check( void )
115 TEST_ASSERT_EQUAL_MESSAGE( ulNumEnterCriticalSection,
116 ulNumExitCriticalSection,
117 "Number of calls to vFakePortEnterCriticalSection does not match the number of calls to vFakePortExitCriticalSection." );
119 TEST_ASSERT_EQUAL_MESSAGE( 0,
120 uxSavedInterruptStatusGlobal,
121 "uxSavedInterruptStatus was non-zero at the end of the preceeding test case." );