Page 1 of 1
Is an interrupt on client connection possible?
Posted: Mon Jan 08, 2018 3:33 am
by jpan127
Hopefully this isn't too stupid of a question, but I was wondering if I create a TCP socket as a server, listening for connections, is there an ISR for a client connection?
I would like to have a high priority task listening for connections, blocked on a semaphore. The semaphore would give from ISR when a client attempts to connect, unblocking this task and allowing it to service the client.
Thanks!
Re: Is an interrupt on client connection possible?
Posted: Mon Jan 08, 2018 6:08 am
by ESP_Angus
Hi jpan227,
What you probably want is "select", which is the standard BSD sockets way to wait for something to happen on a socket (or group of sockets).
Unfortunately all of the ESP-IDF examples which use select() use it with UDP sockets, not TCP. However if you google you should be able to find a lot of sample code (written for Linux or similar OSes) which can be easily adapted to work in ESP-IDF.
Angus
Re: Is an interrupt on client connection possible?
Posted: Tue Jan 09, 2018 5:14 am
by jpan127
Hi, thanks!
I will take a deeper look into how it works.
Off the top of your head, do you have an idea of how it can be used? It seems like I would still need to poll for select() right?
Re: Is an interrupt on client connection possible?
Posted: Tue Jan 09, 2018 11:23 pm
by ESP_Angus
jpan127 wrote:Hi, thanks!
I will take a deeper look into how it works.
Off the top of your head, do you have an idea of how it can be used?
Sure, here's some basic demo code. Copy it over main/main.c in
the idf-template repo to build it:
https://gist.github.com/projectgus/9e62 ... cad4a155ba
This is mostly cribbed from the the
Wikipedia BSD sockets server example. It will need some extending to (for example) support multiple concurrent connections, but BSD sockets programming is well documented.
It seems like I would still need to poll for select() right?
No. select() allows you to suspend the task until one of the sockets you are waiting on is ready (or until an optional timeout expires).
Re: Is an interrupt on client connection possible?
Posted: Wed Jan 10, 2018 4:23 am
by jpan127
Oh wow that is awesome. How does this interact with freertos? What if the task is the lowest priority? Does it work the same way as a semaphore?
Re: Is an interrupt on client connection possible?
Posted: Wed Jan 10, 2018 5:24 am
by ESP_Angus
jpan127 wrote:Oh wow that is awesome. How does this interact with freertos? What if the task is the lowest priority? Does it work the same way as a semaphore?
Yes, internally select() is in fact blocking on a semaphore. The calling task's priority will determine if it preempts other tasks or not, when data is available.
Note that TCP/IP stack handling also happens in a task, so you should orient any priorities relative to the TCP/IP task priority (ESP_TASK_TCPIP_PRIO ie 18). TCP/IP task can migrate between the two cores.
Re: Is an interrupt on client connection possible?
Posted: Thu Jan 11, 2018 4:39 am
by jpan127
Ah, thank you so much, you've been a lot of help!