network_status.c 14 KB

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