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.

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