Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bsp/simulator/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ menu "Peripheral Drivers"
bool "Enable BSD Socket"
select RT_USING_POSIX_FS
select RT_USING_POSIX_SOCKET
select SAL_USING_WINSOCK
default y

config BSP_USING_LVGL
Expand Down
43 changes: 26 additions & 17 deletions bsp/simulator/drivers/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from building import *
Import('rtconfig')

cwd = GetCurrentDir()
src = Glob('*.c')
src = ['board.c', 'uart_console.c']
LIBS = []
LIBPATH = []
CPPPATH = [cwd]
Expand All @@ -30,28 +30,37 @@ if rtconfig.CROSS_TOOL == 'msvc':
]

# remove no need file.
if GetDepend('PKG_USING_GUIENGINE') == False:
SrcRemove(src, 'sdl_fb.c')
if GetDepend('PKG_USING_GUIENGINE') == True:
src += ['sdl_fb.c']
else:
LIBS.append('SDL2')
if sys.platform == 'win32':
LIBPATH.append(os.path.abspath(os.path.join(cwd, '../SDL2/lib/x86')))
CPPPATH.append(os.path.abspath(os.path.join(cwd, '../SDL2/include')))

if GetDepend('BSP_USING_RTC') == False:
SrcRemove(src, 'drv_rtc.c')
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_DFS_ELMFAT') == False:
SrcRemove(src, 'sd_sim.c')
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NAND') == False:
SrcRemove(src, 'nanddrv_file.c')
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NOR') == False:
SrcRemove(src, 'sst25vfxx_mtd_sim.c')
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_DFS_WINSHAREDIR') == False:
SrcRemove(src, 'dfs_win32.c')
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MODULE') == False:
SrcRemove(src, ['module_win32.c'])
if sys.platform[0:5]=="linux": #check whether under linux
SrcRemove(src, ['module_win32.c', 'dfs_win32.c'])

if GetDepend('RT_USING_DFS') == True:
if GetDepend('RT_USING_DFS_ELMFAT') == True:
src += ['sd_sim.c']

if GetDepend('RT_USING_MTD_NAND') == True:
src += ['nanddrv_file.c']

if GetDepend('RT_USING_MTD_NOR') == True:
src += ['sst25vfxx_mtd_sim.c']

if sys.platform == "win32":
if GetDepend('RT_USING_DFS_WINSHAREDIR') == True:
src += ['dfs_win32.c']
if GetDepend('RT_USING_MODULE') == True:
src += ['module_win32.c']

if GetDepend('BSP_USING_RTC') == True:
src += ['drv_rtc.c']

if GetDepend('BSP_USING_SOCKET') == True:
src += [cwd + '/winsock/drv_win_eth.c']
src += [cwd + '/winsock/sal_winsock.c']

group = DefineGroup('Drivers', src, depend = [''],
CPPPATH = CPPPATH, LIBS=LIBS, LIBPATH=LIBPATH, CPPDEFINES=CPPDEFINES)
Expand Down
115 changes: 115 additions & 0 deletions bsp/simulator/drivers/winsock/drv_win_eth.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
117 changes: 117 additions & 0 deletions bsp/simulator/drivers/winsock/rtt_winsock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-05-30 xiangxistu first version
*/

#define DNS_MAX_NAME_LENGTH 256

/*
* 1. Define the structure to avoid conflict with winsock's structure.
* 2. And the same time, in the "af_inet_winsock.c" shouldn't include header files aboult "sal",
* if include header files aboult "sal", the complier in the vs2012 will give me "structure redefined error".
*
* So, i define the same structure with "sal" but not include header files aboult "sal".
* The same sturcture means the same memory in the system. I can offer wonderful compatibility with "sal" ,"winsock", "lwip" and so on.
*
*
* Aross the way, "WinSock2.h" only be included in the "af_inet_winsock.c", the more software packages aboult network can
* work that useless modification is required for "winsock".
*
*/
typedef uint32_t sal_type_socklen_t;
typedef uint16_t in_port_t;
typedef uint8_t sa_family_t;
typedef uint32_t in_addr_t;

typedef struct sal_type_ip4_addr
{
uint32_t addr;
} sal_type_ip4_addr_t;
typedef sal_type_ip4_addr_t sal_type_ip_addr_t;

struct sal_type_sockaddr
{
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
};

struct sal_type_in_addr
{
in_addr_t sal_type_s_addr;
};

struct sal_type_sockaddr_in
{
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct sal_type_in_addr sin_addr;
#define SIN_ZERO_LEN 8
char sin_zero[SIN_ZERO_LEN];
};

struct sal_type_sockaddr_storage
{
uint8_t s2_len;
sa_family_t ss_family;
char s2_data1[2];
uint32_t s2_data2[3];
#if NETDEV_IPV6
uint32_t s2_data3[3];
#endif /* NETDEV_IPV6 */
};

struct sal_type_addrinfo {
int ai_flags; /* Input flags. */
int ai_family; /* Address family of socket. */
int ai_socktype; /* Socket type. */
int ai_protocol; /* Protocol of socket. */
socklen_t ai_addrlen; /* Length of socket address. */
struct sal_type_sockaddr* ai_addr; /* Socket address of socket. */
char* ai_canonname; /* Canonical name of service location. */
struct sal_type_addrinfo* ai_next; /* Pointer to next in list. */
};

struct sal_type_hostent {
char* h_name; /* Official name of the host. */
char** h_aliases; /* A pointer to an array of pointers to alternative host names,
terminated by a null pointer. */
int h_addrtype; /* Address type. */
int h_length; /* The length, in bytes, of the address. */
char** h_addr_list; /* A pointer to an array of pointers to network addresses (in
network byte order) for the host, terminated by a null pointer. */
#define h_addr h_addr_list[0] /* for backward compatibility */
};


/* sal_socket_ops */
int win_socket(int domain, int type, int protocol);
int win_closesocket(int s);
int win_bind(int s, const struct sal_type_sockaddr* name, sal_type_socklen_t namelen);
int win_listen(int s, int backlog);
int win_connect(int s, const struct sal_type_sockaddr* name, sal_type_socklen_t namelen);
int win_accept(int s, struct sal_type_sockaddr* addr, sal_type_socklen_t* addrlen);
int win_sendto(int s, const void* data, size_t size, int flags, const struct sal_type_sockaddr* to, sal_type_socklen_t tolen);
int win_recvfrom(int s, void* mem, size_t len, int flags, struct sal_type_sockaddr* from, sal_type_socklen_t* fromlen);
int win_getsockopt(int s, int level, int optname, void* optval, sal_type_socklen_t* optlen);
int win_setsockopt(int s, int level, int optname, const void* optval, sal_type_socklen_t optlen);
int win_shutdown(int s, int how);
int win_getpeername(int s, struct sal_type_sockaddr* name, sal_type_socklen_t* namelen);
int win_getsockname(int s, struct sal_type_sockaddr* name, sal_type_socklen_t* namelen);
int win_ioctlsocket(int s, long cmd, void* arg);

#ifdef SAL_USING_POSIX
int inet_poll(struct dfs_fd* file, struct rt_pollreq* req);
#endif /* SAL_USING_POSIX */


/* sal_netdb_ops */
struct sal_type_hostent* win_gethostbyname(const char* name);
int win_getaddrinfo(const char* nodename, const char* servname, const struct sal_type_addrinfo* hints, struct sal_type_addrinfo** res);
void win_freeaddrinfo(struct sal_type_addrinfo* ai);
Loading