foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
test_can_cbs_rx_command.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_can_cbs_rx_command.c
44  * @author foxBMS Team
45  * @date 2021-07-28 (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 CAN driver callbacks
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockbal.h"
58 #include "Mockbal_cfg.h"
59 #include "Mockbms_cfg.h"
60 #include "Mockcan.h"
61 #include "Mockdatabase.h"
62 #include "Mockdiag.h"
63 #include "Mockfoxmath.h"
64 #include "Mockimd.h"
65 #include "Mockmpu_prototypes.h"
66 #include "Mockos.h"
67 #include "Mocksys_mon.h"
68 
69 #include "database_cfg.h"
70 
71 #include "can_cbs.h"
72 #include "can_helper.h"
73 #include "test_assert_helper.h"
74 
75 TEST_FILE("can_cbs_rx_command.c")
76 
77 /*========== Definitions and Implementations for Unit Test ==================*/
78 
94 
95 QueueHandle_t imd_canDataQueue = NULL_PTR;
96 
99  .pTableCellVoltage = &can_tableCellVoltages,
100  .pTableCellTemperature = &can_tableTemperatures,
101  .pTableMinMax = &can_tableMinimumMaximumValues,
102  .pTableCurrentSensor = &can_tableCurrentSensor,
103  .pTableOpenWire = &can_tableOpenWire,
104  .pTableStateRequest = &can_tableStateRequest,
105  .pTablePackValues = &can_tablePackValues,
106  .pTableSof = &can_tableSof,
107  .pTableSox = &can_tableSox,
108  .pTableErrorState = &can_tableErrorState,
109  .pTableInsulation = &can_tableInsulation,
110  .pTableMsl = &can_tableMslFlags,
111  .pTableRsl = &can_tableRslFlags,
112  .pTableMol = &can_tableMolFlags,
113 };
114 
115 static uint8_t muxId = 0u;
116 
117 /*========== Setup and Teardown =============================================*/
118 void setUp(void) {
123 }
124 
125 void tearDown(void) {
126 }
127 
128 /*========== Test Cases =====================================================*/
129 /** test the handling of illegal input by callback */
131  uint32_t id = 0u;
132  uint8_t dlc = 0u;
133  CAN_ENDIANNESS_e endianness = CAN_LITTLE_ENDIAN;
134  uint8_t canData[CAN_MAX_DLC] = {0};
135  TEST_ASSERT_FAIL_ASSERT(CAN_RxRequest(CAN_MAX_11BIT_ID, dlc, endianness, canData, &can_kShim));
136  TEST_ASSERT_FAIL_ASSERT(CAN_RxRequest(id, (CAN_DLC + 1u), endianness, canData, &can_kShim));
137  TEST_ASSERT_FAIL_ASSERT(CAN_RxRequest(id, dlc, endianness, NULL_PTR, &can_kShim));
138  TEST_ASSERT_FAIL_ASSERT(CAN_RxRequest(id, dlc, endianness, canData, NULL_PTR));
139 }
140 
141 /** test mode request */
143  uint32_t id = 0u;
144  uint8_t dlc = 0u;
145  CAN_ENDIANNESS_e endianness = CAN_BIG_ENDIAN;
146  uint8_t canData[CAN_MAX_DLC] = {0};
147 
148  /* ignore reads that are not used in this test */
149  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
150  BAL_GetInitializationState_IgnoreAndReturn(STD_NOT_OK);
151  BAL_SetBalancingThreshold_Ignore();
152  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
153 
154  /* request disconnect (STANDBY) */
155  canData[0u] = 0u;
156  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
158 
159  /* request discharge (NORMAL) */
160  canData[0u] = 1u;
161  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
163 
164  /* request charge (CHARGE) */
165  canData[0u] = 2u;
166  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
168 
169  /* no valid request */
170  canData[0u] = 3u;
171  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
173 
174  /* state counter overflow */
176  canData[0u] = 0u;
177  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
178  TEST_ASSERT_EQUAL(0u, can_tableStateRequest.stateCounter);
179 
180  /* state: no update without change an no time */
181  canData[0u] = 0u;
182  OS_CheckTimeHasPassed_ExpectAndReturn(0u, 0u, false);
183  OS_CheckTimeHasPassed_IgnoreArg_oldTimeStamp_ms();
184  OS_CheckTimeHasPassed_IgnoreArg_timeToPass_ms();
186  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
187  TEST_ASSERT_EQUAL(42u, can_tableStateRequest.stateRequestViaCanPending);
188 
189  /* state: update with change of time */
190  canData[0u] = 0u;
191  OS_CheckTimeHasPassed_ExpectAndReturn(0u, 0u, true);
192  OS_CheckTimeHasPassed_IgnoreArg_oldTimeStamp_ms();
193  OS_CheckTimeHasPassed_IgnoreArg_timeToPass_ms();
195  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
197 }
198 
199 /** test balancing request */
201  uint32_t id = 0u;
202  uint8_t dlc = 0u;
203  CAN_ENDIANNESS_e endianness = CAN_BIG_ENDIAN;
204  uint8_t canData[CAN_MAX_DLC] = {0};
205 
206  /* ignore reads that are not used in this test */
207  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
208  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
209  OS_CheckTimeHasPassed_IgnoreAndReturn(false);
210 
211  /* request no balancing */
212  canData[1u] = 0u;
213  canData[2u] = 0u;
214  BAL_GetInitializationState_ExpectAndReturn(STD_OK);
215  BAL_SetStateRequest_ExpectAndReturn(BAL_STATE_GLOBAL_DISABLE_REQUEST, BAL_OK);
216  BAL_SetBalancingThreshold_Expect(0u);
217  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
218 
219  /* request balancing */
220  canData[1u] = 1u;
221  canData[2u] = 0x42;
222  BAL_GetInitializationState_ExpectAndReturn(STD_OK);
223  BAL_SetStateRequest_ExpectAndReturn(BAL_STATE_GLOBAL_ENABLE_REQUEST, BAL_OK);
224  BAL_SetBalancingThreshold_Expect(0x42u);
225  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
226 
227  /* balancing not initialized */
228  canData[1u] = 1u;
229  canData[2u] = 0x33u;
230  BAL_GetInitializationState_ExpectAndReturn(STD_NOT_OK);
231  BAL_SetBalancingThreshold_Expect(0x33u);
232  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
233 }
234 
235 /** test reset flags request */
237  uint32_t id = 0u;
238  uint8_t dlc = 0u;
239  CAN_ENDIANNESS_e endianness = CAN_BIG_ENDIAN;
240  uint8_t canData[CAN_MAX_DLC] = {0};
241 
242  /* ignore reads that are not used in this test */
243  DATA_Read1DataBlock_IgnoreAndReturn(STD_OK);
244  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
245  OS_CheckTimeHasPassed_IgnoreAndReturn(false);
246  BAL_SetBalancingThreshold_Ignore();
247  BAL_GetInitializationState_IgnoreAndReturn(STD_NOT_OK);
248 
249  /* request to reset flags */
250  canData[0u] = 4u;
251  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
252  DIAG_Handler_ExpectAndReturn(
254  }
255  SYSM_ClearAllTimingViolations_Expect();
256  CAN_RxRequest(id, dlc, endianness, canData, &can_kShim);
257 }
@ BAL_OK
Definition: bal.h:114
@ BAL_STATE_GLOBAL_ENABLE_REQUEST
Definition: bal.h:106
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:105
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define BMS_REQ_ID_STANDBY
Definition: bms_cfg.h:68
#define BMS_REQ_ID_NOREQ
Definition: bms_cfg.h:65
#define BMS_REQ_ID_CHARGE
Definition: bms_cfg.h:74
#define BMS_REQ_ID_NORMAL
Definition: bms_cfg.h:71
CAN callbacks header.
uint32_t CAN_RxRequest(uint32_t id, uint8_t dlc, CAN_ENDIANNESS_e endianness, const uint8_t *const kpkCanData, const CAN_SHIM_s *const kpkCanShim)
can rx callback function for state requests
CAN_ENDIANNESS_e
Definition: can_cfg.h:298
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:299
@ CAN_BIG_ENDIAN
Definition: can_cfg.h:300
#define CAN_MAX_11BIT_ID
Definition: can_cfg.h:86
#define CAN_DLC
Definition: can_cfg.h:91
#define CAN_MAX_DLC
Definition: can_cfg.h:88
Headers for the helper functions for the CAN module.
Database configuration header.
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:78
@ DATA_BLOCK_ID_SOX
Definition: database_cfg.h:94
@ DATA_BLOCK_ID_RSL_FLAG
Definition: database_cfg.h:92
@ DATA_BLOCK_ID_INSULATION_MONITORING
Definition: database_cfg.h:104
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:76
@ DATA_BLOCK_ID_CURRENT_SENSOR
Definition: database_cfg.h:79
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:84
@ DATA_BLOCK_ID_SOF
Definition: database_cfg.h:89
@ DATA_BLOCK_ID_MSL_FLAG
Definition: database_cfg.h:91
@ DATA_BLOCK_ID_ERRORSTATE
Definition: database_cfg.h:86
@ DATA_BLOCK_ID_PACK_VALUES
Definition: database_cfg.h:105
@ DATA_BLOCK_ID_MOL_FLAG
Definition: database_cfg.h:93
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:77
@ DATA_BLOCK_ID_STATEREQUEST
Definition: database_cfg.h:95
@ DIAG_HANDLER_RETURN_OK
Definition: diag.h:65
@ DIAG_EVENT_OK
Definition: diag_cfg.h:238
@ DIAG_STRING
Definition: diag_cfg.h:252
@ DIAG_ID_DEEP_DISCHARGE_DETECTED
Definition: diag_cfg.h:209
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
OS_QUEUE * pQueueImd
Definition: can_cfg.h:312
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:146
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:129
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:211
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:329
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:119
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:159
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:492
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:450
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:302
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:187
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:472
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:411
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:512
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:532
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
static DATA_BLOCK_CELL_VOLTAGE_s can_tableCellVoltages
static DATA_BLOCK_ERRORSTATE_s can_tableErrorState
static DATA_BLOCK_MSL_FLAG_s can_tableMslFlags
static DATA_BLOCK_MIN_MAX_s can_tableMinimumMaximumValues
static DATA_BLOCK_OPEN_WIRE_s can_tableOpenWire
static DATA_BLOCK_PACK_VALUES_s can_tablePackValues
static uint8_t muxId
void testRxRequestModeRequest(void)
static DATA_BLOCK_MOL_FLAG_s can_tableMolFlags
void testRxRequestBalancingRequest(void)
void setUp(void)
void tearDown(void)
const CAN_SHIM_s can_kShim
static DATA_BLOCK_CELL_TEMPERATURE_s can_tableTemperatures
void testRxRequestResetFlags(void)
static DATA_BLOCK_SOX_s can_tableSox
void testRxRequestIllegalInput(void)
static DATA_BLOCK_RSL_FLAG_s can_tableRslFlags
static DATA_BLOCK_INSULATION_MONITORING_s can_tableInsulation
static DATA_BLOCK_SOF_s can_tableSof
QueueHandle_t imd_canDataQueue
static DATA_BLOCK_STATEREQUEST_s can_tableStateRequest
static DATA_BLOCK_CURRENT_SENSOR_s can_tableCurrentSensor