4 /* (C) 2008 by Harald Welte <laforge@gnumonks.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <osmocom/core/linuxlist.h>
25 #include <osmocom/core/utils.h>
30 struct llist_head list;
32 /* routing information. */
35 /* Part of which TRX logical channel we were received / transmitted */
36 /* FIXME: move them into the control buffer */
37 struct gsm_bts_trx *trx;
38 struct gsm_lchan *lchan;
40 /* the Layer1 header (if any) */
42 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
44 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
46 /* the layer 4 header */
49 /* the 'control buffer', large enough to contain 5 pointers */
58 unsigned char _data[0];
61 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
62 extern void msgb_free(struct msgb *m);
63 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
64 extern struct msgb *msgb_dequeue(struct llist_head *queue);
65 extern void msgb_reset(struct msgb *m);
68 #include <osmocom/core/panic.h>
69 #define MSGB_ABORT(msg, fmt, args ...) do { \
70 osmo_panic("msgb(%p): " fmt, msg, ## args); \
73 #define MSGB_ABORT(msg, fmt, args ...)
76 #define msgb_l1(m) ((void *)(m->l1h))
77 #define msgb_l2(m) ((void *)(m->l2h))
78 #define msgb_l3(m) ((void *)(m->l3h))
79 #define msgb_sms(m) ((void *)(m->l4h))
81 static inline unsigned int msgb_l1len(const struct msgb *msgb)
83 return msgb->tail - (uint8_t *)msgb_l1(msgb);
86 static inline unsigned int msgb_l2len(const struct msgb *msgb)
88 return msgb->tail - (uint8_t *)msgb_l2(msgb);
91 static inline unsigned int msgb_l3len(const struct msgb *msgb)
93 return msgb->tail - (uint8_t *)msgb_l3(msgb);
96 static inline unsigned int msgb_headlen(const struct msgb *msgb)
98 return msgb->len - msgb->data_len;
101 static inline int msgb_tailroom(const struct msgb *msgb)
103 return (msgb->head + msgb->data_len) - msgb->tail;
106 static inline int msgb_headroom(const struct msgb *msgb)
108 return (msgb->data - msgb->head);
111 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
113 unsigned char *tmp = msgb->tail;
114 if (msgb_tailroom(msgb) < (int) len)
115 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
116 msgb_tailroom(msgb), len);
121 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
123 uint8_t *space = msgb_put(msgb, 1);
124 space[0] = word & 0xFF;
126 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
128 uint8_t *space = msgb_put(msgb, 2);
129 space[0] = word >> 8 & 0xFF;
130 space[1] = word & 0xFF;
132 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
134 uint8_t *space = msgb_put(msgb, 4);
135 space[0] = word >> 24 & 0xFF;
136 space[1] = word >> 16 & 0xFF;
137 space[2] = word >> 8 & 0xFF;
138 space[3] = word & 0xFF;
140 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
142 unsigned char *tmp = msgb->data;
147 static inline uint8_t msgb_get_u8(struct msgb *msgb)
149 uint8_t *space = msgb_get(msgb, 1);
152 static inline uint16_t msgb_get_u16(struct msgb *msgb)
154 uint8_t *space = msgb_get(msgb, 2);
155 return space[0] << 8 | space[1];
157 static inline uint32_t msgb_get_u32(struct msgb *msgb)
159 uint8_t *space = msgb_get(msgb, 4);
160 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
162 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
164 if (msgb_headroom(msgb) < (int) len)
165 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
166 msgb_headroom(msgb), len);
171 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
174 return msgb->data += len;
177 /* increase the headroom of an empty msgb, reducing the tailroom */
178 static inline void msgb_reserve(struct msgb *msg, int len)
184 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
187 osmo_static_assert(size > headroom, headroom_bigger);
189 struct msgb *msg = msgb_alloc(size, name);
191 msgb_reserve(msg, headroom);
195 /* non inline functions to ease binding */
196 uint8_t *msgb_data(const struct msgb *msg);
197 uint16_t msgb_length(const struct msgb *msg);
199 /* set the talloc context for msgb_alloc[_headroom] */
200 void msgb_set_talloc_ctx(void *ctx);