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.

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