CommonCLI.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #include <Arduino.h>
  2. #include "CommonCLI.h"
  3. #include "TxtDataHelpers.h"
  4. #include <RTClib.h>
  5. // Believe it or not, this std C function is busted on some platforms!
  6. static uint32_t _atoi(const char* sp) {
  7. uint32_t n = 0;
  8. while (*sp && *sp >= '0' && *sp <= '9') {
  9. n *= 10;
  10. n += (*sp++ - '0');
  11. }
  12. return n;
  13. }
  14. void CommonCLI::loadPrefs(FILESYSTEM* fs) {
  15. if (fs->exists("/com_prefs")) {
  16. loadPrefsInt(fs, "/com_prefs"); // new filename
  17. } else if (fs->exists("/node_prefs")) {
  18. loadPrefsInt(fs, "/node_prefs");
  19. savePrefs(fs); // save to new filename
  20. fs->remove("/node_prefs"); // remove old
  21. }
  22. }
  23. void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
  24. #if defined(RP2040_PLATFORM)
  25. File file = fs->open(filename, "r");
  26. #else
  27. File file = fs->open(filename);
  28. #endif
  29. if (file) {
  30. uint8_t pad[8];
  31. file.read((uint8_t *) &_prefs->airtime_factor, sizeof(_prefs->airtime_factor)); // 0
  32. file.read((uint8_t *) &_prefs->node_name, sizeof(_prefs->node_name)); // 4
  33. file.read(pad, 4); // 36
  34. file.read((uint8_t *) &_prefs->node_lat, sizeof(_prefs->node_lat)); // 40
  35. file.read((uint8_t *) &_prefs->node_lon, sizeof(_prefs->node_lon)); // 48
  36. file.read((uint8_t *) &_prefs->password[0], sizeof(_prefs->password)); // 56
  37. file.read((uint8_t *) &_prefs->freq, sizeof(_prefs->freq)); // 72
  38. file.read((uint8_t *) &_prefs->tx_power_dbm, sizeof(_prefs->tx_power_dbm)); // 76
  39. file.read((uint8_t *) &_prefs->disable_fwd, sizeof(_prefs->disable_fwd)); // 77
  40. file.read((uint8_t *) &_prefs->advert_interval, sizeof(_prefs->advert_interval)); // 78
  41. file.read((uint8_t *) pad, 1); // 79 was 'unused'
  42. file.read((uint8_t *) &_prefs->rx_delay_base, sizeof(_prefs->rx_delay_base)); // 80
  43. file.read((uint8_t *) &_prefs->tx_delay_factor, sizeof(_prefs->tx_delay_factor)); // 84
  44. file.read((uint8_t *) &_prefs->guest_password[0], sizeof(_prefs->guest_password)); // 88
  45. file.read((uint8_t *) &_prefs->direct_tx_delay_factor, sizeof(_prefs->direct_tx_delay_factor)); // 104
  46. file.read(pad, 4); // 108
  47. file.read((uint8_t *) &_prefs->sf, sizeof(_prefs->sf)); // 112
  48. file.read((uint8_t *) &_prefs->cr, sizeof(_prefs->cr)); // 113
  49. file.read((uint8_t *) &_prefs->allow_read_only, sizeof(_prefs->allow_read_only)); // 114
  50. file.read((uint8_t *) &_prefs->reserved2, sizeof(_prefs->reserved2)); // 115
  51. file.read((uint8_t *) &_prefs->bw, sizeof(_prefs->bw)); // 116
  52. file.read(pad, 4); // 120
  53. file.read((uint8_t *) &_prefs->flood_max, sizeof(_prefs->flood_max)); // 124
  54. file.read((uint8_t *) &_prefs->flood_advert_interval, sizeof(_prefs->flood_advert_interval)); // 125
  55. // sanitise bad pref values
  56. _prefs->rx_delay_base = constrain(_prefs->rx_delay_base, 0, 20.0f);
  57. _prefs->tx_delay_factor = constrain(_prefs->tx_delay_factor, 0, 2.0f);
  58. _prefs->direct_tx_delay_factor = constrain(_prefs->direct_tx_delay_factor, 0, 2.0f);
  59. _prefs->airtime_factor = constrain(_prefs->airtime_factor, 0, 9.0f);
  60. _prefs->freq = constrain(_prefs->freq, 400.0f, 2500.0f);
  61. _prefs->bw = constrain(_prefs->bw, 62.5f, 500.0f);
  62. _prefs->sf = constrain(_prefs->sf, 7, 12);
  63. _prefs->cr = constrain(_prefs->cr, 5, 8);
  64. _prefs->tx_power_dbm = constrain(_prefs->tx_power_dbm, 1, 30);
  65. file.close();
  66. }
  67. }
  68. void CommonCLI::savePrefs(FILESYSTEM* fs) {
  69. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  70. fs->remove("/com_prefs");
  71. File file = fs->open("/com_prefs", FILE_O_WRITE);
  72. #elif defined(RP2040_PLATFORM)
  73. File file = fs->open("/com_prefs", "w");
  74. #else
  75. File file = fs->open("/com_prefs", "w", true);
  76. #endif
  77. if (file) {
  78. uint8_t pad[8];
  79. memset(pad, 0, sizeof(pad));
  80. file.write((uint8_t *) &_prefs->airtime_factor, sizeof(_prefs->airtime_factor)); // 0
  81. file.write((uint8_t *) &_prefs->node_name, sizeof(_prefs->node_name)); // 4
  82. file.write(pad, 4); // 36
  83. file.write((uint8_t *) &_prefs->node_lat, sizeof(_prefs->node_lat)); // 40
  84. file.write((uint8_t *) &_prefs->node_lon, sizeof(_prefs->node_lon)); // 48
  85. file.write((uint8_t *) &_prefs->password[0], sizeof(_prefs->password)); // 56
  86. file.write((uint8_t *) &_prefs->freq, sizeof(_prefs->freq)); // 72
  87. file.write((uint8_t *) &_prefs->tx_power_dbm, sizeof(_prefs->tx_power_dbm)); // 76
  88. file.write((uint8_t *) &_prefs->disable_fwd, sizeof(_prefs->disable_fwd)); // 77
  89. file.write((uint8_t *) &_prefs->advert_interval, sizeof(_prefs->advert_interval)); // 78
  90. file.write((uint8_t *) pad, 1); // 79 was 'unused'
  91. file.write((uint8_t *) &_prefs->rx_delay_base, sizeof(_prefs->rx_delay_base)); // 80
  92. file.write((uint8_t *) &_prefs->tx_delay_factor, sizeof(_prefs->tx_delay_factor)); // 84
  93. file.write((uint8_t *) &_prefs->guest_password[0], sizeof(_prefs->guest_password)); // 88
  94. file.write((uint8_t *) &_prefs->direct_tx_delay_factor, sizeof(_prefs->direct_tx_delay_factor)); // 104
  95. file.write(pad, 4); // 108
  96. file.write((uint8_t *) &_prefs->sf, sizeof(_prefs->sf)); // 112
  97. file.write((uint8_t *) &_prefs->cr, sizeof(_prefs->cr)); // 113
  98. file.write((uint8_t *) &_prefs->allow_read_only, sizeof(_prefs->allow_read_only)); // 114
  99. file.write((uint8_t *) &_prefs->reserved2, sizeof(_prefs->reserved2)); // 115
  100. file.write((uint8_t *) &_prefs->bw, sizeof(_prefs->bw)); // 116
  101. file.write(pad, 4); // 120
  102. file.write((uint8_t *) &_prefs->flood_max, sizeof(_prefs->flood_max)); // 124
  103. file.write((uint8_t *) &_prefs->flood_advert_interval, sizeof(_prefs->flood_advert_interval)); // 125
  104. file.close();
  105. }
  106. }
  107. #define MIN_LOCAL_ADVERT_INTERVAL 60
  108. void CommonCLI::checkAdvertInterval() {
  109. if (_prefs->advert_interval * 2 < MIN_LOCAL_ADVERT_INTERVAL) {
  110. _prefs->advert_interval = 0; // turn it off, now that device has been manually configured
  111. }
  112. }
  113. void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) {
  114. while (*command == ' ') command++; // skip leading spaces
  115. if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
  116. memcpy(reply, command, 3); // reflect the prefix back
  117. reply += 3;
  118. command += 3;
  119. }
  120. if (memcmp(command, "reboot", 6) == 0) {
  121. _board->reboot(); // doesn't return
  122. } else if (memcmp(command, "advert", 6) == 0) {
  123. _callbacks->sendSelfAdvertisement(400);
  124. strcpy(reply, "OK - Advert sent");
  125. } else if (memcmp(command, "clock sync", 10) == 0) {
  126. uint32_t curr = getRTCClock()->getCurrentTime();
  127. if (sender_timestamp > curr) {
  128. getRTCClock()->setCurrentTime(sender_timestamp + 1);
  129. strcpy(reply, "OK - clock set");
  130. } else {
  131. strcpy(reply, "ERR: clock cannot go backwards");
  132. }
  133. } else if (memcmp(command, "start ota", 9) == 0) {
  134. if (!_board->startOTAUpdate(_prefs->node_name, reply)) {
  135. strcpy(reply, "Error");
  136. }
  137. } else if (memcmp(command, "clock", 5) == 0) {
  138. uint32_t now = getRTCClock()->getCurrentTime();
  139. DateTime dt = DateTime(now);
  140. sprintf(reply, "%02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(), dt.day(), dt.month(), dt.year());
  141. } else if (memcmp(command, "time ", 5) == 0) { // set time (to epoch seconds)
  142. uint32_t secs = _atoi(&command[5]);
  143. uint32_t curr = getRTCClock()->getCurrentTime();
  144. if (secs > curr) {
  145. getRTCClock()->setCurrentTime(secs);
  146. strcpy(reply, "(OK - clock set!)");
  147. } else {
  148. strcpy(reply, "(ERR: clock cannot go backwards)");
  149. }
  150. } else if (memcmp(command, "neighbors", 9) == 0) {
  151. _callbacks->formatNeighborsReply(reply);
  152. } else if (memcmp(command, "password ", 9) == 0) {
  153. // change admin password
  154. StrHelper::strncpy(_prefs->password, &command[9], sizeof(_prefs->password));
  155. checkAdvertInterval();
  156. savePrefs();
  157. sprintf(reply, "password now: %s", _prefs->password); // echo back just to let admin know for sure!!
  158. } else if (memcmp(command, "clear stats", 11) == 0) {
  159. _callbacks->clearStats();
  160. strcpy(reply, "(OK - stats reset)");
  161. } else if (memcmp(command, "get ", 4) == 0) {
  162. const char* config = &command[4];
  163. if (memcmp(config, "af", 2) == 0) {
  164. sprintf(reply, "> %s", StrHelper::ftoa(_prefs->airtime_factor));
  165. } else if (memcmp(config, "allow.read.only", 15) == 0) {
  166. sprintf(reply, "> %s", _prefs->allow_read_only ? "on" : "off");
  167. } else if (memcmp(config, "flood.advert.interval", 21) == 0) {
  168. sprintf(reply, "> %d", ((uint32_t) _prefs->flood_advert_interval));
  169. } else if (memcmp(config, "advert.interval", 15) == 0) {
  170. sprintf(reply, "> %d", ((uint32_t) _prefs->advert_interval) * 2);
  171. } else if (memcmp(config, "guest.password", 14) == 0) {
  172. sprintf(reply, "> %s", _prefs->guest_password);
  173. } else if (memcmp(config, "name", 4) == 0) {
  174. sprintf(reply, "> %s", _prefs->node_name);
  175. } else if (memcmp(config, "repeat", 6) == 0) {
  176. sprintf(reply, "> %s", _prefs->disable_fwd ? "off" : "on");
  177. } else if (memcmp(config, "lat", 3) == 0) {
  178. sprintf(reply, "> %s", StrHelper::ftoa(_prefs->node_lat));
  179. } else if (memcmp(config, "lon", 3) == 0) {
  180. sprintf(reply, "> %s", StrHelper::ftoa(_prefs->node_lon));
  181. } else if (memcmp(config, "radio", 5) == 0) {
  182. char freq[16], bw[16];
  183. strcpy(freq, StrHelper::ftoa(_prefs->freq));
  184. strcpy(bw, StrHelper::ftoa(_prefs->bw));
  185. sprintf(reply, "> %s,%s,%d,%d", freq, bw, (uint32_t)_prefs->sf, (uint32_t)_prefs->cr);
  186. } else if (memcmp(config, "rxdelay", 7) == 0) {
  187. sprintf(reply, "> %s", StrHelper::ftoa(_prefs->rx_delay_base));
  188. } else if (memcmp(config, "txdelay", 7) == 0) {
  189. sprintf(reply, "> %s", StrHelper::ftoa(_prefs->tx_delay_factor));
  190. } else if (memcmp(config, "flood.max", 9) == 0) {
  191. sprintf(reply, "> %d", (uint32_t)_prefs->flood_max);
  192. } else if (memcmp(config, "direct.txdelay", 14) == 0) {
  193. sprintf(reply, "> %s", StrHelper::ftoa(_prefs->direct_tx_delay_factor));
  194. } else if (memcmp(config, "tx", 2) == 0 && (config[2] == 0 || config[2] == ' ')) {
  195. sprintf(reply, "> %d", (uint32_t) _prefs->tx_power_dbm);
  196. } else if (memcmp(config, "freq", 4) == 0) {
  197. sprintf(reply, "> %s", StrHelper::ftoa(_prefs->freq));
  198. } else if (memcmp(config, "public.key", 10) == 0) {
  199. strcpy(reply, "> ");
  200. mesh::Utils::toHex(&reply[2], _callbacks->getSelfIdPubKey(), PUB_KEY_SIZE);
  201. } else if (memcmp(config, "role", 4) == 0) {
  202. sprintf(reply, "> %s", _callbacks->getRole());
  203. } else {
  204. sprintf(reply, "??: %s", config);
  205. }
  206. } else if (memcmp(command, "set ", 4) == 0) {
  207. const char* config = &command[4];
  208. if (memcmp(config, "af ", 3) == 0) {
  209. _prefs->airtime_factor = atof(&config[3]);
  210. savePrefs();
  211. strcpy(reply, "OK");
  212. } else if (memcmp(config, "allow.read.only ", 16) == 0) {
  213. _prefs->allow_read_only = memcmp(&config[16], "on", 2) == 0;
  214. savePrefs();
  215. strcpy(reply, "OK");
  216. } else if (memcmp(config, "flood.advert.interval ", 22) == 0) {
  217. int hours = _atoi(&config[22]);
  218. if (hours > 0 && hours < 3) {
  219. sprintf(reply, "Error: min is 3 hours");
  220. } else if (hours > 48) {
  221. strcpy(reply, "Error: max is 48 hours");
  222. } else {
  223. _prefs->flood_advert_interval = (uint8_t)(hours);
  224. _callbacks->updateFloodAdvertTimer();
  225. savePrefs();
  226. strcpy(reply, "OK");
  227. }
  228. } else if (memcmp(config, "advert.interval ", 16) == 0) {
  229. int mins = _atoi(&config[16]);
  230. if (mins > 0 && mins < MIN_LOCAL_ADVERT_INTERVAL) {
  231. sprintf(reply, "Error: min is %d mins", MIN_LOCAL_ADVERT_INTERVAL);
  232. } else if (mins > 240) {
  233. strcpy(reply, "Error: max is 240 mins");
  234. } else {
  235. _prefs->advert_interval = (uint8_t)(mins / 2);
  236. _callbacks->updateAdvertTimer();
  237. savePrefs();
  238. strcpy(reply, "OK");
  239. }
  240. } else if (memcmp(config, "guest.password ", 15) == 0) {
  241. StrHelper::strncpy(_prefs->guest_password, &config[15], sizeof(_prefs->guest_password));
  242. savePrefs();
  243. strcpy(reply, "OK");
  244. } else if (memcmp(config, "name ", 5) == 0) {
  245. StrHelper::strncpy(_prefs->node_name, &config[5], sizeof(_prefs->node_name));
  246. checkAdvertInterval();
  247. savePrefs();
  248. strcpy(reply, "OK");
  249. } else if (memcmp(config, "repeat ", 7) == 0) {
  250. _prefs->disable_fwd = memcmp(&config[7], "off", 3) == 0;
  251. savePrefs();
  252. strcpy(reply, _prefs->disable_fwd ? "OK - repeat is now OFF" : "OK - repeat is now ON");
  253. } else if (memcmp(config, "radio ", 6) == 0) {
  254. strcpy(tmp, &config[6]);
  255. const char *parts[4];
  256. int num = mesh::Utils::parseTextParts(tmp, parts, 4);
  257. float freq = num > 0 ? atof(parts[0]) : 0.0f;
  258. float bw = num > 1 ? atof(parts[1]) : 0.0f;
  259. uint8_t sf = num > 2 ? atoi(parts[2]) : 0;
  260. uint8_t cr = num > 3 ? atoi(parts[3]) : 0;
  261. if (freq >= 300.0f && freq <= 2500.0f && sf >= 7 && sf <= 12 && cr >= 5 && cr <= 8 && bw >= 7.0f && bw <= 500.0f) {
  262. _prefs->sf = sf;
  263. _prefs->cr = cr;
  264. _prefs->freq = freq;
  265. _prefs->bw = bw;
  266. _callbacks->savePrefs();
  267. strcpy(reply, "OK - reboot to apply");
  268. } else {
  269. strcpy(reply, "Error, invalid radio params");
  270. }
  271. } else if (memcmp(config, "lat ", 4) == 0) {
  272. _prefs->node_lat = atof(&config[4]);
  273. checkAdvertInterval();
  274. savePrefs();
  275. strcpy(reply, "OK");
  276. } else if (memcmp(config, "lon ", 4) == 0) {
  277. _prefs->node_lon = atof(&config[4]);
  278. checkAdvertInterval();
  279. savePrefs();
  280. strcpy(reply, "OK");
  281. } else if (memcmp(config, "rxdelay ", 8) == 0) {
  282. float db = atof(&config[8]);
  283. if (db >= 0) {
  284. _prefs->rx_delay_base = db;
  285. savePrefs();
  286. strcpy(reply, "OK");
  287. } else {
  288. strcpy(reply, "Error, cannot be negative");
  289. }
  290. } else if (memcmp(config, "txdelay ", 8) == 0) {
  291. float f = atof(&config[8]);
  292. if (f >= 0) {
  293. _prefs->tx_delay_factor = f;
  294. savePrefs();
  295. strcpy(reply, "OK");
  296. } else {
  297. strcpy(reply, "Error, cannot be negative");
  298. }
  299. } else if (memcmp(config, "flood.max ", 10) == 0) {
  300. uint8_t m = atoi(&config[10]);
  301. if (m <= 64) {
  302. _prefs->flood_max = m;
  303. savePrefs();
  304. strcpy(reply, "OK");
  305. } else {
  306. strcpy(reply, "Error, max 64");
  307. }
  308. } else if (memcmp(config, "direct.txdelay ", 15) == 0) {
  309. float f = atof(&config[15]);
  310. if (f >= 0) {
  311. _prefs->direct_tx_delay_factor = f;
  312. savePrefs();
  313. strcpy(reply, "OK");
  314. } else {
  315. strcpy(reply, "Error, cannot be negative");
  316. }
  317. } else if (memcmp(config, "tx ", 3) == 0) {
  318. _prefs->tx_power_dbm = atoi(&config[3]);
  319. savePrefs();
  320. _callbacks->setTxPower(_prefs->tx_power_dbm);
  321. strcpy(reply, "OK");
  322. } else if (sender_timestamp == 0 && memcmp(config, "freq ", 5) == 0) {
  323. _prefs->freq = atof(&config[5]);
  324. savePrefs();
  325. strcpy(reply, "OK - reboot to apply");
  326. } else {
  327. sprintf(reply, "unknown config: %s", config);
  328. }
  329. } else if (sender_timestamp == 0 && strcmp(command, "erase") == 0) {
  330. bool s = _callbacks->formatFileSystem();
  331. sprintf(reply, "File system erase: %s", s ? "OK" : "Err");
  332. } else if (memcmp(command, "ver", 3) == 0) {
  333. sprintf(reply, "%s (Build: %s)", _callbacks->getFirmwareVer(), _callbacks->getBuildDate());
  334. } else if (memcmp(command, "log start", 9) == 0) {
  335. _callbacks->setLoggingOn(true);
  336. strcpy(reply, " logging on");
  337. } else if (memcmp(command, "log stop", 8) == 0) {
  338. _callbacks->setLoggingOn(false);
  339. strcpy(reply, " logging off");
  340. } else if (memcmp(command, "log erase", 9) == 0) {
  341. _callbacks->eraseLogFile();
  342. strcpy(reply, " log erased");
  343. } else if (sender_timestamp == 0 && memcmp(command, "log", 3) == 0) {
  344. _callbacks->dumpLogFile();
  345. strcpy(reply, " EOF");
  346. } else {
  347. strcpy(reply, "Unknown command");
  348. }
  349. }