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.

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