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.

307 lines
9.2 KiB

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