Add configuration file. Remove debug messages.
[bertos.git] / bertos / net / tcp_socket.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2012 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief TCP sockect with kfile interface.
34  *
35  * \author Luca Ottaviano <lottaviano@develer.com>
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  * notest:avr
39  */
40
41 #include "tcp_socket.h"
42
43 #include "cfg/cfg_tcpsocket.h"
44
45 #define LOG_LEVEL   TCPSOCKET_LOG_LEVEL
46 #define LOG_FORMAT  TCPSOCKET_LOG_FORMAT
47 #include <cfg/log.h>
48 #include <cpu/byteorder.h>
49
50 #include <lwip/ip_addr.h>
51 #include <lwip/api.h>
52 #include <lwip/netif.h>
53 #include <lwip/netbuf.h>
54 #include <lwip/tcpip.h>
55
56
57 INLINE int close_socket(TcpSocket *socket)
58 {
59         /* Clean all previuos states */
60         netbuf_delete(socket->rx_buf_conn);
61         socket->rx_buf_conn = NULL;
62         socket->remaning_data_len = 0;
63         socket->error = 0;
64
65         if (!socket->sock)
66                 return 0;
67
68         /* Close socket if was opened */
69         socket->error = netconn_delete(socket->sock);
70         socket->sock = NULL;
71
72         if (socket->error != ERR_OK)
73         {
74                 LOG_ERR("Closing socket\n");
75                 return -1;
76         }
77
78         return 0;
79 }
80
81 static bool tcpsocket_reconnect(TcpSocket *socket)
82 {
83         LOG_INFO("Reconnecting...\n");
84
85         /* Close socket if was opened */
86         close_socket(socket);
87
88         /* Start with new connection */
89         socket->sock = netconn_new(NETCONN_TCP);
90         if(!socket->sock)
91         {
92                 LOG_ERR("Unabe to alloc new connection\n");
93                 socket->error = -1;
94                 goto error;
95         }
96
97         socket->error = netconn_bind(socket->sock, socket->local_addr, socket->port);
98         if(socket->error != ERR_OK)
99         {
100                 LOG_ERR("Connection error\n");
101                 goto error;
102         }
103
104         socket->error = netconn_connect(socket->sock, socket->remote_addr, socket->port);
105         if(socket->error != ERR_OK)
106         {
107                 LOG_ERR("Cannot create socket\n");
108                 goto error;
109         }
110
111         LOG_INFO("connected ip=%d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(socket->remote_addr->addr));
112         return true;
113
114 error:
115         netconn_delete(socket->sock);
116         socket->sock = NULL;
117         return false;
118 }
119
120 static int tcpsocket_close(KFile *fd)
121 {
122         TcpSocket *socket = TCPSOCKET_CAST(fd);
123         return close_socket(socket);
124 }
125
126 static KFile *tcpsocket_reopen(KFile *fd)
127 {
128         TcpSocket *socket = TCPSOCKET_CAST(fd);
129         if (tcpsocket_reconnect(socket))
130                 return fd;
131
132         return NULL;
133 }
134
135 /*
136  * Read data from socket.
137  *
138  * The read return the bytes that had been received if they are less than we request too.
139  * Otherwise if the byte that we want read are less that the received bytes, we return only
140  * the requested bytes. To get the remaning bytes we need to make an others read, until the
141  * buffer is empty.
142  * When there are not any more bytes, a new read takes data from remote socket.
143  */
144 static size_t tcpsocket_read(KFile *fd, void *buf, size_t len)
145 {
146         TcpSocket *socket = TCPSOCKET_CAST(fd);
147
148         char *data;
149         uint16_t read_len = 0;
150
151         if (socket->remaning_data_len <= 0)
152         {
153                 LOG_INFO("No byte left.\n");
154                 netbuf_delete(socket->rx_buf_conn);
155         }
156         else /* We had byte into buffer use that */
157         {
158                 LOG_INFO("Read stored bytes.\n");
159                 if (!socket->rx_buf_conn)
160                 {
161                         LOG_ERR("Byte stored are corrupted!\n");
162                         socket->remaning_data_len = 0;
163                         return 0;
164                 }
165                 uint16_t tot_data_len = 0;
166                 netbuf_data(socket->rx_buf_conn, (void **)&data, &tot_data_len);
167
168                 if (data)
169                 {
170                         ASSERT(((int)tot_data_len - (int)socket->remaning_data_len) >= 0);
171                         size_t chunk_len = MIN((size_t)(socket->remaning_data_len), len);
172                         memcpy((char *)buf, &data[tot_data_len - socket->remaning_data_len], chunk_len);
173
174                         socket->remaning_data_len -= chunk_len;
175                         return chunk_len;
176                 }
177                 else
178                 {
179                         LOG_ERR("No valid data to read\n");
180                         socket->remaning_data_len = 0;
181                         netbuf_delete(socket->rx_buf_conn);
182                         return 0;
183                 }
184         }
185
186         /* Try reconnecting if our socket isn't valid */
187         if (!socket->sock && !tcpsocket_reconnect(socket))
188                 return 0;
189
190         while (len)
191         {
192                 LOG_INFO("Get bytes from socket.\n");
193                 socket->rx_buf_conn = netconn_recv(socket->sock);
194
195                 socket->error = netconn_err(socket->sock);
196                 if (socket->error != ERR_OK)
197                 {
198                         LOG_ERR("While recv %d\n", socket->error);
199                         close_socket(socket);
200                         return 0;
201                 }
202
203                 size_t chunk_len = 0;
204                 uint16_t data_len = 0;
205                 if (socket->rx_buf_conn)
206                 {
207                         netbuf_data(socket->rx_buf_conn, (void **)&data, &data_len);
208
209                         if (data)
210                         {
211                                 chunk_len = MIN((size_t)data_len, len);
212                                 memcpy(buf, data, chunk_len);
213
214                                 socket->remaning_data_len = data_len - chunk_len;
215                         }
216
217                         if (socket->remaning_data_len <= 0)
218                         {
219                                 netbuf_delete(socket->rx_buf_conn);
220                                 socket->rx_buf_conn = NULL;
221                                 socket->remaning_data_len = 0;
222                                 return chunk_len;
223                         }
224                 }
225
226                 len -= chunk_len;
227                 read_len += chunk_len;
228         }
229
230         return read_len;
231 }
232
233 static size_t tcpsocket_write(KFile *fd, const void *buf, size_t len)
234 {
235         TcpSocket *socket = TCPSOCKET_CAST(fd);
236
237         /* Try reconnecting if our socket isn't valid */
238         if (!socket->sock && !tcpsocket_reconnect(socket))
239                 return 0;
240
241         socket->error = netconn_write(socket->sock, buf, len, NETCONN_COPY);
242         if (socket->error != ERR_OK)
243         {
244                 LOG_ERR("While writing %d\n", socket->error);
245                 close_socket(socket);
246                 return 0;
247         }
248
249         return len;
250 }
251
252 static int tcpsocket_error(KFile *fd)
253 {
254         TcpSocket *socket = TCPSOCKET_CAST(fd);
255         return socket->error;
256 }
257
258 static void tcpsocket_clearerr(KFile *fd)
259 {
260         TcpSocket *socket = TCPSOCKET_CAST(fd);
261         socket->error = 0;
262 }
263
264 void tcpsocket_init(TcpSocket *socket, struct ip_addr *local_addr, struct ip_addr *remote_addr, uint16_t port)
265 {
266         socket->sock = NULL;
267         socket->local_addr = local_addr;
268         socket->remote_addr = remote_addr;
269         socket->port = port;
270
271         socket->fd._type = KFT_TCPSOCKET;
272         socket->fd.read = tcpsocket_read;
273         socket->fd.error = tcpsocket_error;
274         socket->fd.close = tcpsocket_close;
275         socket->fd.write = tcpsocket_write;
276         socket->fd.clearerr = tcpsocket_clearerr;
277         socket->fd.reopen = tcpsocket_reopen;
278
279 }