af_inet_lwip.c 7.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-05-17     ChenYong     First version
 */

#include <rtthread.h>

#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include <lwip/api.h>
#include <lwip/init.h>

#ifdef SAL_USING_POSIX
#include <dfs_poll.h>
#endif

#include <sal.h>
23
#include <af_inet.h>
24 25 26 27 28 29 30 31 32

#if LWIP_VERSION < 0x2000000
#define SELWAIT_T int
#else
#ifndef SELWAIT_T
#define SELWAIT_T u8_t
#endif
#endif

33 34
#ifdef SAL_USING_LWIP

35
#ifdef SAL_USING_POSIX
36 37 38 39

#if LWIP_VERSION >= 0x20100ff
#include <lwip/priv/sockets_priv.h>
#else /* LWIP_VERSION < 0x20100ff */
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*
 * Re-define lwip socket
 *
 * NOTE: please make sure the definitions same in lwip::net_socket.c
 */
struct lwip_sock {
    /** sockets currently are built on netconns, each socket has one netconn */
    struct netconn *conn;
    /** data that was left from the previous read */
    void *lastdata;
    /** offset in the data that was left from the previous read */
    u16_t lastoffset;
    /** number of times data was received, set by event_callback(),
     tested by the receive and select functions */
    s16_t rcvevent;
    /** number of times data was ACKed (free send buffer), set by event_callback(),
     tested by select */
    u16_t sendevent;
    /** error happened for this socket, set by event_callback(), tested by select */
    u16_t errevent;
   /** last error that occurred on this socket */
#if LWIP_VERSION < 0x2000000
   int err;
#else
   u8_t err;
#endif
    /** counter of how many threads are waiting for this socket using select */
    SELWAIT_T select_waiting;

    rt_wqueue_t wait_head;
};
71
#endif /* LWIP_VERSION >= 0x20100ff */
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

extern struct lwip_sock *lwip_tryget_socket(int s);

static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
{
    int s;
    struct lwip_sock *sock;
    uint32_t event = 0;
    SYS_ARCH_DECL_PROTECT(lev);

    LWIP_UNUSED_ARG(len);

    /* Get socket */
    if (conn)
    {
        s = conn->socket;
        if (s < 0)
        {
            /* Data comes in right away after an accept, even though
             * the server task might not have created a new socket yet.
             * Just count down (or up) if that's the case and we
             * will use the data later. Note that only receive events
             * can happen before the new socket is set up. */
            SYS_ARCH_PROTECT(lev);
            if (conn->socket < 0)
            {
                if (evt == NETCONN_EVT_RCVPLUS)
                {
                    conn->socket--;
                }
                SYS_ARCH_UNPROTECT(lev);
                return;
            }
            s = conn->socket;
            SYS_ARCH_UNPROTECT(lev);
        }

        sock = lwip_tryget_socket(s);
        if (!sock)
        {
            return;
        }
    }
    else
    {
        return;
    }

    SYS_ARCH_PROTECT(lev);
    /* Set event as required */
    switch (evt)
    {
    case NETCONN_EVT_RCVPLUS:
        sock->rcvevent++;
        break;
    case NETCONN_EVT_RCVMINUS:
        sock->rcvevent--;
        break;
    case NETCONN_EVT_SENDPLUS:
        sock->sendevent = 1;
        break;
    case NETCONN_EVT_SENDMINUS:
        sock->sendevent = 0;
        break;
    case NETCONN_EVT_ERROR:
        sock->errevent = 1;
        break;
    default:
        LWIP_ASSERT("unknown event", 0);
        break;
    }

144 145 146 147 148
#if LWIP_VERSION >= 0x20100ff
    if ((void*)(sock->lastdata.pbuf) || (sock->rcvevent > 0))
#else
    if ((void*)(sock->lastdata) || (sock->rcvevent > 0))
#endif
149 150 151 152 153 154 155 156 157 158 159 160 161
        event |= POLLIN;
    if (sock->sendevent)
        event |= POLLOUT;
    if (sock->errevent)
        event |= POLLERR;

    SYS_ARCH_UNPROTECT(lev);

    if (event)
    {
        rt_wqueue_wakeup(&sock->wait_head, (void*) event);
    }
}
162
#endif /* SAL_USING_POSIX */
163 164 165

