blob: 44c59cd66e95f0efda2e52e6c7cfcfdc808eac95 [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"
25
Harald Welte652a7232010-07-22 21:55:24 +020026#define MSGB_DEBUG
27
Harald Welteec8b4502010-02-20 20:34:29 +010028struct msgb {
29 struct llist_head list;
30
Harald Welteec8b4502010-02-20 20:34:29 +010031 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020032 /* FIXME: move them into the control buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010033 struct gsm_bts_trx *trx;
34 struct gsm_lchan *lchan;
35
Harald Welte00096ac2010-03-01 12:55:15 +010036 /* the Layer1 header (if any) */
37 unsigned char *l1h;
Harald Welte3415d412010-02-21 19:03:41 +010038 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
Harald Welteec8b4502010-02-20 20:34:29 +010039 unsigned char *l2h;
Harald Welte3415d412010-02-21 19:03:41 +010040 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
Harald Welteec8b4502010-02-20 20:34:29 +010041 unsigned char *l3h;
Harald Welte3415d412010-02-21 19:03:41 +010042 /* the layer 4 header */
Harald Weltebb77c9d2010-04-30 14:26:12 +020043 unsigned char *l4h;
Harald Welteec8b4502010-02-20 20:34:29 +010044
Harald Welte074c9f92010-04-30 14:29:11 +020045 /* the 'control buffer', large enough to contain 5 pointers */
46 unsigned long cb[5];
47
Harald Welteec8b4502010-02-20 20:34:29 +010048 uint16_t data_len;
49 uint16_t len;
50
51 unsigned char *head;
52 unsigned char *tail;
53 unsigned char *data;
54 unsigned char _data[0];
55};
56
57extern struct msgb *msgb_alloc(uint16_t size, const char *name);
58extern void msgb_free(struct msgb *m);
59extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
60extern struct msgb *msgb_dequeue(struct llist_head *queue);
61extern void msgb_reset(struct msgb *m);
62
Harald Welte652a7232010-07-22 21:55:24 +020063#ifdef MSGB_DEBUG
Sylvain Munaut7459d6c2010-07-25 18:09:28 +020064#include <osmocore/panic.h>
Harald Welte088e68a2010-11-05 07:49:39 +010065#include <osmocore/gsm_utils.h>
Harald Welte929d8872010-11-05 07:47:41 +010066#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Welte088e68a2010-11-05 07:49:39 +010067 generate_backtrace(); \
Harald Weltedab02872010-11-09 13:42:26 +010068 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +010069 } while(0)
70#else
71#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +020072#endif
73
Harald Weltefdd0a702010-03-01 22:30:51 +010074#define msgb_l1(m) ((void *)(m->l1h))
Harald Welteec8b4502010-02-20 20:34:29 +010075#define msgb_l2(m) ((void *)(m->l2h))
76#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebb77c9d2010-04-30 14:26:12 +020077#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010078
Harald Weltefdd0a702010-03-01 22:30:51 +010079static inline unsigned int msgb_l1len(const struct msgb *msgb)
80{
81 return msgb->tail - (uint8_t *)msgb_l1(msgb);
82}
83
Harald Welteec8b4502010-02-20 20:34:29 +010084static inline unsigned int msgb_l2len(const struct msgb *msgb)
85{
86 return msgb->tail - (uint8_t *)msgb_l2(msgb);
87}
88
89static inline unsigned int msgb_l3len(const struct msgb *msgb)
90{
91 return msgb->tail - (uint8_t *)msgb_l3(msgb);
92}
93
94static inline unsigned int msgb_headlen(const struct msgb *msgb)
95{
96 return msgb->len - msgb->data_len;
97}
Harald Welte652a7232010-07-22 21:55:24 +020098
99static inline int msgb_tailroom(const struct msgb *msgb)
100{
101 return (msgb->head + msgb->data_len) - msgb->tail;
102}
103
104static inline int msgb_headroom(const struct msgb *msgb)
105{
106 return (msgb->data - msgb->head);
107}
108
Harald Welteec8b4502010-02-20 20:34:29 +0100109static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
110{
111 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100112 if (msgb_tailroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100113 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
114 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100115 msgb->tail += len;
116 msgb->len += len;
117 return tmp;
118}
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100119static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
120{
121 uint8_t *space = msgb_put(msgb, 1);
122 space[0] = word & 0xFF;
123}
124static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
125{
126 uint8_t *space = msgb_put(msgb, 2);
127 space[0] = word >> 8 & 0xFF;
128 space[1] = word & 0xFF;
129}
130static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
131{
132 uint8_t *space = msgb_put(msgb, 4);
133 space[0] = word >> 24 & 0xFF;
134 space[1] = word >> 16 & 0xFF;
135 space[2] = word >> 8 & 0xFF;
136 space[3] = word & 0xFF;
137}
138static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
139{
140 unsigned char *tmp = msgb->data;
141 msgb->data += len;
142 msgb->len -= len;
143 return tmp;
144}
145static inline uint8_t msgb_get_u8(struct msgb *msgb)
146{
147 uint8_t *space = msgb_get(msgb, 1);
148 return space[0];
149}
150static inline uint16_t msgb_get_u16(struct msgb *msgb)
151{
152 uint8_t *space = msgb_get(msgb, 2);
153 return space[0] << 8 | space[1];
154}
155static inline uint32_t msgb_get_u32(struct msgb *msgb)
156{
157 uint8_t *space = msgb_get(msgb, 4);
158 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
159}
Harald Welteec8b4502010-02-20 20:34:29 +0100160static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
161{
Harald Welte46a45a62010-11-09 13:41:48 +0100162 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100163 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
164 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100165 msgb->data -= len;
166 msgb->len += len;
167 return msgb->data;
168}
169static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
170{
171 msgb->len -= len;
172 return msgb->data += len;
173}
Harald Welteec8b4502010-02-20 20:34:29 +0100174
175/* increase the headroom of an empty msgb, reducing the tailroom */
176static inline void msgb_reserve(struct msgb *msg, int len)
177{
178 msg->data += len;
179 msg->tail += len;
180}
181
182static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
183 const char *name)
184{
185 struct msgb *msg = msgb_alloc(size, name);
186 if (msg)
187 msgb_reserve(msg, headroom);
188 return msg;
189}
190
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200191/* non inline functions to ease binding */
192uint8_t *msgb_data(const struct msgb *msg);
193uint16_t msgb_length(const struct msgb *msg);
194
195
Harald Welteec8b4502010-02-20 20:34:29 +0100196#endif /* _MSGB_H */