2 * Copyright (c) 2004, Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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 $
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.
66 * Return value of the buffering functions that indicates that an
67 * end-marker byte was found.
72 /*---------------------------------------------------------------------------*/
73 static void buf_setup( struct psock_buf *buf, u8_t *bufptr, u16_t bufsize )
79 /*---------------------------------------------------------------------------*/
80 static u8_t buf_bufdata( struct psock_buf *buf, u16_t len, u8_t **dataptr, u16_t *datalen )
84 if( *datalen < buf->left )
86 memcpy( buf->ptr, *dataptr, *datalen );
88 buf->left -= *datalen;
93 else if( *datalen == buf->left )
95 memcpy( buf->ptr, *dataptr, *datalen );
104 memcpy( buf->ptr, *dataptr, buf->left );
105 buf->ptr += buf->left;
106 *datalen -= buf->left;
107 *dataptr += buf->left;
113 /*---------------------------------------------------------------------------*/
114 static u8_t buf_bufto( register struct psock_buf *buf, u8_t endmarker, register u8_t **dataptr, register u16_t *datalen )
117 while( buf->left > 0 && *datalen > 0 )
119 c = *buf->ptr = **dataptr;
133 return BUF_NOT_FOUND;
136 while( *datalen > 0 )
144 return BUF_FOUND | 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() );
162 uip_send( s->sendptr, s->sendlen );
165 s->state = STATE_DATA_SENT;
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();
184 s->sendptr += s->sendlen;
188 s->state = STATE_ACKED;
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. */
203 PT_EXIT( &s->psockpt );
206 /* Save the length of and a pointer to the data that is to be
208 s->sendptr = ( u8_t * ) buf;
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
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 )
281 /* There is data in the uip_appdata buffer that has not yet been
282 read with the PSOCK_READ functions. */
285 else if( s->state == STATE_READ )
287 /* All data in uip_appdata buffer already consumed. */
288 s->state = STATE_BLOCKED_NEWDATA;
291 else if( uip_newdata() )
293 /* There is new data that has not been consumed. */
298 /* There is no new data. */
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;
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 /*---------------------------------------------------------------------------*/