foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
test_can.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.c
44  * @author foxBMS Team
45  * @date 2020-04-01 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.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 /*========== Definitions and Implementations for Unit Test ==================*/
77 /* Dummy for version file implementation */
79  .underVersionControl = true,
80  .isDirty = true,
81  .major = 1,
82  .minor = 1,
83  .patch = 1,
84  .distanceFromLastRelease = 42,
85  .commitHash = "deadbeef",
86  .gitRemote = "onTheDarkSideOfTheMoon.git",
87 };
88 
95 
96 QueueHandle_t imd_canDataQueue = NULL_PTR;
97 
100  .pTableCellVoltage = &can_tableCellVoltages,
101  .pTableCellTemperature = &can_tableTemperatures,
102  .pTableMinMax = &can_tableMinimumMaximumValues,
103  .pTableCurrentSensor = &can_tableCurrentSensor,
104  .pTableOpenWire = &can_tableOpenWire,
105  .pTableStateRequest = &can_tableStateRequest,
106 };
107 
108 static uint32_t can_dummy(
109  uint32_t id,
110  uint8_t dlc,
111  CAN_ENDIANNESS_e endianness,
112  uint8_t *pCanData,
113  uint8_t *pMuxId,
114  const CAN_SHIM_s const *kpkCanShim) {
115  return 0;
116 }
117 
119  {CAN1_NODE, 0x001, 8, 100, 0, CAN_LITTLE_ENDIAN, &can_dummy, NULL_PTR},
120 };
121 
123  {CAN1_NODE, 0x002, 8, CAN_LITTLE_ENDIAN, &can_dummy},
124 };
125 
126 const uint8_t can_txLength = sizeof(can_txMessages) / sizeof(can_txMessages[0]);
127 const uint8_t can_rxLength = sizeof(can_rxMessages) / sizeof(can_rxMessages[0]);
128 
130 
131 QueueHandle_t ftsk_dataQueue = NULL_PTR;
133 QueueHandle_t ftsk_canRxQueue = NULL_PTR;
134 volatile bool ftsk_allQueuesCreated = false;
135 
136 /*========== Setup and Teardown =============================================*/
137 void setUp(void) {
139 
140  canTestState->periodicEnable = false;
141 
142  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
145  }
146 }
147 
148 void tearDown(void) {
149 }
150 
151 /*========== Test Cases =====================================================*/
153  uint8_t data = 0;
154  canIsTxMessagePending_IgnoreAndReturn(0u);
155  canUpdateID_Ignore();
156  canTransmit_IgnoreAndReturn(0u);
158 }
159 
161  canBASE_t node = {0};
162  canIsTxMessagePending_IgnoreAndReturn(0u);
163  canUpdateID_Ignore();
164  canTransmit_IgnoreAndReturn(0u);
166 }
167 
169  canBASE_t *pNode = CAN1_NODE;
170  uint8_t data = 0;
171 
172  canIsTxMessagePending_IgnoreAndReturn(1u);
173 
174  for (uint8_t i = 0u; i < 32; i++) {
175  TEST_ASSERT_EQUAL(STD_NOT_OK, CAN_DataSend(pNode, i, &data));
176  }
177 }
178 
180  canBASE_t *pNode = CAN1_NODE;
181  uint8_t data = 0;
182 
183  /* simulate first messageBox has pending message */
184  canIsTxMessagePending_ExpectAndReturn(pNode, 1, 0u);
185  canUpdateID_Expect(pNode, 1, 0x20040000u);
186  canTransmit_ExpectAndReturn(pNode, 1, &data, 0u);
187  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(pNode, 0x001, &data));
188 
189  /* simulate messageBox until the highest to have no pending messages */
190  for (uint8_t messageBox = 1u; messageBox < (CAN_NR_OF_TX_MESSAGE_BOX - 1); messageBox++) {
191  canIsTxMessagePending_ExpectAndReturn(pNode, messageBox, 1u);
192  }
193  /* last message box has message pending */
194  canIsTxMessagePending_ExpectAndReturn(pNode, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0u);
195  canUpdateID_Expect(pNode, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0x20040000u);
196  canTransmit_ExpectAndReturn(pNode, CAN_NR_OF_TX_MESSAGE_BOX - 1, &data, 0u);
197  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(pNode, 0x001, &data));
198 }
199 
200 void testEnablePeriodic(void) {
201  /* check state before */
202  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
203 
204  CAN_EnablePeriodic(false);
205  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
206 
207  CAN_EnablePeriodic(true);
208  TEST_ASSERT_EQUAL(true, canTestState->periodicEnable);
209 }
210 
212  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
213  /* check state before */
214  TEST_ASSERT_EQUAL(false, canTestState->currentSensorPresent[s]);
215 
216  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorPresent(s));
217 
218  /* set state to true */
220 
221  /* check state again */
222  TEST_ASSERT_EQUAL(true, canTestState->currentSensorPresent[s]);
223  }
224 }
225 
227  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
228  /* check state before */
229  TEST_ASSERT_EQUAL(false, canTestState->currentSensorCCPresent[s]);
230  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorCcPresent(s));
231 
232  /* set state to true */
234 
235  /* check state again */
236  TEST_ASSERT_EQUAL(true, canTestState->currentSensorCCPresent[s]);
237  }
238 }
239 
241 }
242 
243 /**
244  * @brief test #CAN_TransmitDieId()
245  * @details Currently not implemented. Implementing a test for this function
246  * would require implementing a harness that mocks away the
247  * systemREG1 (defined in HAL) as otherwise the test would attempt to
248  * read random memory addresses.
249  * Since the benefit of testing this function on unit level is rather
250  * low (next to no interaction with rest of code base), it should be
251  * tested in the integration test.
252  *
253 */
255 }
#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:418
CAN_STATE_s * TEST_CAN_GetCANState(void)
Definition: can.c:563
STD_RETURN_TYPE_e CAN_DataSend(canBASE_t *pNode, uint32_t id, uint8 *pData)
Sends over CAN the data passed in parameters. This function goes over the messageboxes and marks the ...
Definition: can.c:206
bool CAN_IsCurrentSensorPresent(uint8_t stringNumber)
set flag for presence of current sensor.
Definition: can.c:413
void CAN_EnablePeriodic(bool command)
enable/disable the periodic transmit/receive.
Definition: can.c:367
Header for the driver for the CAN module.
#define CAN_NR_OF_TX_MESSAGE_BOX
Definition: can.h:71
CAN_ENDIANNESS_e
Definition: can_cfg.h:298
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:299
#define CAN1_NODE
Definition: can_cfg.h:71
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:78
@ 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_CELL_TEMPERATURE
Definition: database_cfg.h:77
@ DATA_BLOCK_ID_STATEREQUEST
Definition: database_cfg.h:95
@ 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
bool currentSensorCCPresent[BS_NR_OF_STRINGS]
Definition: can.h:82
bool currentSensorPresent[BS_NR_OF_STRINGS]
Definition: can.h:81
bool periodicEnable
Definition: can.h:80
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_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:302
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:532
const bool underVersionControl
Definition: version_cfg.h:79
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:89
void testDataSendNoMessagePending(void)
Definition: test_can.c:168
const CAN_MSG_RX_TYPE_s can_rxMessages[]
Definition: test_can.c:122
const VERSION_s foxbmsVersionInfo
Definition: test_can.c:78
static DATA_BLOCK_MIN_MAX_s can_tableMinimumMaximumValues
Definition: test_can.c:91
static DATA_BLOCK_OPEN_WIRE_s can_tableOpenWire
Definition: test_can.c:93
CAN_STATE_s * canTestState
Definition: test_can.c:129
static uint32_t can_dummy(uint32_t id, uint8_t dlc, CAN_ENDIANNESS_e endianness, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s const *kpkCanShim)
Definition: test_can.c:108
volatile bool ftsk_allQueuesCreated
Definition: test_can.c:134
const uint8_t can_txLength
Definition: test_can.c:126
void testDataSendMessagePending(void)
Definition: test_can.c:179
const CAN_MSG_TX_TYPE_s can_txMessages[]
Definition: test_can.c:118
void testIsCurrentSensorCcPresent(void)
Definition: test_can.c:226
void testCAN_TransmitDieId(void)
test CAN_TransmitDieId()
Definition: test_can.c:254
void setUp(void)
Definition: test_can.c:137
void tearDown(void)
Definition: test_can.c:148
const CAN_SHIM_s can_kShim
Definition: test_can.c:98
static DATA_BLOCK_CELL_TEMPERATURE_s can_tableTemperatures
Definition: test_can.c:90
QueueHandle_t ftsk_dataQueue
Definition: test_can.c:131
void testEnablePeriodic(void)
Definition: test_can.c:200
const uint8_t can_rxLength
Definition: test_can.c:127
void testDataSendNullPointerAsData(void)
Definition: test_can.c:160
void testIsCurrentSensorPresent(void)
Definition: test_can.c:211
QueueHandle_t imd_canDataQueue
Definition: test_can.c:96
void testDataSendNullPointerAsNode(void)
Definition: test_can.c:152
static DATA_BLOCK_STATEREQUEST_s can_tableStateRequest
Definition: test_can.c:94
void testCAN_TransmitBootMessage(void)
Definition: test_can.c:240
QueueHandle_t ftsk_canRxQueue
Definition: test_can.c:133
QueueHandle_t ftsk_imdCanDataQueue
Definition: test_can.c:132
static DATA_BLOCK_CURRENT_SENSOR_s can_tableCurrentSensor
Definition: test_can.c:92
Header file for the version information that is generated by the toolchain.