foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
test_interlock.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the interlock module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 
57 #include "unity.h"
58 #include "MockHL_het.h"
59 #include "Mockdatabase.h"
60 #include "Mockdiag.h"
61 #include "Mockfassert.h"
62 #include "Mockio.h"
63 #include "Mockos.h"
64 
65 #include "interlock_cfg.h"
66 
67 #include "interlock.h"
68 
69 /*========== Definitions and Implementations for Unit Test ==================*/
70 
71 /*========== Setup and Teardown =============================================*/
72 void setUp(void) {
73  /* reset the state of interlock before each test */
74  static ILCK_STATE_s ilck_state = {
75  .timer = 0,
76  .statereq = ILCK_STATE_NO_REQUEST,
78  .substate = ILCK_ENTRY,
80  .lastsubstate = ILCK_ENTRY,
81  .triggerentry = 0,
82  .ErrRequestCounter = 0,
83  .counter = 0,
84  };
86 }
87 
88 void tearDown(void) {
89 }
90 
91 /*========== Test Cases =====================================================*/
92 void testILCK_GetState(void) {
93  /* checks whether GetState returns the state (should be uninitialized when
94  first called) */
95  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
96 }
97 
99  /* test legal value ILCK_STATE_INIT_REQUEST for the state-request */
100  OS_EnterTaskCritical_Expect();
101  OS_ExitTaskCritical_Expect();
103 }
104 
106  /* test legal value ILCK_STATE_NO_REQUEST for the state-request */
107  OS_EnterTaskCritical_Expect();
108  OS_ExitTaskCritical_Expect();
109  /* even though this value is legal, it will return illegal request */
110  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
111 }
112 
114  /* test illegal value INT8_MAX for the state-request */
115  OS_EnterTaskCritical_Expect();
116  OS_ExitTaskCritical_Expect();
117  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
118 }
119 
121  /* run initialization twice, but statemachine not in between */
122  IO_SetPinDirectionToOutput_Ignore();
123  IO_SetPinDirectionToInput_Ignore();
124  IO_PinReset_Ignore();
125  OS_EnterTaskCritical_Expect();
126  OS_ExitTaskCritical_Expect();
128  OS_EnterTaskCritical_Expect();
129  OS_ExitTaskCritical_Expect();
131 }
132 
134  /* run initialization twice and call statemachine between these requests */
135  OS_EnterTaskCritical_Expect();
136  OS_ExitTaskCritical_Expect();
137 
139 
140  /* This group is called by the reentrance check */
141  OS_EnterTaskCritical_Expect();
142  OS_ExitTaskCritical_Expect();
143 
144  /* This group is called by transfer state request */
145  OS_EnterTaskCritical_Expect();
146  OS_ExitTaskCritical_Expect();
147 
148  /* This is the pin initialization */
149  IO_SetPinDirectionToOutput_Ignore();
150  IO_SetPinDirectionToInput_Ignore();
151  IO_PinReset_Ignore();
152 
153  ILCK_Trigger();
154 
155  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_INITIALIZED, ILCK_GetState());
156 
157  OS_EnterTaskCritical_Expect();
158  OS_ExitTaskCritical_Expect();
160 }
161 
163  /* This group is called by the reentrance check */
164  OS_EnterTaskCritical_Expect();
165  OS_ExitTaskCritical_Expect();
166 
167  /* This group is called by transfer state request */
168  OS_EnterTaskCritical_Expect();
169  OS_ExitTaskCritical_Expect();
170 
171  ILCK_Trigger();
172 
173  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
174 }
175 
177  /* run initialization */
178  /* since we are checking only for the statemachine passing through these
179  states, we ignore all unnecessary functions */
180 
181  OS_EnterTaskCritical_Ignore();
182  OS_ExitTaskCritical_Ignore();
183  IO_SetPinDirectionToOutput_Ignore();
184  IO_SetPinDirectionToInput_Ignore();
185  IO_PinReset_Ignore();
186 
187  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
188  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
189  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
190 
192 
193  IO_PinGet_IgnoreAndReturn(STD_PIN_LOW);
194  DIAG_Handler_IgnoreAndReturn(DIAG_HANDLER_RETURN_OK);
195 
196  for (uint8_t i = 0u; i < 10; i++) {
197  /* iterate calling this statemachine 10 times (one shorttime) */
198  ILCK_Trigger();
199  }
200 
201  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_INITIALIZED, ILCK_GetState());
202 }
203 
205  /* test illegal value INT8_MAX for the state-request */
206  OS_EnterTaskCritical_Expect();
207  OS_ExitTaskCritical_Expect();
208  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
209 
210  /* This group is called by the reentrance check */
211  OS_EnterTaskCritical_Expect();
212  OS_ExitTaskCritical_Expect();
213 
214  /* This group is called by transfer state request */
215  OS_EnterTaskCritical_Expect();
216  OS_ExitTaskCritical_Expect();
217 
218  ILCK_Trigger();
219 
220  /* Statemachine should stay uninitialized with illegal state request */
221  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
222 }
223 
225  /* check if on is returned if the pin says on */
226  /* we don't care about these functions in this test */
227  OS_EnterTaskCritical_Ignore();
228  OS_ExitTaskCritical_Ignore();
229 
230  /* set the return value to 1, which means interlock on */
231  IO_PinSet_Ignore();
232  IO_PinReset_Ignore();
233  IO_PinGet_ExpectAndReturn(&ILCK_IO_REG_PORT->DIN, ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE, STD_PIN_LOW);
234  /* gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 1u); */
235  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
236  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
237 
238  TEST_ASSERT_EQUAL(ILCK_SWITCH_ON, TEST_ILCK_GetInterlockFeedback());
239 }
240 
242  /* check if off is returned if the pin says off */
243  /* we don't care about these functions in this test */
244  OS_EnterTaskCritical_Ignore();
245  OS_ExitTaskCritical_Ignore();
246 
247  IO_PinSet_Ignore();
248  IO_PinReset_Ignore();
249  /* set the return value to 0, which means interlock off */
250  IO_PinGet_ExpectAndReturn(&ILCK_IO_REG_PORT->DIN, ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE, STD_PIN_HIGH);
251  /* gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 0u); */
252  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
253  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
254 
255  TEST_ASSERT_EQUAL(ILCK_SWITCH_OFF, TEST_ILCK_GetInterlockFeedback());
256 }
@ DIAG_HANDLER_RETURN_OK
Definition: diag.h:69
@ STD_OK
Definition: fstd_types.h:83
@ STD_PIN_LOW
Definition: fstd_types.h:89
@ STD_PIN_HIGH
Definition: fstd_types.h:90
void TEST_ILCK_SetStateStruct(ILCK_STATE_s state)
Definition: interlock.c:325
static ILCK_STATE_s ilck_state
Definition: interlock.c:93
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:254
void ILCK_Trigger(void)
trigger function for the ILCK driver state machine.
Definition: interlock.c:268
ILCK_ELECTRICAL_STATE_TYPE_e TEST_ILCK_GetInterlockFeedback(void)
Definition: interlock.c:332
ILCK_STATEMACH_e ILCK_GetState(void)
gets the current state.
Definition: interlock.c:250
Headers for the driver for the interlock.
@ ILCK_STATEMACHINE_UNINITIALIZED
Definition: interlock.h:70
@ ILCK_STATEMACHINE_INITIALIZED
Definition: interlock.h:71
@ ILCK_ALREADY_INITIALIZED
Definition: interlock.h:96
@ ILCK_ILLEGAL_REQUEST
Definition: interlock.h:97
@ ILCK_OK
Definition: interlock.h:94
@ ILCK_REQUEST_PENDING
Definition: interlock.h:95
@ ILCK_ENTRY
Definition: interlock.h:79
@ ILCK_STATE_NO_REQUEST
Definition: interlock.h:87
@ ILCK_STATE_INITIALIZATION_REQUEST
Definition: interlock.h:86
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:69
#define ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE
Definition: interlock_cfg.h:75
uint16_t timer
Definition: interlock.h:105
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)