Skip to content

Commit 4a1e9d4

Browse files
moving free_pbuf_chain to an external file in order to be shared from udp and tcp
1 parent e0ce22c commit 4a1e9d4

File tree

4 files changed

+64
-45
lines changed

4 files changed

+64
-45
lines changed

libraries/lwIpWrapper/src/lwipClient.cpp

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern "C" {
55
#include "Arduino.h"
66

77
#include "lwipClient.h"
8+
#include "lwippbuf.h"
89
#include "CNetIf.h"
910
#include "utils.h"
1011
// FIXME understand hos to syncronize the interrupt thread and "userspace"
@@ -310,8 +311,10 @@ int lwipClient::read(uint8_t* buffer, size_t size) {
310311
arduino::lock();
311312
uint16_t copied = pbuf_copy_partial(this->tcp_info->pbuf_head, buffer, size, this->tcp_info->pbuf_offset);
312313

313-
this->free_pbuf_chain(copied);
314-
// __enable_irq();
314+
this->tcp_info->pbuf_head = free_pbuf_chain(this->tcp_info->pbuf_head, copied, &this->tcp_info->pbuf_offset);
315+
316+
// acknowledge the received data
317+
tcp_recved(this->tcp_info->pcb, copied);
315318
arduino::unlock();
316319

317320
return copied;
@@ -433,50 +436,11 @@ size_t lwipClient::read_until_token(
433436

434437
uint16_t copied = pbuf_copy_partial(this->tcp_info->pbuf_head, (uint8_t*)buffer, buf_copy_len, this->tcp_info->pbuf_offset);
435438

436-
this->free_pbuf_chain(copied);
437-
arduino::unlock();
438-
439-
return copied;
440-
}
441-
442-
void lwipClient::free_pbuf_chain(uint16_t copied) {
443-
arduino::lock();
444-
/*
445-
* free pbufs that have been copied, if copied == 0 we have an error
446-
* free the buffer chain starting from the head up to the last entire pbuf ingested
447-
* taking into account the previously not entirely consumed pbuf
448-
*/
449-
uint32_t tobefreed = 0;
450-
copied += this->tcp_info->pbuf_offset;
451-
452-
// in order to clean up the chain we need to find the pbuf in the last pbuf in the chain
453-
// that got completely consumed by the application, dechain it from it successor and delete the chain before it
454-
455-
struct pbuf *head = this->tcp_info->pbuf_head, *last=head, *prev=nullptr; // FIXME little optimization prev can be substituted by last->next
456-
457-
while(last!=nullptr && last->len + tobefreed <= copied) {
458-
tobefreed += last->len;
459-
prev = last;
460-
last = last->next;
461-
}
462-
463-
// dechain if we are not at the end of the chain (last == nullptr)
464-
// and if we haven't copied entirely the first pbuf (prev == nullptr) (head == last)
465-
// if we reached the end of the chain set the this pbuf pointer to nullptr
466-
if(prev != nullptr) {
467-
prev->next = nullptr;
468-
this->tcp_info->pbuf_head = last;
469-
}
470-
471-
// the chain that is referenced by head is detached by the one referenced by this->tcp_info->pbuf_head
472-
// free the chain if we haven't copied entirely the first pbuf (prev == nullptr)
473-
if(this->tcp_info->pbuf_head != head) {
474-
uint8_t refs = pbuf_free(head);
475-
}
476-
477-
this->tcp_info->pbuf_offset = copied - tobefreed; // This offset should be referenced to the first pbuf in queue
439+
this->tcp_info->pbuf_head = free_pbuf_chain(this->tcp_info->pbuf_head, copied, &this->tcp_info->pbuf_offset);
478440

479441
// acknowledge the received data
480442
tcp_recved(this->tcp_info->pcb, copied);
481443
arduino::unlock();
444+
445+
return copied;
482446
}

libraries/lwIpWrapper/src/lwipClient.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ class lwipClient : public arduino::Client {
104104
ip_addr_t _ip;
105105

106106
err_t connected_callback(struct tcp_pcb* tpcb, err_t err);
107-
void free_pbuf_chain(uint16_t copied);
108107
err_t recv_callback(struct tcp_pcb* tpcb, struct pbuf* p, err_t err);
109108

110109
friend err_t _lwip_tcp_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "lwippbuf.h"
2+
#include "utils.h"
3+
4+
struct pbuf* free_pbuf_chain(struct pbuf* p, uint16_t copied, uint16_t *offset) {
5+
arduino::lock();
6+
/*
7+
* free pbufs that have been copied, if copied == 0 we have an error
8+
* free the buffer chain starting from the head up to the last entire pbuf ingested
9+
* taking into account the previously not entirely consumed pbuf
10+
*/
11+
uint32_t tobefreed = 0;
12+
copied += *offset;
13+
14+
// in order to clean up the chain we need to find the pbuf in the last pbuf in the chain
15+
// that got completely consumed by the application, dechain it from it successor and delete the chain before it
16+
17+
struct pbuf *head = p, *last=head, *prev=nullptr; // FIXME little optimization prev can be substituted by last->next
18+
19+
while(last!=nullptr && last->len + tobefreed <= copied) {
20+
tobefreed += last->len;
21+
prev = last;
22+
last = last->next;
23+
}
24+
25+
// dechain if we are not at the end of the chain (last == nullptr)
26+
// and if we haven't copied entirely the first pbuf (prev == nullptr) (head == last)
27+
// if we reached the end of the chain set the this pbuf pointer to nullptr
28+
if(prev != nullptr) {
29+
prev->next = nullptr;
30+
p = last;
31+
}
32+
33+
// the chain that is referenced by head is detached by the one referenced by p
34+
// free the chain if we haven't copied entirely the first pbuf (prev == nullptr)
35+
if(p != head) {
36+
uint8_t refs = pbuf_free(head);
37+
}
38+
39+
*offset = copied - tobefreed; // This offset should be referenced to the first pbuf in queue
40+
41+
arduino::unlock();
42+
43+
return p;
44+
}

libraries/lwIpWrapper/src/lwippbuf.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include <lwip/include/lwip/pbuf.h>
3+
4+
/**
5+
* This function aim to free a pbuf chain that has been partially consumed.
6+
* @param p the head of the pbuf chain
7+
* @param copied the size that had been consumed in the last operation
8+
* @param offset the size that had been consumed in the previous operations,
9+
* this value will be updated with the ffset of the new head
10+
* @return the new pbuf head
11+
*/
12+
struct pbuf* free_pbuf_chain(struct pbuf* p, uint16_t copied, uint16_t *offset);

0 commit comments

Comments
 (0)