Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1 | /* MSC subscriber connection implementation */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2016 by sysmocom s.m.f.c. <info@sysmocom.de> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Neels Hofmeyr |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <osmocom/core/logging.h> |
| 25 | #include <osmocom/core/fsm.h> |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 26 | #include <osmocom/core/signal.h> |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 27 | |
| 28 | #include <openbsc/osmo_msc.h> |
| 29 | #include <openbsc/vlr.h> |
| 30 | #include <openbsc/debug.h> |
| 31 | #include <openbsc/transaction.h> |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 32 | #include <openbsc/signal.h> |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 33 | #include <openbsc/iu.h> |
Philipp Maier | 4b60d07 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 34 | #include <openbsc/a_iface.h> |
| 35 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 36 | |
| 37 | #define SUBSCR_CONN_TIMEOUT 5 /* seconds */ |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 38 | |
| 39 | static const struct value_string subscr_conn_fsm_event_names[] = { |
| 40 | OSMO_VALUE_STRING(SUBSCR_CONN_E_INVALID), |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 41 | OSMO_VALUE_STRING(SUBSCR_CONN_E_START), |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 42 | OSMO_VALUE_STRING(SUBSCR_CONN_E_ACCEPTED), |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 43 | OSMO_VALUE_STRING(SUBSCR_CONN_E_COMMUNICATING), |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 44 | OSMO_VALUE_STRING(SUBSCR_CONN_E_BUMP), |
| 45 | OSMO_VALUE_STRING(SUBSCR_CONN_E_MO_CLOSE), |
| 46 | OSMO_VALUE_STRING(SUBSCR_CONN_E_CN_CLOSE), |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 47 | { 0, NULL } |
| 48 | }; |
| 49 | |
| 50 | const struct value_string subscr_conn_from_names[] = { |
| 51 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_INVALID), |
| 52 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_LU), |
| 53 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_CM_SERVICE_REQ), |
| 54 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_PAGING_RESP), |
| 55 | { 0, NULL } |
| 56 | }; |
| 57 | |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 58 | static void paging_event(struct gsm_subscriber_connection *conn, |
| 59 | enum gsm_paging_event pe) |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 60 | { |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 61 | subscr_paging_dispatch(GSM_HOOK_RR_PAGING, pe, NULL, conn, conn->vsub); |
| 62 | } |
| 63 | |
| 64 | void subscr_conn_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 65 | { |
| 66 | OSMO_ASSERT(event == SUBSCR_CONN_E_START); |
| 67 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_NEW, |
| 68 | SUBSCR_CONN_TIMEOUT, 0); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void subscr_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 72 | { |
| 73 | struct gsm_subscriber_connection *conn = fi->priv; |
| 74 | enum subscr_conn_from from = SUBSCR_CONN_FROM_INVALID; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 75 | bool success; |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 76 | |
| 77 | if (data) { |
| 78 | from = *(enum subscr_conn_from*)data; |
| 79 | LOGPFSM(fi, "%s\n", subscr_conn_from_name(from)); |
| 80 | } |
| 81 | |
| 82 | /* If accepted, transition the state, all other cases mean failure. */ |
| 83 | switch (event) { |
| 84 | case SUBSCR_CONN_E_ACCEPTED: |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 85 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, |
| 86 | SUBSCR_CONN_TIMEOUT, 0); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 87 | break; |
| 88 | |
| 89 | case SUBSCR_CONN_E_MO_CLOSE: |
| 90 | case SUBSCR_CONN_E_CN_CLOSE: |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 91 | if (data) |
| 92 | LOGPFSM(fi, "Close event, cause %u\n", |
| 93 | *(uint32_t*)data); |
| 94 | /* will release further below, see |
| 95 | * 'if (fi->state != SUBSCR_CONN_S_ACCEPTED)' */ |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 96 | break; |
| 97 | |
| 98 | default: |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 99 | LOGPFSML(fi, LOGL_ERROR, |
| 100 | "Unexpected event: %d %s\n", event, |
| 101 | osmo_fsm_event_name(fi->fsm, event)); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 105 | success = (fi->state == SUBSCR_CONN_S_ACCEPTED); |
| 106 | |
| 107 | if (from == SUBSCR_CONN_FROM_LU) |
| 108 | rate_ctr_inc(&conn->network->msc_ctrs->ctr[ |
| 109 | success ? MSC_CTR_LOC_UPDATE_COMPLETED |
| 110 | : MSC_CTR_LOC_UPDATE_FAILED]); |
| 111 | |
| 112 | /* signal paging success or failure in case this was a paging */ |
| 113 | if (from == SUBSCR_CONN_FROM_PAGING_RESP) |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 114 | paging_event(conn, |
| 115 | success ? GSM_PAGING_SUCCEEDED |
| 116 | : GSM_PAGING_EXPIRED); |
| 117 | |
| 118 | /* FIXME rate counters */ |
| 119 | /*rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_COMPLETED]);*/ |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 120 | |
| 121 | /* On failure, discard the conn */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 122 | if (!success) { |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 123 | /* TODO: on MO_CLOSE or CN_CLOSE, first go to RELEASING and |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 124 | * await BSC/RNC confirmation? */ |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 125 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 126 | return; |
| 127 | } |
| 128 | |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 129 | if (from == SUBSCR_CONN_FROM_CM_SERVICE_REQ) { |
| 130 | conn->received_cm_service_request = true; |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 131 | LOGPFSML(fi, LOGL_DEBUG, "received_cm_service_request = true\n"); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_BUMP, data); |
| 135 | } |
| 136 | |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 137 | static void subscr_conn_fsm_bump(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 138 | { |
| 139 | struct gsm_subscriber_connection *conn = fi->priv; |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 140 | struct gsm_trans *trans; |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 141 | |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 142 | if (conn->silent_call) { |
| 143 | LOGPFSML(fi, LOGL_DEBUG, "bump: silent call still active\n"); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 144 | return; |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 145 | } |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 146 | |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 147 | if (conn->received_cm_service_request) { |
| 148 | LOGPFSML(fi, LOGL_DEBUG, "bump: still awaiting first request after a CM Service Request\n"); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 149 | return; |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 150 | } |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 151 | |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 152 | if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) { |
| 153 | struct subscr_request *sr; |
| 154 | if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) { |
| 155 | llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) { |
| 156 | LOGPFSML(fi, LOGL_DEBUG, "bump: still active: %s\n", |
| 157 | sr->label); |
| 158 | } |
| 159 | } |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 160 | return; |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 161 | } |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 162 | |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 163 | if ((trans = trans_has_conn(conn))) { |
| 164 | LOGPFSML(fi, LOGL_DEBUG, |
| 165 | "bump: connection still has active transaction: %s\n", |
| 166 | gsm48_pdisc_name(trans->protocol)); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 167 | return; |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 168 | } |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 169 | |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 170 | LOGPFSML(fi, LOGL_DEBUG, "bump: releasing conn\n"); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 171 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 172 | } |
| 173 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 174 | static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 175 | { |
| 176 | struct gsm_subscriber_connection *conn = fi->priv; |
| 177 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, conn->vsub); |
| 178 | } |
| 179 | |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 180 | static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 181 | { |
| 182 | switch (event) { |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 183 | case SUBSCR_CONN_E_COMMUNICATING: |
| 184 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0); |
| 185 | return; |
| 186 | |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 187 | case SUBSCR_CONN_E_BUMP: |
| 188 | subscr_conn_fsm_bump(fi, event, data); |
| 189 | return; |
| 190 | |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | /* Whatever unexpected happens in the accepted state, it means release. |
| 195 | * Even if an unexpected event is passed, the safest thing to do is |
| 196 | * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */ |
| 197 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 198 | } |
| 199 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 200 | static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 201 | { |
| 202 | switch (event) { |
| 203 | case SUBSCR_CONN_E_COMMUNICATING: |
| 204 | /* no-op */ |
| 205 | return; |
| 206 | |
| 207 | case SUBSCR_CONN_E_BUMP: |
| 208 | subscr_conn_fsm_bump(fi, event, data); |
| 209 | return; |
| 210 | |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | /* Whatever unexpected happens in the accepted state, it means release. |
| 215 | * Even if an unexpected event is passed, the safest thing to do is |
| 216 | * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */ |
| 217 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 218 | } |
| 219 | |
| 220 | static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, |
| 221 | enum osmo_fsm_term_cause cause) |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 222 | { |
| 223 | struct gsm_subscriber_connection *conn = fi->priv; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 224 | fi->priv = NULL; |
| 225 | |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 226 | if (!conn) |
| 227 | return; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 228 | conn->conn_fsm = NULL; |
| 229 | msc_subscr_conn_close(conn, cause); |
| 230 | msc_subscr_conn_put(conn); |
| 231 | } |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 232 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 233 | int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi) |
| 234 | { |
| 235 | struct gsm_subscriber_connection *conn = fi->priv; |
| 236 | if (conn) |
| 237 | vlr_subscr_conn_timeout(conn->vsub); |
| 238 | osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, NULL); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static void subscr_conn_fsm_release(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 243 | { |
| 244 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | #define S(x) (1 << (x)) |
| 248 | |
| 249 | static const struct osmo_fsm_state subscr_conn_fsm_states[] = { |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 250 | [SUBSCR_CONN_S_INIT] = { |
| 251 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_INIT), |
| 252 | .in_event_mask = S(SUBSCR_CONN_E_START), |
| 253 | .out_state_mask = S(SUBSCR_CONN_S_NEW), |
| 254 | .action = subscr_conn_fsm_init, |
| 255 | }, |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 256 | [SUBSCR_CONN_S_NEW] = { |
| 257 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW), |
| 258 | .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) | |
| 259 | S(SUBSCR_CONN_E_MO_CLOSE) | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 260 | S(SUBSCR_CONN_E_CN_CLOSE), |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 261 | .out_state_mask = S(SUBSCR_CONN_S_ACCEPTED) | |
| 262 | S(SUBSCR_CONN_S_RELEASED), |
| 263 | .action = subscr_conn_fsm_new, |
| 264 | }, |
| 265 | [SUBSCR_CONN_S_ACCEPTED] = { |
| 266 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED), |
| 267 | /* allow everything to release for any odd behavior */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 268 | .in_event_mask = S(SUBSCR_CONN_E_COMMUNICATING) | |
| 269 | S(SUBSCR_CONN_E_BUMP) | |
| 270 | S(SUBSCR_CONN_E_ACCEPTED) | |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 271 | S(SUBSCR_CONN_E_MO_CLOSE) | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 272 | S(SUBSCR_CONN_E_CN_CLOSE), |
| 273 | .out_state_mask = S(SUBSCR_CONN_S_RELEASED) | |
| 274 | S(SUBSCR_CONN_S_COMMUNICATING), |
| 275 | .onenter = subscr_conn_fsm_accepted_enter, |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 276 | .action = subscr_conn_fsm_accepted, |
| 277 | }, |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 278 | [SUBSCR_CONN_S_COMMUNICATING] = { |
| 279 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING), |
| 280 | /* allow everything to release for any odd behavior */ |
| 281 | .in_event_mask = S(SUBSCR_CONN_E_BUMP) | |
| 282 | S(SUBSCR_CONN_E_ACCEPTED) | |
| 283 | S(SUBSCR_CONN_E_COMMUNICATING) | |
| 284 | S(SUBSCR_CONN_E_MO_CLOSE) | |
| 285 | S(SUBSCR_CONN_E_CN_CLOSE), |
| 286 | .out_state_mask = S(SUBSCR_CONN_S_RELEASED), |
| 287 | .action = subscr_conn_fsm_communicating, |
| 288 | }, |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 289 | [SUBSCR_CONN_S_RELEASED] = { |
| 290 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED), |
| 291 | .onenter = subscr_conn_fsm_release, |
| 292 | }, |
| 293 | }; |
| 294 | |
| 295 | static struct osmo_fsm subscr_conn_fsm = { |
| 296 | .name = "Subscr_Conn", |
| 297 | .states = subscr_conn_fsm_states, |
| 298 | .num_states = ARRAY_SIZE(subscr_conn_fsm_states), |
| 299 | .allstate_event_mask = 0, |
| 300 | .allstate_action = NULL, |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 301 | .log_subsys = DMM, |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 302 | .event_names = subscr_conn_fsm_event_names, |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 303 | .cleanup = subscr_conn_fsm_cleanup, |
| 304 | .timer_cb = subscr_conn_fsm_timeout, |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | int msc_create_conn_fsm(struct gsm_subscriber_connection *conn, const char *id) |
| 308 | { |
| 309 | struct osmo_fsm_inst *fi; |
| 310 | OSMO_ASSERT(conn); |
| 311 | |
| 312 | if (conn->conn_fsm) { |
| 313 | LOGP(DMM, LOGL_ERROR, |
| 314 | "%s: Error: connection already in use\n", id); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 318 | /* Allocate the FSM not with the subscr_conn. Semantically it would |
| 319 | * make sense, but in subscr_conn_fsm_cleanup(), we want to discard the |
| 320 | * subscriber connection. If the FSM is freed along with the subscriber |
| 321 | * connection, then in _osmo_fsm_inst_term() the osmo_fsm_inst_free() |
| 322 | * that follows the cleanup() call would run into a double free. */ |
| 323 | fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, conn->network, |
| 324 | msc_subscr_conn_get(conn), |
| 325 | LOGL_DEBUG, id); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 326 | |
| 327 | if (!fi) { |
| 328 | LOGP(DMM, LOGL_ERROR, |
| 329 | "%s: Failed to allocate subscr conn master FSM\n", id); |
| 330 | return -ENOMEM; |
| 331 | } |
| 332 | conn->conn_fsm = fi; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 333 | osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_START, NULL); |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | bool msc_subscr_conn_is_accepted(struct gsm_subscriber_connection *conn) |
| 338 | { |
| 339 | if (!conn) |
| 340 | return false; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 341 | if (!conn->vsub) |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 342 | return false; |
| 343 | if (!conn->conn_fsm) |
| 344 | return false; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 345 | if (!(conn->conn_fsm->state == SUBSCR_CONN_S_ACCEPTED |
| 346 | || conn->conn_fsm->state == SUBSCR_CONN_S_COMMUNICATING)) |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 347 | return false; |
| 348 | return true; |
| 349 | } |
| 350 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 351 | void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn) |
| 352 | { |
| 353 | OSMO_ASSERT(conn); |
| 354 | osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_COMMUNICATING, |
| 355 | NULL); |
| 356 | } |
| 357 | |
Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 358 | void msc_subscr_conn_init(void) |
| 359 | { |
| 360 | osmo_fsm_register(&subscr_conn_fsm); |
| 361 | } |