static int inet_socket(int domain, int type, int protocol)
{
166
#ifdef SAL_USING_POSIX
167 168 169 170 171 172 173 174 175 176 177 178 179 180
    int socket;

    socket = lwip_socket(domain, type, protocol);
    if (socket >= 0)
    {
        struct lwip_sock *lwsock;

        lwsock = lwip_tryget_socket(socket);
        lwsock->conn->callback = event_callback;

        rt_wqueue_init(&lwsock->wait_head);
    }

    return socket;
181 182 183
#else
    return lwip_socket(domain, type, protocol);
#endif /* SAL_USING_POSIX */
184 185 186 187
}

static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
{
188
#ifdef SAL_USING_POSIX
189 190 191 192 193 194 195 196 197 198 199 200 201
    int new_socket;

    new_socket = lwip_accept(socket, addr, addrlen);
    if (new_socket >= 0)
    {
        struct lwip_sock *lwsock;

        lwsock = lwip_tryget_socket(new_socket);

        rt_wqueue_init(&lwsock->wait_head);
    }

    return new_socket;
202 203 204
#else
    return lwip_accept(socket, addr, addrlen);
#endif /* SAL_USING_POSIX */
205 206 207 208 209 210 211 212 213 214 215 216
}

static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
{
#if LWIP_VERSION_MAJOR < 2U
    rt_kprintf("ERROR: Your lwIP version is not supported. Please using lwIP 2.0.0+.\n");
    RT_ASSERT(LWIP_VERSION_MAJOR >= 2U);
#endif

    return lwip_getsockname(socket, name, namelen);
}

217
#ifdef SAL_USING_POSIX
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req)
{
    int mask = 0;
    struct lwip_sock *sock;
    struct sal_socket *sal_sock;

    sal_sock = sal_get_socket((int) file->data);
    if(!sal_sock)
    {
        return -1;
    }

    sock = lwip_tryget_socket((int)sal_sock->user_data);
    if (sock != NULL)
    {
        rt_base_t level;

        rt_poll_add(&sock->wait_head, req);

        level = rt_hw_interrupt_disable();
238 239 240 241 242 243

#if LWIP_VERSION >= 0x20100ff
        if ((void*)(sock->lastdata.pbuf) || sock->rcvevent)
#else
        if ((void*)(sock->lastdata) || sock->rcvevent)
#endif
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        {
            mask |= POLLIN;
        }
        if (sock->sendevent)
        {
            mask |= POLLOUT;
        }
        if (sock->errevent)
        {
            mask |= POLLERR;
        }
        rt_hw_interrupt_enable(level);
    }

    return mask;
}
260
#endif
261

armink_ztl's avatar
armink_ztl 已提交
262 263
static const struct sal_socket_ops lwip_socket_ops =
{
264 265 266 267 268 269
    inet_socket,
    lwip_close,
    lwip_bind,
    lwip_listen,
    lwip_connect,
    inet_accept,
M
MurphyZhao 已提交
270 271
    (int (*)(int, const void *, size_t, int, const struct sockaddr *, socklen_t))lwip_sendto,
    (int (*)(int, void *, size_t, int, struct sockaddr *, socklen_t *))lwip_recvfrom,
272 273 274 275 276 277 278
    lwip_getsockopt,
    //TODO fix on 1.4.1
    lwip_setsockopt,
    lwip_shutdown,
    lwip_getpeername,
    inet_getsockname,
    lwip_ioctl,
279
#ifdef SAL_USING_POSIX
280
    inet_poll,
281
#endif
282 283 284 285 286 287 288 289
};

static int inet_create(struct sal_socket *socket, int type, int protocol)
{
    RT_ASSERT(socket);

    //TODO Check type & protocol

armink_ztl's avatar
armink_ztl 已提交
290
    socket->ops = &lwip_socket_ops;
291 292 293 294

    return 0;
}

armink_ztl's avatar
armink_ztl 已提交
295 296
static struct sal_proto_ops lwip_proto_ops =
{
297 298 299
    lwip_gethostbyname,
    lwip_gethostbyname_r,
    lwip_getaddrinfo,
armink_ztl's avatar
armink_ztl 已提交
300 301 302 303 304 305 306 307 308
    lwip_freeaddrinfo,
};

static const struct sal_proto_family lwip_inet_family =
{
    AF_INET,
    AF_INET,
    inet_create,
    &lwip_proto_ops,
309 310 311 312
};

int lwip_inet_init(void)
{
armink_ztl's avatar
armink_ztl 已提交
313
    sal_proto_family_register(&lwip_inet_family);
314 315 316 317

    return 0;
}
INIT_COMPONENT_EXPORT(lwip_inet_init);
318 319

#endif /* SAL_USING_LWIP */