Non-static member function as callback for espconn_regist_connectcb

alex323qp
Posts: 3
Joined: Fri Jul 14, 2017 5:56 am

Non-static member function as callback for espconn_regist_connectcb

Postby alex323qp » Fri Jul 14, 2017 6:37 am

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.

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Non-static member function as callback for espconn_regist_connectcb

Postby ESP_Sprite » Fri Jul 14, 2017 2:13 pm

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.

alex323qp
Posts: 3
Joined: Fri Jul 14, 2017 5:56 am

Re: Non-static member function as callback for espconn_regist_connectcb

Postby alex323qp » Sat Jul 15, 2017 12:05 am

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.

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

Re: Non-static member function as callback for espconn_regist_connectcb

Postby martinayotte » Sun Jul 16, 2017 3:17 pm

( 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
Last edited by martinayotte on Mon Jul 17, 2017 1:49 pm, edited 1 time in total.

alex323qp
Posts: 3
Joined: Fri Jul 14, 2017 5:56 am

Re: Non-static member function as callback for espconn_regist_connectcb

Postby alex323qp » Mon Jul 17, 2017 12:17 am

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.

Who is online

Users browsing this forum: Baidu [Spider] and 71 guests