foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
test_can.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_can.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 CAN driver
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "MockHL_can.h"
58 #include "Mockcan_cfg.h"
59 #include "Mockdatabase.h"
60 #include "Mockdiag.h"
61 #include "Mockfoxmath.h"
62 #include "Mockftask.h"
63 #include "Mockimd.h"
64 #include "Mockio.h"
65 #include "Mockmcu.h"
66 #include "Mockos.h"
67 #include "Mockpex.h"
68 #include "Mockqueue.h"
69 #include "Mocktest_can_mpu_prototype_queue_create_stub.h"
70 
71 #include "version_cfg.h"
72 
73 #include "can.h"
74 #include "test_assert_helper.h"
75 
76 #include <stdbool.h>
77 #include <stdint.h>
78 
79 /*========== Definitions and Implementations for Unit Test ==================*/
80 
81 /* see src/app/driver/config/can_cfg_rx-message-definitions.h, but we omit
82  this include here */
83 #define CANRX_NOT_PERIODIC (0u)
84 
85 /* TX test case */
86 #define TEST_CANTX_ID_DUMMY (0x001)
87 #define TEST_CANTX_DUMMY_PERIOD_ms (100)
88 #define TEST_CANTX_DUMMY_PHASE_ms (0)
89 
90 #define TEST_CANTX_DUMMY_MESSAGE \
91  { \
92  .id = TEST_CANTX_ID_DUMMY, \
93  .dlc = CAN_DEFAULT_DLC, \
94  .endianness = CAN_LITTLE_ENDIAN, \
95  .idType = CAN_STANDARD_IDENTIFIER_11_BIT, \
96  }, \
97  { \
98  .period = TEST_CANTX_DUMMY_PERIOD_ms, .phase = TEST_CANTX_DUMMY_PHASE_ms \
99  }
100 
101 /* Rx test case*/
102 #define TEST_CANRX_ID_DUMMY (0x002)
103 
104 #define TEST_CANRX_DUMMY_MESSAGE \
105  { \
106  .id = TEST_CANRX_ID_DUMMY, \
107  .dlc = CAN_DEFAULT_DLC, \
108  .endianness = CAN_LITTLE_ENDIAN, \
109  .idType = CAN_STANDARD_IDENTIFIER_11_BIT, \
110  }, \
111  { \
112  .period = CANRX_NOT_PERIODIC \
113  }
114 
116  .canNodeRegister = canREG1,
117 };
118 
120  .canNodeRegister = canREG2,
121 };
122 
129 
131 
134  .pTableCellVoltage = &can_tableCellVoltages,
135  .pTableCellTemperature = &can_tableTemperatures,
136  .pTableMinMax = &can_tableMinimumMaximumValues,
137  .pTableCurrentSensor = &can_tableCurrentSensor,
138  .pTableOpenWire = &can_tableOpenWire,
139  .pTableStateRequest = &can_tableStateRequest,
140 };
141 
142 static uint32_t TEST_CANTX_DummyCallback(
143  CAN_MESSAGE_PROPERTIES_s message,
144  uint8_t *pCanData,
145  uint8_t *pMuxId,
146  const CAN_SHIM_s *const kpkCanShim) {
147  return 0;
148 }
149 
150 static uint32_t TEST_CANRX_DummyCallback(
151  CAN_MESSAGE_PROPERTIES_s message,
152  const uint8_t *const kpkCanData,
153  const CAN_SHIM_s *const kpkCanShim) {
154  return 0;
155 }
156 
159 };
160 
163 };
164 
165 const uint8_t can_txLength = sizeof(can_txMessages) / sizeof(can_txMessages[0]);
166 const uint8_t can_rxLength = sizeof(can_rxMessages) / sizeof(can_rxMessages[0]);
167 
169 
173 volatile bool ftsk_allQueuesCreated = false;
174 
175 /*========== Setup and Teardown =============================================*/
176 void setUp(void) {
178 
179  canTestState->periodicEnable = false;
180 
181  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
184  }
185 }
186 
187 void tearDown(void) {
188 }
189 
190 /*========== Test Cases =====================================================*/
192  uint8_t data = 0;
193  canIsTxMessagePending_IgnoreAndReturn(0u);
194  canUpdateID_Ignore();
195  canTransmit_IgnoreAndReturn(0u);
197 }
198 
200  CAN_NODE_s node = {0};
201  canIsTxMessagePending_IgnoreAndReturn(0u);
202  canUpdateID_Ignore();
203  canTransmit_IgnoreAndReturn(0u);
205 }
206 
208  CAN_NODE_s *pNode = CAN_NODE_2;
209  uint8_t data = 0;
210 
211  canIsTxMessagePending_IgnoreAndReturn(1u);
212 
213  for (uint8_t i = 0u; i < 32; i++) {
214  TEST_ASSERT_EQUAL(STD_NOT_OK, CAN_DataSend(pNode, i, CAN_STANDARD_IDENTIFIER_11_BIT, &data));
215  }
216 }
217 
219  CAN_NODE_s *pNode = CAN_NODE_1;
220  uint8_t data = 0;
221  CAN_IDENTIFIER_TYPE_e identifierType = CAN_INVALID_TYPE;
222 
223  canIsTxMessagePending_IgnoreAndReturn(1u);
224  TEST_ASSERT_FAIL_ASSERT(CAN_DataSend(pNode, TEST_CANTX_ID_DUMMY, identifierType, &data));
225 }
226 
228  CAN_NODE_s *pNode = CAN_NODE_1;
229  uint8_t data = 0;
230 
231  /* simulate first messageBox has pending message */
232  canIsTxMessagePending_ExpectAndReturn(pNode->canNodeRegister, 1, 0u);
233  canUpdateID_Expect(pNode->canNodeRegister, 1, 0x20040000u);
234  canTransmit_ExpectAndReturn(pNode->canNodeRegister, 1, &data, 0u);
235  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(pNode, 0x001, CAN_STANDARD_IDENTIFIER_11_BIT, &data));
236 
237  /* simulate messageBox until the highest to have no pending messages */
238  for (uint8_t messageBox = 1u; messageBox < (CAN_NR_OF_TX_MESSAGE_BOX - 1); messageBox++) {
239  canIsTxMessagePending_ExpectAndReturn(pNode->canNodeRegister, messageBox, 1u);
240  }
241  /* last message box has message pending */
242  canIsTxMessagePending_ExpectAndReturn(pNode->canNodeRegister, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0u);
243  canUpdateID_Expect(pNode->canNodeRegister, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0x20040000u);
244  canTransmit_ExpectAndReturn(pNode->canNodeRegister, CAN_NR_OF_TX_MESSAGE_BOX - 1, &data, 0u);
245  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(pNode, 0x001, CAN_STANDARD_IDENTIFIER_11_BIT, &data));
246 }
247 
248 void testEnablePeriodic(void) {
249  /* check state before */
250  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
251 
252  CAN_EnablePeriodic(false);
253  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
254 
255  CAN_EnablePeriodic(true);
256  TEST_ASSERT_EQUAL(true, canTestState->periodicEnable);
257 }
258 
260  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
261  /* check state before */
262  TEST_ASSERT_EQUAL(false, canTestState->currentSensorPresent[s]);
263 
264  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorPresent(s));
265 
266  /* set state to true */
268 
269  /* check state again */
270  TEST_ASSERT_EQUAL(true, canTestState->currentSensorPresent[s]);
271  }
272 }
273 
275  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
276  /* check state before */
277  TEST_ASSERT_EQUAL(false, canTestState->currentSensorCCPresent[s]);
278  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorCcPresent(s));
279 
280  /* set state to true */
282 
283  /* check state again */
284  TEST_ASSERT_EQUAL(true, canTestState->currentSensorCCPresent[s]);
285  }
286 }
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber)
get flag if CC message from current sensor is received.
Definition: can.c:862
CAN_STATE_s * TEST_CAN_GetCANState(void)
Definition: can.c:890
bool CAN_IsCurrentSensorPresent(uint8_t stringNumber)
set flag for presence of current sensor.
Definition: can.c:857
STD_RETURN_TYPE_e CAN_DataSend(CAN_NODE_s *pNode, uint32_t id, CAN_IDENTIFIER_TYPE_e idType, uint8 *pData)
Sends over CAN the data passed in parameters. This function goes over the message boxes and marks the...
Definition: can.c:781
void CAN_EnablePeriodic(bool command)
Enables periodic sending per CAN. This is used to prevent sending uninitialized data per CAN (e....
Definition: can.c:849
Header for the driver for the CAN module.
#define CAN_NR_OF_TX_MESSAGE_BOX
Definition: can.h:74
#define CAN_NODE_2
Definition: can_cfg.h:81
#define CAN_NODE_1
Definition: can_cfg.h:80
CAN_IDENTIFIER_TYPE_e
Definition: can_cfg.h:162
@ CAN_INVALID_TYPE
Definition: can_cfg.h:165
@ CAN_STANDARD_IDENTIFIER_11_BIT
Definition: can_cfg.h:163
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:81
@ DATA_BLOCK_ID_STATE_REQUEST
Definition: database_cfg.h:98
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:79
@ DATA_BLOCK_ID_CURRENT_SENSOR
Definition: database_cfg.h:82
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:87
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:80
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
canBASE_t * canNodeRegister
Definition: can_cfg.h:76
OS_QUEUE * pQueueImd
Definition: can_cfg.h:178
bool currentSensorCCPresent[BS_NR_OF_STRINGS]
Definition: can.h:85
bool currentSensorPresent[BS_NR_OF_STRINGS]
Definition: can.h:84
bool periodicEnable
Definition: can.h:83
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:149
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:132
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:214
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:122
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:162
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:305
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:530
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
Definition: test_can.c:123
#define TEST_CANTX_ID_DUMMY
Definition: test_can.c:86
const CAN_RX_MESSAGE_TYPE_s can_rxMessages[]
Definition: test_can.c:161
void testDataSendNoMessagePending(void)
Definition: test_can.c:207
#define TEST_CANRX_DUMMY_MESSAGE
Definition: test_can.c:104
OS_QUEUE ftsk_imdCanDataQueue
Definition: test_can.c:171
static DATA_BLOCK_MIN_MAX_s can_tableMinimumMaximumValues
Definition: test_can.c:125
static DATA_BLOCK_OPEN_WIRE_s can_tableOpenWire
Definition: test_can.c:127
CAN_STATE_s * canTestState
Definition: test_can.c:168
volatile bool ftsk_allQueuesCreated
Definition: test_can.c:173
const uint8_t can_txLength
Definition: test_can.c:165
void testDataSendMessagePending(void)
Definition: test_can.c:227
void testDataSendInvalidIdentifierType(void)
Definition: test_can.c:218
void testIsCurrentSensorCcPresent(void)
Definition: test_can.c:274
void setUp(void)
Definition: test_can.c:176
void tearDown(void)
Definition: test_can.c:187
const CAN_TX_MESSAGE_TYPE_s can_txMessages[]
Definition: test_can.c:157
const CAN_SHIM_s can_kShim
Definition: test_can.c:132
static DATA_BLOCK_CELL_TEMPERATURE_s can_tableTemperatures
Definition: test_can.c:124
const CAN_NODE_s can_node1
Definition: test_can.c:115
void testEnablePeriodic(void)
Definition: test_can.c:248
static uint32_t TEST_CANTX_DummyCallback(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
Definition: test_can.c:142
static uint32_t TEST_CANRX_DummyCallback(CAN_MESSAGE_PROPERTIES_s message, const uint8_t *const kpkCanData, const CAN_SHIM_s *const kpkCanShim)
Definition: test_can.c:150
const uint8_t can_rxLength
Definition: test_can.c:166
static DATA_BLOCK_STATE_REQUEST_s can_tableStateRequest
Definition: test_can.c:128
void testDataSendNullPointerAsData(void)
Definition: test_can.c:199
#define TEST_CANTX_DUMMY_MESSAGE
Definition: test_can.c:90
OS_QUEUE ftsk_canRxQueue
Definition: test_can.c:172
void testIsCurrentSensorPresent(void)
Definition: test_can.c:259
const CAN_NODE_s can_node2Isolated
Definition: test_can.c:119
void testDataSendNullPointerAsNode(void)
Definition: test_can.c:191
OS_QUEUE ftsk_dataQueue
Definition: test_can.c:170
static DATA_BLOCK_CURRENT_SENSOR_s can_tableCurrentSensor
Definition: test_can.c:126
OS_QUEUE imd_canDataQueue
Definition: test_can.c:130
Header file for the version information that is generated by the toolchain.