- Main Page
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

# uip/psock.c

    00001 /*
    00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
    00003  * All rights reserved.
    00004  *
    00005  * Redistribution and use in source and binary forms, with or without
    00006  * modification, are permitted provided that the following conditions
    00007  * are met:
    00008  * 1. Redistributions of source code must retain the above copyright
    00009  *    notice, this list of conditions and the following disclaimer.
    00010  * 2. Redistributions in binary form must reproduce the above copyright
    00011  *    notice, this list of conditions and the following disclaimer in the
    00012  *    documentation and/or other materials provided with the distribution.
    00013  * 3. Neither the name of the Institute nor the names of its contributors
    00014  *    may be used to endorse or promote products derived from this software
    00015  *    without specific prior written permission.
    00016  *
    00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
    00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
    00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    00027  * SUCH DAMAGE.
    00028  *
    00029  * This file is part of the uIP TCP/IP stack
    00030  *
    00031  * Author: Adam Dunkels <adam@sics.se>
    00032  *
    00033  * $Id: psock.c,v 1.2 2006/06/12 08:00:30 adam Exp $
    00034  */
    00035
    00036 #include <stdio.h>
    00037 #include <string.h>
    00038
    00039 #include "uipopt.h"
    00040 #include "psock.h"
    00041 #include "uip.h"
    00042
    00043 #define STATE_NONE 0
    00044 #define STATE_ACKED 1
    00045 #define STATE_READ 2
    00046 #define STATE_BLOCKED_NEWDATA 3
    00047 #define STATE_BLOCKED_CLOSE 4
    00048 #define STATE_BLOCKED_SEND 5
    00049 #define STATE_DATA_SENT 6
    00050
    00051 /*
    00052  * Return value of the buffering functions that indicates that a
    00053  * buffer was not filled by incoming data.
    00054  *
    00055  */
    00056 #define BUF_NOT_FULL 0
    00057 #define BUF_NOT_FOUND 0
    00058
    00059 /*
    00060  * Return value of the buffering functions that indicates that a
    00061  * buffer was completely filled by incoming data.
    00062  *
    00063  */
    00064 #define BUF_FULL 1
    00065
    00066 /*
    00067  * Return value of the buffering functions that indicates that an
    00068  * end-marker byte was found.
    00069  *
    00070  */
    00071 #define BUF_FOUND 2
    00072
    00073 /*---------------------------------------------------------------------------*/
    00074 static void
    00075 buf_setup(struct psock_buf *buf,
    00076           u8_t *bufptr, u16_t bufsize)
    00077 {
    00078   buf->ptr = bufptr;
    00079   buf->left = bufsize;
    00080 }
    00081 /*---------------------------------------------------------------------------*/
    00082 static u8_t
    00083 buf_bufdata(struct psock_buf *buf, u16_t len,
    00084             u8_t **dataptr, u16_t *datalen)
    00085 {
    00086   if(*datalen < buf->left) {
    00087     memcpy(buf->ptr, *dataptr, *datalen);
    00088     buf->ptr += *datalen;
    00089     buf->left -= *datalen;
    00090     *dataptr += *datalen;
    00091     *datalen = 0;
    00092     return BUF_NOT_FULL;
    00093   } else if(*datalen == buf->left) {
    00094     memcpy(buf->ptr, *dataptr, *datalen);
    00095     buf->ptr += *datalen;
    00096     buf->left = 0;
    00097     *dataptr += *datalen;
    00098     *datalen = 0;
    00099     return BUF_FULL;
    00100   } else {
    00101     memcpy(buf->ptr, *dataptr, buf->left);
    00102     buf->ptr += buf->left;
    00103     *datalen -= buf->left;
    00104     *dataptr += buf->left;
    00105     buf->left = 0;
    00106     return BUF_FULL;
    00107   }
    00108 }
    00109 /*---------------------------------------------------------------------------*/
    00110 static u8_t
    00111 buf_bufto(register struct psock_buf *buf, u8_t endmarker,
    00112           register u8_t **dataptr, register u16_t *datalen)
    00113 {
    00114   u8_t c;
    00115   while(buf->left > 0 && *datalen > 0) {
    00116     c = *buf->ptr = **dataptr;
    00117     ++*dataptr;
    00118     ++buf->ptr;
    00119     --*datalen;
    00120     --buf->left;
    00121
    00122     if(c == endmarker) {
    00123       return BUF_FOUND;
    00124     }
    00125   }
    00126
    00127   if(*datalen == 0) {
    00128     return BUF_NOT_FOUND;
    00129   }
    00130
    00131   while(*datalen > 0) {
    00132     c = **dataptr;
    00133     --*datalen;
    00134     ++*dataptr;
    00135
    00136     if(c == endmarker) {
    00137       return BUF_FOUND | BUF_FULL;
    00138     }
    00139   }
    00140
    00141   return BUF_FULL;
    00142 }
    00143 /*---------------------------------------------------------------------------*/
    00144 static char
    00145 send_data(register struct psock *s)
    00146 {
    00147   if(s->state != STATE_DATA_SENT || uip_rexmit()) {
    00148     if(s->sendlen > uip_mss()) {
    00149       uip_send(s->sendptr, uip_mss());
    00150     } else {
    00151       uip_send(s->sendptr, s->sendlen);
    00152     }
    00153     s->state = STATE_DATA_SENT;
    00154     return 1;
    00155   }
    00156   return 0;
    00157 }
    00158 /*---------------------------------------------------------------------------*/
    00159 static char
    00160 data_acked(register struct psock *s)
    00161 {
    00162   if(s->state == STATE_DATA_SENT && uip_acked()) {
    00163     if(s->sendlen > uip_mss()) {
    00164       s->sendlen -= uip_mss();
    00165       s->sendptr += uip_mss();
    00166     } else {
    00167       s->sendptr += s->sendlen;
    00168       s->sendlen = 0;
    00169     }
    00170     s->state = STATE_ACKED;
    00171     return 1;
    00172   }
    00173   return 0;
    00174 }
    00175 /*---------------------------------------------------------------------------*/
    00176 PT_THREAD(psock_send(register struct psock *s, const char *buf,
    00177                      unsigned int len))
    00178 {
    00179   PT_BEGIN(&s->psockpt);
    00180
    00181   /* If there is no data to send, we exit immediately. */
    00182   if(len == 0) {
    00183     PT_EXIT(&s->psockpt);
    00184   }
    00185
    00186   /* Save the length of and a pointer to the data that is to be
    00187      sent. */
    00188   s->sendptr = buf;
    00189   s->sendlen = len;
    00190
    00191   s->state = STATE_NONE;
    00192
    00193   /* We loop here until all data is sent. The s->sendlen variable is
    00194      updated by the data_sent() function. */
    00195   while(s->sendlen > 0) {
    00196
    00197     /*
    00198      * The condition for this PT_WAIT_UNTIL is a little tricky: the
    00199      * protothread will wait here until all data has been acknowledged
    00200      * (data_acked() returns true) and until all data has been sent
    00201      * (send_data() returns true). The two functions data_acked() and
    00202      * send_data() must be called in succession to ensure that all
    00203      * data is sent. Therefore the & operator is used instead of the
    00204      * && operator, which would cause only the data_acked() function
    00205      * to be called when it returns false.
    00206      */
    00207     PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s));
    00208   }
    00209
    00210   s->state = STATE_NONE;
    00211
    00212   PT_END(&s->psockpt);
    00213 }
    00214 /*---------------------------------------------------------------------------*/
    00215 PT_THREAD(psock_generator_send(register struct psock *s,
    00216                                unsigned short (*generate)(void *), void *arg))
    00217 {
    00218   PT_BEGIN(&s->psockpt);
    00219
    00220   /* Ensure that there is a generator function to call. */
    00221   if(generate == NULL) {
    00222     PT_EXIT(&s->psockpt);
    00223   }
    00224
    00225   /* Call the generator function to generate the data in the
    00226      uip_appdata buffer. */
    00227   s->sendlen = generate(arg);
    00228   s->sendptr = uip_appdata;
    00229
    00230   s->state = STATE_NONE;
    00231   do {
    00232     /* Call the generator function again if we are called to perform a
    00233        retransmission. */
    00234     if(uip_rexmit()) {
    00235       generate(arg);
    00236     }
    00237     /* Wait until all data is sent and acknowledged. */
    00238     PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s));
    00239   } while(s->sendlen > 0);
    00240
    00241   s->state = STATE_NONE;
    00242
    00243   PT_END(&s->psockpt);
    00244 }
    00245 /*---------------------------------------------------------------------------*/
    00246 u16_t
    00247 psock_datalen(struct psock *psock)
    00248 {
    00249   return psock->bufsize - psock->buf.left;
    00250 }
    00251 /*---------------------------------------------------------------------------*/
    00252 char
    00253 psock_newdata(struct psock *s)
    00254 {
    00255   if(s->readlen > 0) {
    00256     /* There is data in the uip_appdata buffer that has not yet been
    00257        read with the PSOCK_READ functions. */
    00258     return 1;
    00259   } else if(s->state == STATE_READ) {
    00260     /* All data in uip_appdata buffer already consumed. */
    00261     s->state = STATE_BLOCKED_NEWDATA;
    00262     return 0;
    00263   } else if(uip_newdata()) {
    00264     /* There is new data that has not been consumed. */
    00265     return 1;
    00266   } else {
    00267     /* There is no new data. */
    00268     return 0;
    00269   }
    00270 }
    00271 /*---------------------------------------------------------------------------*/
    00272 PT_THREAD(psock_readto(register struct psock *psock, unsigned char c))
    00273 {
    00274   PT_BEGIN(&psock->psockpt);
    00275
    00276   buf_setup(&psock->buf, psock->bufptr, psock->bufsize);
    00277
    00278   /* XXX: Should add buf_checkmarker() before do{} loop, if
    00279      incoming data has been handled while waiting for a write. */
    00280
    00281   do {
    00282     if(psock->readlen == 0) {
    00283       PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock));
    00284       psock->state = STATE_READ;
    00285       psock->readptr = (u8_t *)uip_appdata;
    00286       psock->readlen = uip_datalen();
    00287     }
    00288   } while((buf_bufto(&psock->buf, c,
    00289                      &psock->readptr,
    00290                      &psock->readlen) & BUF_FOUND) == 0);
    00291
    00292   if(psock_datalen(psock) == 0) {
    00293     psock->state = STATE_NONE;
    00294     PT_RESTART(&psock->psockpt);
    00295   }
    00296   PT_END(&psock->psockpt);
    00297 }
    00298 /*---------------------------------------------------------------------------*/
    00299 PT_THREAD(psock_readbuf(register struct psock *psock))
    00300 {
    00301   PT_BEGIN(&psock->psockpt);
    00302
    00303   buf_setup(&psock->buf, psock->bufptr, psock->bufsize);
    00304
    00305   /* XXX: Should add buf_checkmarker() before do{} loop, if
    00306      incoming data has been handled while waiting for a write. */
    00307
    00308   do {
    00309     if(psock->readlen == 0) {
    00310       PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock));
    00311       printf("Waited for newdata\n");
    00312       psock->state = STATE_READ;
    00313       psock->readptr = (u8_t *)uip_appdata;
    00314       psock->readlen = uip_datalen();
    00315     }
    00316   } while(buf_bufdata(&psock->buf, psock->bufsize,
    00317                          &psock->readptr,
    00318                          &psock->readlen) != BUF_FULL);
    00319
    00320   if(psock_datalen(psock) == 0) {
    00321     psock->state = STATE_NONE;
    00322     PT_RESTART(&psock->psockpt);
    00323   }
    00324   PT_END(&psock->psockpt);
    00325 }
    00326 /*---------------------------------------------------------------------------*/
    00327 void
    00328 psock_init(register struct psock *psock, char *buffer, unsigned int buffersize)
    00329 {
    00330   psock->state = STATE_NONE;
    00331   psock->readlen = 0;
    00332   psock->bufptr = buffer;
    00333   psock->bufsize = buffersize;
    00334   buf_setup(&psock->buf, buffer, buffersize);
    00335   PT_INIT(&psock->pt);
    00336   PT_INIT(&psock->psockpt);
    00337 }
    00338 /*---------------------------------------------------------------------------*/