These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers psock.c Source File

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   ( void ) len;
00087   if(*datalen < buf->left) {
00088     memcpy(buf->ptr, *dataptr, *datalen);
00089     buf->ptr += *datalen;
00090     buf->left -= *datalen;
00091     *dataptr += *datalen;
00092     *datalen = 0;
00093     return BUF_NOT_FULL;
00094   } else if(*datalen == buf->left) {
00095     memcpy(buf->ptr, *dataptr, *datalen);
00096     buf->ptr += *datalen;
00097     buf->left = 0;
00098     *dataptr += *datalen;
00099     *datalen = 0;
00100     return BUF_FULL;
00101   } else {
00102     memcpy(buf->ptr, *dataptr, buf->left);
00103     buf->ptr += buf->left;
00104     *datalen -= buf->left;
00105     *dataptr += buf->left;
00106     buf->left = 0;
00107     return BUF_FULL;
00108   }
00109 }
00110 /*---------------------------------------------------------------------------*/
00111 static u8_t
00112 buf_bufto(register struct psock_buf *buf, u8_t endmarker,
00113       register u8_t **dataptr, register u16_t *datalen)
00114 {
00115   u8_t c;
00116   while(buf->left > 0 && *datalen > 0) {
00117     c = *buf->ptr = **dataptr;
00118     ++*dataptr;
00119     ++buf->ptr;
00120     --*datalen;
00121     --buf->left;
00122     
00123     if(c == endmarker) {
00124       return BUF_FOUND;
00125     }
00126   }
00127 
00128   if(*datalen == 0) {
00129     return BUF_NOT_FOUND;
00130   }
00131 
00132   while(*datalen > 0) {
00133     c = **dataptr;
00134     --*datalen;
00135     ++*dataptr;
00136     
00137     if(c == endmarker) {
00138       return BUF_FOUND | BUF_FULL;
00139     }
00140   }
00141   
00142   return BUF_FULL;
00143 }
00144 /*---------------------------------------------------------------------------*/
00145 static char
00146 send_data(register struct psock *s)
00147 {
00148   if(s->state != STATE_DATA_SENT || uip_rexmit()) {
00149     if(s->sendlen > uip_mss()) {
00150       uip_send(s->sendptr, uip_mss());
00151     } else {
00152       uip_send(s->sendptr, s->sendlen);
00153     }
00154     s->state = STATE_DATA_SENT;
00155     return 1;
00156   }
00157   return 0;
00158 }
00159 /*---------------------------------------------------------------------------*/
00160 static char
00161 data_acked(register struct psock *s)
00162 {
00163   if(s->state == STATE_DATA_SENT && uip_acked()) {
00164     if(s->sendlen > uip_mss()) {
00165       s->sendlen -= uip_mss();
00166       s->sendptr += uip_mss();
00167     } else {
00168       s->sendptr += s->sendlen;
00169       s->sendlen = 0;
00170     }
00171     s->state = STATE_ACKED;
00172     return 1;
00173   }
00174   return 0;
00175 }
00176 /*---------------------------------------------------------------------------*/
00177 PT_THREAD(psock_send(register struct psock *s, const char *buf,
00178              unsigned int len))
00179 {
00180   PT_BEGIN(&s->psockpt);
00181 
00182   /* If there is no data to send, we exit immediately. */
00183   if(len == 0) {
00184     PT_EXIT(&s->psockpt);
00185   }
00186 
00187   /* Save the length of and a pointer to the data that is to be
00188      sent. */
00189   s->sendptr = (unsigned char*)buf;
00190   s->sendlen = len;
00191 
00192   s->state = STATE_NONE;
00193 
00194   /* We loop here until all data is sent. The s->sendlen variable is
00195      updated by the data_sent() function. */
00196   while(s->sendlen > 0) {
00197 
00198     /*
00199      * The condition for this PT_WAIT_UNTIL is a little tricky: the
00200      * protothread will wait here until all data has been acknowledged
00201      * (data_acked() returns true) and until all data has been sent
00202      * (send_data() returns true). The two functions data_acked() and
00203      * send_data() must be called in succession to ensure that all
00204      * data is sent. Therefore the & operator is used instead of the
00205      * && operator, which would cause only the data_acked() function
00206      * to be called when it returns false.
00207      */
00208     PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s));
00209   }
00210 
00211   s->state = STATE_NONE;
00212   
00213   PT_END(&s->psockpt);
00214 }
00215 /*---------------------------------------------------------------------------*/
00216 PT_THREAD(psock_generator_send(register struct psock *s,
00217                    unsigned short (*generate)(void *), void *arg))
00218 {
00219   PT_BEGIN(&s->psockpt);
00220 
00221   /* Ensure that there is a generator function to call. */
00222   if(generate == NULL) {
00223     PT_EXIT(&s->psockpt);
00224   }
00225 
00226   /* Call the generator function to generate the data in the
00227      uip_appdata buffer. */
00228   s->sendlen = generate(arg);
00229   s->sendptr = uip_appdata;
00230 
00231   s->state = STATE_NONE;  
00232   do {
00233     /* Call the generator function again if we are called to perform a
00234        retransmission. */
00235     if(uip_rexmit()) {
00236       generate(arg);
00237     }
00238     /* Wait until all data is sent and acknowledged. */
00239     PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s));
00240   } while(s->sendlen > 0);
00241   
00242   s->state = STATE_NONE;
00243   
00244   PT_END(&s->psockpt);
00245 }
00246 /*---------------------------------------------------------------------------*/
00247 u16_t
00248 psock_datalen(struct psock *psock)
00249 {
00250   return psock->bufsize - psock->buf.left;
00251 }
00252 /*---------------------------------------------------------------------------*/
00253 char
00254 psock_newdata(struct psock *s)
00255 {
00256   if(s->readlen > 0) {
00257     /* There is data in the uip_appdata buffer that has not yet been
00258        read with the PSOCK_READ functions. */
00259     return 1;
00260   } else if(s->state == STATE_READ) {
00261     /* All data in uip_appdata buffer already consumed. */
00262     s->state = STATE_BLOCKED_NEWDATA;
00263     return 0;
00264   } else if(uip_newdata()) {
00265     /* There is new data that has not been consumed. */
00266     return 1;
00267   } else {
00268     /* There is no new data. */
00269     return 0;
00270   }
00271 }
00272 /*---------------------------------------------------------------------------*/
00273 PT_THREAD(psock_readto(register struct psock *psock, unsigned char c))
00274 {
00275   PT_BEGIN(&psock->psockpt);
00276 
00277   buf_setup(&psock->buf, (unsigned char*)psock->bufptr, psock->bufsize);
00278   
00279   /* XXX: Should add buf_checkmarker() before do{} loop, if
00280      incoming data has been handled while waiting for a write. */
00281 
00282   do {
00283     if(psock->readlen == 0) {
00284       PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock));
00285       psock->state = STATE_READ;
00286       psock->readptr = (u8_t *)uip_appdata;
00287       psock->readlen = uip_datalen();
00288     }
00289   } while((buf_bufto(&psock->buf, c,
00290              &psock->readptr,
00291              &psock->readlen) & BUF_FOUND) == 0);
00292   
00293   if(psock_datalen(psock) == 0) {
00294     psock->state = STATE_NONE;
00295     PT_RESTART(&psock->psockpt);
00296   }
00297   PT_END(&psock->psockpt);
00298 }
00299 /*---------------------------------------------------------------------------*/
00300 PT_THREAD(psock_readbuf(register struct psock *psock))
00301 {
00302   PT_BEGIN(&psock->psockpt);
00303 
00304   buf_setup(&psock->buf, (unsigned char * ) psock->bufptr, psock->bufsize);
00305   
00306   /* XXX: Should add buf_checkmarker() before do{} loop, if
00307      incoming data has been handled while waiting for a write. */
00308 
00309   do {
00310     if(psock->readlen == 0) {
00311       PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock));
00312       printf("Waited for newdata\n");
00313       psock->state = STATE_READ;
00314       psock->readptr = (u8_t *)uip_appdata;
00315       psock->readlen = uip_datalen();
00316     }
00317   } while(buf_bufdata(&psock->buf, psock->bufsize,
00318              &psock->readptr,
00319              &psock->readlen) != BUF_FULL);
00320 
00321   if(psock_datalen(psock) == 0) {
00322     psock->state = STATE_NONE;
00323     PT_RESTART(&psock->psockpt);
00324   }
00325   PT_END(&psock->psockpt);
00326 }
00327 /*---------------------------------------------------------------------------*/
00328 void
00329 psock_init(register struct psock *psock, char *buffer, unsigned int buffersize)
00330 {
00331   psock->state = STATE_NONE;
00332   psock->readlen = 0;
00333   psock->bufptr = buffer;
00334   psock->bufsize = buffersize;
00335   buf_setup(&psock->buf, (unsigned char*) buffer, buffersize);
00336   PT_INIT(&psock->pt);
00337   PT_INIT(&psock->psockpt);
00338 }
00339 /*---------------------------------------------------------------------------*/