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.

278 lines
8.7 KiB

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