blob: 3825de1198ab88877b2c42c79a02f0e7874a70fc [file] [log] [blame]
Harald Welte9ee48252009-07-23 21:25:08 +02001/* 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
32static void *tall_trans_ctx;
33
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020034void _gsm48_cc_trans_free(struct gsm_trans *trans);
35
Harald Welteb8b40732009-07-23 21:58:40 +020036struct gsm_trans *trans_find_by_id(struct gsm_subscriber *subscr,
37 u_int8_t proto, u_int8_t trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020038{
39 struct gsm_trans *trans;
Harald Welteb8b40732009-07-23 21:58:40 +020040 struct gsm_network *net = subscr->net;
Harald Welte9ee48252009-07-23 21:25:08 +020041
42 llist_for_each_entry(trans, &net->trans_list, entry) {
Harald Welteb8b40732009-07-23 21:58:40 +020043 if (trans->subscr == subscr &&
44 trans->protocol == proto &&
45 trans->transaction_id == trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020046 return trans;
47 }
48 return NULL;
49}
50
51struct gsm_trans *trans_find_by_callref(struct gsm_network *net,
52 u_int32_t callref)
53{
54 struct gsm_trans *trans;
55
56 llist_for_each_entry(trans, &net->trans_list, entry) {
57 if (trans->callref == callref)
58 return trans;
59 }
60 return NULL;
61}
62
63struct gsm_trans *trans_alloc(struct gsm_subscriber *subscr,
64 u_int8_t protocol, u_int8_t trans_id,
65 u_int32_t callref)
66{
67 struct gsm_trans *trans;
68
69 DEBUGP(DCC, "subscr=%p, subscr->net=%p\n", subscr, subscr->net);
70
71 trans = talloc_zero(tall_trans_ctx, struct gsm_trans);
72 if (!trans)
73 return NULL;
74
75 trans->subscr = subscr;
76 subscr_get(trans->subscr);
77
78 trans->protocol = protocol;
79 trans->transaction_id = trans_id;
80 trans->callref = callref;
81
82 llist_add_tail(&trans->entry, &subscr->net->trans_list);
83
84 return trans;
85}
86
87void trans_free(struct gsm_trans *trans)
88{
89 struct gsm_bts *bts;
90
91 switch (trans->protocol) {
92 case GSM48_PDISC_CC:
93 _gsm48_cc_trans_free(trans);
94 break;
95 }
96
97 if (trans->lchan)
98 put_lchan(trans->lchan);
99
100 if (!trans->lchan && trans->subscr && trans->subscr->net) {
101 /* Stop paging on all bts' */
102 bts = NULL;
103 do {
104 bts = gsm_bts_by_lac(trans->subscr->net,
105 trans->subscr->lac, bts);
106 if (!bts)
107 break;
108 /* Stop paging */
109 paging_request_stop(bts, trans->subscr, NULL);
110 } while (1);
111 }
112
113 if (trans->subscr)
114 subscr_put(trans->subscr);
115
116 llist_del(&trans->entry);
117
118 talloc_free(trans);
119}
120
Harald Welte78283ef2009-07-23 21:36:44 +0200121/* allocate an unused transaction ID for the given subscriber
122 * in the given protocol using the ti_flag specified */
Harald Welte9ee48252009-07-23 21:25:08 +0200123int trans_assign_trans_id(struct gsm_subscriber *subscr,
124 u_int8_t protocol, u_int8_t ti_flag)
125{
126 struct gsm_network *net = subscr->net;
127 struct gsm_trans *trans;
128 unsigned int used_tid_bitmask = 0;
Harald Welte78283ef2009-07-23 21:36:44 +0200129 int i;
130
131 if (ti_flag)
132 ti_flag = 0x8;
Harald Welte9ee48252009-07-23 21:25:08 +0200133
134 /* generate bitmask of already-used TIDs for this (subscr,proto) */
135 llist_for_each_entry(trans, &net->trans_list, entry) {
136 if (trans->subscr != subscr ||
137 trans->protocol != protocol ||
138 trans->transaction_id == 0xff)
139 continue;
140 used_tid_bitmask |= (1 << trans->transaction_id);
141 }
142
Harald Welte78283ef2009-07-23 21:36:44 +0200143 for (i = 0; i <= 7; i++) {
144 if ((used_tid_bitmask & (1 << (i | ti_flag))) == 0)
145 return i | ti_flag;
146 }
147
148 return -1;
Harald Welte9ee48252009-07-23 21:25:08 +0200149}