/** ****************************************************************************** * @file mico_socket.c * @author William Xu * @version V1.0.0 * @date 05-Aug-2018 * @brief This file provide the MiCO Socket abstract layer convert functions. ****************************************************************************** * * UNPUBLISHED PROPRIETARY SOURCE CODE * Copyright (c) 2016 MXCHIP Inc. * * The contents of this file may not be disclosed to third parties, copied or * duplicated in any form, in whole or in part, without the prior written * permission of MXCHIP Corporation. ****************************************************************************** */ #include #include #include "common.h" #include "lwip/sockets.h" #include "lwip/netdb.h" /****************************************************** * Macros ******************************************************/ #ifdef inet_addr #undef inet_addr #endif #ifdef inet_ntoa #undef inet_ntoa #endif /****************************************************** * Constants ******************************************************/ /****************************************************** * Enumerations ******************************************************/ /****************************************************** * Type Definitions ******************************************************/ /****************************************************** * Structures ******************************************************/ struct pollfd { int fd; /**< fd related to */ short events; /**< which POLL... events to respond to */ short revents; /**< which POLL... events occurred */ }; #define POLLIN 0x0001 #define POLLPRI 0x0002 #define POLLOUT 0x0004 #define POLLERR 0x0008 #define POLLHUP 0x0010 #define POLLNVAL 0x0020 /****************************************************** * Function Declarations ******************************************************/ /****************************************************** * Variables Definitions ******************************************************/ /****************************************************** * Function Definitions ******************************************************/ int socket(int domain, int type, int protocol) { return lwip_socket( domain, type, protocol ); } int setsockopt (int socket, int level, int optname, void *optval, socklen_t optlen) { return lwip_setsockopt( socket, level, optname, optval, optlen ); } int getsockopt (int socket, int level, int optname, void *optval, socklen_t *optlen_ptr) { return lwip_getsockopt( socket, level, optname, optval, optlen_ptr ); } int bind (int socket, struct sockaddr *addr, socklen_t length) { return lwip_bind ( socket, addr, length); } int connect (int socket, struct sockaddr *addr, socklen_t length) { return lwip_connect( socket, addr, length ); } int listen (int socket, int n) { return lwip_listen( socket, n ); } int accept (int socket, struct sockaddr *addr, socklen_t *length_ptr) { return lwip_accept( socket, addr, length_ptr ); } int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { nfds = 64; if ((timeout->tv_sec == 0) && (timeout->tv_usec < 1000)) // timeout must bigger than 1ms. timeout->tv_usec = 1000; return lwip_select( nfds, readfds, writefds, exceptfds, timeout ); } int poll(struct pollfd *fds, int nfds, int timeout) { int maxfd=0; int i, n; fd_set rfds, wfds, efds; struct timeval t; int ret = 0, got; //printf("poll nfds=%d, timeout = %d\r\n", nfds, timeout); FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); for(i=0; i maxfd) maxfd = fds[i].fd; if (fds[i].events & (POLLIN|POLLPRI)) FD_SET(fds[i].fd, &rfds); if (fds[i].events & (POLLOUT)) FD_SET(fds[i].fd, &wfds); if (fds[i].events & (POLLERR|POLLHUP|POLLNVAL)) FD_SET(fds[i].fd, &efds); fds[i].revents = 0; //printf("<%d> fd=%d, evetns = %x\r\n", i, fds[i].fd, fds[i].events); } if (timeout < 0) { n = lwip_select(maxfd+1, &rfds, &wfds, &efds, NULL); } else { t.tv_sec = timeout / 1000; t.tv_usec = (timeout % 1000) * 1000; n = lwip_select(maxfd+1, &rfds, &wfds, &efds, &t); } if (n <= 0) return n; for(i=0; i= 5 return ipaddr_ntoa( (ip_addr_t*) &(addr) ); #elif LwIP_VERSION_MAJOR == 2 return ipaddr_ntoa( (ip_addr_t*) &(addr) ); #else #error LwIP version not supported #endif }