StatsFormatHelper.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "Mesh.h"
  3. #include <Arduino.h>
  4. class StatsFormatHelper {
  5. public:
  6. static void formatCoreStats(char* reply,
  7. size_t reply_size,
  8. mesh::MainBoard& board,
  9. mesh::MillisecondClock& ms,
  10. uint16_t err_flags,
  11. mesh::PacketManager* mgr) {
  12. snprintf(reply, reply_size,
  13. "{\"battery_mv\":%u,\"uptime_secs\":%u,\"errors\":%u,\"queue_len\":%u}",
  14. board.getBattMilliVolts(),
  15. ms.getMillis() / 1000,
  16. err_flags,
  17. mgr->getOutboundTotal()
  18. );
  19. }
  20. template<typename RadioDriverType>
  21. static void formatRadioStats(char* reply,
  22. size_t reply_size,
  23. mesh::Radio* radio,
  24. RadioDriverType& driver,
  25. uint32_t total_air_time_ms,
  26. uint32_t total_rx_air_time_ms) {
  27. snprintf(reply, reply_size,
  28. "{\"noise_floor\":%d,\"last_rssi\":%d,\"last_snr\":%.2f,\"tx_air_secs\":%u,\"rx_air_secs\":%u}",
  29. (int16_t)radio->getNoiseFloor(),
  30. (int16_t)driver.getLastRSSI(),
  31. driver.getLastSNR(),
  32. total_air_time_ms / 1000,
  33. total_rx_air_time_ms / 1000
  34. );
  35. }
  36. template<typename RadioDriverType>
  37. static void formatPacketStats(char* reply,
  38. size_t reply_size,
  39. RadioDriverType& driver,
  40. uint32_t n_sent_flood,
  41. uint32_t n_sent_direct,
  42. uint32_t n_recv_flood,
  43. uint32_t n_recv_direct) {
  44. snprintf(reply, reply_size,
  45. "{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u,\"recv_errors\":%u}",
  46. driver.getPacketsRecv(),
  47. driver.getPacketsSent(),
  48. n_sent_flood,
  49. n_sent_direct,
  50. n_recv_flood,
  51. n_recv_direct,
  52. driver.getPacketsRecvErrors()
  53. );
  54. }
  55. static void formatMemoryStats(char* reply, size_t reply_size) {
  56. if (reply == nullptr || reply_size == 0) {
  57. return;
  58. }
  59. #if defined(ESP32)
  60. snprintf(reply, reply_size,
  61. "{\"heap_free\":%u,\"heap_min\":%u,\"heap_max\":%u,\"psram_free\":%u,\"psram_min\":%u,\"psram_max\":%u}",
  62. ESP.getFreeHeap(),
  63. ESP.getMinFreeHeap(),
  64. ESP.getMaxAllocHeap(),
  65. ESP.getFreePsram(),
  66. ESP.getMinFreePsram(),
  67. ESP.getMaxAllocPsram()
  68. );
  69. #else
  70. snprintf(reply, reply_size, "{\"supported\":false}");
  71. #endif
  72. }
  73. };