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.

167 lines
4.1 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. #if DEBUG_UDP_SUPPORT
  7. #include <WiFiUdp.h>
  8. WiFiUDP _udp_debug;
  9. #if DEBUG_UDP_PORT == 514
  10. char _udp_syslog_header[40] = {0};
  11. #endif
  12. #endif
  13. void _debugSend(char * message) {
  14. bool pause = false;
  15. #if DEBUG_ADD_TIMESTAMP
  16. static bool add_timestamp = true;
  17. char timestamp[10] = {0};
  18. if (add_timestamp) snprintf_P(timestamp, sizeof(timestamp), PSTR("[%06lu] "), millis() % 1000000);
  19. add_timestamp = (message[strlen(message)-1] == 10) || (message[strlen(message)-1] == 13);
  20. #endif
  21. #if DEBUG_SERIAL_SUPPORT
  22. #if DEBUG_ADD_TIMESTAMP
  23. DEBUG_PORT.printf(timestamp);
  24. #endif
  25. DEBUG_PORT.printf(message);
  26. #endif
  27. #if DEBUG_UDP_SUPPORT
  28. #if SYSTEM_CHECK_ENABLED
  29. if (systemCheck()) {
  30. #endif
  31. _udp_debug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
  32. #if DEBUG_UDP_PORT == 514
  33. _udp_debug.write(_udp_syslog_header);
  34. #endif
  35. _udp_debug.write(message);
  36. _udp_debug.endPacket();
  37. pause = true;
  38. #if SYSTEM_CHECK_ENABLED
  39. }
  40. #endif
  41. #endif
  42. #if DEBUG_TELNET_SUPPORT
  43. #if DEBUG_ADD_TIMESTAMP
  44. _telnetWrite(timestamp, strlen(timestamp));
  45. #endif
  46. _telnetWrite(message, strlen(message));
  47. pause = true;
  48. #endif
  49. #if DEBUG_WEB_SUPPORT
  50. if (wsConnected() && (getFreeHeap() > 10000)) {
  51. DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(1) + strlen(message) + 17);
  52. JsonObject &root = jsonBuffer.createObject();
  53. #if DEBUG_ADD_TIMESTAMP
  54. char buffer[strlen(timestamp) + strlen(message) + 1];
  55. snprintf_P(buffer, sizeof(buffer), "%s%s", timestamp, message);
  56. root.set("weblog", buffer);
  57. #else
  58. root.set("weblog", message);
  59. #endif
  60. String out;
  61. root.printTo(out);
  62. jsonBuffer.clear();
  63. wsSend(out.c_str());
  64. pause = true;
  65. }
  66. #endif
  67. if (pause) optimistic_yield(100);
  68. }
  69. // -----------------------------------------------------------------------------
  70. void debugSend(const char * format, ...) {
  71. va_list args;
  72. va_start(args, format);
  73. char test[1];
  74. int len = ets_vsnprintf(test, 1, format, args) + 1;
  75. char * buffer = new char[len];
  76. ets_vsnprintf(buffer, len, format, args);
  77. va_end(args);
  78. _debugSend(buffer);
  79. delete[] buffer;
  80. }
  81. void debugSend_P(PGM_P format_P, ...) {
  82. char format[strlen_P(format_P)+1];
  83. memcpy_P(format, format_P, sizeof(format));
  84. va_list args;
  85. va_start(args, format_P);
  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. #if DEBUG_WEB_SUPPORT
  95. void debugWebSetup() {
  96. wsOnSendRegister([](JsonObject& root) {
  97. root["dbgVisible"] = 1;
  98. });
  99. wsOnActionRegister([](uint32_t client_id, const char * action, JsonObject& data) {
  100. #if TERMINAL_SUPPORT
  101. if (strcmp(action, "dbgcmd") == 0) {
  102. const char* command = data.get<const char*>("command");
  103. char buffer[strlen(command) + 2];
  104. snprintf(buffer, sizeof(buffer), "%s\n", command);
  105. terminalInject((void*) buffer, strlen(buffer));
  106. }
  107. #endif
  108. });
  109. #if DEBUG_UDP_SUPPORT
  110. #if DEBUG_UDP_PORT == 514
  111. snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getSetting("hostname").c_str());
  112. #endif
  113. #endif
  114. }
  115. #endif // DEBUG_WEB_SUPPORT
  116. // -----------------------------------------------------------------------------
  117. void debugSetup() {
  118. #if DEBUG_SERIAL_SUPPORT
  119. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  120. #if DEBUG_ESP_WIFI
  121. DEBUG_PORT.setDebugOutput(true);
  122. #endif
  123. #endif
  124. }
  125. #endif // DEBUG_SUPPORT