Skip to content

weak hook preinit() #2111 #2133 #2136 #5395

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

Merged
merged 25 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
671bbac
weak hook early_setup() #2111 #2133 #2136
d-a-v Nov 30, 2018
53a2d97
Merge branch 'master' into earlysetup
d-a-v Nov 30, 2018
bf76433
rename to early_init (more "c" vs early_setup which is more "c++ardui…
d-a-v Nov 30, 2018
d296d7b
example
d-a-v Nov 30, 2018
dbf6045
improve earlyWiFi example, slightly change AddrList interface, move W…
d-a-v Nov 30, 2018
acdbe07
fix CI
d-a-v Nov 30, 2018
23b7ace
fix local CI runner
d-a-v Nov 30, 2018
b75c40d
Merge branch 'master' into earlysetup
devyte Dec 2, 2018
9c043f6
fix local CI runner
d-a-v Dec 3, 2018
3393ecc
Merge branch 'master' into earlysetup
d-a-v Dec 3, 2018
e8e0b5c
rename early_init() to preinit()
d-a-v Dec 3, 2018
e014a19
Merge branch 'earlysetup' of github.com:d-a-v/Arduino into earlysetup
d-a-v Dec 3, 2018
5aa7ee6
Merge branch 'master' into earlysetup
d-a-v Dec 3, 2018
f887ce8
+ static ESP8266WiFiClass::preinit_wifi_off()
d-a-v Dec 3, 2018
6c09fbd
update early disable wifi example
d-a-v Dec 3, 2018
a636a80
example update
d-a-v Dec 3, 2018
5d770aa
IPv6 example update
d-a-v Dec 3, 2018
6d9072d
Merge branch 'master' into earlysetup
d-a-v Dec 3, 2018
c0043c2
Update ESP8266WiFiGeneric.h
devyte Dec 4, 2018
8b687b0
Update ESP8266WiFiGeneric.cpp
devyte Dec 4, 2018
85d244d
Update EarlyDisableWiFi.ino
devyte Dec 4, 2018
bef67d7
Update core_esp8266_main.cpp
devyte Dec 4, 2018
eb9f9c2
Update core_esp8266_main.cpp
devyte Dec 4, 2018
1925b0c
Update EarlyDisableWiFi.ino
devyte Dec 4, 2018
5f26799
Merge branch 'master' into earlysetup
devyte Dec 4, 2018
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
96 changes: 60 additions & 36 deletions cores/esp8266/AddrList.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
for (auto a: addrList)
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
a.iface().c_str(),
a.number(),
a.ifnumber(),
a.addr().isLegacy(),
a.addr().isV4(),
a.addr().isLocal(),
Expand Down Expand Up @@ -65,7 +65,7 @@
for (auto iface: addrList)
if ((configured = ( !iface.addr().isV4()
&& !iface.addr().isLocal()
&& iface.number() == STATION_IF)))
&& iface.ifnumber() == STATION_IF)))
break;
Serial.print('.');
delay(500);
Expand Down Expand Up @@ -94,26 +94,38 @@ namespace AddressListImplementation

struct netifWrapper
{
netifWrapper(netif * netif) : _netif(netif), _num(-1) {}
netifWrapper(const netifWrapper & o) : _netif(o._netif), _num(o._num) {}
netifWrapper (netif* netif) : _netif(netif), _num(-1) {}
netifWrapper (const netifWrapper& o) : _netif(o._netif), _num(o._num) {}

netifWrapper& operator=(const netifWrapper & o) {_netif = o._netif; _num = o._num; return *this;}
netifWrapper& operator= (const netifWrapper& o)
{
_netif = o._netif;
_num = o._num;
return *this;
}

bool equal (const netifWrapper & o)
bool equal(const netifWrapper& o)
{
return _netif == o._netif && (!_netif || _num == o._num);
}

// address properties
IPAddress addr () const { return ipFromNetifNum(); }
bool isLegacy () const { return _num == 0; }
bool isLocal () const { return addr().isLocal(); }
bool isV4 () const { return addr().isV4(); }
bool isV6 () const { return !addr().isV4(); }
String toString() const { return addr().toString(); }

bool isLegacy() const { return _num == 0; }
bool isLocal() const { return addr().isLocal(); }
IPAddress addr () const { return ipFromNetifNum(); }
IPAddress netmask () const { return _netif->netmask; }
IPAddress gw () const { return _netif->gw; }
String iface () const { return String(_netif->name[0]) + _netif->name[1]; }
const char* hostname () const { return _netif->hostname?: emptyString.c_str(); }
const char* mac () const { return (const char*)_netif->hwaddr; }
int number () const { return _netif->num; }
// related to legacy address (_num=0, ipv4)
IPAddress netmask () const { return _netif->netmask; }
IPAddress gw () const { return _netif->gw; }

// common to all addresses of this interface
String ifname () const { return String(_netif->name[0]) + _netif->name[1]; }
const char* ifhostname () const { return _netif->hostname?: emptyString.c_str(); }
const char* ifmac () const { return (const char*)_netif->hwaddr; }
int ifnumber () const { return _netif->num; }

const ip_addr_t* ipFromNetifNum () const
{
Expand All @@ -124,44 +136,57 @@ struct netifWrapper
#endif
}


netif * _netif;
// lwIP interface
netif* _netif;

// address index within interface
// 0: legacy address (IPv4)
// n>0: (_num-1) is IPv6 index for netif->ip6_addr[]
int _num;
};



