Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listening Mode Timeout defaults and set|get API [ch318] #1182

Merged
merged 6 commits into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions docs/reference/firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,54 @@ It will return `false` when the device is not in listening mode.
{{/if}}


### setListenTimeout()

```cpp
// SYNTAX
WiFi.setListenTimeout(seconds);
```

`WiFi.setListenTimeout(seconds)` is used to set a timeout value for Listening Mode. Values are specified in `seconds`, and 0 disables the timeout. By default, Wi-Fi devices do not have any timeout set (seconds=0). As long as interrupts are enabled, a timer is started and running while the device is in listening mode (WiFi.listening()==true). After the timer expires, listening mode will be exited automatically. If WiFi.setListenTimeout() is called while the timer is currently in progress, the timer will be updated and restarted with the new value (e.g. updating from 10 seconds to 30 seconds, or 10 seconds to 0 seconds (disabled)). {{#unless core}}**Note:** Enabling multi-threaded mode with SYSTEM_THREAD(ENABLED) will allow user code to update the timeout value while Listening Mode is active.{{/unless}} {{#if core}}Because listening mode blocks your application code on the Core, this command should be avoided in loop(). It can be used with the STARTUP() macro or in setup() on the Core.
It will always return `false`.

This setting is not persistent in memory if the {{device}} is rebooted.
{{/if}}

```cpp
// EXAMPLE
// If desired, use the STARTUP() macro to set the timeout value at boot time.
STARTUP(WiFi.setListenTimeout(60)); // set listening mode timeout to 60 seconds

void setup() {
// your setup code
}
{{#unless core}}
void loop() {
// update the timeout later in code based on an expression
if (disableTimeout) WiFi.setListenTimeout(0); // disables the listening mode timeout
}
{{/unless}}
```


### getListenTimeout()

```cpp
// SYNTAX
uint16_t seconds = WiFi.getListenTimeout();
```

`WiFi.getListenTimeout()` is used to get the timeout value currently set for Listening Mode. Values are returned in (uint16_t)`seconds`, and 0 indicates the timeout is disabled. By default, Wi-Fi devices do not have any timeout set (seconds=0).

```cpp
// EXAMPLE
void setup() {
Serial.begin();
Serial.println(WiFi.getListenTimeout());
}
```


### setCredentials()

Allows the application to set credentials for the Wi-Fi network from within the code. These credentials will be added to the device's memory, and the device will automatically attempt to connect to this network in the future.
Expand Down Expand Up @@ -1664,6 +1712,52 @@ or the setup button has been held for 3 seconds, when the RGB LED should be blin
It will return `false` when the device is not in listening mode.


### setListenTimeout()

```cpp
// SYNTAX
Cellular.setListenTimeout(seconds);
```

`Cellular.setListenTimeout(seconds)` is used to set a timeout value for Listening Mode. Values are specified in `seconds`, and 0 disables the timeout. By default, Cellular devices have a 5 minute timeout set (seconds=300). As long as interrupts are enabled, a timer is started and running while the device is in listening mode (Cellular.listening()==true). After the timer expires, listening mode will be exited automatically. If Cellular.setListenTimeout() is called while the timer is currently in progress, the timer will be updated and restarted with the new value (e.g. updating from 10 seconds to 30 seconds, or 10 seconds to 0 seconds (disabled)). **Note:** Enabling multi-threaded mode with SYSTEM_THREAD(ENABLED) will allow user code to update the timeout value while Listening Mode is active.

This setting is not persistent in memory if the {{device}} is rebooted.

```cpp
// EXAMPLE
// If desired, use the STARTUP() macro to set the timeout value at boot time.
STARTUP(Cellular.setListenTimeout(60)); // set listening mode timeout to 60 seconds

void setup() {
// your setup code
}

void loop() {
// update the timeout later in code based on an expression
if (disableTimeout) Cellular.setListenTimeout(0); // disables the listening mode timeout
}
```


### getListenTimeout()

```cpp
// SYNTAX
uint16_t seconds = Cellular.getListenTimeout();
```

`Cellular.getListenTimeout()` is used to get the timeout value currently set for Listening Mode. Values are returned in (uint16_t)`seconds`, and 0 indicates the timeout is disabled. By default, Cellular devices have a 5 minute timeout set (seconds=300).

```cpp
// EXAMPLE
void setup() {
Serial.begin();
Serial.println(Cellular.getListenTimeout());
}
```



### setCredentials()

**Note**: `Cellular.setCredentials()` is not currently enabled, however read on to find out how to use the cellular_hal to do the same thing with `cellular_credentials_set()`.
Expand Down
2 changes: 1 addition & 1 deletion hal/src/electron/modem/mdm_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ bool MDMParser::powerOn(const char* simpin)
{
LOCK();
memset(&_dev, 0, sizeof(_dev));
bool retried_after_reset = false;

/* Power on the modem and perform basic initialization */
if (!_powerOn())
Expand All @@ -571,7 +572,6 @@ bool MDMParser::powerOn(const char* simpin)

// check the sim card
for (int i = 0; (i < 5) && (_dev.sim != SIM_READY) && !_cancel_all_operations; i++) {
static bool retried_after_reset = false;
sendFormated("AT+CPIN?\r\n");
int ret = waitFinalResp(_cbCPIN, &_dev.sim);
// having an error here is ok (sim may still be initializing)
Expand Down
2 changes: 2 additions & 0 deletions system/inc/system_dynalib_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ DYNALIB_FN(8, system_net, network_listening, bool(network_handle_t, uint32_t, vo
DYNALIB_FN(9, system_net, network_has_credentials, bool(network_handle_t, uint32_t, void*))
DYNALIB_FN(10, system_net, network_set_credentials, int(network_handle_t, uint32_t, NetworkCredentials*, void*))
DYNALIB_FN(11, system_net, network_clear_credentials, bool(network_handle_t, uint32_t, NetworkCredentials*, void*))
DYNALIB_FN(12, system_net, network_set_listen_timeout, void(network_handle_t, uint16_t, void*))
DYNALIB_FN(13, system_net, network_get_listen_timeout, uint16_t(network_handle_t, uint32_t, void*))

DYNALIB_END(system_net)

Expand Down
2 changes: 2 additions & 0 deletions system/inc/system_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void network_off(network_handle_t network, uint32_t flags, uint32_t param1, void
* @param reserved
*/
void network_listen(network_handle_t network, uint32_t flags, void* reserved);
void network_set_listen_timeout(network_handle_t network, uint16_t timeout, void* reserved);
uint16_t network_get_listen_timeout(network_handle_t network, uint32_t flags, void* reserved);
bool network_listening(network_handle_t network, uint32_t param1, void* reserved);


Expand Down
5 changes: 4 additions & 1 deletion system/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ void HAL_Notify_Button_State(uint8_t button, uint8_t pressed)
handle_button_click(depressed_duration);
}
pressed_time = 0;
if (depressed_duration>3000 && depressed_duration<8000 && wasListeningOnButtonPress && network.listening())
if (depressed_duration>3000 && depressed_duration<8000 && wasListeningOnButtonPress && network.listening()) {
network.listen(true);
}
}
}
}
Expand Down Expand Up @@ -450,6 +451,8 @@ extern "C" void HAL_SysTick_Handler(void)
// there's a race condition here - the HAL_notify_button_state function should
// be thread safe, but currently isn't.
HAL_Notify_Button_State(0, false);
// LOG(INFO,"BUTTON PRESSED FOR LISTENING");
// TODO: this code is called repeatedly every 1ms while the button is held (from 3-8s) and should only be called once
network.listen();
HAL_Notify_Button_State(0, true);
}
Expand Down
10 changes: 10 additions & 0 deletions system/src/system_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ void network_listen(network_handle_t network, uint32_t flags, void*)
}
}

