foxBMS - Unit Tests  1.1.0
The foxBMS Unit Tests API Documentation
test_can.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_can.c
44  * @author foxBMS Team
45  * @date 2020-04-01 (date of creation)
46  * @updated 2021-07-23 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Tests for the CAN driver
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "unity.h"
56 #include "MockHL_can.h"
57 #include "Mockcan_cfg.h"
58 #include "Mockdatabase.h"
59 #include "Mockdiag.h"
60 #include "Mockfoxmath.h"
61 #include "Mockftask.h"
62 #include "Mockimd.h"
63 #include "Mockio.h"
64 #include "Mockmcu.h"
65 #include "Mockos.h"
66 #include "Mockqueue.h"
67 #include "Mocktest_can_mpu_prototype_queue_create_stub.h"
68 
69 #include "version_cfg.h"
70 
71 #include "can.h"
72 #include "test_assert_helper.h"
73 
74 /*========== Definitions and Implementations for Unit Test ==================*/
75 /* Dummy for version file implementation */
77  .underVersionControl = true,
78  .isDirty = true,
79  .major = 1,
80  .minor = 1,
81  .patch = 1,
82  .distanceFromLastRelease = 42,
83  .commitHash = "deadbeef",
84  .gitRemote = "onTheDarkSideOfTheMoon.git",
85 };
86 
93 
94 QueueHandle_t imd_canDataQueue = NULL_PTR;
95 
98  .pTableCellVoltage = &can_tableCellVoltages,
99  .pTableCellTemperature = &can_tableTemperatures,
100  .pTableMinMax = &can_tableMinimumMaximumValues,
101  .pTableCurrentSensor = &can_tableCurrentSensor,
102  .pTableOpenWire = &can_tableOpenWire,
103  .pTableStateRequest = &can_tableStateRequest,
104 };
105 
106 static uint32_t can_dummy(
107  uint32_t id,
108  uint8_t dlc,
109  CAN_ENDIANNESS_e endianness,
110  uint8_t *pCanData,
111  uint8_t *pMuxId,
112  const CAN_SHIM_s const *kpkCanShim) {
113  return 0;
114 }
115 
117  {0x001, 8, 100, 0, CAN_LITTLE_ENDIAN, &can_dummy, NULL_PTR},
118 };
119 
121  {0x002, 8, CAN_LITTLE_ENDIAN, &can_dummy},
122 };
123 
124 const uint8_t can_txLength = sizeof(can_txMessages) / sizeof(can_txMessages[0]);
125 const uint8_t can_rxLength = sizeof(can_rxMessages) / sizeof(can_rxMessages[0]);
126 
128 
129 QueueHandle_t ftsk_dataQueue = NULL_PTR;
131 QueueHandle_t ftsk_canRxQueue = NULL_PTR;
132 volatile bool ftsk_allQueuesCreated = false;
133 
134 /*========== Setup and Teardown =============================================*/
135 void setUp(void) {
137 
138  canTestState->periodicEnable = false;
139 
140  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
141  canTestState->currentSensorPresent[stringNumber] = false;
142  canTestState->currentSensorCCPresent[stringNumber] = false;
143  }
144 }
145 
146 void tearDown(void) {
147 }
148 
149 /*========== Test Cases =====================================================*/
151  uint8_t data = 0;
152  canIsTxMessagePending_IgnoreAndReturn(0u);
153  canUpdateID_Ignore();
154  canTransmit_IgnoreAndReturn(0u);
156 }
157 
159  canBASE_t node = {0};
160  canIsTxMessagePending_IgnoreAndReturn(0u);
161  canUpdateID_Ignore();
162  canTransmit_IgnoreAndReturn(0u);
164 }
165 
167  canBASE_t node = {0};
168  uint8_t data = 0;
169 
170  canIsTxMessagePending_IgnoreAndReturn(1u);
171 
172  for (uint8_t i = 0u; i < 32; i++) {
173  TEST_ASSERT_EQUAL(STD_NOT_OK, CAN_DataSend(&node, i, &data));
174  }
175 }
176 
178  canBASE_t node = {0};
179  uint8_t data = 0;
180 
181  /* simulate first messageBox has pending message */
182  canIsTxMessagePending_ExpectAndReturn(&node, 1, 0u);
183  canUpdateID_Expect(&node, 1, 0x20040000u);
184  canTransmit_ExpectAndReturn(&node, 1, &data, 0u);
185  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(&node, 0x001, &data));
186 
187  /* simulate messageBox until the highest to have no pending messages */
188  for (uint8_t messageBox = 1u; messageBox < (CAN_NR_OF_TX_MESSAGE_BOX - 1); messageBox++) {
189  canIsTxMessagePending_ExpectAndReturn(&node, messageBox, 1u);
190  }
191  /* last message box has message pending */
192  canIsTxMessagePending_ExpectAndReturn(&node, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0u);
193  canUpdateID_Expect(&node, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0x20040000u);
194  canTransmit_ExpectAndReturn(&node, CAN_NR_OF_TX_MESSAGE_BOX - 1, &data, 0u);
195  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(&node, 0x001, &data));
196 }
197 
198 void testEnablePeriodic(void) {
199  /* check state before */
200  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
201 
202  CAN_EnablePeriodic(false);
203  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
204 
205  CAN_EnablePeriodic(true);
206  TEST_ASSERT_EQUAL(true, canTestState->periodicEnable);
207 }
208 
210  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
211  /* check state before */
212  TEST_ASSERT_EQUAL(false, canTestState->currentSensorPresent[stringNumber]);
213 
214  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorPresent(stringNumber));
215 
216  /* set state to true */
217  canTestState->currentSensorPresent[stringNumber] = true;
218 
219  /* check state again */
220  TEST_ASSERT_EQUAL(true, canTestState->currentSensorPresent[stringNumber]);
221  }
222 }
223 
225  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
226  /* check state before */
227  TEST_ASSERT_EQUAL(false, canTestState->currentSensorCCPresent[stringNumber]);
228 
229  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorCcPresent(stringNumber));
230 
231  /* set state to true */
232  canTestState->currentSensorCCPresent[stringNumber] = true;
233 
234  /* check state again */
235  TEST_ASSERT_EQUAL(true, canTestState->currentSensorCCPresent[stringNumber]);
236  }
237 }
238 
240 }
#define BS_NR_OF_STRINGS
bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber)
get flag if CC message from current sensor is received.
Definition: can.c:425
CAN_STATE_s * TEST_CAN_GetCANState(void)
Definition: can.c:531
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:211
bool CAN_IsCurrentSensorPresent(uint8_t stringNumber)
set flag for presence of current sensor.
Definition: can.c:420
void CAN_EnablePeriodic(bool command)
enable/disable the periodic transmit/receive.
Definition: can.c:374
Header for the driver for the CAN module.
#define CAN_NR_OF_TX_MESSAGE_BOX
Definition: can.h:75
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:289
enum CAN_ENDIANNESS CAN_ENDIANNESS_e
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:75
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:73
@ DATA_BLOCK_ID_CURRENT_SENSOR
Definition: database_cfg.h:76
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:81
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:74
@ DATA_BLOCK_ID_STATEREQUEST
Definition: database_cfg.h:92
@ STD_NOT_OK
Definition: fstd_types.h:73
@ STD_OK
Definition: fstd_types.h:72
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
QueueHandle_t * pQueueImd
Definition: can_cfg.h:295
Definition: can.h:93
bool periodicEnable
Definition: can.h:94
bool currentSensorCCPresent[BS_NR_OF_STRINGS]
Definition: can.h:96
bool currentSensorPresent[BS_NR_OF_STRINGS]
Definition: can.h:95
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:110
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:136
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:120
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:215
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:163
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:303
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:510
const bool underVersionControl
Definition: version_cfg.h:78
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:87
void testDataSendNoMessagePending(void)
Definition: test_can.c:166
const CAN_MSG_RX_TYPE_s can_rxMessages[]
Definition: test_can.c:120
const VERSION_s foxbmsVersionInfo
Definition: test_can.c:76
static DATA_BLOCK_MIN_MAX_s can_tableMinimumMaximumValues
Definition: test_can.c:89
static DATA_BLOCK_OPEN_WIRE_s can_tableOpenWire
Definition: test_can.c:91
CAN_STATE_s * canTestState
Definition: test_can.c:127
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:106
volatile bool ftsk_allQueuesCreated
Definition: test_can.c:132
const uint8_t can_txLength
Definition: test_can.c:124
void testDataSendMessagePending(void)
Definition: test_can.c:177
const CAN_MSG_TX_TYPE_s can_txMessages[]
Definition: test_can.c:116
void testIsCurrentSensorCcPresent(void)
Definition: test_can.c:224
void setUp(void)
Definition: test_can.c:135
void tearDown(void)
Definition: test_can.c:146
const CAN_SHIM_s can_kShim
Definition: test_can.c:96
static DATA_BLOCK_CELL_TEMPERATURE_s can_tableTemperatures
Definition: test_can.c:88
QueueHandle_t ftsk_dataQueue
Definition: test_can.c:129
void testEnablePeriodic(void)
Definition: test_can.c:198
const uint8_t can_rxLength
Definition: test_can.c:125
void testDataSendNullPointerAsData(void)
Definition: test_can.c:158
void testIsCurrentSensorPresent(void)
Definition: test_can.c:209
QueueHandle_t imd_canDataQueue
Definition: test_can.c:94
void testDataSendNullPointerAsNode(void)
Definition: test_can.c:150
static DATA_BLOCK_STATEREQUEST_s can_tableStateRequest
Definition: test_can.c:92
void testCAN_TransmitBootMessage(void)
Definition: test_can.c:239
QueueHandle_t ftsk_canRxQueue
Definition: test_can.c:131
QueueHandle_t ftsk_imdCanDataQueue
Definition: test_can.c:130
static DATA_BLOCK_CURRENT_SENSOR_s can_tableCurrentSensor
Definition: test_can.c:90
Header file for the version information that is generated by the toolchain.