class AddressListIterator
{
public:
AddressListIterator(const netifWrapper &o) : netIf(o) {}
AddressListIterator(netif * netif) : netIf(netif) {}
AddressListIterator (const netifWrapper& o) : netIf(o) {}
AddressListIterator (netif* netif) : netIf(netif)
{
// This constructor is called with lwIP's global netif_list, or
// nullptr. operator++() is designed to loop through _configured_
// addresses. That's why netIf's _num is initialized to -1 to allow
// returning the first usable address to AddressList::begin().
(void)operator++();
}

const netifWrapper& operator* () const {return netIf;}
const netifWrapper* operator->() const {return &netIf;}
const netifWrapper& operator* () const { return netIf; }
const netifWrapper* operator-> () const { return &netIf; }

bool operator==(AddressListIterator & o) {return netIf.equal(*o);}
bool operator!=(AddressListIterator & o) {return !netIf.equal(*o);}
bool operator== (AddressListIterator& o) { return netIf.equal(*o); }
bool operator!= (AddressListIterator& o) { return !netIf.equal(*o); }

AddressListIterator & operator= (const AddressListIterator& o) {netIf = o.netIf; return *this; }
AddressListIterator& operator= (const AddressListIterator& o) { netIf = o.netIf; return *this; }

AddressListIterator operator++(int)
AddressListIterator operator++ (int)
{
AddressListIterator ret = *this;
++(*this);
(void)operator++();
return ret;
}

AddressListIterator & operator++()
AddressListIterator& operator++ ()
{
while (netIf._netif)
{
if (++netIf._num == IF_NUM_ADDRESSES)
{
netIf = netifWrapper(netIf._netif->next); //num is inited to -1
// all addresses from current interface were iterated,
// switching to next interface
netIf = netifWrapper(netIf._netif->next);
continue;
}
if (!ip_addr_isany(netIf.ipFromNetifNum()))
// found an initialized address
break;
}
return *this;
Expand All @@ -171,24 +196,23 @@ class AddressListIterator
};



class AddressList
{
public:
using const_iterator = const AddressListIterator;

const_iterator begin() const {return const_iterator(netif_list);}
const_iterator end() const {return const_iterator(nullptr);}
const_iterator begin () const { return const_iterator(netif_list); }
const_iterator end () const { return const_iterator(nullptr); }

};

inline AddressList::const_iterator begin (const AddressList& a) { return a.begin(); }
inline AddressList::const_iterator end (const AddressList& a) { return a.end(); }

inline AddressList::const_iterator begin(const AddressList &a) {return a.begin();}
inline AddressList::const_iterator end(const AddressList &a) {return a.end();}

} //AddressListImplementation
} // AddressListImplementation

} //esp8266
} // esp8266

extern esp8266::AddressListImplementation::AddressList addrList;

Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
void attachInterrupt(uint8_t pin, void (*)(void), int mode);
void detachInterrupt(uint8_t pin);

void preinit(void);
void setup(void);
void loop(void);

Expand Down
12 changes: 10 additions & 2 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static uint32_t s_micros_at_task_start;

extern "C" {
extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER;
const char* core_release =
const char* core_release =
#ifdef ARDUINO_ESP8266_RELEASE
ARDUINO_ESP8266_RELEASE;
#else
Expand Down Expand Up @@ -243,18 +243,26 @@ extern "C" void ICACHE_RAM_ATTR app_entry (void)
return app_entry_custom();
}

extern "C" void preinit (void) __attribute__((weak));
extern "C" void preinit (void)
{
/* do nothing by default */
}

extern "C" void user_init(void) {
struct rst_info *rtc_info_ptr = system_get_rst_info();
memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo));

uart_div_modify(0, UART_CLK_FREQ / (115200));

init();
init(); // in core_esp8266_wiring.c

initVariant();

cont_init(g_pcont);

preinit(); // user redefinable

ets_task(loop_task,
LOOP_TASK_PRIORITY, s_loop_queue,
LOOP_QUEUE_SIZE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

// preinit() is called before system startup
// from nonos-sdk's user entry point user_init()

void preinit() {
// Global WiFi constructors are not called yet
// (global class instances like WiFi, Serial... are not yet initialized)..
ESP8266WiFiClass::preinit_wifi_off();
}

void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("sleeping 5s");

// during this period, a simple amp meter shows
// an average of 20mA with a Wemos D1 mini
// a DSO is needed to check #2111
delay(5000);

Serial.println("waking WiFi up, sleeping 5s");
WiFi.forceSleepWake();

// amp meter raises to 75mA
delay(5000);

Serial.println("connecting to AP " STASSID);
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);

