Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
9.0 KiB

7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. /*
  2. DEBUG MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DEBUG_SUPPORT
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include <EEPROM_Rotate.h>
  9. extern "C" {
  10. #include "user_interface.h"
  11. }
  12. #if DEBUG_UDP_SUPPORT
  13. #include <WiFiUdp.h>
  14. WiFiUDP _udp_debug;
  15. #if DEBUG_UDP_PORT == 514
  16. char _udp_syslog_header[40] = {0};
  17. #endif
  18. #endif
  19. void _debugSend(char * message) {
  20. bool pause = false;
  21. #if DEBUG_ADD_TIMESTAMP
  22. static bool add_timestamp = true;
  23. char timestamp[10] = {0};
  24. if (add_timestamp) snprintf_P(timestamp, sizeof(timestamp), PSTR("[%06lu] "), millis() % 1000000);
  25. add_timestamp = (message[strlen(message)-1] == 10) || (message[strlen(message)-1] == 13);
  26. #endif
  27. #if DEBUG_SERIAL_SUPPORT
  28. #if DEBUG_ADD_TIMESTAMP
  29. DEBUG_PORT.printf(timestamp);
  30. #endif
  31. DEBUG_PORT.printf(message);
  32. #endif
  33. #if DEBUG_UDP_SUPPORT
  34. #if SYSTEM_CHECK_ENABLED
  35. if (systemCheck()) {
  36. #endif
  37. _udp_debug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
  38. #if DEBUG_UDP_PORT == 514
  39. _udp_debug.write(_udp_syslog_header);
  40. #endif
  41. _udp_debug.write(message);
  42. _udp_debug.endPacket();
  43. pause = true;
  44. #if SYSTEM_CHECK_ENABLED
  45. }
  46. #endif
  47. #endif
  48. #if DEBUG_TELNET_SUPPORT
  49. #if DEBUG_ADD_TIMESTAMP
  50. _telnetWrite(timestamp, strlen(timestamp));
  51. #endif
  52. _telnetWrite(message, strlen(message));
  53. pause = true;
  54. #endif
  55. #if DEBUG_WEB_SUPPORT
  56. if (wsConnected() && (getFreeHeap() > 10000)) {
  57. String m = String(message);
  58. m.replace("\"", "&quot;");
  59. m.replace("{", "&#123");
  60. m.replace("}", "&#125");
  61. char buffer[m.length() + 24];
  62. #if DEBUG_ADD_TIMESTAMP
  63. snprintf_P(buffer, sizeof(buffer), PSTR("{\"weblog\": \"%s%s\"}"), timestamp, m.c_str());
  64. #else
  65. snprintf_P(buffer, sizeof(buffer), PSTR("{\"weblog\": \"%s\"}"), m.c_str());
  66. #endif
  67. wsSend(buffer);
  68. pause = true;
  69. }
  70. #endif
  71. if (pause) optimistic_yield(100);
  72. }
  73. // -----------------------------------------------------------------------------
  74. void debugSend(const char * format, ...) {
  75. va_list args;
  76. va_start(args, format);
  77. char test[1];
  78. int len = ets_vsnprintf(test, 1, format, args) + 1;
  79. char * buffer = new char[len];
  80. ets_vsnprintf(buffer, len, format, args);
  81. va_end(args);
  82. _debugSend(buffer);
  83. delete[] buffer;
  84. }
  85. void debugSend_P(PGM_P format_P, ...) {
  86. char format[strlen_P(format_P)+1];
  87. memcpy_P(format, format_P, sizeof(format));
  88. va_list args;
  89. va_start(args, format_P);
  90. char test[1];
  91. int len = ets_vsnprintf(test, 1, format, args) + 1;
  92. char * buffer = new char[len];
  93. ets_vsnprintf(buffer, len, format, args);
  94. va_end(args);
  95. _debugSend(buffer);
  96. delete[] buffer;
  97. }
  98. #if DEBUG_WEB_SUPPORT
  99. void debugWebSetup() {
  100. wsOnSendRegister([](JsonObject& root) {
  101. root["dbgVisible"] = 1;
  102. });
  103. wsOnActionRegister([](uint32_t client_id, const char * action, JsonObject& data) {
  104. if (strcmp(action, "dbgcmd") == 0) {
  105. const char* command = data.get<const char*>("command");
  106. char buffer[strlen(command) + 2];
  107. snprintf(buffer, sizeof(buffer), "%s\n", command);
  108. settingsInject((void*) buffer, strlen(buffer));
  109. }
  110. });
  111. #if DEBUG_UDP_SUPPORT
  112. #if DEBUG_UDP_PORT == 514
  113. snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getSetting("hostname").c_str());
  114. #endif
  115. #endif
  116. }
  117. #endif // DEBUG_WEB_SUPPORT
  118. void debugSetup() {
  119. #if DEBUG_SERIAL_SUPPORT
  120. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  121. #if DEBUG_ESP_WIFI
  122. DEBUG_PORT.setDebugOutput(true);
  123. #endif
  124. #endif
  125. }
  126. // -----------------------------------------------------------------------------
  127. // Save crash info
  128. // Taken from krzychb EspSaveCrash
  129. // https://github.com/krzychb/EspSaveCrash
  130. // -----------------------------------------------------------------------------
  131. #define SAVE_CRASH_EEPROM_OFFSET 0x0100 // initial address for crash data
  132. /**
  133. * Structure of the single crash data set
  134. *
  135. * 1. Crash time
  136. * 2. Restart reason
  137. * 3. Exception cause
  138. * 4. epc1
  139. * 5. epc2
  140. * 6. epc3
  141. * 7. excvaddr
  142. * 8. depc
  143. * 9. adress of stack start
  144. * 10. adress of stack end
  145. * 11. stack trace bytes
  146. * ...
  147. */
  148. #define SAVE_CRASH_CRASH_TIME 0x00 // 4 bytes
  149. #define SAVE_CRASH_RESTART_REASON 0x04 // 1 byte
  150. #define SAVE_CRASH_EXCEPTION_CAUSE 0x05 // 1 byte
  151. #define SAVE_CRASH_EPC1 0x06 // 4 bytes
  152. #define SAVE_CRASH_EPC2 0x0A // 4 bytes
  153. #define SAVE_CRASH_EPC3 0x0E // 4 bytes
  154. #define SAVE_CRASH_EXCVADDR 0x12 // 4 bytes
  155. #define SAVE_CRASH_DEPC 0x16 // 4 bytes
  156. #define SAVE_CRASH_STACK_START 0x1A // 4 bytes
  157. #define SAVE_CRASH_STACK_END 0x1E // 4 bytes
  158. #define SAVE_CRASH_STACK_TRACE 0x22 // variable
  159. /**
  160. * Save crash information in EEPROM
  161. * This function is called automatically if ESP8266 suffers an exception
  162. * It should be kept quick / consise to be able to execute before hardware wdt may kick in
  163. */
  164. extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack_start, uint32_t stack_end ) {
  165. // This method assumes EEPROM has already been initialized
  166. // which is the first thing ESPurna does
  167. // write crash time to EEPROM
  168. uint32_t crash_time = millis();
  169. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  170. // write reset info to EEPROM
  171. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON, rst_info->reason);
  172. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE, rst_info->exccause);
  173. // write epc1, epc2, epc3, excvaddr and depc to EEPROM
  174. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, rst_info->epc1);
  175. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, rst_info->epc2);
  176. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, rst_info->epc3);
  177. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, rst_info->excvaddr);
  178. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, rst_info->depc);
  179. // write stack start and end address to EEPROM
  180. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  181. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  182. // write stack trace to EEPROM
  183. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  184. for (uint32_t i = stack_start; i < stack_end; i++) {
  185. byte* byteValue = (byte*) i;
  186. EEPROMr.write(current_address++, *byteValue);
  187. }
  188. EEPROMr.commit();
  189. }
  190. /**
  191. * Clears crash info
  192. */
  193. void debugClearCrashInfo() {
  194. uint32_t crash_time = 0xFFFFFFFF;
  195. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  196. EEPROMr.commit();
  197. }
  198. /**
  199. * Print out crash information that has been previusly saved in EEPROM
  200. */
  201. void debugDumpCrashInfo() {
  202. uint32_t crash_time;
  203. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  204. if ((crash_time == 0) || (crash_time == 0xFFFFFFFF)) {
  205. DEBUG_MSG_P(PSTR("[DEBUG] No crash info\n"));
  206. return;
  207. }
  208. DEBUG_MSG_P(PSTR("[DEBUG] Latest crash was at %lu ms after boot\n"), crash_time);
  209. DEBUG_MSG_P(PSTR("[DEBUG] Reason of restart: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON));
  210. DEBUG_MSG_P(PSTR("[DEBUG] Exception cause: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE));
  211. uint32_t epc1, epc2, epc3, excvaddr, depc;
  212. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, epc1);
  213. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, epc2);
  214. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3);
  215. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr);
  216. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc);
  217. DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3);
  218. DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc);
  219. uint32_t stack_start, stack_end;
  220. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  221. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  222. DEBUG_MSG_P(PSTR("[DEBUG] >>>stack>>>\n[DEBUG] "));
  223. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  224. int16_t stack_len = stack_end - stack_start;
  225. uint32_t stack_trace;
  226. for (int16_t i = 0; i < stack_len; i += 0x10) {
  227. DEBUG_MSG_P(PSTR("%08x: "), stack_start + i);
  228. for (byte j = 0; j < 4; j++) {
  229. EEPROMr.get(current_address, stack_trace);
  230. DEBUG_MSG_P(PSTR("%08x "), stack_trace);
  231. current_address += 4;
  232. }
  233. DEBUG_MSG_P(PSTR("\n[DEBUG] "));
  234. }
  235. DEBUG_MSG_P(PSTR("<<<stack<<<\n"));
  236. }
  237. #endif // DEBUG_SUPPORT