1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2015-02-17 Bernard First version 9 * 2016-05-07 Bernard Rename dfs_lwip to dfs_net 10 * 2018-03-09 Bernard Fix the last data issue in poll. 11 * 2018-05-24 ChenYong Add socket abstraction layer 12 */ 13 14 #include <rtthread.h> 15 16 #include <dfs.h> 17 #include <dfs_net.h> 18 19 #include <sys/socket.h> 20 21 int dfs_net_getsocket(int fd) 22 { 23 int socket; 24 struct dfs_fd *_dfs_fd; 25 26 _dfs_fd = fd_get(fd); 27 if (_dfs_fd == NULL) return -1; 28 29 if (_dfs_fd->type != FT_SOCKET) socket = -1; 30 else socket = (int)_dfs_fd->data; 31 32 fd_put(_dfs_fd); /* put this dfs fd */ 33 return socket; 34 } 35 36 static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args) 37 { 38 return -EIO; 39 } 40 41 static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count) 42 { 43 int socket = (int) file->data; 44 45 return sal_recvfrom(socket, buf, count, 0, NULL, NULL); 46 } 47 48 static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count) 49 { 50 int socket = (int) file->data; 51 52 return sal_sendto(socket, buf, count, 0, NULL, 0); 53 } 54 55 static int dfs_net_close(struct dfs_fd* file) 56 { 57 int socket = (int) file->data; 58 59 return sal_closesocket(socket); 60 } 61 62 static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req) 63 { 64 extern int sal_poll(struct dfs_fd *file, struct rt_pollreq *req); 65 66 return sal_poll(file, req); 67 } 68 69 const struct dfs_file_ops _net_fops = 70 { 71 NULL, /* open */ 72 dfs_net_close, 73 dfs_net_ioctl, 74 dfs_net_read, 75 dfs_net_write, 76 NULL, 77 NULL, /* lseek */ 78 NULL, /* getdents */ 79 dfs_net_poll, 80 }; 81 82 const struct dfs_file_ops *dfs_net_get_fops(void) 83 { 84 return &_net_fops; 85 } 86