network_status.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifdef NETWORK_STATUS_LOG_LEVEL
  2. #define LOG_LOCAL_LEVEL NETWORK_STATUS_LOG_LEVEL
  3. #endif
  4. #include "network_status.h"
  5. #include <string.h>
  6. #ifdef CONFIG_BT_ENABLED
  7. #include "bt_app_core.h"
  8. #endif
  9. #include "Config.h"
  10. #include "WifiList.h"
  11. #include "esp_log.h"
  12. #include "lwip/inet.h"
  13. #include "monitor.h"
  14. #include "network_ethernet.h"
  15. #include "network_wifi.h"
  16. #include "platform_esp32.h"
  17. #include "tools.h"
  18. #include "trace.h"
  19. #include "bootstate.h"
  20. #ifndef CONFIG_SQUEEZELITE_ESP32_RELEASE_URL
  21. #pragma message "Defaulting release url"
  22. #define CONFIG_SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
  23. #endif
  24. static const char TAG[] = "network_status";
  25. sys_status_data* sys_status = nullptr;
  26. __attribute__((section(".ext_ram.bss"))) System::PB<sys_status_data> sys_status_obj("status", &sys_status_data_msg,sizeof(sys_status_data_msg));
  27. static void (*chained_notify)(in_addr_t, u16_t, u16_t);
  28. static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport);
  29. #define STA_IP_LEN sizeof(char) * IP4ADDR_STRLEN_MAX
  30. void network_status_set_static_info() {
  31. ESP_LOGD(TAG, "network_status_set_static_info starting");
  32. if (sys_status_obj.Lock()) {
  33. const esp_app_desc_t* desc = esp_ota_get_app_description();
  34. auto status_obj = sys_status_obj.get();
  35. status_obj->has_platform = true;
  36. strncpy(
  37. status_obj->platform.project, desc->project_name, sizeof(status_obj->platform.project));
  38. if (platform->target && strlen(platform->target) > 0) {
  39. if (status_obj->platform.target) {
  40. free(status_obj->platform.target);
  41. }
  42. status_obj->platform.target = strdup_psram(platform->target);
  43. }
  44. strncpy(status_obj->platform.version, desc->version, sizeof(status_obj->platform.version));
  45. status_obj->platform.recovery = is_recovery_running;
  46. status_obj->platform.depth = 16;
  47. #if DEPTH == 16 || DEPTH == 32
  48. status_obj->platform.depth = DEPTH;
  49. #endif
  50. status_obj->has_hw = true;
  51. status_obj->hw.supports_jack_inserted = platform->gpios.has_jack &&platform->gpios.jack.pin>=0;
  52. status_obj->hw.supports_spk_fault = platform->gpios.has_spkfault && platform->gpios.spkfault.pin>=0;
  53. status_obj->has_net = true;
  54. status_obj->net.has_wifi = true;
  55. #ifdef CONFIG_BT_ENABLED
  56. if (platform->has_services && platform->services.has_bt_sink &&
  57. platform->services.bt_sink.enabled) {
  58. status_obj->has_bt = true;
  59. }
  60. #endif
  61. ESP_LOGD(TAG, "network_status_set_static_info done");
  62. sys_status_obj.Unlock();
  63. } else {
  64. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  65. }
  66. }
  67. void init_network_status() {
  68. chained_notify = server_notify;
  69. server_notify = connect_notify;
  70. ESP_LOGD(TAG, "init_network_sys_status->Getting status object");
  71. sys_status = sys_status_obj.get();
  72. network_status_set_static_info();
  73. }
  74. void set_lms_server_details(in_addr_t ip, u16_t hport, u16_t cport) {
  75. sys_net_server* LMS = &sys_status->LMS;
  76. if (sys_status_obj.Lock()) {
  77. sys_status->has_LMS = true;
  78. strncpy(LMS->ip, inet_ntoa(ip), sizeof(LMS->ip));
  79. LMS->port = hport;
  80. LMS->cport = cport;
  81. ESP_LOGI(TAG, "LMS IP: %s, hport: %d, cport: %d", LMS->ip, LMS->port, LMS->cport);
  82. sys_status_obj.Unlock();
  83. }
  84. }
  85. static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport) {
  86. set_lms_server_details(ip, hport, cport);
  87. if (chained_notify) (*chained_notify)(ip, hport, cport);
  88. network_async_update_status();
  89. }
  90. void network_status_set_basic_info() {
  91. if (sys_status_obj.Lock()) {
  92. network_t* nm = network_get_state_machine();
  93. sys_status->hw.jack_inserted = jack_inserted_svc();
  94. sys_status->hw.spk_fault = spkfault_svc();
  95. sys_status->hw.batt_voltage = battery_value_svc();
  96. sys_status->net.wifi.disconnect_count = nm->num_disconnect;
  97. sys_status->net.wifi.avg_conn_time =
  98. nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0;
  99. #ifdef CONFIG_BT_ENABLED
  100. if (platform->has_services && platform->services.has_bt_sink &&
  101. platform->services.bt_sink.enabled) {
  102. sys_status->has_bt = true;
  103. sys_status->bt.bt_status = bt_app_source_get_a2d_state();
  104. sys_status->bt.bt_media_state = bt_app_source_get_media_state();
  105. }
  106. #endif
  107. if (network_ethernet_enabled()) {
  108. sys_status->net.eth_up = network_ethernet_is_up();
  109. }
  110. ESP_LOGV(TAG, "network_status_get_basic_info done");
  111. sys_status_obj.Unlock();
  112. } else {
  113. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  114. }
  115. }
  116. void network_status_update_address(esp_netif_ip_info_t* ip_info) {
  117. if (sys_status_obj.Lock()) {
  118. sys_status->has_net = true;
  119. sys_status->net.has_ip = true;
  120. strncpy(sys_status->net.ip.ip, ip4addr_ntoa((ip4_addr_t*)&ip_info->ip),
  121. sizeof(sys_status->net.ip.ip));
  122. strncpy(sys_status->net.ip.netmask, ip4addr_ntoa((ip4_addr_t*)&ip_info->netmask),
  123. sizeof(sys_status->net.ip.netmask));
  124. strncpy(sys_status->net.ip.gw, ip4addr_ntoa((ip4_addr_t*)&ip_info->gw),
  125. sizeof(sys_status->net.ip.gw));
  126. sys_status_obj.Unlock();
  127. }
  128. }
  129. void network_status_update_ip_info(sys_status_reasons update_reason_code) {
  130. ESP_LOGV(TAG, "network_status_update_ip_info called");
  131. esp_netif_ip_info_t ip_info;
  132. if (sys_status_obj.Lock()) {
  133. /* generate the connection info with success */
  134. network_status_set_basic_info();
  135. sys_status->net.updt_reason = update_reason_code;
  136. sys_status->net.wifi.has_connected_sta = false;
  137. sys_status->net.wifi.connected_sta.connected = false;
  138. ESP_LOGD(TAG,
  139. "Updating ip info with reason code %s. Checking if Wifi interface is connected",
  140. sys_status_reasons_name(update_reason_code));
  141. if (network_is_interface_connected(network_wifi_get_interface()) ||
  142. update_reason_code == sys_status_reasons_R_FAILED_ATTEMPT) {
  143. sys_status->net.interface = sys_status_interfaces_IF_WIFI;
  144. esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
  145. network_status_update_address(&ip_info);
  146. if (!network_wifi_is_ap_mode()) {
  147. /* wifi is active, and associated to an AP */
  148. wifi_ap_record_t ap;
  149. esp_wifi_sta_get_ap_info(&ap);
  150. WifiList::ToSTAEntry(&ap, sys_status->net.wifi.connected_sta);
  151. sys_status->net.wifi.connected_sta.connected = true;
  152. sys_status->net.has_wifi = true;
  153. sys_status->net.wifi.has_connected_sta = true;
  154. }
  155. }
  156. ESP_LOGD(TAG, "Checking if ethernet interface is connected");
  157. if (network_is_interface_connected(network_ethernet_get_interface())) {
  158. sys_status->net.interface = sys_status_interfaces_IF_ETHERNET;
  159. esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
  160. network_status_update_address(&ip_info);
  161. }
  162. sys_status_obj.Unlock();
  163. } else {
  164. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  165. }
  166. ESP_LOGV(TAG, "wifi_status_generate_ip_info_json done");
  167. }
  168. bool network_status_send_object(httpd_req_t* req) {
  169. try {
  170. auto data = sys_status_obj.Encode();
  171. httpd_resp_send(req, (const char*)data.data(), data.size());
  172. return true;
  173. } catch (const std::runtime_error& e) {
  174. std::string errdesc = (std::string("Unable to get status: ") + e.what());
  175. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, errdesc.c_str());
  176. }
  177. return false;
  178. }