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.

299 lines
9.1 KiB

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