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.

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