Merge commit 'd3ff15fc818887be3720265b297ca7f1ec28c458'
[osmocom-bb.git] / src / target / firmware / include / comm / msgb.h
1 #ifndef _MSGB_H
2 #define _MSGB_H
3
4 /* (C) 2008-2010 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 <linuxlist.h>
24
25 struct msgb {
26         struct llist_head list;
27
28         /* the layer 1 header, if any */
29         unsigned char *l1h;
30         /* the A-bis layer 2 header: OML, RSL(RLL), NS */
31         unsigned char *l2h;
32         /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
33         unsigned char *l3h;
34
35         uint16_t data_len;
36         uint16_t len;
37
38         unsigned char *head;    /* start of buffer */
39         unsigned char *tail;    /* end of message */
40         unsigned char *data;    /* start of message */
41         unsigned char _data[0];
42 };
43
44 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
45 extern void msgb_free(struct msgb *m);
46 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
47 extern struct msgb *msgb_dequeue(struct llist_head *queue);
48 extern void msgb_reset(struct msgb *m);
49
50 #define msgb_l1(m)      ((void *)(m->l1h))
51 #define msgb_l2(m)      ((void *)(m->l2h))
52 #define msgb_l3(m)      ((void *)(m->l3h))
53
54 static inline unsigned int msgb_l1len(const struct msgb *msgb)
55 {
56         return msgb->tail - (uint8_t *)msgb_l1(msgb);
57 }
58
59 static inline unsigned int msgb_l2len(const struct msgb *msgb)
60 {
61         return msgb->tail - (uint8_t *)msgb_l2(msgb);
62 }
63
64 static inline unsigned int msgb_l3len(const struct msgb *msgb)
65 {
66         return msgb->tail - (uint8_t *)msgb_l3(msgb);
67 }
68
69 static inline unsigned int msgb_headlen(const struct msgb *msgb)
70 {
71         return msgb->len - msgb->data_len;
72 }
73 static inline int msgb_tailroom(const struct msgb *msgb)
74 {
75         return (msgb->head + msgb->data_len) - msgb->tail;
76 }
77 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
78 {
79         unsigned char *tmp = msgb->tail;
80
81         /* we intentionally call cons_puts() here to display an allocation
82          * failure on the _other_ serial port (i.e. the one that doesn't
83          * have the HDLC layer on it */
84         if (msgb_tailroom(msgb) < len)
85                 cons_puts("msgb_tailroom insufficient!\n");
86
87         msgb->tail += len;
88         msgb->len += len;
89         return tmp;
90 }
91 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
92 {
93         uint8_t *space = msgb_put(msgb, 1);
94         space[0] = word & 0xFF;
95 }
96 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
97 {
98         uint8_t *space = msgb_put(msgb, 2);
99         space[0] = word >> 8 & 0xFF;
100         space[1] = word & 0xFF;
101 }
102 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
103 {
104         uint8_t *space = msgb_put(msgb, 4);
105         space[0] = word >> 24 & 0xFF;
106         space[1] = word >> 16 & 0xFF;
107         space[2] = word >> 8 & 0xFF;
108         space[3] = word & 0xFF;
109 }
110 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
111 {
112         unsigned char *tmp = msgb->data;
113         msgb->data += len;
114         msgb->len -= len;
115         return tmp;
116 }
117 static inline uint8_t msgb_get_u8(struct msgb *msgb)
118 {
119         uint8_t *space = msgb_get(msgb, 1);
120         return space[0];
121 }
122 static inline uint16_t msgb_get_u16(struct msgb *msgb)
123 {
124         uint8_t *space = msgb_get(msgb, 2);
125         return space[0] << 8 | space[1];
126 }
127 static inline uint32_t msgb_get_u32(struct msgb *msgb)
128 {
129         uint8_t *space = msgb_get(msgb, 4);
130         return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
131 }
132 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
133 {
134         msgb->data -= len;
135         msgb->len += len;
136         return msgb->data;
137 }
138 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
139 {
140         msgb->len -= len;
141         return msgb->data += len;
142 }
143
144 /* increase the headroom of an empty msgb, reducing the tailroom */
145 static inline void msgb_reserve(struct msgb *msg, int len)
146 {
147         msg->data += len;
148         msg->tail += len;
149 }
150
151 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
152                                                 const char *name)
153 {
154         struct msgb *msg = msgb_alloc(size, name);
155         if (msg)
156                 msgb_reserve(msg, headroom);
157         return msg;
158 }
159
160 #endif /* _MSGB_H */