foxBMS - Unit Tests  1.1.0
The foxBMS Unit Tests API Documentation
test_bender_iso165c.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_bender_iso165c.c
44  * @author foxBMS Team
45  * @date 2021-01-19 (date of creation)
46  * @updated 2021-07-23 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Test of the Bender iso165c driver
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "unity.h"
56 #include "Mockcan.h"
57 #include "Mockcan_cfg.h"
58 #include "Mockdatabase.h"
59 #include "Mockdiag.h"
60 #include "Mockftask.h"
61 #include "Mockio.h"
62 #include "Mockmcu.h"
63 #include "Mockmpu_prototypes.h"
64 #include "Mockos.h"
65 
66 #include "bender_iso165c.h"
67 #include "test_assert_helper.h"
68 
69 /*========== Definitions and Implementations for Unit Test ==================*/
70 
71 QueueHandle_t ftsk_dataQueue = NULL_PTR;
73 QueueHandle_t ftsk_canRxQueue = NULL_PTR;
74 volatile bool ftsk_allQueuesCreated = false;
75 
76 /*========== Setup and Teardown =============================================*/
77 void setUp(void) {
78 }
79 
80 void tearDown(void) {
81 }
82 
83 /*========== Test Cases =====================================================*/
84 
85 /**
86  * @brief Test function to compose/use CAN messages.
87  *
88  */
90  CAN_BUFFERELEMENT_s canMessage;
91  uint8_t dataWord;
92  uint8_t dataByte;
93  uint16_t data16;
94  uint8_t data8;
95  uint8_t command;
96  uint8_t id;
97  I165C_STATE_e currentState;
98  I165C_STATE_e nextState;
99  uint8_t tries;
100 
101  /* Do as if there is a message in the queue */
102  MPU_uxQueueMessagesWaiting_IgnoreAndReturn(1u);
103  MPU_xQueueReceive_IgnoreAndReturn(1u);
104 
105  canMessage.id = 0x22u;
106  for (uint8_t i = 1u; i <= 8u; i++) {
107  canMessage.data[i] = i;
108  }
109  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_ResetCanData(NULL_PTR));
110  /* Reset must set all data to 0 */
111  TEST_I165C_ResetCanData(&canMessage);
112  for (uint8_t i = 0u; i < 8u; i++) {
113  TEST_ASSERT_EQUAL(0u, canMessage.data[i]);
114  }
115 
116  data16 = 666u;
117  dataWord = I165C_DW1;
118  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_WriteDataWord(dataWord, data16, NULL_PTR););
119  /* Test that data is written correctly to CAN frame */
120  TEST_I165C_WriteDataWord(dataWord, data16, &canMessage);
121  TEST_ASSERT_EQUAL(0x9Au, canMessage.data[1u]);
122  TEST_ASSERT_EQUAL(0x02u, canMessage.data[2u]);
123  dataWord = I165C_DW2;
124  TEST_I165C_WriteDataWord(dataWord, data16, &canMessage);
125  TEST_ASSERT_EQUAL(0x9Au, canMessage.data[3u]);
126  TEST_ASSERT_EQUAL(0x02u, canMessage.data[4u]);
127 
128  for (uint8_t i = 1u; i <= 4u; i++) {
129  canMessage.data[i] = i;
130  }
131  data16 = 0u;
132  dataWord = I165C_DW1;
133  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_ReadDataWord(dataWord, NULL_PTR, canMessage));
134  /* Test that dataword data is read correctly from usual CAN frame */
135  TEST_I165C_ReadDataWord(dataWord, &data16, canMessage);
136  TEST_ASSERT_EQUAL(0x201u, data16);
137  data16 = 0u;
138  dataWord = I165C_DW2;
139  TEST_I165C_ReadDataWord(dataWord, &data16, canMessage);
140  TEST_ASSERT_EQUAL(0x403u, data16);
141 
142  for (uint8_t i = 0u; i <= 5u; i++) {
143  canMessage.data[i] = i + 1u;
144  }
145  data16 = 0u;
146  dataWord = I165C_DW1;
147  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_ReadDataWordImdInfo(dataWord, NULL_PTR, canMessage));
148  /* Test that dataword data is read correctly from special IMD_Info CAN frame */
149  TEST_I165C_ReadDataWordImdInfo(dataWord, &data16, canMessage);
150  TEST_ASSERT_EQUAL(0x201u, data16);
151  data16 = 0u;
152  dataWord = I165C_DW2;
153  TEST_I165C_ReadDataWordImdInfo(dataWord, &data16, canMessage);
154  TEST_ASSERT_EQUAL(0x403u, data16);
155  data16 = 0u;
156  dataWord = I165C_DW3;
157  TEST_I165C_ReadDataWordImdInfo(dataWord, &data16, canMessage);
158  TEST_ASSERT_EQUAL(0x605u, data16);
159 
160  for (uint8_t i = 1u; i <= 4u; i++) {
161  canMessage.data[i] = i;
162  }
163  data8 = 0u;
164  dataByte = I165C_DB1;
165  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_ReadDataByte(dataByte, NULL_PTR, canMessage));
166  /* Test that databyte data is read correctly from usual CAN frame */
167  TEST_I165C_ReadDataByte(dataByte, &data8, canMessage);
168  TEST_ASSERT_EQUAL(0x1u, data8);
169  data8 = 0u;
170  dataByte = I165C_DB2;
171  TEST_I165C_ReadDataByte(dataByte, &data8, canMessage);
172  TEST_ASSERT_EQUAL(0x2u, data8);
173  data8 = 0u;
174  dataByte = I165C_DB3;
175  TEST_I165C_ReadDataByte(dataByte, &data8, canMessage);
176  TEST_ASSERT_EQUAL(0x3u, data8);
177  data8 = 0u;
178  dataByte = I165C_DB4;
179  TEST_I165C_ReadDataByte(dataByte, &data8, canMessage);
180  TEST_ASSERT_EQUAL(0x4u, data8);
181 
182  id = 0xA;
183  command = 0xB;
184  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_WriteCmd(id, command, NULL_PTR));
185  /* Test that cmd is written correctly to CAN frame */
186  TEST_I165C_WriteCmd(id, command, &canMessage);
187  TEST_ASSERT_EQUAL(0xAu, canMessage.id);
188  TEST_ASSERT_EQUAL(0xBu, canMessage.data[0u]);
189 
190  canMessage.data[0u] = 0xA;
191  command = 0xA;
192  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_CheckResponse(command, NULL_PTR));
193  /* Check that response ID corresponds to awaited acknowledge */
194  TEST_ASSERT_EQUAL(true, TEST_I165C_CheckResponse(command, &canMessage));
195  canMessage.data[0u] = 0xA;
196  command = 0xB;
197  TEST_ASSERT_EQUAL(false, TEST_I165C_CheckResponse(command, &canMessage));
198 
199  canMessage.id = I165C_MESSAGETYPE_IMD_INFO;
200  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_GetImdInfo(NULL_PTR));
201  /* Test that an IMD_info frame was received on CAN */
202  TEST_ASSERT_EQUAL(true, TEST_I165C_GetImdInfo(&canMessage));
203  canMessage.id = I165C_MESSAGETYPE_IMD_INFO + 1u;
204  TEST_ASSERT_EQUAL(false, TEST_I165C_GetImdInfo(&canMessage));
205 
206  for (uint8_t i = 0u; i < 8u; i++) {
207  canMessage.data[i] = 0u;
208  }
209  /* Test if CAN data indicated that iso165c is initialized */
210  TEST_ASSERT_EQUAL(true, TEST_I165C_IsInitialized(canMessage));
211  for (uint8_t i = 0u; i < 8u; i++) {
212  canMessage.data[i] = 0xFFu;
213  }
214  TEST_ASSERT_EQUAL(false, TEST_I165C_IsInitialized(canMessage));
215 
216  tries = 0u;
217  command = 0xA;
218  canMessage.data[0] = 0xA;
219  I165C_STATE_SELFTEST;
220  nextState = I165C_STATE_SET_ERROR_THRESHOLD_WAIT_ACK;
221  currentState = I165C_STATE_SET_ERROR_THRESHOLD;
222  /* Test function that waits for acknowledge and switches state */
223 
224  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_CheckAcknowledgeArrived(command, NULL_PTR, nextState, &tries, &canMessage));
226  TEST_I165C_CheckAcknowledgeArrived(command, &currentState, nextState, NULL_PTR, &canMessage));
227  TEST_ASSERT_FAIL_ASSERT(TEST_I165C_CheckAcknowledgeArrived(command, &currentState, nextState, &tries, NULL_PTR));
228  /* Acknowledge arrived, switch to next state */
229  TEST_I165C_CheckAcknowledgeArrived(command, &currentState, nextState, &tries, &canMessage);
230  TEST_ASSERT_EQUAL(I165C_STATE_SET_ERROR_THRESHOLD_WAIT_ACK, currentState);
231  tries = 0u;
232  command = 0xA;
233  canMessage.data[0] = 0xB;
234  nextState = I165C_STATE_SET_ERROR_THRESHOLD_WAIT_ACK;
235  currentState = I165C_STATE_SET_ERROR_THRESHOLD;
236  /* Acknowledge not arrived, increment try counter */
237  TEST_I165C_CheckAcknowledgeArrived(command, &currentState, nextState, &tries, &canMessage);
238  TEST_ASSERT_EQUAL(tries, 1u);
239  tries = I165C_TRANSMISSION_TRIES - 1u;
240  command = 0xA;
241  canMessage.data[0] = 0xB;
242  nextState = I165C_STATE_SET_ERROR_THRESHOLD_WAIT_ACK;
243  currentState = I165C_STATE_SET_ERROR_THRESHOLD;
244  /**
245  * Acknowledge not arrived, and allowed number of tries made, restart
246  * Initialization (go to self test)
247  */
248  TEST_I165C_CheckAcknowledgeArrived(command, &currentState, nextState, &tries, &canMessage);
249  TEST_ASSERT_EQUAL(I165C_STATE_SELFTEST, currentState);
250 }
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
uint8_t data[CAN_MAX_DLC]
Definition: can_cfg.h:234
uint32_t id
Definition: can_cfg.h:233
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
volatile bool ftsk_allQueuesCreated
void testMessageComposition(void)
Test function to compose/use CAN messages.
void setUp(void)
void tearDown(void)
QueueHandle_t ftsk_dataQueue
QueueHandle_t ftsk_canRxQueue
QueueHandle_t ftsk_imdCanDataQueue