blob: 61c224fec7ebd69d23e07fa3f0bb4dda80e8d979 [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
Harald Welteec8b4502010-02-20 20:34:29 +010026struct msgb {
27 struct llist_head list;
28
Harald Welteec8b4502010-02-20 20:34:29 +010029 /* Part of which TRX logical channel we were received / transmitted */
30 struct gsm_bts_trx *trx;
31 struct gsm_lchan *lchan;
32
Harald Welte00096ac2010-03-01 12:55:15 +010033 /* the Layer1 header (if any) */
34 unsigned char *l1h;
Harald Welte3415d412010-02-21 19:03:41 +010035 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
Harald Welteec8b4502010-02-20 20:34:29 +010036 unsigned char *l2h;
Harald Welte3415d412010-02-21 19:03:41 +010037 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
Harald Welteec8b4502010-02-20 20:34:29 +010038 unsigned char *l3h;
Harald Welte3415d412010-02-21 19:03:41 +010039 /* the layer 4 header */
Harald Weltebb77c9d2010-04-30 14:26:12 +020040 unsigned char *l4h;
Harald Welteec8b4502010-02-20 20:34:29 +010041
42 uint16_t data_len;
43 uint16_t len;
44
45 unsigned char *head;
46 unsigned char *tail;
47 unsigned char *data;
48 unsigned char _data[0];
49};
50
51extern struct msgb *msgb_alloc(uint16_t size, const char *name);
52extern void msgb_free(struct msgb *m);
53extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
54extern struct msgb *msgb_dequeue(struct llist_head *queue);
55extern void msgb_reset(struct msgb *m);
56
Harald Weltefdd0a702010-03-01 22:30:51 +010057#define msgb_l1(m) ((void *)(m->l1h))
Harald Welteec8b4502010-02-20 20:34:29 +010058#define msgb_l2(m) ((void *)(m->l2h))
59#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebb77c9d2010-04-30 14:26:12 +020060#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010061
Harald Weltefdd0a702010-03-01 22:30:51 +010062static inline unsigned int msgb_l1len(const struct msgb *msgb)
63{
64 return msgb->tail - (uint8_t *)msgb_l1(msgb);
65}
66
Harald Welteec8b4502010-02-20 20:34:29 +010067static inline unsigned int msgb_l2len(const struct msgb *msgb)
68{
69 return msgb->tail - (uint8_t *)msgb_l2(msgb);
70}
71
72static inline unsigned int msgb_l3len(const struct msgb *msgb)
73{
74 return msgb->tail - (uint8_t *)msgb_l3(msgb);
75}
76
77static inline unsigned int msgb_headlen(const struct msgb *msgb)
78{
79 return msgb->len - msgb->data_len;
80}
81static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
82{
83 unsigned char *tmp = msgb->tail;
84 msgb->tail += len;
85 msgb->len += len;
86 return tmp;
87}
Ingo Albrecht48e17f82010-03-07 18:03:41 +010088static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
89{
90 uint8_t *space = msgb_put(msgb, 1);
91 space[0] = word & 0xFF;
92}
93static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
94{
95 uint8_t *space = msgb_put(msgb, 2);
96 space[0] = word >> 8 & 0xFF;
97 space[1] = word & 0xFF;
98}
99static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
100{
101 uint8_t *space = msgb_put(msgb, 4);
102 space[0] = word >> 24 & 0xFF;
103 space[1] = word >> 16 & 0xFF;
104 space[2] = word >> 8 & 0xFF;
105 space[3] = word & 0xFF;
106}
107static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
108{
109 unsigned char *tmp = msgb->data;
110 msgb->data += len;
111 msgb->len -= len;
112 return tmp;
113}
114static inline uint8_t msgb_get_u8(struct msgb *msgb)
115{
116 uint8_t *space = msgb_get(msgb, 1);
117 return space[0];
118}
119static inline uint16_t msgb_get_u16(struct msgb *msgb)
120{
121 uint8_t *space = msgb_get(msgb, 2);
122 return space[0] << 8 | space[1];
123}
124static inline uint32_t msgb_get_u32(struct msgb *msgb)
125{
126 uint8_t *space = msgb_get(msgb, 4);
127 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
128}
Harald Welteec8b4502010-02-20 20:34:29 +0100129static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
130{
131 msgb->data -= len;
132 msgb->len += len;
133 return msgb->data;
134}
135static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
136{
137 msgb->len -= len;
138 return msgb->data += len;
139}
140static inline int msgb_tailroom(const struct msgb *msgb)
141{
Harald Welteac778fb2010-02-24 22:54:11 +0100142 return (msgb->head + msgb->data_len) - msgb->tail;
Harald Welteec8b4502010-02-20 20:34:29 +0100143}
144
145/* increase the headroom of an empty msgb, reducing the tailroom */
146static inline void msgb_reserve(struct msgb *msg, int len)
147{
148 msg->data += len;
149 msg->tail += len;
150}
151
152static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
153 const char *name)
154{
155 struct msgb *msg = msgb_alloc(size, name);
156 if (msg)
157 msgb_reserve(msg, headroom);
158 return msg;
159}
160
161#endif /* _MSGB_H */