blob: 915c4a0444bb7a5b536b88b536152efbde1ba31c [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef _MSGB_H
2#define _MSGB_H
3
4/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
5 * All Rights Reserved
6 *
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.
11 *
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.
16 *
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.
20 *
21 */
22
23#include <stdint.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010024#include <osmocom/core/linuxlist.h>
25#include <osmocom/core/utils.h>
Harald Welteec8b4502010-02-20 20:34:29 +010026
Harald Welte652a7232010-07-22 21:55:24 +020027#define MSGB_DEBUG
28
Harald Welteec8b4502010-02-20 20:34:29 +010029struct msgb {
30 struct llist_head list;
31
Pablo Neira Ayuso29cbf612011-07-07 19:46:44 +020032
Harald Welteec8b4502010-02-20 20:34:29 +010033 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020034 /* FIXME: move them into the control buffer */
Harald Weltec5a0ded2011-07-18 16:59:27 +020035 union {
36 void *dst;
37 struct gsm_bts_trx *trx;
38 };
Harald Welteec8b4502010-02-20 20:34:29 +010039 struct gsm_lchan *lchan;
40
Harald Welte00096ac2010-03-01 12:55:15 +010041 /* the Layer1 header (if any) */
42 unsigned char *l1h;
Harald Welte3415d412010-02-21 19:03:41 +010043 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
Harald Welteec8b4502010-02-20 20:34:29 +010044 unsigned char *l2h;
Harald Welte3415d412010-02-21 19:03:41 +010045 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
Harald Welteec8b4502010-02-20 20:34:29 +010046 unsigned char *l3h;
Harald Welte3415d412010-02-21 19:03:41 +010047 /* the layer 4 header */
Harald Weltebb77c9d2010-04-30 14:26:12 +020048 unsigned char *l4h;
Harald Welteec8b4502010-02-20 20:34:29 +010049
Harald Welte074c9f92010-04-30 14:29:11 +020050 /* the 'control buffer', large enough to contain 5 pointers */
51 unsigned long cb[5];
52
Harald Welteec8b4502010-02-20 20:34:29 +010053 uint16_t data_len;
54 uint16_t len;
55
56 unsigned char *head;
57 unsigned char *tail;
58 unsigned char *data;
59 unsigned char _data[0];
60};
61
62extern struct msgb *msgb_alloc(uint16_t size, const char *name);
63extern void msgb_free(struct msgb *m);
64extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
65extern struct msgb *msgb_dequeue(struct llist_head *queue);
66extern void msgb_reset(struct msgb *m);
67
Harald Welte652a7232010-07-22 21:55:24 +020068#ifdef MSGB_DEBUG
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010069#include <osmocom/core/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +010070#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +010071 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +010072 } while(0)
73#else
74#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +020075#endif
76
Harald Weltefdd0a702010-03-01 22:30:51 +010077#define msgb_l1(m) ((void *)(m->l1h))
Harald Welteec8b4502010-02-20 20:34:29 +010078#define msgb_l2(m) ((void *)(m->l2h))
79#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebb77c9d2010-04-30 14:26:12 +020080#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010081
Harald Weltefdd0a702010-03-01 22:30:51 +010082static inline unsigned int msgb_l1len(const struct msgb *msgb)
83{
84 return msgb->tail - (uint8_t *)msgb_l1(msgb);
85}
86
Harald Welteec8b4502010-02-20 20:34:29 +010087static inline unsigned int msgb_l2len(const struct msgb *msgb)
88{
89 return msgb->tail - (uint8_t *)msgb_l2(msgb);
90}
91
92static inline unsigned int msgb_l3len(const struct msgb *msgb)
93{
94 return msgb->tail - (uint8_t *)msgb_l3(msgb);
95}
96
97static inline unsigned int msgb_headlen(const struct msgb *msgb)
98{
99 return msgb->len - msgb->data_len;
100}
Harald Welte652a7232010-07-22 21:55:24 +0200101
102static inline int msgb_tailroom(const struct msgb *msgb)
103{
104 return (msgb->head + msgb->data_len) - msgb->tail;
105}
106
107static inline int msgb_headroom(const struct msgb *msgb)
108{
109 return (msgb->data - msgb->head);
110}
111
Harald Welteec8b4502010-02-20 20:34:29 +0100112static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
113{
114 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100115 if (msgb_tailroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100116 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
117 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100118 msgb->tail += len;
119 msgb->len += len;
120 return tmp;
121}
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100122static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
123{
124 uint8_t *space = msgb_put(msgb, 1);
125 space[0] = word & 0xFF;
126}
127static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
128{
129 uint8_t *space = msgb_put(msgb, 2);
130 space[0] = word >> 8 & 0xFF;
131 space[1] = word & 0xFF;
132}
133static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
134{
135 uint8_t *space = msgb_put(msgb, 4);
136 space[0] = word >> 24 & 0xFF;
137 space[1] = word >> 16 & 0xFF;
138 space[2] = word >> 8 & 0xFF;
139 space[3] = word & 0xFF;
140}
141static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
142{
143 unsigned char *tmp = msgb->data;
144 msgb->data += len;
145 msgb->len -= len;
146 return tmp;
147}
148static inline uint8_t msgb_get_u8(struct msgb *msgb)
149{
150 uint8_t *space = msgb_get(msgb, 1);
151 return space[0];
152}
153static inline uint16_t msgb_get_u16(struct msgb *msgb)
154{
155 uint8_t *space = msgb_get(msgb, 2);
156 return space[0] << 8 | space[1];
157}
158static inline uint32_t msgb_get_u32(struct msgb *msgb)
159{
160 uint8_t *space = msgb_get(msgb, 4);
161 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
162}
Harald Welteec8b4502010-02-20 20:34:29 +0100163static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
164{
Harald Welte46a45a62010-11-09 13:41:48 +0100165 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100166 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
167 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100168 msgb->data -= len;
169 msgb->len += len;
170 return msgb->data;
171}
172static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
173{
174 msgb->len -= len;
175 return msgb->data += len;
176}
Harald Welteec8b4502010-02-20 20:34:29 +0100177
178/* increase the headroom of an empty msgb, reducing the tailroom */
179static inline void msgb_reserve(struct msgb *msg, int len)
180{
181 msg->data += len;
182 msg->tail += len;
183}
184
185static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
186 const char *name)
187{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200188 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100189
Harald Welteec8b4502010-02-20 20:34:29 +0100190 struct msgb *msg = msgb_alloc(size, name);
191 if (msg)
192 msgb_reserve(msg, headroom);
193 return msg;
194}
195
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200196/* non inline functions to ease binding */
197uint8_t *msgb_data(const struct msgb *msg);
198uint16_t msgb_length(const struct msgb *msg);
199
Harald Welte9e1f0602011-06-29 18:46:10 +0200200/* set the talloc context for msgb_alloc[_headroom] */
201void msgb_set_talloc_ctx(void *ctx);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200202
Harald Welteec8b4502010-02-20 20:34:29 +0100203#endif /* _MSGB_H */