foxBMS - Unit Tests  1.2.1
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 "Mockpex.h"
67 #include "Mockqueue.h"
68 #include "Mocktest_can_mpu_prototype_queue_create_stub.h"
69 
70 #include "version_cfg.h"
71 
72 #include "can.h"
73 #include "test_assert_helper.h"
74 
75 /*========== Definitions and Implementations for Unit Test ==================*/
76 /* Dummy for version file implementation */
78  .underVersionControl = true,
79  .isDirty = true,
80  .major = 1,
81  .minor = 1,
82  .patch = 1,
83  .distanceFromLastRelease = 42,
84  .commitHash = "deadbeef",
85  .gitRemote = "onTheDarkSideOfTheMoon.git",
86 };
87 
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 };
106 
107 static uint32_t can_dummy(
108  uint32_t id,
109  uint8_t dlc,
110  CAN_ENDIANNESS_e endianness,
111  uint8_t *pCanData,
112  uint8_t *pMuxId,
113  const CAN_SHIM_s const *kpkCanShim) {
114  return 0;
115 }
116 
118  {CAN1_NODE, 0x001, 8, 100, 0, CAN_LITTLE_ENDIAN, &can_dummy, NULL_PTR},
119 };
120 
122  {CAN1_NODE, 0x002, 8, CAN_LITTLE_ENDIAN, &can_dummy},
123 };
124 
125 const uint8_t can_txLength = sizeof(can_txMessages) / sizeof(can_txMessages[0]);
126 const uint8_t can_rxLength = sizeof(can_rxMessages) / sizeof(can_rxMessages[0]);
127 
129 
130 QueueHandle_t ftsk_dataQueue = NULL_PTR;
132 QueueHandle_t ftsk_canRxQueue = NULL_PTR;
133 volatile bool ftsk_allQueuesCreated = false;
134 
135 /*========== Setup and Teardown =============================================*/
136 void setUp(void) {
138 
139  canTestState->periodicEnable = false;
140 
141  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
142  canTestState->currentSensorPresent[stringNumber] = false;
143  canTestState->currentSensorCCPresent[stringNumber] = false;
144  }
145 }
146 
147 void tearDown(void) {
148 }
149 
150 /*========== Test Cases =====================================================*/
152  uint8_t data = 0;
153  canIsTxMessagePending_IgnoreAndReturn(0u);
154  canUpdateID_Ignore();
155  canTransmit_IgnoreAndReturn(0u);
157 }
158 
160  canBASE_t node = {0};
161  canIsTxMessagePending_IgnoreAndReturn(0u);
162  canUpdateID_Ignore();
163  canTransmit_IgnoreAndReturn(0u);
165 }
166 
168  canBASE_t *pNode = CAN1_NODE;
169  uint8_t data = 0;
170 
171  canIsTxMessagePending_IgnoreAndReturn(1u);
172 
173  for (uint8_t i = 0u; i < 32; i++) {
174  TEST_ASSERT_EQUAL(STD_NOT_OK, CAN_DataSend(pNode, i, &data));
175  }
176 }
177 
179  canBASE_t *pNode = CAN1_NODE;
180  uint8_t data = 0;
181 
182  /* simulate first messageBox has pending message */
183  canIsTxMessagePending_ExpectAndReturn(pNode, 1, 0u);
184  canUpdateID_Expect(pNode, 1, 0x20040000u);
185  canTransmit_ExpectAndReturn(pNode, 1, &data, 0u);
186  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(pNode, 0x001, &data));
187 
188  /* simulate messageBox until the highest to have no pending messages */
189  for (uint8_t messageBox = 1u; messageBox < (CAN_NR_OF_TX_MESSAGE_BOX - 1); messageBox++) {
190  canIsTxMessagePending_ExpectAndReturn(pNode, messageBox, 1u);
191  }
192  /* last message box has message pending */
193  canIsTxMessagePending_ExpectAndReturn(pNode, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0u);
194  canUpdateID_Expect(pNode, CAN_NR_OF_TX_MESSAGE_BOX - 1, 0x20040000u);
195  canTransmit_ExpectAndReturn(pNode, CAN_NR_OF_TX_MESSAGE_BOX - 1, &data, 0u);
196  TEST_ASSERT_EQUAL(STD_OK, CAN_DataSend(pNode, 0x001, &data));
197 }
198 
199 void testEnablePeriodic(void) {
200  /* check state before */
201  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
202 
203  CAN_EnablePeriodic(false);
204  TEST_ASSERT_EQUAL(false, canTestState->periodicEnable);
205 
206  CAN_EnablePeriodic(true);
207  TEST_ASSERT_EQUAL(true, canTestState->periodicEnable);
208 }
209 
211  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
212  /* check state before */
213  TEST_ASSERT_EQUAL(false, canTestState->currentSensorPresent[stringNumber]);
214 
215  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorPresent(stringNumber));
216 
217  /* set state to true */
218  canTestState->currentSensorPresent[stringNumber] = true;
219 
220  /* check state again */
221  TEST_ASSERT_EQUAL(true, canTestState->currentSensorPresent[stringNumber]);
222  }
223 }
224 
226  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
227  /* check state before */
228  TEST_ASSERT_EQUAL(false, canTestState->currentSensorCCPresent[stringNumber]);
229 
230  TEST_ASSERT_EQUAL(false, CAN_IsCurrentSensorCcPresent(stringNumber));
231 
232  /* set state to true */
233  canTestState->currentSensorCCPresent[stringNumber] = true;
234 
235  /* check state again */
236  TEST_ASSERT_EQUAL(true, canTestState->currentSensorCCPresent[stringNumber]);
237  }
238 }
239 
241 }
#define BS_NR_OF_STRINGS
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: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:205
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:70
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:291
enum CAN_ENDIANNESS CAN_ENDIANNESS_e
#define CAN1_NODE
Definition: can_cfg.h:70
@ 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:82
@ STD_OK
Definition: fstd_types.h:81
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
OS_QUEUE * pQueueImd
Definition: can_cfg.h:304
Definition: can.h:78
bool periodicEnable
Definition: can.h:79
bool currentSensorCCPresent[BS_NR_OF_STRINGS]
Definition: can.h:81
bool currentSensorPresent[BS_NR_OF_STRINGS]
Definition: can.h:80
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:111
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:137
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:121
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:202
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:150
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:290
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:502
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:88
void testDataSendNoMessagePending(void)
Definition: test_can.c:167
const CAN_MSG_RX_TYPE_s can_rxMessages[]
Definition: test_can.c:121
const VERSION_s foxbmsVersionInfo
Definition: test_can.c:77
static DATA_BLOCK_MIN_MAX_s can_tableMinimumMaximumValues
Definition: test_can.c:90
static DATA_BLOCK_OPEN_WIRE_s can_tableOpenWire
Definition: test_can.c:92
CAN_STATE_s * canTestState
Definition: test_can.c:128
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:107
volatile bool ftsk_allQueuesCreated
Definition: test_can.c:133
const uint8_t can_txLength
Definition: test_can.c:125
void testDataSendMessagePending(void)
Definition: test_can.c:178
const CAN_MSG_TX_TYPE_s can_txMessages[]
Definition: test_can.c:117
void testIsCurrentSensorCcPresent(void)
Definition: test_can.c:225
void setUp(void)
Definition: test_can.c:136
void tearDown(void)
Definition: test_can.c:147
const CAN_SHIM_s can_kShim
Definition: test_can.c:97
static DATA_BLOCK_CELL_TEMPERATURE_s can_tableTemperatures
Definition: test_can.c:89
QueueHandle_t ftsk_dataQueue
Definition: test_can.c:130
void testEnablePeriodic(void)
Definition: test_can.c:199
const uint8_t can_rxLength
Definition: test_can.c:126
void testDataSendNullPointerAsData(void)
Definition: test_can.c:159
void testIsCurrentSensorPresent(void)
Definition: test_can.c:210
QueueHandle_t imd_canDataQueue
Definition: test_can.c:95
void testDataSendNullPointerAsNode(void)
Definition: test_can.c:151
static DATA_BLOCK_STATEREQUEST_s can_tableStateRequest
Definition: test_can.c:93
void testCAN_TransmitBootMessage(void)
Definition: test_can.c:240
QueueHandle_t ftsk_canRxQueue
Definition: test_can.c:132
QueueHandle_t ftsk_imdCanDataQueue
Definition: test_can.c:131
static DATA_BLOCK_CURRENT_SENSOR_s can_tableCurrentSensor
Definition: test_can.c:91
Header file for the version information that is generated by the toolchain.