blob: 6dc0778456a7b2351a7d77d8e02a9c966a51d1da [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 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
43 /* the layer 4 header */
44 union {
45 unsigned char *smsh;
46 unsigned char *llch;
47 };
48
49 /* the layer 5 header, GPRS: GMM header */
50 unsigned char *gmmh;
51 uint32_t tlli;
Harald Welteec8b4502010-02-20 20:34:29 +010052
53 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
68#define msgb_l2(m) ((void *)(m->l2h))
69#define msgb_l3(m) ((void *)(m->l3h))
70#define msgb_sms(m) ((void *)(m->smsh))
71
72static inline unsigned int msgb_l2len(const struct msgb *msgb)
73{
74 return msgb->tail - (uint8_t *)msgb_l2(msgb);
75}
76
77static inline unsigned int msgb_l3len(const struct msgb *msgb)
78{
79 return msgb->tail - (uint8_t *)msgb_l3(msgb);
80}
81
82static inline unsigned int msgb_headlen(const struct msgb *msgb)
83{
84 return msgb->len - msgb->data_len;
85}
86static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
87{
88 unsigned char *tmp = msgb->tail;
89 msgb->tail += len;
90 msgb->len += len;
91 return tmp;
92}
93static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
94{
95 msgb->data -= len;
96 msgb->len += len;
97 return msgb->data;
98}
99static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
100{
101 msgb->len -= len;
102 return msgb->data += len;
103}
104static inline int msgb_tailroom(const struct msgb *msgb)
105{
Harald Welteac778fb2010-02-24 22:54:11 +0100106 return (msgb->head + msgb->data_len) - msgb->tail;
Harald Welteec8b4502010-02-20 20:34:29 +0100107}
108
109/* increase the headroom of an empty msgb, reducing the tailroom */
110static inline void msgb_reserve(struct msgb *msg, int len)
111{
112 msg->data += len;
113 msg->tail += len;
114}
115
116static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
117 const char *name)
118{
119 struct msgb *msg = msgb_alloc(size, name);
120 if (msg)
121 msgb_reserve(msg, headroom);
122 return msg;
123}
124
125#endif /* _MSGB_H */