From: Daniele Basile Date: Mon, 23 Jan 2012 11:21:01 +0000 (+0100) Subject: Add server tcp api. X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=1a8a2b9b57118be3f52d1261ebefac3875e67bcf;p=bertos.git Add server tcp api. --- diff --git a/bertos/net/tcp_socket.c b/bertos/net/tcp_socket.c index 34a6ef66..65db328f 100644 --- a/bertos/net/tcp_socket.c +++ b/bertos/net/tcp_socket.c @@ -85,6 +85,11 @@ static bool tcpsocket_reconnect(TcpSocket *socket) /* Close socket if was opened */ close_socket(socket); + /* If we are in server mode we do nothing */ + if (socket->handler) + return true; + + /* Start with new connection */ socket->sock = netconn_new(NETCONN_TCP); if(!socket->sock) @@ -261,9 +266,27 @@ static void tcpsocket_clearerr(KFile *fd) socket->error = 0; } +void tcpsocket_serverPoll(KFile *fd) +{ + TcpSocket *socket = TCPSOCKET_CAST(fd); + + + if (!socket->sock) + socket->sock = netconn_accept(socket->server_sock); + + if (!socket->sock) + { + LOG_ERR("Unable to connect with client\n"); + return; + } + + socket->handler(fd); +} + void tcpsocket_init(TcpSocket *socket, struct ip_addr *local_addr, struct ip_addr *remote_addr, uint16_t port) { - socket->sock = NULL; + memset(socket, 0, sizeof(TcpSocket)); + socket->local_addr = local_addr; socket->remote_addr = remote_addr; socket->port = port; @@ -277,3 +300,15 @@ void tcpsocket_init(TcpSocket *socket, struct ip_addr *local_addr, struct ip_add socket->fd.reopen = tcpsocket_reopen; } + +void tcpsocket_serverInit(TcpSocket *socket, struct ip_addr *local_addr, struct ip_addr *listen_addr, uint16_t port, tcphandler_t handler) +{ + tcpsocket_init(socket, local_addr, listen_addr, port); + socket->handler = handler; + + socket->server_sock = netconn_new(NETCONN_TCP); + socket->error = netconn_bind(socket->server_sock, listen_addr, port); + socket->error = netconn_listen(socket->server_sock); + if(socket->error != ERR_OK) + LOG_ERR("Init server\n"); +} diff --git a/bertos/net/tcp_socket.h b/bertos/net/tcp_socket.h index 74ca1074..a5eef825 100644 --- a/bertos/net/tcp_socket.h +++ b/bertos/net/tcp_socket.h @@ -46,10 +46,12 @@ #include #include +typedef void (*tcphandler_t)(KFile *fd); + typedef struct TcpSocket { KFile fd; ///< KFile context. - struct netconn *sock; ///< Current sockect connection pointer. + struct netconn *sock; ///< Current socket connection. struct netbuf *rx_buf_conn; ///< Current received buffer from socket. size_t remaning_data_len; ///< Number of bytes to read from the received buffer. @@ -58,6 +60,9 @@ typedef struct TcpSocket uint16_t port; ///< Number port to connect. int error; ///< Error status. + + struct netconn *server_sock; ///< Server sockect connection. + tcphandler_t handler; ///< TCP handler when are in server mode. } TcpSocket; #define KFT_TCPSOCKET MAKE_ID('T', 'S', 'C', 'K') @@ -70,4 +75,7 @@ INLINE TcpSocket *TCPSOCKET_CAST(KFile *fd) void tcpsocket_init(TcpSocket *socket, struct ip_addr *local_addr, struct ip_addr *remote_addr, uint16_t port); +void tcpsocket_serverPoll(KFile *fd); +void tcpsocket_serverInit(TcpSocket *socket, struct ip_addr *local_addr, struct ip_addr *remote_addr, uint16_t port, tcphandler_t handler); + #endif /* NET_TCP_SOCKET_H */