void network_set_listen_timeout(network_handle_t network, uint16_t timeout, void*)
{
return nif(network).set_listen_timeout(timeout);
}

uint16_t network_get_listen_timeout(network_handle_t network, uint32_t flags, void*)
{
return nif(network).get_listen_timeout();
}

bool network_listening(network_handle_t network, uint32_t, void*)
{
return nif(network).listening();
Expand Down
71 changes: 39 additions & 32 deletions system/src/system_network_cellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

class CellularNetworkInterface : public ManagedIPNetworkInterface<CellularConfig, CellularNetworkInterface>
{
volatile bool connect_cancelled = false;
volatile bool connecting = false;
volatile bool connect_cancelled = false;
volatile bool connecting = false;

protected:

Expand All @@ -51,44 +51,43 @@ class CellularNetworkInterface : public ManagedIPNetworkInterface<CellularConfig
void connect_finalize_impl() {
cellular_result_t result = -1;
result = cellular_init(NULL);
if (result) return;
if (result) { return; }

result = cellular_register(NULL);
if (result) return;
if (result) { return; }

CellularCredentials* savedCreds;
savedCreds = cellular_credentials_get(NULL);
result = cellular_pdp_activate(savedCreds, NULL);
if (result) return;
if (result) { return; }

//DEBUG_D("savedCreds = %s %s %s\r\n", savedCreds->apn, savedCreds->username, savedCreds->password);
result = cellular_gprs_attach(savedCreds, NULL);
if (result) return;
if (result) { return; }

HAL_NET_notify_connected();
HAL_NET_notify_dhcp(true);
}

void connect_finalize() override {
ATOMIC_BLOCK() { connecting = true; }
ATOMIC_BLOCK() { connecting = true; }

connect_finalize_impl();
connect_finalize_impl();

bool require_resume = false;
bool require_resume = false;

ATOMIC_BLOCK() {
// ensure after connection exits the cancel flag is cleared if it was set during connection
if (connect_cancelled) {
require_resume = true;
}
connecting = false;
// ensure after connection exits the cancel flag is cleared if it was set during connection
if (connect_cancelled) {
require_resume = true;
}
connecting = false;
}
if (require_resume) {
cellular_cancel(false, HAL_IsISR(), NULL);
}
if (require_resume)
cellular_cancel(false, HAL_IsISR(), NULL);
}



void on_now() override {
cellular_on(NULL);
}
Expand Down Expand Up @@ -138,24 +137,32 @@ class CellularNetworkInterface : public ManagedIPNetworkInterface<CellularConfig
bool clear_credentials() override { /* n/a */ return true; }
bool has_credentials() override
{
return cellular_sim_ready(NULL);
bool rv = cellular_sim_ready(NULL);
LOG(INFO,"%s", (rv)?"Sim Ready":"Sim not inserted? Detecting...");
if (!rv) {
cellular_on(NULL);
rv = cellular_sim_ready(NULL);
LOG(INFO,"%s", (rv)?"Sim Ready":"Sim not inserted.");
}
return rv;
}
int set_credentials(NetworkCredentials* creds) override { /* n/a */ return -1; }

void connect_cancel(bool cancel) override {
// only cancel if presently connecting
bool require_cancel = false;
ATOMIC_BLOCK() {
if (connecting)
{
if (cancel!=connect_cancelled) {
require_cancel = true;
connect_cancelled = cancel;
}
}
}
if (require_cancel)
cellular_cancel(cancel, HAL_IsISR(), NULL);
// only cancel if presently connecting
bool require_cancel = false;
ATOMIC_BLOCK() {
if (connecting)
{
if (cancel!=connect_cancelled) {
require_cancel = true;
connect_cancelled = cancel;
}
}
}
if (require_cancel) {
cellular_cancel(cancel, HAL_IsISR(), NULL);
}
}

void set_error_count(unsigned count) override { /* n/a */ }
Expand Down
Loading