Morse Micro IoT SDK  2.9.7
mmbuf.h
1/*
2 * Copyright 2024 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
19#pragma once
20
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdint.h>
24
25#include "mmosal.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31struct mmbuf_ops;
32
52struct mmbuf
53{
55 uint8_t *buf;
57 uint32_t buf_len;
59 uint32_t start_offset;
61 uint32_t data_len;
63 const struct mmbuf_ops *ops;
65 struct mmbuf *volatile next;
66};
67
70{
72 void (*free_mmbuf)(void *mmbuf);
73};
74
84static inline void mmbuf_init(struct mmbuf *mmbuf, uint8_t *buf, uint32_t buf_len,
85 uint32_t data_start_offset, const struct mmbuf_ops *ops)
86{
87 memset(mmbuf, 0, sizeof(*mmbuf));
88 mmbuf->buf = buf;
89 mmbuf->buf_len = buf_len;
90 mmbuf->start_offset = data_start_offset;
91 mmbuf->ops = ops;
92}
93
105struct mmbuf *mmbuf_alloc_on_heap(uint32_t space_at_start, uint32_t space_at_end);
106
116struct mmbuf *mmbuf_make_copy_on_heap(struct mmbuf *original);
117
125
133static inline uint8_t *mmbuf_get_data_start(struct mmbuf *mmbuf)
134{
135 return mmbuf->buf + mmbuf->start_offset;
136}
137
145static inline uint8_t *mmbuf_get_data_end(struct mmbuf *mmbuf)
146{
148}
149
158static inline uint32_t mmbuf_get_data_length(struct mmbuf *mmbuf)
159{
160 return mmbuf->data_len;
161}
162
170static inline uint32_t mmbuf_available_space_at_start(struct mmbuf *mmbuf)
171{
172 return mmbuf->start_offset;
173}
174
182static inline uint32_t mmbuf_available_space_at_end(struct mmbuf *mmbuf)
183{
185}
186
200static inline uint8_t *mmbuf_prepend(struct mmbuf *mmbuf, uint32_t len)
201{
203 mmbuf->start_offset -= len;
204 mmbuf->data_len += len;
205 return mmbuf->buf + mmbuf->start_offset;
206}
207
219static inline void mmbuf_prepend_data(struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
220{
221 uint8_t *dest = mmbuf_prepend(mmbuf, len);
222 memcpy(dest, data, len);
223}
224
238static inline uint8_t *mmbuf_append(struct mmbuf *mmbuf, uint32_t len)
239{
240 uint8_t *ret = mmbuf_get_data_end(mmbuf);
242 mmbuf->data_len += len;
243 return ret;
244}
245
255static inline void mmbuf_append_data(struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
256{
257 uint8_t *dest = mmbuf_append(mmbuf, len);
258 memcpy(dest, data, len);
259}
260
269static inline uint8_t *mmbuf_remove_from_start(struct mmbuf *mmbuf, uint32_t len)
270{
271 uint8_t *ret;
272
273 if (mmbuf_get_data_length(mmbuf) < len)
274 {
275 return NULL;
276 }
277
279
280 mmbuf->start_offset += len;
281 mmbuf->data_len -= len;
282
283 return ret;
284}
285
294static inline uint8_t *mmbuf_remove_from_end(struct mmbuf *mmbuf, uint32_t len)
295{
296 uint8_t *ret;
297
298 if (mmbuf_get_data_length(mmbuf) < len)
299 {
300 return NULL;
301 }
302
303 ret = mmbuf_get_data_end(mmbuf) - len;
304
305 mmbuf->data_len -= len;
306
307 return ret;
308}
309
317static inline void mmbuf_truncate(struct mmbuf *mmbuf, uint32_t len)
318{
319 MMOSAL_ASSERT(len <= mmbuf->data_len);
320 mmbuf->data_len = len;
321}
322
323/* --------------------------------------------------------------------------------------------- */
324
327{
329 struct mmbuf *volatile head;
331 struct mmbuf *volatile tail;
333 volatile uint32_t len;
334};
335
337#define MMBUF_LIST_INIT { NULL, NULL, 0 }
338
345static inline void mmbuf_list_init(struct mmbuf_list *list)
346{
347 list->head = NULL;
348 list->tail = NULL;
349 list->len = 0;
350}
351
358void mmbuf_list_prepend(struct mmbuf_list *list, struct mmbuf *mmbuf);
359
366void mmbuf_list_append(struct mmbuf_list *list, struct mmbuf *mmbuf);
367
376bool mmbuf_list_remove(struct mmbuf_list *list, struct mmbuf *mmbuf);
377
385struct mmbuf *mmbuf_list_dequeue(struct mmbuf_list *list);
386
395
403static inline struct mmbuf *mmbuf_list_dequeue_all(struct mmbuf_list *list)
404{
405 struct mmbuf *head = list->head;
406 list->head = NULL;
407 list->tail = NULL;
408 list->len = 0;
409 return head;
410}
411
419static inline bool mmbuf_list_is_empty(struct mmbuf_list *list)
420{
421 return (list->head == NULL);
422}
423
431static inline struct mmbuf *mmbuf_list_peek(struct mmbuf_list *list)
432{
433 return list->head;
434}
435
443static inline struct mmbuf *mmbuf_list_peek_tail(struct mmbuf_list *list)
444{
445 return list->tail;
446}
447
453void mmbuf_list_clear(struct mmbuf_list *list);
454
455#ifdef __cplusplus
456}
457#endif
458
static uint8_t * mmbuf_append(struct mmbuf *mmbuf, uint32_t len)
Reserves space immediately after the data currently in the given mmbuf and returns a pointer to this ...
Definition: mmbuf.h:238
static uint32_t mmbuf_available_space_at_end(struct mmbuf *mmbuf)
Returns the amount of space available for appending to the data in the buffer.
Definition: mmbuf.h:182
static uint32_t mmbuf_get_data_length(struct mmbuf *mmbuf)
Gets the length of the data currently in the mmbuf.
Definition: mmbuf.h:158
static struct mmbuf * mmbuf_list_dequeue_all(struct mmbuf_list *list)
Remove all mmbufs from the list and return as a linked list.
Definition: mmbuf.h:403
static uint8_t * mmbuf_get_data_end(struct mmbuf *mmbuf)
Gets a pointer to the end of the data in the mmbuf.
Definition: mmbuf.h:145
void mmbuf_list_prepend(struct mmbuf_list *list, struct mmbuf *mmbuf)
Add an mmbuf to the start of an mmbuf list.
struct mmbuf * mmbuf_list_dequeue_tail(struct mmbuf_list *list)
Remove the mmbuf at the tail of the list and return it.
static uint8_t * mmbuf_remove_from_end(struct mmbuf *mmbuf, uint32_t len)
Remove data from the end of the mmbuf.
Definition: mmbuf.h:294
void mmbuf_list_clear(struct mmbuf_list *list)
Free all the packets in the given list and reset the list to empty state.
static bool mmbuf_list_is_empty(struct mmbuf_list *list)
Checks whether the given mmbuf list is empty.
Definition: mmbuf.h:419
static uint8_t * mmbuf_remove_from_start(struct mmbuf *mmbuf, uint32_t len)
Remove data from the start of the mmbuf.
Definition: mmbuf.h:269
static uint8_t * mmbuf_prepend(struct mmbuf *mmbuf, uint32_t len)
Reserves space immediately before the data currently in the given mmbuf and returns a pointer to this...
Definition: mmbuf.h:200
struct mmbuf * mmbuf_alloc_on_heap(uint32_t space_at_start, uint32_t space_at_end)
Allocate a new mmbuf on the heap (using mmosal_malloc()).
static uint32_t mmbuf_available_space_at_start(struct mmbuf *mmbuf)
Returns the amount of space available for prepending to the data in the buffer.
Definition: mmbuf.h:170
bool mmbuf_list_remove(struct mmbuf_list *list, struct mmbuf *mmbuf)
Remove an mmbuf from an mmbuf list.
void mmbuf_release(struct mmbuf *mmbuf)
Release a reference to the given mmbuf.
static void mmbuf_append_data(struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
Appends the given data to the data already in the mmbuf.
Definition: mmbuf.h:255
static struct mmbuf * mmbuf_list_peek_tail(struct mmbuf_list *list)
Returns the tail of the mmbuf list.
Definition: mmbuf.h:443
static void mmbuf_list_init(struct mmbuf_list *list)
Initialization function for mmbuf_list, for cases where MMBUF_LIST_INIT cannot be used.
Definition: mmbuf.h:345
static void mmbuf_prepend_data(struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
Prepends the given data to the data already in the mmbuf.
Definition: mmbuf.h:219
static void mmbuf_truncate(struct mmbuf *mmbuf, uint32_t len)
Truncate the mmbuf data to the given length.
Definition: mmbuf.h:317
struct mmbuf * mmbuf_list_dequeue(struct mmbuf_list *list)
Remove the mmbuf at the head of the list and return it.
static uint8_t * mmbuf_get_data_start(struct mmbuf *mmbuf)
Gets a pointer to the start of the data in the mmbuf.
Definition: mmbuf.h:133
static void mmbuf_init(struct mmbuf *mmbuf, uint8_t *buf, uint32_t buf_len, uint32_t data_start_offset, const struct mmbuf_ops *ops)
Initialize an mmbuf header with the given values.
Definition: mmbuf.h:84
void mmbuf_list_append(struct mmbuf_list *list, struct mmbuf *mmbuf)
Add an mmbuf to the end of an mmbuf list.
static struct mmbuf * mmbuf_list_peek(struct mmbuf_list *list)
Returns the head of the mmbuf list.
Definition: mmbuf.h:431
struct mmbuf * mmbuf_make_copy_on_heap(struct mmbuf *original)
Make a copy of the given mmbuf.
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
Definition: mmosal.h:927
char buf[1408]
Statically allocated buffer for HTTP GET request, just under 1 packet size.
Definition: sslclient.c:177
Structure that can be used as the head of a linked list of mmbufs that counts its length.
Definition: mmbuf.h:327
volatile uint32_t len
Length of the list.
Definition: mmbuf.h:333
struct mmbuf *volatile head
First mmbuf in the list.
Definition: mmbuf.h:329
struct mmbuf *volatile tail
Last mmbuf in the list.
Definition: mmbuf.h:331
Operations data structure for mmbuf.
Definition: mmbuf.h:70
void(* free_mmbuf)(void *mmbuf)
Free the given mmbuf.
Definition: mmbuf.h:72
Core mmbuf data structure.
Definition: mmbuf.h:53
struct mmbuf *volatile next
Pointer that can be used to construct linked lists.
Definition: mmbuf.h:65
uint32_t start_offset
Offset where actual data starts in the buffer.
Definition: mmbuf.h:59
const struct mmbuf_ops * ops
Reference to operations data structure for this mmbuf.
Definition: mmbuf.h:63
uint32_t buf_len
Length of the buffer.
Definition: mmbuf.h:57
uint8_t * buf
The buffer where data is stored.
Definition: mmbuf.h:55
uint32_t data_len
Length of actual data in the buffer.
Definition: mmbuf.h:61