struct espconn pointer in callbacks

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

struct espconn pointer in callbacks

Postby tve » Sat Jun 20, 2015 1:16 pm

The SDK 1.1.2 docs for espconn_recv_callback state:
void *arg : pointer corresponding structure espconn. This pointer may be
different in different callbacks, please don’t use this pointer directly to
distinguish one from another in multiple connections, use remote_ip and
remote_port in espconn instead.

How is this supposed to work?

Take the following situation:
- I get an espconn_connect_callback(arg), where arg points to an espconn struct, let's call this "espconn1"
- I then receive an espconn_recv_callback(arg), where arg points to a different espconn struct, call this "espconn2"
- In the receive callback I want to send a response, I thus need to call espconn_sent, do I use espconn1 or espconn2 for this?
- After the receive callback completes, I have a timer firing and I need to send some more data on this same connection, so I need to call espconn_sent again, do I now use espconn1 or esconn2 for this?

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: struct espconn pointer in callbacks

Postby tve » Tue Jun 23, 2015 3:22 am

Another issue: I see in the AT example provided in SDK 1.1.2 that the diconnect callback frees the espconn pointer that gets passed to it. This structure was allocated by the SDK so it seems really, really weird for the user code to free it. Is this code correct or "just happens to work"?
See line 85 of examples/at/user/at_upgrade.c

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: struct espconn pointer in callbacks

Postby ESP_Faye » Thu Jun 25, 2015 10:30 am

Hi,

Sorry that at_upgrade.c may lead to some misunderstanding.

Code: Select all

static void ICACHE_FLASH_ATTR
at_upDate_discon_cb(void *arg)
{
  struct espconn *pespconn = (struct espconn *)arg;
   ....
}


This pointer is same as the one zalloced in "at_exeCmdCiupdate"

Code: Select all

void ICACHE_FLASH_ATTR
at_exeCmdCiupdate(uint8_t id)
{
  pespconn = (struct espconn *)os_zalloc(sizeof(struct espconn));
  ......
}


These two will be the same pointer. It is malloc by AT app, and free by AT app.

So sorry for these may bother you.

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: struct espconn pointer in callbacks

Postby tve » Thu Jun 25, 2015 1:04 pm

Thanks, I'll look more closely into the AT example to understand.

Could you comment on my first post?

Her Mary
Posts: 537
Joined: Mon Oct 27, 2014 11:09 am

Re: struct espconn pointer in callbacks

Postby Her Mary » Thu Jun 25, 2015 1:44 pm

- I get an espconn_connect_callback(arg), where arg points to an espconn struct, let's call this "espconn1"
- I then receive an espconn_recv_callback(arg), where arg points to a different espconn struct, call this "espconn2"


It seems that "espconn1" and "espconn2" are the same pointer, aren't them ? :?

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: struct espconn pointer in callbacks

Postby tve » Thu Jun 25, 2015 2:47 pm

It seems that "espconn1" and "espconn2" are the same pointer, aren't them ?


That's my whole point: they are not guaranteed to be! And experimentally I can confirm that this is indeed the case. It's particularly bad when you get a disconnect callback. I see some pretty systematic cases where the pointer you get is different from the one I previously got for the same connection and the structure that the old pointer refers to has been reused for a new connection!

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: struct espconn pointer in callbacks

Postby ESP_Faye » Fri Jun 26, 2015 3:58 pm

Hi,

espconn pointer of connected callback, recv callback , sent callback would be the same, they are provided by SDK espconn.

espconn pointer of disconnected callback, reconnect callback are the same, they are malloc by user app.

Sorry that may confuse you..

Who is online

Users browsing this forum: No registered users and 98 guests