LWIP can't Read Unsigned Byte ?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LWIP can't Read Unsigned Byte ?

1,055 Views
ramazan_kurban3
Contributor III

Hi. My application is Lwip tcp/ip client app. LPC1769 conntect to my server and server send unsigned byte array. Lwip can't read data that bigger than 128.

How can ı solve this problem.

Not: I modify pbuf_copy_partial function with char to int but dont affected.

MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);  // original

MEMCPY(&((uint8_t*)dataptr)[left], &((uint8_t*)p->payload)[offset], buf_copy_len); // changed

this is my tcp_recv callback function.

uint8_t rcvbuff[1024];

err_t
echo_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{

enet_recv_flag=1;


struct echo_state *es;
err_t ret_err;

LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct echo_state *)arg;


if (p == NULL)
{
/* remote host closed connection */
es->state = ES_CLOSING;
if(es->p == NULL)
{
/* we're done sending, close it */
echo_close(tpcb, es);
}
else
{
/* we're not done yet */
tcp_sent(tpcb, echo_sent);
echo_send(tpcb, es);
}
ret_err = ERR_OK;
}
else if(err != ERR_OK)
{
/* cleanup, for unkown reason */
if (p != NULL)
{
es->p = NULL;
pbuf_free(p);
}
ret_err = err;
}
else if(es->state == ES_ACCEPTED)
{
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
/* store reference to incoming pbuf (chain) */
es->p = p;
/* install send completion notifier */

memset(rcvbuff, 0, 1024);
pbuf_copy_partial(p, rcvbuff, 1024, 0);
DEBUGOUT("Receiveda Data is : %s\r\n", rcvbuff);
tcp_sent(tpcb, echo_sent);
echo_send(tpcb, es);
ret_err = ERR_OK;
}
else if (es->state == ES_RECEIVED)
{
/* read some more data */
if(es->p == NULL)
{
memset(rcvbuff, 0, 1024);
pbuf_copy_partial(p, rcvbuff, 1024, 0);

DEBUGOUT("Received Data is : %s\r\n", rcvbuff);
es->p = p;
tcp_sent(tpcb, echo_sent);
echo_send(tpcb, es);
}
else
{
struct pbuf *ptr;

/* chain pbufs to the end of what we recv'ed previously */
ptr = es->p;
pbuf_chain(ptr,p);
}
ret_err = ERR_OK;
}
else if(es->state == ES_CLOSING)
{
/* odd case, remote side closing twice, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
else
{
/* unkown es->state, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}

void
echo_error(void *arg, err_t err)
{
struct echo_state *es;

LWIP_UNUSED_ARG(err);

es = (struct echo_state *)arg;
if (es != NULL)
{
mem_free(es);
}
}

u16_t
pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
{
struct pbuf *p;
u16_t left;
u16_t buf_copy_len;
u16_t copied_total = 0;

LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);

left = 0;

if((buf == NULL) || (dataptr == NULL)) {
return 0;
}

/* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
for(p = buf; len != 0 && p != NULL; p = p->next) {
if ((offset != 0) && (offset >= p->len)) {
/* don't copy from this buffer -> on to the next */
offset -= p->len;
} else {
/* copy from this buffer. maybe only partially. */
buf_copy_len = p->len - offset;
if (buf_copy_len > len)
buf_copy_len = len;
/* copy the necessary parts of the buffer */
MEMCPY(&((uint8_t*)dataptr)[left], &((uint8_t*)p->payload)[offset], buf_copy_len);
copied_total += buf_copy_len;
left += buf_copy_len;
len -= buf_copy_len;
offset = 0;
}
}
return copied_total;
}

Labels (1)
0 Kudos
1 Reply

936 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi

Please be aware that the parameter 'len' of the pbuf_copy_partial.

pbuf_copy_partial()

u16_t pbuf_copy_partial(const struct pbuf buf,
void * dataptr,
u16_t len,
u16_t offset 
)

Copy (part of) the contents of a packet buffer to an application supplied buffer.

Parameters
bufthe pbuf from which to copy data
dataptrthe application supplied buffer
lenlength of data to copy (dataptr must be big enough). No more than buf->tot_len will be copied, irrespective of len
offsetoffset into the packet buffer from where to begin copying len bytes

Regards

Daniel

0 Kudos