_utils.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. //
  4. // based on work done by Thomas Telkamp for Raspberry PI 1ch gateway
  5. // and many others.
  6. //
  7. // All rights reserved. This program and the accompanying materials
  8. // are made available under the terms of the MIT License
  9. // which accompanies this distribution, and is available at
  10. // https://opensource.org/licenses/mit-license.php
  11. //
  12. // NO WARRANTY OF ANY KIND IS PROVIDED
  13. //
  14. // Author: Maarten Westenberg (mw12554@hotmail.com)
  15. //
  16. // This file contains the utilities for time and other functions
  17. // ========================================================================================
  18. // ==================== STRING STRING STRING ==============================================
  19. // ----------------------------------------------------------------------------------------
  20. // Print to the monitor console.
  21. // This function is used all over the gateway code as a substitue for USB debug code.
  22. // It allows webserver users to view printed/debugging code.
  23. //
  24. // Parameters:
  25. // txt: The text to be printed.
  26. // return:
  27. // <None>
  28. // ----------------------------------------------------------------------------------------
  29. static void mPrint(String txt)
  30. {
  31. # if DUSB>=1
  32. Serial.println(F(txt));
  33. if (debug>=2) Serial.flush();
  34. # endif //_DUSB
  35. # if _MONITOR>=1
  36. time_t tt = now();
  37. for (int i=_MONITOR; i>0; i--) { // Do only for values present....
  38. monitor[i]= monitor[i-1];
  39. }
  40. monitor[0].txt = String(day(tt)) + "-";
  41. monitor[0].txt += String(month(tt)) + "-";
  42. monitor[0].txt += String(year(tt)) + " ";
  43. byte _hour = hour(tt);
  44. byte _minute = minute(tt);
  45. byte _second = second(tt);
  46. if (_hour < 10) monitor[0].txt += "0"; monitor[0].txt += String( _hour )+ ":";
  47. if (_minute < 10) monitor[0].txt += "0"; monitor[0].txt += String(_minute) + ":";
  48. if (_second < 10) monitor[0].txt += "0"; monitor[0].txt += String(_second) + "- ";
  49. monitor[0].txt += String(txt);
  50. # endif //_MONITOR
  51. return;
  52. }
  53. // ----------------------------------------------------------------------------
  54. // mStat
  55. // Print the statistics on Serial (USB) port and/or Monitor
  56. // Depending on setting of _DUSB and _MONITOR.
  57. // Note: This function does not initialise the response var, will only append.
  58. // Parameters:
  59. // Interrupt: 8-bit
  60. // Response: String
  61. // Return:
  62. // 1: If successful
  63. // 0: No Success
  64. // ----------------------------------------------------------------------------
  65. int mStat(uint8_t intr, String & response)
  66. {
  67. #if _MONITOR>=1
  68. if (debug>=0) {
  69. response += "I=";
  70. if (intr & IRQ_LORA_RXTOUT_MASK) response += String("RXTOUT "); // 0x80
  71. if (intr & IRQ_LORA_RXDONE_MASK) response += String("RXDONE "); // 0x40
  72. if (intr & IRQ_LORA_CRCERR_MASK) response += "CRCERR "; // 0x20
  73. if (intr & IRQ_LORA_HEADER_MASK) response += "HEADER "; // 0x10
  74. if (intr & IRQ_LORA_TXDONE_MASK) response += "TXDONE "; // 0x08
  75. if (intr & IRQ_LORA_CDDONE_MASK) response += String("CDDONE "); // 0x04
  76. if (intr & IRQ_LORA_FHSSCH_MASK) response += "FHSSCH "; // 0x02
  77. if (intr & IRQ_LORA_CDDETD_MASK) response += "CDDETD "; // 0x01
  78. if (intr == 0x00) response += " -- ";
  79. response += ", F=" + String(ifreq);
  80. response += ", SF=" + String(sf);
  81. response += ", E=" + String(_event);
  82. response += ", S=";
  83. switch (_state) {
  84. case S_INIT:
  85. response += "INIT ";
  86. break;
  87. case S_SCAN:
  88. response += "SCAN ";
  89. break;
  90. case S_CAD:
  91. response += "CAD ";
  92. break;
  93. case S_RX:
  94. response += String("RX ");
  95. break;
  96. case S_TX:
  97. response += "TX ";
  98. break;
  99. case S_TXDONE:
  100. response += "TXDONE";
  101. break;
  102. default:
  103. response += " -- ";
  104. }
  105. response += ", eT=";
  106. response += String( micros() - eventTime );
  107. response += ", dT=";
  108. response += String( micros() - doneTime );
  109. //mPrint(response); // Do actual printing
  110. }
  111. #endif //_MONITOR
  112. return(1);
  113. }
  114. // ----------------------------------------------------------------------------------------
  115. // Fill a HEXadecimal String from a 4-byte char array
  116. //
  117. // ----------------------------------------------------------------------------------------
  118. static void printHEX(char * hexa, const char sep, String& response)
  119. {
  120. char m;
  121. m = hexa[0]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  122. m = hexa[1]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  123. m = hexa[2]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  124. m = hexa[3]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  125. }
  126. // ----------------------------------------------------------------------------------------
  127. // stringTime
  128. // Print the time t into the String reponse. t is of type time_t in seconds.
  129. // Only when RTC is present we print real time values
  130. // t contains number of seconds since system started that the event happened.
  131. // So a value of 100 would mean that the event took place 1 minute and 40 seconds ago
  132. // ----------------------------------------------------------------------------------------
  133. static void stringTime(time_t t, String& response) {
  134. if (t==0) { response += "--"; return; }
  135. // now() gives seconds since 1970
  136. // as millis() does rotate every 50 days
  137. // So we need another timing parameter
  138. time_t eTime = t;
  139. // Rest is standard
  140. byte _hour = hour(eTime);
  141. byte _minute = minute(eTime);
  142. byte _second = second(eTime);
  143. switch(weekday(eTime)) {
  144. case 1: response += "Sunday "; break;
  145. case 2: response += "Monday "; break;
  146. case 3: response += "Tuesday "; break;
  147. case 4: response += "Wednesday "; break;
  148. case 5: response += "Thursday "; break;
  149. case 6: response += "Friday "; break;
  150. case 7: response += "Saturday "; break;
  151. }
  152. response += String(day(eTime)) + "-";
  153. response += String(month(eTime)) + "-";
  154. response += String(year(eTime)) + " ";
  155. if (_hour < 10) response += "0"; response += String(_hour) + ":";
  156. if (_minute < 10) response += "0"; response += String(_minute) + ":";
  157. if (_second < 10) response += "0"; response += String(_second);
  158. }
  159. // ============== SERIAL SERIAL SERIAL ========================================
  160. // ----------------------------------------------------------------------------
  161. // Print utin8_t values in HEX with leading 0 when necessary
  162. // ----------------------------------------------------------------------------
  163. void printHexDigit(uint8_t digit)
  164. {
  165. // utility function for printing Hex Values with leading 0
  166. if(digit < 0x10)
  167. Serial.print('0');
  168. Serial.print(digit,HEX);
  169. }
  170. // ----------------------------------------------------------------------------
  171. // Print leading '0' digits for hours(0) and second(0) when
  172. // printing values less than 10
  173. // ----------------------------------------------------------------------------
  174. void printDigits(unsigned long digits)
  175. {
  176. // utility function for digital clock display: prints leading 0
  177. if(digits < 10)
  178. Serial.print(F("0"));
  179. Serial.print(digits);
  180. }
  181. // ----------------------------------------------------------------------------
  182. // Print the current time
  183. // ----------------------------------------------------------------------------
  184. static void printTime() {
  185. switch (weekday())
  186. {
  187. case 1: Serial.print(F("Sunday")); break;
  188. case 2: Serial.print(F("Monday")); break;
  189. case 3: Serial.print(F("Tuesday")); break;
  190. case 4: Serial.print(F("Wednesday")); break;
  191. case 5: Serial.print(F("Thursday")); break;
  192. case 6: Serial.print(F("Friday")); break;
  193. case 7: Serial.print(F("Saturday")); break;
  194. default: Serial.print(F("ERROR")); break;
  195. }
  196. Serial.print(F(" "));
  197. printDigits(hour());
  198. Serial.print(F(":"));
  199. printDigits(minute());
  200. Serial.print(F(":"));
  201. printDigits(second());
  202. return;
  203. }
  204. // ----------------------------------------------------------------------------
  205. // SerialTime
  206. // Print the current time on the Serial (USB), with leading 0.
  207. // ----------------------------------------------------------------------------
  208. void SerialTime()
  209. {
  210. uint32_t thrs = hour();
  211. uint32_t tmin = minute();
  212. uint32_t tsec = second();
  213. if (thrs<10) Serial.print('0'); Serial.print(thrs);
  214. Serial.print(':');
  215. if (tmin<10) Serial.print('0'); Serial.print(tmin);
  216. Serial.print(':');
  217. if (tsec<10) Serial.print('0'); Serial.print(tsec);
  218. if (debug>=2) Serial.flush();
  219. return;
  220. }
  221. // ----------------------------------------------------------------------------
  222. // SerialStat
  223. // Print the statistics on Serial (USB) port
  224. // ----------------------------------------------------------------------------
  225. void SerialStat(uint8_t intr)
  226. {
  227. #if _DUSB>=1
  228. if (debug>=0) {
  229. Serial.print(F("I="));
  230. if (intr & IRQ_LORA_RXTOUT_MASK) Serial.print(F("RXTOUT ")); // 0x80
  231. if (intr & IRQ_LORA_RXDONE_MASK) Serial.print(F("RXDONE ")); // 0x40
  232. if (intr & IRQ_LORA_CRCERR_MASK) Serial.print(F("CRCERR ")); // 0x20
  233. if (intr & IRQ_LORA_HEADER_MASK) Serial.print(F("HEADER ")); // 0x10
  234. if (intr & IRQ_LORA_TXDONE_MASK) Serial.print(F("TXDONE ")); // 0x08
  235. if (intr & IRQ_LORA_CDDONE_MASK) Serial.print(F("CDDONE ")); // 0x04
  236. if (intr & IRQ_LORA_FHSSCH_MASK) Serial.print(F("FHSSCH ")); // 0x02
  237. if (intr & IRQ_LORA_CDDETD_MASK) Serial.print(F("CDDETD ")); // 0x01
  238. if (intr == 0x00) Serial.print(F(" -- "));
  239. Serial.print(F(", F="));
  240. Serial.print(ifreq);
  241. Serial.print(F(", SF="));
  242. Serial.print(sf);
  243. Serial.print(F(", E="));
  244. Serial.print(_event);
  245. Serial.print(F(", S="));
  246. //Serial.print(_state);
  247. switch (_state) {
  248. case S_INIT:
  249. Serial.print(F("INIT "));
  250. break;
  251. case S_SCAN:
  252. Serial.print(F("SCAN "));
  253. break;
  254. case S_CAD:
  255. Serial.print(F("CAD "));
  256. break;
  257. case S_RX:
  258. Serial.print(F("RX "));
  259. break;
  260. case S_TX:
  261. Serial.print(F("TX "));
  262. break;
  263. case S_TXDONE:
  264. Serial.print(F("TXDONE"));
  265. break;
  266. default:
  267. Serial.print(F(" -- "));
  268. }
  269. Serial.print(F(", eT="));
  270. Serial.print( micros() - eventTime );
  271. Serial.print(F(", dT="));
  272. Serial.print( micros() - doneTime );
  273. Serial.println();
  274. }
  275. #endif
  276. }
  277. // ----------------------------------------------------------------------------
  278. // SerialName(id, response)
  279. // Check whether for address a (4 bytes in Unsigned Long) there is a name
  280. // This function only works if _TRUSTED_NODES is set
  281. // ----------------------------------------------------------------------------
  282. int SerialName(char * a, String& response)
  283. {
  284. #if _TRUSTED_NODES>=1
  285. uint32_t id = ((a[0]<<24) | (a[1]<<16) | (a[2]<<8) | a[3]);
  286. int i;
  287. for ( i=0; i< (sizeof(nodes)/sizeof(nodex)); i++) {
  288. if (id == nodes[i].id) {
  289. #if _DUSB >=1
  290. if (( debug>=3 ) && ( pdebug & P_GUI )) {
  291. Serial.print(F("G Name="));
  292. Serial.print(nodes[i].nm);
  293. Serial.print(F(" for node=0x"));
  294. Serial.print(nodes[i].id,HEX);
  295. Serial.println();
  296. }
  297. #endif // _DUSB
  298. response += nodes[i].nm;
  299. return(i);
  300. }
  301. }
  302. #endif // _TRUSTED_NODES
  303. return(-1); // If no success OR is TRUSTED NODES not defined
  304. }
  305. #if _LOCALSERVER==1
  306. // ----------------------------------------------------------------------------
  307. // inDecodes(id)
  308. // Find the id in Decodes array, and return the index of the item
  309. // Parameters:
  310. // id: The first field in the array (normally DevAddr id). Must be char[4]
  311. // Returns:
  312. // The index of the ID in the Array. Returns -1 if not found
  313. // ----------------------------------------------------------------------------
  314. int inDecodes(char * id) {
  315. uint32_t ident = ((id[3]<<24) | (id[2]<<16) | (id[1]<<8) | id[0]);
  316. int i;
  317. for ( i=0; i< (sizeof(decodes)/sizeof(codex)); i++) {
  318. if (ident == decodes[i].id) {
  319. return(i);
  320. }
  321. }
  322. return(-1);
  323. }
  324. #endif