44#include "FreeRTOS_IP.h"
45#include "FreeRTOS_Sockets.h"
48#include "FreeRTOSTCPSocket.hxx"
50static SocketSet_t socket_set = NULL;
52static QueueHandle_t close_queue = NULL;
60 eIPCallbackEvent_t eNetworkEvent);
86 int sel_result, result, tries;
87 struct freertos_sockaddr address;
89 socket_set = FreeRTOS_CreateSocketSet();
92 close_queue = xQueueCreate(10,
sizeof(
Socket_t));
96 wakeup = FREERTOS_INVALID_SOCKET;
97 while ((++tries <= 5) && (
wakeup == FREERTOS_INVALID_SOCKET))
100 FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP);
101 if (
wakeup == FREERTOS_INVALID_SOCKET)
110 address.sin_port = FreeRTOS_htons(8000);
111 address.sin_addr = FreeRTOS_htonl(0);
112 result = FreeRTOS_bind(
wakeup, &address,
sizeof(address));
115 FreeRTOS_FD_SET(
wakeup, socket_set, eSELECT_READ | eSELECT_INTR);
119 const TickType_t timeout = pdMS_TO_TICKS(30000);
121 sel_result = FreeRTOS_select(socket_set, timeout);
128 if (sel_result & eSELECT_INTR)
133 while (xQueueReceive(close_queue, &sd, 0) == pdTRUE)
135 portENTER_CRITICAL();
136 FreeRTOS_FD_CLR(sd, socket_set,
137 eSELECT_READ | eSELECT_WRITE | eSELECT_EXCEPT);
141 FreeRTOS_shutdown(sd, FREERTOS_SHUT_RDWR);
142 FreeRTOS_closesocket(sd);
149 for (
int i = MAX_SOCKETS - 1; i >= 0; --i)
172 evt = FreeRTOS_FD_ISSET(sd, socket_set);
178 if (evt & eSELECT_READ)
186 wakeup, data,
sizeof(data), 0,
nullptr,
nullptr);
191 FreeRTOS_FD_CLR(sd, socket_set, eSELECT_READ);
197 if (evt & eSELECT_WRITE)
201 FreeRTOS_FD_CLR(sd, socket_set, eSELECT_WRITE);
205 FreeRTOS_FD_CLR(sd, socket_set, eSELECT_WRITE);
209 if (evt & eSELECT_EXCEPT)
212 FreeRTOS_FD_CLR(sd, socket_set, eSELECT_EXCEPT);
227#if ipconfigSUPPORT_SIGNALS == 1
232 xQueueSend(close_queue, &data, 0);
234 FreeRTOS_SignalSocket(
wakeup);
236#error "FreeRTOSIPConfig.h must include ipconfigSUPPORT_SIGNALS = 1"
245 FreeRTOS_FD_SET(
socket, socket_set, eSELECT_READ);
254 FreeRTOS_FD_SET(
socket, socket_set, eSELECT_WRITE);
void vApplicationIPNetworkEventHook(eIPCallbackEvent_t eNetworkEvent)
Callback from FreeRTOS network event handler.
bool network_layer_preinit(void)
Called before the TCP stack is initialized in FreeRTOS.
void * Socket_t
local declaraction of Socket_t to avoid dependency on FreeRTOSTCP headers
static OSEvent wakeup
event used to wakeup select calls
int socket(int domain, int type, int protocol)
Create an unbound socket in a communications domain.
bool writeActive
indicates our "best guess" at current socket's write active status
static FreeRTOSTCPSocket * get_sd_by_index(int inx)
Get the socket descriptor assoicated with socket index.
static void remove_instance_from_sd(Socket_t sd)
Remove the FreeRTOSTCP instance from the active FreeRTOSTCP list.
bool readActive
indicates our "best guess" at current socket's read active status
static FreeRTOSTCPSocket * get_instance_from_sd(Socket_t sd)
Get the FreeRTOSTCP instance given a specific socket descriptor.
Socket_t sd
FreeRTOS socket descriptor.
Socket_t wakeup
signal socket to wakeup select()
FreeRTOSTCP()
Constructor.
void net_task()
Thread that will manage the net connection inside object context.
void start()
Startup the networking processes.
void fd_set_read(Socket_t socket)
Add socket to the read fd set.
static void * net_task_entry(void *context)
Thread that will manage the network connection.
void select_wakeup(Socket_t data=nullptr)
Asynchronously wakeup the select call.
void fd_set_write(Socket_t socket)
Add socket to the write fd set.
SelectInfo selInfoRd
select wakeup metadata for read active
SelectInfo selInfoWr
select wakeup metadata for write active
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
int os_thread_create(os_thread_t *thread, const char *name, int priority, size_t stack_size, void *(*start_routine)(void *), void *arg)
Create a thread.
static void select_wakeup(SelectInfo *info)
Wakeup the list of clients needing woken.