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.

160 lines
3.6 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. constexpr const uint8_t TIMESTAMP_LENGTH = 10;
  7. #if DEBUG_UDP_SUPPORT
  8. #include <WiFiUdp.h>
  9. WiFiUDP _udp_debug;
  10. #if DEBUG_UDP_PORT == 514
  11. char _udp_syslog_header[40] = {0};
  12. #endif
  13. #endif
  14. void _debugSend(char * message) {
  15. size_t msg_len = strlen(message);
  16. bool pause = false;
  17. #if DEBUG_ADD_TIMESTAMP
  18. static bool add_timestamp = true;
  19. size_t offset = 0;
  20. char buffer[TIMESTAMP_LENGTH + msg_len];
  21. if (add_timestamp) {
  22. snprintf_P(buffer, TIMESTAMP_LENGTH, PSTR("[%06lu] "), millis() % 1000000);
  23. offset = TIMESTAMP_LENGTH - 1;
  24. }
  25. memcpy(buffer + offset, message, msg_len);
  26. buffer[msg_len + offset] = '\0';
  27. add_timestamp = (message[msg_len - 1] == 10) || (message[msg_len - 1] == 13);
  28. #else
  29. char* buffer = message;
  30. #endif
  31. #if DEBUG_SERIAL_SUPPORT
  32. DEBUG_PORT.print(buffer);
  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. _telnetWrite(buffer, strlen(buffer));
  51. pause = true;
  52. #endif
  53. #if DEBUG_WEB_SUPPORT
  54. wsDebugSend(buffer);
  55. #endif
  56. if (pause) optimistic_yield(100);
  57. }
  58. // -----------------------------------------------------------------------------
  59. void debugSend(const char * format, ...) {
  60. va_list args;
  61. va_start(args, format);
  62. char test[1];
  63. int len = ets_vsnprintf(test, 1, format, args) + 1;
  64. char * buffer = new char[len];
  65. ets_vsnprintf(buffer, len, format, args);
  66. va_end(args);
  67. _debugSend(buffer);
  68. delete[] buffer;
  69. }
  70. void debugSend_P(PGM_P format_P, ...) {
  71. char format[strlen_P(format_P)+1];
  72. memcpy_P(format, format_P, sizeof(format));
  73. va_list args;
  74. va_start(args, format_P);
  75. char test[1];
  76. int len = ets_vsnprintf(test, 1, format, args) + 1;
  77. char * buffer = new char[len];
  78. ets_vsnprintf(buffer, len, format, args);
  79. va_end(args);
  80. _debugSend(buffer);
  81. delete[] buffer;
  82. }
  83. #if DEBUG_WEB_SUPPORT
  84. void debugWebSetup() {
  85. wsOnSendRegister([](JsonObject& root) {
  86. root["dbgVisible"] = 1;
  87. });
  88. wsOnActionRegister([](uint32_t client_id, const char * action, JsonObject& data) {
  89. #if TERMINAL_SUPPORT
  90. if (strcmp(action, "dbgcmd") == 0) {
  91. const char* command = data.get<const char*>("command");
  92. char buffer[strlen(command) + 2];
  93. snprintf(buffer, sizeof(buffer), "%s\n", command);
  94. terminalInject((void*) buffer, strlen(buffer));
  95. }
  96. #endif
  97. });
  98. #if DEBUG_UDP_SUPPORT
  99. #if DEBUG_UDP_PORT == 514
  100. snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getSetting("hostname").c_str());
  101. #endif
  102. #endif
  103. }
  104. #endif // DEBUG_WEB_SUPPORT
  105. // -----------------------------------------------------------------------------
  106. void debugSetup() {
  107. #if DEBUG_SERIAL_SUPPORT
  108. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  109. #if DEBUG_ESP_WIFI
  110. DEBUG_PORT.setDebugOutput(true);
  111. #endif
  112. #endif
  113. }
  114. #endif // DEBUG_SUPPORT