FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / ethernet / FreeRTOS-uIP / psock.c
blobd29ef0172c8dcf1f76aff9a1b905d0c4f5413078
1 /*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * This file is part of the uIP TCP/IP stack
31 * Author: Adam Dunkels <adam@sics.se>
33 * $Id: psock.c,v 1.1 2007/01/04 11:06:40 adamdunkels Exp $
35 #include <stdio.h>
36 #include <string.h>
38 #include "uipopt.h"
39 #include "psock.h"
40 #include "uip.h"
42 #define STATE_NONE 0
43 #define STATE_ACKED 1
44 #define STATE_READ 2
45 #define STATE_BLOCKED_NEWDATA 3
46 #define STATE_BLOCKED_CLOSE 4
47 #define STATE_BLOCKED_SEND 5
48 #define STATE_DATA_SENT 6
51 * Return value of the buffering functions that indicates that a
52 * buffer was not filled by incoming data.
55 #define BUF_NOT_FULL 0
56 #define BUF_NOT_FOUND 0
59 * Return value of the buffering functions that indicates that a
60 * buffer was completely filled by incoming data.
63 #define BUF_FULL 1
66 * Return value of the buffering functions that indicates that an
67 * end-marker byte was found.
70 #define BUF_FOUND 2
72 /*---------------------------------------------------------------------------*/
73 static void buf_setup( struct psock_buf *buf, u8_t *bufptr, u16_t bufsize )
75 buf->ptr = bufptr;
76 buf->left = bufsize;
79 /*---------------------------------------------------------------------------*/
80 static u8_t buf_bufdata( struct psock_buf *buf, u16_t len, u8_t **dataptr, u16_t *datalen )
82 ( void ) len;
84 if( *datalen < buf->left )
86 memcpy( buf->ptr, *dataptr, *datalen );
87 buf->ptr += *datalen;
88 buf->left -= *datalen;
89 *dataptr += *datalen;
90 *datalen = 0;
91 return BUF_NOT_FULL;
93 else if( *datalen == buf->left )
95 memcpy( buf->ptr, *dataptr, *datalen );
96 buf->ptr += *datalen;
97 buf->left = 0;
98 *dataptr += *datalen;
99 *datalen = 0;
100 return BUF_FULL;
102 else
104 memcpy( buf->ptr, *dataptr, buf->left );
105 buf->ptr += buf->left;
106 *datalen -= buf->left;
107 *dataptr += buf->left;
108 buf->left = 0;
109 return BUF_FULL;
113 /*---------------------------------------------------------------------------*/
114 static u8_t buf_bufto( register struct psock_buf *buf, u8_t endmarker, register u8_t **dataptr, register u16_t *datalen )
116 u8_t c;
117 while( buf->left > 0 && *datalen > 0 )
119 c = *buf->ptr = **dataptr;
120 ++ *dataptr;
121 ++buf->ptr;
122 -- *datalen;
123 --buf->left;
125 if( c == endmarker )
127 return BUF_FOUND;
131 if( *datalen == 0 )
133 return BUF_NOT_FOUND;
136 while( *datalen > 0 )
138 c = **dataptr;
139 -- *datalen;
140 ++ *dataptr;
142 if( c == endmarker )
144 return BUF_FOUND | BUF_FULL;
148 return BUF_FULL;
151 /*---------------------------------------------------------------------------*/
152 static char send_data( register struct psock *s )
154 if( s->state != STATE_DATA_SENT || uip_rexmit() )
156 if( s->sendlen > uip_mss() )
158 uip_send( s->sendptr, uip_mss() );
160 else
162 uip_send( s->sendptr, s->sendlen );
165 s->state = STATE_DATA_SENT;
166 return 1;
169 return 0;
172 /*---------------------------------------------------------------------------*/
173 static char data_acked( register struct psock *s )
175 if( s->state == STATE_DATA_SENT && uip_acked() )
177 if( s->sendlen > uip_mss() )
179 s->sendlen -= uip_mss();
180 s->sendptr += uip_mss();
182 else
184 s->sendptr += s->sendlen;
185 s->sendlen = 0;
188 s->state = STATE_ACKED;
189 return 1;
192 return 0;
195 /*---------------------------------------------------------------------------*/
196 PT_THREAD( psock_send ( register struct psock *s, const char *buf, unsigned int len ) )
198 PT_BEGIN( &s->psockpt );
200 /* If there is no data to send, we exit immediately. */
201 if( len == 0 )
203 PT_EXIT( &s->psockpt );
206 /* Save the length of and a pointer to the data that is to be
207 sent. */
208 s->sendptr = ( u8_t * ) buf;
209 s->sendlen = len;
211 s->state = STATE_NONE;
213 /* We loop here until all data is sent. The s->sendlen variable is
214 updated by the data_sent() function. */
215 while( s->sendlen > 0 )
218 * The condition for this PT_WAIT_UNTIL is a little tricky: the
219 * protothread will wait here until all data has been acknowledged
220 * (data_acked() returns true) and until all data has been sent
221 * (send_data() returns true). The two functions data_acked() and
222 * send_data() must be called in succession to ensure that all
223 * data is sent. Therefore the & operator is used instead of the
224 * && operator, which would cause only the data_acked() function
225 * to be called when it returns false.
227 PT_WAIT_UNTIL( &s->psockpt, data_acked(s) & send_data(s) );
230 s->state = STATE_NONE;
232 PT_END( &s->psockpt );
235 /*---------------------------------------------------------------------------*/
236 PT_THREAD( psock_generator_send ( register struct psock *s, unsigned short ( *generate ) ( void * ), void *arg ) )
238 PT_BEGIN( &s->psockpt );
240 /* Ensure that there is a generator function to call. */
241 if( generate == NULL )
243 PT_EXIT( &s->psockpt );
246 /* Call the generator function to generate the data in the
247 uip_appdata buffer. */
248 s->sendlen = generate( arg );
249 s->sendptr = uip_appdata;
251 s->state = STATE_NONE;
254 /* Call the generator function again if we are called to perform a
255 retransmission. */
256 if( uip_rexmit() )
258 generate( arg );
261 /* Wait until all data is sent and acknowledged. */
262 PT_WAIT_UNTIL( &s->psockpt, data_acked(s) & send_data(s) );
263 } while( s->sendlen > 0 );
265 s->state = STATE_NONE;
267 PT_END( &s->psockpt );
270 /*---------------------------------------------------------------------------*/
271 u16_t psock_datalen( struct psock *psock )
273 return psock->bufsize - psock->buf.left;
276 /*---------------------------------------------------------------------------*/
277 char psock_newdata( struct psock *s )
279 if( s->readlen > 0 )
281 /* There is data in the uip_appdata buffer that has not yet been
282 read with the PSOCK_READ functions. */
283 return 1;
285 else if( s->state == STATE_READ )
287 /* All data in uip_appdata buffer already consumed. */
288 s->state = STATE_BLOCKED_NEWDATA;
289 return 0;
291 else if( uip_newdata() )
293 /* There is new data that has not been consumed. */
294 return 1;
296 else
298 /* There is no new data. */
299 return 0;
303 /*---------------------------------------------------------------------------*/
304 PT_THREAD( psock_readto ( register struct psock *psock, unsigned char c ) )
306 PT_BEGIN( &psock->psockpt );
308 buf_setup( &psock->buf, ( u8_t * ) psock->bufptr, psock->bufsize );
310 /* XXX: Should add buf_checkmarker() before do{} loop, if
311 incoming data has been handled while waiting for a write. */
314 if( psock->readlen == 0 )
316 PT_WAIT_UNTIL( &psock->psockpt, psock_newdata(psock) );
317 psock->state = STATE_READ;
318 psock->readptr = ( u8_t * ) uip_appdata;
319 psock->readlen = uip_datalen();
321 } while( (buf_bufto(&psock->buf, c, &psock->readptr, &psock->readlen) & BUF_FOUND) == 0 );
323 if( psock_datalen(psock) == 0 )
325 psock->state = STATE_NONE;
326 PT_RESTART( &psock->psockpt );
329 PT_END( &psock->psockpt );
332 /*---------------------------------------------------------------------------*/
333 PT_THREAD( psock_readbuf ( register struct psock *psock ) )
335 PT_BEGIN( &psock->psockpt );
337 buf_setup( &psock->buf, ( u8_t * ) psock->bufptr, psock->bufsize );
339 /* XXX: Should add buf_checkmarker() before do{} loop, if
340 incoming data has been handled while waiting for a write. */
343 if( psock->readlen == 0 )
345 PT_WAIT_UNTIL( &psock->psockpt, psock_newdata(psock) );
346 printf( "Waited for newdata\n" );
347 psock->state = STATE_READ;
348 psock->readptr = ( u8_t * ) uip_appdata;
349 psock->readlen = uip_datalen();
351 } while( buf_bufdata(&psock->buf, psock->bufsize, &psock->readptr, &psock->readlen) != BUF_FULL );
353 if( psock_datalen(psock) == 0 )
355 psock->state = STATE_NONE;
356 PT_RESTART( &psock->psockpt );
359 PT_END( &psock->psockpt );
362 /*---------------------------------------------------------------------------*/
363 void psock_init( register struct psock *psock, char *buffer, unsigned int buffersize )
365 psock->state = STATE_NONE;
366 psock->readlen = 0;
367 psock->bufptr = buffer;
368 psock->bufsize = buffersize;
369 buf_setup( &psock->buf, ( u8_t * ) buffer, buffersize );
370 PT_INIT( &psock->pt );
371 PT_INIT( &psock->psockpt );
374 /*---------------------------------------------------------------------------*/