Harald Welte | c072ad6 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 1 | /* GSM 04.07 Transaction handling */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/transaction.h> |
| 23 | #include <openbsc/gsm_data.h> |
| 24 | #include <openbsc/mncc.h> |
| 25 | #include <openbsc/debug.h> |
| 26 | #include <openbsc/talloc.h> |
| 27 | #include <openbsc/gsm_subscriber.h> |
| 28 | #include <openbsc/gsm_04_08.h> |
| 29 | #include <openbsc/mncc.h> |
| 30 | #include <openbsc/paging.h> |
| 31 | |
| 32 | static void *tall_trans_ctx; |
| 33 | |
| 34 | struct gsm_trans *trans_find_by_id(struct gsm_lchan *lchan, u_int8_t trans_id) |
| 35 | { |
| 36 | struct gsm_trans *trans; |
| 37 | struct gsm_network *net = lchan->ts->trx->bts->network; |
| 38 | |
| 39 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 40 | if (trans->lchan == lchan && trans->transaction_id == trans_id) |
| 41 | return trans; |
| 42 | } |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | struct gsm_trans *trans_find_by_callref(struct gsm_network *net, |
| 47 | u_int32_t callref) |
| 48 | { |
| 49 | struct gsm_trans *trans; |
| 50 | |
| 51 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 52 | if (trans->callref == callref) |
| 53 | return trans; |
| 54 | } |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | struct gsm_trans *trans_alloc(struct gsm_subscriber *subscr, |
| 59 | u_int8_t protocol, u_int8_t trans_id, |
| 60 | u_int32_t callref) |
| 61 | { |
| 62 | struct gsm_trans *trans; |
| 63 | |
| 64 | DEBUGP(DCC, "subscr=%p, subscr->net=%p\n", subscr, subscr->net); |
| 65 | |
| 66 | trans = talloc_zero(tall_trans_ctx, struct gsm_trans); |
| 67 | if (!trans) |
| 68 | return NULL; |
| 69 | |
| 70 | trans->subscr = subscr; |
| 71 | subscr_get(trans->subscr); |
| 72 | |
| 73 | trans->protocol = protocol; |
| 74 | trans->transaction_id = trans_id; |
| 75 | trans->callref = callref; |
| 76 | |
| 77 | llist_add_tail(&trans->entry, &subscr->net->trans_list); |
| 78 | |
| 79 | return trans; |
| 80 | } |
| 81 | |
| 82 | void trans_free(struct gsm_trans *trans) |
| 83 | { |
| 84 | struct gsm_bts *bts; |
| 85 | |
| 86 | switch (trans->protocol) { |
| 87 | case GSM48_PDISC_CC: |
| 88 | _gsm48_cc_trans_free(trans); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | if (trans->lchan) |
| 93 | put_lchan(trans->lchan); |
| 94 | |
| 95 | if (!trans->lchan && trans->subscr && trans->subscr->net) { |
| 96 | /* Stop paging on all bts' */ |
| 97 | bts = NULL; |
| 98 | do { |
| 99 | bts = gsm_bts_by_lac(trans->subscr->net, |
| 100 | trans->subscr->lac, bts); |
| 101 | if (!bts) |
| 102 | break; |
| 103 | /* Stop paging */ |
| 104 | paging_request_stop(bts, trans->subscr, NULL); |
| 105 | } while (1); |
| 106 | } |
| 107 | |
| 108 | if (trans->subscr) |
| 109 | subscr_put(trans->subscr); |
| 110 | |
| 111 | llist_del(&trans->entry); |
| 112 | |
| 113 | talloc_free(trans); |
| 114 | } |
| 115 | |
Harald Welte | b49248b | 2009-07-23 21:36:44 +0200 | [diff] [blame^] | 116 | /* allocate an unused transaction ID for the given subscriber |
| 117 | * in the given protocol using the ti_flag specified */ |
Harald Welte | c072ad6 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 118 | int trans_assign_trans_id(struct gsm_subscriber *subscr, |
| 119 | u_int8_t protocol, u_int8_t ti_flag) |
| 120 | { |
| 121 | struct gsm_network *net = subscr->net; |
| 122 | struct gsm_trans *trans; |
| 123 | unsigned int used_tid_bitmask = 0; |
Harald Welte | b49248b | 2009-07-23 21:36:44 +0200 | [diff] [blame^] | 124 | int i; |
| 125 | |
| 126 | if (ti_flag) |
| 127 | ti_flag = 0x8; |
Harald Welte | c072ad6 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 128 | |
| 129 | /* generate bitmask of already-used TIDs for this (subscr,proto) */ |
| 130 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 131 | if (trans->subscr != subscr || |
| 132 | trans->protocol != protocol || |
| 133 | trans->transaction_id == 0xff) |
| 134 | continue; |
| 135 | used_tid_bitmask |= (1 << trans->transaction_id); |
| 136 | } |
| 137 | |
Harald Welte | b49248b | 2009-07-23 21:36:44 +0200 | [diff] [blame^] | 138 | for (i = 0; i <= 7; i++) { |
| 139 | if ((used_tid_bitmask & (1 << (i | ti_flag))) == 0) |
| 140 | return i | ti_flag; |
| 141 | } |
| 142 | |
| 143 | return -1; |
Harald Welte | c072ad6 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 144 | } |