NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
os_mbuf.h
1/*
2 * SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF)
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * SPDX-FileContributor: 2019-2022 Espressif Systems (Shanghai) CO LTD
7 */
8/*
9 * Licensed to the Apache Software Foundation (ASF) under one
10 * or more contributor license agreements. See the NOTICE file
11 * distributed with this work for additional information
12 * regarding copyright ownership. The ASF licenses this file
13 * to you under the Apache License, Version 2.0 (the
14 * "License"); you may not use this file except in compliance
15 * with the License. You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing,
20 * software distributed under the License is distributed on an
21 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22 * KIND, either express or implied. See the License for the
23 * specific language governing permissions and limitations
24 * under the License.
25 */
26
27
36#ifndef _OS_MBUF_H
37#define _OS_MBUF_H
38
39#include "os.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
62
63 STAILQ_ENTRY(os_mbuf_pool) omp_next;
64};
65
66
74 uint16_t omp_len;
78 uint16_t omp_flags;
79
80 STAILQ_ENTRY(os_mbuf_pkthdr) omp_next;
81};
82
86struct os_mbuf {
90 uint8_t *om_data;
94 uint8_t om_flags;
102 uint16_t om_len;
103
108
109 SLIST_ENTRY(os_mbuf) om_next;
110
114 uint8_t om_databuf[0];
115};
116
120struct os_mqueue {
121 STAILQ_HEAD(, os_mbuf_pkthdr) mq_head;
123 struct ble_npl_event mq_ev;
124};
125
126/*
127 * Given a flag number, provide the mask for it
128 *
129 * @param __n The number of the flag in the mask
130 */
131#define OS_MBUF_F_MASK(__n) (1 << (__n))
132
133/*
134 * Checks whether a given mbuf is a packet header mbuf
135 *
136 * @param __om The mbuf to check
137 */
138#define OS_MBUF_IS_PKTHDR(__om) \
139 ((__om)->om_pkthdr_len >= sizeof (struct os_mbuf_pkthdr))
140
142#define OS_MBUF_PKTHDR(__om) ((struct os_mbuf_pkthdr *) \
143 (void *)((uint8_t *)&(__om)->om_data + sizeof(struct os_mbuf)))
144
146#define OS_MBUF_PKTHDR_TO_MBUF(__hdr) \
147 (struct os_mbuf *)(void *)((uint8_t *)(__hdr) - sizeof(struct os_mbuf))
148
153#define OS_MBUF_PKTLEN(__om) (OS_MBUF_PKTHDR(__om)->omp_len)
154
161#define OS_MBUF_DATA(__om, __type) \
162 (__type) ((__om)->om_data)
163
169#define OS_MBUF_USRHDR(om) \
170 (void *)((uint8_t *)om + sizeof (struct os_mbuf) + \
171 sizeof (struct os_mbuf_pkthdr))
172
178#define OS_MBUF_USRHDR_LEN(om) \
179 ((om)->om_pkthdr_len - sizeof (struct os_mbuf_pkthdr))
180
181
184/*
185 * Called by OS_MBUF_LEADINGSPACE() macro
186 */
187static inline uint16_t
188_os_mbuf_leadingspace(struct os_mbuf *om)
189{
190 uint16_t startoff;
191 uint16_t leadingspace;
192
193 startoff = 0;
194 if (OS_MBUF_IS_PKTHDR(om)) {
195 startoff = om->om_pkthdr_len;
196 }
197
198 leadingspace = (uint16_t) (OS_MBUF_DATA(om, uint8_t *) -
199 ((uint8_t *) &om->om_databuf[0] + startoff));
200
201 return (leadingspace);
202}
203
216#define OS_MBUF_LEADINGSPACE(__om) _os_mbuf_leadingspace(__om)
217
218
221/* Called by OS_MBUF_TRAILINGSPACE() macro. */
222static inline uint16_t
223_os_mbuf_trailingspace(struct os_mbuf *om)
224{
225 struct os_mbuf_pool *omp;
226
227 omp = om->om_omp;
228
229 return (&om->om_databuf[0] + omp->omp_databuf_len) -
230 (om->om_data + om->om_len);
231}
232
244#define OS_MBUF_TRAILINGSPACE(__om) _os_mbuf_trailingspace(__om)
245
246
247#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
265int r_os_mqueue_init(struct os_mqueue *mq, ble_npl_event_fn *ev_cb, void *arg);
266#define os_mqueue_init r_os_mqueue_init
267
268
276struct os_mbuf *r_os_mqueue_get(struct os_mqueue *);
277#define os_mqueue_get r_os_mqueue_get
288int r_os_mqueue_put(struct os_mqueue *, struct ble_npl_eventq *, struct os_mbuf *);
289#define os_mqueue_put r_os_mqueue_put
290
291
308int r_os_msys_register(struct os_mbuf_pool *);
309#define os_msys_register r_os_msys_register
310
311
321struct os_mbuf *r_os_msys_get(uint16_t dsize, uint16_t leadingspace);
322#define os_msys_get r_os_msys_get
326void r_os_msys_reset(void);
327#define os_msys_reset r_os_msys_reset
328
329
339struct os_mbuf *r_os_msys_get_pkthdr(uint16_t dsize, uint16_t user_hdr_len);
340#define os_msys_get_pkthdr r_os_msys_get_pkthdr
346int r_os_msys_count(void);
347#define os_msys_count r_os_msys_count
348
349
355int r_os_msys_num_free(void);
356#define os_msys_num_free r_os_msys_num_free
357
358
369int r_os_mbuf_pool_init(struct os_mbuf_pool *, struct os_mempool *mp,
370 uint16_t, uint16_t);
371#define os_mbuf_pool_init r_os_mbuf_pool_init
382struct os_mbuf *r_os_mbuf_get(struct os_mbuf_pool *omp, uint16_t);
383#define os_mbuf_get r_os_mbuf_get
392struct os_mbuf *r_os_mbuf_get_pkthdr(struct os_mbuf_pool *omp,
393 uint8_t pkthdr_len);
394#define os_mbuf_get_pkthdr r_os_mbuf_get_pkthdr
403struct os_mbuf *r_os_mbuf_dup(struct os_mbuf *m);
404#define os_mbuf_dup r_os_mbuf_dup
418struct os_mbuf *r_os_mbuf_off(const struct os_mbuf *om, int off,
419 uint16_t *out_off);
420#define os_mbuf_off r_os_mbuf_off
421
422/*
423 * Copy data from an mbuf chain starting "off" bytes from the beginning,
424 * continuing for "len" bytes, into the indicated buffer.
425 *
426 * @param m The mbuf chain to copy from
427 * @param off The offset into the mbuf chain to begin copying from
428 * @param len The length of the data to copy
429 * @param dst The destination buffer to copy into
430 *
431 * @return 0 on success;
432 * -1 if the mbuf does not contain enough data.
433 */
434int r_os_mbuf_copydata(const struct os_mbuf *m, int off, int len, void *dst);
435#define os_mbuf_copydata r_os_mbuf_copydata
436
437
450uint16_t r_os_mbuf_len(const struct os_mbuf *om);
451#define os_mbuf_len r_os_mbuf_len
452
453
463int r_os_mbuf_append(struct os_mbuf *m, const void *, uint16_t);
464#define os_mbuf_append r_os_mbuf_append
465
466
482int r_os_mbuf_appendfrom(struct os_mbuf *dst, const struct os_mbuf *src,
483 uint16_t src_off, uint16_t len);
484#define os_mbuf_appendfrom r_os_mbuf_appendfrom
493int r_os_mbuf_free(struct os_mbuf *mb);
494#define os_mbuf_free r_os_mbuf_free
495
496
505int r_os_mbuf_free_chain(struct os_mbuf *om);
506#define os_mbuf_free_chain r_os_mbuf_free_chain
507
508
518void r_os_mbuf_adj(struct os_mbuf *mp, int req_len);
519#define os_mbuf_adj r_os_mbuf_adj
520
521
522
537int r_os_mbuf_cmpf(const struct os_mbuf *om, int off, const void *data, int len);
538#define os_mbuf_cmpf r_os_mbuf_cmpf
539
540
560int r_os_mbuf_cmpm(const struct os_mbuf *om1, uint16_t offset1,
561 const struct os_mbuf *om2, uint16_t offset2,
562 uint16_t len);
563#define os_mbuf_cmpm r_os_mbuf_cmpm
579struct os_mbuf *r_os_mbuf_prepend(struct os_mbuf *om, int len);
580#define os_mbuf_prepend r_os_mbuf_prepend
592struct os_mbuf *r_os_mbuf_prepend_pullup(struct os_mbuf *om, uint16_t len);
593#define os_mbuf_prepend_pullup r_os_mbuf_prepend_pullup
608int r_os_mbuf_copyinto(struct os_mbuf *om, int off, const void *src, int len);
609#define os_mbuf_copyinto r_os_mbuf_copyinto
610
611
620void r_os_mbuf_concat(struct os_mbuf *first, struct os_mbuf *second);
621#define os_mbuf_concat r_os_mbuf_concat
622
623
624
638void *r_os_mbuf_extend(struct os_mbuf *om, uint16_t len);
639#define os_mbuf_extend r_os_mbuf_extend
656struct os_mbuf *r_os_mbuf_pullup(struct os_mbuf *om, uint16_t len);
657#define os_mbuf_pullup r_os_mbuf_pullup
658
667struct os_mbuf *r_os_mbuf_trim_front(struct os_mbuf *om);
668#define os_mbuf_trim_front r_os_mbuf_trim_front
682int r_os_mbuf_widen(struct os_mbuf *om, uint16_t off, uint16_t len);
683#define os_mbuf_widen r_os_mbuf_widen
684
685
686
704struct os_mbuf *r_os_mbuf_pack_chains(struct os_mbuf *m1, struct os_mbuf *m2);
705#define os_mbuf_pack_chains r_os_mbuf_pack_chains
706
707#else
725int os_mqueue_init(struct os_mqueue *mq, ble_npl_event_fn *ev_cb, void *arg);
726
734struct os_mbuf *os_mqueue_get(struct os_mqueue *);
735
746int os_mqueue_put(struct os_mqueue *, struct ble_npl_eventq *, struct os_mbuf *);
747
764int os_msys_register(struct os_mbuf_pool *);
765
775struct os_mbuf *os_msys_get(uint16_t dsize, uint16_t leadingspace);
776
780void os_msys_reset(void);
781
791struct os_mbuf *os_msys_get_pkthdr(uint16_t dsize, uint16_t user_hdr_len);
792
798int os_msys_count(void);
799
805int os_msys_num_free(void);
806
817int os_mbuf_pool_init(struct os_mbuf_pool *, struct os_mempool *mp,
818 uint16_t, uint16_t);
819
830struct os_mbuf *os_mbuf_get(struct os_mbuf_pool *omp, uint16_t);
831
840struct os_mbuf *os_mbuf_get_pkthdr(struct os_mbuf_pool *omp,
841 uint8_t pkthdr_len);
842
851struct os_mbuf *os_mbuf_dup(struct os_mbuf *m);
852
866struct os_mbuf *os_mbuf_off(const struct os_mbuf *om, int off,
867 uint16_t *out_off);
868
869
870/*
871 * Copy data from an mbuf chain starting "off" bytes from the beginning,
872 * continuing for "len" bytes, into the indicated buffer.
873 *
874 * @param m The mbuf chain to copy from
875 * @param off The offset into the mbuf chain to begin copying from
876 * @param len The length of the data to copy
877 * @param dst The destination buffer to copy into
878 *
879 * @return 0 on success;
880 * -1 if the mbuf does not contain enough data.
881 */
882int os_mbuf_copydata(const struct os_mbuf *m, int off, int len, void *dst);
883
896uint16_t os_mbuf_len(const struct os_mbuf *om);
897
907int os_mbuf_append(struct os_mbuf *m, const void *, uint16_t);
908
924int os_mbuf_appendfrom(struct os_mbuf *dst, const struct os_mbuf *src,
925 uint16_t src_off, uint16_t len);
926
935int os_mbuf_free(struct os_mbuf *mb);
936
945int os_mbuf_free_chain(struct os_mbuf *om);
946
956void os_mbuf_adj(struct os_mbuf *mp, int req_len);
957
958
973int os_mbuf_cmpf(const struct os_mbuf *om, int off, const void *data, int len);
974
994int os_mbuf_cmpm(const struct os_mbuf *om1, uint16_t offset1,
995 const struct os_mbuf *om2, uint16_t offset2,
996 uint16_t len);
997
1013struct os_mbuf *os_mbuf_prepend(struct os_mbuf *om, int len);
1014
1026struct os_mbuf *os_mbuf_prepend_pullup(struct os_mbuf *om, uint16_t len);
1027
1042int os_mbuf_copyinto(struct os_mbuf *om, int off, const void *src, int len);
1043
1052void os_mbuf_concat(struct os_mbuf *first, struct os_mbuf *second);
1053
1054
1068void *os_mbuf_extend(struct os_mbuf *om, uint16_t len);
1069
1086struct os_mbuf *os_mbuf_pullup(struct os_mbuf *om, uint16_t len);
1087
1088
1097struct os_mbuf *os_mbuf_trim_front(struct os_mbuf *om);
1098
1112int os_mbuf_widen(struct os_mbuf *om, uint16_t off, uint16_t len);
1113
1114
1132struct os_mbuf *os_mbuf_pack_chains(struct os_mbuf *m1, struct os_mbuf *m2);
1133
1134#endif
1135#ifdef __cplusplus
1136}
1137#endif
1138
1139#endif /* _OS_MBUF_H */
1140
1141
struct os_mbuf * os_mbuf_trim_front(struct os_mbuf *om)
Definition os_mbuf.c:1044
struct os_mbuf * os_mqueue_get(struct os_mqueue *)
Definition os_mbuf.c:77
struct os_mbuf * os_mbuf_pack_chains(struct os_mbuf *m1, struct os_mbuf *m2)
Definition os_mbuf.c:1176
struct os_mbuf * os_mbuf_off(const struct os_mbuf *om, int off, uint16_t *out_off)
Definition os_mbuf.c:543
int os_mbuf_cmpm(const struct os_mbuf *om1, uint16_t offset1, const struct os_mbuf *om2, uint16_t offset2, uint16_t len)
Definition os_mbuf.c:723
int os_msys_register(struct os_mbuf_pool *)
Definition os_mbuf.c:129
struct os_mbuf * os_mbuf_get(struct os_mbuf_pool *omp, uint16_t)
Definition os_mbuf.c:251
int os_mbuf_pool_init(struct os_mbuf_pool *, struct os_mempool *mp, uint16_t, uint16_t)
Definition os_mbuf.c:241
struct os_mbuf * os_mbuf_dup(struct os_mbuf *m)
Definition os_mbuf.c:499
int os_mbuf_free(struct os_mbuf *mb)
Definition os_mbuf.c:314
void * os_mbuf_extend(struct os_mbuf *om, uint16_t len)
Definition os_mbuf.c:941
#define OS_MBUF_DATA(__om, __type)
Definition os_mbuf.h:161
int os_mqueue_init(struct os_mqueue *mq, ble_npl_event_fn *ev_cb, void *arg)
int os_mbuf_copyinto(struct os_mbuf *om, int off, const void *src, int len)
Definition os_mbuf.c:848
void os_mbuf_concat(struct os_mbuf *first, struct os_mbuf *second)
Definition os_mbuf.c:905
int os_mbuf_appendfrom(struct os_mbuf *dst, const struct os_mbuf *src, uint16_t src_off, uint16_t len)
Definition os_mbuf.c:470
int os_mbuf_free_chain(struct os_mbuf *om)
Definition os_mbuf.c:335
int os_mbuf_append(struct os_mbuf *m, const void *, uint16_t)
Definition os_mbuf.c:395
uint16_t os_mbuf_len(const struct os_mbuf *om)
Calculates the length of an mbuf chain.
Definition os_mbuf.c:379
struct os_mbuf * os_mbuf_prepend_pullup(struct os_mbuf *om, uint16_t len)
Definition os_mbuf.c:832
int os_msys_count(void)
Definition os_mbuf.c:212
struct os_mbuf * os_msys_get(uint16_t dsize, uint16_t leadingspace)
Definition os_mbuf.c:176
int os_mbuf_widen(struct os_mbuf *om, uint16_t off, uint16_t len)
Definition os_mbuf.c:1089
struct os_mbuf * os_mbuf_get_pkthdr(struct os_mbuf_pool *omp, uint8_t pkthdr_len)
Definition os_mbuf.c:281
void os_msys_reset(void)
Definition os_mbuf.c:150
int os_mqueue_put(struct os_mqueue *, struct ble_npl_eventq *, struct os_mbuf *)
Definition os_mbuf.c:100
int os_mbuf_cmpf(const struct os_mbuf *om, int off, const void *data, int len)
Definition os_mbuf.c:681
struct os_mbuf * os_mbuf_pullup(struct os_mbuf *om, uint16_t len)
Definition os_mbuf.c:979
void os_mbuf_adj(struct os_mbuf *mp, int req_len)
Definition os_mbuf.c:605
struct os_mbuf * os_msys_get_pkthdr(uint16_t dsize, uint16_t user_hdr_len)
Definition os_mbuf.c:193
int os_msys_num_free(void)
Definition os_mbuf.c:226
struct os_mbuf * os_mbuf_prepend(struct os_mbuf *om, int len)
Definition os_mbuf.c:779
Definition os_mbuf.h:70
uint16_t omp_len
Definition os_mbuf.h:74
uint16_t omp_flags
Definition os_mbuf.h:78
Definition os_mbuf.h:52
uint16_t omp_databuf_len
Definition os_mbuf.h:57
struct os_mempool * omp_pool
Definition os_mbuf.h:61
Definition os_mbuf.h:86
struct os_mbuf_pool * om_omp
Definition os_mbuf.h:107
uint8_t * om_data
Definition os_mbuf.h:90
uint8_t om_databuf[0]
Definition os_mbuf.h:114
uint8_t om_flags
Definition os_mbuf.h:94
uint16_t om_len
Definition os_mbuf.h:102
uint8_t om_pkthdr_len
Definition os_mbuf.h:98
Definition os_mempool.h:57
Definition os_mbuf.h:120
struct ble_npl_event mq_ev
Definition os_mbuf.h:123