foxBMS - Unit Tests  1.6.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-10-12 (date of last update)
47  * @version v1.6.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 /*========== Unit Testing Framework Directives ==============================*/
70 TEST_INCLUDE_PATH("../../src/app/driver/config")
71 TEST_INCLUDE_PATH("../../src/app/driver/interlock")
72 TEST_INCLUDE_PATH("../../src/app/driver/io")
73 TEST_INCLUDE_PATH("../../src/app/engine/diag")
74 
75 /*========== Definitions and Implementations for Unit Test ==================*/
76 
77 /*========== Setup and Teardown =============================================*/
78 void setUp(void) {
79  /* reset the state of interlock before each test */
80  static ILCK_STATE_s ilck_state = {
81  .timer = 0,
82  .statereq = ILCK_STATE_NO_REQUEST,
84  .substate = ILCK_ENTRY,
86  .lastsubstate = ILCK_ENTRY,
87  .triggerentry = 0,
88  .ErrRequestCounter = 0,
89  .counter = 0,
90  };
92 }
93 
94 void tearDown(void) {
95 }
96 
97 /*========== Test Cases =====================================================*/
98 void testILCK_GetState(void) {
99  /* checks whether GetState returns the state (should be uninitialized when
100  first called) */
101  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
102 }
103 
105  /* test legal value ILCK_STATE_INIT_REQUEST for the state-request */
106  OS_EnterTaskCritical_Expect();
107  OS_ExitTaskCritical_Expect();
109 }
110 
112  /* test legal value ILCK_STATE_NO_REQUEST for the state-request */
113  OS_EnterTaskCritical_Expect();
114  OS_ExitTaskCritical_Expect();
115  /* even though this value is legal, it will return illegal request */
116  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
117 }
118 
120  /* test illegal value INT8_MAX for the state-request */
121  OS_EnterTaskCritical_Expect();
122  OS_ExitTaskCritical_Expect();
123  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
124 }
125 
127  /* run initialization twice, but state machine not in between */
128  IO_SetPinDirectionToOutput_Ignore();
129  IO_SetPinDirectionToInput_Ignore();
130  IO_PinReset_Ignore();
131  OS_EnterTaskCritical_Expect();
132  OS_ExitTaskCritical_Expect();
134  OS_EnterTaskCritical_Expect();
135  OS_ExitTaskCritical_Expect();
137 }
138 
140  /* run initialization twice and call state machine between these requests */
141  OS_EnterTaskCritical_Expect();
142  OS_ExitTaskCritical_Expect();
143 
145 
146  /* This group is called by the reentrance check */
147  OS_EnterTaskCritical_Expect();
148  OS_ExitTaskCritical_Expect();
149 
150  /* This group is called by transfer state request */
151  OS_EnterTaskCritical_Expect();
152  OS_ExitTaskCritical_Expect();
153 
154  /* This is the pin initialization */
155  IO_SetPinDirectionToOutput_Ignore();
156  IO_SetPinDirectionToInput_Ignore();
157  IO_PinReset_Ignore();
158 
159  ILCK_Trigger();
160 
161  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_INITIALIZED, ILCK_GetState());
162 
163  OS_EnterTaskCritical_Expect();
164  OS_ExitTaskCritical_Expect();
166 }
167 
169  /* This group is called by the reentrance check */
170  OS_EnterTaskCritical_Expect();
171  OS_ExitTaskCritical_Expect();
172 
173  /* This group is called by transfer state request */
174  OS_EnterTaskCritical_Expect();
175  OS_ExitTaskCritical_Expect();
176 
177  ILCK_Trigger();
178 
179  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
180 }
181 
183  /* run initialization */
184  /* since we are checking only for the state machine passing through these
185  states, we ignore all unnecessary functions */
186 
187  OS_EnterTaskCritical_Ignore();
188  OS_ExitTaskCritical_Ignore();
189  IO_SetPinDirectionToOutput_Ignore();
190  IO_SetPinDirectionToInput_Ignore();
191  IO_PinReset_Ignore();
192 
193  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
194  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
195  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
196 
198 
199  IO_PinGet_IgnoreAndReturn(STD_PIN_LOW);
200  DIAG_Handler_IgnoreAndReturn(DIAG_HANDLER_RETURN_OK);
201 
202  for (uint8_t i = 0u; i < 10; i++) {
203  /* iterate calling this state machine 10 times (one shorttime) */
204  ILCK_Trigger();
205  }
206 
207  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_INITIALIZED, ILCK_GetState());
208 }
209 
211  /* test illegal value INT8_MAX for the state-request */
212  OS_EnterTaskCritical_Expect();
213  OS_ExitTaskCritical_Expect();
214  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
215 
216  /* This group is called by the reentrance check */
217  OS_EnterTaskCritical_Expect();
218  OS_ExitTaskCritical_Expect();
219 
220  /* This group is called by transfer state request */
221  OS_EnterTaskCritical_Expect();
222  OS_ExitTaskCritical_Expect();
223 
224  ILCK_Trigger();
225 
226  /* Statemachine should stay uninitialized with illegal state request */
227  TEST_ASSERT_EQUAL(ILCK_STATEMACHINE_UNINITIALIZED, ILCK_GetState());
228 }
229 
231  /* check if on is returned if the pin says on */
232  /* we don't care about these functions in this test */
233  OS_EnterTaskCritical_Ignore();
234  OS_ExitTaskCritical_Ignore();
235 
236  /* set the return value to 1, which means interlock on */
237  IO_PinSet_Ignore();
238  IO_PinReset_Ignore();
239  IO_PinGet_ExpectAndReturn(&ILCK_IO_REG_PORT->DIN, ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE, STD_PIN_LOW);
240  /* gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 1u); */
241  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
242  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
243 
244  TEST_ASSERT_EQUAL(ILCK_SWITCH_ON, TEST_ILCK_GetInterlockFeedback());
245 }
246 
248  /* check if off is returned if the pin says off */
249  /* we don't care about these functions in this test */
250  OS_EnterTaskCritical_Ignore();
251  OS_ExitTaskCritical_Ignore();
252 
253  IO_PinSet_Ignore();
254  IO_PinReset_Ignore();
255  /* set the return value to 0, which means interlock off */
256  IO_PinGet_ExpectAndReturn(&ILCK_IO_REG_PORT->DIN, ILCK_INTERLOCK_FEEDBACK_PIN_IL_STATE, STD_PIN_HIGH);
257  /* gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 0u); */
258  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
259  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
260 
261  TEST_ASSERT_EQUAL(ILCK_SWITCH_OFF, TEST_ILCK_GetInterlockFeedback());
262 }
static DIAG_DIAGNOSIS_STATE_s diag
Definition: diag.c:71
@ 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)