network_status.c 14 KB

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