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.

289 lines
8.9 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 debugWebSetup() {
  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. void debugSetup() {
  114. #if DEBUG_SERIAL_SUPPORT
  115. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  116. #if DEBUG_ESP_WIFI
  117. DEBUG_PORT.setDebugOutput(true);
  118. #endif
  119. #endif
  120. }
  121. // -----------------------------------------------------------------------------
  122. // Save crash info
  123. // Taken from krzychb EspSaveCrash
  124. // https://github.com/krzychb/EspSaveCrash
  125. // -----------------------------------------------------------------------------
  126. #define SAVE_CRASH_EEPROM_OFFSET 0x0100 // initial address for crash data
  127. /**
  128. * Structure of the single crash data set
  129. *
  130. * 1. Crash time
  131. * 2. Restart reason
  132. * 3. Exception cause
  133. * 4. epc1
  134. * 5. epc2
  135. * 6. epc3
  136. * 7. excvaddr
  137. * 8. depc
  138. * 9. adress of stack start
  139. * 10. adress of stack end
  140. * 11. stack trace bytes
  141. * ...
  142. */
  143. #define SAVE_CRASH_CRASH_TIME 0x00 // 4 bytes
  144. #define SAVE_CRASH_RESTART_REASON 0x04 // 1 byte
  145. #define SAVE_CRASH_EXCEPTION_CAUSE 0x05 // 1 byte
  146. #define SAVE_CRASH_EPC1 0x06 // 4 bytes
  147. #define SAVE_CRASH_EPC2 0x0A // 4 bytes
  148. #define SAVE_CRASH_EPC3 0x0E // 4 bytes
  149. #define SAVE_CRASH_EXCVADDR 0x12 // 4 bytes
  150. #define SAVE_CRASH_DEPC 0x16 // 4 bytes
  151. #define SAVE_CRASH_STACK_START 0x1A // 4 bytes
  152. #define SAVE_CRASH_STACK_END 0x1E // 4 bytes
  153. #define SAVE_CRASH_STACK_TRACE 0x22 // variable
  154. /**
  155. * Save crash information in EEPROM
  156. * This function is called automatically if ESP8266 suffers an exception
  157. * It should be kept quick / consise to be able to execute before hardware wdt may kick in
  158. */
  159. extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack_start, uint32_t stack_end ) {
  160. // This method assumes EEPROM has already been initialized
  161. // which is the first thing ESPurna does
  162. // write crash time to EEPROM
  163. uint32_t crash_time = millis();
  164. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  165. // write reset info to EEPROM
  166. EEPROM.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON, rst_info->reason);
  167. EEPROM.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE, rst_info->exccause);
  168. // write epc1, epc2, epc3, excvaddr and depc to EEPROM
  169. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, rst_info->epc1);
  170. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, rst_info->epc2);
  171. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, rst_info->epc3);
  172. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, rst_info->excvaddr);
  173. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, rst_info->depc);
  174. // write stack start and end address to EEPROM
  175. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  176. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  177. // write stack trace to EEPROM
  178. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  179. for (uint32_t i = stack_start; i < stack_end; i++) {
  180. byte* byteValue = (byte*) i;
  181. EEPROM.write(current_address++, *byteValue);
  182. }
  183. EEPROM.commit();
  184. }
  185. /**
  186. * Clears crash info
  187. */
  188. void debugClearCrashInfo() {
  189. uint32_t crash_time = 0xFFFFFFFF;
  190. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  191. EEPROM.commit();
  192. }
  193. /**
  194. * Print out crash information that has been previusly saved in EEPROM
  195. */
  196. void debugDumpCrashInfo() {
  197. uint32_t crash_time;
  198. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  199. if ((crash_time == 0) || (crash_time == 0xFFFFFFFF)) {
  200. DEBUG_MSG_P(PSTR("[DEBUG] No crash info\n"));
  201. return;
  202. }
  203. DEBUG_MSG_P(PSTR("[DEBUG] Latest crash was at %lu ms after boot\n"), crash_time);
  204. DEBUG_MSG_P(PSTR("[DEBUG] Reason of restart: %u\n"), EEPROM.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON));
  205. DEBUG_MSG_P(PSTR("[DEBUG] Exception cause: %u\n"), EEPROM.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE));
  206. uint32_t epc1, epc2, epc3, excvaddr, depc;
  207. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, epc1);
  208. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, epc2);
  209. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3);
  210. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr);
  211. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc);
  212. DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3);
  213. DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc);
  214. uint32_t stack_start, stack_end;
  215. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  216. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  217. DEBUG_MSG_P(PSTR("[DEBUG] >>>stack>>>\n[DEBUG] "));
  218. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  219. int16_t stack_len = stack_end - stack_start;
  220. uint32_t stack_trace;
  221. for (int16_t i = 0; i < stack_len; i += 0x10) {
  222. DEBUG_MSG_P(PSTR("%08x: "), stack_start + i);
  223. for (byte j = 0; j < 4; j++) {
  224. EEPROM.get(current_address, stack_trace);
  225. DEBUG_MSG_P(PSTR("%08x "), stack_trace);
  226. current_address += 4;
  227. }
  228. DEBUG_MSG_P(PSTR("\n[DEBUG] "));
  229. }
  230. DEBUG_MSG_P(PSTR("<<<stack<<<\n"));
  231. }
  232. #endif // DEBUG_SUPPORT