Merge pull request #11430 from phobos-/crazybee-icm
[betaflight.git] / src / test / unit / cli_unittest.cc
blob28cab551394e7d3a3bf33f8b5029e0c8ea829535
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) {
212 return 100;
215 uint16_t getBatteryVoltage(void) {
216 return 42;
219 batteryState_e getBatteryState(void) {
220 return BATTERY_OK;
223 uint8_t calculateBatteryPercentageRemaining(void) {
224 return 67;
227 uint8_t getMotorCount() {
228 return 4;
231 size_t getEEPROMStorageSize() {
232 return 0;
236 void setPrintfSerialPort(struct serialPort_s) {}
238 static const box_t boxes[] = { { 0, "DUMMYBOX", 0 } };
239 const box_t *findBoxByPermanentId(uint8_t) { return &boxes[0]; }
240 const box_t *findBoxByBoxId(boxId_e) { return &boxes[0]; }
242 int8_t unitTestDataArray[3];
244 void pgResetFn_unitTestData(int8_t *ptr) {
245 ptr = &unitTestDataArray[0];
248 uint32_t getBeeperOffMask(void) { return 0; }
249 uint32_t getPreferredBeeperOffMask(void) { return 0; }
251 void beeper(beeperMode_e) {}
252 void beeperSilence(void) {}
253 void beeperConfirmationBeeps(uint8_t) {}
254 void beeperWarningBeeps(uint8_t) {}
255 void beeperUpdate(timeUs_t) {}
256 uint32_t getArmingBeepTimeMicros(void) {return 0;}
257 beeperMode_e beeperModeForTableIndex(int) {return BEEPER_SILENCE;}
258 uint32_t beeperModeMaskForTableIndex(int idx) {UNUSED(idx); return 0;}
259 const char *beeperNameForTableIndex(int) {return NULL;}
260 int beeperTableEntryCount(void) {return 0;}
261 bool isBeeperOn(void) {return false;}
262 void beeperOffSetAll(uint8_t) {}
263 void setBeeperOffMask(uint32_t) {}
264 void setPreferredBeeperOffMask(uint32_t) {}
266 void beeperOffSet(uint32_t) {}
267 void beeperOffClear(uint32_t) {}
268 void beeperOffClearAll(void) {}
269 bool parseColor(int, const char *) {return false; }
270 bool resetEEPROM(bool) { return true; }
271 void bufWriterFlush(bufWriter_t *) {}
272 void mixerResetDisarmedMotors(void) {}
273 void gpsEnablePassthrough(struct serialPort_s *) {}
274 bool parseLedStripConfig(int, const char *){return false; }
275 const char rcChannelLetters[] = "AERT12345678abcdefgh";
277 void parseRcChannels(const char *, rxConfig_t *){}
278 void mixerLoadMix(int, motorMixer_t *) {}
279 bool setModeColor(ledModeIndex_e, int, int) { return false; }
280 float motorConvertFromExternal(uint16_t) { return 1.0; }
281 void motorShutdown(void) { }
282 uint8_t getCurrentPidProfileIndex(void){ return 1; }
283 uint8_t getCurrentControlRateProfileIndex(void){ return 1; }
284 void changeControlRateProfile(uint8_t) {}
285 void resetAllRxChannelRangeConfigurations(rxChannelRangeConfig_t *) {}
286 void writeEEPROM() {}
287 serialPortConfig_t *serialFindPortConfigurationMutable(serialPortIdentifier_e) {return NULL; }
288 baudRate_e lookupBaudRateIndex(uint32_t){return BAUD_9600; }
289 serialPortUsage_t *findSerialPortUsageByIdentifier(serialPortIdentifier_e){ return NULL; }
290 serialPort_t *openSerialPort(serialPortIdentifier_e, serialPortFunction_e, serialReceiveCallbackPtr, void *, uint32_t, portMode_e, portOptions_e) { return NULL; }
291 void serialSetBaudRate(serialPort_t *, uint32_t) {}
292 void serialSetMode(serialPort_t *, portMode_e) {}
293 void serialPassthrough(serialPort_t *, serialPort_t *, serialConsumer *, serialConsumer *) {}
294 uint32_t millis(void) { return 0; }
295 uint8_t getBatteryCellCount(void) { return 1; }
296 void servoMixerLoadMix(int) {}
297 const char * getBatteryStateString(void){ return "_getBatteryStateString_"; }
299 uint32_t stackTotalSize(void) { return 0x4000; }
300 uint32_t stackHighMem(void) { return 0x80000000; }
301 uint16_t getEEPROMConfigSize(void) { return 1024; }
303 uint8_t __config_start = 0x00;
304 uint8_t __config_end = 0x10;
305 uint16_t averageSystemLoadPercent = 0;
307 timeDelta_t getTaskDeltaTimeUs(taskId_e){ return 0; }
308 uint16_t currentRxRefreshRate = 9000;
309 armingDisableFlags_e getArmingDisableFlags(void) { return ARMING_DISABLED_NO_GYRO; }
311 const char *armingDisableFlagNames[]= {
312 "DUMMYDISABLEFLAGNAME"
315 void getTaskInfo(taskId_e, taskInfo_t *) {}
316 void getCheckFuncInfo(cfCheckFuncInfo_t *) {}
317 void schedulerResetTaskMaxExecutionTime(taskId_e) {}
318 void schedulerResetCheckFunctionMaxExecutionTime(void) {}
320 const char * const targetName = "UNITTEST";
321 const char* const buildDate = "Jan 01 2017";
322 const char * const buildTime = "00:00:00";
323 const char * const shortGitRevision = "MASTER";
325 uint32_t serialRxBytesWaiting(const serialPort_t *) {return 0;}
326 uint8_t serialRead(serialPort_t *){return 0;}
328 void bufWriterAppend(bufWriter_t *, uint8_t ch){ printf("%c", ch); }
329 void serialWriteBufShim(void *, const uint8_t *, int) {}
330 bufWriter_t *bufWriterInit(uint8_t *, int, bufWrite_t, void *) {return NULL;}
331 void setArmingDisabled(armingDisableFlags_e) {}
333 void waitForSerialPortToFinishTransmitting(serialPort_t *) {}
334 void systemResetToBootloader(void) {}
335 void resetConfig(void) {}
336 void systemReset(void) {}
337 void writeUnmodifiedConfigToEEPROM(void) {}
339 void changePidProfile(uint8_t) {}
340 bool serialIsPortAvailable(serialPortIdentifier_e) { return false; }
341 void generateLedConfig(ledConfig_t *, char *, size_t) {}
342 bool isSerialTransmitBufferEmpty(const serialPort_t *) {return true; }
343 void serialWrite(serialPort_t *, uint8_t ch) { printf("%c", ch);}
345 void serialSetCtrlLineStateCb(serialPort_t *, void (*)(void *, uint16_t ), void *) {}
346 void serialSetCtrlLineStateDtrPin(serialPort_t *, ioTag_t ) {}
347 void serialSetCtrlLineState(serialPort_t *, uint16_t ) {}
349 void serialSetBaudRateCb(serialPort_t *, void (*)(serialPort_t *context, uint32_t baud), serialPort_t *) {}
351 char *getBoardName(void) { return NULL; }
352 char *getManufacturerId(void) { return NULL; }
353 bool boardInformationIsSet(void) { return true; }
355 bool setBoardName(char *newBoardName) { UNUSED(newBoardName); return true; };
356 bool setManufacturerId(char *newManufacturerId) { UNUSED(newManufacturerId); return true; };
357 bool persistBoardInformation(void) { return true; };
359 void activeAdjustmentRangeReset(void) {}
360 void analyzeModeActivationConditions(void) {}
361 bool isModeActivationConditionConfigured(const modeActivationCondition_t *, const modeActivationCondition_t *) { return false; }
363 void delay(uint32_t) {}
364 displayPort_t *osdGetDisplayPort(osdDisplayPortDevice_e *) { return NULL; }
365 mcuTypeId_e getMcuTypeId(void) { return MCU_TYPE_UNKNOWN; }
366 uint16_t getCurrentRxRefreshRate(void) { return 0; }
367 uint16_t getAverageSystemLoadPercent(void) { return 0; }