[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lwip-users] Using tcp_write() outside tcp_recv callback
From: |
Jamie |
Subject: |
Re: [lwip-users] Using tcp_write() outside tcp_recv callback |
Date: |
Tue, 7 Apr 2020 15:08:13 -0700 (MST) |
Just following up on this, as I haven't had time to continue working on it
until today. To provide more context, I'll provide some code snippets and
commentary around the code. I don't imagine I'm too far off getting it
working, as I've not changed too much of the echo example source.
*My init function:*
init_PCB = tcp_new();
if (init_PCB != NULL) {
err_t err;
err = tcp_bind(init_PCB, IP_ADDR_ANY, 9999);
if (err == ERR_OK) {
init_PCB = tcp_listen(init_PCB);
tcp_accept(init_PCB, tcp_accept_callback);
} else {
memp_free(MEMP_TCP_PCB, init_PCB);
}
}
My accept callback: (void *arg, struct tcp_pcb *tempPCB, err_t err)
err_t ret_err;
struct tcp_package *tcpPackage;
global_PCB = tempPCB;
tcp_nagle_disable(global_PCB);
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(err);
tcp_setprio(global_PCB, TCP_PRIO_MIN);
tcpPackage = (struct tcp_package *)mem_malloc(sizeof(struct tcp_package));
if (tcpPackage != NULL) {
tcpPackage->state = TCP_STATE_ACCEPTED;
tcpPackage->pcb = global_PCB;
tcpPackage->retries = 0;
tcpPackage->p = NULL;
tcp_arg(global_PCB, tcpPackage);
tcp_recv(global_PCB, tcp_recv_callback);
tcp_err(global_PCB, tcp_err_callback);
ret_err = ERR_OK;
} else {
ret_err = ERR_MEM;
}
I'll leave the tcp_recv_callback function for the time being, as I'm
focusing on TX at the moment, and the RX side seems to work without issue.
In terms of sending data, I have an interrupt callback function, where some
data is processed (not shown below for simplicity). I package an unsigned
char buffer with data from two other buffers, separated by a ';' character.
Code shown below:
unsigned char bufferMain[32];
char buffer1[10];
char buffer2[10];
//Some processing of data; buffer1 and buffer2 are populated
snprintf(bufferMain, sizeof(bufferMain), "%s%s%s", buffer1, ";", buffer2);
//Register the sent callback
tcp_sent(global_PCB, tcp_sent_callback);
err_t err;
err = tcp_write(global_PCB, bufferMain, sizeof(bufferMain), 1);
err = tcp_output(global_PCB);
As mentioned in my original post, I have trace points throughout the code,
including the 'err' return values for tcp_write() and tcp_output() above.
When I run the code, I get 2-3 ERR_OK values from tcp_write, and data is
received by the remote CPU, but err then becomes -1, and (through another
trace point) I determine the error to be:
"tcp_write : could not allocate memory for pbuf copy size 32"
Here is my tcp_sent_callback: (void *arg, struct tcp_pcb *tpcb, u16_t len)
struct tcp_package *tcpPackage;
struct pbuf *ptr;
LWIP_UNUSED_ARGE(len);
tcpPackage = (struct tcp_package *)arg;
ptr = tcpPackage->p;
tcpPackage->retries = 0;
if (tcpPackage->p != NULL) {
tcp_sent(tpcb, tcp_sent_callback);
//I don't use the tcp_send function, so don't currently use this
functionality
//In theory I shouldn't need this functionality, as each tcp_write() is
for a single pbuf
//tcp_send)tpcb, tcpPackage);
} else {
if (tcpPackage->state == TCP_STATE_CLOSING) {
tcpClose(tpcb, tcpPackage);
}
uint8_t freePBUF;
do {
freePBUF = pbuf_free(ptr);
} while (freePBUF == 0);
}
return ERR_OK;
Is there anything obvious that I'm missing or have done incorrectly in the
above? In my mind, I've registered the sent callback, so after tcp_write()
and tcp_output(), the do-while pbuf_free() should deallocate memory, but it
looks like that may not be the case, given the "could not allocate memory
for pbuf copy size" error.
Any guidance would be greatly appreciated!
--
Sent from: http://lwip.100.n7.nabble.com/lwip-users-f3.html