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