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.

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