foxBMS - Unit Tests  1.2.1
The foxBMS Unit Tests API Documentation
test_mxm_registry.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_mxm_registry.c
44  * @author foxBMS Team
45  * @date 2020-07-16 (date of creation)
46  * @updated 2021-12-06 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix MXM
49  *
50  * @brief Test for the Maxim registry tool.
51  *
52  * @details def
53  *
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "unity.h"
58 #include "Mockfassert.h"
59 #include "Mockmxm_1785x.h"
60 #include "Mockmxm_basic_defines.h"
61 
62 #include "foxmath.h"
63 #include "mxm_1785x_tools.h"
64 #include "mxm_registry.h"
65 #include "test_assert_helper.h"
66 
67 /*========== Definitions and Implementations for Unit Test ==================*/
70  .operationSubstate = MXM_INIT_ENTRY,
71  .allowStartup = false,
72  .operationRequested = false,
73  .firstMeasurementDone = false,
74  .stopRequested = false,
75  .openwireRequested = false,
76  .undervoltageAlert = false,
77  .dcByte = MXM_DC_EMPTY,
78  .mxmVoltageCellCounter = 0,
79  .highest5xDevice = 0,
80  .requestStatus5x = MXM_5X_STATE_UNSENT,
81  .batteryCmdBuffer =
82  {
83  .regAddress = (MXM_REG_NAME_e)0x00,
84  .lsb = 0x00,
85  .msb = 0x00,
86  .deviceAddress = 0x00,
87  .blocksize = 0,
88  .model = MXM_MODEL_ID_MAX17852,
89  },
90  .resultSelfCheck = STD_NOT_OK,
91  .selfCheck =
92  {
93  .crc = STD_NOT_OK,
94  .conv = STD_NOT_OK,
95  .firstSetBit = STD_NOT_OK,
96  .extractValueFromRegister = STD_NOT_OK,
97  .parseVoltageReadall = STD_NOT_OK,
98  .addressSpaceChecker = STD_NOT_OK,
99  .fmeaStatusASCI = STD_NOT_OK,
100  },
101 };
102 
104  TEST_ASSERT(pState != NULL_PTR);
105  for (uint16_t i = 0u; i <= MXM_RX_BUFFER_LENGTH; i++) {
106  pState->rxBuffer[i] = i;
107  }
108 }
109 
110 /*========== Setup and Teardown =============================================*/
111 void setUp(void) {
114 }
115 
116 void tearDown(void) {
117 }
118 
119 /*========== Test Cases =====================================================*/
121  /* check once: should be initiated from init */
122  for (uint8_t i = 0u; i < MXM_MAXIMUM_NR_OF_MODULES; i++) {
123  TEST_ASSERT_EQUAL(false, mxm_state.registry[i].connected);
124  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceAddress);
125  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceID);
126  TEST_ASSERT_EQUAL(MXM_MODEL_ID_NONE, mxm_state.registry[i].model);
127  TEST_ASSERT_EQUAL(MXM_siliconVersion_0, mxm_state.registry[i].siliconVersion);
128  }
129 
130  /* set some values to the registry */
131  mxm_state.registry[8].connected = true;
132  mxm_state.registry[15].deviceAddress = 42u;
134  mxm_state.registry[30].model = 0x358;
135  mxm_state.registry[18].deviceID = 1414;
136 
137  /* call init again */
139 
140  /* check a second time: should be initiated again */
141  for (uint8_t i = 0u; i < MXM_MAXIMUM_NR_OF_MODULES; i++) {
142  TEST_ASSERT_EQUAL(false, mxm_state.registry[i].connected);
143  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceAddress);
144  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceID);
145  TEST_ASSERT_EQUAL(MXM_MODEL_ID_NONE, mxm_state.registry[i].model);
146  TEST_ASSERT_EQUAL(MXM_siliconVersion_0, mxm_state.registry[i].siliconVersion);
147  }
148 }
149 
152 }
153 
156 }
157 
159  TEST_ASSERT_EQUAL(STD_OK, MXM_MonRegistryConnectDevices(&mxm_state, 0));
160  TEST_ASSERT_EQUAL(false, mxm_state.registry[0].connected);
161  TEST_ASSERT_EQUAL(STD_OK, MXM_MonRegistryConnectDevices(&mxm_state, 1));
162  TEST_ASSERT_EQUAL(true, mxm_state.registry[0].connected);
164  TEST_ASSERT_EQUAL(true, mxm_state.registry[MXM_MAXIMUM_NR_OF_MODULES - 1u].connected);
165 }
166 
169 }
170 
173 }
174 
175 void testHighest5X(void) {
177 
178  TEST_ASSERT_EQUAL(42u, MXM_MonRegistryGetHighestConnected5XDevice(&mxm_state));
179 }
180 
183 }
184 
188 }
189 
193 }
194 
196  /* fill the buffer with ascending values and check at few places that the
197  parsing is consistent (always the same value) */
199  const uint8_t highestDevice = 13u;
200  mxm_state.highest5xDevice = highestDevice;
201  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
203  &mxm_state, (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice)), MXM_REG_ID1));
204  TEST_ASSERT_EQUAL(6938, mxm_state.registry[0].deviceID);
205  TEST_ASSERT_EQUAL(6424, mxm_state.registry[1].deviceID);
206 }
207 
209  /* check if wrong buffer lengths are sanitized */
210  const uint8_t highestDevice = 13u;
211  mxm_state.highest5xDevice = highestDevice;
212  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
215 }
216 
218  /* fill the buffer with ascending values and check at few places that the
219  parsing is consistent (always the same value) */
221  const uint8_t highestDevice = 13u;
222  mxm_state.highest5xDevice = highestDevice;
223  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
225  &mxm_state, (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice)), MXM_REG_ID2));
226  TEST_ASSERT_EQUAL(454688768, mxm_state.registry[0].deviceID);
227  TEST_ASSERT_EQUAL(421003264, mxm_state.registry[1].deviceID);
228 }
229 
231  /* check if wrong buffer lengths are sanitized */
232  const uint8_t highestDevice = 13u;
233  mxm_state.highest5xDevice = highestDevice;
234  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
237 }
238 
241 }
242 
244  /* fill the buffer with ascending values and check at few places that the
245  parsing is consistent (always the same value) */
247  const uint8_t highestDevice = 13u;
248  mxm_state.highest5xDevice = highestDevice;
249  /* inject values so that satellite with highestDevice - 1u will report a model ID MXM_MODEL_ID_MAX17854 */
250  mxm_state.rxBuffer[3u] = 0x85u;
251  mxm_state.rxBuffer[2u] = 0x40u;
252  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
254  &mxm_state, (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice))));
255  TEST_ASSERT_EQUAL(MXM_MODEL_ID_invalid, mxm_state.registry[0].model); /*!< invalid model ID */
256  TEST_ASSERT_EQUAL(8, mxm_state.registry[1].siliconVersion);
257  TEST_ASSERT_EQUAL(MXM_MODEL_ID_MAX17854, mxm_state.registry[highestDevice - 1u].model);
258  TEST_ASSERT_EQUAL(0, mxm_state.registry[highestDevice - 1u].siliconVersion);
259 }
260 
262  /* check if wrong buffer lengths are sanitized */
263  const uint8_t highestDevice = 13u;
264  mxm_state.highest5xDevice = highestDevice;
265  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
267 }
268 
269 /** call #MXM_MonRegistryParseStatusFmeaIntoDevices() with a null pointer */
272 }
273 
275  /* fill the buffer with ascending values and check at few places that the
276  parsing is consistent (always the same value) */
278  /* inject the "correct" register number (STATUS2) in the stream */
280  const uint8_t highestDevice = 13u;
281  mxm_state.highest5xDevice = highestDevice;
282  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
283  const uint8_t messageLength = (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice));
285  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerStatus2);
286  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerStatus2);
287  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerStatus2);
288 
289  /* inject the "STATUS1" register number in the stream */
292  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerStatus1);
293  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerStatus1);
294  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerStatus1);
295 
296  /* inject the "STATUS3" register number in the stream */
299  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerStatus3);
300  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerStatus3);
301  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerStatus3);
302 
303  /* inject the "FMEA1" register number in the stream */
306  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerFmea1);
307  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerFmea1);
308  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerFmea1);
309 
310  /* inject the "FMEA2" register number in the stream */
313  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerFmea2);
314  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerFmea2);
315  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerFmea2);
316 }
317 
319  /* check if wrong buffer lengths are sanitized */
320  const uint8_t highestDevice = 13u;
321  mxm_state.highest5xDevice = highestDevice;
322  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
324 }
325 
326 /** check that #MXM_CheckIfADeviceHasBeenReset() recognizes a set ALRTRST bit */
329 
330  const uint8_t highestDevice = 10u;
331  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
332  /* place fake cleared register values */
333  for (uint8_t i = 0u; i < highestDevice; i++) {
335  }
336  /* therefore, no device should be reset */
337  TEST_ASSERT_EQUAL(false, MXM_CheckIfADeviceHasBeenReset(&mxm_state));
338 
339  /* fake one device being reset */
340  mxm_state.registry[0u].registerStatus1 = 0x4000u;
341  TEST_ASSERT_EQUAL(true, MXM_CheckIfADeviceHasBeenReset(&mxm_state));
342 }
343 
344 /** check that invalid values trip #MXM_CheckIfADeviceIsConnected() */
348 }
349 
350 /** check if #MXM_CheckIfADeviceIsConnected() does its job */
352  for (uint8_t checkedDevice = 0u; checkedDevice < MXM_MAXIMUM_NR_OF_MODULES; checkedDevice++) {
353  mxm_state.registry[checkedDevice].connected = false;
354  TEST_ASSERT_FALSE(MXM_CheckIfADeviceIsConnected(&mxm_state, checkedDevice));
355  mxm_state.registry[checkedDevice].connected = true;
356  TEST_ASSERT_TRUE(MXM_CheckIfADeviceIsConnected(&mxm_state, checkedDevice));
357  }
358 }
math library for often used math functions
@ 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
This is a collection of helper functions for the MAX1785x ICs.
@ MXM_INIT_ENTRY
@ MXM_STATEMACHINE_STATES_UNINITIALIZED
#define MXM_RX_BUFFER_LENGTH
Length of RX buffer.
@ MXM_MODEL_ID_MAX17852
@ MXM_MODEL_ID_MAX17854
@ MXM_MODEL_ID_NONE
@ MXM_MODEL_ID_invalid
@ MXM_DC_EMPTY
@ MXM_siliconVersion_0
#define MXM_MAXIMUM_NR_OF_MODULES
Maximum number of modules.
@ MXM_5X_STATE_UNSENT
#define BATTERY_MANAGEMENT_TX_LENGTH_READALL
Battery Management Protocol lengths of TX buffer.
MXM_REG_NAME_e
MAX1785x register names.
@ MXM_REG_STATUS1
@ MXM_REG_STATUS3
@ MXM_REG_STATUS2
@ MXM_REG_ID1
@ MXM_REG_FMEA1
@ MXM_REG_ID2
@ MXM_REG_FMEA2
bool MXM_CheckIfADeviceIsConnected(const MXM_MONITORING_INSTANCE_s *const kpkState, uint8_t device)
check if a device is connected
Definition: mxm_registry.c:256
void MXM_MonRegistryParseIdIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength, MXM_REG_NAME_e type)
Parse ID (1 or 2) into the registry.
Definition: mxm_registry.c:118
void MXM_MonRegistryInit(MXM_MONITORING_INSTANCE_s *pState)
Initialize monitoring registry.
Definition: mxm_registry.c:78
STD_RETURN_TYPE_e MXM_MonRegistryConnectDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t numberOfDevices)
Mark devices as connected in the registry and set the address.
Definition: mxm_registry.c:95
uint8_t MXM_MonRegistryGetHighestConnected5XDevice(const MXM_MONITORING_INSTANCE_s *const kpkState)
Parse number of highest connected device from monitoring- register.
Definition: mxm_registry.c:112
void MXM_MonRegistryParseStatusFmeaIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength)
Parse STATUS or FMEA into the registry.
Definition: mxm_registry.c:189
void MXM_MonRegistryParseVersionIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength)
Parse Version into the registry.
Definition: mxm_registry.c:149
bool MXM_CheckIfADeviceHasBeenReset(const MXM_MONITORING_INSTANCE_s *const kpkState)
check if one of the devices in the registry has the ALRTRST bit set
Definition: mxm_registry.c:233
Functions in order to have a registry of monitoring ICs.
uint8_t rxBuffer[MXM_RX_BUFFER_LENGTH]
MXM_REGISTRY_ENTRY_s registry[MXM_MAXIMUM_NR_OF_MODULES]
MXM_STATEMACHINE_STATES_e state
uint16_t registerStatus2
uint16_t registerStatus3
MXM_siliconVersion_e siliconVersion
uint32_t deviceID
uint16_t registerFmea2
uint8_t deviceAddress
MXM_MODEL_ID_e model
uint16_t registerStatus1
uint16_t registerFmea1
bool connected
Helper for unit tests.
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
void testMXM_MonRegistryParseIdIntoDevicesCheckAscendingBufferId1(void)
void testMXM_MonRegistryParseVersionIntoDevicesWrongBufferLength(void)
void testMXM_CheckIfADeviceIsConnectedinvalidValues(void)
void testMXM_MonRegistryInitNullPointer(void)
void testMXM_MonRegistryGetHighestConnected5XDeviceNullPointer(void)
void testMXM_MonRegistryParseStatusFmeaIntoDevicesCheckAscendingBuffer(void)
void testMXM_MonRegistryConnectDevicesNullPointer(void)
void testMXM_CheckIfADeviceHasBeenReset(void)
void setUp(void)
static void rxBufferFillAscending(MXM_MONITORING_INSTANCE_s *pState)
void tearDown(void)
void testMXM_MonRegistryParseIdIntoDevicesWrongBufferLengthId2(void)
void testHighest5X(void)
void testMXM_CheckIfADeviceIsConnected(void)
void testMXM_MonRegistryParseVersionIntoDevicesCheckAscendingBuffer(void)
void testMXM_MonRegistryParseStatusFmeaIntoDevicesNullPointer(void)
void testMXM_MonRegistryParseVersionIntoDevicesNullPointer(void)
static MXM_MONITORING_INSTANCE_s mxm_state
void testRegistryConnectDevicesInvalidNumber(void)
void testRegistryInitiated(void)
void testMXM_MonRegistryParseIdIntoDevicesNullPointer(void)
void testMXM_MonRegistryParseIdIntoDevicesCheckAscendingBufferId2(void)
void testMXM_MonRegistryParseIdIntoDevicesWrongBufferLengthId1(void)
void testMXM_MonRegistryParseIdIntoDevicesInvalidRegister(void)
void testMXM_MonRegistryParseStatusFmeaIntoDevicesWrongBufferLength(void)
void testMXM_MonRegistryParseIdIntoDevicesValidRegister(void)
void testRegistryConnectDevices(void)