Skip to content

Commit

Permalink
Merge pull request #2309 from particle-iot/fix/unexpected_connection_…
Browse files Browse the repository at this point in the history
…establishment

Gen2: network connection is established unexpectedly.
  • Loading branch information
XuGuohui committed Apr 30, 2021
2 parents 19db7e8 + 644aab7 commit 7bcea8f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
1 change: 1 addition & 0 deletions system/inc/system_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 14 additions & 3 deletions system/src/system_network_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions system/src/system_network_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions system/src/system_network_manager_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 8 additions & 5 deletions system/src/system_sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
23 changes: 15 additions & 8 deletions system/src/system_sleep_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/*******************************************************************************
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7bcea8f

Please sign in to comment.