Skip to content

Commit

Permalink
Uninitialize WICED's WLAN subsystem when turning off the WiFi connect…
Browse files Browse the repository at this point in the history
…ivity; cancel active connection attempt to unblock the system thread
  • Loading branch information
sergeuz committed Sep 23, 2016
1 parent 60f703b commit ba6f212
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
3 changes: 2 additions & 1 deletion hal/src/photon/wlan_hal.cpp
Expand Up @@ -308,7 +308,8 @@ wlan_result_t wlan_activate()
wlan_result_t wlan_deactivate()
{
wlan_disconnect_now();
return 0;
wiced_result_t result = wiced_wlan_connectivity_deinit();
return result;
}

wlan_result_t wlan_disconnect_now()
Expand Down
9 changes: 7 additions & 2 deletions system/src/system_sleep.cpp
Expand Up @@ -23,6 +23,7 @@
#include "system_task.h"
#include "system_cloud.h"
#include "system_cloud_internal.h"
#include "system_network_internal.h"
#include "system_threading.h"
#include "rtc_hal.h"
#include "core_hal.h"
Expand All @@ -43,10 +44,12 @@ WakeupState wakeupState;

static void network_suspend() {
// save the current state so it can be restored on wakeup
wakeupState.wifi = !SPARK_WLAN_SLEEP;
wakeupState.wifiConnected = wakeupState.cloud | network_ready(0, 0, NULL) | network_connecting(0, 0, NULL);
#ifndef SPARK_NO_CLOUD
wakeupState.cloud = spark_cloud_flag_auto_connect();
#endif
wakeupState.wifi = !SPARK_WLAN_SLEEP;
wakeupState.wifiConnected = wakeupState.cloud || network_ready(0, 0, NULL) || network_connecting(0, 0, NULL);
#ifndef SPARK_NO_CLOUD
// disconnect the cloud now, and clear the auto connect status
spark_cloud_socket_disconnect();
spark_cloud_flag_disconnect();
Expand Down Expand Up @@ -173,5 +176,7 @@ int system_sleep_pin_impl(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long sec
*/
void system_sleep_pin(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long seconds, uint32_t param, void* reserved)
{
// Cancel current connection attempt to unblock the system thread
network.connect_cancel(true);
system_sleep_pin_impl(wakeUpPin, edgeTriggerMode, seconds, param, reserved);
}
42 changes: 42 additions & 0 deletions user/tests/app/stop_mode_power_usage_issue_1098/app.cpp
@@ -0,0 +1,42 @@
#include "application.h"

#define MIN_CONNECT_DURATION 1000 // Milliseconds
#define MAX_CONNECT_DURATION 5000
#define SLEEP_DURATION 5 // Seconds

SYSTEM_MODE(MANUAL)
SYSTEM_THREAD(ENABLED)

namespace {

SerialLogHandler logHandler(LOG_LEVEL_WARN, { // Default logging level
{ "app", LOG_LEVEL_ALL } // Logging level for application messages
});

uint32_t t = 0, d = 0;

void resetTimer() {
d = rand() % (MAX_CONNECT_DURATION - MIN_CONNECT_DURATION) + MIN_CONNECT_DURATION;
t = millis();
}

} // namespace

void setup() {
// Set incorrect WiFi credentials to cause repeated connection attempts
WiFi.on();
WiFi.clearCredentials();
WiFi.setCredentials("param-pam-pam");
WiFi.connect();
resetTimer();
}

void loop() {
if (millis() - t >= d) {
LOG(INFO, "Entering stop mode");
// The current taken by the device should be around 3 mA when the stop mode is active
System.sleep(D1, RISING, SLEEP_DURATION);
LOG(INFO, "Leaving stop mode");
resetTimer();
}
}

0 comments on commit ba6f212

Please sign in to comment.