Skip to content

Commit

Permalink
Wiznetif: report module power state.
Browse files Browse the repository at this point in the history
  • Loading branch information
XuGuohui committed Dec 23, 2020
1 parent d7d25e5 commit 6d1c316
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
41 changes: 37 additions & 4 deletions hal/network/lwip/wiznet/wiznetif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ WizNetif::WizNetif(hal_spi_interface_t spi, pin_t cs, pin_t reset, pin_t interru
cs_(cs),
reset_(reset),
interrupt_(interrupt),
pwrState_(IF_POWER_STATE_NONE),
spiLock_(spi, WIZNET_DEFAULT_CONFIG) {

LOG(INFO, "Creating Wiznet LwIP interface");
Expand Down Expand Up @@ -265,8 +266,10 @@ err_t WizNetif::initInterface() {

hwReset();
if (!isPresent()) {
pwrState_ = IF_POWER_STATE_DOWN;
return ERR_IF;
}
pwrState_ = IF_POWER_STATE_UP;

return ERR_OK;
}
Expand Down Expand Up @@ -345,6 +348,12 @@ int WizNetif::up() {
down_ = false;
}

// Just in case. We might call up() directly after powerDown().
if (pwrState_ != IF_POWER_STATE_UP) {
pwrState_ = IF_POWER_STATE_UP;
notifyPowerState();
}

return r;
}

Expand All @@ -358,16 +367,40 @@ int WizNetif::down() {
}

int WizNetif::powerUp() {
return 0;
// FIXME: As long as the interface is initialized,
// it must have been powered up as of right now.
if (pwrState_ != IF_POWER_STATE_UP) {
pwrState_ = IF_POWER_STATE_UP;
notifyPowerState();
}
return SYSTEM_ERROR_NONE;
}

int WizNetif::powerDown() {
return down();
int ret = down();
// FIXME: This don't really power off the module.
// Notify the system network manager that the module is powered down
// to bypass waitInterfaceOff() as required by system sleep.
if (pwrState_ != IF_POWER_STATE_DOWN) {
pwrState_ = IF_POWER_STATE_DOWN;
notifyPowerState();
}
return ret;
}

void WizNetif::notifyPowerState() {
if_event evt = {};
struct if_event_power_state ev_if_power_state = {};
evt.ev_len = sizeof(if_event);
evt.ev_type = IF_EVENT_POWER_STATE;
evt.ev_power_state = &ev_if_power_state;
evt.ev_power_state->state = pwrState_;
if_notify_event(interface(), &evt, nullptr);
}

int WizNetif::getPowerState(if_power_state_t* state) const {
// TODO: implement it
return SYSTEM_ERROR_NOT_SUPPORTED;
*state = pwrState_;
return SYSTEM_ERROR_NONE;
}

int WizNetif::getNcpState(unsigned int* state) const {
Expand Down
4 changes: 4 additions & 0 deletions hal/network/lwip/wiznet/wiznetif.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ class WizNetif : public BaseNetif {
static err_t linkOutputCb(netif* netif, pbuf* p);
err_t linkOutput(pbuf* p);

void notifyPowerState();

private:
hal_spi_interface_t spi_;
pin_t cs_;
pin_t reset_;
pin_t interrupt_;

std::atomic<if_power_state_t> pwrState_;

os_thread_t thread_ = nullptr;
os_queue_t queue_ = nullptr;
os_semaphore_t spiSem_ = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion system/src/system_sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static bool system_sleep_network_suspend(network_interface_index index) {
#endif
// Turn off the modem
network_off(index, 0, 0, NULL);
LOG(TRACE, "Waiting interface to be off...");
LOG(TRACE, "Waiting interface %d to be off...", (int)index);
// There might be up to 30s delay to turn off the modem for particular platforms.
network_wait_off(index, 120000/*ms*/, nullptr);
return resume;
Expand Down

0 comments on commit 6d1c316

Please sign in to comment.