foxBMS - Unit Tests  1.1.2
The foxBMS Unit Tests API Documentation
test_interlock.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, 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 2020-04-01 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Tests for the interlock module
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "unity.h"
56 #include "MockHL_gio.h"
57 #include "Mockdatabase.h"
58 #include "Mockdiag.h"
59 #include "Mockfassert.h"
60 #include "Mockos.h"
61 
62 #include "interlock_cfg.h"
63 
64 #include "interlock.h"
65 
66 /*========== Definitions and Implementations for Unit Test ==================*/
67 
68 /*========== Setup and Teardown =============================================*/
69 void setUp(void) {
70  /* reset the state of interlock before each test */
71  static ILCK_STATE_s ilck_state = {
72  .timer = 0,
73  .statereq = ILCK_STATE_NO_REQUEST,
75  .substate = ILCK_ENTRY,
76  .laststate = ILCK_STATEMACH_UNINITIALIZED,
77  .lastsubstate = ILCK_ENTRY,
78  .triggerentry = 0,
79  .ErrRequestCounter = 0,
80  .counter = 0,
81  };
83 }
84 
85 void tearDown(void) {
86 }
87 
88 /*========== Test Cases =====================================================*/
89 void testILCK_GetState(void) {
90  /* checks whether GetState returns the state (should be uninitialized when
91  first called) */
92  TEST_ASSERT_EQUAL(ILCK_STATEMACH_UNINITIALIZED, ILCK_GetState());
93 }
94 
96  /* test legal value ILCK_STATE_INIT_REQUEST for the state-request */
97  OS_EnterTaskCritical_Expect();
98  OS_ExitTaskCritical_Expect();
100 }
101 
103  /* test legal value ILCK_STATE_OPEN_REQUEST for the state-request */
104  OS_EnterTaskCritical_Expect();
105  OS_ExitTaskCritical_Expect();
107 }
108 
110  /* test legal value ILCK_STATE_CLOSE_REQUEST for the state-request */
111  OS_EnterTaskCritical_Expect();
112  OS_ExitTaskCritical_Expect();
114 }
115 
117  /* test legal value ILCK_STATE_ERROR_REQUEST for the state-request */
118  OS_EnterTaskCritical_Expect();
119  OS_ExitTaskCritical_Expect();
121 }
122 
124  /* test legal value ILCK_STATE_NO_REQUEST for the state-request */
125  OS_EnterTaskCritical_Expect();
126  OS_ExitTaskCritical_Expect();
127  /* even though this value is legal, it will return illegal request */
129 }
130 
132  /* test illegal value INT8_MAX for the state-request */
133  OS_EnterTaskCritical_Expect();
134  OS_ExitTaskCritical_Expect();
135  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
136 }
137 
139  /* run initialization twice, but statemachine not in between */
140  OS_EnterTaskCritical_Expect();
141  OS_ExitTaskCritical_Expect();
143  OS_EnterTaskCritical_Expect();
144  OS_ExitTaskCritical_Expect();
146 }
147 
149  /* run initialization twice and call statemachine between these requests */
150  OS_EnterTaskCritical_Expect();
151  OS_ExitTaskCritical_Expect();
153 
154  /* This group is called by the reentrance check */
155  OS_EnterTaskCritical_Expect();
156  OS_ExitTaskCritical_Expect();
157 
158  /* This group is called by transfer state request */
159  OS_EnterTaskCritical_Expect();
160  OS_ExitTaskCritical_Expect();
161 
162  /* This is the pin initialization */
163  gioSetBit_Expect(ILCK_IO_REG, ILCK_INTERLOCK_CONTROL, 1u);
164  gioSetBit_Expect(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 0u);
165 
166  ILCK_Trigger();
167 
168  TEST_ASSERT_EQUAL(ILCK_STATEMACH_INITIALIZATION, ILCK_GetState());
169 
170  OS_EnterTaskCritical_Expect();
171  OS_ExitTaskCritical_Expect();
173 }
174 
176  /* This group is called by the reentrance check */
177  OS_EnterTaskCritical_Expect();
178  OS_ExitTaskCritical_Expect();
179 
180  /* This group is called by transfer state request */
181  OS_EnterTaskCritical_Expect();
182  OS_ExitTaskCritical_Expect();
183 
184  ILCK_Trigger();
185 
186  TEST_ASSERT_EQUAL(ILCK_STATEMACH_UNINITIALIZED, ILCK_GetState());
187 }
188 
190  /* run initialization */
191  /* since we are checking only for the statemachine passing through these
192  states, we ignore all unnecessary functions */
193  OS_EnterTaskCritical_Ignore();
194  OS_ExitTaskCritical_Ignore();
195  gioSetBit_Ignore();
196  gioGetBit_IgnoreAndReturn(0u);
197  DATA_Write_1_DataBlock_IgnoreAndReturn(STD_OK);
198  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
199 
201 
202  for (uint8_t i = 0u; i < 10; i++) {
203  /* iterate calling this statemachine 10 times (one shorttime) */
204  ILCK_Trigger();
205  }
206 
207  TEST_ASSERT_EQUAL(ILCK_STATEMACH_INITIALIZATION, ILCK_GetState());
208 
209  for (uint8_t i = 0u; i < 10; i++) {
210  /* iterate calling this statemachine 10 times (one shorttime) */
211  ILCK_Trigger();
212  }
213 
214  TEST_ASSERT_EQUAL(ILCK_STATEMACH_INITIALIZED, ILCK_GetState());
215 
216  for (uint8_t i = 0u; i < 10; i++) {
217  /* iterate calling this statemachine 10 times (one shorttime) */
218  ILCK_Trigger();
219  }
220 
221  TEST_ASSERT_EQUAL(ILCK_STATEMACH_WAIT_FIRST_REQUEST, ILCK_GetState());
222 
223  for (uint8_t i = 0u; i < 10; i++) {
224  /* iterate calling this statemachine 10 times (one shorttime) */
225  ILCK_Trigger();
226  }
227 
228  /* when we are initialized, the state-machine will stay here */
229  TEST_ASSERT_EQUAL(ILCK_STATEMACH_WAIT_FIRST_REQUEST, ILCK_GetState());
230 }
231 
233  /* test illegal value INT8_MAX for the state-request */
234  OS_EnterTaskCritical_Expect();
235  OS_ExitTaskCritical_Expect();
236  TEST_ASSERT_EQUAL(ILCK_ILLEGAL_REQUEST, ILCK_SetStateRequest(INT8_MAX));
237 
238  /* This group is called by the reentrance check */
239  OS_EnterTaskCritical_Expect();
240  OS_ExitTaskCritical_Expect();
241 
242  /* This group is called by transfer state request */
243  OS_EnterTaskCritical_Expect();
244  OS_ExitTaskCritical_Expect();
245 
246  ILCK_Trigger();
247 
248  /* Statemachine should stay uninitialized with illegal state request */
249  TEST_ASSERT_EQUAL(ILCK_STATEMACH_UNINITIALIZED, ILCK_GetState());
250 }
251 
253  /* check if on is returned if the pin says on */
254  /* we don't care about these functions in this test */
255  OS_EnterTaskCritical_Ignore();
256  OS_ExitTaskCritical_Ignore();
257 
258  /* set the return value to 1, which means interlock on */
259  gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 1u);
260 
261  TEST_ASSERT_EQUAL(ILCK_SWITCH_ON, ILCK_GetInterlockFeedback());
262 }
263 
265  /* check if off is returned if the pin says off */
266  /* we don't care about these functions in this test */
267  OS_EnterTaskCritical_Ignore();
268  OS_ExitTaskCritical_Ignore();
269 
270  /* set the return value to 0, which means interlock off */
271  gioGetBit_ExpectAndReturn(ILCK_IO_REG, ILCK_INTERLOCK_FEEDBACK, 0u);
272 
273  TEST_ASSERT_EQUAL(ILCK_SWITCH_OFF, ILCK_GetInterlockFeedback());
274 }
275 
277  /* ignore port functions and similar */
278  OS_EnterTaskCritical_Ignore();
279  OS_ExitTaskCritical_Ignore();
280  gioSetBit_Ignore();
281  gioGetBit_IgnoreAndReturn(0u);
282  DATA_Write_1_DataBlock_IgnoreAndReturn(STD_OK);
283  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
284 
285  /* configure the statemachine in wait for request and transition to open */
286  static ILCK_STATE_s ilck_state = {
287  .timer = 0,
288  .statereq = ILCK_STATE_NO_REQUEST,
290  .substate = ILCK_ENTRY,
291  .laststate = ILCK_STATEMACH_INITIALIZED,
292  .lastsubstate = ILCK_ENTRY,
293  .triggerentry = 0,
294  .ErrRequestCounter = 0,
295  .counter = 0,
296  };
298 
299  TEST_ASSERT_EQUAL(ILCK_STATEMACH_WAIT_FIRST_REQUEST, ILCK_GetState());
300 
302 
303  for (uint8_t i = 0u; i < 10; i++) {
304  /* iterate calling this statemachine 10 times (one shorttime) */
305  ILCK_Trigger();
306  }
307 
308  /* when we are initialized, the state-machine will stay here */
309  TEST_ASSERT_EQUAL(ILCK_STATEMACH_OPEN, ILCK_GetState());
310 
311  for (uint8_t i = 0u; i < 10; i++) {
312  /* iterate calling this statemachine 10 times (one shorttime) */
313  ILCK_Trigger();
314  }
315 
316  /* check that we stay here */
317  TEST_ASSERT_EQUAL(ILCK_STATEMACH_OPEN, ILCK_GetState());
318 }
319 
321  /* ignore port functions and similar */
322  OS_EnterTaskCritical_Ignore();
323  OS_ExitTaskCritical_Ignore();
324  gioSetBit_Ignore();
325  gioGetBit_IgnoreAndReturn(0u);
326  DATA_Write_1_DataBlock_IgnoreAndReturn(STD_OK);
327  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
328 
329  /* configure the statemachine in wait for request and transition to closed */
330  static ILCK_STATE_s ilck_state = {
331  .timer = 0,
332  .statereq = ILCK_STATE_NO_REQUEST,
334  .substate = ILCK_ENTRY,
335  .laststate = ILCK_STATEMACH_INITIALIZED,
336  .lastsubstate = ILCK_ENTRY,
337  .triggerentry = 0,
338  .ErrRequestCounter = 0,
339  .counter = 0,
340  };
342 
343  TEST_ASSERT_EQUAL(ILCK_STATEMACH_WAIT_FIRST_REQUEST, ILCK_GetState());
344 
346 
347  for (uint8_t i = 0u; i < 10; i++) {
348  /* iterate calling this statemachine 10 times (one shorttime) */
349  ILCK_Trigger();
350  }
351 
352  /* when we are initialized, the state-machine will stay here */
353  TEST_ASSERT_EQUAL(ILCK_STATEMACH_CLOSED, ILCK_GetState());
354 
355  for (uint8_t i = 0u; i < 10; i++) {
356  /* iterate calling this statemachine 10 times (one shorttime) */
357  ILCK_Trigger();
358  }
359 
360  /* check that we stay here */
361  TEST_ASSERT_EQUAL(ILCK_STATEMACH_CLOSED, ILCK_GetState());
362 }
363 
365  /* ignore port functions and similar */
366  OS_EnterTaskCritical_Ignore();
367  OS_ExitTaskCritical_Ignore();
368  gioSetBit_Ignore();
369  gioGetBit_IgnoreAndReturn(0u);
370  DATA_Write_1_DataBlock_IgnoreAndReturn(STD_OK);
371  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
372 
373  /* configure the statemachine in open and transition to closed */
374  static ILCK_STATE_s ilck_state = {
375  .timer = 0,
376  .statereq = ILCK_STATE_NO_REQUEST,
377  .state = ILCK_STATEMACH_OPEN,
378  .substate = ILCK_ENTRY,
380  .lastsubstate = ILCK_ENTRY,
381  .triggerentry = 0,
382  .ErrRequestCounter = 0,
383  .counter = 0,
384  };
386 
387  TEST_ASSERT_EQUAL(ILCK_STATEMACH_OPEN, ILCK_GetState());
388 
390 
391  for (uint8_t i = 0u; i < 10; i++) {
392  /* iterate calling this statemachine 10 times (one shorttime) */
393  ILCK_Trigger();
394  }
395 
396  /* when we are initialized, the state-machine will stay here */
397  TEST_ASSERT_EQUAL(ILCK_STATEMACH_CLOSED, ILCK_GetState());
398 
399  for (uint8_t i = 0u; i < 10; i++) {
400  /* iterate calling this statemachine 10 times (one shorttime) */
401  ILCK_Trigger();
402  }
403 
404  /* check that we stay here */
405  TEST_ASSERT_EQUAL(ILCK_STATEMACH_CLOSED, ILCK_GetState());
406 }
407 
409  /* ignore port functions and similar */
410  OS_EnterTaskCritical_Ignore();
411  OS_ExitTaskCritical_Ignore();
412  gioSetBit_Ignore();
413  gioGetBit_IgnoreAndReturn(0u);
414  DATA_Write_1_DataBlock_IgnoreAndReturn(STD_OK);
415  DIAG_CheckEvent_IgnoreAndReturn(STD_OK);
416 
417  /* configure the statemachine in closed and transition to open */
418  static ILCK_STATE_s ilck_state = {
419  .timer = 0,
420  .statereq = ILCK_STATE_NO_REQUEST,
421  .state = ILCK_STATEMACH_CLOSED,
422  .substate = ILCK_ENTRY,
424  .lastsubstate = ILCK_ENTRY,
425  .triggerentry = 0,
426  .ErrRequestCounter = 0,
427  .counter = 0,
428  };
430 
431  TEST_ASSERT_EQUAL(ILCK_STATEMACH_CLOSED, ILCK_GetState());
432 
434 
435  for (uint8_t i = 0u; i < 10; i++) {
436  /* iterate calling this statemachine 10 times (one shorttime) */
437  ILCK_Trigger();
438  }
439 
440  /* when we are initialized, the state-machine will stay here */
441  TEST_ASSERT_EQUAL(ILCK_STATEMACH_OPEN, ILCK_GetState());
442 
443  for (uint8_t i = 0u; i < 10; i++) {
444  /* iterate calling this statemachine 10 times (one shorttime) */
445  ILCK_Trigger();
446  }
447 
448  /* check that we stay here */
449  TEST_ASSERT_EQUAL(ILCK_STATEMACH_OPEN, ILCK_GetState());
450 }
@ STD_OK
Definition: fstd_types.h:72
void TEST_ILCK_SetStateStruct(ILCK_STATE_s state)
Definition: interlock.c:432
static ILCK_STATE_s ilck_state
Definition: interlock.c:80
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:306
void ILCK_Trigger(void)
trigger function for the ILCK driver state machine.
Definition: interlock.c:320
ILCK_ELECTRICAL_STATE_TYPE_e ILCK_GetInterlockFeedback(void)
Reads the feedback pin of the interlock and returns its current value (ILCK_SWITCH_OFF/ILCK_SWITCH_ON...
Definition: interlock.c:287
ILCK_STATEMACH_e ILCK_GetState(void)
gets the current state.
Definition: interlock.c:302
Headers for the driver for the interlock.
@ ILCK_STATEMACH_UNINITIALIZED
Definition: interlock.h:67
@ ILCK_STATEMACH_OPEN
Definition: interlock.h:71
@ ILCK_STATEMACH_CLOSED
Definition: interlock.h:72
@ ILCK_STATEMACH_INITIALIZED
Definition: interlock.h:69
@ ILCK_STATEMACH_WAIT_FIRST_REQUEST
Definition: interlock.h:70
@ ILCK_STATEMACH_INITIALIZATION
Definition: interlock.h:68
@ ILCK_ALREADY_INITIALIZED
Definition: interlock.h:105
@ ILCK_ILLEGAL_REQUEST
Definition: interlock.h:102
@ ILCK_OK
Definition: interlock.h:99
@ ILCK_REQUEST_PENDING
Definition: interlock.h:101
@ ILCK_ENTRY
Definition: interlock.h:81
@ ILCK_STATE_NO_REQUEST
Definition: interlock.h:92
@ ILCK_STATE_OPEN_REQUEST
Definition: interlock.h:89
@ ILCK_STATE_INIT_REQUEST
Definition: interlock.h:88
@ ILCK_STATE_ERROR_REQUEST
Definition: interlock.h:91
@ ILCK_STATE_CLOSE_REQUEST
Definition: interlock.h:90
Header for the configuration for the driver for the interlock.
#define ILCK_INTERLOCK_FEEDBACK
Definition: interlock_cfg.h:79
@ ILCK_SWITCH_ON
Definition: interlock_cfg.h:97
@ ILCK_SWITCH_OFF
Definition: interlock_cfg.h:96
#define ILCK_INTERLOCK_CONTROL
Definition: interlock_cfg.h:76
#define ILCK_IO_REG
Definition: interlock_cfg.h:65
uint16_t timer
Definition: interlock.h:114
void testInitializeStatemachine(void)
void testILCK_SetStateRequestLegalValuesILCK_STATE_CLOSE_REQUEST(void)
void testStateMachineTransitionClosedToOpen(void)
void testILCK_GetState(void)
void testStateMachineTransitionWaitForRequestToOpen(void)
void testILCK_GetInterlockFeedbackFeedbackOff(void)
void testILCK_SetStateRequestLegalValuesILCK_STATE_INIT_REQUEST(void)
void setUp(void)
void testILCK_SetStateRequestLegalValuesILCK_STATE_ERROR_REQUEST(void)
void tearDown(void)
void testILCK_SetStateRequestLegalValuesILCK_STATE_OPEN_REQUEST(void)
void testILCK_SetStateRequestLegalValuesILCK_STATE_NO_REQUEST(void)
void testILCK_SetStateRequestIllegalValue(void)
void testStateMachineTransitionOpenToClosed(void)
void testILCK_SetStateRequestIllegalValueAndThenRunStatemachine(void)
void testILCK_SetStateRequestDoubleInitialization(void)
void testRunStatemachineWithoutRequest(void)
void testILCK_SetStateRequestDoubleInitializationWithoutStatemachine(void)
void testILCK_GetInterlockFeedbackFeedbackOn(void)
void testStateMachineTransitionWaitForRequestToClosed(void)