These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
EMAC/uIP/uip/psock.c@0:bf7b9fba3924, 2011-03-20 (annotated)
- Committer:
- frank26080115
- Date:
- Sun Mar 20 05:38:56 2011 +0000
- Revision:
- 0:bf7b9fba3924
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frank26080115 | 0:bf7b9fba3924 | 1 | /* |
frank26080115 | 0:bf7b9fba3924 | 2 | * Copyright (c) 2004, Swedish Institute of Computer Science. |
frank26080115 | 0:bf7b9fba3924 | 3 | * All rights reserved. |
frank26080115 | 0:bf7b9fba3924 | 4 | * |
frank26080115 | 0:bf7b9fba3924 | 5 | * Redistribution and use in source and binary forms, with or without |
frank26080115 | 0:bf7b9fba3924 | 6 | * modification, are permitted provided that the following conditions |
frank26080115 | 0:bf7b9fba3924 | 7 | * are met: |
frank26080115 | 0:bf7b9fba3924 | 8 | * 1. Redistributions of source code must retain the above copyright |
frank26080115 | 0:bf7b9fba3924 | 9 | * notice, this list of conditions and the following disclaimer. |
frank26080115 | 0:bf7b9fba3924 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
frank26080115 | 0:bf7b9fba3924 | 11 | * notice, this list of conditions and the following disclaimer in the |
frank26080115 | 0:bf7b9fba3924 | 12 | * documentation and/or other materials provided with the distribution. |
frank26080115 | 0:bf7b9fba3924 | 13 | * 3. Neither the name of the Institute nor the names of its contributors |
frank26080115 | 0:bf7b9fba3924 | 14 | * may be used to endorse or promote products derived from this software |
frank26080115 | 0:bf7b9fba3924 | 15 | * without specific prior written permission. |
frank26080115 | 0:bf7b9fba3924 | 16 | * |
frank26080115 | 0:bf7b9fba3924 | 17 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
frank26080115 | 0:bf7b9fba3924 | 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
frank26080115 | 0:bf7b9fba3924 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
frank26080115 | 0:bf7b9fba3924 | 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
frank26080115 | 0:bf7b9fba3924 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
frank26080115 | 0:bf7b9fba3924 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
frank26080115 | 0:bf7b9fba3924 | 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
frank26080115 | 0:bf7b9fba3924 | 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
frank26080115 | 0:bf7b9fba3924 | 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
frank26080115 | 0:bf7b9fba3924 | 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
frank26080115 | 0:bf7b9fba3924 | 27 | * SUCH DAMAGE. |
frank26080115 | 0:bf7b9fba3924 | 28 | * |
frank26080115 | 0:bf7b9fba3924 | 29 | * This file is part of the uIP TCP/IP stack |
frank26080115 | 0:bf7b9fba3924 | 30 | * |
frank26080115 | 0:bf7b9fba3924 | 31 | * Author: Adam Dunkels <adam@sics.se> |
frank26080115 | 0:bf7b9fba3924 | 32 | * |
frank26080115 | 0:bf7b9fba3924 | 33 | * $Id: psock.c,v 1.2 2006/06/12 08:00:30 adam Exp $ |
frank26080115 | 0:bf7b9fba3924 | 34 | */ |
frank26080115 | 0:bf7b9fba3924 | 35 | |
frank26080115 | 0:bf7b9fba3924 | 36 | #include <stdio.h> |
frank26080115 | 0:bf7b9fba3924 | 37 | #include <string.h> |
frank26080115 | 0:bf7b9fba3924 | 38 | |
frank26080115 | 0:bf7b9fba3924 | 39 | #include "uipopt.h" |
frank26080115 | 0:bf7b9fba3924 | 40 | #include "psock.h" |
frank26080115 | 0:bf7b9fba3924 | 41 | #include "uip.h" |
frank26080115 | 0:bf7b9fba3924 | 42 | |
frank26080115 | 0:bf7b9fba3924 | 43 | #define STATE_NONE 0 |
frank26080115 | 0:bf7b9fba3924 | 44 | #define STATE_ACKED 1 |
frank26080115 | 0:bf7b9fba3924 | 45 | #define STATE_READ 2 |
frank26080115 | 0:bf7b9fba3924 | 46 | #define STATE_BLOCKED_NEWDATA 3 |
frank26080115 | 0:bf7b9fba3924 | 47 | #define STATE_BLOCKED_CLOSE 4 |
frank26080115 | 0:bf7b9fba3924 | 48 | #define STATE_BLOCKED_SEND 5 |
frank26080115 | 0:bf7b9fba3924 | 49 | #define STATE_DATA_SENT 6 |
frank26080115 | 0:bf7b9fba3924 | 50 | |
frank26080115 | 0:bf7b9fba3924 | 51 | /* |
frank26080115 | 0:bf7b9fba3924 | 52 | * Return value of the buffering functions that indicates that a |
frank26080115 | 0:bf7b9fba3924 | 53 | * buffer was not filled by incoming data. |
frank26080115 | 0:bf7b9fba3924 | 54 | * |
frank26080115 | 0:bf7b9fba3924 | 55 | */ |
frank26080115 | 0:bf7b9fba3924 | 56 | #define BUF_NOT_FULL 0 |
frank26080115 | 0:bf7b9fba3924 | 57 | #define BUF_NOT_FOUND 0 |
frank26080115 | 0:bf7b9fba3924 | 58 | |
frank26080115 | 0:bf7b9fba3924 | 59 | /* |
frank26080115 | 0:bf7b9fba3924 | 60 | * Return value of the buffering functions that indicates that a |
frank26080115 | 0:bf7b9fba3924 | 61 | * buffer was completely filled by incoming data. |
frank26080115 | 0:bf7b9fba3924 | 62 | * |
frank26080115 | 0:bf7b9fba3924 | 63 | */ |
frank26080115 | 0:bf7b9fba3924 | 64 | #define BUF_FULL 1 |
frank26080115 | 0:bf7b9fba3924 | 65 | |
frank26080115 | 0:bf7b9fba3924 | 66 | /* |
frank26080115 | 0:bf7b9fba3924 | 67 | * Return value of the buffering functions that indicates that an |
frank26080115 | 0:bf7b9fba3924 | 68 | * end-marker byte was found. |
frank26080115 | 0:bf7b9fba3924 | 69 | * |
frank26080115 | 0:bf7b9fba3924 | 70 | */ |
frank26080115 | 0:bf7b9fba3924 | 71 | #define BUF_FOUND 2 |
frank26080115 | 0:bf7b9fba3924 | 72 | |
frank26080115 | 0:bf7b9fba3924 | 73 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 74 | static void |
frank26080115 | 0:bf7b9fba3924 | 75 | buf_setup(struct psock_buf *buf, |
frank26080115 | 0:bf7b9fba3924 | 76 | u8_t *bufptr, u16_t bufsize) |
frank26080115 | 0:bf7b9fba3924 | 77 | { |
frank26080115 | 0:bf7b9fba3924 | 78 | buf->ptr = bufptr; |
frank26080115 | 0:bf7b9fba3924 | 79 | buf->left = bufsize; |
frank26080115 | 0:bf7b9fba3924 | 80 | } |
frank26080115 | 0:bf7b9fba3924 | 81 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 82 | static u8_t |
frank26080115 | 0:bf7b9fba3924 | 83 | buf_bufdata(struct psock_buf *buf, u16_t len, |
frank26080115 | 0:bf7b9fba3924 | 84 | u8_t **dataptr, u16_t *datalen) |
frank26080115 | 0:bf7b9fba3924 | 85 | { |
frank26080115 | 0:bf7b9fba3924 | 86 | ( void ) len; |
frank26080115 | 0:bf7b9fba3924 | 87 | if(*datalen < buf->left) { |
frank26080115 | 0:bf7b9fba3924 | 88 | memcpy(buf->ptr, *dataptr, *datalen); |
frank26080115 | 0:bf7b9fba3924 | 89 | buf->ptr += *datalen; |
frank26080115 | 0:bf7b9fba3924 | 90 | buf->left -= *datalen; |
frank26080115 | 0:bf7b9fba3924 | 91 | *dataptr += *datalen; |
frank26080115 | 0:bf7b9fba3924 | 92 | *datalen = 0; |
frank26080115 | 0:bf7b9fba3924 | 93 | return BUF_NOT_FULL; |
frank26080115 | 0:bf7b9fba3924 | 94 | } else if(*datalen == buf->left) { |
frank26080115 | 0:bf7b9fba3924 | 95 | memcpy(buf->ptr, *dataptr, *datalen); |
frank26080115 | 0:bf7b9fba3924 | 96 | buf->ptr += *datalen; |
frank26080115 | 0:bf7b9fba3924 | 97 | buf->left = 0; |
frank26080115 | 0:bf7b9fba3924 | 98 | *dataptr += *datalen; |
frank26080115 | 0:bf7b9fba3924 | 99 | *datalen = 0; |
frank26080115 | 0:bf7b9fba3924 | 100 | return BUF_FULL; |
frank26080115 | 0:bf7b9fba3924 | 101 | } else { |
frank26080115 | 0:bf7b9fba3924 | 102 | memcpy(buf->ptr, *dataptr, buf->left); |
frank26080115 | 0:bf7b9fba3924 | 103 | buf->ptr += buf->left; |
frank26080115 | 0:bf7b9fba3924 | 104 | *datalen -= buf->left; |
frank26080115 | 0:bf7b9fba3924 | 105 | *dataptr += buf->left; |
frank26080115 | 0:bf7b9fba3924 | 106 | buf->left = 0; |
frank26080115 | 0:bf7b9fba3924 | 107 | return BUF_FULL; |
frank26080115 | 0:bf7b9fba3924 | 108 | } |
frank26080115 | 0:bf7b9fba3924 | 109 | } |
frank26080115 | 0:bf7b9fba3924 | 110 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 111 | static u8_t |
frank26080115 | 0:bf7b9fba3924 | 112 | buf_bufto(register struct psock_buf *buf, u8_t endmarker, |
frank26080115 | 0:bf7b9fba3924 | 113 | register u8_t **dataptr, register u16_t *datalen) |
frank26080115 | 0:bf7b9fba3924 | 114 | { |
frank26080115 | 0:bf7b9fba3924 | 115 | u8_t c; |
frank26080115 | 0:bf7b9fba3924 | 116 | while(buf->left > 0 && *datalen > 0) { |
frank26080115 | 0:bf7b9fba3924 | 117 | c = *buf->ptr = **dataptr; |
frank26080115 | 0:bf7b9fba3924 | 118 | ++*dataptr; |
frank26080115 | 0:bf7b9fba3924 | 119 | ++buf->ptr; |
frank26080115 | 0:bf7b9fba3924 | 120 | --*datalen; |
frank26080115 | 0:bf7b9fba3924 | 121 | --buf->left; |
frank26080115 | 0:bf7b9fba3924 | 122 | |
frank26080115 | 0:bf7b9fba3924 | 123 | if(c == endmarker) { |
frank26080115 | 0:bf7b9fba3924 | 124 | return BUF_FOUND; |
frank26080115 | 0:bf7b9fba3924 | 125 | } |
frank26080115 | 0:bf7b9fba3924 | 126 | } |
frank26080115 | 0:bf7b9fba3924 | 127 | |
frank26080115 | 0:bf7b9fba3924 | 128 | if(*datalen == 0) { |
frank26080115 | 0:bf7b9fba3924 | 129 | return BUF_NOT_FOUND; |
frank26080115 | 0:bf7b9fba3924 | 130 | } |
frank26080115 | 0:bf7b9fba3924 | 131 | |
frank26080115 | 0:bf7b9fba3924 | 132 | while(*datalen > 0) { |
frank26080115 | 0:bf7b9fba3924 | 133 | c = **dataptr; |
frank26080115 | 0:bf7b9fba3924 | 134 | --*datalen; |
frank26080115 | 0:bf7b9fba3924 | 135 | ++*dataptr; |
frank26080115 | 0:bf7b9fba3924 | 136 | |
frank26080115 | 0:bf7b9fba3924 | 137 | if(c == endmarker) { |
frank26080115 | 0:bf7b9fba3924 | 138 | return BUF_FOUND | BUF_FULL; |
frank26080115 | 0:bf7b9fba3924 | 139 | } |
frank26080115 | 0:bf7b9fba3924 | 140 | } |
frank26080115 | 0:bf7b9fba3924 | 141 | |
frank26080115 | 0:bf7b9fba3924 | 142 | return BUF_FULL; |
frank26080115 | 0:bf7b9fba3924 | 143 | } |
frank26080115 | 0:bf7b9fba3924 | 144 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 145 | static char |
frank26080115 | 0:bf7b9fba3924 | 146 | send_data(register struct psock *s) |
frank26080115 | 0:bf7b9fba3924 | 147 | { |
frank26080115 | 0:bf7b9fba3924 | 148 | if(s->state != STATE_DATA_SENT || uip_rexmit()) { |
frank26080115 | 0:bf7b9fba3924 | 149 | if(s->sendlen > uip_mss()) { |
frank26080115 | 0:bf7b9fba3924 | 150 | uip_send(s->sendptr, uip_mss()); |
frank26080115 | 0:bf7b9fba3924 | 151 | } else { |
frank26080115 | 0:bf7b9fba3924 | 152 | uip_send(s->sendptr, s->sendlen); |
frank26080115 | 0:bf7b9fba3924 | 153 | } |
frank26080115 | 0:bf7b9fba3924 | 154 | s->state = STATE_DATA_SENT; |
frank26080115 | 0:bf7b9fba3924 | 155 | return 1; |
frank26080115 | 0:bf7b9fba3924 | 156 | } |
frank26080115 | 0:bf7b9fba3924 | 157 | return 0; |
frank26080115 | 0:bf7b9fba3924 | 158 | } |
frank26080115 | 0:bf7b9fba3924 | 159 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 160 | static char |
frank26080115 | 0:bf7b9fba3924 | 161 | data_acked(register struct psock *s) |
frank26080115 | 0:bf7b9fba3924 | 162 | { |
frank26080115 | 0:bf7b9fba3924 | 163 | if(s->state == STATE_DATA_SENT && uip_acked()) { |
frank26080115 | 0:bf7b9fba3924 | 164 | if(s->sendlen > uip_mss()) { |
frank26080115 | 0:bf7b9fba3924 | 165 | s->sendlen -= uip_mss(); |
frank26080115 | 0:bf7b9fba3924 | 166 | s->sendptr += uip_mss(); |
frank26080115 | 0:bf7b9fba3924 | 167 | } else { |
frank26080115 | 0:bf7b9fba3924 | 168 | s->sendptr += s->sendlen; |
frank26080115 | 0:bf7b9fba3924 | 169 | s->sendlen = 0; |
frank26080115 | 0:bf7b9fba3924 | 170 | } |
frank26080115 | 0:bf7b9fba3924 | 171 | s->state = STATE_ACKED; |
frank26080115 | 0:bf7b9fba3924 | 172 | return 1; |
frank26080115 | 0:bf7b9fba3924 | 173 | } |
frank26080115 | 0:bf7b9fba3924 | 174 | return 0; |
frank26080115 | 0:bf7b9fba3924 | 175 | } |
frank26080115 | 0:bf7b9fba3924 | 176 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 177 | PT_THREAD(psock_send(register struct psock *s, const char *buf, |
frank26080115 | 0:bf7b9fba3924 | 178 | unsigned int len)) |
frank26080115 | 0:bf7b9fba3924 | 179 | { |
frank26080115 | 0:bf7b9fba3924 | 180 | PT_BEGIN(&s->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 181 | |
frank26080115 | 0:bf7b9fba3924 | 182 | /* If there is no data to send, we exit immediately. */ |
frank26080115 | 0:bf7b9fba3924 | 183 | if(len == 0) { |
frank26080115 | 0:bf7b9fba3924 | 184 | PT_EXIT(&s->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 185 | } |
frank26080115 | 0:bf7b9fba3924 | 186 | |
frank26080115 | 0:bf7b9fba3924 | 187 | /* Save the length of and a pointer to the data that is to be |
frank26080115 | 0:bf7b9fba3924 | 188 | sent. */ |
frank26080115 | 0:bf7b9fba3924 | 189 | s->sendptr = (unsigned char*)buf; |
frank26080115 | 0:bf7b9fba3924 | 190 | s->sendlen = len; |
frank26080115 | 0:bf7b9fba3924 | 191 | |
frank26080115 | 0:bf7b9fba3924 | 192 | s->state = STATE_NONE; |
frank26080115 | 0:bf7b9fba3924 | 193 | |
frank26080115 | 0:bf7b9fba3924 | 194 | /* We loop here until all data is sent. The s->sendlen variable is |
frank26080115 | 0:bf7b9fba3924 | 195 | updated by the data_sent() function. */ |
frank26080115 | 0:bf7b9fba3924 | 196 | while(s->sendlen > 0) { |
frank26080115 | 0:bf7b9fba3924 | 197 | |
frank26080115 | 0:bf7b9fba3924 | 198 | /* |
frank26080115 | 0:bf7b9fba3924 | 199 | * The condition for this PT_WAIT_UNTIL is a little tricky: the |
frank26080115 | 0:bf7b9fba3924 | 200 | * protothread will wait here until all data has been acknowledged |
frank26080115 | 0:bf7b9fba3924 | 201 | * (data_acked() returns true) and until all data has been sent |
frank26080115 | 0:bf7b9fba3924 | 202 | * (send_data() returns true). The two functions data_acked() and |
frank26080115 | 0:bf7b9fba3924 | 203 | * send_data() must be called in succession to ensure that all |
frank26080115 | 0:bf7b9fba3924 | 204 | * data is sent. Therefore the & operator is used instead of the |
frank26080115 | 0:bf7b9fba3924 | 205 | * && operator, which would cause only the data_acked() function |
frank26080115 | 0:bf7b9fba3924 | 206 | * to be called when it returns false. |
frank26080115 | 0:bf7b9fba3924 | 207 | */ |
frank26080115 | 0:bf7b9fba3924 | 208 | PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s)); |
frank26080115 | 0:bf7b9fba3924 | 209 | } |
frank26080115 | 0:bf7b9fba3924 | 210 | |
frank26080115 | 0:bf7b9fba3924 | 211 | s->state = STATE_NONE; |
frank26080115 | 0:bf7b9fba3924 | 212 | |
frank26080115 | 0:bf7b9fba3924 | 213 | PT_END(&s->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 214 | } |
frank26080115 | 0:bf7b9fba3924 | 215 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 216 | PT_THREAD(psock_generator_send(register struct psock *s, |
frank26080115 | 0:bf7b9fba3924 | 217 | unsigned short (*generate)(void *), void *arg)) |
frank26080115 | 0:bf7b9fba3924 | 218 | { |
frank26080115 | 0:bf7b9fba3924 | 219 | PT_BEGIN(&s->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 220 | |
frank26080115 | 0:bf7b9fba3924 | 221 | /* Ensure that there is a generator function to call. */ |
frank26080115 | 0:bf7b9fba3924 | 222 | if(generate == NULL) { |
frank26080115 | 0:bf7b9fba3924 | 223 | PT_EXIT(&s->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 224 | } |
frank26080115 | 0:bf7b9fba3924 | 225 | |
frank26080115 | 0:bf7b9fba3924 | 226 | /* Call the generator function to generate the data in the |
frank26080115 | 0:bf7b9fba3924 | 227 | uip_appdata buffer. */ |
frank26080115 | 0:bf7b9fba3924 | 228 | s->sendlen = generate(arg); |
frank26080115 | 0:bf7b9fba3924 | 229 | s->sendptr = uip_appdata; |
frank26080115 | 0:bf7b9fba3924 | 230 | |
frank26080115 | 0:bf7b9fba3924 | 231 | s->state = STATE_NONE; |
frank26080115 | 0:bf7b9fba3924 | 232 | do { |
frank26080115 | 0:bf7b9fba3924 | 233 | /* Call the generator function again if we are called to perform a |
frank26080115 | 0:bf7b9fba3924 | 234 | retransmission. */ |
frank26080115 | 0:bf7b9fba3924 | 235 | if(uip_rexmit()) { |
frank26080115 | 0:bf7b9fba3924 | 236 | generate(arg); |
frank26080115 | 0:bf7b9fba3924 | 237 | } |
frank26080115 | 0:bf7b9fba3924 | 238 | /* Wait until all data is sent and acknowledged. */ |
frank26080115 | 0:bf7b9fba3924 | 239 | PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s)); |
frank26080115 | 0:bf7b9fba3924 | 240 | } while(s->sendlen > 0); |
frank26080115 | 0:bf7b9fba3924 | 241 | |
frank26080115 | 0:bf7b9fba3924 | 242 | s->state = STATE_NONE; |
frank26080115 | 0:bf7b9fba3924 | 243 | |
frank26080115 | 0:bf7b9fba3924 | 244 | PT_END(&s->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 245 | } |
frank26080115 | 0:bf7b9fba3924 | 246 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 247 | u16_t |
frank26080115 | 0:bf7b9fba3924 | 248 | psock_datalen(struct psock *psock) |
frank26080115 | 0:bf7b9fba3924 | 249 | { |
frank26080115 | 0:bf7b9fba3924 | 250 | return psock->bufsize - psock->buf.left; |
frank26080115 | 0:bf7b9fba3924 | 251 | } |
frank26080115 | 0:bf7b9fba3924 | 252 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 253 | char |
frank26080115 | 0:bf7b9fba3924 | 254 | psock_newdata(struct psock *s) |
frank26080115 | 0:bf7b9fba3924 | 255 | { |
frank26080115 | 0:bf7b9fba3924 | 256 | if(s->readlen > 0) { |
frank26080115 | 0:bf7b9fba3924 | 257 | /* There is data in the uip_appdata buffer that has not yet been |
frank26080115 | 0:bf7b9fba3924 | 258 | read with the PSOCK_READ functions. */ |
frank26080115 | 0:bf7b9fba3924 | 259 | return 1; |
frank26080115 | 0:bf7b9fba3924 | 260 | } else if(s->state == STATE_READ) { |
frank26080115 | 0:bf7b9fba3924 | 261 | /* All data in uip_appdata buffer already consumed. */ |
frank26080115 | 0:bf7b9fba3924 | 262 | s->state = STATE_BLOCKED_NEWDATA; |
frank26080115 | 0:bf7b9fba3924 | 263 | return 0; |
frank26080115 | 0:bf7b9fba3924 | 264 | } else if(uip_newdata()) { |
frank26080115 | 0:bf7b9fba3924 | 265 | /* There is new data that has not been consumed. */ |
frank26080115 | 0:bf7b9fba3924 | 266 | return 1; |
frank26080115 | 0:bf7b9fba3924 | 267 | } else { |
frank26080115 | 0:bf7b9fba3924 | 268 | /* There is no new data. */ |
frank26080115 | 0:bf7b9fba3924 | 269 | return 0; |
frank26080115 | 0:bf7b9fba3924 | 270 | } |
frank26080115 | 0:bf7b9fba3924 | 271 | } |
frank26080115 | 0:bf7b9fba3924 | 272 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 273 | PT_THREAD(psock_readto(register struct psock *psock, unsigned char c)) |
frank26080115 | 0:bf7b9fba3924 | 274 | { |
frank26080115 | 0:bf7b9fba3924 | 275 | PT_BEGIN(&psock->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 276 | |
frank26080115 | 0:bf7b9fba3924 | 277 | buf_setup(&psock->buf, (unsigned char*)psock->bufptr, psock->bufsize); |
frank26080115 | 0:bf7b9fba3924 | 278 | |
frank26080115 | 0:bf7b9fba3924 | 279 | /* XXX: Should add buf_checkmarker() before do{} loop, if |
frank26080115 | 0:bf7b9fba3924 | 280 | incoming data has been handled while waiting for a write. */ |
frank26080115 | 0:bf7b9fba3924 | 281 | |
frank26080115 | 0:bf7b9fba3924 | 282 | do { |
frank26080115 | 0:bf7b9fba3924 | 283 | if(psock->readlen == 0) { |
frank26080115 | 0:bf7b9fba3924 | 284 | PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock)); |
frank26080115 | 0:bf7b9fba3924 | 285 | psock->state = STATE_READ; |
frank26080115 | 0:bf7b9fba3924 | 286 | psock->readptr = (u8_t *)uip_appdata; |
frank26080115 | 0:bf7b9fba3924 | 287 | psock->readlen = uip_datalen(); |
frank26080115 | 0:bf7b9fba3924 | 288 | } |
frank26080115 | 0:bf7b9fba3924 | 289 | } while((buf_bufto(&psock->buf, c, |
frank26080115 | 0:bf7b9fba3924 | 290 | &psock->readptr, |
frank26080115 | 0:bf7b9fba3924 | 291 | &psock->readlen) & BUF_FOUND) == 0); |
frank26080115 | 0:bf7b9fba3924 | 292 | |
frank26080115 | 0:bf7b9fba3924 | 293 | if(psock_datalen(psock) == 0) { |
frank26080115 | 0:bf7b9fba3924 | 294 | psock->state = STATE_NONE; |
frank26080115 | 0:bf7b9fba3924 | 295 | PT_RESTART(&psock->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 296 | } |
frank26080115 | 0:bf7b9fba3924 | 297 | PT_END(&psock->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 298 | } |
frank26080115 | 0:bf7b9fba3924 | 299 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 300 | PT_THREAD(psock_readbuf(register struct psock *psock)) |
frank26080115 | 0:bf7b9fba3924 | 301 | { |
frank26080115 | 0:bf7b9fba3924 | 302 | PT_BEGIN(&psock->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 303 | |
frank26080115 | 0:bf7b9fba3924 | 304 | buf_setup(&psock->buf, (unsigned char * ) psock->bufptr, psock->bufsize); |
frank26080115 | 0:bf7b9fba3924 | 305 | |
frank26080115 | 0:bf7b9fba3924 | 306 | /* XXX: Should add buf_checkmarker() before do{} loop, if |
frank26080115 | 0:bf7b9fba3924 | 307 | incoming data has been handled while waiting for a write. */ |
frank26080115 | 0:bf7b9fba3924 | 308 | |
frank26080115 | 0:bf7b9fba3924 | 309 | do { |
frank26080115 | 0:bf7b9fba3924 | 310 | if(psock->readlen == 0) { |
frank26080115 | 0:bf7b9fba3924 | 311 | PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock)); |
frank26080115 | 0:bf7b9fba3924 | 312 | printf("Waited for newdata\n"); |
frank26080115 | 0:bf7b9fba3924 | 313 | psock->state = STATE_READ; |
frank26080115 | 0:bf7b9fba3924 | 314 | psock->readptr = (u8_t *)uip_appdata; |
frank26080115 | 0:bf7b9fba3924 | 315 | psock->readlen = uip_datalen(); |
frank26080115 | 0:bf7b9fba3924 | 316 | } |
frank26080115 | 0:bf7b9fba3924 | 317 | } while(buf_bufdata(&psock->buf, psock->bufsize, |
frank26080115 | 0:bf7b9fba3924 | 318 | &psock->readptr, |
frank26080115 | 0:bf7b9fba3924 | 319 | &psock->readlen) != BUF_FULL); |
frank26080115 | 0:bf7b9fba3924 | 320 | |
frank26080115 | 0:bf7b9fba3924 | 321 | if(psock_datalen(psock) == 0) { |
frank26080115 | 0:bf7b9fba3924 | 322 | psock->state = STATE_NONE; |
frank26080115 | 0:bf7b9fba3924 | 323 | PT_RESTART(&psock->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 324 | } |
frank26080115 | 0:bf7b9fba3924 | 325 | PT_END(&psock->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 326 | } |
frank26080115 | 0:bf7b9fba3924 | 327 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 328 | void |
frank26080115 | 0:bf7b9fba3924 | 329 | psock_init(register struct psock *psock, char *buffer, unsigned int buffersize) |
frank26080115 | 0:bf7b9fba3924 | 330 | { |
frank26080115 | 0:bf7b9fba3924 | 331 | psock->state = STATE_NONE; |
frank26080115 | 0:bf7b9fba3924 | 332 | psock->readlen = 0; |
frank26080115 | 0:bf7b9fba3924 | 333 | psock->bufptr = buffer; |
frank26080115 | 0:bf7b9fba3924 | 334 | psock->bufsize = buffersize; |
frank26080115 | 0:bf7b9fba3924 | 335 | buf_setup(&psock->buf, (unsigned char*) buffer, buffersize); |
frank26080115 | 0:bf7b9fba3924 | 336 | PT_INIT(&psock->pt); |
frank26080115 | 0:bf7b9fba3924 | 337 | PT_INIT(&psock->psockpt); |
frank26080115 | 0:bf7b9fba3924 | 338 | } |
frank26080115 | 0:bf7b9fba3924 | 339 | /*---------------------------------------------------------------------------*/ |