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_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,

View File

@ -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));
/**