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.

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