_loraFiles.ino 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. // Version 6.1.1
  4. // Date: 2019-11-06
  5. //
  6. // based on work done by Thomas Telkamp for Raspberry PI 1ch gateway
  7. // and many others.
  8. //
  9. // All rights reserved. This program and the accompanying materials
  10. // are made available under the terms of the MIT License
  11. // which accompanies this distribution, and is available at
  12. // https://opensource.org/licenses/mit-license.php
  13. //
  14. // NO WARRANTY OF ANY KIND IS PROVIDED
  15. //
  16. // Author: Maarten Westenberg (mw12554@hotmail.com)
  17. //
  18. // This file contains the LoRa filesystem specific code
  19. // ============================================================================
  20. // LORA SPIFFS FILESYSTEM FUNCTIONS
  21. //
  22. // The LoRa supporting functions are in the section below
  23. // ----------------------------------------------------------------------------
  24. // Supporting function to readConfig
  25. // ----------------------------------------------------------------------------
  26. void id_print (String id, String val) {
  27. #if _DUSB>=1
  28. if (( debug>=0 ) && ( pdebug & P_MAIN )) {
  29. Serial.print(id);
  30. Serial.print(F("=\t"));
  31. Serial.println(val);
  32. }
  33. #endif
  34. }
  35. // ----------------------------------------------------------------------------
  36. // INITCONFIG; Init the gateway configuration file
  37. // Espcecially when calling SPIFFS.format() the gateway is left in an init
  38. // which is not very well defined. This function will init some of the settings
  39. // to well known settings.
  40. // ----------------------------------------------------------------------------
  41. int initConfig(struct espGwayConfig *c) {
  42. (*c).ch = 0;
  43. (*c).sf = _SPREADING;
  44. (*c).debug = 1;
  45. (*c).pdebug = P_GUI;
  46. (*c).cad = _CAD;
  47. (*c).hop = false;
  48. (*c).expert = false;
  49. (*c).txDelay = 0; // First Value without saving is 0;
  50. }
  51. // ----------------------------------------------------------------------------
  52. // Read the gateway configuration file
  53. // ----------------------------------------------------------------------------
  54. int readConfig(const char *fn, struct espGwayConfig *c) {
  55. int tries = 0;
  56. #if _DUSB>=1
  57. Serial.println(F("readConfig:: Starting "));
  58. #endif
  59. if (!SPIFFS.exists(fn)) {
  60. #if _DUSB>=1
  61. Serial.print(F("readConfig ERR:: file="));
  62. Serial.print(fn);
  63. Serial.println(F(" does not exist .. Formatting"));
  64. #endif
  65. SPIFFS.format();
  66. initConfig(c);
  67. return(-1);
  68. }
  69. File f = SPIFFS.open(fn, "r");
  70. if (!f) {
  71. #if _DUSB>=1
  72. Serial.println(F("ERROR:: SPIFFS open failed"));
  73. #endif
  74. return(-1);
  75. }
  76. while (f.available()) {
  77. #if _DUSB>=1
  78. if (( debug>=0 ) && ( pdebug & P_MAIN )) {
  79. Serial.print('.');
  80. }
  81. #endif
  82. // If we wait for more than 10 times, reformat the filesystem
  83. // We do this so that the system will be responsive (over OTA for example).
  84. //
  85. if (tries >= 10) {
  86. f.close();
  87. #if _DUSB>=1
  88. if (( debug>=0 ) && ( pdebug & P_MAIN ))
  89. Serial.println(F("Formatting"));
  90. #endif
  91. SPIFFS.format();
  92. initConfig(c);
  93. f = SPIFFS.open(fn, "r");
  94. tries = 0;
  95. }
  96. String id =f.readStringUntil('='); // Read keyword until '=', C++ thing
  97. String val=f.readStringUntil('\n'); // Read value until End of Line (EOL)
  98. #if _DUSB>=1
  99. Serial.print(F("readConfig:: reading line="));
  100. Serial.print(id);
  101. Serial.print(F("="));
  102. Serial.print(val);
  103. Serial.println();
  104. #endif
  105. if (id == "SSID") { // WiFi SSID
  106. id_print(id, val);
  107. (*c).ssid = val; // val contains ssid, we do NO check
  108. }
  109. else if (id == "PASS") { // WiFi Password
  110. id_print(id, val);
  111. (*c).pass = val;
  112. }
  113. else if (id == "CH") { // Frequency Channel
  114. id_print(id,val);
  115. (*c).ch = (uint32_t) val.toInt();
  116. }
  117. else if (id == "SF") { // Spreading Factor
  118. id_print(id, val);
  119. (*c).sf = (uint32_t) val.toInt();
  120. }
  121. else if (id == "FCNT") { // Frame Counter
  122. id_print(id, val);
  123. (*c).fcnt = (uint32_t) val.toInt();
  124. }
  125. else if (id == "DEBUG") { // Debug Level
  126. id_print(id, val);
  127. (*c).debug = (uint8_t) val.toInt();
  128. }
  129. else if (id == "PDEBUG") { // pDebug Pattern
  130. Serial.print(F("PDEBUG=")); Serial.println(val);
  131. (*c).pdebug = (uint8_t) val.toInt();
  132. }
  133. else if (id == "CAD") { // CAD setting
  134. Serial.print(F("CAD=")); Serial.println(val);
  135. (*c).cad = (uint8_t) val.toInt();
  136. }
  137. else if (id == "HOP") { // HOP setting
  138. Serial.print(F("HOP=")); Serial.println(val);
  139. (*c).hop = (uint8_t) val.toInt();
  140. }
  141. else if (id == "BOOTS") { // BOOTS setting
  142. id_print(id, val);
  143. (*c).boots = (uint8_t) val.toInt();
  144. }
  145. else if (id == "RESETS") { // RESET setting
  146. id_print(id, val);
  147. (*c).resets = (uint8_t) val.toInt();
  148. }
  149. else if (id == "WIFIS") { // WIFIS setting
  150. id_print(id, val);
  151. (*c).wifis = (uint8_t) val.toInt();
  152. }
  153. else if (id == "VIEWS") { // VIEWS setting
  154. id_print(id, val);
  155. (*c).views = (uint8_t) val.toInt();
  156. }
  157. else if (id == "NODE") { // NODE setting
  158. id_print(id, val);
  159. (*c).isNode = (uint8_t) val.toInt();
  160. }
  161. else if (id == "REFR") { // REFR setting
  162. id_print(id, val);
  163. (*c).refresh = (uint8_t) val.toInt();
  164. }
  165. else if (id == "REENTS") { // REENTS setting
  166. id_print(id, val);
  167. (*c).reents = (uint8_t) val.toInt();
  168. }
  169. else if (id == "NTPERR") { // NTPERR setting
  170. id_print(id, val);
  171. (*c).ntpErr = (uint8_t) val.toInt();
  172. }
  173. else if (id == "NTPETIM") { // NTPERR setting
  174. id_print(id, val);
  175. (*c).ntpErrTime = (uint32_t) val.toInt();
  176. }
  177. else if (id == "NTPS") { // NTPS setting
  178. id_print(id, val);
  179. (*c).ntps = (uint8_t) val.toInt();
  180. }
  181. else if (id == "FILENO") { // FILENO setting
  182. id_print(id, val);
  183. (*c).logFileNo = (uint8_t) val.toInt();
  184. }
  185. else if (id == "FILEREC") { // FILEREC setting
  186. id_print(id, val);
  187. (*c).logFileRec = (uint16_t) val.toInt();
  188. }
  189. else if (id == "FILENUM") { // FILEREC setting
  190. id_print(id, val);
  191. (*c).logFileNum = (uint16_t) val.toInt();
  192. }
  193. else if (id == "EXPERT") { // EXPERT setting
  194. id_print(id, val);
  195. (*c).expert = (uint8_t) val.toInt();
  196. }
  197. else if (id == "DELAY") { // DELAY setting
  198. id_print(id, val);
  199. (*c).txDelay = (int32_t) val.toInt();
  200. }
  201. else {
  202. #if _DUSB>=1
  203. Serial.print(F("readConfig:: tries++"));
  204. #endif
  205. tries++;
  206. }
  207. }
  208. f.close();
  209. #if _DUSB>=1
  210. if (debug>=0) {
  211. Serial.println(F("readConfig:: Fini"));
  212. }
  213. #endif
  214. Serial.println();
  215. return(1);
  216. }//readConfig
  217. // ----------------------------------------------------------------------------
  218. // Write the current gateway configuration to SPIFFS. First copy all the
  219. // separate data items to the gwayConfig structure
  220. //
  221. // ----------------------------------------------------------------------------
  222. int writeGwayCfg(const char *fn) {
  223. gwayConfig.ssid = WiFi.SSID();
  224. gwayConfig.pass = WiFi.psk(); // XXX We should find a way to store the password too
  225. gwayConfig.ch = ifreq; // Frequency Index
  226. gwayConfig.sf = (uint8_t) sf; // Spreading Factor
  227. gwayConfig.debug = debug;
  228. gwayConfig.pdebug = pdebug;
  229. gwayConfig.cad = _cad;
  230. gwayConfig.hop = _hop;
  231. #if GATEWAYNODE==1
  232. gwayConfig.fcnt = frameCount;
  233. #endif
  234. return(writeConfig(fn, &gwayConfig));
  235. }
  236. // ----------------------------------------------------------------------------
  237. // Write the configuration as found in the espGwayConfig structure
  238. // to SPIFFS
  239. // Parameters:
  240. // fn; Filename
  241. // c; struct config
  242. // Returns:
  243. // 1 when successful, -1 on error
  244. // ----------------------------------------------------------------------------
  245. int writeConfig(const char *fn, struct espGwayConfig *c) {
  246. if (!SPIFFS.exists(fn)) {
  247. Serial.print("WARNING:: writeConfig, file not exists, formatting ");
  248. SPIFFS.format();
  249. initConfig(c); // XXX make all initial declarations here if config vars need to have a value
  250. Serial.println(fn);
  251. }
  252. File f = SPIFFS.open(fn, "w");
  253. if (!f) {
  254. Serial.print("ERROR:: writeConfig, open file=");
  255. Serial.print(fn);
  256. Serial.println();
  257. return(-1);
  258. }
  259. f.print("SSID"); f.print('='); f.print((*c).ssid); f.print('\n');
  260. f.print("PASS"); f.print('='); f.print((*c).pass); f.print('\n');
  261. f.print("CH"); f.print('='); f.print((*c).ch); f.print('\n');
  262. f.print("SF"); f.print('='); f.print((*c).sf); f.print('\n');
  263. f.print("FCNT"); f.print('='); f.print((*c).fcnt); f.print('\n');
  264. f.print("DEBUG"); f.print('='); f.print((*c).debug); f.print('\n');
  265. f.print("PDEBUG"); f.print('='); f.print((*c).pdebug); f.print('\n');
  266. f.print("CAD"); f.print('='); f.print((*c).cad); f.print('\n');
  267. f.print("HOP"); f.print('='); f.print((*c).hop); f.print('\n');
  268. f.print("NODE"); f.print('='); f.print((*c).isNode); f.print('\n');
  269. f.print("BOOTS"); f.print('='); f.print((*c).boots); f.print('\n');
  270. f.print("RESETS"); f.print('='); f.print((*c).resets); f.print('\n');
  271. f.print("WIFIS"); f.print('='); f.print((*c).wifis); f.print('\n');
  272. f.print("VIEWS"); f.print('='); f.print((*c).views); f.print('\n');
  273. f.print("REFR"); f.print('='); f.print((*c).refresh); f.print('\n');
  274. f.print("REENTS"); f.print('='); f.print((*c).reents); f.print('\n');
  275. f.print("NTPETIM"); f.print('='); f.print((*c).ntpErrTime); f.print('\n');
  276. f.print("NTPERR"); f.print('='); f.print((*c).ntpErr); f.print('\n');
  277. f.print("NTPS"); f.print('='); f.print((*c).ntps); f.print('\n');
  278. f.print("FILEREC"); f.print('='); f.print((*c).logFileRec); f.print('\n');
  279. f.print("FILENO"); f.print('='); f.print((*c).logFileNo); f.print('\n');
  280. f.print("FILENUM"); f.print('='); f.print((*c).logFileNum); f.print('\n');
  281. f.print("EXPERT"); f.print('='); f.print((*c).expert); f.print('\n');
  282. f.print("DELAY"); f.print('='); f.print((*c).txDelay); f.print('\n');
  283. f.close();
  284. return(1);
  285. }
  286. // ----------------------------------------------------------------------------
  287. // Add a line with statistics to the log.
  288. //
  289. // We put the check in the function to protect against calling
  290. // the function without STAT_LOG being proper defined
  291. // ToDo: Store the fileNo and the fileRec in the status file to save for
  292. // restarts
  293. // Parameters:
  294. // line; char array with characters to write to log
  295. // cnt;
  296. // Returns:
  297. // <none>
  298. // ----------------------------------------------------------------------------
  299. void addLog(const unsigned char * line, int cnt)
  300. {
  301. #if STAT_LOG==1
  302. char fn[16];
  303. if (gwayConfig.logFileRec > LOGFILEREC) { // Have to make define for this
  304. gwayConfig.logFileRec = 0; // In new logFile start with record 0
  305. gwayConfig.logFileNo++; // Increase file ID
  306. gwayConfig.logFileNum++; // Increase number of log files
  307. }
  308. gwayConfig.logFileRec++;
  309. // If we have too many logfies, delete the oldest
  310. //
  311. if (gwayConfig.logFileNum > LOGFILEMAX){
  312. sprintf(fn,"/log-%d", gwayConfig.logFileNo - LOGFILEMAX);
  313. #if _DUSB>=1
  314. if (( debug>=0 ) && ( pdebug & P_GUI )) {
  315. Serial.print(F("G addLog:: Too many logfile, deleting="));
  316. Serial.println(fn);
  317. }
  318. #endif
  319. SPIFFS.remove(fn);
  320. gwayConfig.logFileNum--;
  321. }
  322. // Make sure we have the right fileno
  323. sprintf(fn,"/log-%d", gwayConfig.logFileNo);
  324. // If there is no SPIFFS, Error
  325. // Make sure to write the config record/line also
  326. if (!SPIFFS.exists(fn)) {
  327. #if _DUSB>=1
  328. if (( debug >= 1 ) && ( pdebug & P_GUI )) {
  329. Serial.print(F("G ERROR:: addLog:: file="));
  330. Serial.print(fn);
  331. Serial.print(F(" does not exist .. rec="));
  332. Serial.print(gwayConfig.logFileRec);
  333. Serial.println();
  334. }
  335. #endif
  336. }
  337. File f = SPIFFS.open(fn, "a");
  338. if (!f) {
  339. #if _DUSB>=1
  340. if (( debug>=1 ) && ( pdebug & P_GUI )) {
  341. Serial.println("G file open failed=");
  342. Serial.println(fn);
  343. }
  344. #endif
  345. return; // If file open failed, return
  346. }
  347. int i;
  348. #if _DUSB>=1
  349. if (( debug>=1 ) && ( pdebug & P_GUI )) {
  350. Serial.print(F("G addLog:: fileno="));
  351. Serial.print(gwayConfig.logFileNo);
  352. Serial.print(F(", rec="));
  353. Serial.print(gwayConfig.logFileRec);
  354. Serial.print(F(": "));
  355. for (i=0; i< 12; i++) { // The first 12 bytes contain non printable characters
  356. Serial.print(line[i],HEX);
  357. Serial.print(' ');
  358. }
  359. Serial.print((char *) &line[i]); // The rest if the buffer contains ascii
  360. Serial.println();
  361. }
  362. #endif //DUSB
  363. for (i=0; i< 12; i++) { // The first 12 bytes contain non printable characters
  364. // f.print(line[i],HEX);
  365. f.print('*');
  366. }
  367. f.write(&(line[i]), cnt-12); // write/append the line to the file
  368. f.print('\n');
  369. f.close(); // Close the file after appending to it
  370. #endif //STAT_LOG
  371. }
  372. // ----------------------------------------------------------------------------
  373. // Print (all) logfiles
  374. //
  375. // ----------------------------------------------------------------------------
  376. void printLog()
  377. {
  378. char fn[16];
  379. int i=0;
  380. #if _DUSB>=1
  381. while (i< LOGFILEMAX ) {
  382. sprintf(fn,"/log-%d", gwayConfig.logFileNo - i);
  383. if (!SPIFFS.exists(fn)) break; // break the loop
  384. // Open the file for reading
  385. File f = SPIFFS.open(fn, "r");
  386. int j;
  387. for (j=0; j<LOGFILEREC; j++) {
  388. String s=f.readStringUntil('\n');
  389. if (s.length() == 0) break;
  390. Serial.println(s.substring(12)); // Skip the first 12 Gateway specific binary characters
  391. yield();
  392. }
  393. i++;
  394. }
  395. #endif
  396. } //printLog
  397. #if _SEENMAX>0
  398. // ----------------------------------------------------------------------------
  399. // writeSeen
  400. // - Once every few messages, update the SPIFFS file and write the array.
  401. //
  402. // ----------------------------------------------------------------------------
  403. int writeSeen(struct nodeSeen *listSeen) {
  404. return(1);
  405. }
  406. // ----------------------------------------------------------------------------
  407. // printSeen
  408. // - Once every few messages, update the SPIFFS file and write the array.
  409. // ----------------------------------------------------------------------------
  410. int printSeen(struct nodeSeen *listSeen) {
  411. #if _DUSB>=1
  412. if (( debug>=0 ) && ( pdebug & P_MAIN )) {
  413. Serial.print(F("printSeen:: print list"));
  414. Serial.println();
  415. }
  416. #endif
  417. int i;
  418. #if _DUSB>=1
  419. if (( debug>=0 ) && ( pdebug & P_MAIN )) {
  420. for (i=0; i<_SEENMAX; i++) {
  421. if (listSeen[i].idSeen != 0) {
  422. String response;
  423. Serial.print(i);
  424. Serial.print(F(", TM="));
  425. stringTime(listSeen[i].timSeen, response);
  426. Serial.print(response);
  427. Serial.print(F(", addr=0x"));
  428. Serial.print(listSeen[i].idSeen,HEX);
  429. Serial.print(F(", SF=0x"));
  430. Serial.print(listSeen[i].sfSeen,HEX);
  431. Serial.println();
  432. }
  433. }
  434. }
  435. #endif
  436. return(1);
  437. }
  438. // ----------------------------------------------------------------------------
  439. // addSeen
  440. // With every new message received:
  441. // - Look whether the message is already in the array, if so update existing
  442. // message. If not, create new record.
  443. // - With this record, update the SF settings
  444. // ----------------------------------------------------------------------------
  445. int addSeen(struct nodeSeen *listSeen, uint32_t idSeen, uint8_t sfSeen, unsigned long timSeen) {
  446. int i;
  447. // ( message[4]<<24 | message[3]<<16 | message[2]<<8 | message[1] )
  448. #if _DUSB>=1
  449. if (( debug>=0 ) && ( pdebug & P_MAIN )) {
  450. Serial.print(F("addSeen:: "));
  451. // Serial.print(F(" listSeen[0]="));
  452. // Serial.print(listSeen[0].idSeen,HEX);
  453. Serial.print(F(", tim="));
  454. Serial.print(timSeen);
  455. Serial.print(F(", idSeen="));
  456. Serial.print(idSeen,HEX);
  457. Serial.print(F(", sfSeen="));
  458. Serial.print(sfSeen,HEX);
  459. Serial.println();
  460. }
  461. #endif
  462. for (i=0; i< _SEENMAX; i++) {
  463. if ((listSeen[i].idSeen==idSeen) ||
  464. (listSeen[i].idSeen==0))
  465. {
  466. break;
  467. }
  468. }
  469. if (i>=_SEENMAX) {
  470. #if _DUSB>=1
  471. if (( debug>=0 ) && ( pdebug & P_MAIN )) {
  472. Serial.print(F("addSeen:: exit=0, index="));
  473. Serial.println(i);
  474. }
  475. #endif
  476. return(0);
  477. }
  478. #if _DUSB>=2
  479. if (( debug>=0 ) && ( pdebug & P_MAIN )) {
  480. Serial.print(F("addSeen:: index="));
  481. Serial.print(i);
  482. }
  483. #endif
  484. listSeen[i].idSeen = idSeen;
  485. listSeen[i].sfSeen |= sfSeen;
  486. listSeen[i].timSeen = timSeen;
  487. return(1);
  488. }
  489. // ----------------------------------------------------------------------------
  490. // initSeen
  491. // Init the lisrScreen array
  492. // ----------------------------------------------------------------------------
  493. int initSeen(struct nodeSeen *listSeen) {
  494. int i;
  495. for (i=0; i< _SEENMAX; i++) {
  496. listSeen[i].idSeen=0;
  497. listSeen[i].sfSeen=0;
  498. listSeen[i].timSeen=0;
  499. }
  500. return(1);
  501. }
  502. // ----------------------------------------------------------------------------
  503. // listDir
  504. // List the directory and put it in
  505. // ----------------------------------------------------------------------------
  506. void listDir(char * dir)
  507. {
  508. #if _DUSB>=1
  509. // Nothing here
  510. #endif
  511. }
  512. #endif //_SEENMAX>0