summaryrefslogtreecommitdiffstats
path: root/include/osmocore/msgb.h
blob: 354c5d88a4c8fb147ac11507b0b76e639dc2f740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef _MSGB_H
#define _MSGB_H

/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdint.h>
#include "linuxlist.h"

#define MSGB_DEBUG

struct msgb {
	struct llist_head list;

	/* Part of which TRX logical channel we were received / transmitted */
	/* FIXME: move them into the control buffer */
	struct gsm_bts_trx *trx;
	struct gsm_lchan *lchan;

	/* the Layer1 header (if any) */
	unsigned char *l1h;
	/* the A-bis layer 2 header: OML, RSL(RLL), NS */
	unsigned char *l2h;
	/* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
	unsigned char *l3h;
	/* the layer 4 header */
	unsigned char *l4h;

	/* the 'control buffer', large enough to contain 5 pointers */
	unsigned long cb[5];

	uint16_t data_len;
	uint16_t len;

	unsigned char *head;
	unsigned char *tail;
	unsigned char *data;
	unsigned char _data[0];
};

extern struct msgb *msgb_alloc(uint16_t size, const char *name);
extern void msgb_free(struct msgb *m);
extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
extern struct msgb *msgb_dequeue(struct llist_head *queue);
extern void msgb_reset(struct msgb *m);

#ifdef MSGB_DEBUG
#include <osmocore/panic.h>
static inline void msgb_abort(struct msgb *msg, const char *text)
{
	osmo_panic("%s", text);
}
#endif

#define msgb_l1(m)	((void *)(m->l1h))
#define msgb_l2(m)	((void *)(m->l2h))
#define msgb_l3(m)	((void *)(m->l3h))
#define msgb_sms(m)	((void *)(m->l4h))

static inline unsigned int msgb_l1len(const struct msgb *msgb)
{
	return msgb->tail - (uint8_t *)msgb_l1(msgb);
}

static inline unsigned int msgb_l2len(const struct msgb *msgb)
{
	return msgb->tail - (uint8_t *)msgb_l2(msgb);
}

static inline unsigned int msgb_l3len(const struct msgb *msgb)
{
	return msgb->tail - (uint8_t *)msgb_l3(msgb);
}

static inline unsigned int msgb_headlen(const struct msgb *msgb)
{
	return msgb->len - msgb->data_len;
}

static inline int msgb_tailroom(const struct msgb *msgb)
{
	return (msgb->head + msgb->data_len) - msgb->tail;
}

static inline int msgb_headroom(const struct msgb *msgb)
{
	return (msgb->data - msgb->head);
}

static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
{
	unsigned char *tmp = msgb->tail;
#ifdef MSGB_DEBUG
	if (msgb_tailroom(msgb) < len)
		msgb_abort(msgb, "Not enough tailroom\n");
#endif
	msgb->tail += len;
	msgb->len += len;
	return tmp;
}
static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
{
	uint8_t *space = msgb_put(msgb, 1);
	space[0] = word & 0xFF;
}
static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
{
	uint8_t *space = msgb_put(msgb, 2);
	space[0] = word >> 8 & 0xFF;
	space[1] = word & 0xFF;
}
static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
{
	uint8_t *space = msgb_put(msgb, 4);
	space[0] = word >> 24 & 0xFF;
	space[1] = word >> 16 & 0xFF;
	space[2] = word >> 8 & 0xFF;
	space[3] = word & 0xFF;
}
static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
{
	unsigned char *tmp = msgb->data;
	msgb->data += len;
	msgb->len -= len;
	return tmp;
}
static inline uint8_t msgb_get_u8(struct msgb *msgb)
{
	uint8_t *space = msgb_get(msgb, 1);
	return space[0];
}
static inline uint16_t msgb_get_u16(struct msgb *msgb)
{
	uint8_t *space = msgb_get(msgb, 2);
	return space[0] << 8 | space[1];
}
static inline uint32_t msgb_get_u32(struct msgb *msgb)
{
	uint8_t *space = msgb_get(msgb, 4);
	return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
}
static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
{
#ifdef MSGB_DEBUG
	if (msgb_headroom(msgb) < len)
		msgb_abort(msgb, "Not enough headroom\n");
#endif
	msgb->data -= len;
	msgb->len += len;
	return msgb->data;
}
static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
{
	msgb->len -= len;
	return msgb->data += len;
}

/* increase the headroom of an empty msgb, reducing the tailroom */
static inline void msgb_reserve(struct msgb *msg, int len)
{
	msg->data += len;
	msg->tail += len;
}

static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
						const char *name)
{
	struct msgb *msg = msgb_alloc(size, name);
	if (msg)
		msgb_reserve(msg, headroom);
	return msg;
}

#endif /* _MSGB_H */