blob: cfb912135b1d3e91c75337148563e29ab2a1a66c [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001#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
Harald Welte8470bf22008-12-25 23:28:35 +000023#include <openbsc/linuxlist.h>
24
Harald Welte52b1f982008-12-23 20:25:15 +000025struct bts_link;
26
27struct msgb {
Harald Welte8470bf22008-12-25 23:28:35 +000028 struct llist_head list;
29
30 /* ptr to the physical E1 link to the BTS(s) */
Harald Welte52b1f982008-12-23 20:25:15 +000031 struct gsm_bts_link *bts_link;
32
Harald Welte8470bf22008-12-25 23:28:35 +000033 /* Part of which TRX logical channel we were received / transmitted */
34 struct gsm_bts_trx *trx;
35 struct gsm_lchan *lchan;
36
Harald Weltead384642008-12-26 10:20:07 +000037 unsigned char *l2h;
38 unsigned char *l3h;
Harald Welte52b1f982008-12-23 20:25:15 +000039
40 u_int16_t data_len;
41 u_int16_t len;
42
43 unsigned char *head;
44 unsigned char *tail;
45 unsigned char *data;
46 unsigned char _data[0];
47};
48
49extern struct msgb *msgb_alloc(u_int16_t size);
50extern void msgb_free(struct msgb *m);
Harald Welte8470bf22008-12-25 23:28:35 +000051extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
52extern struct msgb *msgb_dequeue(struct llist_head *queue);
Harald Welte52b1f982008-12-23 20:25:15 +000053
Harald Weltead384642008-12-26 10:20:07 +000054#define msgb_l2(m) ((void *)(m->l2h))
55#define msgb_l3(m) ((void *)(m->l3h))
Harald Welte52b1f982008-12-23 20:25:15 +000056
57static inline unsigned int msgb_headlen(const struct msgb *msgb)
58{
59 return msgb->len - msgb->data_len;
60}
61static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
62{
63 unsigned char *tmp = msgb->tail;
64 msgb->tail += len;
65 msgb->len += len;
66 return tmp;
67}
68static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
69{
70 msgb->data -= len;
71 msgb->len += len;
72 return msgb->data;
73}
74static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
75{
76 msgb->len -= len;
77 return msgb->data += len;
78}
79static inline int msgb_tailroom(const struct msgb *msgb)
80{
81 return (msgb->data + msgb->data_len) - msgb->tail;
82}
83
Harald Welte8470bf22008-12-25 23:28:35 +000084/* increase the headroom of an empty msgb, reducing the tailroom */
85static inline void msgb_reserve(struct msgb *msg, int len)
86{
87 msg->data += len;
88 msg->tail += len;
89}
90
91static inline struct msgb *msgb_alloc_headroom(int size, int headroom)
92{
93 struct msgb *msg = msgb_alloc(size);
94 if (msg)
95 msgb_reserve(msg, headroom);
96 return msg;
97}
98
Harald Welte52b1f982008-12-23 20:25:15 +000099#endif /* _MSGB_H */