From c9af578eab3aa625b4da814215cc029e6c522031 Mon Sep 17 00:00:00 2001 From: Joakim Eriksson Date: Wed, 9 Mar 2011 02:40:53 -0500 Subject: [PATCH] added psock function for reading specified number of bytes --- core/net/psock.c | 29 ++++++++++++++++------------- core/net/psock.h | 21 +++++++++++++++++++-- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/core/net/psock.c b/core/net/psock.c index 117618229..9d3d0781d 100644 --- a/core/net/psock.c +++ b/core/net/psock.c @@ -286,31 +286,34 @@ PT_THREAD(psock_readto(CC_REGISTER_ARG struct psock *psock, unsigned char c)) PT_END(&psock->psockpt); } /*---------------------------------------------------------------------------*/ -PT_THREAD(psock_readbuf(CC_REGISTER_ARG struct psock *psock)) +PT_THREAD(psock_readbuf_len(CC_REGISTER_ARG struct psock *psock, uint16_t len)) { PT_BEGIN(&psock->psockpt); buf_setup(&psock->buf, psock->bufptr, psock->bufsize); - + /* XXX: Should add buf_checkmarker() before do{} loop, if incoming data has been handled while waiting for a write. */ - - if(psock->readlen == 0) { - PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock)); - psock->state = STATE_READ; - psock->readptr = (u8_t *)uip_appdata; - psock->readlen = uip_datalen(); - } - buf_bufdata(&psock->buf, psock->bufsize, - &psock->readptr, - &psock->readlen); - + + /* read len bytes or to end of data */ + do { + if(psock->readlen == 0) { + PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock)); + psock->state = STATE_READ; + psock->readptr = (u8_t *)uip_appdata; + psock->readlen = uip_datalen(); + } + } while(buf_bufdata(&psock->buf, psock->bufsize, + &psock->readptr, &psock->readlen) == BUF_NOT_FULL && + psock_datalen(psock) < len); + if(psock_datalen(psock) == 0) { psock->state = STATE_NONE; PT_RESTART(&psock->psockpt); } PT_END(&psock->psockpt); } + /*---------------------------------------------------------------------------*/ void psock_init(CC_REGISTER_ARG struct psock *psock, diff --git a/core/net/psock.h b/core/net/psock.h index 1d3a7fd3f..bdd6e95b6 100644 --- a/core/net/psock.h +++ b/core/net/psock.h @@ -241,7 +241,7 @@ PT_THREAD(psock_generator_send(struct psock *psock, */ #define PSOCK_CLOSE(psock) uip_close() -PT_THREAD(psock_readbuf(struct psock *psock)); +PT_THREAD(psock_readbuf_len(struct psock *psock, uint16_t len)); /** * Read data until the buffer is full. * @@ -255,7 +255,24 @@ PT_THREAD(psock_readbuf(struct psock *psock)); * \hideinitializer */ #define PSOCK_READBUF(psock) \ - PT_WAIT_THREAD(&((psock)->pt), psock_readbuf(psock)) + PT_WAIT_THREAD(&((psock)->pt), psock_readbuf_len(psock, 1)) + + +/** + * Read data until at least len bytes have been read. + * + * This macro will block waiting for data and read the data into the + * input buffer specified with the call to PSOCK_INIT(). Data is read + * until the buffer is full or len bytes have been read. + * + * \param psock (struct psock *) A pointer to the protosocket from which + * data should be read. + * \param len (uint16_t) The minimum number of bytes to read. + * + * \hideinitializer + */ +#define PSOCK_READBUF_LEN(psock, len) \ + PT_WAIT_THREAD(&((psock)->pt), psock_readbuf_len(psock, len)) PT_THREAD(psock_readto(struct psock *psock, unsigned char c)); /**