blob: 31e54dcd4fde40519e24f505ad0881e2b223b669 [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 Welteec8b4502010-02-20 20:34:29 +010026struct msgb {
27 struct llist_head list;
28
Harald Welteec8b4502010-02-20 20:34:29 +010029 /* Part of which TRX logical channel we were received / transmitted */
30 struct gsm_bts_trx *trx;
31 struct gsm_lchan *lchan;
32
Harald Welte00096ac2010-03-01 12:55:15 +010033 /* the Layer1 header (if any) */
34 unsigned char *l1h;
Harald Welte3415d412010-02-21 19:03:41 +010035 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
Harald Welteec8b4502010-02-20 20:34:29 +010036 unsigned char *l2h;
Harald Welte3415d412010-02-21 19:03:41 +010037 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
Harald Welteec8b4502010-02-20 20:34:29 +010038 unsigned char *l3h;
Harald Welte3415d412010-02-21 19:03:41 +010039
40 /* the layer 4 header */
41 union {
42 unsigned char *smsh;
43 unsigned char *llch;
Holger Hans Peter Freyther045cc222010-03-22 03:11:10 +010044 unsigned char *l4h;
Harald Welte3415d412010-02-21 19:03:41 +010045 };
46
47 /* the layer 5 header, GPRS: GMM header */
48 unsigned char *gmmh;
49 uint32_t tlli;
Harald Welteec8b4502010-02-20 20:34:29 +010050
51 uint16_t data_len;
52 uint16_t len;
53
54 unsigned char *head;
55 unsigned char *tail;
56 unsigned char *data;
57 unsigned char _data[0];
58};
59
60extern struct msgb *msgb_alloc(uint16_t size, const char *name);
61extern void msgb_free(struct msgb *m);
62extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
63extern struct msgb *msgb_dequeue(struct llist_head *queue);
64extern void msgb_reset(struct msgb *m);
65
Harald Weltefdd0a702010-03-01 22:30:51 +010066#define msgb_l1(m) ((void *)(m->l1h))
Harald Welteec8b4502010-02-20 20:34:29 +010067#define msgb_l2(m) ((void *)(m->l2h))
68#define msgb_l3(m) ((void *)(m->l3h))
69#define msgb_sms(m) ((void *)(m->smsh))
70
Harald Weltefdd0a702010-03-01 22:30:51 +010071static inline unsigned int msgb_l1len(const struct msgb *msgb)
72{
73 return msgb->tail - (uint8_t *)msgb_l1(msgb);
74}
75
Harald Welteec8b4502010-02-20 20:34:29 +010076static inline unsigned int msgb_l2len(const struct msgb *msgb)
77{
78 return msgb->tail - (uint8_t *)msgb_l2(msgb);
79}
80
81static inline unsigned int msgb_l3len(const struct msgb *msgb)
82{
83 return msgb->tail - (uint8_t *)msgb_l3(msgb);
84}
85
86static inline unsigned int msgb_headlen(const struct msgb *msgb)
87{
88 return msgb->len - msgb->data_len;
89}
90static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
91{
92 unsigned char *tmp = msgb->tail;
93 msgb->tail += len;
94 msgb->len += len;
95 return tmp;
96}
Ingo Albrecht48e17f82010-03-07 18:03:41 +010097static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
98{
99 uint8_t *space = msgb_put(msgb, 1);
100 space[0] = word & 0xFF;
101}
102static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
103{
104 uint8_t *space = msgb_put(msgb, 2);
105 space[0] = word >> 8 & 0xFF;
106 space[1] = word & 0xFF;
107}
108static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
109{
110 uint8_t *space = msgb_put(msgb, 4);
111 space[0] = word >> 24 & 0xFF;
112 space[1] = word >> 16 & 0xFF;
113 space[2] = word >> 8 & 0xFF;
114 space[3] = word & 0xFF;
115}
116static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
117{
118 unsigned char *tmp = msgb->data;
119 msgb->data += len;
120 msgb->len -= len;
121 return tmp;
122}
123static inline uint8_t msgb_get_u8(struct msgb *msgb)
124{
125 uint8_t *space = msgb_get(msgb, 1);
126 return space[0];
127}
128static inline uint16_t msgb_get_u16(struct msgb *msgb)
129{
130 uint8_t *space = msgb_get(msgb, 2);
131 return space[0] << 8 | space[1];
132}
133static inline uint32_t msgb_get_u32(struct msgb *msgb)
134{
135 uint8_t *space = msgb_get(msgb, 4);
136 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
137}
Harald Welteec8b4502010-02-20 20:34:29 +0100138static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
139{
140 msgb->data -= len;
141 msgb->len += len;
142 return msgb->data;
143}
144static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
145{
146 msgb->len -= len;
147 return msgb->data += len;
148}
149static inline int msgb_tailroom(const struct msgb *msgb)
150{
Harald Welteac778fb2010-02-24 22:54:11 +0100151 return (msgb->head + msgb->data_len) - msgb->tail;
Harald Welteec8b4502010-02-20 20:34:29 +0100152}
153
154/* increase the headroom of an empty msgb, reducing the tailroom */
155static inline void msgb_reserve(struct msgb *msg, int len)
156{
157 msg->data += len;
158 msg->tail += len;
159}
160
161static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
162 const char *name)
163{
164 struct msgb *msg = msgb_alloc(size, name);
165 if (msg)
166 msgb_reserve(msg, headroom);
167 return msg;
168}
169
170#endif /* _MSGB_H */