libosmocore
0.9.3
Osmocom core library
Main Page
Modules
Data Structures
Files
File List
Globals
msgb.h
Go to the documentation of this file.
1
#pragma once
2
3
/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
4
* All Rights Reserved
5
*
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License along
17
* with this program; if not, write to the Free Software Foundation, Inc.,
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
*/
21
22
#include <stdint.h>
23
#include <osmocom/core/linuxlist.h>
24
#include <
osmocom/core/utils.h
>
25
#include <
osmocom/core/bits.h
>
26
39
#define MSGB_DEBUG
40
42
struct
msgb
{
43
struct
llist_head
list
;
46
/* Part of which TRX logical channel we were received / transmitted */
47
/* FIXME: move them into the control buffer */
48
union
{
49
void
*
dst
;
50
struct
gsm_bts_trx *trx;
51
};
52
struct
gsm_lchan *
lchan
;
54
unsigned
char
*
l1h
;
55
unsigned
char
*
l2h
;
56
unsigned
char
*
l3h
;
57
unsigned
char
*
l4h
;
59
unsigned
long
cb
[5];
61
uint16_t
data_len
;
62
uint16_t
len
;
64
unsigned
char
*
head
;
65
unsigned
char
*
tail
;
66
unsigned
char
*
data
;
67
unsigned
char
_data
[0];
68
};
69
70
extern
struct
msgb
*
msgb_alloc
(uint16_t size,
const
char
*name);
71
extern
void
msgb_free
(
struct
msgb
*m);
72
extern
void
msgb_enqueue
(
struct
llist_head
*queue,
struct
msgb
*msg);
73
extern
struct
msgb
*
msgb_dequeue
(
struct
llist_head
*queue);
74
extern
void
msgb_reset
(
struct
msgb
*m);
75
uint16_t
msgb_length
(
const
struct
msgb
*msg);
76
extern
const
char
*
msgb_hexdump
(
const
struct
msgb
*msg);
77
78
#ifdef MSGB_DEBUG
79
#include <
osmocom/core/panic.h
>
80
#define MSGB_ABORT(msg, fmt, args ...) do { \
81
osmo_panic("msgb(%p): " fmt, msg, ## args); \
82
} while(0)
83
#else
84
#define MSGB_ABORT(msg, fmt, args ...)
85
#endif
86
88
#define msgb_l1(m) ((void *)(m->l1h))
89
90
#define msgb_l2(m) ((void *)(m->l2h))
91
92
#define msgb_l3(m) ((void *)(m->l3h))
93
94
#define msgb_sms(m) ((void *)(m->l4h))
95
103
static
inline
unsigned
int
msgb_l1len
(
const
struct
msgb
*
msgb
)
104
{
105
return
msgb->
tail
- (uint8_t *)
msgb_l1
(msgb);
106
}
107
115
static
inline
unsigned
int
msgb_l2len
(
const
struct
msgb
*
msgb
)
116
{
117
return
msgb->
tail
- (uint8_t *)
msgb_l2
(msgb);
118
}
119
127
static
inline
unsigned
int
msgb_l3len
(
const
struct
msgb
*
msgb
)
128
{
129
return
msgb->
tail
- (uint8_t *)
msgb_l3
(msgb);
130
}
131
139
static
inline
unsigned
int
msgb_headlen
(
const
struct
msgb
*
msgb
)
140
{
141
return
msgb->
len
- msgb->
data_len
;
142
}
143
151
static
inline
int
msgb_tailroom
(
const
struct
msgb
*
msgb
)
152
{
153
return
(msgb->
head
+ msgb->
data_len
) - msgb->
tail
;
154
}
155
163
static
inline
int
msgb_headroom
(
const
struct
msgb
*
msgb
)
164
{
165
return
(msgb->
data
- msgb->
head
);
166
}
167
180
static
inline
unsigned
char
*
msgb_put
(
struct
msgb
*
msgb
,
unsigned
int
len
)
181
{
182
unsigned
char
*tmp = msgb->
tail
;
183
if
(
msgb_tailroom
(msgb) < (
int
) len)
184
MSGB_ABORT(msgb,
"Not enough tailroom msgb_push (%u < %u)\n"
,
185
msgb_tailroom
(msgb), len);
186
msgb->
tail
+=
len
;
187
msgb->
len
+=
len
;
188
return
tmp;
189
}
190
195
static
inline
void
msgb_put_u8
(
struct
msgb
*
msgb
, uint8_t word)
196
{
197
uint8_t *space =
msgb_put
(msgb, 1);
198
space[0] = word & 0xFF;
199
}
200
205
static
inline
void
msgb_put_u16
(
struct
msgb
*
msgb
, uint16_t word)
206
{
207
uint8_t *space =
msgb_put
(msgb, 2);
208
osmo_store16be(word, space);
209
}
210
215
static
inline
void
msgb_put_u32
(
struct
msgb
*
msgb
, uint32_t word)
216
{
217
uint8_t *space =
msgb_put
(msgb, 4);
218
osmo_store32be(word, space);
219
}
220
225
static
inline
unsigned
char
*
msgb_get
(
struct
msgb
*
msgb
,
unsigned
int
len
)
226
{
227
unsigned
char
*tmp = msgb->
tail
-
len
;
228
if
(
msgb_length
(msgb) < len)
229
MSGB_ABORT(msgb,
"msgb too small to get %u (len %u)\n"
,
230
len,
msgb_length
(msgb));
231
msgb->
tail
-=
len
;
232
msgb->
len
-=
len
;
233
return
tmp;
234
}
235
240
static
inline
uint8_t
msgb_get_u8
(
struct
msgb
*
msgb
)
241
{
242
uint8_t *space =
msgb_get
(msgb, 1);
243
return
space[0];
244
}
245
250
static
inline
uint16_t
msgb_get_u16
(
struct
msgb
*
msgb
)
251
{
252
uint8_t *space =
msgb_get
(msgb, 2);
253
return
osmo_load16be(space);
254
}
255
260
static
inline
uint32_t
msgb_get_u32
(
struct
msgb
*
msgb
)
261
{
262
uint8_t *space =
msgb_get
(msgb, 4);
263
return
osmo_load32be(space);
264
}
265
278
static
inline
unsigned
char
*
msgb_push
(
struct
msgb
*
msgb
,
unsigned
int
len
)
279
{
280
if
(
msgb_headroom
(msgb) < (
int
) len)
281
MSGB_ABORT(msgb,
"Not enough headroom msgb_push (%u < %u)\n"
,
282
msgb_headroom
(msgb), len);
283
msgb->
data
-=
len
;
284
msgb->
len
+=
len
;
285
return
msgb->
data
;
286
}
287
297
static
inline
unsigned
char
*
msgb_pull
(
struct
msgb
*
msgb
,
unsigned
int
len
)
298
{
299
msgb->
len
-=
len
;
300
return
msgb->
data
+=
len
;
301
}
302
311
static
inline
unsigned
char
*
msgb_pull_to_l3
(
struct
msgb
*msg)
312
{
313
unsigned
char
*ret =
msgb_pull
(msg, msg->
l3h
- msg->
data
);
314
msg->
l1h
= msg->
l2h
= NULL;
315
return
ret;
316
}
317
322
static
inline
uint8_t
msgb_pull_u8
(
struct
msgb
*
msgb
)
323
{
324
uint8_t *space =
msgb_pull
(msgb, 1) - 1;
325
return
space[0];
326
}
327
332
static
inline
uint16_t
msgb_pull_u16
(
struct
msgb
*
msgb
)
333
{
334
uint8_t *space =
msgb_pull
(msgb, 2) - 2;
335
return
osmo_load16be(space);
336
}
337
342
static
inline
uint32_t
msgb_pull_u32
(
struct
msgb
*
msgb
)
343
{
344
uint8_t *space =
msgb_pull
(msgb, 4) - 4;
345
return
osmo_load32be(space);
346
}
347
359
static
inline
void
msgb_reserve
(
struct
msgb
*msg,
int
len
)
360
{
361
msg->
data
+=
len
;
362
msg->
tail
+=
len
;
363
}
364
370
static
inline
int
msgb_trim
(
struct
msgb
*msg,
int
len
)
371
{
372
if
(len > msg->
data_len
)
373
return
-1;
374
375
msg->
len
=
len
;
376
msg->
tail
= msg->
data
+
len
;
377
378
return
0;
379
}
380
386
static
inline
int
msgb_l3trim
(
struct
msgb
*msg,
int
l3len)
387
{
388
return
msgb_trim
(msg, (msg->
l3h
- msg->
data
) + l3len);
389
}
390
401
static
inline
struct
msgb
*
msgb_alloc_headroom
(
int
size,
int
headroom,
402
const
char
*name)
403
{
404
osmo_static_assert(size > headroom, headroom_bigger);
405
406
struct
msgb
*msg =
msgb_alloc
(size, name);
407
if
(msg)
408
msgb_reserve
(msg, headroom);
409
return
msg;
410
}
411
412
/* non inline functions to ease binding */
413
414
uint8_t *
msgb_data
(
const
struct
msgb
*msg);
415
void
msgb_set_talloc_ctx
(
void
*ctx);
416
include
osmocom
core
msgb.h
Generated on Mon Jan 18 2016 13:17:40 for libosmocore by
1.8.1.2