network_status.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "esp_log.h"
  10. #include "lwip/inet.h"
  11. #include "monitor.h"
  12. #include "network_ethernet.h"
  13. #include "network_wifi.h"
  14. // #include "Configurator.h"
  15. #pragma message("fixme: search for TODO below")
  16. #include "platform_esp32.h"
  17. #include "tools.h"
  18. #include "trace.h"
  19. #ifndef CONFIG_SQUEEZELITE_ESP32_RELEASE_URL
  20. #pragma message "Defaulting release url"
  21. #define CONFIG_SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
  22. #endif
  23. static const char TAG[] = "network_status";
  24. sys_Status status;
  25. SemaphoreHandle_t network_status_structure_mutex = NULL;
  26. static TaskHandle_t network_structure_locked_task = NULL;
  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 init_network_status() {
  31. chained_notify = server_notify;
  32. server_notify = connect_notify;
  33. ESP_LOGD(TAG, "init_network_status. Creating mutexes");
  34. network_status_structure_mutex = xSemaphoreCreateMutex();
  35. ESP_LOGD(TAG, "Getting release url ");
  36. }
  37. void destroy_network_status() {
  38. vSemaphoreDelete(network_status_structure_mutex);
  39. network_status_structure_mutex = NULL;
  40. }
  41. void network_status_unlock_structure() {
  42. ESP_LOGV(TAG, "Unlocking json buffer!");
  43. network_structure_locked_task = NULL;
  44. xSemaphoreGive(network_status_structure_mutex);
  45. }
  46. bool network_status_lock_structure(TickType_t xTicksToWait) {
  47. ESP_LOGV(TAG, "Locking structure buffer");
  48. TaskHandle_t calling_task = xTaskGetCurrentTaskHandle();
  49. if (calling_task == network_structure_locked_task) {
  50. ESP_LOGV(TAG, "structure buffer already locked to current task");
  51. return true;
  52. }
  53. if (network_status_structure_mutex) {
  54. if (xSemaphoreTake(network_status_structure_mutex, xTicksToWait) == pdTRUE) {
  55. ESP_LOGV(TAG, "structure locked!");
  56. network_structure_locked_task = calling_task;
  57. return true;
  58. } else {
  59. ESP_LOGE(TAG, "Semaphore take failed. Unable to lock structure mutex");
  60. return false;
  61. }
  62. } else {
  63. ESP_LOGV(TAG, "Unable to lock structure mutex");
  64. return false;
  65. }
  66. }
  67. char* network_status_get_sta_ip_string() {
  68. return status.has_net && status.net.has_ip?status.net.ip.ip:"0.0.0.0";
  69. }
  70. void set_lms_server_details(in_addr_t ip, u16_t hport, u16_t cport) {
  71. if (network_status_lock_structure(portMAX_DELAY)) {
  72. status.has_LMS = true;
  73. strncpy(status.LMS.ip, inet_ntoa(ip), sizeof(status.LMS.ip));
  74. status.LMS.port = hport;
  75. status.LMS.cport = cport;
  76. ESP_LOGI(TAG, "LMS IP: %s, hport: %d, cport: %d", status.LMS.ip, status.LMS.port, status.LMS.cport);
  77. network_status_unlock_structure();
  78. }
  79. }
  80. static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport) {
  81. set_lms_server_details(ip, hport, cport);
  82. if (chained_notify)
  83. (*chained_notify)(ip, hport, cport);
  84. network_async_update_status();
  85. }
  86. void network_status_set_basic_info() {
  87. if (network_status_lock_structure(portMAX_DELAY)) {
  88. network_t* nm = network_get_state_machine();
  89. sys_GPIO * gpio = NULL;
  90. const esp_app_desc_t* desc = esp_ota_get_app_description();
  91. status.has_platform = true;
  92. strncpy(status.platform.project, desc->project_name, sizeof(status.platform.project));
  93. #ifdef CONFIG_FW_PLATFORM_NAME
  94. strncpy(status.platform.name, CONFIG_FW_PLATFORM_NAME, sizeof(status.platform.name));
  95. #endif
  96. strncpy(status.platform.version, desc->version, sizeof(status.platform.version));
  97. strncpy(status.platform.version, desc->version, sizeof(status.platform.version));
  98. status.platform.recovery = is_recovery_running;
  99. status.platform.depth = 16;
  100. #if DEPTH == 16 || DEPTH == 32
  101. status.platform.depth = DEPTH;
  102. #endif
  103. status.has_hw = true;
  104. status.hw.jack_inserted = jack_inserted_svc();
  105. status.hw.has_jack_inserted = SYS_GPIOS_NAME(jack,gpio) && gpio->pin>=0;
  106. status.hw.has_spk_fault = SYS_GPIOS_NAME(spkfault,gpio) && gpio->pin>=0;
  107. status.hw.spk_fault = spkfault_svc();
  108. status.hw.batt_voltage = battery_value_svc();
  109. status.has_net = true;
  110. status.net.has_wifi = true;
  111. status.net.wifi.disconnect_count = nm->num_disconnect;
  112. status.net.wifi.avg_conn_time = nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0;
  113. #ifdef CONFIG_BT_ENABLED
  114. if(platform->has_services && platform->services.has_bt_sink && platform->services.bt_sink.enabled){
  115. status.has_bt = true;
  116. status.bt.bt_status = bt_app_source_get_a2d_state();
  117. status.bt.bt_media_state = bt_app_source_get_media_state();
  118. }
  119. #endif
  120. if (network_ethernet_enabled()) {
  121. status.net.eth_up = network_ethernet_is_up();
  122. }
  123. ESP_LOGV(TAG, "network_status_get_basic_info done");
  124. network_status_unlock_structure();
  125. } else {
  126. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  127. }
  128. }
  129. void network_status_update_address(esp_netif_ip_info_t* ip_info) {
  130. status.has_net = true;
  131. status.net.has_ip = true;
  132. strncpy(status.net.ip.ip,ip4addr_ntoa((ip4_addr_t*)&ip_info->ip),sizeof(status.net.ip.ip));
  133. strncpy(status.net.ip.netmask,ip4addr_ntoa((ip4_addr_t*)&ip_info->netmask),sizeof(status.net.ip.netmask));
  134. strncpy(status.net.ip.gw,ip4addr_ntoa((ip4_addr_t*)&ip_info->gw),sizeof(status.net.ip.gw));
  135. }
  136. void network_status_update_ip_info(sys_UPDATE_REASONS update_reason_code) {
  137. ESP_LOGV(TAG, "network_status_update_ip_info called");
  138. esp_netif_ip_info_t ip_info;
  139. if (network_status_lock_structure(portMAX_DELAY)) {
  140. /* generate the connection info with success */
  141. network_status_set_basic_info();
  142. status.net.updt_reason = update_reason_code;
  143. status.net.wifi.has_connected_sta = false;
  144. status.net.wifi.connected_sta.connected = false;
  145. ESP_LOGD(TAG,"Updating ip info with reason code %d. Checking if Wifi interface is connected",update_reason_code);
  146. if (network_is_interface_connected(network_wifi_get_interface()) || update_reason_code == sys_UPDATE_REASONS_R_FAILED_ATTEMPT ) {
  147. status.net.interface = sys_CONNECTED_IF_IF_WIFI;
  148. esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
  149. network_status_update_address(&ip_info);
  150. if (!network_wifi_is_ap_mode()) {
  151. /* wifi is active, and associated to an AP */
  152. wifi_ap_record_t ap;
  153. esp_wifi_sta_get_ap_info(&ap);
  154. network_wifi_esp_sta_to_sta(&ap, &status.net.wifi.connected_sta);
  155. status.net.wifi.connected_sta.connected = true;
  156. status.net.has_wifi = true;
  157. status.net.wifi.has_connected_sta = true;
  158. }
  159. }
  160. ESP_LOGD(TAG,"Checking if ethernet interface is connected");
  161. if (network_is_interface_connected(network_ethernet_get_interface())) {
  162. status.net.interface = sys_CONNECTED_IF_IF_ETHERNET;
  163. esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
  164. network_status_update_address(&ip_info);
  165. }
  166. network_status_unlock_structure();
  167. } else {
  168. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  169. }
  170. ESP_LOGV(TAG, "wifi_status_generate_ip_info_json done");
  171. }