network_status.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_string(cJSON** root, const char* key, const char* value) {
  179. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  180. if (*root == NULL) {
  181. *root = cJSON_CreateObject();
  182. if (*root == NULL) {
  183. ESP_LOGE(TAG, "Error creating cJSON object!");
  184. }
  185. }
  186. if (!key || !value || strlen(key) == 0) {
  187. ESP_LOGE(TAG, "network_status_update_string. Invalid key or value passed! key: %s, value: %s", STR_OR_ALT(key, ""), STR_OR_ALT(value, ""));
  188. network_status_unlock_json_buffer();
  189. return *root;
  190. }
  191. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  192. if (cjsonvalue && strcasecmp(cJSON_GetStringValue(cjsonvalue), value) != 0) {
  193. ESP_LOGD(TAG, "Value %s changed from %s to %s", key, cJSON_GetStringValue(cjsonvalue), value);
  194. cJSON_SetValuestring(cjsonvalue, value);
  195. } else if(!cjsonvalue){
  196. cJSON_AddItemToObject(*root, key, cJSON_CreateString(value));
  197. }
  198. network_status_unlock_json_buffer();
  199. } else {
  200. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  201. }
  202. return *root;
  203. }
  204. cJSON* network_status_update_number(cJSON** root, const char* key, int value) {
  205. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  206. if (*root == NULL) {
  207. *root = cJSON_CreateObject();
  208. }
  209. if (key && strlen(key) != 0) {
  210. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  211. if (cjsonvalue) {
  212. cJSON_SetNumberValue(cjsonvalue, value);
  213. } else {
  214. cJSON_AddNumberToObject(*root, key, value);
  215. }
  216. }
  217. network_status_unlock_json_buffer();
  218. } else {
  219. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  220. }
  221. return *root;
  222. }
  223. cJSON* network_status_update_float(cJSON** root, const char* key, float value) {
  224. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  225. if (*root == NULL) {
  226. *root = cJSON_CreateObject();
  227. }
  228. if (key && strlen(key) != 0) {
  229. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  230. if (cjsonvalue) {
  231. cJSON_SetNumberValue(cjsonvalue, value);
  232. } else {
  233. cJSON_AddNumberToObject(*root, key, value);
  234. }
  235. }
  236. network_status_unlock_json_buffer();
  237. } else {
  238. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  239. }
  240. return *root;
  241. }
  242. cJSON* network_status_update_bool(cJSON** root, const char* key, bool value) {
  243. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  244. if (*root == NULL) {
  245. *root = cJSON_CreateObject();
  246. }
  247. if (key && strlen(key) != 0) {
  248. cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
  249. if (cjsonvalue) {
  250. cjsonvalue->type = value ? cJSON_True : cJSON_False;
  251. } else {
  252. cJSON_AddBoolToObject(*root, key, value);
  253. }
  254. }
  255. network_status_unlock_json_buffer();
  256. } else {
  257. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  258. }
  259. return *root;
  260. }
  261. cJSON* network_status_get_basic_info(cJSON** old) {
  262. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  263. network_t* nm = network_get_state_machine();
  264. monitor_gpio_t* mgpio = get_jack_insertion_gpio();
  265. const esp_app_desc_t* desc = esp_ota_get_app_description();
  266. *old = network_status_update_string(old, "project_name", desc->project_name);
  267. #ifdef CONFIG_FW_PLATFORM_NAME
  268. *old = network_status_update_string(old, "platform_name", CONFIG_FW_PLATFORM_NAME);
  269. #endif
  270. *old = network_status_update_string(old, "version", desc->version);
  271. if (release_url != NULL)
  272. *old = network_status_update_string(old, "release_url", release_url);
  273. *old = network_status_update_number(old, "recovery", is_recovery_running ? 1 : 0);
  274. *old = network_status_update_bool(old, "Jack", mgpio->gpio >= 0 && jack_inserted_svc());
  275. *old = network_status_update_float(old, "Voltage", battery_value_svc());
  276. *old = network_status_update_number(old, "disconnect_count", nm->num_disconnect);
  277. *old = network_status_update_float(old, "avg_conn_time", nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0);
  278. *old = network_status_update_number(old, "bt_status", bt_app_source_get_a2d_state());
  279. *old = network_status_update_number(old, "bt_sub_status", bt_app_source_get_media_state());
  280. #if CONFIG_I2C_LOCKED
  281. *old = network_status_update_bool(old, "is_i2c_locked", true);
  282. #else
  283. *old = network_status_update_bool(old, "is_i2c_locked", false);
  284. #endif
  285. if (network_ethernet_enabled()) {
  286. *old = network_status_update_bool(old, "eth_up", network_ethernet_is_up());
  287. }
  288. if (lms_server_cport > 0) {
  289. *old = network_status_update_number(old, "lms_cport", lms_server_cport);
  290. }
  291. if (lms_server_port > 0) {
  292. *old = network_status_update_number(old, "lms_port", lms_server_port);
  293. }
  294. if (strlen(lms_server_ip) > 0) {
  295. *old = network_status_update_string(old, "lms_ip", lms_server_ip);
  296. }
  297. ESP_LOGV(TAG, "network_status_get_basic_info done");
  298. network_status_unlock_json_buffer();
  299. } else {
  300. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  301. }
  302. return *old;
  303. }
  304. void network_status_update_address(cJSON* root, esp_netif_ip_info_t* ip_info) {
  305. if (!root || !ip_info) {
  306. ESP_LOGE(TAG, "Cannor update IP address. JSON structure or ip_info is null");
  307. return;
  308. }
  309. network_status_update_string(&root, "ip", ip4addr_ntoa((ip4_addr_t*)&ip_info->ip));
  310. network_status_update_string(&root, "netmask", ip4addr_ntoa((ip4_addr_t*)&ip_info->netmask));
  311. network_status_update_string(&root, "gw", ip4addr_ntoa((ip4_addr_t*)&ip_info->gw));
  312. }
  313. void network_status_update_ip_info(update_reason_code_t update_reason_code) {
  314. ESP_LOGV(TAG, "network_status_update_ip_info called");
  315. esp_netif_ip_info_t ip_info;
  316. if (network_status_lock_json_buffer(portMAX_DELAY)) {
  317. /* generate the connection info with success */
  318. ip_info_cjson = network_status_get_basic_info(&ip_info_cjson);
  319. ip_info_cjson = network_status_update_number(&ip_info_cjson, "urc", update_reason_code);
  320. ESP_LOGD(TAG,"Updating ip info with reason code %d. Checking if Wifi interface is connected",update_reason_code);
  321. if (network_is_interface_connected(network_wifi_get_interface()) || update_reason_code == UPDATE_FAILED_ATTEMPT ) {
  322. network_status_update_string(ip_info_cjson, "if", "wifi");
  323. esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
  324. network_status_update_address(ip_info_cjson, &ip_info);
  325. if (!network_wifi_is_ap_mode()) {
  326. /* wifi is active, and associated to an AP */
  327. wifi_ap_record_t ap;
  328. esp_wifi_sta_get_ap_info(&ap);
  329. network_status_update_string(&ip_info_cjson, "ssid", ((char*)ap.ssid));
  330. network_status_update_number(&ip_info_cjson, "rssi", ap.rssi);
  331. }
  332. } else {
  333. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "rssi");
  334. cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ssid");
  335. }
  336. ESP_LOGD(TAG,"Checking if ethernet interface is connected");
  337. if (network_is_interface_connected(network_ethernet_get_interface())) {
  338. network_status_update_string(ip_info_cjson, "if", "eth");
  339. esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
  340. network_status_update_address(ip_info_cjson, &ip_info);
  341. }
  342. network_status_unlock_json_buffer();
  343. } else {
  344. ESP_LOGW(TAG, "Unable to lock status json buffer. ");
  345. }
  346. ESP_LOGV(TAG, "wifi_status_generate_ip_info_json done");
  347. }