added psock function for reading specified number of bytes

This commit is contained in:
Joakim Eriksson 2011-03-09 02:40:53 -05:00
parent 2baa6cf1b5
commit c9af578eab
2 changed files with 35 additions and 15 deletions

View file

@ -286,31 +286,34 @@ PT_THREAD(psock_readto(CC_REGISTER_ARG struct psock *psock, unsigned char c))
PT_END(&psock->psockpt); 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); PT_BEGIN(&psock->psockpt);
buf_setup(&psock->buf, psock->bufptr, psock->bufsize); buf_setup(&psock->buf, psock->bufptr, psock->bufsize);
/* XXX: Should add buf_checkmarker() before do{} loop, if /* XXX: Should add buf_checkmarker() before do{} loop, if
incoming data has been handled while waiting for a write. */ incoming data has been handled while waiting for a write. */
if(psock->readlen == 0) { /* read len bytes or to end of data */
PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock)); do {
psock->state = STATE_READ; if(psock->readlen == 0) {
psock->readptr = (u8_t *)uip_appdata; PT_WAIT_UNTIL(&psock->psockpt, psock_newdata(psock));
psock->readlen = uip_datalen(); psock->state = STATE_READ;
} psock->readptr = (u8_t *)uip_appdata;
buf_bufdata(&psock->buf, psock->bufsize, psock->readlen = uip_datalen();
&psock->readptr, }
&psock->readlen); } while(buf_bufdata(&psock->buf, psock->bufsize,
&psock->readptr, &psock->readlen) == BUF_NOT_FULL &&
psock_datalen(psock) < len);
if(psock_datalen(psock) == 0) { if(psock_datalen(psock) == 0) {
psock->state = STATE_NONE; psock->state = STATE_NONE;
PT_RESTART(&psock->psockpt); PT_RESTART(&psock->psockpt);
} }
PT_END(&psock->psockpt); PT_END(&psock->psockpt);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
psock_init(CC_REGISTER_ARG struct psock *psock, psock_init(CC_REGISTER_ARG struct psock *psock,

View file

@ -241,7 +241,7 @@ PT_THREAD(psock_generator_send(struct psock *psock,
*/ */
#define PSOCK_CLOSE(psock) uip_close() #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. * Read data until the buffer is full.
* *
@ -255,7 +255,24 @@ PT_THREAD(psock_readbuf(struct psock *psock));
* \hideinitializer * \hideinitializer
*/ */
#define PSOCK_READBUF(psock) \ #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)); PT_THREAD(psock_readto(struct psock *psock, unsigned char c));
/** /**