Morse Micro IoT SDK  2.10.4
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{
30#endif
31
32struct mmbuf_ops;
33
53struct mmbuf
54{
56 uint8_t *buf;
58 uint32_t buf_len;
60 uint32_t start_offset;
62 uint32_t data_len;
64 const struct mmbuf_ops *ops;
66 struct mmbuf *volatile next;
67};
68
71{
73 void (*free_mmbuf)(void *mmbuf);
74};
75
85static inline void mmbuf_init(struct mmbuf *mmbuf,
86 uint8_t *buf,
87 uint32_t buf_len,
88 uint32_t data_start_offset,
89 const struct mmbuf_ops *ops)
90{
91 memset(mmbuf, 0, sizeof(*mmbuf));
92 mmbuf->buf = buf;
93 mmbuf->buf_len = buf_len;
94 mmbuf->start_offset = data_start_offset;
95 mmbuf->ops = ops;
96}
97
109struct mmbuf *mmbuf_alloc_on_heap(uint32_t space_at_start, uint32_t space_at_end);
110
120struct mmbuf *mmbuf_make_copy_on_heap(struct mmbuf *original);
121
129
137static inline uint8_t *mmbuf_get_data_start(struct mmbuf *mmbuf)
138{
139 return mmbuf->buf + mmbuf->start_offset;
140}
141
149static inline uint8_t *mmbuf_get_data_end(struct mmbuf *mmbuf)
150{
152}
153
162static inline uint32_t mmbuf_get_data_length(struct mmbuf *mmbuf)
163{
164 return mmbuf->data_len;
165}
166
174static inline uint32_t mmbuf_available_space_at_start(struct mmbuf *mmbuf)
175{
176 return mmbuf->start_offset;
177}
178
186static inline uint32_t mmbuf_available_space_at_end(struct mmbuf *mmbuf)
187{
189}
190
204static inline uint8_t *mmbuf_prepend(struct mmbuf *mmbuf, uint32_t len)
205{
207 mmbuf->start_offset -= len;
208 mmbuf->data_len += len;
209 return mmbuf->buf + mmbuf->start_offset;
210}
211
223static inline void mmbuf_prepend_data(struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
224{
225 uint8_t *dest = mmbuf_prepend(mmbuf, len);
226 memcpy(dest, data, len);
227}
228
242static inline uint8_t *mmbuf_append(struct mmbuf *mmbuf, uint32_t len)
243{
244 uint8_t *ret = mmbuf_get_data_end(mmbuf);
246 mmbuf->data_len += len;
247 return ret;
248}
249
259static inline void mmbuf_append_data(struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
260{
261 uint8_t *dest = mmbuf_append(mmbuf, len);
262 memcpy(dest, data, len);
263}
264
273static inline uint8_t *mmbuf_remove_from_start(struct mmbuf *mmbuf, uint32_t len)
274{
275 uint8_t *ret;
276
277 if (mmbuf_get_data_length(mmbuf) < len)
278 {
279 return NULL;
280 }
281
283
284 mmbuf->start_offset += len;
285 mmbuf->data_len -= len;
286
287 return ret;
288}
289
298static inline uint8_t *mmbuf_remove_from_end(struct mmbuf *mmbuf, uint32_t len)
299{
300 uint8_t *ret;
301
302 if (mmbuf_get_data_length(mmbuf) < len)
303 {
304 return NULL;
305 }
306
307 ret = mmbuf_get_data_end(mmbuf) - len;
308
309 mmbuf->data_len -= len;
310
311 return ret;
312}
313
321static inline void mmbuf_truncate(struct mmbuf *mmbuf, uint32_t len)
322{
323 MMOSAL_ASSERT(len <= mmbuf->data_len);
324 mmbuf->data_len = len;
325}
326
327/* --------------------------------------------------------------------------------------------- */
328
331{
333 struct mmbuf *volatile head;
335 struct mmbuf *volatile tail;
337 volatile uint32_t len;
338};
339
341#define MMBUF_LIST_INIT { NULL, NULL, 0 }
342
349static inline void mmbuf_list_init(struct mmbuf_list *list)
350{
351 list->head = NULL;
352 list->tail = NULL;
353 list->len = 0;
354}
355
362void mmbuf_list_prepend(struct mmbuf_list *list, struct mmbuf *mmbuf);
363
370void mmbuf_list_append(struct mmbuf_list *list, struct mmbuf *mmbuf);
371
380bool mmbuf_list_remove(struct mmbuf_list *list, struct mmbuf *mmbuf);
381
389struct mmbuf *mmbuf_list_dequeue(struct mmbuf_list *list);
390
399
407static inline struct mmbuf *mmbuf_list_dequeue_all(struct mmbuf_list *list)
408{
409 struct mmbuf *head = list->head;
410 list->head = NULL;
411 list->tail = NULL;
412 list->len = 0;
413 return head;
414}
415
423static inline bool mmbuf_list_is_empty(struct mmbuf_list *list)
424{
425 return (list->head == NULL);
426}
427
435static inline struct mmbuf *mmbuf_list_peek(struct mmbuf_list *list)
436{
437 return list->head;
438}
439
447static inline struct mmbuf *mmbuf_list_peek_tail(struct mmbuf_list *list)
448{
449 return list->tail;
450}
451
457void mmbuf_list_clear(struct mmbuf_list *list);
458
459#ifdef __cplusplus
460}
461#endif
462
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:242
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:186
static uint32_t mmbuf_get_data_length(struct mmbuf *mmbuf)
Gets the length of the data currently in the mmbuf.
Definition: mmbuf.h:162
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:407
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:149
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:298
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:423
static uint8_t * mmbuf_remove_from_start(struct mmbuf *mmbuf, uint32_t len)
Remove data from the start of the mmbuf.
Definition: mmbuf.h:273
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:204
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:174
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:259
static struct mmbuf * mmbuf_list_peek_tail(struct mmbuf_list *list)
Returns the tail of the mmbuf list.
Definition: mmbuf.h:447
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:349
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:223
static void mmbuf_truncate(struct mmbuf *mmbuf, uint32_t len)
Truncate the mmbuf data to the given length.
Definition: mmbuf.h:321
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:137
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:85
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:435
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:934
char buf[1408]
Statically allocated buffer for HTTP GET request, just under 1 packet size.
Definition: sslclient.c:176
Structure that can be used as the head of a linked list of mmbufs that counts its length.
Definition: mmbuf.h:331
volatile uint32_t len
Length of the list.
Definition: mmbuf.h:337
struct mmbuf *volatile head
First mmbuf in the list.
Definition: mmbuf.h:333
struct mmbuf *volatile tail
Last mmbuf in the list.
Definition: mmbuf.h:335
Operations data structure for mmbuf.
Definition: mmbuf.h:71
void(* free_mmbuf)(void *mmbuf)
Free the given mmbuf.
Definition: mmbuf.h:73
Core mmbuf data structure.
Definition: mmbuf.h:54
struct mmbuf *volatile next
Pointer that can be used to construct linked lists.
Definition: mmbuf.h:66
uint32_t start_offset
Offset where actual data starts in the buffer.
Definition: mmbuf.h:60
const struct mmbuf_ops * ops
Reference to operations data structure for this mmbuf.
Definition: mmbuf.h:64
uint32_t buf_len
Length of the buffer.
Definition: mmbuf.h:58
uint8_t * buf
The buffer where data is stored.
Definition: mmbuf.h:56
uint32_t data_len
Length of actual data in the buffer.
Definition: mmbuf.h:62