blob: d0efc90b507523b4a643e8046aa0e6926d925da5 [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
Holger Hans Peter Freyther6d89dac2010-02-12 01:47:10 +010023#include <sys/types.h>
24#include "linuxlist.h"
Harald Welte8470bf22008-12-25 23:28:35 +000025
Harald Welte52b1f982008-12-23 20:25:15 +000026struct bts_link;
27
28struct msgb {
Harald Welte8470bf22008-12-25 23:28:35 +000029 struct llist_head list;
30
31 /* ptr to the physical E1 link to the BTS(s) */
Harald Welte52b1f982008-12-23 20:25:15 +000032 struct gsm_bts_link *bts_link;
33
Harald Welte8470bf22008-12-25 23:28:35 +000034 /* Part of which TRX logical channel we were received / transmitted */
35 struct gsm_bts_trx *trx;
36 struct gsm_lchan *lchan;
37
Harald Weltead384642008-12-26 10:20:07 +000038 unsigned char *l2h;
39 unsigned char *l3h;
Daniel Willmanne0fbec82008-12-29 00:44:41 +000040 unsigned char *smsh;
Harald Welte52b1f982008-12-23 20:25:15 +000041
42 u_int16_t data_len;
43 u_int16_t len;
44
45 unsigned char *head;
46 unsigned char *tail;
47 unsigned char *data;
48 unsigned char _data[0];
49};
50
Harald Welte966636f2009-06-26 19:39:35 +020051extern struct msgb *msgb_alloc(u_int16_t size, const char *name);
Harald Welte52b1f982008-12-23 20:25:15 +000052extern void msgb_free(struct msgb *m);
Harald Welte8470bf22008-12-25 23:28:35 +000053extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
54extern struct msgb *msgb_dequeue(struct llist_head *queue);
Holger Hans Peter Freythercbbd4982009-10-09 07:33:36 +020055extern void msgb_reset(struct msgb *m);
Harald Welte52b1f982008-12-23 20:25:15 +000056
Harald Weltead384642008-12-26 10:20:07 +000057#define msgb_l2(m) ((void *)(m->l2h))
58#define msgb_l3(m) ((void *)(m->l3h))
Daniel Willmanne0fbec82008-12-29 00:44:41 +000059#define msgb_sms(m) ((void *)(m->smsh))
Harald Welte52b1f982008-12-23 20:25:15 +000060
Harald Weltee0212542009-02-16 21:09:34 +000061static inline unsigned int msgb_l2len(const struct msgb *msgb)
62{
63 return msgb->tail - (u_int8_t *)msgb_l2(msgb);
64}
65
Harald Welte0a239902009-02-15 17:18:37 +000066static inline unsigned int msgb_l3len(const struct msgb *msgb)
67{
68 return msgb->tail - (u_int8_t *)msgb_l3(msgb);
69}
70
Harald Welte52b1f982008-12-23 20:25:15 +000071static inline unsigned int msgb_headlen(const struct msgb *msgb)
72{
73 return msgb->len - msgb->data_len;
74}
75static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
76{
77 unsigned char *tmp = msgb->tail;
78 msgb->tail += len;
79 msgb->len += len;
80 return tmp;
81}
82static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
83{
84 msgb->data -= len;
85 msgb->len += len;
86 return msgb->data;
87}
88static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
89{
90 msgb->len -= len;
91 return msgb->data += len;
92}
93static inline int msgb_tailroom(const struct msgb *msgb)
94{
95 return (msgb->data + msgb->data_len) - msgb->tail;
96}
97
Harald Welte8470bf22008-12-25 23:28:35 +000098/* increase the headroom of an empty msgb, reducing the tailroom */
99static inline void msgb_reserve(struct msgb *msg, int len)
100{
101 msg->data += len;
102 msg->tail += len;
103}
104
Harald Welte966636f2009-06-26 19:39:35 +0200105static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
106 const char *name)
Harald Welte8470bf22008-12-25 23:28:35 +0000107{
Harald Welte966636f2009-06-26 19:39:35 +0200108 struct msgb *msg = msgb_alloc(size, name);
Harald Welte8470bf22008-12-25 23:28:35 +0000109 if (msg)
110 msgb_reserve(msg, headroom);
111 return msg;
112}
113
Harald Welte52b1f982008-12-23 20:25:15 +0000114#endif /* _MSGB_H */