mkae the new 'void *dst' member part of a union with the trx pointer
[osmocom-bb.git] / include / osmocom / core / msgb.h
1 #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 <osmocom/core/linuxlist.h>
25 #include <osmocom/core/utils.h>
26
27 #define MSGB_DEBUG
28
29 struct msgb {
30         struct llist_head list;
31
32
33         /* Part of which TRX logical channel we were received / transmitted */
34         /* FIXME: move them into the control buffer */
35         union {
36                 void *dst;
37                 struct gsm_bts_trx *trx;
38         };
39         struct gsm_lchan *lchan;
40
41         /* the Layer1 header (if any) */
42         unsigned char *l1h;
43         /* the A-bis layer 2 header: OML, RSL(RLL), NS */
44         unsigned char *l2h;
45         /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
46         unsigned char *l3h;
47         /* the layer 4 header */
48         unsigned char *l4h;
49
50         /* the 'control buffer', large enough to contain 5 pointers */
51         unsigned long cb[5];
52
53         uint16_t data_len;
54         uint16_t len;
55
56         unsigned char *head;
57         unsigned char *tail;
58         unsigned char *data;
59         unsigned char _data[0];
60 };
61
62 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
63 extern void msgb_free(struct msgb *m);
64 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
65 extern struct msgb *msgb_dequeue(struct llist_head *queue);
66 extern void msgb_reset(struct msgb *m);
67
68 #ifdef MSGB_DEBUG
69 #include <osmocom/core/panic.h>
70 #define MSGB_ABORT(msg, fmt, args ...) do {             \
71         osmo_panic("msgb(%p): " fmt, msg, ## args);     \
72         } while(0)
73 #else
74 #define MSGB_ABORT(msg, fmt, args ...)
75 #endif
76
77 #define msgb_l1(m)      ((void *)(m->l1h))
78 #define msgb_l2(m)      ((void *)(m->l2h))
79 #define msgb_l3(m)      ((void *)(m->l3h))
80 #define msgb_sms(m)     ((void *)(m->l4h))
81
82 static inline unsigned int msgb_l1len(const struct msgb *msgb)
83 {
84         return msgb->tail - (uint8_t *)msgb_l1(msgb);
85 }
86
87 static inline unsigned int msgb_l2len(const struct msgb *msgb)
88 {
89         return msgb->tail - (uint8_t *)msgb_l2(msgb);
90 }
91
92 static inline unsigned int msgb_l3len(const struct msgb *msgb)
93 {
94         return msgb->tail - (uint8_t *)msgb_l3(msgb);
95 }
96
97 static inline unsigned int msgb_headlen(const struct msgb *msgb)
98 {
99         return msgb->len - msgb->data_len;
100 }
101
102 static inline int msgb_tailroom(const struct msgb *msgb)
103 {
104         return (msgb->head + msgb->data_len) - msgb->tail;
105 }
106
107 static inline int msgb_headroom(const struct msgb *msgb)
108 {
109         return (msgb->data - msgb->head);
110 }
111
112 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
113 {
114         unsigned char *tmp = msgb->tail;
115         if (msgb_tailroom(msgb) < (int) len)
116                 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
117                            msgb_tailroom(msgb), len);
118         msgb->tail += len;
119         msgb->len += len;
120         return tmp;
121 }
122 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
123 {
124         uint8_t *space = msgb_put(msgb, 1);
125         space[0] = word & 0xFF;
126 }
127 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
128 {
129         uint8_t *space = msgb_put(msgb, 2);
130         space[0] = word >> 8 & 0xFF;
131         space[1] = word & 0xFF;
132 }
133 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
134 {
135         uint8_t *space = msgb_put(msgb, 4);
136         space[0] = word >> 24 & 0xFF;
137         space[1] = word >> 16 & 0xFF;
138         space[2] = word >> 8 & 0xFF;
139         space[3] = word & 0xFF;
140 }
141 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
142 {
143         unsigned char *tmp = msgb->data;
144         msgb->data += len;
145         msgb->len -= len;
146         return tmp;
147 }
148 static inline uint8_t msgb_get_u8(struct msgb *msgb)
149 {
150         uint8_t *space = msgb_get(msgb, 1);
151         return space[0];
152 }
153 static inline uint16_t msgb_get_u16(struct msgb *msgb)
154 {
155         uint8_t *space = msgb_get(msgb, 2);
156         return space[0] << 8 | space[1];
157 }
158 static inline uint32_t msgb_get_u32(struct msgb *msgb)
159 {
160         uint8_t *space = msgb_get(msgb, 4);
161         return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
162 }
163 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
164 {
165         if (msgb_headroom(msgb) < (int) len)
166                 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
167                            msgb_headroom(msgb), len);
168         msgb->data -= len;
169         msgb->len += len;
170         return msgb->data;
171 }
172 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
173 {
174         msgb->len -= len;
175         return msgb->data += len;
176 }
177
178 /* increase the headroom of an empty msgb, reducing the tailroom */
179 static inline void msgb_reserve(struct msgb *msg, int len)
180 {
181         msg->data += len;
182         msg->tail += len;
183 }
184
185 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
186                                                 const char *name)
187 {
188         osmo_static_assert(size > headroom, headroom_bigger);
189
190         struct msgb *msg = msgb_alloc(size, name);
191         if (msg)
192                 msgb_reserve(msg, headroom);
193         return msg;
194 }
195
196 /* non inline functions to ease binding */
197 uint8_t *msgb_data(const struct msgb *msg);
198 uint16_t msgb_length(const struct msgb *msg);
199
200 /* set the talloc context for msgb_alloc[_headroom] */
201 void msgb_set_talloc_ctx(void *ctx);
202
203 #endif /* _MSGB_H */