1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-05-26     xiangxistu   first version
9  */
10 
11 #include <rtthread.h>
12 #include <sal_low_lvl.h>
13 #include <sal_socket.h>
14 #include <sal_netdb.h>
15 #include <netdev_ipaddr.h>
16 #include <netdev.h>
17 #include "rtt_winsock.h"
18 
win_netdev_ping(struct netdev * netif,const char * host,size_t data_len,uint32_t timeout,struct netdev_ping_resp * ping_resp)19 static win_netdev_ping(struct netdev* netif, const char* host, size_t data_len,
20     uint32_t timeout, struct netdev_ping_resp* ping_resp)
21 {
22     return 0;
23 }
24 
25 const struct netdev_ops win_netdev_ops =
26 {
27     RT_NULL,
28     RT_NULL,
29     RT_NULL,
30     RT_NULL,
31     RT_NULL,
32 #ifdef RT_USING_FINSH
33     win_netdev_ping,
34     RT_NULL,
35 #endif /* RT_USING_FINSH */
36     RT_NULL,
37 };
38 
39  static const struct sal_socket_ops windows_socket_ops =
40  {
41      win_socket,
42      win_closesocket,
43      win_bind,
44      win_listen,
45      win_connect,
46      win_accept,
47      win_sendto,
48      win_recvfrom,
49      win_getsockopt,
50      win_setsockopt,
51      win_shutdown,
52      win_getpeername,
53      win_getsockname,
54      win_ioctlsocket,
55  #ifdef SAL_USING_POSIX
56      inet_poll,
57  #else
58      RT_NULL,
59  #endif
60  };
61 
62  static const struct sal_netdb_ops windows_netdb_ops =
63  {
64      win_gethostbyname,
65      RT_NULL,
66      win_getaddrinfo,
67      win_freeaddrinfo,
68  };
69 
70  static const struct sal_proto_family windows_inet_family =
71  {
72      AF_INET,
73      AF_INET6,
74      &windows_socket_ops,
75      &windows_netdb_ops,
76  };
77 
78  /* Set lwIP network interface device protocol family information */
sal_win_netdev_set_pf_info(struct netdev * netdev)79  int sal_win_netdev_set_pf_info(struct netdev* netdev)
80  {
81      RT_ASSERT(netdev);
82 
83      netdev->sal_user_data = (void*)&windows_inet_family;
84      return 0;
85  }
86 
win_netdev_add(void)87 static int win_netdev_add(void)
88 {
89 #define ETHERNET_MTU        1500
90 #define HWADDR_LEN          6
91 
92     rt_err_t result = RT_EOK;
93     struct netdev* netdev = RT_NULL;
94 
95     char name[RT_NAME_MAX] = {0};
96 
97     netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
98     if (netdev == RT_NULL)
99     {
100         return -RT_EEMPTY;
101     }
102 
103     sal_win_netdev_set_pf_info(netdev);
104 
105     rt_strncpy(name, "win_e0", RT_NAME_MAX);
106     result = netdev_register(netdev, name, RT_NULL);
107 
108     netdev->flags = NETDEV_FLAG_UP | NETDEV_FLAG_LINK_UP | NETDEV_FLAG_INTERNET_UP;
109     netdev->mtu = ETHERNET_MTU;
110     netdev->ops = &win_netdev_ops;
111     netdev->hwaddr_len = HWADDR_LEN;
112 
113     return result;
114 }
115 INIT_ENV_EXPORT(win_netdev_add);
116