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 2008/08/06 23:21:19 andrewlxia Exp $
39 #include <uip/uipopt.h>
40 #include <uip/psock.h>
46 #define STATE_BLOCKED_NEWDATA 3
47 #define STATE_BLOCKED_CLOSE 4
48 #define STATE_BLOCKED_SEND 5
49 #define STATE_DATA_SENT 6
52 * Return value of the buffering functions that indicates that a
53 * buffer was not filled by incoming data.
56 #define BUF_NOT_FULL 0
57 #define BUF_NOT_FOUND 0
60 * Return value of the buffering functions that indicates that a
61 * buffer was completely filled by incoming data.
67 * Return value of the buffering functions that indicates that an
68 * end-marker byte was found.
73 /*---------------------------------------------------------------------------*/
75 buf_setup(struct psock_buf *buf,
76 u8_t *bufptr, u16_t bufsize)
81 /*---------------------------------------------------------------------------*/
83 buf_bufdata(struct psock_buf *buf, u16_t len,
84 u8_t **dataptr, u16_t *datalen)
86 if(*datalen < buf->left) {
87 memcpy(buf->ptr, *dataptr, *datalen);
89 buf->left -= *datalen;
93 } else if(*datalen == buf->left) {
94 memcpy(buf->ptr, *dataptr, *datalen);
101 memcpy(buf->ptr, *dataptr, buf->left);
102 buf->ptr += buf->left;
103 *datalen -= buf->left;
104 *dataptr += buf->left;
109 /*---------------------------------------------------------------------------*/
111 buf_bufto(register struct psock_buf *buf, u8_t endmarker,
112 register u8_t **dataptr, register u16_t *datalen)
115 while(buf->left > 0 && *datalen > 0) {
116 c = *buf->ptr = **dataptr;
128 return BUF_NOT_FOUND;
131 while(*datalen > 0) {
137 return BUF_FOUND | BUF_FULL;
143 /*---------------------------------------------------------------------------*/
145 send_data(register struct psock *s)
147 if(s->state != STATE_DATA_SENT || uip_rexmit()) {
148 if(s->sendlen > uip_mss()) {
149 uip_send(s->sendptr, uip_mss());
151 uip_send(s->sendptr, s->sendlen);
153 s->state = STATE_DATA_SENT;
158 /*---------------------------------------------------------------------------*/
160 data_acked(register struct psock *s)
162 if(s->state == STATE_DATA_SENT && uip_acked()) {
163 if(s->sendlen > uip_mss()) {
164 s->sendlen -= uip_mss();
165 s->sendptr += uip_mss();
167 s->sendptr += s->sendlen;
170 s->state = STATE_ACKED;
175 /*---------------------------------------------------------------------------*/
176 PT_THREAD(psock_send(register struct psock *s, const char *buf,
179 PT_BEGIN(&s->psockpt);
181 /* If there is no data to send, we exit immediately. */
183 PT_EXIT(&s->psockpt);
186 /* Save the length of and a pointer to the data that is to be
191 s->state = STATE_NONE;
193 /* We loop here until all data is sent. The s->sendlen variable is
194 updated by the data_sent() function. */
195 while(s->sendlen > 0) {
198 * The condition for this PT_WAIT_UNTIL is a little tricky: the
199 * protothread will wait here until all data has been acknowledged
200 * (data_acked() returns true) and until all data has been sent
201 * (send_data() returns true). The two functions data_acked() and
202 * send_data() must be called in succession to ensure that all
203 * data is sent. Therefore the & operator is used instead of the
204 * && operator, which would cause only the data_acked() function
205 * to be called when it returns false.
207 PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s));
210 s->state = STATE_NONE;
214 /*---------------------------------------------------------------------------*/
215 PT_THREAD(psock_generator_send(register struct psock *s,
216 unsigned short (*generate)(void *), void *arg))
218 PT_BEGIN(&s->psockpt);
220 /* Ensure that there is a generator function to call. */
221 if(generate == NULL) {
222 PT_EXIT(&s->psockpt);
225 /* Call the generator function to generate the data in the
226 uip_appdata buffer. */
227 s->sendlen = generate(arg);
228 s->sendptr = uip_appdata;
230 s->state = STATE_NONE;
232 /* Call the generator function again if we are called to perform a
237 /* Wait until all data is sent and acknowledged. */
238 PT_WAIT_UNTIL(&s->psockpt, data_acked(s) & send_data(s));
239 } while(s->sendlen > 0);
241 s->state = STATE_NONE;
245 /*---------------------------------------------------------------------------*/
247 psock_datalen(struct psock *psock)
249 return psock->bufsize - psock->buf.left;
251 /*---------------------------------------------------------------------------*/
253 psock_newdata(struct psock *s)
256 /* There is data in the uip_appdata buffer that has not yet been
257 read with the PSOCK_READ functions. */
259 } else if(s->state == STATE_READ) {
260 /* All data in uip_appdata buffer already consumed. */
261 s->state = STATE_BLOCKED_NEWDATA;
263 } else if(uip_newdata()) {
264 /* There is new data that has not been consumed. */
267 /* There is no new data. */
271 /*---------------------------------------------------------------------------*/
272 PT_THREAD(psock_readto(register struct psock *psock, unsigned char c))
274 PT_BEGIN(&psock->psockpt);
276 buf_setup(&psock->buf, psock->bufptr, psock->bufsize);
278 /* XXX: Should add buf_checkmarker() before do{} loop, if
279 incoming data has been handled while waiting for a write. */
282 if(psock->readlen == 0) {
283 PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock));
284 psock->state = STATE_READ;
285 psock->readptr = (u8_t *)uip_appdata;
286 psock->readlen = uip_datalen();
288 } while((buf_bufto(&psock->buf, c,
290 &psock->readlen) & BUF_FOUND) == 0);
292 if(psock_datalen(psock) == 0) {
293 psock->state = STATE_NONE;
294 PT_RESTART(&psock->psockpt);
296 PT_END(&psock->psockpt);
298 /*---------------------------------------------------------------------------*/
299 PT_THREAD(psock_readbuf(register struct psock *psock))
301 PT_BEGIN(&psock->psockpt);
303 buf_setup(&psock->buf, psock->bufptr, psock->bufsize);
305 /* XXX: Should add buf_checkmarker() before do{} loop, if
306 incoming data has been handled while waiting for a write. */
309 if(psock->readlen == 0) {
310 PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock));
311 //PrintDebug("Waited for newdata\n");
312 psock->state = STATE_READ;
313 psock->readptr = (u8_t *)uip_appdata;
314 psock->readlen = uip_datalen();
316 } while(buf_bufdata(&psock->buf, psock->bufsize,
318 &psock->readlen) != BUF_FULL);
320 if(psock_datalen(psock) == 0) {
321 psock->state = STATE_NONE;
322 PT_RESTART(&psock->psockpt);
324 PT_END(&psock->psockpt);
326 /*---------------------------------------------------------------------------*/
328 psock_init(register struct psock *psock, char *buffer, unsigned int buffersize)
330 psock->state = STATE_NONE;
332 psock->bufptr = buffer;
333 psock->bufsize = buffersize;
334 buf_setup(&psock->buf, buffer, buffersize);
336 PT_INIT(&psock->psockpt);
338 /*---------------------------------------------------------------------------*/