diff --git a/system/inc/system_task.h b/system/inc/system_task.h index ca37f55cac..c15a273df1 100644 --- a/system/inc/system_task.h +++ b/system/inc/system_task.h @@ -57,6 +57,7 @@ extern volatile uint32_t TimingFlashUpdateTimeout; extern volatile uint8_t SPARK_WLAN_RESET; extern volatile uint8_t SPARK_WLAN_SLEEP; +extern volatile uint8_t SPARK_WLAN_CONNECT_RESTORE; extern volatile uint8_t SPARK_WLAN_STARTED; extern volatile uint8_t SPARK_CLOUD_SOCKETED; extern volatile uint8_t SPARK_CLOUD_CONNECTED; diff --git a/system/src/system_network_compat.cpp b/system/src/system_network_compat.cpp index f26ecc33c3..a5f570f461 100644 --- a/system/src/system_network_compat.cpp +++ b/system/src/system_network_compat.cpp @@ -40,6 +40,7 @@ uint32_t wlan_watchdog_duration = 0; volatile uint8_t SPARK_WLAN_RESET = 0; volatile uint8_t SPARK_WLAN_SLEEP = 0; +volatile uint8_t SPARK_WLAN_CONNECT_RESTORE = 0; volatile uint8_t SPARK_WLAN_STARTED = 0; @@ -248,6 +249,7 @@ void manage_network_connection() { WARN("Resetting WLAN due to %s", (WLAN_WD_TO()) ? "WLAN_WD_TO()":((SPARK_WLAN_RESET) ? "SPARK_WLAN_RESET" : "SPARK_WLAN_SLEEP")); auto was_sleeping = SPARK_WLAN_SLEEP; + SPARK_WLAN_CONNECT_RESTORE = network_ready(0, 0, 0) || network_connecting(0, 0, 0); //auto was_disconnected = network.manual_disconnect(); // Note: The cloud connectivity layer may "detect" an unanticipated network disconnection // before the networking layer, and due to current recovery logic, which resets the network, @@ -264,9 +266,18 @@ void manage_network_connection() } else { - if (!SPARK_WLAN_STARTED || (spark_cloud_flag_auto_connect() && !network_ready(0, 0, 0))) - { - // INFO("Network Connect: %s", (!SPARK_WLAN_STARTED) ? "!SPARK_WLAN_STARTED" : "SPARK_CLOUD_CONNECT && !network.ready()"); + if (!SPARK_WLAN_STARTED) { + // INFO("Network On: !SPARK_WLAN_STARTED"); + network_on(0, 0, 0, 0); + } + + if ((SPARK_WLAN_CONNECT_RESTORE || spark_cloud_flag_auto_connect()) && !network_ready(0, 0, 0)) { + // INFO("Network Connect:%s%s", (SPARK_WLAN_CONNECT_RESTORE ? " SPARK_WLAN_CONNECT_RESTORE" : " "), + // (spark_cloud_flag_auto_connect() ? " spark_cloud_flag_auto_connect()" : " ")); + + // XXX: If the auto-connect flag is not set, we used to only call network_connect() once here, + // even if it failed to connect to network for whatever reason. So we should clear the flag here. + SPARK_WLAN_CONNECT_RESTORE = 0; network_connect(0, 0, 0, 0); } else { nif(0).process(); diff --git a/system/src/system_network_internal.h b/system/src/system_network_internal.h index a2a0c516d9..b33827cf89 100644 --- a/system/src/system_network_internal.h +++ b/system/src/system_network_internal.h @@ -43,6 +43,7 @@ enum eWanTimings extern volatile uint8_t SPARK_WLAN_RESET; extern volatile uint8_t SPARK_WLAN_SLEEP; +extern volatile uint8_t SPARK_WLAN_CONNECT_RESTORE; extern volatile uint8_t SPARK_WLAN_STARTED; extern uint32_t wlan_watchdog_duration; diff --git a/system/src/system_network_manager_api.cpp b/system/src/system_network_manager_api.cpp index 3adb6c38ac..3f33a6fb27 100644 --- a/system/src/system_network_manager_api.cpp +++ b/system/src/system_network_manager_api.cpp @@ -48,6 +48,7 @@ uint32_t wlan_watchdog_duration; /* FIXME */ volatile uint8_t SPARK_WLAN_RESET; volatile uint8_t SPARK_WLAN_SLEEP; +volatile uint8_t SPARK_WLAN_CONNECT_RESTORE; volatile uint8_t SPARK_WLAN_STARTED; extern int cfod_count; diff --git a/system/src/system_sleep.cpp b/system/src/system_sleep.cpp index 1ab0eae4bc..1827aa47ae 100644 --- a/system/src/system_sleep.cpp +++ b/system/src/system_sleep.cpp @@ -60,11 +60,11 @@ network_status_t system_sleep_network_suspend(network_interface_index index) { status.suspended = true; // Disconnect from network - if (network_connecting(index, 0, NULL) || network_ready(index, 0, NULL)) { - if (network_connecting(index, 0, NULL)) { + if (network_connecting(index, 0, nullptr) || network_ready(index, 0, nullptr)) { + if (network_connecting(index, 0, nullptr)) { network_connect_cancel(index, 1, 0, 0); } - network_disconnect(index, NETWORK_DISCONNECT_REASON_SLEEP, NULL); + network_disconnect(index, NETWORK_DISCONNECT_REASON_SLEEP, nullptr); status.connected = true; } @@ -101,9 +101,12 @@ int system_sleep_network_resume(network_interface_index index, network_status_t * if single threaded, or this function is invoked synchronously by the system thread if system threading * is enabled. In both case, that would block the user application. Setting a flag here to unblock the user * application and restore the connection later. */ - if (status.on || status.connected) { + if (status.on) { SPARK_WLAN_SLEEP = 0; } + if (status.connected) { + SPARK_WLAN_CONNECT_RESTORE = 1; + } #else if (status.on) { network_on(index, 0, 0, nullptr); @@ -254,7 +257,7 @@ int system_sleep_ext(const hal_sleep_config_t* config, hal_wakeup_source_base_t* #if HAL_PLATFORM_GEN == 2 // Cancel current connection attempt to unblock the system thread // on Gen 2 platforms - if (network_connecting(NETWORK_INTERFACE_ALL, 0, NULL)) { + if (network_connecting(NETWORK_INTERFACE_ALL, 0, nullptr)) { network_connect_cancel(NETWORK_INTERFACE_ALL, 1, 0, 0); } #endif // HAL_PLATFORM_GEN == 2 diff --git a/system/src/system_sleep_compat.cpp b/system/src/system_sleep_compat.cpp index cb136557b0..5f02ec3d54 100644 --- a/system/src/system_sleep_compat.cpp +++ b/system/src/system_sleep_compat.cpp @@ -52,20 +52,27 @@ static void network_suspend() { wakeupState.cloud = spark_cloud_flag_auto_connect(); wakeupState.wifi = !SPARK_WLAN_SLEEP; - wakeupState.wifiConnected = wakeupState.cloud || network_ready(0, 0, NULL) || network_connecting(0, 0, NULL); + wakeupState.wifiConnected = wakeupState.cloud || network_ready(0, 0, nullptr) || network_connecting(0, 0, nullptr); // Disconnect the cloud and the network - network_disconnect(0, NETWORK_DISCONNECT_REASON_SLEEP, NULL); + network_disconnect(0, NETWORK_DISCONNECT_REASON_SLEEP, nullptr); // Clear the auto connect status spark_cloud_flag_disconnect(); - network_off(0, 0, 0, NULL); + network_off(0, 0, 0, nullptr); } static void network_resume() { - // Set the system flags that triggers the wifi/cloud reconnection in the background loop - if (wakeupState.wifiConnected || wakeupState.wifi) // at present, no way to get the background loop to only turn on wifi. + if (wakeupState.wifi) { SPARK_WLAN_SLEEP = 0; - if (wakeupState.cloud) + } + // Gen2-only: Set the system flags that triggers the wifi/cloud reconnection in the background loop + // FIXME: Gen3 won't automatically restore the modem state and network connection if cloud auto-connect flag is not set. + // See manage_network_connection() in system_network_manager_api.cpp. + if (wakeupState.wifiConnected) { + SPARK_WLAN_CONNECT_RESTORE = 1; + } + if (wakeupState.cloud) { spark_cloud_flag_connect(); + } } /******************************************************************************* @@ -205,8 +212,8 @@ int system_sleep_impl(Spark_Sleep_TypeDef sleepMode, long seconds, uint32_t para #if HAL_PLATFORM_SETUP_BUTTON_UX case SLEEP_MODE_SOFTPOWEROFF: if (!networkTurnedOff) { - network_disconnect(0, NETWORK_DISCONNECT_REASON_SLEEP, NULL); - network_off(0, 0, 0, NULL); + network_disconnect(0, NETWORK_DISCONNECT_REASON_SLEEP, nullptr); + network_off(0, 0, 0, nullptr); } return system_sleep_enter_standby_compat(seconds, param); #endif