Arm Mbed OS support forum

Socket sigio usage

Board: NUCLEO-H743ZI2.

I want to use sigio for tracking TCPSocket state by nsapi_error codes.

I’m not familiar with sigio and how to use it with mbed.
I read #3620 and #11025.

I try to do someting like this:

Code
#include "mbed.h"
#include "EthernetInterface.h"
#include "nsapi_types.h"

static constexpr char IP_ADDR[] = "192.168.088.254";
static constexpr uint16_t PORT = 3333;

EthernetInterface net;
SocketAddress a;
TCPSocket socket;
nsapi_size_or_error_t socket_response;
Mutex print_mutex;
char socket_buffer[250] = {};

void safe_print(const char *msg /*format*/, ...);
void status_callback(nsapi_event_t status, intptr_t param);
void tcp_handler();

int main()
{
    nsapi_error_t socket_connect, socket_open;

    socket.sigio(&tcp_handler);

    net.add_event_listener(&status_callback);
    safe_print("connecting...\r\n");
    net.connect();
    safe_print("Connecting started...\r\n");
    net.get_ip_address(&a);
    printf("IP address: %s\n", a.get_ip_address() ? a.get_ip_address() : "None");
    socket_open = socket.open(&net);
    safe_print("socket_open: %d\n",socket_open);
    net.gethostbyname(IP_ADDR, &a);
    a.set_port(PORT);
    socket_connect = socket.connect(a);
    safe_print("socket_connect: %d\n\r", socket_connect);

    while(true)
    {

    }

    socket.close();
    net.disconnect();
    return 0;
}

void safe_print(const char *msg /*format*/, ...)
{
    print_mutex.lock();
    printf(msg);
    print_mutex.unlock();
}

void status_callback(nsapi_event_t status, intptr_t param)
{
    safe_print("Connection status changed!\r\n");
    switch (param) {
        case NSAPI_STATUS_LOCAL_UP:
            safe_print("Local IP address set!\r\n");
            break;
        case NSAPI_STATUS_GLOBAL_UP:
            safe_print("Global IP address set!\r\n");
            break;
        case NSAPI_STATUS_DISCONNECTED:
            safe_print("No connection to network!\r\n");
            break;
        case NSAPI_STATUS_CONNECTING:
            safe_print("Connecting to network!\r\n");
            break;
        default:
            safe_print("Not supported\r\n");
            break;
    }
}

void tcp_handler()
{
    printf("sigio called\n");
    socket_response = socket.recv(socket_buffer, sizeof socket_buffer);
    printf("%d\n",socket_response);
    if (socket_response == NSAPI_ERROR_WOULD_BLOCK) {
        safe_print("Would_Block\n\r");
    } else if (socket_response < 0) {
        safe_print("%d\n\r", socket_response);
    } else if (socket_response == 0) {
        safe_print("=0\n\r");
    }
}

tcp_handler called after socket_open and then all freezes.
Here my serial output:

connecting...
Connection status changed!
Connecting to network!
Connection status changed!
Global IP address set!
Connecting started...
IP address: 192.168.88.253
socket_open: 0
sigio called

Can anyone explain me how to use sigio correctly? Or using sigio in my case is wrong?