Browse Source

[DFS] Add lwIP file system interface for DFS

Bernard Xiong 10 years ago
parent
commit
8dfc80b4a2

+ 11 - 0
components/dfs/filesystems/lwip/SConscript

@@ -0,0 +1,11 @@
+# RT-Thread building script for component
+
+from building import *
+
+cwd = GetCurrentDir()
+src = Glob('*.c')
+CPPPATH = [cwd]
+
+group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS', 'RT_USING_DFS_LWIP'], CPPPATH = CPPPATH)
+
+Return('group')

+ 30 - 0
components/dfs/filesystems/lwip/arpa/inet.h

@@ -0,0 +1,30 @@
+/*
+ * File      : inet.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#ifndef INET_H__
+#define INET_H__
+
+#include <lwip/inet.h>
+
+#endif

+ 125 - 0
components/dfs/filesystems/lwip/dfs_lwip.c

@@ -0,0 +1,125 @@
+/*
+ * File      : dfs_lwip.c
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#include <rtthread.h>
+#include <dfs.h>
+#include <dfs_fs.h>
+
+#include "dfs_lwip.h"
+
+int dfs_lwip_getsocket(int fd)
+{
+    struct dfs_fd *_dfs_fd; 
+    
+    _dfs_fd = fd_get(fd);
+    if (_dfs_fd == RT_NULL) return -1;
+
+    return 0;
+}
+
+int dfs_lwip_ioctl(struct dfs_fd* file, int cmd, void* args)
+{
+    return -DFS_STATUS_EIO;
+}
+
+int dfs_lwip_read(struct dfs_fd* file, void *buf, rt_size_t count)
+{
+    int sock;
+
+    sock = (int)file->data;
+    count = lwip_read(sock, buf, count);
+
+    return count;
+}
+
+int dfs_lwip_write(struct dfs_fd *file, const void *buf, rt_size_t count)
+{
+    int sock;
+    
+    sock = (int)file->data;
+    count = lwip_write(sock, buf, count);
+
+    return count;
+}
+
+int dfs_lwip_close(struct dfs_fd* file)
+{
+    int sock;
+    int result;
+    
+    sock = (int)file->data;
+    result = lwip_close(sock);
+    
+    if (result == 0) return DFS_STATUS_OK;
+    
+    return -result;
+}
+
+static const struct dfs_filesystem_operation _lwip_fs_ops = 
+{
+    "lwip",
+    DFS_FS_FLAG_DEFAULT,
+    RT_NULL,    /* mount    */
+    RT_NULL,    /* unmont   */
+    RT_NULL,    /* mkfs     */
+    RT_NULL,    /* statfs   */
+
+    RT_NULL,    /* open     */
+    dfs_lwip_close,
+    dfs_lwip_ioctl,
+    dfs_lwip_read,
+    dfs_lwip_write,
+    RT_NULL,
+    RT_NULL,    /* lseek    */
+    RT_NULL,    /* getdents */
+    RT_NULL,    /* unlink   */
+    RT_NULL,    /* stat     */
+    RT_NULL,    /* rename   */
+};
+
+static struct dfs_filesystem _lwip_fs = 
+{
+    0,              /* dev_id */
+    RT_NULL,        /* path */
+    &_lwip_fs_ops,
+    RT_NULL         /* data */
+};
+
+struct dfs_filesystem* dfs_lwip_get_fs(void)
+{
+    return &_lwip_fs;
+}
+
+/*
+NOTE: Beause we don't need to mount lwIP file system, the filesystem_ops is not 
+registered to the system. 
+
+int dfs_lwip_system_init(void)
+{
+    dfs_register(&_lwip_fs_ops);
+    
+    return 0;
+}
+INIT_FS_EXPORT(dfs_lwip_system_init);
+*/

+ 41 - 0
components/dfs/filesystems/lwip/dfs_lwip.h

