diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 263eb6652..e4d1ef614 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -3,6 +3,7 @@ #include "TxtDataHelpers.h" #include "AdvertDataHelpers.h" #include +#include // Believe it or not, this std C function is busted on some platforms! static uint32_t _atoi(const char* sp) { @@ -22,6 +23,10 @@ static bool isValidName(const char *n) { return true; } +static bool prefixEq(const char* text, const char* prefix, size_t prefix_len) { + return text && prefix && strnlen(text, prefix_len) == prefix_len && std::memcmp(text, prefix, prefix_len) == 0; +} + void CommonCLI::loadPrefs(FILESYSTEM* fs) { if (fs->exists("/com_prefs")) { loadPrefsInt(fs, "/com_prefs"); // new filename @@ -194,17 +199,17 @@ uint8_t CommonCLI::buildAdvertData(uint8_t node_type, uint8_t* app_data) { } void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) { - if (memcmp(command, "reboot", 6) == 0) { + if (prefixEq(command, "reboot", 6)) { _board->reboot(); // doesn't return - } else if (memcmp(command, "clkreboot", 9) == 0) { + } else if (prefixEq(command, "clkreboot", 9)) { // Reset clock getRTCClock()->setCurrentTime(1715770351); // 15 May 2024, 8:50pm _board->reboot(); // doesn't return - } else if (memcmp(command, "advert", 6) == 0) { + } else if (prefixEq(command, "advert", 6)) { // send flood advert _callbacks->sendSelfAdvertisement(1500, true); // longer delay, give CLI response time to be sent first strcpy(reply, "OK - Advert sent"); - } else if (memcmp(command, "clock sync", 10) == 0) { + } else if (prefixEq(command, "clock sync", 10)) { uint32_t curr = getRTCClock()->getCurrentTime(); if (sender_timestamp > curr) { getRTCClock()->setCurrentTime(sender_timestamp + 1); @@ -214,15 +219,15 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "ERR: clock cannot go backwards"); } - } else if (memcmp(command, "start ota", 9) == 0) { + } else if (prefixEq(command, "start ota", 9)) { if (!_board->startOTAUpdate(_prefs->node_name, reply)) { strcpy(reply, "Error"); } - } else if (memcmp(command, "clock", 5) == 0) { + } else if (prefixEq(command, "clock", 5)) { uint32_t now = getRTCClock()->getCurrentTime(); DateTime dt = DateTime(now); sprintf(reply, "%02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(), dt.day(), dt.month(), dt.year()); - } else if (memcmp(command, "time ", 5) == 0) { // set time (to epoch seconds) + } else if (prefixEq(command, "time ", 5)) { // set time (to epoch seconds) uint32_t secs = _atoi(&command[5]); uint32_t curr = getRTCClock()->getCurrentTime(); if (secs > curr) { @@ -233,9 +238,9 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "(ERR: clock cannot go backwards)"); } - } else if (memcmp(command, "neighbors", 9) == 0) { + } else if (prefixEq(command, "neighbors", 9)) { _callbacks->formatNeighborsReply(reply); - } else if (memcmp(command, "neighbor.remove ", 16) == 0) { + } else if (prefixEq(command, "neighbor.remove ", 16)) { const char* hex = &command[16]; uint8_t pubkey[PUB_KEY_SIZE]; int hex_len = min((int)strlen(hex), PUB_KEY_SIZE*2); @@ -246,7 +251,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "ERR: bad pubkey"); } - } else if (memcmp(command, "tempradio ", 10) == 0) { + } else if (prefixEq(command, "tempradio ", 10)) { strcpy(tmp, &command[10]); const char *parts[5]; int num = mesh::Utils::parseTextParts(tmp, parts, 5); @@ -261,62 +266,62 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, invalid params"); } - } else if (memcmp(command, "password ", 9) == 0) { + } else if (prefixEq(command, "password ", 9)) { // change admin password StrHelper::strncpy(_prefs->password, &command[9], sizeof(_prefs->password)); savePrefs(); sprintf(reply, "password now: %s", _prefs->password); // echo back just to let admin know for sure!! - } else if (memcmp(command, "clear stats", 11) == 0) { + } else if (prefixEq(command, "clear stats", 11)) { _callbacks->clearStats(); strcpy(reply, "(OK - stats reset)"); /* * GET commands */ - } else if (memcmp(command, "get ", 4) == 0) { + } else if (prefixEq(command, "get ", 4)) { const char* config = &command[4]; - if (memcmp(config, "af", 2) == 0) { + if (prefixEq(config, "af", 2)) { sprintf(reply, "> %s", StrHelper::ftoa(_prefs->airtime_factor)); - } else if (memcmp(config, "int.thresh", 10) == 0) { + } else if (prefixEq(config, "int.thresh", 10)) { sprintf(reply, "> %d", (uint32_t) _prefs->interference_threshold); - } else if (memcmp(config, "agc.reset.interval", 18) == 0) { + } else if (prefixEq(config, "agc.reset.interval", 18)) { sprintf(reply, "> %d", ((uint32_t) _prefs->agc_reset_interval) * 4); - } else if (memcmp(config, "multi.acks", 10) == 0) { + } else if (prefixEq(config, "multi.acks", 10)) { sprintf(reply, "> %d", (uint32_t) _prefs->multi_acks); - } else if (memcmp(config, "allow.read.only", 15) == 0) { + } else if (prefixEq(config, "allow.read.only", 15)) { sprintf(reply, "> %s", _prefs->allow_read_only ? "on" : "off"); - } else if (memcmp(config, "flood.advert.interval", 21) == 0) { + } else if (prefixEq(config, "flood.advert.interval", 21)) { sprintf(reply, "> %d", ((uint32_t) _prefs->flood_advert_interval)); - } else if (memcmp(config, "advert.interval", 15) == 0) { + } else if (prefixEq(config, "advert.interval", 15)) { sprintf(reply, "> %d", ((uint32_t) _prefs->advert_interval) * 2); - } else if (memcmp(config, "guest.password", 14) == 0) { + } else if (prefixEq(config, "guest.password", 14)) { sprintf(reply, "> %s", _prefs->guest_password); - } else if (sender_timestamp == 0 && memcmp(config, "prv.key", 7) == 0) { // from serial command line only + } else if (sender_timestamp == 0 && prefixEq(config, "prv.key", 7)) { // from serial command line only uint8_t prv_key[PRV_KEY_SIZE]; int len = _callbacks->getSelfId().writeTo(prv_key, PRV_KEY_SIZE); mesh::Utils::toHex(tmp, prv_key, len); sprintf(reply, "> %s", tmp); - } else if (memcmp(config, "name", 4) == 0) { + } else if (prefixEq(config, "name", 4)) { sprintf(reply, "> %s", _prefs->node_name); - } else if (memcmp(config, "repeat", 6) == 0) { + } else if (prefixEq(config, "repeat", 6)) { sprintf(reply, "> %s", _prefs->disable_fwd ? "off" : "on"); - } else if (memcmp(config, "lat", 3) == 0) { + } else if (prefixEq(config, "lat", 3)) { sprintf(reply, "> %s", StrHelper::ftoa(_prefs->node_lat)); - } else if (memcmp(config, "lon", 3) == 0) { + } else if (prefixEq(config, "lon", 3)) { sprintf(reply, "> %s", StrHelper::ftoa(_prefs->node_lon)); - } else if (memcmp(config, "radio", 5) == 0) { + } else if (prefixEq(config, "radio", 5)) { char freq[16], bw[16]; strcpy(freq, StrHelper::ftoa(_prefs->freq)); strcpy(bw, StrHelper::ftoa3(_prefs->bw)); sprintf(reply, "> %s,%s,%d,%d", freq, bw, (uint32_t)_prefs->sf, (uint32_t)_prefs->cr); - } else if (memcmp(config, "rxdelay", 7) == 0) { + } else if (prefixEq(config, "rxdelay", 7)) { sprintf(reply, "> %s", StrHelper::ftoa(_prefs->rx_delay_base)); - } else if (memcmp(config, "txdelay", 7) == 0) { + } else if (prefixEq(config, "txdelay", 7)) { sprintf(reply, "> %s", StrHelper::ftoa(_prefs->tx_delay_factor)); - } else if (memcmp(config, "flood.max", 9) == 0) { + } else if (prefixEq(config, "flood.max", 9)) { sprintf(reply, "> %d", (uint32_t)_prefs->flood_max); - } else if (memcmp(config, "direct.txdelay", 14) == 0) { + } else if (prefixEq(config, "direct.txdelay", 14)) { sprintf(reply, "> %s", StrHelper::ftoa(_prefs->direct_tx_delay_factor)); - } else if (memcmp(config, "owner.info", 10) == 0) { + } else if (prefixEq(config, "owner.info", 10)) { *reply++ = '>'; *reply++ = ' '; const char* sp = _prefs->owner_info; @@ -325,16 +330,16 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch sp++; } *reply = 0; // set null terminator - } else if (memcmp(config, "tx", 2) == 0 && (config[2] == 0 || config[2] == ' ')) { + } else if (prefixEq(config, "tx", 2) && (config[2] == 0 || config[2] == ' ')) { sprintf(reply, "> %d", (int32_t) _prefs->tx_power_dbm); - } else if (memcmp(config, "freq", 4) == 0) { + } else if (prefixEq(config, "freq", 4)) { sprintf(reply, "> %s", StrHelper::ftoa(_prefs->freq)); - } else if (memcmp(config, "public.key", 10) == 0) { + } else if (prefixEq(config, "public.key", 10)) { strcpy(reply, "> "); mesh::Utils::toHex(&reply[2], _callbacks->getSelfId().pub_key, PUB_KEY_SIZE); - } else if (memcmp(config, "role", 4) == 0) { + } else if (prefixEq(config, "role", 4)) { sprintf(reply, "> %s", _callbacks->getRole()); - } else if (memcmp(config, "bridge.type", 11) == 0) { + } else if (prefixEq(config, "bridge.type", 11)) { sprintf(reply, "> %s", #ifdef WITH_RS232_BRIDGE "rs232" @@ -345,24 +350,24 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch #endif ); #ifdef WITH_BRIDGE - } else if (memcmp(config, "bridge.enabled", 14) == 0) { + } else if (prefixEq(config, "bridge.enabled", 14)) { sprintf(reply, "> %s", _prefs->bridge_enabled ? "on" : "off"); - } else if (memcmp(config, "bridge.delay", 12) == 0) { + } else if (prefixEq(config, "bridge.delay", 12)) { sprintf(reply, "> %d", (uint32_t)_prefs->bridge_delay); - } else if (memcmp(config, "bridge.source", 13) == 0) { + } else if (prefixEq(config, "bridge.source", 13)) { sprintf(reply, "> %s", _prefs->bridge_pkt_src ? "logRx" : "logTx"); #endif #ifdef WITH_RS232_BRIDGE - } else if (memcmp(config, "bridge.baud", 11) == 0) { + } else if (prefixEq(config, "bridge.baud", 11)) { sprintf(reply, "> %d", (uint32_t)_prefs->bridge_baud); #endif #ifdef WITH_ESPNOW_BRIDGE - } else if (memcmp(config, "bridge.channel", 14) == 0) { + } else if (prefixEq(config, "bridge.channel", 14)) { sprintf(reply, "> %d", (uint32_t)_prefs->bridge_channel); - } else if (memcmp(config, "bridge.secret", 13) == 0) { + } else if (prefixEq(config, "bridge.secret", 13)) { sprintf(reply, "> %s", _prefs->bridge_secret); #endif - } else if (memcmp(config, "bootloader.ver", 14) == 0) { + } else if (prefixEq(config, "bootloader.ver", 14)) { #ifdef NRF52_PLATFORM char ver[32]; if (_board->getBootloaderVersion(ver, sizeof(ver))) { @@ -373,7 +378,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch #else strcpy(reply, "ERROR: unsupported"); #endif - } else if (memcmp(config, "adc.multiplier", 14) == 0) { + } else if (prefixEq(config, "adc.multiplier", 14)) { float adc_mult = _board->getAdcMultiplier(); if (adc_mult == 0.0f) { strcpy(reply, "Error: unsupported by this board"); @@ -381,19 +386,19 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch sprintf(reply, "> %.3f", adc_mult); } // Power management commands - } else if (memcmp(config, "pwrmgt.support", 14) == 0) { + } else if (prefixEq(config, "pwrmgt.support", 14)) { #ifdef NRF52_POWER_MANAGEMENT strcpy(reply, "> supported"); #else strcpy(reply, "> unsupported"); #endif - } else if (memcmp(config, "pwrmgt.source", 13) == 0) { + } else if (prefixEq(config, "pwrmgt.source", 13)) { #ifdef NRF52_POWER_MANAGEMENT strcpy(reply, _board->isExternalPowered() ? "> external" : "> battery"); #else strcpy(reply, "ERROR: Power management not supported"); #endif - } else if (memcmp(config, "pwrmgt.bootreason", 17) == 0) { + } else if (prefixEq(config, "pwrmgt.bootreason", 17)) { #ifdef NRF52_POWER_MANAGEMENT sprintf(reply, "> Reset: %s; Shutdown: %s", _board->getResetReasonString(_board->getResetReason()), @@ -401,7 +406,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch #else strcpy(reply, "ERROR: Power management not supported"); #endif - } else if (memcmp(config, "pwrmgt.bootmv", 13) == 0) { + } else if (prefixEq(config, "pwrmgt.bootmv", 13)) { #ifdef NRF52_POWER_MANAGEMENT sprintf(reply, "> %u mV", _board->getBootVoltage()); #else @@ -413,29 +418,29 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch /* * SET commands */ - } else if (memcmp(command, "set ", 4) == 0) { + } else if (prefixEq(command, "set ", 4)) { const char* config = &command[4]; - if (memcmp(config, "af ", 3) == 0) { + if (prefixEq(config, "af ", 3)) { _prefs->airtime_factor = atof(&config[3]); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "int.thresh ", 11) == 0) { + } else if (prefixEq(config, "int.thresh ", 11)) { _prefs->interference_threshold = atoi(&config[11]); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "agc.reset.interval ", 19) == 0) { + } else if (prefixEq(config, "agc.reset.interval ", 19)) { _prefs->agc_reset_interval = atoi(&config[19]) / 4; savePrefs(); sprintf(reply, "OK - interval rounded to %d", ((uint32_t) _prefs->agc_reset_interval) * 4); - } else if (memcmp(config, "multi.acks ", 11) == 0) { + } else if (prefixEq(config, "multi.acks ", 11)) { _prefs->multi_acks = atoi(&config[11]); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "allow.read.only ", 16) == 0) { - _prefs->allow_read_only = memcmp(&config[16], "on", 2) == 0; + } else if (prefixEq(config, "allow.read.only ", 16)) { + _prefs->allow_read_only = prefixEq(&config[16], "on", 2); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "flood.advert.interval ", 22) == 0) { + } else if (prefixEq(config, "flood.advert.interval ", 22)) { int hours = _atoi(&config[22]); if ((hours > 0 && hours < 3) || (hours > 168)) { strcpy(reply, "Error: interval range is 3-168 hours"); @@ -445,7 +450,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch savePrefs(); strcpy(reply, "OK"); } - } else if (memcmp(config, "advert.interval ", 16) == 0) { + } else if (prefixEq(config, "advert.interval ", 16)) { int mins = _atoi(&config[16]); if ((mins > 0 && mins < MIN_LOCAL_ADVERT_INTERVAL) || (mins > 240)) { sprintf(reply, "Error: interval range is %d-240 minutes", MIN_LOCAL_ADVERT_INTERVAL); @@ -455,11 +460,11 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch savePrefs(); strcpy(reply, "OK"); } - } else if (memcmp(config, "guest.password ", 15) == 0) { + } else if (prefixEq(config, "guest.password ", 15)) { StrHelper::strncpy(_prefs->guest_password, &config[15], sizeof(_prefs->guest_password)); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "prv.key ", 8) == 0) { + } else if (prefixEq(config, "prv.key ", 8)) { uint8_t prv_key[PRV_KEY_SIZE]; bool success = mesh::Utils::fromHex(prv_key, PRV_KEY_SIZE, &config[8]); // only allow rekey if key is valid @@ -472,7 +477,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, bad key"); } - } else if (memcmp(config, "name ", 5) == 0) { + } else if (prefixEq(config, "name ", 5)) { if (isValidName(&config[5])) { StrHelper::strncpy(_prefs->node_name, &config[5], sizeof(_prefs->node_name)); savePrefs(); @@ -480,11 +485,11 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, bad chars"); } - } else if (memcmp(config, "repeat ", 7) == 0) { - _prefs->disable_fwd = memcmp(&config[7], "off", 3) == 0; + } else if (prefixEq(config, "repeat ", 7)) { + _prefs->disable_fwd = prefixEq(&config[7], "off", 3); savePrefs(); strcpy(reply, _prefs->disable_fwd ? "OK - repeat is now OFF" : "OK - repeat is now ON"); - } else if (memcmp(config, "radio ", 6) == 0) { + } else if (prefixEq(config, "radio ", 6)) { strcpy(tmp, &config[6]); const char *parts[4]; int num = mesh::Utils::parseTextParts(tmp, parts, 4); @@ -502,15 +507,15 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, invalid radio params"); } - } else if (memcmp(config, "lat ", 4) == 0) { + } else if (prefixEq(config, "lat ", 4)) { _prefs->node_lat = atof(&config[4]); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "lon ", 4) == 0) { + } else if (prefixEq(config, "lon ", 4)) { _prefs->node_lon = atof(&config[4]); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "rxdelay ", 8) == 0) { + } else if (prefixEq(config, "rxdelay ", 8)) { float db = atof(&config[8]); if (db >= 0) { _prefs->rx_delay_base = db; @@ -519,7 +524,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, cannot be negative"); } - } else if (memcmp(config, "txdelay ", 8) == 0) { + } else if (prefixEq(config, "txdelay ", 8)) { float f = atof(&config[8]); if (f >= 0) { _prefs->tx_delay_factor = f; @@ -528,7 +533,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, cannot be negative"); } - } else if (memcmp(config, "flood.max ", 10) == 0) { + } else if (prefixEq(config, "flood.max ", 10)) { uint8_t m = atoi(&config[10]); if (m <= 64) { _prefs->flood_max = m; @@ -537,7 +542,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, max 64"); } - } else if (memcmp(config, "direct.txdelay ", 15) == 0) { + } else if (prefixEq(config, "direct.txdelay ", 15)) { float f = atof(&config[15]); if (f >= 0) { _prefs->direct_tx_delay_factor = f; @@ -546,7 +551,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error, cannot be negative"); } - } else if (memcmp(config, "owner.info ", 11) == 0) { + } else if (prefixEq(config, "owner.info ", 11)) { config += 11; char *dp = _prefs->owner_info; while (*config && dp - _prefs->owner_info < sizeof(_prefs->owner_info)-1) { @@ -556,22 +561,22 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch *dp = 0; savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "tx ", 3) == 0) { + } else if (prefixEq(config, "tx ", 3)) { _prefs->tx_power_dbm = atoi(&config[3]); savePrefs(); _callbacks->setTxPower(_prefs->tx_power_dbm); strcpy(reply, "OK"); - } else if (sender_timestamp == 0 && memcmp(config, "freq ", 5) == 0) { + } else if (sender_timestamp == 0 && prefixEq(config, "freq ", 5)) { _prefs->freq = atof(&config[5]); savePrefs(); strcpy(reply, "OK - reboot to apply"); #ifdef WITH_BRIDGE - } else if (memcmp(config, "bridge.enabled ", 15) == 0) { - _prefs->bridge_enabled = memcmp(&config[15], "on", 2) == 0; + } else if (prefixEq(config, "bridge.enabled ", 15)) { + _prefs->bridge_enabled = prefixEq(&config[15], "on", 2); _callbacks->setBridgeState(_prefs->bridge_enabled); savePrefs(); strcpy(reply, "OK"); - } else if (memcmp(config, "bridge.delay ", 13) == 0) { + } else if (prefixEq(config, "bridge.delay ", 13)) { int delay = _atoi(&config[13]); if (delay >= 0 && delay <= 10000) { _prefs->bridge_delay = (uint16_t)delay; @@ -580,13 +585,13 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error: delay must be between 0-10000 ms"); } - } else if (memcmp(config, "bridge.source ", 14) == 0) { - _prefs->bridge_pkt_src = memcmp(&config[14], "rx", 2) == 0; + } else if (prefixEq(config, "bridge.source ", 14)) { + _prefs->bridge_pkt_src = prefixEq(&config[14], "rx", 2); savePrefs(); strcpy(reply, "OK"); #endif #ifdef WITH_RS232_BRIDGE - } else if (memcmp(config, "bridge.baud ", 12) == 0) { + } else if (prefixEq(config, "bridge.baud ", 12)) { uint32_t baud = atoi(&config[12]); if (baud >= 9600 && baud <= 115200) { _prefs->bridge_baud = (uint32_t)baud; @@ -598,7 +603,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } #endif #ifdef WITH_ESPNOW_BRIDGE - } else if (memcmp(config, "bridge.channel ", 15) == 0) { + } else if (prefixEq(config, "bridge.channel ", 15)) { int ch = atoi(&config[15]); if (ch > 0 && ch < 15) { _prefs->bridge_channel = (uint8_t)ch; @@ -608,13 +613,13 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "Error: channel must be between 1-14"); } - } else if (memcmp(config, "bridge.secret ", 14) == 0) { + } else if (prefixEq(config, "bridge.secret ", 14)) { StrHelper::strncpy(_prefs->bridge_secret, &config[14], sizeof(_prefs->bridge_secret)); _callbacks->restartBridge(); savePrefs(); strcpy(reply, "OK"); #endif - } else if (memcmp(config, "adc.multiplier ", 15) == 0) { + } else if (prefixEq(config, "adc.multiplier ", 15)) { _prefs->adc_multiplier = atof(&config[15]); if (_board->setAdcMultiplier(_prefs->adc_multiplier)) { savePrefs(); @@ -633,11 +638,11 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else if (sender_timestamp == 0 && strcmp(command, "erase") == 0) { bool s = _callbacks->formatFileSystem(); sprintf(reply, "File system erase: %s", s ? "OK" : "Err"); - } else if (memcmp(command, "ver", 3) == 0) { + } else if (prefixEq(command, "ver", 3)) { sprintf(reply, "%s (Build: %s)", _callbacks->getFirmwareVer(), _callbacks->getBuildDate()); - } else if (memcmp(command, "board", 5) == 0) { + } else if (prefixEq(command, "board", 5)) { sprintf(reply, "%s", _board->getManufacturerName()); - } else if (memcmp(command, "sensor get ", 11) == 0) { + } else if (prefixEq(command, "sensor get ", 11)) { const char* key = command + 11; const char* val = _sensors->getSettingByKey(key); if (val != NULL) { @@ -645,7 +650,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "null"); } - } else if (memcmp(command, "sensor set ", 11) == 0) { + } else if (prefixEq(command, "sensor set ", 11)) { strcpy(tmp, &command[11]); const char *parts[2]; int num = mesh::Utils::parseTextParts(tmp, parts, 2, ' '); @@ -656,7 +661,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "can't find custom var"); } - } else if (memcmp(command, "sensor list", 11) == 0) { + } else if (prefixEq(command, "sensor list", 11)) { char* dp = reply; int start = 0; int end = _sensors->getNumSettings(); @@ -682,7 +687,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } } #if ENV_INCLUDE_GPS == 1 - } else if (memcmp(command, "gps on", 6) == 0) { + } else if (prefixEq(command, "gps on", 6)) { if (_sensors->setSettingValue("gps", "1")) { _prefs->gps_enabled = 1; savePrefs(); @@ -690,7 +695,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "gps toggle not found"); } - } else if (memcmp(command, "gps off", 7) == 0) { + } else if (prefixEq(command, "gps off", 7)) { if (_sensors->setSettingValue("gps", "0")) { _prefs->gps_enabled = 0; savePrefs(); @@ -698,17 +703,17 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "gps toggle not found"); } - } else if (memcmp(command, "gps sync", 8) == 0) { + } else if (prefixEq(command, "gps sync", 8)) { LocationProvider * l = _sensors->getLocationProvider(); if (l != NULL) { l->syncTime(); } - } else if (memcmp(command, "gps setloc", 10) == 0) { + } else if (prefixEq(command, "gps setloc", 10)) { _prefs->node_lat = _sensors->node_lat; _prefs->node_lon = _sensors->node_lon; savePrefs(); strcpy(reply, "ok"); - } else if (memcmp(command, "gps advert", 10) == 0) { + } else if (prefixEq(command, "gps advert", 10)) { if (strlen(command) == 10) { switch (_prefs->advert_loc_policy) { case ADVERT_LOC_NONE: @@ -723,22 +728,22 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch default: strcpy(reply, "error"); } - } else if (memcmp(command+11, "none", 4) == 0) { + } else if (prefixEq(command+11, "none", 4)) { _prefs->advert_loc_policy = ADVERT_LOC_NONE; savePrefs(); strcpy(reply, "ok"); - } else if (memcmp(command+11, "share", 5) == 0) { + } else if (prefixEq(command+11, "share", 5)) { _prefs->advert_loc_policy = ADVERT_LOC_SHARE; savePrefs(); strcpy(reply, "ok"); - } else if (memcmp(command+11, "prefs", 4) == 0) { + } else if (prefixEq(command+11, "prefs", 4)) { _prefs->advert_loc_policy = ADVERT_LOC_PREFS; savePrefs(); strcpy(reply, "ok"); } else { strcpy(reply, "error"); } - } else if (memcmp(command, "gps", 3) == 0) { + } else if (prefixEq(command, "gps", 3)) { LocationProvider * l = _sensors->getLocationProvider(); if (l != NULL) { bool enabled = l->isEnabled(); // is EN pin on ? @@ -757,37 +762,37 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch strcpy(reply, "Can't find GPS"); } #endif - } else if (memcmp(command, "powersaving on", 14) == 0) { + } else if (prefixEq(command, "powersaving on", 14)) { _prefs->powersaving_enabled = 1; savePrefs(); strcpy(reply, "ok"); // TODO: to return Not supported if required - } else if (memcmp(command, "powersaving off", 15) == 0) { + } else if (prefixEq(command, "powersaving off", 15)) { _prefs->powersaving_enabled = 0; savePrefs(); strcpy(reply, "ok"); - } else if (memcmp(command, "powersaving", 11) == 0) { + } else if (prefixEq(command, "powersaving", 11)) { if (_prefs->powersaving_enabled) { strcpy(reply, "on"); } else { strcpy(reply, "off"); } - } else if (memcmp(command, "log start", 9) == 0) { + } else if (prefixEq(command, "log start", 9)) { _callbacks->setLoggingOn(true); strcpy(reply, " logging on"); - } else if (memcmp(command, "log stop", 8) == 0) { + } else if (prefixEq(command, "log stop", 8)) { _callbacks->setLoggingOn(false); strcpy(reply, " logging off"); - } else if (memcmp(command, "log erase", 9) == 0) { + } else if (prefixEq(command, "log erase", 9)) { _callbacks->eraseLogFile(); strcpy(reply, " log erased"); - } else if (sender_timestamp == 0 && memcmp(command, "log", 3) == 0) { + } else if (sender_timestamp == 0 && prefixEq(command, "log", 3)) { _callbacks->dumpLogFile(); strcpy(reply, " EOF"); - } else if (sender_timestamp == 0 && memcmp(command, "stats-packets", 13) == 0 && (command[13] == 0 || command[13] == ' ')) { + } else if (sender_timestamp == 0 && prefixEq(command, "stats-packets", 13) && (command[13] == 0 || command[13] == ' ')) { _callbacks->formatPacketStatsReply(reply); - } else if (sender_timestamp == 0 && memcmp(command, "stats-radio", 11) == 0 && (command[11] == 0 || command[11] == ' ')) { + } else if (sender_timestamp == 0 && prefixEq(command, "stats-radio", 11) && (command[11] == 0 || command[11] == ' ')) { _callbacks->formatRadioStatsReply(reply); - } else if (sender_timestamp == 0 && memcmp(command, "stats-core", 10) == 0 && (command[10] == 0 || command[10] == ' ')) { + } else if (sender_timestamp == 0 && prefixEq(command, "stats-core", 10) && (command[10] == 0 || command[10] == ' ')) { _callbacks->formatStatsReply(reply); } else { strcpy(reply, "Unknown command");