for (bool configured = false; !configured;) {
for (auto addr : addrList)
if ((configured = !addr.isLocal() && addr.ifnumber() == STATION_IF)) {
Serial.printf("STA: IF='%s' hostname='%s' addr= %s\n",
addr.ifname().c_str(),
addr.ifhostname(),
addr.toString().c_str());
break;
}
Serial.print('.');
delay(500);
}

// amp meter cycles within 75-80 mA

}

void loop() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ void status(Print& out) {
out.println(F("(with 'telnet <addr> or 'nc -u <addr> 23')"));
for (auto a : addrList) {
out.printf("IF='%s' IPv6=%d local=%d hostname='%s' addr= %s",
a.iface().c_str(),
!a.addr().isV4(),
a.addr().isLocal(),
a.hostname(),
a.addr().toString().c_str());
a.ifname().c_str(),
a.isV6(),
a.isLocal(),
a.ifhostname(),
a.toString().c_str());

if (a.isLegacy()) {
out.printf(" / mask:%s / gw:%s",
Expand All @@ -79,6 +79,7 @@ void status(Print& out) {
}

out.println();

}

// lwIP's dns client will ask for IPv4 first (by default)
Expand All @@ -96,12 +97,14 @@ void setup() {
Serial.println();
Serial.println(ESP.getFullVersion());

Serial.printf("IPV6 is%s enabled\n", LWIP_IPV6 ? emptyString.c_str() : " NOT");

WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);

status(Serial);

#if 0
#if 0 // 0: legacy connecting loop - 1: wait for IPv6

// legacy loop (still valid with IPv4 only)

Expand Down
21 changes: 21 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,24 @@ void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *ca
}


void ESP8266WiFiGenericClass::preinit_wifi_off () {
// https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391
// WiFi.persistent(false);
// WiFi.mode(WIFI_OFF);
// WiFi.forceSleepBegin();

//WiFi.mode(WIFI_OFF) equivalent:
// datasheet:
// Set Wi-Fi working mode to Station mode, SoftAP
// or Station + SoftAP, and do not update flash
// (not persistent)
wifi_set_opmode_current(WIFI_OFF);

//WiFi.forceSleepBegin(/*default*/0) equivalent:
// sleep forever until wifi_fpm_do_wakeup() is called
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(0xFFFFFFF);

// use WiFi.forceSleepWake() to wake WiFi up
}
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class ESP8266WiFiGenericClass {
bool forceSleepBegin(uint32 sleepUs = 0);
bool forceSleepWake();

static void preinit_wifi_off ();

protected:
static bool _persistent;
static WiFiMode_t _forceSleepLastMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

ESP8266WiFiMulti WiFiMulti;

#define MANUAL_SIGNING 0
Expand Down Expand Up @@ -66,7 +71,7 @@ void setup() {
}

WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASS");
WiFiMulti.addAP(STASSID, STAPSK);

#if MANUAL_SIGNING
signPubKey = new BearSSL::PublicKey(pubkey);
Expand Down
4 changes: 2 additions & 2 deletions tests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ function install_ide()
debug_flags="-DDEBUG_ESP_PORT=Serial -DDEBUG_ESP_SSL -DDEBUG_ESP_TLS_MEM -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_HTTP_SERVER -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_UPDATE -DDEBUG_ESP_UPDATER -DDEBUG_ESP_OTA -DDEBUG_ESP_OOM"
fi
# Set custom warnings for all builds (i.e. could add -Wextra at some point)
echo "compiler.c.extra_flags=-Wall -Wextra -Werror $debug_flags" > esp8266/platform.local.txt
echo "compiler.cpp.extra_flags=-Wall -Wextra -Werror $debug_flags" >> esp8266/platform.local.txt
echo "compiler.c.extra_flags=-Wall -Wextra -Werror -DLWIP_IPV6=0 $debug_flags" > esp8266/platform.local.txt
echo "compiler.cpp.extra_flags=-Wall -Wextra -Werror -DLWIP_IPV6=0 $debug_flags" >> esp8266/platform.local.txt
echo -e "\n----platform.local.txt----"
cat esp8266/platform.local.txt
echo -e "\n----\n"
Expand Down
2 changes: 1 addition & 1 deletion tests/run_CI_locally.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if [ -d ${TMPCI} ]; then
echo ""
echo " -- updating CI directory in ${TMPCI} --"
echo ""
(cd ${TMPCI}; git checkout ${branch}; git pull)
(cd ${TMPCI}; git checkout master; git branch -D ${branch} || true; git checkout -b ${branch}; git pull origin ${branch})
else
echo ""
echo " -- installing CI directory in ${TMPCI} --"
Expand Down