Milos RTOS v0.3.4a
Real Time Operating System
Functions
Collaboration diagram for Functions:

Functions

__PSOCKET __socketCreate (u8 type, u16 port)
 Creates a socket.
i32 __socketDestroy (__PSOCKET socket)
 Destroys a socket.
__PSOCKET __socketFind (u8 proto, u16 local_port, u16 remote_port, u32 remote_addr)
 Finds a socket.
i32 __socketClose (__PSOCKET socket)
 Closes a socket.
i32 __socketSendTo (__PSOCKET socket, u32 addr, u16 port, __PVOID buf, u32 qty)
 Sends data to a specific remote end. Only for UDP sockets.
i32 __socketRecvFrom (__PSOCKET socket, u32 *addr, u16 *port, __PVOID buf, u32 qty)
 Reads data from a socket retrieving the address and port of the sender.
i32 __socketListen (__PSOCKET socket, u16 port)
 Waits for incoming connections.
i32 __socketConnect (__PSOCKET socket, u32 addr, u16 port)
 Tries to establish a connection with a remote host.
i32 __socketBind (__PSOCKET socket, u32 addr)
 Bind a socket to a port.
i32 __socketRecv (__PSOCKET socket, __PVOID buf, u32 qty)
 Reads data from a TCP socket.
i32 __socketSend (__PSOCKET socket, __PVOID buf, u32 qty)
 Sends data through a TCP socket.
__PSOCKET __socketAccept (__PSOCKET socket, u32 *addr)
 Accepts an incoming connection.
i32 __socketSetSockOpt (__PSOCKET socket, i16 optname, __PVOID optval, u32 optlen)
 Sets socket options.

Function Documentation

__PSOCKET __socketCreate ( u8  type,
u16  port 
)

Creates a socket.

Call this function to create a socket. The returned pointer can be used with the rest of socket or device functions.

Parameters:
typeType of socket. One of the following values.
  • __SOCK_TYPE_TCP For TCP sockets.
  • __SOCK_TYPE_UDP For UDP sockets.
portPort of the socket. Can be zero, in this case the function will choose an appropriate port.
Returns:
A pointer to a socket on success, otherwise __NULL.

Definition at line 413 of file socket.c.

{
    __PSOCKET sock;
    __PSOCKET_PDB pdb;
    __EVENT *rxev;
    __EVENT *txev;
    u8* buffer;

    if (type != __SOCK_TYPE_TCP && type != __SOCK_TYPE_UDP) return __NULL;

    /* Check if the port is already opened by another socket */
    pdb = __sock_list;
    while (pdb)
    {
        if (pdb->local_port == port) return __NULL;
        pdb = pdb->next;
    }

    /* Dynamically allocate a buffer for our socket, TX-RX event, and PDB */
    buffer = __heapAllocZero(sizeof(__SOCKET) +
                             sizeof(__EVENT) + sizeof(__EVENT) +
                             sizeof(__SOCKET_PDB));

    if (!buffer) return __NULL;

    /* Pointers */
    sock = (__PSOCKET) buffer;
    pdb  = (__PSOCKET_PDB) ((u8*) sock + sizeof(__SOCKET));
    rxev = (__EVENT*) ((u8*) pdb + sizeof(__SOCKET_PDB));
    txev = (__EVENT*) ((u8*) rxev + sizeof(__EVENT));

    /* Assign events */
    sock->dv_rxev = rxev;
    sock->dv_txev = txev;

    /* Assign device functions */
    sock->dv_init = __sockInit;
    sock->dv_deinit = __sockDeinit;
    sock->dv_flush = __sockFlush;
    sock->dv_open = __sockOpen;
    sock->dv_read = __sockRead;
    sock->dv_write = __sockWrite;
    sock->dv_ioctl = __sockIOCtl;
    sock->dv_size = __sockSize;
    sock->dv_close = __sockClose;

    /* Configure PDB */
    pdb->type = type;
    pdb->sock = sock;
    pdb->rxsize = 10;   /* TODO Configurable RX buffer size */

    if (port == 0)
    {
        port = __sock_last_port;
        __sock_last_port++;
    }

    pdb->local_port = __htons(port);
    if (type == __SOCK_TYPE_UDP) pdb->rxtmo = 0;

    sock->dv_pdb = pdb;

    __sock_count++;

    /* Name */
    __strFmt(sock->dv_name, "sock%lu", __sock_count);

    /* Call initialize */
    if (__deviceInit(sock, __NULL, 0) != __DEV_OK)
    {
        __heapFree(buffer);
        return __NULL;
    }

    if (pdb->type == __SOCK_TYPE_UDP)
    {
        __deviceOpen(sock, 0);
    }

    __systemDisableScheduler();
    pdb->next = __sock_list;
    __sock_list = pdb;
    __systemEnableScheduler();

    /* Add our socket to the available devices list */
    __deviceAdd(sock, 1);

    return sock;
}

