Prusa MINI Firmware overview
pbuf.c File Reference
#include "lwip/opt.h"
#include "lwip/stats.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/pbuf.h"
#include "lwip/sys.h"
#include <string.h>

Macros

#define SIZEOF_STRUCT_PBUF   LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
 
#define PBUF_POOL_BUFSIZE_ALIGNED   LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
 
#define PBUF_POOL_IS_EMPTY()
 

Functions

struct pbufpbuf_alloc (pbuf_layer layer, u16_t length, pbuf_type type)
 
void pbuf_realloc (struct pbuf *p, u16_t new_len)
 
static u8_t pbuf_header_impl (struct pbuf *p, s16_t header_size_increment, u8_t force)
 
u8_t pbuf_header (struct pbuf *p, s16_t header_size_increment)
 
u8_t pbuf_header_force (struct pbuf *p, s16_t header_size_increment)
 
u8_t pbuf_free (struct pbuf *p)
 
u16_t pbuf_clen (const struct pbuf *p)
 
void pbuf_ref (struct pbuf *p)
 
void pbuf_cat (struct pbuf *h, struct pbuf *t)
 
void pbuf_chain (struct pbuf *h, struct pbuf *t)
 
struct pbufpbuf_dechain (struct pbuf *p)
 
err_t pbuf_copy (struct pbuf *p_to, const struct pbuf *p_from)
 
u16_t pbuf_copy_partial (const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
 
static const struct pbufpbuf_skip_const (const struct pbuf *in, u16_t in_offset, u16_t *out_offset)
 
struct pbufpbuf_skip (struct pbuf *in, u16_t in_offset, u16_t *out_offset)
 
err_t pbuf_take (struct pbuf *buf, const void *dataptr, u16_t len)
 
err_t pbuf_take_at (struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
 
struct pbufpbuf_coalesce (struct pbuf *p, pbuf_layer layer)
 
u8_t pbuf_get_at (const struct pbuf *p, u16_t offset)
 
int pbuf_try_get_at (const struct pbuf *p, u16_t offset)
 
void pbuf_put_at (struct pbuf *p, u16_t offset, u8_t data)
 
u16_t pbuf_memcmp (const struct pbuf *p, u16_t offset, const void *s2, u16_t n)
 
u16_t pbuf_memfind (const struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
 
u16_t pbuf_strstr (const struct pbuf *p, const char *substr)
 

Detailed Description

Packet buffer management

Macro Definition Documentation

◆ SIZEOF_STRUCT_PBUF

#define SIZEOF_STRUCT_PBUF   LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))

◆ PBUF_POOL_BUFSIZE_ALIGNED

#define PBUF_POOL_BUFSIZE_ALIGNED   LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)

◆ PBUF_POOL_IS_EMPTY

#define PBUF_POOL_IS_EMPTY ( )

Function Documentation

◆ pbuf_header_impl()

static u8_t pbuf_header_impl ( struct pbuf p,
s16_t  header_size_increment,
u8_t  force 
)
static

Adjusts the payload pointer to hide or reveal headers in the payload.

See also
pbuf_header.
Parameters
ppbuf to change the header size.
header_size_incrementNumber of bytes to increment header size.
forceAllow 'header_size_increment > 0' for PBUF_REF/PBUF_ROM types
Returns
non-zero on failure, zero on success.
570 {
571  u16_t type;
572  void *payload;
573  u16_t increment_magnitude;
574 
575  LWIP_ASSERT("p != NULL", p != NULL);
576  if ((header_size_increment == 0) || (p == NULL)) {
577  return 0;
578  }
579 
580  if (header_size_increment < 0) {
581  increment_magnitude = (u16_t)-header_size_increment;
582  /* Check that we aren't going to move off the end of the pbuf */
583  LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
584  } else {
585  increment_magnitude = (u16_t)header_size_increment;
586 #if 0
587  /* Can't assert these as some callers speculatively call
588  pbuf_header() to see if it's OK. Will return 1 below instead. */
589  /* Check that we've got the correct type of pbuf to work with */
590  LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
591  p->type == PBUF_RAM || p->type == PBUF_POOL);
592  /* Check that we aren't going to move off the beginning of the pbuf */
593  LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
594  (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
595 #endif
596  }
597 
598  type = p->type;
599  /* remember current payload pointer */
600  payload = p->payload;
601 
602  /* pbuf types containing payloads? */
603  if (type == PBUF_RAM || type == PBUF_POOL) {
604  /* set new payload pointer */
605  p->payload = (u8_t *)p->payload - header_size_increment;
606  /* boundary check fails? */
607  if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
609  ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
610  (void *)p->payload, (void *)((u8_t *)p + SIZEOF_STRUCT_PBUF)));
611  /* restore old payload pointer */
612  p->payload = payload;
613  /* bail out unsuccessfully */
614  return 1;
615  }
616  /* pbuf types referring to external payloads? */
617  } else if (type == PBUF_REF || type == PBUF_ROM) {
618  /* hide a header in the payload? */
619  if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
620  /* increase payload pointer */
621  p->payload = (u8_t *)p->payload - header_size_increment;
622  } else if ((header_size_increment > 0) && force) {
623  p->payload = (u8_t *)p->payload - header_size_increment;
624  } else {
625  /* cannot expand payload to front (yet!)
626  * bail out unsuccessfully */
627  return 1;
628  }
629  } else {
630  /* Unknown type */
631  LWIP_ASSERT("bad pbuf type", 0);
632  return 1;
633  }
634  /* modify pbuf length fields */
635  p->len += header_size_increment;
636  p->tot_len += header_size_increment;
637 
638  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
639  (void *)payload, (void *)p->payload, header_size_increment));
640 
641  return 0;
642 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pbuf_header()

