Detect revisions of the STM32H743.
[betaflight.git] / src / test / unit / cli_unittest.cc
blob1758c462f2a2e9651bb1bde6972f1b16db28a74f
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdint.h>
19 #include <stdbool.h>
21 #include <limits.h>
23 #include <math.h>
25 extern "C" {
26 #include "platform.h"
27 #include "target.h"
28 #include "build/version.h"
29 #include "cli/cli.h"
30 #include "cli/settings.h"
31 #include "common/printf.h"
32 #include "config/feature.h"
33 #include "drivers/buf_writer.h"
34 #include "drivers/vtx_common.h"
35 #include "config/config.h"
36 #include "fc/rc_adjustments.h"
37 #include "fc/runtime_config.h"
38 #include "flight/mixer.h"
39 #include "flight/pid.h"
40 #include "flight/servos.h"
41 #include "io/beeper.h"
42 #include "io/ledstrip.h"
43 #include "io/serial.h"
44 #include "io/vtx.h"
45 #include "msp/msp.h"
46 #include "msp/msp_box.h"
47 #include "osd/osd.h"
48 #include "pg/pg.h"
49 #include "pg/pg_ids.h"
50 #include "pg/beeper.h"
51 #include "pg/rx.h"
52 #include "rx/rx.h"
53 #include "scheduler/scheduler.h"
54 #include "sensors/battery.h"
56 void cliSet(char *cmdline);
57 void cliGet(char *cmdline);
58 int cliGetSettingIndex(char *name, uint8_t length);
59 void *cliGetValuePointer(const clivalue_t *value);
61 const clivalue_t valueTable[] = {
62 { "array_unit_test", VAR_INT8 | MODE_ARRAY | MASTER_VALUE, .config.array.length = 3, PG_RESERVED_FOR_TESTING_1, 0 },
63 { "str_unit_test", VAR_UINT8 | MODE_STRING | MASTER_VALUE, .config.string = { 0, 16, 0 }, PG_RESERVED_FOR_TESTING_1, 0 },
64 { "wos_unit_test", VAR_UINT8 | MODE_STRING | MASTER_VALUE, .config.string = { 0, 16, STRING_FLAGS_WRITEONCE }, PG_RESERVED_FOR_TESTING_1, 0 },
66 const uint16_t valueTableEntryCount = ARRAYLEN(valueTable);
67 const lookupTableEntry_t lookupTables[] = {};
68 const char * const lookupTableOsdDisplayPortDevice[] = {};
71 PG_REGISTER(osdConfig_t, osdConfig, PG_OSD_CONFIG, 0);
72 PG_REGISTER(batteryConfig_t, batteryConfig, PG_BATTERY_CONFIG, 0);
73 PG_REGISTER(ledStripConfig_t, ledStripConfig, PG_LED_STRIP_CONFIG, 0);
74 PG_REGISTER(ledStripStatusModeConfig_t, ledStripStatusModeConfig, PG_LED_STRIP_STATUS_MODE_CONFIG, 0);
75 PG_REGISTER(systemConfig_t, systemConfig, PG_SYSTEM_CONFIG, 0);
76 PG_REGISTER(pilotConfig_t, pilotConfig, PG_PILOT_CONFIG, 0);
77 PG_REGISTER_ARRAY(adjustmentRange_t, MAX_ADJUSTMENT_RANGE_COUNT, adjustmentRanges, PG_ADJUSTMENT_RANGE_CONFIG, 0);
78 PG_REGISTER_ARRAY(modeActivationCondition_t, MAX_MODE_ACTIVATION_CONDITION_COUNT, modeActivationConditions, PG_MODE_ACTIVATION_PROFILE, 0);
79 PG_REGISTER(mixerConfig_t, mixerConfig, PG_MIXER_CONFIG, 0);
80 PG_REGISTER_ARRAY(motorMixer_t, MAX_SUPPORTED_MOTORS, customMotorMixer, PG_MOTOR_MIXER, 0);
81 PG_REGISTER_ARRAY(servoParam_t, MAX_SUPPORTED_SERVOS, servoParams, PG_SERVO_PARAMS, 0);
82 PG_REGISTER_ARRAY(servoMixer_t, MAX_SERVO_RULES, customServoMixers, PG_SERVO_MIXER, 0);
83 PG_REGISTER(beeperConfig_t, beeperConfig, PG_BEEPER_CONFIG, 0);
84 PG_REGISTER(rxConfig_t, rxConfig, PG_RX_CONFIG, 0);
85 PG_REGISTER(serialConfig_t, serialConfig, PG_SERIAL_CONFIG, 0);
86 PG_REGISTER_ARRAY(rxChannelRangeConfig_t, NON_AUX_CHANNEL_COUNT, rxChannelRangeConfigs, PG_RX_CHANNEL_RANGE_CONFIG, 0);
87 PG_REGISTER_ARRAY(rxFailsafeChannelConfig_t, MAX_SUPPORTED_RC_CHANNEL_COUNT, rxFailsafeChannelConfigs, PG_RX_FAILSAFE_CHANNEL_CONFIG, 0);
88 PG_REGISTER(pidConfig_t, pidConfig, PG_PID_CONFIG, 0);
90 PG_REGISTER_WITH_RESET_FN(int8_t, unitTestData, PG_RESERVED_FOR_TESTING_1, 0);
93 #include "unittest_macros.h"
94 #include "gtest/gtest.h"
96 TEST(CLIUnittest, TestCliSetArray)
98 char *str = (char *)"array_unit_test = 123, -3 , 1";
99 cliSet(str);
101 const uint16_t index = cliGetSettingIndex(str, 15);
102 EXPECT_LT(index, valueTableEntryCount);
104 const clivalue_t val = valueTable[index];
106 printf("\n===============================\n");
107 int8_t *data = (int8_t *)cliGetValuePointer(&val);
108 for(int i=0; i < val.config.array.length; i++){
109 printf("data[%d] = %d\n", i, data[i]);
111 printf("\n===============================\n");
114 EXPECT_EQ(123, data[0]);
115 EXPECT_EQ( -3, data[1]);
116 EXPECT_EQ( 1, data[2]);
119 TEST(CLIUnittest, TestCliSetStringNoFlags)
121 char *str = (char *)"str_unit_test = SAMPLE";
122 cliSet(str);
124 const uint16_t index = cliGetSettingIndex(str, 13);
125 EXPECT_LT(index, valueTableEntryCount);
127 const clivalue_t val = valueTable[index];
129 printf("\n===============================\n");
130 uint8_t *data = (uint8_t *)cliGetValuePointer(&val);
131 for(int i = 0; i < val.config.string.maxlength && data[i] != 0; i++){
132 printf("data[%d] = %d (%c)\n", i, data[i], data[i]);
134 printf("\n===============================\n");
137 EXPECT_EQ('S', data[0]);
138 EXPECT_EQ('A', data[1]);
139 EXPECT_EQ('M', data[2]);
140 EXPECT_EQ('P', data[3]);
141 EXPECT_EQ('L', data[4]);
142 EXPECT_EQ('E', data[5]);
143 EXPECT_EQ(0, data[6]);
146 TEST(CLIUnittest, TestCliSetStringWriteOnce)
148 char *str1 = (char *)"wos_unit_test = SAMPLE";
149 char *str2 = (char *)"wos_unit_test = ELPMAS";
150 cliSet(str1);
152 const uint16_t index = cliGetSettingIndex(str1, 13);
153 EXPECT_LT(index, valueTableEntryCount);
155 const clivalue_t val = valueTable[index];
157 printf("\n===============================\n");
158 uint8_t *data = (uint8_t *)cliGetValuePointer(&val);
159 for(int i = 0; i < val.config.string.maxlength && data[i] != 0; i++){
160 printf("data[%d] = %d (%c)\n", i, data[i], data[i]);
162 printf("\n===============================\n");
164 EXPECT_EQ('S', data[0]);
165 EXPECT_EQ('A', data[1]);
166 EXPECT_EQ('M', data[2]);
167 EXPECT_EQ('P', data[3]);
168 EXPECT_EQ('L', data[4]);
169 EXPECT_EQ('E', data[5]);
170 EXPECT_EQ(0, data[6]);
172 cliSet(str2);
174 EXPECT_EQ('S', data[0]);
175 EXPECT_EQ('A', data[1]);
176 EXPECT_EQ('M', data[2]);
177 EXPECT_EQ('P', data[3]);
178 EXPECT_EQ('L', data[4]);
179 EXPECT_EQ('E', data[5]);
180 EXPECT_EQ(0, data[6]);
182 cliSet(str1);
184 EXPECT_EQ('S', data[0]);
185 EXPECT_EQ('A', data[1]);
186 EXPECT_EQ('M', data[2]);
187 EXPECT_EQ('P', data[3]);
188 EXPECT_EQ('L', data[4]);
189 EXPECT_EQ('E', data[5]);
190 EXPECT_EQ(0, data[6]);
192 printf("\n");
195 // STUBS
196 extern "C" {
198 float motor_disarmed[MAX_SUPPORTED_MOTORS];
200 uint16_t batteryWarningVoltage;
201 uint8_t useHottAlarmSoundPeriod (void) { return 0; }
202 const uint32_t baudRates[] = {0, 9600, 19200, 38400, 57600, 115200, 230400, 250000, 400000}; // see baudRate_e
204 uint32_t micros(void) {return 0;}
206 int32_t getAmperage(void) {
207 return 100;
210 uint16_t getBatteryVoltage(void) {
211 return 42;
214 batteryState_e getBatteryState(void) {
215 return BATTERY_OK;
218 uint8_t calculateBatteryPercentageRemaining(void) {
219 return 67;
222 uint8_t getMotorCount() {
223 return 4;
226 size_t getEEPROMStorageSize() {
227 return 0;
231 void setPrintfSerialPort(struct serialPort_s) {}
233 static const box_t boxes[] = { { 0, "DUMMYBOX", 0 } };
234 const box_t *findBoxByPermanentId(uint8_t) { return &boxes[0]; }
235 const box_t *findBoxByBoxId(boxId_e) { return &boxes[0]; }
237 int8_t unitTestDataArray[3];
239 void pgResetFn_unitTestData(int8_t *ptr) {
240 ptr = &unitTestDataArray[0];
243 uint32_t getBeeperOffMask(void) { return 0; }
244 uint32_t getPreferredBeeperOffMask(void) { return 0; }
246 void beeper(beeperMode_e) {}
247 void beeperSilence(void) {}
248 void beeperConfirmationBeeps(uint8_t) {}
249 void beeperWarningBeeps(uint8_t) {}
250 void beeperUpdate(timeUs_t) {}
251 uint32_t getArmingBeepTimeMicros(void) {return 0;}
252 beeperMode_e beeperModeForTableIndex(int) {return BEEPER_SILENCE;}
253 uint32_t beeperModeMaskForTableIndex(int idx) {UNUSED(idx); return 0;}
254 const char *beeperNameForTableIndex(int) {return NULL;}
255 int beeperTableEntryCount(void) {return 0;}
256 bool isBeeperOn(void) {return false;}
257 void beeperOffSetAll(uint8_t) {}
258 void setBeeperOffMask(uint32_t) {}
259 void setPreferredBeeperOffMask(uint32_t) {}
261 void beeperOffSet(uint32_t) {}
262 void beeperOffClear(uint32_t) {}
263 void beeperOffClearAll(void) {}
264 bool parseColor(int, const char *) {return false; }
265 bool resetEEPROM(bool) { return true; }
266 void bufWriterFlush(bufWriter_t *) {}
267 void mixerResetDisarmedMotors(void) {}
268 void gpsEnablePassthrough(struct serialPort_s *) {}
269 bool parseLedStripConfig(int, const char *){return false; }
270 const char rcChannelLetters[] = "AERT12345678abcdefgh";
272 void parseRcChannels(const char *, rxConfig_t *){}
273 void mixerLoadMix(int, motorMixer_t *) {}
274 bool setModeColor(ledModeIndex_e, int, int) { return false; }
275 float motorConvertFromExternal(uint16_t) { return 1.0; }
276 void motorShutdown(void) { }
277 uint8_t getCurrentPidProfileIndex(void){ return 1; }
278 uint8_t getCurrentControlRateProfileIndex(void){ return 1; }
279 void changeControlRateProfile(uint8_t) {}
280 void resetAllRxChannelRangeConfigurations(rxChannelRangeConfig_t *) {}
281 void writeEEPROM() {}
282 serialPortConfig_t *serialFindPortConfigurationMutable(serialPortIdentifier_e) {return NULL; }
283 baudRate_e lookupBaudRateIndex(uint32_t){return BAUD_9600; }
284 serialPortUsage_t *findSerialPortUsageByIdentifier(serialPortIdentifier_e){ return NULL; }
285 serialPort_t *openSerialPort(serialPortIdentifier_e, serialPortFunction_e, serialReceiveCallbackPtr, void *, uint32_t, portMode_e, portOptions_e) { return NULL; }
286 void serialSetBaudRate(serialPort_t *, uint32_t) {}
287 void serialSetMode(serialPort_t *, portMode_e) {}
288 void serialPassthrough(serialPort_t *, serialPort_t *, serialConsumer *, serialConsumer *) {}
289 uint32_t millis(void) { return 0; }
290 uint8_t getBatteryCellCount(void) { return 1; }
291 void servoMixerLoadMix(int) {}
292 const char * getBatteryStateString(void){ return "_getBatteryStateString_"; }
294 uint32_t stackTotalSize(void) { return 0x4000; }
295 uint32_t stackHighMem(void) { return 0x80000000; }
296 uint16_t getEEPROMConfigSize(void) { return 1024; }
298 uint8_t __config_start = 0x00;
299 uint8_t __config_end = 0x10;
300 uint16_t averageSystemLoadPercent = 0;
302 timeDelta_t getTaskDeltaTime(cfTaskId_e){ return 0; }
303 uint16_t currentRxRefreshRate = 9000;
304 armingDisableFlags_e getArmingDisableFlags(void) { return ARMING_DISABLED_NO_GYRO; }
306 const char *armingDisableFlagNames[]= {
307 "DUMMYDISABLEFLAGNAME"
310 void getTaskInfo(cfTaskId_e, cfTaskInfo_t *) {}
311 void getCheckFuncInfo(cfCheckFuncInfo_t *) {}
312 void schedulerResetTaskMaxExecutionTime(cfTaskId_e) {}
313 void schedulerResetCheckFunctionMaxExecutionTime(void) {}
315 const char * const targetName = "UNITTEST";
316 const char* const buildDate = "Jan 01 2017";
317 const char * const buildTime = "00:00:00";
318 const char * const shortGitRevision = "MASTER";
320 uint32_t serialRxBytesWaiting(const serialPort_t *) {return 0;}
321 uint8_t serialRead(serialPort_t *){return 0;}
323 void bufWriterAppend(bufWriter_t *, uint8_t ch){ printf("%c", ch); }
324 void serialWriteBufShim(void *, const uint8_t *, int) {}
325 bufWriter_t *bufWriterInit(uint8_t *, int, bufWrite_t, void *) {return NULL;}
326 void schedulerSetCalulateTaskStatistics(bool) {}
327 void setArmingDisabled(armingDisableFlags_e) {}
329 void waitForSerialPortToFinishTransmitting(serialPort_t *) {}
330 void systemResetToBootloader(void) {}
331 void resetConfig(void) {}
332 void systemReset(void) {}
333 void writeUnmodifiedConfigToEEPROM(void) {}
335 void changePidProfile(uint8_t) {}
336 bool serialIsPortAvailable(serialPortIdentifier_e) { return false; }
337 void generateLedConfig(ledConfig_t *, char *, size_t) {}
338 bool isSerialTransmitBufferEmpty(const serialPort_t *) {return true; }
339 void serialWrite(serialPort_t *, uint8_t ch) { printf("%c", ch);}
341 void serialSetCtrlLineStateCb(serialPort_t *, void (*)(void *, uint16_t ), void *) {}
342 void serialSetCtrlLineStateDtrPin(serialPort_t *, ioTag_t ) {}
343 void serialSetCtrlLineState(serialPort_t *, uint16_t ) {}
345 void serialSetBaudRateCb(serialPort_t *, void (*)(serialPort_t *context, uint32_t baud), serialPort_t *) {}
347 char *getBoardName(void) { return NULL; }
348 char *getManufacturerId(void) { return NULL; }
349 bool boardInformationIsSet(void) { return true; }
351 bool setBoardName(char *newBoardName) { UNUSED(newBoardName); return true; };
352 bool setManufacturerId(char *newManufacturerId) { UNUSED(newManufacturerId); return true; };
353 bool persistBoardInformation(void) { return true; };
355 void activeAdjustmentRangeReset(void) {}
356 void analyzeModeActivationConditions(void) {}
357 bool isModeActivationConditionConfigured(const modeActivationCondition_t *, const modeActivationCondition_t *) { return false; }
359 void delay(uint32_t) {}
360 displayPort_t *osdGetDisplayPort(osdDisplayPortDevice_e *) { return NULL; }
361 mcuTypeId_e getMcuTypeId(void) { return MCU_TYPE_UNKNOWN; }