Search code examples
c++exceptionarduino-esp8266

Binder function for encapsulated ESP8266 Web Server


I'm trying to encapsulate a WebServer hosted on ESP8266 that function as Access Point as well. I'm using a binder to be able to reference the methods in my encapsulated class for the server targets, eg: if a client enters the root address for the website it runs a response code written in one of the methods in the encapsulated class. I've used this binder in many other occasions but it seems that the error that I'm getting points out to this binder that I'm sure it's correct. I'm running with the latest Arduino ESP8266 SDK release 2.4.0-rc2, the full code is here, the relevant points are below:

Instance of the binder variable:

...
bind_member<WebServerAPClass, &WebServerAPClass::HandleRoot> handleRoot(this);
...
server->on("/", handleRoot);
...

The binder:

template<class T, void(T::*PTR)()>
struct bind_member
{
    typedef void(*fn_type)();
    explicit bind_member(T* _ptr)
    {
        ptr = _ptr;
    }
    static void func(void)
    {
        (ptr->*PTR)();
    }
    operator fn_type()
    {
        return &func;
    }
private:
    static T*  ptr;
};
template<class T, void(T::*PTR)()>
T* bind_member<T, PTR>::ptr = NULL;

The output:

Setting wifi AP mode ... Setting soft-AP configuration ... [APConfig] local_ip: 192.168.88.1 gateway: 192.168.88.1 subnet: 255.255.255.0
[APConfig] DHCP IP start: 192.168.88.100
[APConfig] DHCP IP end: 192.168.88.200
Ready
Setting soft-AP ... [AP] softap config unchanged
Ready
AP IP address: 192.168.88.1
...[stack]...
ets Jan  8 2013,rst cause:2, boot mode:(3,6)

The stack decoded:

Exception 28: LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads
Decoding 17 results
0x40206e65: std::function ::function(std::function  const&) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2380
0x40206e76: std::function ::function(std::function  const&) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2439
0x4020403e: ESP8266WebServer::on(String const&, HTTPMethod, std::function ) at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\libraries\ESP8266WebServer\src/ESP8266WebServer.cpp line 214
0x40206a70: operator new(unsigned int) at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\cores\esp8266/abi.cpp line 84
0x40201ff0: std::_Function_base::_Base_manager   >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 1931
0x402021cc: bind_member ::func() at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\libraries\RTCTimer/SimpleBinders.h line 14
:  (inlined by) std::_Function_handler   >::_M_invoke(std::_Any_data const&) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2071
0x40206e76: std::function ::function(std::function  const&) at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\libraries\ESP8266WebServer\src/ESP8266WebServer.cpp line 214
0x40204085: ~function at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\libraries\ESP8266WebServer\src/ESP8266WebServer.cpp line 214
:  (inlined by) ESP8266WebServer::on(String const&, std::function ) at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\libraries\ESP8266WebServer\src/ESP8266WebServer.cpp line 233
0x40201ff0: std::_Function_base::_Base_manager   >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 1931
0x402021cc: bind_member ::func() at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\libraries\RTCTimer/SimpleBinders.h line 14
:  (inlined by) std::_Function_handler   >::_M_invoke(std::_Any_data const&) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2071
0x4020215f: ~_Function_base at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2029
:  (inlined by) ~function at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2174
:  (inlined by) WebServerAPClass::Init() at C:\Users\Rinaldi\AppData\Local\Temp\arduino_build_415025\sketch/WebServerAP.cpp line 27
0x40201ff0: std::_Function_base::_Base_manager   >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 1931
0x402021cc: bind_member ::func() at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\libraries\RTCTimer/SimpleBinders.h line 14
:  (inlined by) std::_Function_handler   >::_M_invoke(std::_Any_data const&) at c:\users\rinaldi\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2071
0x40205c00: HardwareSerial::flush() at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\cores\esp8266/HardwareSerial.cpp line 180
0x40201f91: setup at C:\Users\Rinaldi\Documents\Arduino\MindTheGate/MindTheGate.ino line 11
0x40206ba3: loop_wrapper at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\cores\esp8266/core_esp8266_main.cpp line 57
0x40100a20: cont_norm at C:\Users\Rinaldi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc2\cores\esp8266/cont.S line 109

Solution

  • The problem wasn't with the binder, I had forgotten to instantiate ESP8266WebServer. See project on github to full example.