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