Page 1 of 1

Non-static member function as callback for espconn_regist_connectcb

Posted: Fri Jul 14, 2017 6:37 am
by alex323qp
Hi guys, I'm new to the ESP32 world and just started working with the NONOS-SDK.

I'm writing my own libraries in c++ and got stuck trying to define the callbacks for a few network events. I've been trying to find ways around it but no luck so far.

Normally in C you would do:

Code: Select all

void onConnected(void *args){...}

void connect(){
	struct espconn *conn = (struct espconn *) os_zalloc(sizeof(struct espconn));
	// ...
	espconn_regist_connectcb(conn, (espconn_connect_callback) onConnected);
}
But since my callback is a member function I'm obviously getting a compilation error (error: converting from 'void (FooClass::*)(void*)' to 'espconn_connect_callback {aka void (*)(void*)}')

Is there any c++ expert out there that could throw some light on this? I basically need the following (or similar) to work:

Code: Select all

void FooClass::onConnected(void *args){...}

void FooClass::connect(){
	struct espconn *conn = (struct espconn *) os_zalloc(sizeof(struct espconn));
	// ...
	espconn_regist_connectcb(conn, (espconn_connect_callback) onConnected);
}
Any help would be greatly appreciated!

A.

Re: Non-static member function as callback for espconn_regist_connectcb

Posted: Fri Jul 14, 2017 2:13 pm
by ESP_Sprite
Erm... are you sure you didn't accidentally grab one of the ESP8266 SDKs? They are not compatible with the ESP32. The ESP32 only has ESP-IDF as an SDK.

Re: Non-static member function as callback for espconn_regist_connectcb

Posted: Sat Jul 15, 2017 12:05 am
by alex323qp
oh dear... sorry guys, wrong forum... I even wrote 'new to the ESP32 world..', I guess no more coffees late at night for me.

Cheers!

A.

Re: Non-static member function as callback for espconn_regist_connectcb

Posted: Sun Jul 16, 2017 3:17 pm
by martinayotte
( strange thing here : I've posted an answer here yesterday, and today, my post has been deleted ... :evil: )
( Ok ! I find why : it was deleted, but posted on esp8266 forum, where OP posted the same question there_

Because of the nature of C++ object, the signature of non-static method isn't the same as the signature of plain C callback, this is due to the fact that non-static method require a "this" as argument. Therefore the compiler error ...
To workaround this, the method needs to be static. But this method will not be able to access object data member, except if a global pointer is kept as a singleton.

https://stackoverflow.com/questions/240 ... d-in-class

Re: Non-static member function as callback for espconn_regist_connectcb

Posted: Mon Jul 17, 2017 12:17 am
by alex323qp
Hi @martinayotte, @ESP_Sprite made me realize this wasn't the right place for this question, so I re-posted it on a different forum. I think you replied there. Thanks heaps for your help, I'll investigate and let you know :-D.

A.