network_status.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "network_status.h"
  2. #include <string.h>
  3. #include "bt_app_core.h"
  4. #include "esp_log.h"
  5. #include "globdefs.h"
  6. #include "lwip/inet.h"
  7. #include "monitor.h"
  8. #include "network_ethernet.h"
  9. #include "network_wifi.h"
  10. #include "platform_config.h"
  11. #include "platform_esp32.h"
  12. #include "tools.h"
  13. #include "trace.h"
  14. #ifndef CONFIG_SQUEEZELITE_ESP32_RELEASE_URL
  15. #pragma message "Defaulting release url"
  16. #define CONFIG_SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
  17. #endif
  18. static const char TAG[] = "network_status";
  19. SemaphoreHandle_t wifi_manager_json_mutex = NULL;
  20. SemaphoreHandle_t wifi_manager_sta_ip_mutex = NULL;
  21. char* release_url = NULL;
  22. char* wifi_manager_sta_ip = NULL;
  23. char* ip_info_json = NULL;
  24. cJSON* ip_info_cjson = NULL;
  25. static char lms_server_ip[IP4ADDR_STRLEN_MAX] = {0};
  26. static uint16_t lms_server_port = 0;
  27. static uint16_t lms_server_cport = 0;
  28. static void (*chained_notify)(in_addr_t, u16_t, u16_t);
  29. static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport);
  30. #define STA_IP_LEN sizeof(char) * IP4ADDR_STRLEN_MAX
  31. void init_network_status() {
  32. chained_notify = server_notify;
  33. server_notify = connect_notify;
  34. ESP_LOGD(TAG, "init_network_status. Creating mutexes");
  35. wifi_manager_json_mutex = xSemaphoreCreateMutex();
  36. wifi_manager_sta_ip_mutex = xSemaphoreCreateMutex();
  37. ip_info_json = NULL;
  38. ESP_LOGD(TAG, "init_network_status. Creating status json structure");
  39. ip_info_cjson = wifi_manager_clear_ip_info_json(&ip_info_cjson);
  40. ESP_LOGD(TAG, "Getting release url ");
  41. char* release_url = (char*)config_alloc_get_default(NVS_TYPE_STR, "release_url", QUOTE(CONFIG_SQUEEZELITE_ESP32_RELEASE_URL), 0);
  42. if (release_url == NULL) {
  43. ESP_LOGE(TAG, "Unable to retrieve the release url from nvs");
  44. } else {
  45. ESP_LOGD(TAG, "Found release url %s", release_url);
  46. }
  47. ESP_LOGD(TAG, "About to set the STA IP String to 0.0.0.0");
  48. wifi_manager_sta_ip = (char*)malloc(STA_IP_LEN);
  49. wifi_manager_safe_update_sta_ip_string(NULL);
  50. }
  51. void destroy_network_status() {
  52. FREE_AND_NULL(release_url);
  53. FREE_AND_NULL(ip_info_json);
  54. FREE_AND_NULL(wifi_manager_sta_ip);
  55. cJSON_Delete(ip_info_cjson);
  56. vSemaphoreDelete(wifi_manager_json_mutex);
  57. wifi_manager_json_mutex = NULL;
  58. vSemaphoreDelete(wifi_manager_sta_ip_mutex);
  59. wifi_manager_sta_ip_mutex = NULL;
  60. ip_info_cjson = NULL;
  61. }
  62. cJSON* wifi_manager_get_new_json(cJSON** old) {
  63. ESP_LOGV(TAG, "wifi_manager_get_new_json called");
  64. cJSON* root = *old;
  65. if (root != NULL) {
  66. cJSON_Delete(root);
  67. *old = NULL;
  68. }
  69. ESP_LOGV(TAG, "wifi_manager_get_new_json done");
  70. return cJSON_CreateObject();
  71. }
  72. cJSON* wifi_manager_clear_ip_info_json(cJSON** old) {
  73. ESP_LOGV(TAG, "wifi_manager_clear_ip_info_json called");
  74. cJSON* root = wifi_manager_get_basic_info(old);
  75. ESP_LOGV(TAG, "wifi_manager_clear_ip_info_json done");
  76. return root;
  77. }
  78. void network_status_clear_ip() {
  79. if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
  80. ip_info_cjson = wifi_manager_clear_ip_info_json(&ip_info_cjson);
  81. wifi_manager_unlock_json_buffer();
  82. }
  83. }
  84. char* wifi_manager_alloc_get_ip_info_json() {
  85. return cJSON_PrintUnformatted(ip_info_cjson);
  86. }
  87. void wifi_manager_unlock_json_buffer() {
  88. ESP_LOGV(TAG, "Unlocking json buffer!");
  89. xSemaphoreGive(wifi_manager_json_mutex);
  90. }
  91. bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait) {
  92. ESP_LOGV(TAG, "Locking json buffer");
  93. if (wifi_manager_json_mutex) {
  94. if (xSemaphoreTake(wifi_manager_json_mutex, xTicksToWait) == pdTRUE) {
  95. ESP_LOGV(TAG, "Json buffer locked!");
  96. return true;
  97. } else {
  98. ESP_LOGE(TAG, "Semaphore take failed. Unable to lock json buffer mutex");
  99. return false;
  100. }
  101. } else {
  102. ESP_LOGV(TAG, "Unable to lock json buffer mutex");
  103. return false;
  104. }
  105. }
  106. bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait) {
  107. if (wifi_manager_sta_ip_mutex) {
  108. if (xSemaphoreTake(wifi_manager_sta_ip_mutex, xTicksToWait) == pdTRUE) {
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. } else {
  114. return false;
  115. }
  116. }
  117. void wifi_manager_unlock_sta_ip_string() {
  118. xSemaphoreGive(wifi_manager_sta_ip_mutex);
  119. }
  120. void wifi_manager_safe_update_sta_ip_string(esp_ip4_addr_t* ip4) {
  121. if (wifi_manager_lock_sta_ip_string(portMAX_DELAY)) {
  122. strcpy(wifi_manager_sta_ip, ip4 != NULL ? ip4addr_ntoa(ip4) : "0.0.0.0");
  123. ESP_LOGD(TAG, "Set STA IP String to: %s", wifi_manager_sta_ip);
  124. wifi_manager_unlock_sta_ip_string();
  125. }
  126. }
  127. void wifi_manager_safe_reset_sta_ip_string() {
  128. if (wifi_manager_lock_sta_ip_string(portMAX_DELAY)) {
  129. strcpy(wifi_manager_sta_ip, "0.0.0.0");
  130. ESP_LOGD(TAG, "Set STA IP String to: %s", wifi_manager_sta_ip);
  131. wifi_manager_unlock_sta_ip_string();
  132. }
  133. }
  134. char* wifi_manager_get_sta_ip_string() {
  135. return wifi_manager_sta_ip;
  136. }
  137. void set_lms_server_details(in_addr_t ip, u16_t hport, u16_t cport) {
  138. strncpy(lms_server_ip, inet_ntoa(ip), sizeof(lms_server_ip));
  139. lms_server_ip[sizeof(lms_server_ip) - 1] = '\0';
  140. ESP_LOGI(TAG, "LMS IP: %s, hport: %d, cport: %d", lms_server_ip, hport, cport);
  141. lms_server_port = hport;
  142. lms_server_cport = cport;
  143. }
  144. static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport) {
  145. set_lms_server_details(ip, hport, cport);
  146. if (chained_notify)
  147. (*chained_notify)(ip, hport, cport);
  148. network_manager_async_update_status();
  149. }
  150. void wifi_manager_update_basic_info() {
  151. int32_t total_connected_time = 0;
  152. int64_t last_connected = 0;
  153. uint16_t num_disconnect = 0;
  154. network_wifi_get_stats(&total_connected_time, &last_connected, &num_disconnect);
  155. if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
  156. monitor_gpio_t* mgpio = get_jack_insertion_gpio();
  157. cJSON* voltage = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "Voltage");
  158. if (voltage) {
  159. cJSON_SetNumberValue(voltage, battery_value_svc());
  160. }
  161. cJSON* bt_status = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "bt_status");
  162. if (bt_status) {
  163. cJSON_SetNumberValue(bt_status, bt_app_source_get_a2d_state());
  164. }
  165. cJSON* bt_sub_status = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "bt_sub_status");
  166. if (bt_sub_status) {
  167. cJSON_SetNumberValue(bt_sub_status, bt_app_source_get_media_state());
  168. }
  169. cJSON* jack = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "Jack");
  170. if (jack) {
  171. jack->type = mgpio->gpio >= 0 && jack_inserted_svc() ? cJSON_True : cJSON_False;
  172. }
  173. cJSON* disconnect_count = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "disconnect_count");
  174. if (disconnect_count) {
  175. cJSON_SetNumberValue(disconnect_count, num_disconnect);
  176. }
  177. cJSON* avg_conn_time = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "avg_conn_time");
  178. if (avg_conn_time) {
  179. cJSON_SetNumberValue(avg_conn_time, num_disconnect > 0 ? (total_connected_time / num_disconnect) : 0);
  180. }
  181. if (lms_server_cport > 0) {
  182. cJSON* value = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "lms_cport");
  183. if (value) {
  184. cJSON_SetNumberValue(value, lms_server_cport);
  185. } else {
  186. cJSON_AddNumberToObject(ip_info_cjson, "lms_cport", lms_server_cport);
  187. }
  188. }
  189. if (lms_server_port > 0) {
  190. cJSON* value = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "lms_port");
  191. if (value) {
  192. cJSON_SetNumberValue(value, lms_server_port);
  193. } else {
  194. cJSON_AddNumberToObject(ip_info_cjson, "lms_port", lms_server_port);
  195. }
  196. }
  197. if (strlen(lms_server_ip) > 0) {
  198. cJSON* value = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "lms_ip");
  199. if (!value) {
  200. // only create if it does not exist. Since we're creating a reference
  201. // to a char buffer, updates to cJSON aren't needed
  202. cJSON_AddItemToObject(ip_info_cjson, "lms_ip", cJSON_CreateStringReference(lms_server_ip));
  203. }
  204. }
  205. if (network_ethernet_enabled()) {
  206. cJSON* eth = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "eth_up");
  207. if (eth) {
  208. eth->type = network_ethernet_is_up() ? cJSON_True : cJSON_False;
  209. }
  210. }
  211. wifi_manager_unlock_json_buffer();
  212. }
  213. }
  214. cJSON* network_status_update_string(cJSON** root, const char* key, const char* value) {
  215. if (*root == NULL) {
  216. *root = cJSON_CreateObject();
  217. }
  218. if (!key || !value || strlen(key) == 0)
  219. return *root;
  220. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  221. if (cjsonvalue && strcasecmp(cJSON_GetStringValue(cjsonvalue), value) != 0) {
  222. ESP_LOGD(TAG, "Value %s changed from %s to %s", key, cJSON_GetStringValue(cjsonvalue), value);
  223. cJSON_SetValuestring(cjsonvalue, value);
  224. } else {
  225. cJSON_AddItemToObject(*root, key, cJSON_CreateString(value));
  226. }
  227. return *root;
  228. }
  229. cJSON* network_status_update_number(cJSON** root, const char* key, int value) {
  230. if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
  231. if (*root == NULL) {
  232. *root = cJSON_CreateObject();
  233. }
  234. if (key && value && strlen(key) != 0) {
  235. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  236. if (cjsonvalue) {
  237. cJSON_SetNumberValue(cjsonvalue, value);
  238. } else {
  239. cJSON_AddNumberToObject(*root, key, value);
  240. }
  241. }
  242. wifi_manager_unlock_json_buffer();
  243. } else {
  244. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  245. }
  246. return *root;
  247. }
  248. cJSON* network_status_update_float(cJSON** root, const char* key, float value) {
  249. if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
  250. if (*root == NULL) {
  251. *root = cJSON_CreateObject();
  252. }
  253. if (key && strlen(key) != 0) {
  254. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  255. if (cjsonvalue) {
  256. cJSON_SetNumberValue(cjsonvalue, value);
  257. } else {
  258. cJSON_AddNumberToObject(*root, key, value);
  259. }
  260. }
  261. wifi_manager_unlock_json_buffer();
  262. } else {
  263. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  264. }
  265. return *root;
  266. }
  267. cJSON* network_status_update_bool(cJSON** root, const char* key, bool value) {
  268. if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
  269. if (*root == NULL) {
  270. *root = cJSON_CreateObject();
  271. }
  272. if (key && strlen(key) != 0) {
  273. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  274. if (cjsonvalue) {
  275. cjsonvalue->type = value ? cJSON_True : cJSON_False;
  276. } else {
  277. cJSON_AddBoolToObject(*root, key, value);
  278. }
  279. }
  280. wifi_manager_unlock_json_buffer();
  281. } else {
  282. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  283. }
  284. return *root;
  285. }
  286. cJSON* wifi_manager_get_basic_info(cJSON** old) {
  287. int32_t total_connected_time = 0;
  288. int64_t last_connected = 0;
  289. uint16_t num_disconnect = 0;
  290. network_wifi_get_stats(&total_connected_time, &last_connected, &num_disconnect);
  291. monitor_gpio_t* mgpio = get_jack_insertion_gpio();
  292. const esp_app_desc_t* desc = esp_ota_get_app_description();
  293. ESP_LOGV(TAG, "wifi_manager_get_basic_info called");
  294. cJSON* root = network_status_update_string(&root, "project_name", desc->project_name);
  295. #ifdef CONFIG_FW_PLATFORM_NAME
  296. root = network_status_update_string(&root, "platform_name", CONFIG_FW_PLATFORM_NAME);
  297. #endif
  298. root = network_status_update_string(&root, "version", desc->version);
  299. if (release_url != NULL)
  300. root = network_status_update_string(&root, "release_url", release_url);
  301. root = network_status_update_number(&root, "recovery", is_recovery_running ? 1 : 0);
  302. root = network_status_update_bool(&root, "Jack", mgpio->gpio >= 0 && jack_inserted_svc());
  303. root = network_status_update_float(&root, "Voltage", battery_value_svc());
  304. root = network_status_update_number(&root, "disconnect_count", num_disconnect);
  305. root = network_status_update_float(&root, "avg_conn_time", num_disconnect > 0 ? (total_connected_time / num_disconnect) : 0);
  306. root = network_status_update_number(&root, "bt_status", bt_app_source_get_a2d_state());
  307. root = network_status_update_number(&root, "bt_sub_status", bt_app_source_get_media_state());
  308. #if CONFIG_I2C_LOCKED
  309. root = network_status_update_bool(&root, "is_i2c_locked", true);
  310. #else
  311. root = network_status_update_bool(&root, "is_i2c_locked", false);
  312. #endif
  313. if (network_ethernet_enabled()) {
  314. root = network_status_update_bool(&root, "eth_up", network_ethernet_is_up());
  315. }
  316. ESP_LOGV(TAG, "wifi_manager_get_basic_info done");
  317. return root;
  318. }
  319. void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code) {
  320. ESP_LOGD(TAG, "wifi_manager_generate_ip_info_json called");
  321. if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
  322. /* generate the connection info with success */
  323. ip_info_cjson = wifi_manager_get_basic_info(&ip_info_cjson);
  324. wifi_manager_unlock_json_buffer();
  325. } else {
  326. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  327. }
  328. ip_info_cjson = network_status_update_number(&ip_info_cjson, "urc", update_reason_code);
  329. if (update_reason_code == UPDATE_CONNECTION_OK || update_reason_code == UPDATE_ETHERNET_CONNECTED) {
  330. /* rest of the information is copied after the ssid */
  331. tcpip_adapter_ip_info_t ip_info;
  332. esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
  333. network_status_update_string(&ip_info_cjson, "ip", ip4addr_ntoa((ip4_addr_t*)&ip_info.ip));
  334. network_status_update_string(&ip_info_cjson, "netmask", ip4addr_ntoa((ip4_addr_t*)&ip_info.netmask));
  335. network_status_update_string(&ip_info_cjson, "gw", ip4addr_ntoa((ip4_addr_t*)&ip_info.gw));
  336. wifi_mode_t mode;
  337. if (esp_wifi_get_mode(&mode) == ESP_OK && (mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA)) {
  338. /* wifi is active, and associated to an AP */
  339. wifi_ap_record_t ap;
  340. esp_wifi_sta_get_ap_info(&ap);
  341. network_status_update_string(&ip_info_cjson, "ssid", ((char*)ap.ssid));
  342. network_status_update_number(&ip_info_cjson, "rssi", ap.rssi);
  343. }
  344. if (network_ethernet_is_up()) {
  345. esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
  346. cJSON* ethernet_ip = cJSON_CreateObject();
  347. cJSON_AddItemToObject(ethernet_ip, "ip", cJSON_CreateString(ip4addr_ntoa((ip4_addr_t*)&ip_info.ip)));
  348. cJSON_AddItemToObject(ethernet_ip, "netmask", cJSON_CreateString(ip4addr_ntoa((ip4_addr_t*)&ip_info.netmask)));
  349. cJSON_AddItemToObject(ethernet_ip, "gw", cJSON_CreateString(ip4addr_ntoa((ip4_addr_t*)&ip_info.gw)));
  350. cJSON_AddItemToObject(ip_info_cjson, "eth", ethernet_ip);
  351. }
  352. } else {
  353. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ip");
  354. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "netmask");
  355. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "gw");
  356. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "rssi");
  357. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ssid");
  358. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "eth");
  359. }
  360. ESP_LOGV(TAG, "wifi_manager_generate_ip_info_json done");
  361. }