network_status.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "platform_config.h"
  15. #include "platform_esp32.h"
  16. #include "tools.h"
  17. #include "trace.h"
  18. #ifndef CONFIG_SQUEEZELITE_ESP32_RELEASE_URL
  19. #pragma message "Defaulting release url"
  20. #define CONFIG_SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
  21. #endif
  22. static const char TAG[] = "network_status";
  23. SemaphoreHandle_t network_status_json_mutex = NULL;
  24. static TaskHandle_t network_json_locked_task = NULL;
  25. SemaphoreHandle_t network_status_ip_address_mutex = NULL;
  26. static TaskHandle_t network_status_ip_address_locked_task = NULL;
  27. char* release_url = NULL;
  28. char* network_status_ip_address = NULL;
  29. char* ip_info_json = NULL;
  30. cJSON* ip_info_cjson = NULL;
  31. static char lms_server_ip[IP4ADDR_STRLEN_MAX] = {0};
  32. static uint16_t lms_server_port = 0;
  33. static uint16_t lms_server_cport = 0;
  34. static void (*chained_notify)(in_addr_t, u16_t, u16_t);
  35. static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport);
  36. #define STA_IP_LEN sizeof(char) * IP4ADDR_STRLEN_MAX
  37. void init_network_status() {
  38. chained_notify = server_notify;
  39. server_notify = connect_notify;
  40. ESP_LOGD(TAG, "init_network_status. Creating mutexes");
  41. network_status_json_mutex = xSemaphoreCreateMutex();
  42. network_status_ip_address_mutex = xSemaphoreCreateMutex();
  43. ip_info_json = NULL;
  44. ESP_LOGD(TAG, "init_network_status. Creating status json structure");
  45. ip_info_cjson = network_status_clear_ip_info_json(&ip_info_cjson);
  46. ESP_LOGD(TAG, "Getting release url ");
  47. char* release_url = (char*)config_alloc_get_default(NVS_TYPE_STR, "release_url", QUOTE(CONFIG_SQUEEZELITE_ESP32_RELEASE_URL), 0);
  48. if (release_url == NULL) {
  49. ESP_LOGE(TAG, "Unable to retrieve the release url from nvs");
  50. } else {
  51. ESP_LOGD(TAG, "Found release url %s", release_url);
  52. }
  53. ESP_LOGD(TAG, "About to set the STA IP String to 0.0.0.0");
  54. network_status_ip_address = (char*)malloc_init_external(STA_IP_LEN);
  55. network_status_safe_update_sta_ip_string(NULL);
  56. }
  57. void destroy_network_status() {
  58. FREE_AND_NULL(release_url);
  59. FREE_AND_NULL(ip_info_json);
  60. FREE_AND_NULL(network_status_ip_address);
  61. cJSON_Delete(ip_info_cjson);
  62. vSemaphoreDelete(network_status_json_mutex);
  63. network_status_json_mutex = NULL;
  64. vSemaphoreDelete(network_status_ip_address_mutex);
  65. network_status_ip_address_mutex = NULL;
  66. ip_info_cjson = NULL;
  67. }
  68. cJSON* network_status_get_new_json(cJSON** old) {
  69. ESP_LOGV(TAG, "network_status_get_new_json called");
  70. cJSON* root = *old;
  71. if (root != NULL) {
  72. cJSON_Delete(root);
  73. *old = NULL;
  74. }
  75. ESP_LOGV(TAG, "network_status_get_new_json done");
  76. return cJSON_CreateObject();
  77. }
  78. cJSON* network_status_clear_ip_info_json(cJSON** old) {
  79. ESP_LOGV(TAG, "network_status_clear_ip_info_json called");
  80. cJSON* root = network_status_get_basic_info(old);
  81. cJSON_DeleteItemFromObjectCaseSensitive(root, "ip");
  82. cJSON_DeleteItemFromObjectCaseSensitive(root, "netmask");
  83. cJSON_DeleteItemFromObjectCaseSensitive(root, "gw");
  84. cJSON_DeleteItemFromObjectCaseSensitive(root, "rssi");
  85. cJSON_DeleteItemFromObjectCaseSensitive(root, "ssid");
  86. cJSON_DeleteItemFromObjectCaseSensitive(root, "eth");
  87. ESP_LOGV(TAG, "network_status_clear_ip_info_json done");
  88. return root;
  89. }
  90. void network_status_clear_ip() {
  91. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  92. ip_info_cjson = network_status_clear_ip_info_json(&ip_info_cjson);
  93. network_status_unlock_json_buffer();
  94. }
  95. }
  96. char* network_status_alloc_get_ip_info_json() {
  97. return cJSON_PrintUnformatted(ip_info_cjson);
  98. }
  99. void network_status_unlock_json_buffer() {
  100. ESP_LOGV(TAG, "Unlocking json buffer!");
  101. network_json_locked_task = NULL;
  102. xSemaphoreGive(network_status_json_mutex);
  103. }
  104. bool network_status_lock_json_buffer(TickType_t xTicksToWait) {
  105. ESP_LOGV(TAG, "Locking json buffer");
  106. TaskHandle_t calling_task = xTaskGetCurrentTaskHandle();
  107. if (calling_task == network_json_locked_task) {
  108. ESP_LOGV(TAG, "json buffer already locked to current task");
  109. return true;
  110. }
  111. if (network_status_json_mutex) {
  112. if (xSemaphoreTake(network_status_json_mutex, xTicksToWait) == pdTRUE) {
  113. ESP_LOGV(TAG, "Json buffer locked!");
  114. network_json_locked_task = calling_task;
  115. return true;
  116. } else {
  117. ESP_LOGE(TAG, "Semaphore take failed. Unable to lock json buffer mutex");
  118. return false;
  119. }
  120. } else {
  121. ESP_LOGV(TAG, "Unable to lock json buffer mutex");
  122. return false;
  123. }
  124. }
  125. bool network_status_lock_sta_ip_string(TickType_t xTicksToWait) {
  126. TaskHandle_t calling_task = xTaskGetCurrentTaskHandle();
  127. if (calling_task == network_status_ip_address_locked_task) {
  128. ESP_LOGD(TAG, "json buffer already locked to current task ");
  129. return true;
  130. }
  131. if (network_status_ip_address_mutex) {
  132. if (xSemaphoreTake(network_status_ip_address_mutex, xTicksToWait) == pdTRUE) {
  133. network_status_ip_address_locked_task = calling_task;
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. } else {
  139. return false;
  140. }
  141. }
  142. void network_status_unlock_sta_ip_string() {
  143. network_status_ip_address_locked_task = NULL;
  144. xSemaphoreGive(network_status_ip_address_mutex);
  145. }
  146. void network_status_safe_update_sta_ip_string(esp_ip4_addr_t* ip4) {
  147. if (network_status_lock_sta_ip_string(portMAX_DELAY)) {
  148. strcpy(network_status_ip_address, ip4 != NULL ? ip4addr_ntoa((ip4_addr_t*)ip4) : "0.0.0.0");
  149. ESP_LOGD(TAG, "Set STA IP String to: %s", network_status_ip_address);
  150. network_status_unlock_sta_ip_string();
  151. }
  152. }
  153. void network_status_safe_reset_sta_ip_string() {
  154. if (network_status_lock_sta_ip_string(portMAX_DELAY)) {
  155. strcpy(network_status_ip_address, "0.0.0.0");
  156. ESP_LOGD(TAG, "Set STA IP String to: %s", network_status_ip_address);
  157. network_status_unlock_sta_ip_string();
  158. }
  159. }
  160. char* network_status_get_sta_ip_string() {
  161. return network_status_ip_address;
  162. }
  163. void set_lms_server_details(in_addr_t ip, u16_t hport, u16_t cport) {
  164. strncpy(lms_server_ip, inet_ntoa(ip), sizeof(lms_server_ip));
  165. lms_server_ip[sizeof(lms_server_ip) - 1] = '\0';
  166. ESP_LOGI(TAG, "LMS IP: %s, hport: %d, cport: %d", lms_server_ip, hport, cport);
  167. lms_server_port = hport;
  168. lms_server_cport = cport;
  169. }
  170. static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport) {
  171. set_lms_server_details(ip, hport, cport);
  172. if (chained_notify)
  173. (*chained_notify)(ip, hport, cport);
  174. network_async_update_status();
  175. }
  176. void network_status_update_basic_info() {
  177. // locking happens below this level
  178. network_status_get_basic_info(&ip_info_cjson);
  179. }
  180. cJSON* network_status_update_float(cJSON** root, const char* key, float value) {
  181. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  182. if (*root == NULL) {
  183. *root = cJSON_CreateObject();
  184. }
  185. if (key && strlen(key) != 0) {
  186. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  187. if (cjsonvalue) {
  188. cJSON_SetNumberValue(cjsonvalue, value);
  189. } else {
  190. cJSON_AddNumberToObject(*root, key, value);
  191. }
  192. }
  193. network_status_unlock_json_buffer();
  194. } else {
  195. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  196. }
  197. return *root;
  198. }
  199. cJSON* network_status_update_bool(cJSON** root, const char* key, bool value) {
  200. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  201. if (*root == NULL) {
  202. *root = cJSON_CreateObject();
  203. }
  204. if (key && strlen(key) != 0) {
  205. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  206. if (cjsonvalue) {
  207. cjsonvalue->type = value ? cJSON_True : cJSON_False;
  208. } else {
  209. cJSON_AddBoolToObject(*root, key, value);
  210. }
  211. }
  212. network_status_unlock_json_buffer();
  213. } else {
  214. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  215. }
  216. return *root;
  217. }
  218. cJSON * network_update_cjson_string(cJSON** root, const char* key, const char* value){
  219. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  220. cjson_update_string(root, key, value);
  221. network_status_unlock_json_buffer();
  222. } else {
  223. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  224. }
  225. return *root;
  226. }
  227. cJSON * network_update_cjson_number(cJSON** root, const char* key, int value){
  228. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  229. cjson_update_number(root, key, value);
  230. network_status_unlock_json_buffer();
  231. } else {
  232. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  233. }
  234. return *root;
  235. }
  236. cJSON* network_status_get_basic_info(cJSON** old) {
  237. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  238. network_t* nm = network_get_state_machine();
  239. monitor_gpio_t* mgpio = get_jack_insertion_gpio();
  240. const esp_app_desc_t* desc = esp_ota_get_app_description();
  241. *old = network_update_cjson_string(old, "project_name", desc->project_name);
  242. #ifdef CONFIG_FW_PLATFORM_NAME
  243. *old = network_update_cjson_string(old, "platform_name", CONFIG_FW_PLATFORM_NAME);
  244. #endif
  245. *old = network_update_cjson_string(old, "version", desc->version);
  246. if (release_url != NULL)
  247. *old = network_update_cjson_string(old, "release_url", release_url);
  248. *old = network_update_cjson_number(old, "recovery", is_recovery_running ? 1 : 0);
  249. *old = network_status_update_bool(old, "Jack", mgpio->gpio >= 0 && jack_inserted_svc());
  250. *old = network_status_update_float(old, "Voltage", battery_value_svc());
  251. *old = network_update_cjson_number(old, "disconnect_count", nm->num_disconnect);
  252. *old = network_status_update_float(old, "avg_conn_time", nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0);
  253. #ifdef CONFIG_BT_ENABLED
  254. *old = network_update_cjson_number(old, "bt_status", bt_app_source_get_a2d_state());
  255. *old = network_update_cjson_number(old, "bt_sub_status", bt_app_source_get_media_state());
  256. #endif
  257. #if DEPTH == 16
  258. *old = network_update_cjson_number(old, "depth", 16);
  259. #elif DEPTH == 32
  260. *old = network_update_cjson_number(old, "depth", 32);
  261. #endif
  262. #if CONFIG_I2C_LOCKED
  263. *old = network_status_update_bool(old, "is_i2c_locked", true);
  264. #else
  265. *old = network_status_update_bool(old, "is_i2c_locked", false);
  266. #endif
  267. if (network_ethernet_enabled()) {
  268. *old = network_status_update_bool(old, "eth_up", network_ethernet_is_up());
  269. }
  270. if (lms_server_cport > 0) {
  271. *old = network_update_cjson_number(old, "lms_cport", lms_server_cport);
  272. }
  273. if (lms_server_port > 0) {
  274. *old = network_update_cjson_number(old, "lms_port", lms_server_port);
  275. }
  276. if (strlen(lms_server_ip) > 0) {
  277. *old = network_update_cjson_string(old, "lms_ip", lms_server_ip);
  278. }
  279. ESP_LOGV(TAG, "network_status_get_basic_info done");
  280. network_status_unlock_json_buffer();
  281. } else {
  282. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  283. }
  284. return *old;
  285. }
  286. void network_status_update_address(cJSON* root, esp_netif_ip_info_t* ip_info) {
  287. if (!root || !ip_info) {
  288. ESP_LOGE(TAG, "Cannor update IP address. JSON structure or ip_info is null");
  289. return;
  290. }
  291. network_update_cjson_string(&root, "ip", ip4addr_ntoa((ip4_addr_t*)&ip_info->ip));
  292. network_update_cjson_string(&root, "netmask", ip4addr_ntoa((ip4_addr_t*)&ip_info->netmask));
  293. network_update_cjson_string(&root, "gw", ip4addr_ntoa((ip4_addr_t*)&ip_info->gw));
  294. }
  295. void network_status_update_ip_info(update_reason_code_t update_reason_code) {
  296. ESP_LOGV(TAG, "network_status_update_ip_info called");
  297. esp_netif_ip_info_t ip_info;
  298. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  299. /* generate the connection info with success */
  300. ip_info_cjson = network_status_get_basic_info(&ip_info_cjson);
  301. ip_info_cjson = network_update_cjson_number(&ip_info_cjson, "urc", (int)update_reason_code);
  302. ESP_LOGD(TAG,"Updating ip info with reason code %d. Checking if Wifi interface is connected",update_reason_code);
  303. if (network_is_interface_connected(network_wifi_get_interface()) || update_reason_code == UPDATE_FAILED_ATTEMPT ) {
  304. network_update_cjson_string(&ip_info_cjson, "if", "wifi");
  305. esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
  306. network_status_update_address(ip_info_cjson, &ip_info);
  307. if (!network_wifi_is_ap_mode()) {
  308. /* wifi is active, and associated to an AP */
  309. wifi_ap_record_t ap;
  310. esp_wifi_sta_get_ap_info(&ap);
  311. network_update_cjson_string(&ip_info_cjson, "ssid", ((char*)ap.ssid));
  312. network_update_cjson_number(&ip_info_cjson, "rssi", ap.rssi);
  313. }
  314. } else {
  315. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "rssi");
  316. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ssid");
  317. }
  318. ESP_LOGD(TAG,"Checking if ethernet interface is connected");
  319. if (network_is_interface_connected(network_ethernet_get_interface())) {
  320. network_update_cjson_string(&ip_info_cjson, "if", "eth");
  321. esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
  322. network_status_update_address(ip_info_cjson, &ip_info);
  323. }
  324. network_status_unlock_json_buffer();
  325. } else {
  326. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  327. }
  328. ESP_LOGV(TAG, "wifi_status_generate_ip_info_json done");
  329. }