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.

138 lines
3.2 KiB

7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. /*
  2. DEBUG MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DEBUG_SUPPORT
  6. #include "libs/DebugSend.h"
  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. #if DEBUG_SERIAL_SUPPORT
  15. void _debugSendSerial(const char* prefix, const char* data) {
  16. if (prefix && (prefix[0] != '\0')) {
  17. DEBUG_PORT.print(prefix);
  18. }
  19. DEBUG_PORT.print(data);
  20. }
  21. #endif
  22. #if DEBUG_TELNET_SUPPORT
  23. void _debugSendTelnet(const char* prefix, const char* data) {
  24. if (prefix && (prefix[0] != '\0')) {
  25. _telnetWrite(prefix);
  26. }
  27. _telnetWrite(data);
  28. }
  29. #endif
  30. void debugSendImpl(const char * message) {
  31. const size_t msg_len = strlen(message);
  32. bool pause = false;
  33. char timestamp[10] = {0};
  34. #if DEBUG_ADD_TIMESTAMP
  35. static bool add_timestamp = true;
  36. if (add_timestamp) {
  37. snprintf(timestamp, sizeof(timestamp), "[%06lu] ", millis() % 1000000);
  38. }
  39. add_timestamp = (message[msg_len - 1] == 10) || (message[msg_len - 1] == 13);
  40. #endif
  41. #if DEBUG_SERIAL_SUPPORT
  42. _debugSendSerial(timestamp, message);
  43. #endif
  44. #if DEBUG_UDP_SUPPORT
  45. #if SYSTEM_CHECK_ENABLED
  46. if (systemCheck()) {
  47. #endif
  48. _udp_debug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
  49. #if DEBUG_UDP_PORT == 514
  50. _udp_debug.write(_udp_syslog_header);
  51. #endif
  52. _udp_debug.write(message);
  53. _udp_debug.endPacket();
  54. pause = true;
  55. #if SYSTEM_CHECK_ENABLED
  56. }
  57. #endif
  58. #endif
  59. #if DEBUG_TELNET_SUPPORT
  60. _debugSendTelnet(timestamp, message);
  61. pause = true;
  62. #endif
  63. #if DEBUG_WEB_SUPPORT
  64. wsDebugSend(timestamp, message);
  65. pause = true;
  66. #endif
  67. if (pause) optimistic_yield(100);
  68. }
  69. #if DEBUG_WEB_SUPPORT
  70. void _debugWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  71. #if TERMINAL_SUPPORT
  72. if (strcmp(action, "dbgcmd") == 0) {
  73. if (!data.containsKey("command") || !data["command"].is<const char*>()) return;
  74. const char* command = data["command"];
  75. if (command && strlen(command)) {
  76. auto command = data.get<const char*>("command");
  77. terminalInject((void*) command, strlen(command));
  78. terminalInject('\n');
  79. }
  80. }
  81. #endif
  82. }
  83. void debugWebSetup() {
  84. wsRegister()
  85. .onVisible([](JsonObject& root) { root["dbgVisible"] = 1; })
  86. .onAction(_debugWebSocketOnAction);
  87. #if DEBUG_UDP_SUPPORT
  88. #if DEBUG_UDP_PORT == 514
  89. snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getSetting("hostname").c_str());
  90. #endif
  91. #endif
  92. }
  93. #endif // DEBUG_WEB_SUPPORT
  94. // -----------------------------------------------------------------------------
  95. void debugSetup() {
  96. #if DEBUG_SERIAL_SUPPORT
  97. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  98. #if DEBUG_ESP_WIFI
  99. DEBUG_PORT.setDebugOutput(true);
  100. #endif
  101. #endif
  102. }
  103. #endif // DEBUG_SUPPORT