network_status.c 15 KB

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