drv_win_eth.c 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 71 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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-05-26     xiangxistu   first version
 */

#include <rtthread.h>
#include <sal_low_lvl.h>
#include <sal_socket.h>
#include <sal_netdb.h>
#include <netdev_ipaddr.h>
#include <netdev.h>
#include "rtt_winsock.h"

static win_netdev_ping(struct netdev* netif, const char* host, size_t data_len,
    uint32_t timeout, struct netdev_ping_resp* ping_resp)
{
    return 0;
}

const struct netdev_ops win_netdev_ops =
{
    RT_NULL,
    RT_NULL,
    RT_NULL,
    RT_NULL,
    RT_NULL,
#ifdef RT_USING_FINSH
    win_netdev_ping,
    RT_NULL,
#endif /* RT_USING_FINSH */
    RT_NULL,
};

 static const struct sal_socket_ops windows_socket_ops =
 {
     win_socket,
     win_closesocket,
     win_bind,
     win_listen,
     win_connect,
     win_accept,
     win_sendto,
     win_recvfrom,
     win_getsockopt,
     win_setsockopt,
     win_shutdown,
     win_getpeername,
     win_getsockname,
     win_ioctlsocket,
 #ifdef SAL_USING_POSIX
     inet_poll,
 #else
     RT_NULL,
 #endif
 };

 static const struct sal_netdb_ops windows_netdb_ops =
 {
     win_gethostbyname,
     RT_NULL,
     win_getaddrinfo,
     win_freeaddrinfo,
 };

 static const struct sal_proto_family windows_inet_family =
 {
     AF_INET,
     AF_INET6,
     &windows_socket_ops,
     &windows_netdb_ops,
 };

 /* Set lwIP network interface device protocol family information */
 int sal_win_netdev_set_pf_info(struct netdev* netdev)
 {
     RT_ASSERT(netdev);

     netdev->sal_user_data = (void*)&windows_inet_family;
     return 0;
 }

static int win_netdev_add(void)
{
#define ETHERNET_MTU        1500
#define HWADDR_LEN          6

    rt_err_t result = RT_EOK;
    struct netdev* netdev = RT_NULL;

    char name[RT_NAME_MAX] = {0};

    netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
    if (netdev == RT_NULL)
    {
        return -RT_EEMPTY;
    }

    sal_win_netdev_set_pf_info(netdev);

    rt_strncpy(name, "win_e0", RT_NAME_MAX);
    result = netdev_register(netdev, name, RT_NULL);

    netdev->flags = NETDEV_FLAG_UP | NETDEV_FLAG_LINK_UP | NETDEV_FLAG_INTERNET_UP;
    netdev->mtu = ETHERNET_MTU;
    netdev->ops = &win_netdev_ops;
    netdev->hwaddr_len = HWADDR_LEN;

    return result;
}
INIT_ENV_EXPORT(win_netdev_add);