Refactor Feedforward Angle and RC Smoothing - mashup of 12578 and 12594 (#12605)
[betaflight.git] / src / test / unit / cli_unittest.cc
bloba81a176a3c660c72c69ae8c79fef685a1b00d846
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"
55 #include "sensors/gyro.h"
57 void cliSet(const char *cmdName, 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);
89 PG_REGISTER(gyroConfig_t, gyroConfig, PG_GYRO_CONFIG, 0);
91 PG_REGISTER_WITH_RESET_FN(int8_t, unitTestData, PG_RESERVED_FOR_TESTING_1, 0);
94 #include "unittest_macros.h"
95 #include "gtest/gtest.h"
97 TEST(CLIUnittest, TestCliSetArray)
99 char *str = (char *)"array_unit_test = 123, -3 , 1";
100 cliSet("", str);
102 const uint16_t index = cliGetSettingIndex(str, 15);
103 EXPECT_LT(index, valueTableEntryCount);
105 const clivalue_t val = valueTable[index];
107 printf("\n===============================\n");
108 int8_t *data = (int8_t *)cliGetValuePointer(&val);
109 for(int i=0; i < val.config.array.length; i++){
110 printf("data[%d] = %d\n", i, data[i]);
112 printf("\n===============================\n");
115 EXPECT_EQ(123, data[0]);
116 EXPECT_EQ( -3, data[1]);
117 EXPECT_EQ( 1, data[2]);
120 TEST(CLIUnittest, TestCliSetStringNoFlags)
122 char *str = (char *)"str_unit_test = SAMPLE";
123 cliSet("", str);
125 const uint16_t index = cliGetSettingIndex(str, 13);
126 EXPECT_LT(index, valueTableEntryCount);
128 const clivalue_t val = valueTable[index];
130 printf("\n===============================\n");
131 uint8_t *data = (uint8_t *)cliGetValuePointer(&val);
132 for(int i = 0; i < val.config.string.maxlength && data[i] != 0; i++){
133 printf("data[%d] = %d (%c)\n", i, data[i], data[i]);
135 printf("\n===============================\n");
138 EXPECT_EQ('S', data[0]);
139 EXPECT_EQ('A', data[1]);
140 EXPECT_EQ('M', data[2]);
141 EXPECT_EQ('P', data[3]);
142 EXPECT_EQ('L', data[4]);
143 EXPECT_EQ('E', data[5]);
144 EXPECT_EQ(0, data[6]);
147 TEST(CLIUnittest, TestCliSetStringWriteOnce)
149 char *str1 = (char *)"wos_unit_test = SAMPLE";
150 char *str2 = (char *)"wos_unit_test = ELPMAS";
151 cliSet("", str1);
153 const uint16_t index = cliGetSettingIndex(str1, 13);
154 EXPECT_LT(index, valueTableEntryCount);
156 const clivalue_t val = valueTable[index];
158 printf("\n===============================\n");
159 uint8_t *data = (uint8_t *)cliGetValuePointer(&val);
160 for(int i = 0; i < val.config.string.maxlength && data[i] != 0; i++){
161 printf("data[%d] = %d (%c)\n", i, data[i], data[i]);
163 printf("\n===============================\n");
165 EXPECT_EQ('S', data[0]);
166 EXPECT_EQ('A', data[1]);
167 EXPECT_EQ('M', data[2]);
168 EXPECT_EQ('P', data[3]);
169 EXPECT_EQ('L', data[4]);
170 EXPECT_EQ('E', data[5]);
171 EXPECT_EQ(0, data[6]);
173 cliSet("", str2);
175 EXPECT_EQ('S', data[0]);
176 EXPECT_EQ('A', data[1]);
177 EXPECT_EQ('M', data[2]);
178 EXPECT_EQ('P', data[3]);
179 EXPECT_EQ('L', data[4]);
180 EXPECT_EQ('E', data[5]);
181 EXPECT_EQ(0, data[6]);
183 cliSet("", str1);
185 EXPECT_EQ('S', data[0]);
186 EXPECT_EQ('A', data[1]);
187 EXPECT_EQ('M', data[2]);
188 EXPECT_EQ('P', data[3]);
189 EXPECT_EQ('L', data[4]);
190 EXPECT_EQ('E', data[5]);
191 EXPECT_EQ(0, data[6]);
193 printf("\n");
196 // STUBS
197 extern "C" {
199 float motor_disarmed[MAX_SUPPORTED_MOTORS];
201 uint16_t batteryWarningVoltage;
202 uint8_t useHottAlarmSoundPeriod (void) { return 0; }
203 const uint32_t baudRates[] = {0, 9600, 19200, 38400, 57600, 115200, 230400, 250000, 400000}; // see baudRate_e
205 uint8_t debugMode;
206 int32_t schedLoopStartCycles;
207 int32_t taskGuardCycles;
209 uint32_t micros(void) {return 0;}
211 int32_t getAmperage(void)
213 return 100;
216 uint16_t getBatteryVoltage(void)
218 return 42;
221 batteryState_e getBatteryState(void)
223 return BATTERY_OK;
226 uint8_t calculateBatteryPercentageRemaining(void)
228 return 67;
231 uint8_t getMotorCount()
233 return 4;
236 size_t getEEPROMStorageSize()
238 return 0;
242 void setPrintfSerialPort(struct serialPort_s) {}
244 static const box_t boxes[] = { { "DUMMYBOX", 0, 0 } };
245 const box_t *findBoxByPermanentId(uint8_t) { return &boxes[0]; }
246 const box_t *findBoxByBoxId(boxId_e) { return &boxes[0]; }
248 int8_t unitTestDataArray[3];
250 void pgResetFn_unitTestData(int8_t *)
253 uint32_t getBeeperOffMask(void) { return 0; }
254 uint32_t getPreferredBeeperOffMask(void) { return 0; }
256 void beeper(beeperMode_e) {}
257 void beeperSilence(void) {}
258 void beeperConfirmationBeeps(uint8_t) {}
259 void beeperWarningBeeps(uint8_t) {}
260 void beeperUpdate(timeUs_t) {}
261 uint32_t getArmingBeepTimeMicros(void) {return 0;}
262 beeperMode_e beeperModeForTableIndex(int) {return BEEPER_SILENCE;}
263 uint32_t beeperModeMaskForTableIndex(int idx) {UNUSED(idx); return 0;}
264 const char *beeperNameForTableIndex(int) {return NULL;}
265 int beeperTableEntryCount(void) {return 0;}
266 bool isBeeperOn(void) {return false;}
267 void beeperOffSetAll(uint8_t) {}
268 void setBeeperOffMask(uint32_t) {}
269 void setPreferredBeeperOffMask(uint32_t) {}
271 void beeperOffSet(uint32_t) {}
272 void beeperOffClear(uint32_t) {}
273 void beeperOffClearAll(void) {}
274 bool parseColor(int, const char *) {return false; }
275 bool resetEEPROM(void) { return true; }
276 void bufWriterFlush(bufWriter_t *) {}
277 void mixerResetDisarmedMotors(void) {}
278 void gpsEnablePassthrough(struct serialPort_s *) {}
279 bool parseLedStripConfig(int, const char *){return false; }
280 const char rcChannelLetters[] = "AERT12345678abcdefgh";
282 void parseRcChannels(const char *, rxConfig_t *){}
283 void mixerLoadMix(int, motorMixer_t *) {}
284 bool setModeColor(ledModeIndex_e, int, int) { return false; }
285 float motorConvertFromExternal(uint16_t) { return 1.0; }
286 void motorShutdown(void) { }
287 uint8_t getCurrentPidProfileIndex(void){ return 1; }
288 uint8_t getCurrentControlRateProfileIndex(void){ return 1; }
289 void changeControlRateProfile(uint8_t) {}
290 void resetAllRxChannelRangeConfigurations(rxChannelRangeConfig_t *) {}
291 void writeEEPROM() {}
292 serialPortConfig_t *serialFindPortConfigurationMutable(serialPortIdentifier_e) {return NULL; }
293 baudRate_e lookupBaudRateIndex(uint32_t){return BAUD_9600; }
294 serialPortUsage_t *findSerialPortUsageByIdentifier(serialPortIdentifier_e){ return NULL; }
295 serialPort_t *openSerialPort(serialPortIdentifier_e, serialPortFunction_e, serialReceiveCallbackPtr, void *, uint32_t, portMode_e, portOptions_e) { return NULL; }
296 void serialSetBaudRate(serialPort_t *, uint32_t) {}
297 void serialSetMode(serialPort_t *, portMode_e) {}
298 void serialPassthrough(serialPort_t *, serialPort_t *, serialConsumer *, serialConsumer *) {}
299 uint32_t millis(void) { return 0; }
300 uint8_t getBatteryCellCount(void) { return 1; }
301 void servoMixerLoadMix(int) {}
302 const char * getBatteryStateString(void){ return "_getBatteryStateString_"; }
304 uint32_t stackTotalSize(void) { return 0x4000; }
305 uint32_t stackHighMem(void) { return 0x80000000; }
306 uint16_t getEEPROMConfigSize(void) { return 1024; }
308 uint8_t __config_start = 0x00;
309 uint8_t __config_end = 0x10;
310 uint16_t averageSystemLoadPercent = 0;
312 timeDelta_t getTaskDeltaTimeUs(taskId_e){ return 0; }
313 uint16_t currentRxIntervalUs = 9000;
314 armingDisableFlags_e getArmingDisableFlags(void) { return ARMING_DISABLED_NO_GYRO; }
316 const char *armingDisableFlagNames[]= {
317 "DUMMYDISABLEFLAGNAME"
320 void getTaskInfo(taskId_e, taskInfo_t *) {}
321 void getCheckFuncInfo(cfCheckFuncInfo_t *) {}
322 void schedulerResetTaskMaxExecutionTime(taskId_e) {}
323 void schedulerResetCheckFunctionMaxExecutionTime(void) {}
325 const char * const targetName = "UNITTEST";
326 const char* const buildDate = "Jan 01 2017";
327 const char * const buildTime = "00:00:00";
328 const char * const shortGitRevision = "MASTER";
330 uint32_t serialRxBytesWaiting(const serialPort_t *) {return 0;}
331 uint8_t serialRead(serialPort_t *){return 0;}
333 void bufWriterAppend(bufWriter_t *, uint8_t ch){ printf("%c", ch); }
334 void serialWriteBufShim(void *, const uint8_t *, int) {}
335 void bufWriterInit(bufWriter_t *, uint8_t *, int, bufWrite_t, void *) { }
336 void setArmingDisabled(armingDisableFlags_e) {}
338 void waitForSerialPortToFinishTransmitting(serialPort_t *) {}
339 void systemResetToBootloader(void) {}
340 void resetConfig(void) {}
341 void systemReset(void) {}
342 void writeUnmodifiedConfigToEEPROM(void) {}
344 void changePidProfile(uint8_t) {}
345 bool serialIsPortAvailable(serialPortIdentifier_e) { return false; }
346 void generateLedConfig(ledConfig_t *, char *, size_t) {}
347 bool isSerialTransmitBufferEmpty(const serialPort_t *) {return true; }
348 void serialWrite(serialPort_t *, uint8_t ch) { printf("%c", ch);}
350 void serialSetCtrlLineStateCb(serialPort_t *, void (*)(void *, uint16_t ), void *) {}
351 void serialSetCtrlLineStateDtrPin(serialPort_t *, ioTag_t ) {}
352 void serialSetCtrlLineState(serialPort_t *, uint16_t ) {}
354 void serialSetBaudRateCb(serialPort_t *, void (*)(serialPort_t *context, uint32_t baud), serialPort_t *) {}
356 char *getBoardName(void) { return NULL; }
357 char *getManufacturerId(void) { return NULL; }
358 bool boardInformationIsSet(void) { return true; }
360 bool setBoardName(char *newBoardName) { UNUSED(newBoardName); return true; };
361 bool setManufacturerId(char *newManufacturerId) { UNUSED(newManufacturerId); return true; };
362 bool persistBoardInformation(void) { return true; };
364 void activeAdjustmentRangeReset(void) {}
365 void analyzeModeActivationConditions(void) {}
366 bool isModeActivationConditionConfigured(const modeActivationCondition_t *, const modeActivationCondition_t *) { return false; }
368 void delay(uint32_t) {}
369 displayPort_t *osdGetDisplayPort(osdDisplayPortDevice_e *) { return NULL; }
370 mcuTypeId_e getMcuTypeId(void) { return MCU_TYPE_UNKNOWN; }
371 uint16_t getCurrentRxIntervalUs(void) { return 0; }
372 uint16_t getAverageSystemLoadPercent(void) { return 0; }