foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
test_interlock.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file test_interlock.c
44  * @author foxBMS Team
45  * @date 2020-04-01 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the interlock module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "general.h"
57 
58 #include "unity.h"
59 #include "MockHL_het.h"
60 #include "Mockdatabase.h"
61 #include "Mockdiag.h"
62 #include "Mockfassert.h"
63 #include "Mockio.h"
64 #include "Mockos.h"
65 
66 #include "interlock_cfg.h"
67 
68 #include "interlock.h"
69 
70 /*========== Definitions and Implementations for Unit Test ==================*/
71 
72 /*========== Setup and Teardown =============================================*/
73 void setUp(void) {
74  /* reset the state of interlock before each test */
75  static ILCK_STATE_s ilck_state = {
76  .timer = 0,
77  .statereq = ILCK_STATE_NO_REQUEST,
79  .substate = ILCK_ENTRY,
81  .lastsubstate = ILCK_ENTRY,
82  .triggerentry = 0,
83  .ErrRequestCounter = 0,
84  .counter = 0,
85  };
87 }
88 
89 void tearDown(void) {
90 }
91 
92 /*========== Test Cases =====================================================*/
93 void testILCK_GetState(void) {
94  /* checks whether GetState returns the state (should be uninitialized when
95  first called) */
96  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
97 }
98 
100  /* test legal value ILCK_STATE_INIT_REQUEST for the state-request */
101  OS_EnterTaskCritical_Expect();
102  OS_ExitTaskCritical_Expect();
104 }
105 
107  /* test legal value ILCK_STATE_NO_REQUEST for the state-request */
108  OS_EnterTaskCritical_Expect();
109  OS_ExitTaskCritical_Expect();
110  /* even though this value is legal, it will return illegal request */
111  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
112 }
113 
115  /* test illegal value INT8_MAX for the state-request */
116  OS_EnterTaskCritical_Expect();
117  OS_ExitTaskCritical_Expect();
118  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
119 }
120 
122  /* run initialization twice, but statemachine not in between */
123  IO_SetPinDirectionToOutput_Ignore();
124  IO_SetPinDirectionToInput_Ignore();
125  IO_PinReset_Ignore();
126  OS_EnterTaskCritical_Expect();
127  OS_ExitTaskCritical_Expect();
129  OS_EnterTaskCritical_Expect();
130  OS_ExitTaskCritical_Expect();
132 }
133 
135  /* run initialization twice and call statemachine between these requests */
136  OS_EnterTaskCritical_Expect();
137  OS_ExitTaskCritical_Expect();
138 
140 
141  /* This group is called by the reentrance check */
142  OS_EnterTaskCritical_Expect();
143  OS_ExitTaskCritical_Expect();
144 
145  /* This group is called by transfer state request */
146  OS_EnterTaskCritical_Expect();
147  OS_ExitTaskCritical_Expect();
148 
149  /* This is the pin initialization */
150  IO_SetPinDirectionToOutput_Ignore();
151  IO_SetPinDirectionToInput_Ignore();
152  IO_PinReset_Ignore();
153 
154  ILCK_Trigger();
155 
156  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_INITIALIZED, ILCK_GetState());
157 
158  OS_EnterTaskCritical_Expect();
159  OS_ExitTaskCritical_Expect();
161 }
162 
164  /* This group is called by the reentrance check */
165  OS_EnterTaskCritical_Expect();
166  OS_ExitTaskCritical_Expect();
167 
168  /* This group is called by transfer state request */
169  OS_EnterTaskCritical_Expect();
170  OS_ExitTaskCritical_Expect();
171 
172  ILCK_Trigger();
173 
174  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
175 }
176 
178  /* run initialization */
179  /* since we are checking only for the statemachine passing through these
180  states, we ignore all unnecessary functions */
181 
182  OS_EnterTaskCritical_Ignore();
183  OS_ExitTaskCritical_Ignore();
184  IO_SetPinDirectionToOutput_Ignore();
185  IO_SetPinDirectionToInput_Ignore();
186  IO_PinReset_Ignore();
187 
188  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
189  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
190  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
191 
193 
194  IO_PinGet_IgnoreAndReturn(STD_PIN_LOW);
195  DIAG_Handler_IgnoreAndReturn(DIAG_HANDLER_RETURN_OK);
196 
197  for (uint8_t i = 0u; i < 10; i++) {
198  /* iterate calling this statemachine 10 times (one shorttime) */
199  ILCK_Trigger();
200  }
201 
202  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_INITIALIZED, ILCK_GetState());
203 }
204 
206  /* test illegal value INT8_MAX for the state-request */
207  OS_EnterTaskCritical_Expect();
208  OS_ExitTaskCritical_Expect();
209  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
210 
211  /* This group is called by the reentrance check */
212  OS_EnterTaskCritical_Expect();
213  OS_ExitTaskCritical_Expect();
214 
215  /* This group is called by transfer state request */
216  OS_EnterTaskCritical_Expect();
217  OS_ExitTaskCritical_Expect();
218 
219  ILCK_Trigger();
220 
221  /* Statemachine should stay uninitialized with illegal state request */
222  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
223 }
224 
226  /* check if on is returned if the pin says on */
227  /* we don't care about these functions in this test */
228  OS_EnterTaskCritical_Ignore();
229  OS_ExitTaskCritical_Ignore();
230 
231  /* set the return value to 1, which means interlock on */
232  IO_PinSet_Ignore();
233  IO_PinReset_Ignore();
234  IO_PinGet_ExpectAndReturn(&ILCK_IO_REG_PORT->DIN, ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE, STD_PIN_LOW);
235  /* gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 1u); */
236  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
237  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
238 
239  TEST_ASSERT_EQUAL(ILCK_SWITCH_ON, TEST_ILCK_GetInterlockFeedback());
240 }
241 
243  /* check if off is returned if the pin says off */
244  /* we don't care about these functions in this test */
245  OS_EnterTaskCritical_Ignore();
246  OS_ExitTaskCritical_Ignore();
247 
248  IO_PinSet_Ignore();
249  IO_PinReset_Ignore();
250  /* set the return value to 0, which means interlock off */
251  IO_PinGet_ExpectAndReturn(&ILCK_IO_REG_PORT->DIN, ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE, STD_PIN_HIGH);
252  /* gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 0u); */
253  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
254  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
255 
256  TEST_ASSERT_EQUAL(ILCK_SWITCH_OFF, TEST_ILCK_GetInterlockFeedback());
257 }
@ DIAG_HANDLER_RETURN_OK
Definition: diag.h:65
@ STD_OK
Definition: fstd_types.h:82
@ STD_PIN_LOW
Definition: fstd_types.h:88
@ STD_PIN_HIGH
Definition: fstd_types.h:89
General macros and definitions for the whole platform.
void TEST_ILCK_SetStateStruct(ILCK_STATE_s state)
Definition: interlock.c:322
static ILCK_STATE_s ilck_state
Definition: interlock.c:90
ILCK_RETURN_TYPE_e ILCK_SetStateRequest(ILCK_STATE_REQUEST_e statereq)
sets the current state request of the state variable ilck_state.
Definition: interlock.c:251
void ILCK_Trigger(void)
trigger function for the ILCK driver state machine.
Definition: interlock.c:265
ILCK_ELECTRICAL_STATE_TYPE_e TEST_ILCK_GetInterlockFeedback(void)
Definition: interlock.c:325
ILCK_STATEMACH_e ILCK_GetState(void)
gets the current state.
Definition: interlock.c:247
Headers for the driver for the interlock.
@ ILCK_STATEMACHINE_UNINITIALIZED
Definition: interlock.h:68
@ ILCK_STATEMACHINE_INITIALIZED
Definition: interlock.h:69
@ ILCK_ALREADY_INITIALIZED
Definition: interlock.h:94
@ ILCK_ILLEGAL_REQUEST
Definition: interlock.h:95
@ ILCK_OK
Definition: interlock.h:92
@ ILCK_REQUEST_PENDING
Definition: interlock.h:93
@ ILCK_ENTRY
Definition: interlock.h:77
@ ILCK_STATE_NO_REQUEST
Definition: interlock.h:85
@ ILCK_STATE_INITIALIZATION_REQUEST
Definition: interlock.h:84
Header for the configuration for the driver for the interlock.
@ ILCK_SWITCH_ON
@ ILCK_SWITCH_OFF
#define ILCK_IO_REG_PORT
Definition: interlock_cfg.h:68
#define ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE
Definition: interlock_cfg.h:74
uint16_t timer
Definition: interlock.h:103
void testInitializeStatemachine(void)
void testILCK_GetState(void)
void testILCK_GetInterlockFeedbackFeedbackOff(void)
void testILCK_SetStateRequestLegalValuesILCK_STATE_INIT_REQUEST(void)
void setUp(void)
void tearDown(void)
void testILCK_SetStateRequestLegalValuesILCK_STATE_NO_REQUEST(void)
void testILCK_SetStateRequestIllegalValue(void)
void testILCK_SetStateRequestIllegalValueAndThenRunStatemachine(void)
void testILCK_SetStateRequestDoubleInitialization(void)
void testRunStatemachineWithoutRequest(void)
void testILCK_SetStateRequestDoubleInitializationWithoutStatemachine(void)
void testILCK_GetInterlockFeedbackFeedbackOn(void)