Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [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 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 23 | #include <openbsc/linuxlist.h> |
| 24 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 25 | struct bts_link; |
| 26 | |
| 27 | struct msgb { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 28 | struct llist_head list; |
| 29 | |
| 30 | /* ptr to the physical E1 link to the BTS(s) */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 31 | struct gsm_bts_link *bts_link; |
| 32 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 33 | /* Part of which TRX logical channel we were received / transmitted */ |
| 34 | struct gsm_bts_trx *trx; |
| 35 | struct gsm_lchan *lchan; |
| 36 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 37 | unsigned char *l2h; |
| 38 | unsigned char *l3h; |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 39 | unsigned char *smsh; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 40 | |
| 41 | u_int16_t data_len; |
| 42 | u_int16_t len; |
| 43 | |
| 44 | unsigned char *head; |
| 45 | unsigned char *tail; |
| 46 | unsigned char *data; |
| 47 | unsigned char _data[0]; |
| 48 | }; |
| 49 | |
| 50 | extern struct msgb *msgb_alloc(u_int16_t size); |
| 51 | extern void msgb_free(struct msgb *m); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 52 | extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); |
| 53 | extern struct msgb *msgb_dequeue(struct llist_head *queue); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 54 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 55 | #define msgb_l2(m) ((void *)(m->l2h)) |
| 56 | #define msgb_l3(m) ((void *)(m->l3h)) |
Daniel Willmann | e0fbec8 | 2008-12-29 00:44:41 +0000 | [diff] [blame] | 57 | #define msgb_sms(m) ((void *)(m->smsh)) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 58 | |
Harald Welte | e021254 | 2009-02-16 21:09:34 +0000 | [diff] [blame] | 59 | static inline unsigned int msgb_l2len(const struct msgb *msgb) |
| 60 | { |
| 61 | return msgb->tail - (u_int8_t *)msgb_l2(msgb); |
| 62 | } |
| 63 | |
Harald Welte | 0a23990 | 2009-02-15 17:18:37 +0000 | [diff] [blame] | 64 | static inline unsigned int msgb_l3len(const struct msgb *msgb) |
| 65 | { |
| 66 | return msgb->tail - (u_int8_t *)msgb_l3(msgb); |
| 67 | } |
| 68 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 69 | static inline unsigned int msgb_headlen(const struct msgb *msgb) |
| 70 | { |
| 71 | return msgb->len - msgb->data_len; |
| 72 | } |
| 73 | static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) |
| 74 | { |
| 75 | unsigned char *tmp = msgb->tail; |
| 76 | msgb->tail += len; |
| 77 | msgb->len += len; |
| 78 | return tmp; |
| 79 | } |
| 80 | static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) |
| 81 | { |
| 82 | msgb->data -= len; |
| 83 | msgb->len += len; |
| 84 | return msgb->data; |
| 85 | } |
| 86 | static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) |
| 87 | { |
| 88 | msgb->len -= len; |
| 89 | return msgb->data += len; |
| 90 | } |
| 91 | static inline int msgb_tailroom(const struct msgb *msgb) |
| 92 | { |
| 93 | return (msgb->data + msgb->data_len) - msgb->tail; |
| 94 | } |
| 95 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 96 | /* increase the headroom of an empty msgb, reducing the tailroom */ |
| 97 | static inline void msgb_reserve(struct msgb *msg, int len) |
| 98 | { |
| 99 | msg->data += len; |
| 100 | msg->tail += len; |
| 101 | } |
| 102 | |
| 103 | static inline struct msgb *msgb_alloc_headroom(int size, int headroom) |
| 104 | { |
| 105 | struct msgb *msg = msgb_alloc(size); |
| 106 | if (msg) |
| 107 | msgb_reserve(msg, headroom); |
| 108 | return msg; |
| 109 | } |
| 110 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 111 | #endif /* _MSGB_H */ |