blob: 2841dc56e772594c53d53554afc25c91cc71fde2 [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 */
Harald Welte074c9f92010-04-30 14:29:11 +020030 /* FIXME: move them into the control buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010031 struct gsm_bts_trx *trx;
32 struct gsm_lchan *lchan;
33
Harald Welte00096ac2010-03-01 12:55:15 +010034 /* the Layer1 header (if any) */
35 unsigned char *l1h;
Harald Welte3415d412010-02-21 19:03:41 +010036 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
Harald Welteec8b4502010-02-20 20:34:29 +010037 unsigned char *l2h;
Harald Welte3415d412010-02-21 19:03:41 +010038 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
Harald Welteec8b4502010-02-20 20:34:29 +010039 unsigned char *l3h;
Harald Welte3415d412010-02-21 19:03:41 +010040 /* the layer 4 header */
Harald Weltebb77c9d2010-04-30 14:26:12 +020041 unsigned char *l4h;
Harald Welteec8b4502010-02-20 20:34:29 +010042
Harald Welte074c9f92010-04-30 14:29:11 +020043 /* the 'control buffer', large enough to contain 5 pointers */
44 unsigned long cb[5];
45
Harald Welteec8b4502010-02-20 20:34:29 +010046 uint16_t data_len;
47 uint16_t len;
48
49 unsigned char *head;
50 unsigned char *tail;
51 unsigned char *data;
52 unsigned char _data[0];
53};
54
55extern struct msgb *msgb_alloc(uint16_t size, const char *name);
56extern void msgb_free(struct msgb *m);
57extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
58extern struct msgb *msgb_dequeue(struct llist_head *queue);
59extern void msgb_reset(struct msgb *m);
60
Harald Weltefdd0a702010-03-01 22:30:51 +010061#define msgb_l1(m) ((void *)(m->l1h))
Harald Welteec8b4502010-02-20 20:34:29 +010062#define msgb_l2(m) ((void *)(m->l2h))
63#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebb77c9d2010-04-30 14:26:12 +020064#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010065
Harald Weltefdd0a702010-03-01 22:30:51 +010066static inline unsigned int msgb_l1len(const struct msgb *msgb)
67{
68 return msgb->tail - (uint8_t *)msgb_l1(msgb);
69}
70
Harald Welteec8b4502010-02-20 20:34:29 +010071static inline unsigned int msgb_l2len(const struct msgb *msgb)
72{
73 return msgb->tail - (uint8_t *)msgb_l2(msgb);
74}
75
76static inline unsigned int msgb_l3len(const struct msgb *msgb)
77{
78 return msgb->tail - (uint8_t *)msgb_l3(msgb);
79}
80
81static inline unsigned int msgb_headlen(const struct msgb *msgb)
82{
83 return msgb->len - msgb->data_len;
84}
85static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
86{
87 unsigned char *tmp = msgb->tail;
88 msgb->tail += len;
89 msgb->len += len;
90 return tmp;
91}
Ingo Albrecht48e17f82010-03-07 18:03:41 +010092static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
93{
94 uint8_t *space = msgb_put(msgb, 1);
95 space[0] = word & 0xFF;
96}
97static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
98{
99 uint8_t *space = msgb_put(msgb, 2);
100 space[0] = word >> 8 & 0xFF;
101 space[1] = word & 0xFF;
102}
103static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
104{
105 uint8_t *space = msgb_put(msgb, 4);
106 space[0] = word >> 24 & 0xFF;
107 space[1] = word >> 16 & 0xFF;
108 space[2] = word >> 8 & 0xFF;
109 space[3] = word & 0xFF;
110}
111static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
112{
113 unsigned char *tmp = msgb->data;
114 msgb->data += len;
115 msgb->len -= len;
116 return tmp;
117}
118static inline uint8_t msgb_get_u8(struct msgb *msgb)
119{
120 uint8_t *space = msgb_get(msgb, 1);
121 return space[0];
122}
123static inline uint16_t msgb_get_u16(struct msgb *msgb)
124{
125 uint8_t *space = msgb_get(msgb, 2);
126 return space[0] << 8 | space[1];
127}
128static inline uint32_t msgb_get_u32(struct msgb *msgb)
129{
130 uint8_t *space = msgb_get(msgb, 4);
131 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
132}
Harald Welteec8b4502010-02-20 20:34:29 +0100133static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
134{
135 msgb->data -= len;
136 msgb->len += len;
137 return msgb->data;
138}
139static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
140{
141 msgb->len -= len;
142 return msgb->data += len;
143}
144static inline int msgb_tailroom(const struct msgb *msgb)
145{
Harald Welteac778fb2010-02-24 22:54:11 +0100146 return (msgb->head + msgb->data_len) - msgb->tail;
Harald Welteec8b4502010-02-20 20:34:29 +0100147}
148
149/* increase the headroom of an empty msgb, reducing the tailroom */
150static inline void msgb_reserve(struct msgb *msg, int len)
151{
152 msg->data += len;
153 msg->tail += len;
154}
155
156static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
157 const char *name)
158{
159 struct msgb *msg = msgb_alloc(size, name);
160 if (msg)
161 msgb_reserve(msg, headroom);
162 return msg;
163}
164
165#endif /* _MSGB_H */