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.

315 lines
9.5 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_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. // Do not record crash data when resetting the board
  168. if (checkNeedsReset()) {
  169. return;
  170. }
  171. // This method assumes EEPROM has already been initialized
  172. // which is the first thing ESPurna does
  173. // write crash time to EEPROM
  174. uint32_t crash_time = millis();
  175. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  176. // write reset info to EEPROM
  177. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON, rst_info->reason);
  178. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE, rst_info->exccause);
  179. // write epc1, epc2, epc3, excvaddr and depc to EEPROM
  180. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, rst_info->epc1);
  181. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, rst_info->epc2);
  182. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, rst_info->epc3);
  183. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, rst_info->excvaddr);
  184. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, rst_info->depc);
  185. // write stack start and end address to EEPROM
  186. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  187. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  188. // starting address of Embedis data plus reserve
  189. const uint16_t settings_start = SPI_FLASH_SEC_SIZE - settingsSize() - 0x10;
  190. // write stack trace to EEPROM and avoid overwriting settings
  191. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  192. for (uint32_t i = stack_start; i < stack_end; i++) {
  193. if (current_address >= settings_start) break;
  194. byte* byteValue = (byte*) i;
  195. EEPROMr.write(current_address++, *byteValue);
  196. }
  197. EEPROMr.commit();
  198. }
  199. /**
  200. * Clears crash info
  201. */
  202. void debugClearCrashInfo() {
  203. uint32_t crash_time = 0xFFFFFFFF;
  204. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  205. EEPROMr.commit();
  206. }
  207. /**
  208. * Print out crash information that has been previusly saved in EEPROM
  209. */
  210. void debugDumpCrashInfo() {
  211. uint32_t crash_time;
  212. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  213. if ((crash_time == 0) || (crash_time == 0xFFFFFFFF)) {
  214. DEBUG_MSG_P(PSTR("[DEBUG] No crash info\n"));
  215. return;
  216. }
  217. DEBUG_MSG_P(PSTR("[DEBUG] Latest crash was at %lu ms after boot\n"), crash_time);
  218. DEBUG_MSG_P(PSTR("[DEBUG] Reason of restart: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON));
  219. DEBUG_MSG_P(PSTR("[DEBUG] Exception cause: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE));
  220. uint32_t epc1, epc2, epc3, excvaddr, depc;
  221. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, epc1);
  222. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, epc2);
  223. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3);
  224. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr);
  225. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc);
  226. DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3);
  227. DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc);
  228. uint32_t stack_start, stack_end;
  229. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  230. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  231. DEBUG_MSG_P(PSTR("[DEBUG] sp=0x%08x end=0x%08x\n"), stack_start, stack_end);
  232. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  233. int16_t stack_len = stack_end - stack_start;
  234. uint32_t stack_trace;
  235. DEBUG_MSG_P(PSTR("[DEBUG] >>>stack>>>\n[DEBUG] "));
  236. for (int16_t i = 0; i < stack_len; i += 0x10) {
  237. DEBUG_MSG_P(PSTR("%08x: "), stack_start + i);
  238. for (byte j = 0; j < 4; j++) {
  239. EEPROMr.get(current_address, stack_trace);
  240. DEBUG_MSG_P(PSTR("%08x "), stack_trace);
  241. current_address += 4;
  242. }
  243. DEBUG_MSG_P(PSTR("\n[DEBUG] "));
  244. }
  245. DEBUG_MSG_P(PSTR("<<<stack<<<\n"));
  246. }
  247. #endif // DEBUG_SUPPORT