u8_t pbuf_header ( struct pbuf p,
s16_t  header_size_increment 
)

Adjusts the payload pointer to hide or reveal headers in the payload.

Adjusts the ->payload pointer so that space for a header (dis)appears in the pbuf payload.

The ->payload, ->tot_len and ->len fields are adjusted.

Parameters
ppbuf to change the header size.
header_size_incrementNumber of bytes to increment header size which increases the size of the pbuf. New space is on the front. (Using a negative value decreases the header size.) If hdr_size_inc is 0, this function does nothing and returns successful.

PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so the call will fail. A check is made that the increase in header size does not move the payload pointer in front of the start of the buffer.

Returns
non-zero on failure, zero on success.
666 {
667  return pbuf_header_impl(p, header_size_increment, 0);
668 }
Here is the call graph for this function:

◆ pbuf_header_force()

u8_t pbuf_header_force ( struct pbuf p,
s16_t  header_size_increment 
)

Same as pbuf_header but does not check if 'header_size > 0' is allowed. This is used internally only, to allow PBUF_REF for RX.

676 {
677  return pbuf_header_impl(p, header_size_increment, 1);
678 }
Here is the call graph for this function:

◆ pbuf_clen()

u16_t pbuf_clen ( const struct pbuf p)

Count number of pbufs in a chain

Parameters
pfirst pbuf of chain
Returns
the number of pbufs in a chain
801 {
802  u16_t len;
803 
804  len = 0;
805  while (p != NULL) {
806  ++len;
807  p = p->next;
808  }
809  return len;
810 }

◆ pbuf_dechain()

struct pbuf* pbuf_dechain ( struct pbuf p)

Dechains the first pbuf from its succeeding pbufs in the chain.

Makes p->tot_len field equal to p->len.

Parameters
ppbuf to dechain
Returns
remainder of the pbuf chain, or NULL if it was de-allocated.
Note
May not be called on a packet queue.
900 {
901  struct pbuf *q;
902  u8_t tail_gone = 1;
903  /* tail */
904  q = p->next;
905  /* pbuf has successor in chain? */
906  if (q != NULL) {
907  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
908  LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
909  /* enforce invariant if assertion is disabled */
910  q->tot_len = p->tot_len - p->len;
911  /* decouple pbuf from remainder */
912  p->next = NULL;
913  /* total length of pbuf p is its own length only */
914  p->tot_len = p->len;
915  /* q is no longer referenced by p, free it */
916  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
917  tail_gone = pbuf_free(q);
918  if (tail_gone > 0) {
920  ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
921  }
922  /* return remaining tail or NULL if deallocated */
923  }
924  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
925  LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
926  return ((tail_gone > 0) ? NULL : q);
927 }
Here is the call graph for this function:

◆ pbuf_skip_const()

static const struct pbuf* pbuf_skip_const ( const struct pbuf in,
u16_t  in_offset,
u16_t out_offset 
)
static
1106 {
1107  u16_t offset_left = in_offset;
1108  const struct pbuf* q = in;
1109 
1110  /* get the correct pbuf */
1111  while ((q != NULL) && (q->len <= offset_left)) {
1112  offset_left -= q->len;
1113  q = q->next;
1114  }
1115  if (out_offset != NULL) {
1116  *out_offset = offset_left;
1117  }
1118  return q;
1119 }
Here is the caller graph for this function:

◆ pbuf_strstr()

u16_t pbuf_strstr ( const struct pbuf p,
const char *  substr 
)

Find occurrence of substr with length substr_len in pbuf p, start at offset start_offset WARNING: in contrast to strstr(), this one does not stop at the first \0 in the pbuf/source string!

Parameters
ppbuf to search, maximum length is 0xFFFE since 0xFFFF is used as return value 'not found'
substrstring to search for in p, maximum length is 0xFFFE
Returns
0xFFFF if substr was not found in p or the index where it was found
1432 {
1433  size_t substr_len;
1434  if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
1435  return 0xFFFF;
1436  }
1437  substr_len = strlen(substr);
1438  if (substr_len >= 0xFFFF) {
1439  return 0xFFFF;
1440  }
1441  return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
1442 }
Here is the call graph for this function:
pbuf_memfind
u16_t pbuf_memfind(const struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
Definition: pbuf.c:1404
PBUF_POOL
Definition: pbuf.h:123
S16_F
#define S16_F
Definition: arch.h:146
pbuf::len
u16_t len
Definition: pbuf.h:159
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
PBUF_ROM
Definition: pbuf.h:112
u16_t
uint16_t u16_t
Definition: arch.h:121
pbuf::tot_len
u16_t tot_len
Definition: pbuf.h:156
type
uint8_t type
Definition: UsbCore.h:184
pbuf::next
struct pbuf * next
Definition: pbuf.h:144
pbuf_free
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:715
SIZEOF_STRUCT_PBUF
#define SIZEOF_STRUCT_PBUF
Definition: pbuf.c:129
LWIP_DBG_TRACE
#define LWIP_DBG_TRACE
Definition: debug.h:83
NULL
#define NULL
Definition: usbd_def.h:53
LWIP_ERROR
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:135
u8_t
uint8_t u8_t
Definition: arch.h:119
if
if(size<=((png_alloc_size_t) -1) - ob)
Definition: pngwrite.c:2176
PBUF_DEBUG
#define PBUF_DEBUG
Definition: opt.h:2645
PBUF_RAM
Definition: pbuf.h:108
pbuf_header_impl
static u8_t pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force)
Definition: pbuf.c:569
pbuf::type
u8_t type
Definition: pbuf.h:162
pbuf
Definition: pbuf.h:142
LWIP_DEBUGF
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:164
PBUF_REF
Definition: pbuf.h:116
pbuf::payload
void * payload
Definition: pbuf.h:147