foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
test_mxm_registry.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix MXM
50  *
51  * @brief Test for the Maxim registry tool.
52  *
53  * @details def
54  *
55  */
56 
57 /*========== Includes =======================================================*/
58 #include "unity.h"
59 #include "Mockfassert.h"
60 #include "Mockmxm_1785x.h"
61 #include "Mockmxm_basic_defines.h"
62 
63 #include "foxmath.h"
64 #include "mxm_1785x_tools.h"
65 #include "mxm_registry.h"
66 #include "test_assert_helper.h"
67 
68 #include <stdbool.h>
69 #include <stdint.h>
70 
71 /*========== Unit Testing Framework Directives ==============================*/
72 TEST_INCLUDE_PATH("../../src/app/driver/afe/maxim/common")
73 TEST_INCLUDE_PATH("../../src/app/driver/afe/maxim/common/config")
74 TEST_INCLUDE_PATH("../../src/app/driver/config")
75 TEST_INCLUDE_PATH("../../src/app/driver/foxmath")
76 TEST_INCLUDE_PATH("../../src/app/driver/spi")
77 
78 /*========== Definitions and Implementations for Unit Test ==================*/
81  .operationSubstate = MXM_INIT_ENTRY,
82  .allowStartup = false,
83  .operationRequested = false,
84  .firstMeasurementDone = false,
85  .stopRequested = false,
86  .openWireRequested = false,
87  .undervoltageAlert = false,
88  .dcByte = MXM_DC_EMPTY,
89  .mxmVoltageCellCounter = 0,
90  .highest5xDevice = 0,
91  .requestStatus5x = MXM_5X_STATE_UNSENT,
92  .batteryCmdBuffer =
93  {
94  .regAddress = (MXM_REG_NAME_e)0x00,
95  .lsb = 0x00,
96  .msb = 0x00,
97  .deviceAddress = 0x00,
98  .blocksize = 0,
99  .model = MXM_MODEL_ID_MAX17852,
100  },
101  .resultSelfCheck = STD_NOT_OK,
102  .selfCheck =
103  {
104  .crc = STD_NOT_OK,
105  .conv = STD_NOT_OK,
106  .firstSetBit = STD_NOT_OK,
107  .extractValueFromRegister = STD_NOT_OK,
108  .parseVoltageReadall = STD_NOT_OK,
109  .addressSpaceChecker = STD_NOT_OK,
110  .fmeaStatusASCI = STD_NOT_OK,
111  },
112 };
113 
115  TEST_ASSERT(pState != NULL_PTR);
116  for (uint16_t i = 0u; i <= MXM_RX_BUFFER_LENGTH; i++) {
117  pState->rxBuffer[i] = i;
118  }
119 }
120 
121 /*========== Setup and Teardown =============================================*/
122 void setUp(void) {
125 }
126 
127 void tearDown(void) {
128 }
129 
130 /*========== Test Cases =====================================================*/
132  /* check once: should be initiated from init */
133  for (uint8_t i = 0u; i < MXM_MAXIMUM_NR_OF_MODULES; i++) {
134  TEST_ASSERT_EQUAL(false, mxm_state.registry[i].connected);
135  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceAddress);
136  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceID);
137  TEST_ASSERT_EQUAL(MXM_MODEL_ID_NONE, mxm_state.registry[i].model);
139  }
140 
141  /* set some values to the registry */
142  mxm_state.registry[8].connected = true;
143  mxm_state.registry[15].deviceAddress = 42u;
145  mxm_state.registry[30].model = 0x358;
146  mxm_state.registry[18].deviceID = 1414;
147 
148  /* call init again */
150 
151  /* check a second time: should be initiated again */
152  for (uint8_t i = 0u; i < MXM_MAXIMUM_NR_OF_MODULES; i++) {
153  TEST_ASSERT_EQUAL(false, mxm_state.registry[i].connected);
154  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceAddress);
155  TEST_ASSERT_EQUAL(0u, mxm_state.registry[i].deviceID);
156  TEST_ASSERT_EQUAL(MXM_MODEL_ID_NONE, mxm_state.registry[i].model);
158  }
159 }
160 
163 }
164 
167 }
168 
170  TEST_ASSERT_EQUAL(STD_OK, MXM_MonRegistryConnectDevices(&mxm_state, 0));
171  TEST_ASSERT_EQUAL(false, mxm_state.registry[0].connected);
172  TEST_ASSERT_EQUAL(STD_OK, MXM_MonRegistryConnectDevices(&mxm_state, 1));
173  TEST_ASSERT_EQUAL(true, mxm_state.registry[0].connected);
175  TEST_ASSERT_EQUAL(true, mxm_state.registry[MXM_MAXIMUM_NR_OF_MODULES - 1u].connected);
176 }
177 
180 }
181 
184 }
185 
186 void testHighest5X(void) {
188 
189  TEST_ASSERT_EQUAL(42u, MXM_MonRegistryGetHighestConnected5XDevice(&mxm_state));
190 }
191 
194 }
195 
199 }
200 
204 }
205 
207  /* fill the buffer with ascending values and check at few places that the
208  parsing is consistent (always the same value) */
210  const uint8_t highestDevice = 13u;
211  mxm_state.highest5xDevice = highestDevice;
212  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
214  &mxm_state, (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice)), MXM_REG_ID1));
215  TEST_ASSERT_EQUAL(6938, mxm_state.registry[0].deviceID);
216  TEST_ASSERT_EQUAL(6424, mxm_state.registry[1].deviceID);
217 }
218 
220  /* check if wrong buffer lengths are sanitized */
221  const uint8_t highestDevice = 13u;
222  mxm_state.highest5xDevice = highestDevice;
223  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
226 }
227 
229  /* fill the buffer with ascending values and check at few places that the
230  parsing is consistent (always the same value) */
232  const uint8_t highestDevice = 13u;
233  mxm_state.highest5xDevice = highestDevice;
234  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
236  &mxm_state, (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice)), MXM_REG_ID2));
237  TEST_ASSERT_EQUAL(454688768, mxm_state.registry[0].deviceID);
238  TEST_ASSERT_EQUAL(421003264, mxm_state.registry[1].deviceID);
239 }
240 
242  /* check if wrong buffer lengths are sanitized */
243  const uint8_t highestDevice = 13u;
244  mxm_state.highest5xDevice = highestDevice;
245  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
248 }
249 
252 }
253 
255  /* fill the buffer with ascending values and check at few places that the
256  parsing is consistent (always the same value) */
258  const uint8_t highestDevice = 13u;
259  mxm_state.highest5xDevice = highestDevice;
260  /* inject values so that satellite with highestDevice - 1u will report a model ID MXM_MODEL_ID_MAX17854 */
261  mxm_state.rxBuffer[3u] = 0x85u;
262  mxm_state.rxBuffer[2u] = 0x40u;
263  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
265  &mxm_state, (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice))));
266  TEST_ASSERT_EQUAL(MXM_MODEL_ID_invalid, mxm_state.registry[0].model); /*!< invalid model ID */
267  TEST_ASSERT_EQUAL(8, mxm_state.registry[1].siliconVersion);
268  TEST_ASSERT_EQUAL(MXM_MODEL_ID_MAX17854, mxm_state.registry[highestDevice - 1u].model);
269  TEST_ASSERT_EQUAL(0, mxm_state.registry[highestDevice - 1u].siliconVersion);
270 }
271 
273  /* check if wrong buffer lengths are sanitized */
274  const uint8_t highestDevice = 13u;
275  mxm_state.highest5xDevice = highestDevice;
276  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
278 }
279 
280 /** call #MXM_MonRegistryParseStatusFmeaIntoDevices() with a null pointer */
283 }
284 
286  /* fill the buffer with ascending values and check at few places that the
287  parsing is consistent (always the same value) */
289  /* inject the "correct" register number (STATUS2) in the stream */
291  const uint8_t highestDevice = 13u;
292  mxm_state.highest5xDevice = highestDevice;
293  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
294  const uint8_t messageLength = (BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2u * highestDevice));
296  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerStatus2);
297  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerStatus2);
298  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerStatus2);
299 
300  /* inject the "STATUS1" register number in the stream */
303  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerStatus1);
304  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerStatus1);
305  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerStatus1);
306 
307  /* inject the "STATUS3" register number in the stream */
310  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerStatus3);
311  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerStatus3);
312  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerStatus3);
313 
314  /* inject the "FMEA1" register number in the stream */
317  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerFmea1);
318  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerFmea1);
319  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerFmea1);
320 
321  /* inject the "FMEA2" register number in the stream */
324  TEST_ASSERT_EQUAL(0x1B1Au, mxm_state.registry[0u].registerFmea2);
325  TEST_ASSERT_EQUAL(0x1918u, mxm_state.registry[1u].registerFmea2);
326  TEST_ASSERT_EQUAL(0x0302u, mxm_state.registry[12u].registerFmea2);
327 }
328 
330  /* check if wrong buffer lengths are sanitized */
331  const uint8_t highestDevice = 13u;
332  mxm_state.highest5xDevice = highestDevice;
333  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
335 }
336 
337 /** check that #MXM_CheckIfADeviceHasBeenReset() recognizes a set ALRTRST bit */
340 
341  const uint8_t highestDevice = 10u;
342  MXM_MonRegistryConnectDevices(&mxm_state, highestDevice);
343  /* place fake cleared register values */
344  for (uint8_t i = 0u; i < highestDevice; i++) {
346  }
347  /* therefore, no device should be reset */
348  TEST_ASSERT_EQUAL(false, MXM_CheckIfADeviceHasBeenReset(&mxm_state));
349 
350  /* fake one device being reset */
351  mxm_state.registry[0u].registerStatus1 = 0x4000u;
352  TEST_ASSERT_EQUAL(true, MXM_CheckIfADeviceHasBeenReset(&mxm_state));
353 }
354 
355 /** check that invalid values trip #MXM_CheckIfADeviceIsConnected() */
359 }
360 
361 /** check if #MXM_CheckIfADeviceIsConnected() does its job */
363  for (uint8_t checkedDevice = 0u; checkedDevice < MXM_MAXIMUM_NR_OF_MODULES; checkedDevice++) {
364  mxm_state.registry[checkedDevice].connected = false;
365  TEST_ASSERT_FALSE(MXM_CheckIfADeviceIsConnected(&mxm_state, checkedDevice));
366  mxm_state.registry[checkedDevice].connected = true;
367  TEST_ASSERT_TRUE(MXM_CheckIfADeviceIsConnected(&mxm_state, checkedDevice));
368  }
369 }
math library for often used math functions
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
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_SILICON_VERSION_0
@ MXM_MODEL_ID_MAX17852
@ MXM_MODEL_ID_MAX17854
@ MXM_MODEL_ID_NONE
@ MXM_MODEL_ID_invalid
@ MXM_DC_EMPTY
#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:266
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:125
void MXM_MonRegistryInit(MXM_MONITORING_INSTANCE_s *pState)
Initialize monitoring registry.
Definition: mxm_registry.c:83
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:100
uint8_t MXM_MonRegistryGetHighestConnected5XDevice(const MXM_MONITORING_INSTANCE_s *const kpkState)
Parse number of highest connected device from monitoring- register.
Definition: mxm_registry.c:119
void MXM_MonRegistryParseStatusFmeaIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength)
Parse STATUS or FMEA into the registry.
Definition: mxm_registry.c:196
void MXM_MonRegistryParseVersionIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength)
Parse Version into the registry.
Definition: mxm_registry.c:156
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:243
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
MXM_SILICON_VERSION_e siliconVersion
uint16_t registerStatus2
uint16_t registerStatus3
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)