Here is the call graph for this function:

Destroys a socket.

The unsent/unread that will be deallocated and lost.

Parameters:
socketPointer to a socket.
Returns:
0 on success, otherwise non-zero.

Definition at line 512 of file socket.c.

{
    __PSOCKET_PDB list;
    __PSOCKET_PDB pdb = socket->dv_pdb;

    /* Remove the socket from the devices list */
    __deviceRemove(socket);

    /* Call de-initialize */
    __deviceDeinit(socket);

    /* Remove the socket from the linked list */
    __systemDisableScheduler();

    list = __sock_list;

    if (list == pdb)
    {
        __sock_list = pdb->next;
    } else {
        while (list)
        {
            if (list->next == pdb)
            {
                list->next = pdb->next;
                break;
            }
            list = list->next;
        }
    }

    __systemEnableScheduler();

    if (__eventGetWaitingThreads(socket->dv_rxev))
    {
        __eventAbort(socket->dv_rxev);
    }

    if (__eventGetWaitingThreads(socket->dv_txev))
    {
        __eventAbort(socket->dv_txev);
    }

    /* Freeing the socket will also free the PDB and the events */
    __heapFree(socket);

    return 0;
}

Here is the call graph for this function:

__PSOCKET __socketFind ( u8  proto,
u16  local_port,
u16  remote_port,
u32  remote_addr 
)

Finds a socket.

Parameters:
protoType of protocol.
  • __PROTO_UDP For UDP.
  • __PROTO_TCP For TPC.
local_portLocal socket port.
remote_portRemote socket port.
remote_addrRemote address.
Returns:
A pointer to the socket, otherwise __NULL.

Definition at line 573 of file socket.c.

{
    __PSOCKET_PDB pdb = __sock_list;

    if (proto == __PROTO_UDP)
    {
        while (pdb)
        {
            if (pdb->local_port == local_port) return pdb->sock;
            pdb = pdb->next;
        }
    } else if (proto == __PROTO_TCP)
    {
        /* TODO find TCP socket */
    }

    return __NULL;
}

Closes a socket.

Parameters:
socketPointer to a socket.
Returns:
0 on success, otherwise non-zero.

Definition at line 599 of file socket.c.

{
    return __deviceClose(socket);
}

Here is the call graph for this function:

i32 __socketSendTo ( __PSOCKET  socket,
u32  addr,
u16  port,
__PVOID  buf,
u32  qty 
)

Sends data to a specific remote end. Only for UDP sockets.

Parameters:
socketPointer to a socket created with __socketCreate().
addrRemote IP address (in network byte order).
portRemote port (in host byte order).
bufPointer to a buffer containing the data to be sent.
qtyQuantity of data to be sent, in bytes.
Returns:
On success, the quantity of data sent. Otherwise an error code.

Definition at line 615 of file socket.c.

{
    __PSOCKET_PDB pd = socket->dv_pdb;

    pd->tx_dst_addr.addr_type = __NET_ADDR_IP;
    pd->tx_dst_addr.type.ip = addr;
    pd->tx_dst_port = __htons(port);

    return __deviceWrite(socket, buf, qty);
}

Here is the call graph for this function:

