cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 F417VGT RTOS LwIP - close task on client disconnection

Martin Vanco
Associate
Posted on July 10, 2018 at 15:37

Hi,

Ineed to create firmware on STM32F417VGT to act as an TCP server and to accept multiple connection at the same time.

Project is generated using CubeMX.

I have default task

void StartDefaultTask(void const * argument)
{
 /* init code for LWIP */
 MX_LWIP_Init();
 /* USER CODE BEGIN StartDefaultTask */
 struct netconn *conn, *in_conn;
 err_t err, accept_err;
 struct netbuf *buf;
 uint16_t len;
 char echoBuff[100];
 conn = netconn_new(NETCONN_TCP);
 if (conn != NULL)
 {
 err = netconn_bind(conn, IP_ADDR_ANY, 4001);
 if (err == ERR_OK)
 {
 netconn_listen(conn);
 while (1)
 {
 accept_err = netconn_accept(conn, &in_conn);
 if (accept_err == ERR_OK)
 {
 len = sprintf(echoBuff, 'netconn_accept - OK\r\n');
 HAL_UART_Transmit(&huart2, (uint8_t*)echoBuff, len, 1000);
 osThreadDef(netTask, StartNetTask, osPriorityIdle, 0, 128);
 netTaskHandle = osThreadCreate(osThread(netTask), (void*)in_conn);
 }
 }
 }
 }�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

After each netconn_accept a new task is created to handle certain connection.

void StartNetTask(void const * argument)
{
 /* USER CODE BEGIN StartNetTask */
struct netconn * nc = (struct netconn *)argument;
struct netbuf * nb;
char * buffer = pvPortMalloc(2048);
uint16_t len;
err_t err;
void *data;
char echoBuff[100];
sprintf(buffer, 'Hello from STM\r\n');
netconn_write(nc, buffer, strlen(buffer), NETCONN_COPY);
/* Infinite loop */
while (netconn_recv(nc, &nb) == ERR_OK)
{
 do
 {
 netbuf_data(nb, &data, &len);
 netconn_write(nc, data, len, NETCONN_COPY);
 }
 while (netbuf_next(nb) >= 0);
}
err = netconn_close(nc);
if (err == ERR_OK)
{
len = sprintf(echoBuff, 'netconn_close - OK\r\n');
HAL_UART_Transmit(&huart2, (uint8_t*)echoBuff, len, 1000);
}
err = netconn_delete(nc);
if (err == ERR_OK)
{
len = sprintf(echoBuff, 'netconn_delete - OK\r\n');
HAL_UART_Transmit(&huart2, (uint8_t*)echoBuff, len, 1000);
}
len = sprintf(echoBuff, 'Deleting current NetTask\r\n');
HAL_UART_Transmit(&huart2, (uint8_t*)echoBuff, len, 1000);
vTaskDelete(NULL);
 /* USER CODE END StartNetTask */
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

What I need to implement is situation when the client is disconnected from server. In such a case I want to delete task that was created for this connection. Code for StartNetTask is not working as I require.

Is there any way how to solve this task?

Thank you

martin

0 REPLIES 0