From f78ec5ce0d0f6038147d9b9e14d81094309ba5d5 Mon Sep 17 00:00:00 2001 From: Jacob Erlbeck Date: Tue, 17 Nov 2015 09:53:23 +0100 Subject: gb: Add bssgp_msgb_copy function This function originates from openbsc/src/gprs but is just specific to BSSGP/Gb on the same level like bssgp_msgb_alloc. This commit puts the former gprs_msgb_copy function beside bssgp_msgb_alloc. Renamed function: gprs_msgb_copy -> bssgp_msgb_copy Sponsored-by: On-Waves ehf --- include/osmocom/gprs/gprs_bssgp.h | 1 + src/gb/gprs_bssgp_util.c | 30 ++++++++++++++++++++++++++++++ src/gb/libosmogb.map | 1 + tests/gb/gprs_bssgp_test.c | 34 ++++++++++++++++++++++++++++++++++ tests/gb/gprs_bssgp_test.ok | 4 ++++ 5 files changed, 70 insertions(+) diff --git a/include/osmocom/gprs/gprs_bssgp.h b/include/osmocom/gprs/gprs_bssgp.h index e24b563e..c0b3f656 100644 --- a/include/osmocom/gprs/gprs_bssgp.h +++ b/include/osmocom/gprs/gprs_bssgp.h @@ -12,6 +12,7 @@ /* gprs_bssgp_util.c */ extern struct gprs_ns_inst *bssgp_nsi; struct msgb *bssgp_msgb_alloc(void); +struct msgb *bssgp_msgb_copy(const struct msgb *msg, const char *name); const char *bssgp_cause_str(enum gprs_bssgp_cause cause); /* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */ int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei, diff --git a/src/gb/gprs_bssgp_util.c b/src/gb/gprs_bssgp_util.c index 3c42e4d4..19ae23a0 100644 --- a/src/gb/gprs_bssgp_util.c +++ b/src/gb/gprs_bssgp_util.c @@ -79,6 +79,36 @@ struct msgb *bssgp_msgb_alloc(void) return msg; } +struct msgb *bssgp_msgb_copy(const struct msgb *msg, const char *name) +{ + struct libgb_msgb_cb *old_cb, *new_cb; + struct msgb *new_msg; + + new_msg = msgb_copy(msg, name); + if (!new_msg) + return NULL; + + /* copy GB specific data */ + old_cb = LIBGB_MSGB_CB(msg); + new_cb = LIBGB_MSGB_CB(new_msg); + + if (old_cb->bssgph) + new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data); + if (old_cb->llch) + new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data); + + /* bssgp_cell_id is a pointer into the old msgb, so we need to make + * it a pointer into the new msgb */ + if (old_cb->bssgp_cell_id) + new_cb->bssgp_cell_id = new_msg->_data + + (old_cb->bssgp_cell_id - msg->_data); + new_cb->nsei = old_cb->nsei; + new_cb->bvci = old_cb->bvci; + new_cb->tlli = old_cb->tlli; + + return new_msg; +} + /* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */ int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei, uint16_t bvci, uint16_t ns_bvci) diff --git a/src/gb/libosmogb.map b/src/gb/libosmogb.map index 43ebbf8b..75406c04 100644 --- a/src/gb/libosmogb.map +++ b/src/gb/libosmogb.map @@ -6,6 +6,7 @@ bssgp_fc_in; bssgp_fc_init; bssgp_fc_ms_init; bssgp_msgb_alloc; +bssgp_msgb_copy; bssgp_msgb_tlli_put; bssgp_parse_cell_id; bssgp_tx_bvc_block; diff --git a/tests/gb/gprs_bssgp_test.c b/tests/gb/gprs_bssgp_test.c index 707b64bf..3de05ddb 100644 --- a/tests/gb/gprs_bssgp_test.c +++ b/tests/gb/gprs_bssgp_test.c @@ -254,6 +254,39 @@ static void test_bssgp_flow_control_bvc(void) printf("----- %s END\n", __func__); } +static void test_bssgp_msgb_copy() +{ + struct msgb *msg, *msg2; + uint16_t bvci_be = htons(2); + uint8_t cause = BSSGP_CAUSE_OML_INTERV; + + printf("----- %s START\n", __func__); + msg = bssgp_msgb_alloc(); + + msg->l3h = msgb_data(msg); + msgb_v_put(msg, BSSGP_PDUT_BVC_RESET); + msgb_tvlv_put(msg, BSSGP_IE_BVCI, sizeof(bvci_be), (uint8_t *)&bvci_be); + msgb_tvlv_put(msg, BSSGP_IE_CAUSE, sizeof(cause), &cause); + + msgb_bvci(msg) = 0xbad; + msgb_nsei(msg) = 0xbee; + + printf("Old msgb: %s\n", msgb_hexdump(msg)); + msg2 = bssgp_msgb_copy(msg, "test"); + printf("New msgb: %s\n", msgb_hexdump(msg2)); + + OSMO_ASSERT(msgb_bvci(msg2) == 0xbad); + OSMO_ASSERT(msgb_nsei(msg2) == 0xbee); + OSMO_ASSERT(msgb_l3(msg2) == msgb_data(msg2)); + OSMO_ASSERT(msgb_bssgph(msg2) == msgb_data(msg2)); + OSMO_ASSERT(msgb_bssgp_len(msg2) == msgb_length(msg2)); + + msgb_free(msg); + msgb_free(msg2); + + printf("----- %s END\n", __func__); +} + static struct log_info info = {}; int main(int argc, char **argv) @@ -278,6 +311,7 @@ int main(int argc, char **argv) test_bssgp_status(); test_bssgp_bad_reset(); test_bssgp_flow_control_bvc(); + test_bssgp_msgb_copy(); printf("===== BSSGP test END\n\n"); exit(EXIT_SUCCESS); diff --git a/tests/gb/gprs_bssgp_test.ok b/tests/gb/gprs_bssgp_test.ok index 83d633bc..c5b3e7d1 100644 --- a/tests/gb/gprs_bssgp_test.ok +++ b/tests/gb/gprs_bssgp_test.ok @@ -13,5 +13,9 @@ BSSGP primitive, SAP 16777221, prim = 11, op = 2, msg = 41 07 81 05 04 82 04 d2 Got message: 26 1e 81 2a 05 82 10 22 03 82 c0 40 01 82 08 11 1c 82 60 20 Got message: 26 1e 81 2a 05 82 10 22 03 82 c0 40 01 82 08 11 1c 82 60 20 3c 81 78 06 82 11 44 ----- test_bssgp_flow_control_bvc END +----- test_bssgp_msgb_copy START +Old msgb: [L3]> 22 04 82 00 02 07 81 08 +New msgb: [L3]> 22 04 82 00 02 07 81 08 +----- test_bssgp_msgb_copy END ===== BSSGP test END -- cgit v1.2.3