i32 __socketRecvFrom ( __PSOCKET  socket,
u32 *  addr,
u16 *  port,
__PVOID  buf,
u32  qty 
)

Reads data from a socket retrieving the address and port of the sender.

Parameters:
socketPointer to a socket created with __socketCreate().
addrPointer to a 32-bit value to receive the remote address.
portPointer to a 16-bit value to receive the remote port.
bufPointer to a buffer containing the data to be sent.
qtyQuantity of data to be read.
Returns:
The quantity of data read, in bytes.

Definition at line 637 of file socket.c.

{
    i32 ret;
    __PSOCKET_PDB pd = socket->dv_pdb;

    ret = __deviceRead(socket, buf, qty);
    if (ret > 0)
    {
        if (addr) *addr = __ntohl(pd->rx_src_addr.type.ip);
        if (port) *port = __ntohs(pd->rx_src_port);
    }

    return ret;
}

Here is the call graph for this function:

i32 __socketListen ( __PSOCKET  socket,
u16  port 
)

Waits for incoming connections.

For TCP sockets. TODO.

Definition at line 657 of file socket.c.

{
    /* TODO TCP listen */
    return 0;
}
i32 __socketConnect ( __PSOCKET  socket,
u32  addr,
u16  port 
)

Tries to establish a connection with a remote host.

For TCP sockets. TODO.

Definition at line 668 of file socket.c.

{
    /* TODO TCP connect */
    return 0;
}
i32 __socketBind ( __PSOCKET  socket,
u32  addr 
)

Bind a socket to a port.

For TCP sockets. TODO.

Definition at line 679 of file socket.c.

{
    /* TODO TCP bind */
    return 0;
}
i32 __socketRecv ( __PSOCKET  socket,
__PVOID  buf,
u32  qty 
)

Reads data from a TCP socket.

For TCP sockets. TODO.

Definition at line 690 of file socket.c.

{
    /* TODO TCP receive */
    return 0;
}
i32 __socketSend ( __PSOCKET  socket,
__PVOID  buf,
u32  qty 
)

Sends data through a TCP socket.

For TCP sockets. TODO.

Definition at line 701 of file socket.c.

{
    /* TODO TCP send */
    return 0;
}
__PSOCKET __socketAccept ( __PSOCKET  socket,
u32 *  addr 
)

Accepts an incoming connection.

For TCP sockets. TODO.

Definition at line 712 of file socket.c.

{
    /* TODO TCP accept */
    return __NULL;
}
i32 __socketSetSockOpt ( __PSOCKET  socket,
i16  optname,
__PVOID  optval,
u32  optlen 
)

Sets socket options.

Parameters:
socketPointer to a socket created with __socketCreate().
optnameOption, one of the following values:
  • __SOCK_SO_RCVBUF Sets the size of the RX buffer.
  • __SOCK_SO_SNDTIMEO Sets the transmission timeout.
  • __SOCK_SO_RCVTIMEO Sets the reception timeout.
optvalPointer to the option values. Depends on the optname parameter value.
optlenLength of the optval parameter value.
Returns:
0 on success, otherwise non-zero.

Definition at line 731 of file socket.c.

{
    if (!socket) return -1;

    switch (optname)
    {
        case __SOCK_SO_RCVBUF:
            if (!optval || optlen != 4) return -1;
            return __deviceIOCtl(socket, __IOCTL_SET_RXBUF_SIZE, (u32) *((u32*) optval), __NULL, 0);

        case __SOCK_SO_SNDTIMEO:
            if (!optval || optlen != 4) return -1;
            return __deviceIOCtl(socket, __IOCTL_SETTXTIMEOUT, (u16) *((u32*) optval), __NULL, 0);

        case __SOCK_SO_RCVTIMEO:
            if (!optval || optlen != 4) return -1;
            return __deviceIOCtl(socket, __IOCTL_SETRXTIMEOUT, (u16) *((u32*) optval), __NULL, 0);

    }

    return -1;
}

Here is the call graph for this function:

 All Data Structures Files Functions Variables Typedefs Defines