@@ -0,0 +1,41 @@
+/*
+ * File      : dfs_lwip.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#ifndef DFS_LWIP_H__
+#define DFS_LWIP_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct dfs_filesystem* dfs_lwip_get_fs(void);
+int dfs_lwip_getsocket(int fd);
+
+int dfs_lwip_system_init(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 164 - 0
components/dfs/filesystems/lwip/lwip_sockets.c

@@ -0,0 +1,164 @@
+/*
+ * File      : lwip_sockets.c
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#include <dfs.h>
+#include <dfs_def.h>
+#include <lwip/sockets.h>
+#include <sys/socket.h>
+
+#include "dfs_lwip.h"
+
+int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
+{
+    int sock = dfs_lwip_getsocket(s);
+
+    return lwip_accept(sock, addr, addrlen);
+}
+
+int bind(int s, const struct sockaddr *name, socklen_t namelen)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_bind(sock, name, namelen);
+}
+
+int shutdown(int s, int how)
+{
+    int sock = dfs_lwip_getsocket(s);
+
+    return lwip_shutdown(s, how);
+}
+
+int getpeername (int s, struct sockaddr *name, socklen_t *namelen)
+{
+    int sock = dfs_lwip_getsocket(s);
+
+    return lwip_getpeername(sock, name, namelen);
+}
+
+int getsockname (int s, struct sockaddr *name, socklen_t *namelen)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_getsockname(sock, name, namelen);
+}
+
+int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_getsockopt(sock, level, optname, optval, optlen);
+}
+
+int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_setsockopt(sock, level, optname, optval, optlen);
+}
+
+int connect(int s, const struct sockaddr *name, socklen_t namelen)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_connect(sock, name, namelen);
+}
+
+int listen(int s, int backlog)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_listen(sock, backlog);
+}
+
+int recv(int s, void *mem, size_t len, int flags)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_recv(sock, mem, len, flags);
+}
+
+int recvfrom(int s, void *mem, size_t len, int flags,
+      struct sockaddr *from, socklen_t *fromlen)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_recvfrom(sock, mem, len, flags, from, fromlen);
+}
+
+int send(int s, const void *dataptr, size_t size, int flags)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_send(sock, dataptr, size, flags);
+}
+
+int sendto(int s, const void *dataptr, size_t size, int flags,
+    const struct sockaddr *to, socklen_t tolen)
+{
+    int sock = dfs_lwip_getsocket(s);
+    
+    return lwip_sendto(sock, dataptr, size, flags, to, tolen);
+}
+
+int socket(int domain, int type, int protocol)
+{
+    /* create a BSD socket */
+    int fd;
+    int sock;
+    struct dfs_fd *d;
+
+    /* allocate a fd */
+    fd = fd_new();
+    if (fd < 0)
+    {
+        rt_set_errno(-DFS_STATUS_ENOMEM);
+
+        return -1;
+    }
+    d = fd_get(fd);
+
+    /* create socket in lwip and then put it to the dfs_fd */
+    sock = lwip_socket(domain, type, protocol);
+    if (sock > 0)
+    {
+        /* this is a socket fd */
+        d->type = FT_SOCKET;
+        d->path = RT_NULL;
+
+        d->fs = dfs_lwip_get_fs();
+        
+        d->flags = DFS_O_RDWR; /* set flags as read and write */
+        d->size = 0;
+        d->pos  = 0;
+        
+        /* set socket to the data of dfs_fd */
+        d->data = (void*) sock;
+    }
+
+    /* release the ref-count of fd */
+    fd_put(d);
+
+    return fd;
+}

+ 50 - 0
components/dfs/filesystems/lwip/netdb.c

@@ -0,0 +1,50 @@
+/*
+ * File      : netdb.c
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#include "netdb.h"
+#include <lwip/netdb.h>
+
+struct hostent *gethostbyname(const char *name)
+{
+    return lwip_gethostbyname(name);
+}
+
+int gethostbyname_r(const char *name, struct hostent *ret, char *buf,
+                size_t buflen, struct hostent **result, int *h_errnop)
+{
+    return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
+}
+
+void freeaddrinfo(struct addrinfo *ai)
+{
+    lwip_freeaddrinfo(ai);
+}
+
+int getaddrinfo(const char *nodename,
+       const char *servname,
+       const struct addrinfo *hints,
+       struct addrinfo **res)
+{
+    return lwip_getaddrinfo(nodename, servname, hints, res);
+}

+ 48 - 0
components/dfs/filesystems/lwip/netdb.h

@@ -0,0 +1,48 @@
+/*
+ * File      : netdb.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#ifndef NETDB_H__
+#define NETDB_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <lwip/netdb.h>
+
+struct hostent *gethostbyname(const char *name);
+
+int gethostbyname_r(const char *name, struct hostent *ret, char *buf,
+                size_t buflen, struct hostent **result, int *h_errnop);
+void freeaddrinfo(struct addrinfo *ai);
+int getaddrinfo(const char *nodename,
+       const char *servname,
+       const struct addrinfo *hints,
+       struct addrinfo **res);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 30 - 0
components/dfs/filesystems/lwip/netinet/in.h

@@ -0,0 +1,30 @@
+/*
+ * File      : in.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#ifndef IN_H__
+#define IN_H__
+
+#include <lwip/sockets.h>
+
+#endif

+ 53 - 0
components/dfs/filesystems/lwip/sys/socket.h

@@ -0,0 +1,53 @@
+/*
+ * File      : netdb.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2015, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2015-02-17     Bernard      First version
+ */
+
+#ifndef SOCKET_H__
+#define SOCKET_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
+int bind(int s, const struct sockaddr *name, socklen_t namelen);
+int shutdown(int s, int how);
+int getpeername (int s, struct sockaddr *name, socklen_t *namelen);
+int getsockname (int s, struct sockaddr *name, socklen_t *namelen);
+int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
+int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
+int connect(int s, const struct sockaddr *name, socklen_t namelen);
+int listen(int s, int backlog);
+int recv(int s, void *mem, size_t len, int flags);
+int recvfrom(int s, void *mem, size_t len, int flags,
+      struct sockaddr *from, socklen_t *fromlen);
+int send(int s, const void *dataptr, size_t size, int flags);
+int sendto(int s, const void *dataptr, size_t size, int flags,
+    const struct sockaddr *to, socklen_t tolen);
+int socket(int domain, int type, int protocol);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 4 - 0
components/net/lwip-1.4.1/src/lwipopts.h

@@ -353,4 +353,8 @@
 #define LWIP_RAND                  rand
 #endif
 
+#ifdef RT_USING_DFS_LWIP
+#define LWIP_COMPAT_SOCKETS        0
+#endif
+
 #endif /* __LWIPOPTS_H__ */