100 lines
3.5 KiB
C++
100 lines
3.5 KiB
C++
#pragma once
|
|
// CUSTOM (TeTeHacko): strict number arguments for CLI `set` commands.
|
|
//
|
|
// atoi()/atof() take any text: "abc" is 0, "300" goes into a uint8_t as 44,
|
|
// and "nan" is a float that no range check refuses (under -ffast-math, which
|
|
// the nRF52 builds use, the compiler may even fold that check away). The set
|
|
// commands then saved the result and answered "OK" -- `set tx 30` on an
|
|
// SX1262, `set freq abc` (150 MHz after the next boot's clamp), `set flood.max
|
|
// 300` (44). These helpers accept a whole number (or decimal) in a range and
|
|
// nothing else: optional surrounding white space and sign, digits, and for the
|
|
// float a '.' and fraction. No exponent, no hex, no trailing text.
|
|
|
|
#include <ctype.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
namespace cli_args {
|
|
|
|
// Returns true and sets *out when `s` is an integer in [lo, hi].
|
|
inline bool parseInt(const char* s, long lo, long hi, long* out) {
|
|
if (!s) return false;
|
|
while (isspace((unsigned char)*s)) s++;
|
|
const char* p = s;
|
|
if (*p == '+' || *p == '-') p++;
|
|
if (!isdigit((unsigned char)*p)) return false;
|
|
char* end;
|
|
long v = strtol(s, &end, 10);
|
|
while (isspace((unsigned char)*end)) end++;
|
|
if (*end != 0) return false;
|
|
if (v < lo || v > hi) return false;
|
|
*out = v;
|
|
return true;
|
|
}
|
|
|
|
// Returns true and sets *out when `s` is a decimal number in [lo, hi].
|
|
inline bool parseFloat(const char* s, float lo, float hi, float* out) {
|
|
if (!s) return false;
|
|
while (isspace((unsigned char)*s)) s++;
|
|
const char* p = s;
|
|
if (*p == '+' || *p == '-') p++;
|
|
bool digits = false;
|
|
while (isdigit((unsigned char)*p)) { p++; digits = true; }
|
|
if (*p == '.') {
|
|
p++;
|
|
while (isdigit((unsigned char)*p)) { p++; digits = true; }
|
|
}
|
|
if (!digits) return false;
|
|
const char* rest = p;
|
|
while (isspace((unsigned char)*rest)) rest++;
|
|
if (*rest != 0) return false; // exponent, "nan", "inf" or trailing text
|
|
float v = strtof(s, nullptr);
|
|
if (!(v >= lo && v <= hi)) return false;
|
|
*out = v;
|
|
return true;
|
|
}
|
|
|
|
// Strips leading spaces and an optional 3-byte "XX|" prefix (reflected into
|
|
// `reply`, deducting 3 from `*reply_max` when provided).
|
|
inline void stripPrefix(const char*& command, char*& reply, int* reply_max = nullptr) {
|
|
while (*command == ' ') command++;
|
|
if (strlen(command) > 4 && command[2] == '|') {
|
|
if (!reply_max || *reply_max >= 3) {
|
|
memcpy(reply, command, 3);
|
|
reply += 3;
|
|
*reply = 0;
|
|
if (reply_max) *reply_max -= 3;
|
|
}
|
|
command += 3;
|
|
while (*command == ' ') command++;
|
|
}
|
|
}
|
|
|
|
// Security gate for companion `set pin`: blocked in locked CLI rescue and over
|
|
// unencrypted local transports (USB), allowed over paired BLE or remote ACL.
|
|
inline bool canSetPin(uint32_t sender_timestamp, bool cli_rescue, bool locked_node,
|
|
bool serial_secure, const char** err_out = nullptr) {
|
|
if (locked_node && cli_rescue) {
|
|
if (err_out) *err_out = "Error: locked build";
|
|
return false;
|
|
}
|
|
if (sender_timestamp == 0 && !cli_rescue && !serial_secure) {
|
|
if (err_out) *err_out = "Error: set pin only over paired BLE";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Appends `line` to a paged CLI reply only if it fits with `trailer_reserve`
|
|
// bytes left for the end marker; returns false when the page is full.
|
|
inline bool appendLine(char*& dp, const char* reply, int max_len, int trailer_reserve, const char* line) {
|
|
int ll = (int)strlen(line);
|
|
if ((dp - reply) + ll + trailer_reserve > max_len) return false;
|
|
memcpy(dp, line, ll);
|
|
dp += ll;
|
|
return true;
|
|
}
|
|
|
|
} // namespace cli_args
|