4 /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
23 #include <osmocore/linuxlist.h>
27 struct llist_head list;
29 /* the layer 1 header, if any */
31 /* the A-bis layer 2 header: OML, RSL(RLL), NS */
33 /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
39 unsigned char *head; /* start of buffer */
40 unsigned char *tail; /* end of message */
41 unsigned char *data; /* start of message */
42 unsigned char _data[0];
45 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
46 extern void msgb_free(struct msgb *m);
47 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
48 extern struct msgb *msgb_dequeue(struct llist_head *queue);
49 extern void msgb_reset(struct msgb *m);
51 #define msgb_l1(m) ((void *)(m->l1h))
52 #define msgb_l2(m) ((void *)(m->l2h))
53 #define msgb_l3(m) ((void *)(m->l3h))
55 static inline unsigned int msgb_l1len(const struct msgb *msgb)
57 return msgb->tail - (uint8_t *)msgb_l1(msgb);
60 static inline unsigned int msgb_l2len(const struct msgb *msgb)
62 return msgb->tail - (uint8_t *)msgb_l2(msgb);
65 static inline unsigned int msgb_l3len(const struct msgb *msgb)
67 return msgb->tail - (uint8_t *)msgb_l3(msgb);
70 static inline unsigned int msgb_headlen(const struct msgb *msgb)
72 return msgb->len - msgb->data_len;
74 static inline int msgb_tailroom(const struct msgb *msgb)
76 return (msgb->head + msgb->data_len) - msgb->tail;
78 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
80 unsigned char *tmp = msgb->tail;
82 /* we intentionally call cons_puts() here to display an allocation
83 * failure on the _other_ serial port (i.e. the one that doesn't
84 * have the HDLC layer on it */
85 if (msgb_tailroom(msgb) < len)
86 cons_puts("msgb_tailroom insufficient!\n");
92 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
94 uint8_t *space = msgb_put(msgb, 1);
95 space[0] = word & 0xFF;
97 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
99 uint8_t *space = msgb_put(msgb, 2);
100 space[0] = word >> 8 & 0xFF;
101 space[1] = word & 0xFF;
103 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
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;
111 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
113 unsigned char *tmp = msgb->data;
118 static inline uint8_t msgb_get_u8(struct msgb *msgb)
120 uint8_t *space = msgb_get(msgb, 1);
123 static inline uint16_t msgb_get_u16(struct msgb *msgb)
125 uint8_t *space = msgb_get(msgb, 2);
126 return space[0] << 8 | space[1];
128 static inline uint32_t msgb_get_u32(struct msgb *msgb)
130 uint8_t *space = msgb_get(msgb, 4);
131 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
133 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
139 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
142 return msgb->data += len;
145 /* increase the headroom of an empty msgb, reducing the tailroom */
146 static inline void msgb_reserve(struct msgb *msg, int len)
152 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
155 struct msgb *msg = msgb_alloc(size, name);
157 msgb_reserve(msg, headroom);