Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 "linuxlist.h" |
| 25 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 26 | #define MSGB_DEBUG |
| 27 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 28 | struct msgb { |
| 29 | struct llist_head list; |
| 30 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 31 | /* Part of which TRX logical channel we were received / transmitted */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 32 | /* FIXME: move them into the control buffer */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 33 | struct gsm_bts_trx *trx; |
| 34 | struct gsm_lchan *lchan; |
| 35 | |
Harald Welte | 00096ac | 2010-03-01 12:55:15 +0100 | [diff] [blame] | 36 | /* the Layer1 header (if any) */ |
| 37 | unsigned char *l1h; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 38 | /* the A-bis layer 2 header: OML, RSL(RLL), NS */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 39 | unsigned char *l2h; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 40 | /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | unsigned char *l3h; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 42 | /* the layer 4 header */ |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 43 | unsigned char *l4h; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 44 | |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 45 | /* the 'control buffer', large enough to contain 5 pointers */ |
| 46 | unsigned long cb[5]; |
| 47 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 48 | uint16_t data_len; |
| 49 | uint16_t len; |
| 50 | |
| 51 | unsigned char *head; |
| 52 | unsigned char *tail; |
| 53 | unsigned char *data; |
| 54 | unsigned char _data[0]; |
| 55 | }; |
| 56 | |
| 57 | extern struct msgb *msgb_alloc(uint16_t size, const char *name); |
| 58 | extern void msgb_free(struct msgb *m); |
| 59 | extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); |
| 60 | extern struct msgb *msgb_dequeue(struct llist_head *queue); |
| 61 | extern void msgb_reset(struct msgb *m); |
| 62 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 63 | #ifdef MSGB_DEBUG |
Sylvain Munaut | 7459d6c | 2010-07-25 18:09:28 +0200 | [diff] [blame] | 64 | #include <osmocore/panic.h> |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 65 | static inline void msgb_abort(struct msgb *msg, const char *text) |
| 66 | { |
Sylvain Munaut | 7459d6c | 2010-07-25 18:09:28 +0200 | [diff] [blame] | 67 | osmo_panic("%s", text); |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 68 | } |
| 69 | #endif |
| 70 | |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 71 | #define msgb_l1(m) ((void *)(m->l1h)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 72 | #define msgb_l2(m) ((void *)(m->l2h)) |
| 73 | #define msgb_l3(m) ((void *)(m->l3h)) |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 74 | #define msgb_sms(m) ((void *)(m->l4h)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 75 | |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 76 | static inline unsigned int msgb_l1len(const struct msgb *msgb) |
| 77 | { |
| 78 | return msgb->tail - (uint8_t *)msgb_l1(msgb); |
| 79 | } |
| 80 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 81 | static inline unsigned int msgb_l2len(const struct msgb *msgb) |
| 82 | { |
| 83 | return msgb->tail - (uint8_t *)msgb_l2(msgb); |
| 84 | } |
| 85 | |
| 86 | static inline unsigned int msgb_l3len(const struct msgb *msgb) |
| 87 | { |
| 88 | return msgb->tail - (uint8_t *)msgb_l3(msgb); |
| 89 | } |
| 90 | |
| 91 | static inline unsigned int msgb_headlen(const struct msgb *msgb) |
| 92 | { |
| 93 | return msgb->len - msgb->data_len; |
| 94 | } |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 95 | |
| 96 | static inline int msgb_tailroom(const struct msgb *msgb) |
| 97 | { |
| 98 | return (msgb->head + msgb->data_len) - msgb->tail; |
| 99 | } |
| 100 | |
| 101 | static inline int msgb_headroom(const struct msgb *msgb) |
| 102 | { |
| 103 | return (msgb->data - msgb->head); |
| 104 | } |
| 105 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 106 | static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) |
| 107 | { |
| 108 | unsigned char *tmp = msgb->tail; |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 109 | #ifdef MSGB_DEBUG |
| 110 | if (msgb_tailroom(msgb) < len) |
| 111 | msgb_abort(msgb, "Not enough tailroom\n"); |
| 112 | #endif |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 113 | msgb->tail += len; |
| 114 | msgb->len += len; |
| 115 | return tmp; |
| 116 | } |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 117 | static inline void msgb_put_u8(struct msgb *msgb, uint8_t word) |
| 118 | { |
| 119 | uint8_t *space = msgb_put(msgb, 1); |
| 120 | space[0] = word & 0xFF; |
| 121 | } |
| 122 | static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) |
| 123 | { |
| 124 | uint8_t *space = msgb_put(msgb, 2); |
| 125 | space[0] = word >> 8 & 0xFF; |
| 126 | space[1] = word & 0xFF; |
| 127 | } |
| 128 | static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) |
| 129 | { |
| 130 | uint8_t *space = msgb_put(msgb, 4); |
| 131 | space[0] = word >> 24 & 0xFF; |
| 132 | space[1] = word >> 16 & 0xFF; |
| 133 | space[2] = word >> 8 & 0xFF; |
| 134 | space[3] = word & 0xFF; |
| 135 | } |
| 136 | static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) |
| 137 | { |
| 138 | unsigned char *tmp = msgb->data; |
| 139 | msgb->data += len; |
| 140 | msgb->len -= len; |
| 141 | return tmp; |
| 142 | } |
| 143 | static inline uint8_t msgb_get_u8(struct msgb *msgb) |
| 144 | { |
| 145 | uint8_t *space = msgb_get(msgb, 1); |
| 146 | return space[0]; |
| 147 | } |
| 148 | static inline uint16_t msgb_get_u16(struct msgb *msgb) |
| 149 | { |
| 150 | uint8_t *space = msgb_get(msgb, 2); |
| 151 | return space[0] << 8 | space[1]; |
| 152 | } |
| 153 | static inline uint32_t msgb_get_u32(struct msgb *msgb) |
| 154 | { |
| 155 | uint8_t *space = msgb_get(msgb, 4); |
| 156 | return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3]; |
| 157 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 158 | static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) |
| 159 | { |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 160 | #ifdef MSGB_DEBUG |
| 161 | if (msgb_headroom(msgb) < len) |
| 162 | msgb_abort(msgb, "Not enough headroom\n"); |
| 163 | #endif |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 164 | msgb->data -= len; |
| 165 | msgb->len += len; |
| 166 | return msgb->data; |
| 167 | } |
| 168 | static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) |
| 169 | { |
| 170 | msgb->len -= len; |
| 171 | return msgb->data += len; |
| 172 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 173 | |
| 174 | /* increase the headroom of an empty msgb, reducing the tailroom */ |
| 175 | static inline void msgb_reserve(struct msgb *msg, int len) |
| 176 | { |
| 177 | msg->data += len; |
| 178 | msg->tail += len; |
| 179 | } |
| 180 | |
| 181 | static inline struct msgb *msgb_alloc_headroom(int size, int headroom, |
| 182 | const char *name) |
| 183 | { |
| 184 | struct msgb *msg = msgb_alloc(size, name); |
| 185 | if (msg) |
| 186 | msgb_reserve(msg, headroom); |
| 187 | return msg; |
| 188 | } |
| 189 | |
| 190 | #endif /* _MSGB_H */ |