blob: db1f8aee216f817950c088c098717102269a952f [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;
Daniel Willmanne0fbec82008-12-29 00:44:41 +000039 unsigned char *smsh;
Harald Welte52b1f982008-12-23 20:25:15 +000040
41 u_int16_t data_len;
42 u_int16_t len;
43
44 unsigned char *head;
45 unsigned char *tail;
46 unsigned char *data;
47 unsigned char _data[0];
48};
49
50extern struct msgb *msgb_alloc(u_int16_t size);
51extern void msgb_free(struct msgb *m);
Harald Welte8470bf22008-12-25 23:28:35 +000052extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
53extern struct msgb *msgb_dequeue(struct llist_head *queue);
Harald Welte52b1f982008-12-23 20:25:15 +000054
Harald Weltead384642008-12-26 10:20:07 +000055#define msgb_l2(m) ((void *)(m->l2h))
56#define msgb_l3(m) ((void *)(m->l3h))
Daniel Willmanne0fbec82008-12-29 00:44:41 +000057#define msgb_sms(m) ((void *)(m->smsh))
Harald Welte52b1f982008-12-23 20:25:15 +000058
59static inline unsigned int msgb_headlen(const struct msgb *msgb)
60{
61 return msgb->len - msgb->data_len;
62}
63static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
64{
65 unsigned char *tmp = msgb->tail;
66 msgb->tail += len;
67 msgb->len += len;
68 return tmp;
69}
70static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
71{
72 msgb->data -= len;
73 msgb->len += len;
74 return msgb->data;
75}
76static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
77{
78 msgb->len -= len;
79 return msgb->data += len;
80}
81static inline int msgb_tailroom(const struct msgb *msgb)
82{
83 return (msgb->data + msgb->data_len) - msgb->tail;
84}
85
Harald Welte8470bf22008-12-25 23:28:35 +000086/* increase the headroom of an empty msgb, reducing the tailroom */
87static inline void msgb_reserve(struct msgb *msg, int len)
88{
89 msg->data += len;
90 msg->tail += len;
91}
92
93static inline struct msgb *msgb_alloc_headroom(int size, int headroom)
94{
95 struct msgb *msg = msgb_alloc(size);
96 if (msg)
97 msgb_reserve(msg, headroom);
98 return msg;
99}
100
Harald Welte52b1f982008-12-23 20:25:15 +0000101#endif /* _MSGB_H */