blob: c4f443058ae93b7a714444cc97293f420049dd37 [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>
24#include "linuxlist.h"
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +010025#include "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
Harald Welteec8b4502010-02-20 20:34:29 +010032 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020033 /* FIXME: move them into the control buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010034 struct gsm_bts_trx *trx;
35 struct gsm_lchan *lchan;
36
Harald Welte00096ac2010-03-01 12:55:15 +010037 /* the Layer1 header (if any) */
38 unsigned char *l1h;
Harald Welte3415d412010-02-21 19:03:41 +010039 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
Harald Welteec8b4502010-02-20 20:34:29 +010040 unsigned char *l2h;
Harald Welte3415d412010-02-21 19:03:41 +010041 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
Harald Welteec8b4502010-02-20 20:34:29 +010042 unsigned char *l3h;
Harald Welte3415d412010-02-21 19:03:41 +010043 /* the layer 4 header */
Harald Weltebb77c9d2010-04-30 14:26:12 +020044 unsigned char *l4h;
Harald Welteec8b4502010-02-20 20:34:29 +010045
Harald Welte074c9f92010-04-30 14:29:11 +020046 /* the 'control buffer', large enough to contain 5 pointers */
47 unsigned long cb[5];
48
Harald Welteec8b4502010-02-20 20:34:29 +010049 uint16_t data_len;
50 uint16_t len;
51
52 unsigned char *head;
53 unsigned char *tail;
54 unsigned char *data;
55 unsigned char _data[0];
56};
57
58extern struct msgb *msgb_alloc(uint16_t size, const char *name);
59extern void msgb_free(struct msgb *m);
60extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
61extern struct msgb *msgb_dequeue(struct llist_head *queue);
62extern void msgb_reset(struct msgb *m);
63
Harald Welte652a7232010-07-22 21:55:24 +020064#ifdef MSGB_DEBUG
Sylvain Munaut7459d6c2010-07-25 18:09:28 +020065#include <osmocore/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +010066#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +010067 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +010068 } while(0)
69#else
70#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +020071#endif
72
Harald Weltefdd0a702010-03-01 22:30:51 +010073#define msgb_l1(m) ((void *)(m->l1h))
Harald Welteec8b4502010-02-20 20:34:29 +010074#define msgb_l2(m) ((void *)(m->l2h))
75#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebb77c9d2010-04-30 14:26:12 +020076#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010077
Harald Weltefdd0a702010-03-01 22:30:51 +010078static inline unsigned int msgb_l1len(const struct msgb *msgb)
79{
80 return msgb->tail - (uint8_t *)msgb_l1(msgb);
81}
82
Harald Welteec8b4502010-02-20 20:34:29 +010083static inline unsigned int msgb_l2len(const struct msgb *msgb)
84{
85 return msgb->tail - (uint8_t *)msgb_l2(msgb);
86}
87
88static inline unsigned int msgb_l3len(const struct msgb *msgb)
89{
90 return msgb->tail - (uint8_t *)msgb_l3(msgb);
91}
92
93static inline unsigned int msgb_headlen(const struct msgb *msgb)
94{
95 return msgb->len - msgb->data_len;
96}
Harald Welte652a7232010-07-22 21:55:24 +020097
98static inline int msgb_tailroom(const struct msgb *msgb)
99{
100 return (msgb->head + msgb->data_len) - msgb->tail;
101}
102
103static inline int msgb_headroom(const struct msgb *msgb)
104{
105 return (msgb->data - msgb->head);
106}
107
Harald Welteec8b4502010-02-20 20:34:29 +0100108static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
109{
110 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100111 if (msgb_tailroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100112 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
113 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100114 msgb->tail += len;
115 msgb->len += len;
116 return tmp;
117}
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100118static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
119{
120 uint8_t *space = msgb_put(msgb, 1);
121 space[0] = word & 0xFF;
122}
123static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
124{
125 uint8_t *space = msgb_put(msgb, 2);
126 space[0] = word >> 8 & 0xFF;
127 space[1] = word & 0xFF;
128}
129static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
130{
131 uint8_t *space = msgb_put(msgb, 4);
132 space[0] = word >> 24 & 0xFF;
133 space[1] = word >> 16 & 0xFF;
134 space[2] = word >> 8 & 0xFF;
135 space[3] = word & 0xFF;
136}
137static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
138{
139 unsigned char *tmp = msgb->data;
140 msgb->data += len;
141 msgb->len -= len;
142 return tmp;
143}
144static inline uint8_t msgb_get_u8(struct msgb *msgb)
145{
146 uint8_t *space = msgb_get(msgb, 1);
147 return space[0];
148}
149static inline uint16_t msgb_get_u16(struct msgb *msgb)
150{
151 uint8_t *space = msgb_get(msgb, 2);
152 return space[0] << 8 | space[1];
153}
154static inline uint32_t msgb_get_u32(struct msgb *msgb)
155{
156 uint8_t *space = msgb_get(msgb, 4);
157 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
158}
Harald Welteec8b4502010-02-20 20:34:29 +0100159static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
160{
Harald Welte46a45a62010-11-09 13:41:48 +0100161 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100162 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
163 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100164 msgb->data -= len;
165 msgb->len += len;
166 return msgb->data;
167}
168static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
169{
170 msgb->len -= len;
171 return msgb->data += len;
172}
Harald Welteec8b4502010-02-20 20:34:29 +0100173
174/* increase the headroom of an empty msgb, reducing the tailroom */
175static inline void msgb_reserve(struct msgb *msg, int len)
176{
177 msg->data += len;
178 msg->tail += len;
179}
180
181static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
182 const char *name)
183{
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100184 static_assert(size > headroom, headroom_bigger);
185
Harald Welteec8b4502010-02-20 20:34:29 +0100186 struct msgb *msg = msgb_alloc(size, name);
187 if (msg)
188 msgb_reserve(msg, headroom);
189 return msg;
190}
191
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200192/* non inline functions to ease binding */
193uint8_t *msgb_data(const struct msgb *msg);
194uint16_t msgb_length(const struct msgb *msg);
195
196
Harald Welteec8b4502010-02-20 20:34:29 +0100197#endif /* _MSGB_H */