Advertisement
michalmonday

esp8266_deauther (functions.h parseJSONFile examine)

Jul 15th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.32 KB | None | 0 0
  1. #ifndef functions_h
  2. #define functions_h
  3.  
  4. #include "Arduino.h"
  5. #include <FS.h>
  6. extern "C" {
  7. #include "user_interface.h"
  8. }
  9. #include "ArduinoJson.h"
  10.  
  11. /*
  12. Here is a collection of useful functions and variables.
  13. They are used globally via an 'extern' reference in every class.
  14. Making everything static will lead to problems with the Arduino ESP8266 2.0.0 SDK,
  15. there were some fixed in later version but we need to use the old version for injecting deauth packets.
  16. */
  17.  
  18. uint8_t broadcast[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  19. uint8_t wifi_channel = 1;
  20.  
  21. // ===== UTF8 FIX ===== //
  22. String escape(String str) {
  23. str.replace(String(BACKSLASH), String(BACKSLASH) + String(BACKSLASH));
  24. str.replace(String(DOUBLEQUOTES), String(BACKSLASH) + String(DOUBLEQUOTES));
  25. return str;
  26. }
  27.  
  28. bool ascii(char c) {
  29. return c >= 0 && c <= 127;
  30. }
  31.  
  32. bool printableAscii(char c) {
  33. return c >= 32 && c <= 126;
  34. }
  35.  
  36. bool getBit(uint8_t b, uint8_t n) {
  37. return (b >> n) % 2 != 0;
  38. }
  39.  
  40. uint8_t utf8(uint8_t c) {
  41. if (!getBit(c, 7)) return 1;
  42.  
  43. if (getBit(c, 7) && getBit(c, 6) && !getBit(c, 5)) return 2;
  44.  
  45. if (getBit(c, 7) && getBit(c, 6) && getBit(c, 5) && !getBit(c, 4)) return 3;
  46.  
  47. if (getBit(c, 7) && getBit(c, 6) && getBit(c, 5) && getBit(c, 4) && !getBit(c, 3)) return 4;
  48.  
  49. return 0;
  50. }
  51.  
  52. bool utf8Part(uint8_t c) {
  53. return getBit(c, 7) && !getBit(c, 6);
  54. }
  55.  
  56. String fixUtf8(String str) {
  57. int size = str.length();
  58.  
  59. String result = String();
  60. char c;
  61. uint8_t len;
  62. bool ok;
  63.  
  64. for (int i = 0; i < size; i++) {
  65. c = str.charAt(i); // get character
  66. len = utf8(c); // get utf8 char len
  67.  
  68. if (len <= 1) {
  69. result += c; // when 1 byte char, add it :)
  70. }
  71. else if (i + len > size) { // when char bigger than remaining string, end loop
  72. i = size + 1;
  73. }
  74. else {
  75. ok = true;
  76.  
  77. for (int j = 1; j < len && ok; j++) {
  78. ok = utf8Part(str.charAt(i + j)); // if following char is compliant or not
  79. }
  80.  
  81. if (ok) result += c; // everything is ok, add char and continue
  82. else { // utf8 char is broken
  83. for (int j = 1; j < len; j++) { // go through the next bytes
  84. c = str.charAt(i + j);
  85.  
  86. if (utf8(c) == 1) result += c; // when byte is ascii, add it :)
  87. }
  88. i += len - 1; // skip utf8 char because we already managed it
  89. }
  90. }
  91. }
  92. return result;
  93. }
  94.  
  95. String removeUtf8(String str) {
  96. str = fixUtf8(str); // fix it in case a utf char is broken
  97. int size = str.length();
  98.  
  99. String result = String();
  100. char c;
  101. uint8_t len;
  102.  
  103. for (int i = 0; i < size; i++) {
  104. c = str.charAt(i); // get character
  105. len = utf8(c); // get utf8 char len
  106.  
  107. if (len <= 1) result += c; // when 1 byte char, add it :)
  108. else i += len - 1; // skip other chars
  109. }
  110.  
  111. return result;
  112. }
  113.  
  114. int utf8Len(String str) {
  115. int size = str.length();
  116.  
  117. int result = 0;
  118. char c;
  119. uint8_t len;
  120.  
  121. for (int i = 0; i < size; i++) {
  122. c = str.charAt(i); // get character
  123. len = utf8(c); // get utf8 char len
  124.  
  125. if (len <= 1) result++; // when 1 byte char, add 1 :)
  126. else {
  127. result++;
  128.  
  129. for (int j = 1; j < len; j++) {
  130. c = str.charAt(i + j);
  131.  
  132. if (!utf8Part(c) && (utf8(c) == 1)) {
  133. Serial.println(c, HEX);
  134. result++; // if following char is compliant or not
  135. }
  136. }
  137. i += len - 1;
  138. }
  139. }
  140.  
  141. return result;
  142. }
  143.  
  144. String replaceUtf8(String str, String r) {
  145. str = fixUtf8(str); // fix it in case a utf char is broken
  146. int size = str.length();
  147.  
  148. String result = String();
  149. char c;
  150. uint8_t len;
  151.  
  152. for (int i = 0; i < size; i++) {
  153. c = str.charAt(i); // get character
  154. len = utf8(c); // get utf8 char len
  155.  
  156. if (len <= 1) result += c; // when 1 byte char, add it :)
  157. else {
  158. result += r;
  159. i += len - 1; // skip other chars
  160. }
  161. }
  162.  
  163. return result;
  164. }
  165.  
  166. // ===== LANGUAGE STRING FUNCTIONS ===== //
  167.  
  168. // for reading Strings from the PROGMEM
  169. String str(const char* ptr) {
  170. char keyword[strlen_P(ptr)];
  171.  
  172. strcpy_P(keyword, ptr);
  173. return String(keyword);
  174. }
  175.  
  176. // for converting keywords
  177. String keyword(const char* keywordPtr) {
  178. char keyword[strlen_P(keywordPtr)];
  179.  
  180. strcpy_P(keyword, keywordPtr);
  181.  
  182. String str = "";
  183. uint8_t len = strlen(keyword);
  184. uint8_t i = 0;
  185.  
  186. while (i < len && keyword[i] != SLASH && keyword[i] != COMMA) {
  187. str += keyword[i];
  188. i++;
  189. }
  190.  
  191. return str;
  192. }
  193.  
  194. // equals function
  195. bool eqls(const char* str, const char* keywordPtr) {
  196. if (strlen(str) > 255) return false; // when string too long
  197.  
  198. char keyword[strlen_P(keywordPtr) + 1];
  199. strcpy_P(keyword, keywordPtr);
  200.  
  201. uint8_t lenStr = strlen(str);
  202. uint8_t lenKeyword = strlen(keyword);
  203.  
  204. if (lenStr > lenKeyword) return false; // string can't be longer than keyword (but can be smaller because of '/'
  205. // and ',')
  206.  
  207. uint8_t a = 0;
  208. uint8_t b = 0;
  209. bool result = true;
  210.  
  211. while (a < lenStr && b < lenKeyword) {
  212. if ((keyword[b] == SLASH) || (keyword[b] == COMMA)) b++;
  213.  
  214. if (tolower(str[a]) != tolower(keyword[b])) result = false;
  215.  
  216. if (((a == lenStr) && !result) || !result) { // fast forward to next comma
  217. while (b < lenKeyword && keyword[b] != COMMA) b++;
  218. result = true;
  219. a = 0;
  220. } else {
  221. a++;
  222. b++;
  223. }
  224. }
  225. // comparison correct AND string checked until the end AND keyword checked until the end
  226. return result && a == lenStr && (keyword[b] == COMMA || keyword[b] == SLASH || keyword[b] == ENDOFLINE);
  227. }
  228.  
  229. bool eqls(String str, const char* keywordPtr) {
  230. return eqls(str.c_str(), keywordPtr);
  231. }
  232.  
  233. // boolean to string
  234. String b2s(bool input) {
  235. return str(input ? STR_TRUE : STR_FALSE);
  236. }
  237.  
  238. // boolean to asterix *
  239. String b2a(bool input) {
  240. return input ? String(ASTERIX) : String(SPACE);
  241. }
  242.  
  243. // string to boolean
  244. bool s2b(String input) {
  245. return eqls(input, STR_TRUE);
  246. }
  247.  
  248. // ===== PRINT FUNCTIONS ===== //
  249. void prnt(String s) {
  250. Serial.print(s);
  251. }
  252.  
  253. void prnt(bool b) {
  254. Serial.print(b2s(b));
  255. }
  256.  
  257. void prnt(char c) {
  258. Serial.print(c);
  259. }
  260.  
  261. void prnt(const char* ptr) {
  262. Serial.print(FPSTR(ptr));
  263. }
  264.  
  265. void prnt(int i) {
  266. Serial.print((String)i);
  267. }
  268.  
  269. void prntln() {
  270. Serial.println();
  271. }
  272.  
  273. void prntln(String s) {
  274. Serial.println(s);
  275. }
  276.  
  277. void prntln(bool b) {
  278. Serial.println(b2s(b));
  279. }
  280.  
  281. void prntln(char c) {
  282. Serial.println(c);
  283. }
  284.  
  285. void prntln(const char* ptr) {
  286. Serial.println(FPSTR(ptr));
  287. }
  288.  
  289. void prntln(int i) {
  290. Serial.println((String)i);
  291. }
  292.  
  293. /* ===== WiFi ===== */
  294. void setWifiChannel(uint8_t ch) {
  295. if ((ch != wifi_channel) && (ch > 0) && (ch < 15)) {
  296. wifi_channel = ch;
  297. wifi_set_channel(wifi_channel);
  298. }
  299. }
  300.  
  301. void setOutputPower(float dBm) {
  302. if (dBm > 20.5) {
  303. dBm = 20.5;
  304. } else if (dBm < 0) {
  305. dBm = 0;
  306. }
  307.  
  308. uint8_t val = (dBm * 4.0f);
  309. system_phy_set_max_tpw(val);
  310. }
  311.  
  312. /* ===== MAC ADDRESSES ===== */
  313. bool macBroadcast(uint8_t* mac) {
  314. for (uint8_t i = 0; i < 6; i++)
  315. if (mac[i] != broadcast[i]) return false;
  316.  
  317. return true;
  318. }
  319.  
  320. bool macValid(uint8_t* mac) {
  321. for (uint8_t i = 0; i < 6; i++)
  322. if (mac[i] != 0x00) return true;
  323.  
  324. return false;
  325. }
  326.  
  327. bool macMulticast(uint8_t* mac) {
  328. // see https://en.wikipedia.org/wiki/Multicast_address
  329. if ((mac[0] == 0x33) && (mac[1] == 0x33)) return true;
  330.  
  331. if ((mac[0] == 0x01) && (mac[1] == 0x80) && (mac[2] == 0xC2)) return true;
  332.  
  333. if ((mac[0] == 0x01) && (mac[1] == 0x00) && ((mac[2] == 0x5E) || (mac[2] == 0x0C))) return true;
  334.  
  335. if ((mac[0] == 0x01) && (mac[1] == 0x0C) && (mac[2] == 0xCD) &&
  336. ((mac[3] == 0x01) || (mac[3] == 0x02) || (mac[3] == 0x04)) &&
  337. ((mac[4] == 0x00) || (mac[4] == 0x01))) return true;
  338.  
  339. if ((mac[0] == 0x01) && (mac[1] == 0x00) && (mac[2] == 0x0C) && (mac[3] == 0xCC) && (mac[4] == 0xCC) &&
  340. ((mac[5] == 0xCC) || (mac[5] == 0xCD))) return true;
  341.  
  342. if ((mac[0] == 0x01) && (mac[1] == 0x1B) && (mac[2] == 0x19) && (mac[3] == 0x00) && (mac[4] == 0x00) &&
  343. (mac[5] == 0x00)) return true;
  344.  
  345. return false;
  346. }
  347.  
  348. /* ===== VENDOR LIST (oui.h) ===== */
  349. void getRandomMac(uint8_t* mac) {
  350. int num = random(sizeof(data_vendors) / 11 - 1);
  351. uint8_t i;
  352.  
  353. for (i = 0; i < 3; i++) mac[i] = pgm_read_byte_near(data_macs + num * 5 + i);
  354.  
  355. for (i = 3; i < 6; i++) mac[i] = random(256);
  356. }
  357.  
  358. int binSearchVendors(uint8_t* searchBytes, int lowerEnd, int upperEnd) {
  359. uint8_t listBytes[3];
  360. int res;
  361. int mid = (lowerEnd + upperEnd) / 2;
  362.  
  363. while (lowerEnd <= upperEnd) {
  364. listBytes[0] = pgm_read_byte_near(data_macs + mid * 5);
  365. listBytes[1] = pgm_read_byte_near(data_macs + mid * 5 + 1);
  366. listBytes[2] = pgm_read_byte_near(data_macs + mid * 5 + 2);
  367.  
  368. res = memcmp(searchBytes, listBytes, 3);
  369.  
  370. if (res == 0) {
  371. return mid;
  372. } else if (res < 0) {
  373. upperEnd = mid - 1;
  374. mid = (lowerEnd + upperEnd) / 2;
  375. } else if (res > 0) {
  376. lowerEnd = mid + 1;
  377. mid = (lowerEnd + upperEnd) / 2;
  378. }
  379. }
  380.  
  381. return -1;
  382. }
  383.  
  384. String searchVendor(uint8_t* mac) {
  385. String vendorName = String();
  386. int pos = binSearchVendors(mac, 0, sizeof(data_macs) / 5 - 1);
  387. int realPos = pgm_read_byte_near(data_macs + pos * 5 + 3) | pgm_read_byte_near(data_macs + pos * 5 + 4) << 8;
  388.  
  389. if (pos >= 0) {
  390. char tmp;
  391.  
  392. for (int i = 0; i < 8; i++) {
  393. tmp = (char)pgm_read_byte_near(data_vendors + realPos * 8 + i);
  394.  
  395. if (tmp != ENDOFLINE) vendorName += tmp;
  396. tmp += SPACE;
  397. }
  398. }
  399.  
  400. return vendorName;
  401. }
  402.  
  403. /* ===== STRING ===== */
  404. String bytesToStr(uint8_t* b, uint32_t size) {
  405. String str;
  406.  
  407. for (uint32_t i = 0; i < size; i++) {
  408. if (b[i] < 0x10) str += ZERO;
  409. str += String(b[i], HEX);
  410.  
  411. if (i < size - 1) str += DOUBLEPOINT;
  412. }
  413. return str;
  414. }
  415.  
  416. String macToStr(uint8_t* mac) {
  417. return bytesToStr(mac, 6);
  418. }
  419.  
  420. bool strToMac(String macStr, uint8_t* mac) {
  421. macStr.replace(String(DOUBLEPOINT), String()); // ":" -> ""
  422. macStr.replace("0x", String()); // "0x" -> ""
  423. macStr.replace(String(COMMA), String()); // "," -> ""
  424. macStr.replace(String(DOUBLEQUOTES), String()); // "\"" -> ""
  425. macStr.toUpperCase();
  426.  
  427. if (macStr.length() != 12) {
  428. prntln(F_ERROR_MAC);
  429. return false;
  430. }
  431.  
  432. for (uint8_t i = 0; i < 6; i++) mac[i] = strtoul((macStr.substring(i * 2, i * 2 + 2)).c_str(), NULL, 16);
  433.  
  434. return true;
  435. }
  436.  
  437. void strToColor(String str, uint8_t* buf) {
  438. str.replace(":", "");
  439. str.replace("0x", "");
  440. str.replace(",", "");
  441. str.replace("#", "");
  442. str.toUpperCase();
  443.  
  444. if (str.length() != 6) {
  445. prntln(F_COLOR_INVALID);
  446. return;
  447. }
  448.  
  449. for (uint8_t i = 0; i < 3; i++) buf[i] = strtoul((str.substring(i * 2, i * 2 + 2)).c_str(), NULL, 16);
  450. }
  451.  
  452. String buildString(String left, String right, int maxLen) {
  453. String result = left;
  454. int spacesToAdd = maxLen - left.length() /*utf8Len(left)*/ - right.length() /*utf8Len(right)*/;
  455.  
  456. for (int i = 0; i < spacesToAdd; i++) {
  457. result += SPACE;
  458. }
  459. result += right;
  460. return result;
  461. }
  462.  
  463. /* ===== SPIFFS ===== */
  464. bool progmemToSpiffs(const char* adr, int len, String path) {
  465. prnt(str(SETUP_COPYING) + path + str(SETUP_PROGMEM_TO_SPIFFS));
  466. File f = SPIFFS.open(path, "w+");
  467.  
  468. if (!f) {
  469. prntln(SETUP_ERROR);
  470. return false;
  471. }
  472.  
  473. for (int i = 0; i < len; i++) {
  474. f.write(pgm_read_byte_near(adr + i));
  475. }
  476. f.close();
  477.  
  478. prntln(SETUP_OK);
  479.  
  480. return true;
  481. }
  482.  
  483. bool readFile(String path, String& buf) {
  484. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  485. File f = SPIFFS.open(path, "r");
  486.  
  487. if (!f) return false;
  488.  
  489. if (f.size() == 0) return false;
  490.  
  491. while (f.available()) buf += (char)f.read();
  492.  
  493. f.close();
  494.  
  495. return true;
  496. }
  497.  
  498. void readFileToSerial(String path, bool showLineNum) {
  499. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  500. File f = SPIFFS.open(path, "r");
  501.  
  502. if (!f) {
  503. prnt(F_ERROR_READING_FILE);
  504. prntln(path);
  505. return;
  506. }
  507.  
  508. uint32_t c = 0;
  509. char tmp;
  510.  
  511. if (showLineNum) {
  512. prnt(buildString(String(), (String)c + String(VERTICALBAR), 6));
  513. }
  514.  
  515. while (f.available()) {
  516. tmp = f.read();
  517. prnt(tmp);
  518.  
  519. if ((tmp == NEWLINE) && showLineNum) {
  520. c++;
  521. prnt(buildString(String(), (String)c + String(VERTICALBAR), 6));
  522. }
  523. }
  524.  
  525. f.close();
  526. }
  527.  
  528. bool copyFile(String pathFrom, String pathTo) {
  529. if (pathFrom.charAt(0) != SLASH) pathFrom = String(SLASH) + pathFrom;
  530.  
  531. if (pathTo.charAt(0) != SLASH) pathTo = String(SLASH) + pathTo;
  532.  
  533. if (!SPIFFS.exists(pathFrom)) {
  534. prnt(F_ERROR_FILE);
  535. prntln(pathFrom);
  536. return false;
  537. }
  538.  
  539. File f1 = SPIFFS.open(pathFrom, "r");
  540. File f2 = SPIFFS.open(pathTo, "w+");
  541.  
  542. if (!f1 || !f2) return false;
  543.  
  544. while (f1.available()) {
  545. f2.write(f1.read());
  546. }
  547.  
  548. return true;
  549. }
  550.  
  551. bool renameFile(String pathFrom, String pathTo) {
  552. if (pathFrom.charAt(0) != SLASH) pathFrom = String(SLASH) + pathFrom;
  553.  
  554. if (pathTo.charAt(0) != SLASH) pathTo = String(SLASH) + pathTo;
  555.  
  556. if (!SPIFFS.exists(pathFrom)) {
  557. prnt(F_ERROR_FILE);
  558. prntln(pathFrom);
  559. return false;
  560. }
  561.  
  562. SPIFFS.rename(pathFrom, pathTo);
  563. return true;
  564. }
  565.  
  566. bool writeFile(String path, String& buf) {
  567. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  568. File f = SPIFFS.open(path, "w+");
  569.  
  570. if (!f) return false;
  571.  
  572. uint32_t len = buf.length();
  573.  
  574. for (uint32_t i = 0; i < len; i++) f.write(buf.charAt(i));
  575. f.close();
  576.  
  577. return true;
  578. }
  579.  
  580. bool appendFile(String path, String& buf) {
  581. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  582. File f = SPIFFS.open(path, "a+");
  583.  
  584. if (!f) return false;
  585.  
  586. uint32_t len = buf.length();
  587.  
  588. for (uint32_t i = 0; i < len; i++) f.write(buf[i]);
  589. f.close();
  590.  
  591. return true;
  592. }
  593.  
  594. void checkFile(String path, String data) {
  595. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  596.  
  597. if (!SPIFFS.exists(path)) writeFile(path, data);
  598. }
  599.  
  600. bool removeLines(String path, int lineFrom, int lineTo) {
  601. int c = 0;
  602. char tmp;
  603.  
  604. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  605.  
  606. String tmpPath = str(F_TMP) + path + str(F_COPY);
  607.  
  608. File f = SPIFFS.open(path, "r");
  609. File f2 = SPIFFS.open(tmpPath, "w");
  610.  
  611. if (!f || !f2) return false;
  612.  
  613. while (f.available()) {
  614. tmp = f.read();
  615.  
  616. if ((c < lineFrom) || (c > lineTo)) f2.write(tmp);
  617.  
  618. if (tmp == NEWLINE) c++;
  619. }
  620.  
  621. f.close();
  622. f2.close();
  623. SPIFFS.remove(path);
  624. SPIFFS.rename(tmpPath, path);
  625.  
  626. return true;
  627. }
  628.  
  629. bool replaceLine(String path, int line, String& buf) {
  630. int c = 0;
  631. char tmp;
  632.  
  633. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  634.  
  635. String tmpPath = "/tmp" + path + "_copy";
  636.  
  637. File f = SPIFFS.open(path, "r");
  638. File f2 = SPIFFS.open(tmpPath, "w");
  639.  
  640. if (!f || !f2) return false;
  641.  
  642. while (f.available()) {
  643. tmp = f.read();
  644.  
  645. if (c != line) f2.write(tmp);
  646. else {
  647. f2.println(buf);
  648.  
  649. while (f.read() != NEWLINE && f.available()) {}
  650. c++;
  651. }
  652.  
  653. if (tmp == NEWLINE) c++;
  654. }
  655.  
  656. f.close();
  657. f2.close();
  658. SPIFFS.remove(path);
  659. SPIFFS.rename(tmpPath, path);
  660.  
  661. return true;
  662. }
  663.  
  664. JsonVariant parseJSONFile(String path, DynamicJsonBuffer& jsonBuffer) {
  665. Serial.println("JsonVariant parseJSONFile(String path, DynamicJsonBuffer& jsonBuffer) entrance");
  666. delay(500);
  667. Serial.println("Arguments:");
  668. delay(500);
  669. Serial.println("path=" + path);
  670. delay(500);
  671. Serial.println("jsonBuffer(ptr?)=" + String(jsonBuffer)) // idk what datatype is DynamicJsonBuffer so maybe it will fail
  672. delay(500);
  673.  
  674. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  675.  
  676. Serial.println("JsonVariant parseJSONFile 1");
  677. delay(500);
  678.  
  679. // create JSON Variant
  680. JsonVariant root;
  681.  
  682. // create buffer
  683. String buf = "";
  684.  
  685. Serial.println("JsonVariant parseJSONFile 2");
  686. delay(500);
  687.  
  688. // read file into buffer
  689. if (!readFile(path, buf)) { // if file couldn't be opened, send 404 error
  690. Serial.println("JsonVariant parseJSONFile 2a");
  691. delay(500);
  692. prnt(F_ERROR_OPEN);
  693. prntln(path);
  694. buf = "{}";
  695. }
  696.  
  697. Serial.println("JsonVariant parseJSONFile 3");
  698. delay(500);
  699.  
  700. // parse file-buffer into a JSON Variant
  701. root = jsonBuffer.parse(buf);
  702.  
  703. Serial.println("JsonVariant parseJSONFile 4");
  704. delay(500);
  705.  
  706. // if parsing unsuccessful
  707. if (!root.success()) {
  708. Serial.println("JsonVariant parseJSONFile 4a");
  709. delay(500);
  710. prnt(F_ERROR_PARSING_JSON);
  711. prntln(path);
  712. prntln(buf);
  713. }
  714.  
  715. Serial.println("JsonVariant parseJSONFile - end of function");
  716. delay(500);
  717.  
  718. return root;
  719. }
  720.  
  721. bool removeFile(String path) {
  722. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  723. return SPIFFS.remove(path);
  724. }
  725.  
  726. void saveJSONFile(String path, JsonObject& root) {
  727. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  728.  
  729. // create buffer
  730. String buf;
  731.  
  732. // convert JSON object into string and write it into buffer
  733. root.printTo(buf);
  734.  
  735. // if buffer too big
  736. if (buf.length() > 2048) {
  737. prntln(F_ERROR_TO_BIG);
  738. prntln(path);
  739. prntln(buf);
  740. return;
  741. }
  742.  
  743. // write buffer into SPIFFS file
  744. writeFile(path, buf);
  745. }
  746.  
  747. void saveJSONFile(String path, JsonArray& root) {
  748. if (path.charAt(0) != SLASH) path = String(SLASH) + path;
  749.  
  750. // create buffer
  751. String buf;
  752.  
  753. // convert JSON object into string and write it into buffer
  754. root.printTo(buf);
  755.  
  756. // if buffer too big
  757. if (buf.length() > 2048) {
  758. prntln(F_ERROR_TO_BIG);
  759. prntln(path);
  760. prntln(buf);
  761. return;
  762. }
  763.  
  764. // write buffer into SPIFFS file
  765. writeFile(path, buf);
  766. }
  767.  
  768. String formatBytes(size_t bytes) {
  769. if (bytes < 1024) return String(bytes) + "B";
  770. else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + "KB";
  771. else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + "MB";
  772. else return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
  773. }
  774.  
  775. #endif // ifndef functions_h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement