Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 1 | /*! \file v110_ta.c |
| 2 | * TA (Terminal Adapter) implementation as per ITU-T V.110. */ |
| 3 | /* |
| 4 | * (C) 2022 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 6 | * |
| 7 | * Initial (Work-in-Progress) implementation by Harald Welte, |
| 8 | * completed and co-authored by Vadim Yanitskiy. |
| 9 | * |
| 10 | * All Rights Reserved |
| 11 | * |
| 12 | * SPDX-License-Identifier: GPL-2.0+ |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <osmocom/core/logging.h> |
| 31 | #include <osmocom/core/talloc.h> |
| 32 | #include <osmocom/core/utils.h> |
| 33 | #include <osmocom/core/bits.h> |
| 34 | #include <osmocom/core/tdef.h> |
| 35 | #include <osmocom/core/fsm.h> |
| 36 | |
| 37 | #include <osmocom/isdn/v110.h> |
| 38 | #include <osmocom/isdn/v110_ta.h> |
| 39 | |
| 40 | #define S(x) (1 << (x)) |
| 41 | |
| 42 | #define V24_FLAGMASK_IS_ON(flags, circuit) \ |
| 43 | (((flags) & S(circuit)) != 0) |
| 44 | |
| 45 | #define V24_FLAGMASK_IS_OFF(flags, circuit) \ |
| 46 | (((flags) & S(circuit)) == 0) |
| 47 | |
| 48 | #define V24_FLAGMASK_SET_ON(flags, circuit) \ |
| 49 | (flags) |= S(circuit) |
| 50 | |
| 51 | #define V24_FLAGMASK_SET_OFF(flags, circuit) \ |
| 52 | (flags) &= ~S(circuit) |
| 53 | |
| 54 | /* inverse logic: ON = binary 0; OFF = binary 1 */ |
| 55 | #define V110_SX_BIT_ON 0 |
| 56 | #define V110_SX_BIT_OFF 1 |
| 57 | |
| 58 | const struct value_string osmo_v110_ta_circuit_names[] = { |
| 59 | { OSMO_V110_TA_C_105, "105/RTS" }, |
| 60 | { OSMO_V110_TA_C_106, "106/CTS" }, |
| 61 | { OSMO_V110_TA_C_107, "107/DSR" }, |
| 62 | { OSMO_V110_TA_C_108, "108/DTR" }, |
| 63 | { OSMO_V110_TA_C_109, "109/DCD" }, |
| 64 | { OSMO_V110_TA_C_133, "133" }, |
| 65 | { 0, NULL } |
| 66 | }; |
| 67 | |
| 68 | const struct value_string osmo_v110_ta_circuit_descs[] = { |
| 69 | { OSMO_V110_TA_C_105, "Request to Send" }, |
| 70 | { OSMO_V110_TA_C_106, "Clear to Send" }, |
| 71 | { OSMO_V110_TA_C_107, "Data Set Ready" }, |
| 72 | { OSMO_V110_TA_C_108, "Data Terminal Ready" }, |
| 73 | { OSMO_V110_TA_C_109, "Data Carrier Detect" }, |
| 74 | { OSMO_V110_TA_C_133, "Ready for receiving" }, |
| 75 | { 0, NULL } |
| 76 | }; |
| 77 | |
| 78 | static const struct osmo_tdef v110_ta_tdef[] = { |
| 79 | { .T = OSMO_V110_TA_TIMER_X1, |
| 80 | .unit = OSMO_TDEF_MS, .default_val = 3000, /* suggested in 7.1.5 e) */ |
| 81 | .desc = "ITU-T V.110 7.1.5 Loss of frame synchronization: sync recovery timer" }, |
| 82 | { .T = OSMO_V110_TA_TIMER_T1, |
| 83 | .unit = OSMO_TDEF_MS, .default_val = 10000, /* suggested in 7.1.2.2 */ |
| 84 | .desc = "ITU-T V.110 7.1.2 Connect TA to line: sync establishment timer" }, |
| 85 | { .T = OSMO_V110_TA_TIMER_T2, |
| 86 | .unit = OSMO_TDEF_MS, .default_val = 5000, /* suggested in 7.1.4.1 */ |
| 87 | .desc = "ITU-T V.110 7.1.4 Disconnect mode: disconnect confirmation timer" }, |
| 88 | { /* end of list */ } |
| 89 | }; |
| 90 | |
| 91 | /********************************************************************************* |
| 92 | * V.110 TERMINAL ADAPTER FSM |
| 93 | *********************************************************************************/ |
| 94 | |
| 95 | enum v110_ta_fsm_state { |
| 96 | V110_TA_ST_IDLE_READY, /* 7.1.1 Idle (or ready) state */ |
| 97 | V110_TA_ST_CON_TA_TO_LINE, /* 7.1.2 Connect TA to line state */ |
| 98 | V110_TA_ST_DATA_TRANSFER, /* 7.1.3 Data transfer state */ |
| 99 | V110_TA_ST_DISCONNECTING, /* 7.1.4 Disconnect mode */ |
| 100 | V110_TA_ST_RESYNCING, /* 7.1.5 Re-synchronizing state */ |
| 101 | }; |
| 102 | |
| 103 | enum v110_ta_fsm_event { |
| 104 | V110_TA_EV_RX_FRAME_IND, /* a V.110 frame was received by the lower layer */ |
| 105 | V110_TA_EV_TX_FRAME_RTS, /* a V.110 frame is to be sent by the lower layer */ |
| 106 | V110_TA_EV_V24_STATUS_CHG, /* V.24 flag-mask has been updated by TE */ |
| 107 | V110_TA_EV_SYNC_IND, /* the lower layer has synchronized to the frame clock */ |
| 108 | V110_TA_EV_DESYNC_IND, /* the lower layer has lost frame clock synchronization */ |
| 109 | V110_TA_EV_TIMEOUT, /* generic event for handling a timeout condition */ |
| 110 | }; |
| 111 | |
| 112 | static const struct value_string v110_ta_fsm_event_names[] = { |
| 113 | { V110_TA_EV_RX_FRAME_IND, "RX_FRAME_IND" }, |
| 114 | { V110_TA_EV_TX_FRAME_RTS, "TX_FRAME_RTS" }, |
| 115 | { V110_TA_EV_V24_STATUS_CHG, "V24_STATUS_CHG" }, |
| 116 | { V110_TA_EV_SYNC_IND, "SYNC_IND" }, |
| 117 | { V110_TA_EV_DESYNC_IND, "DESYNC_IND" }, |
| 118 | { V110_TA_EV_TIMEOUT, "TIMEOUT" }, |
| 119 | { 0, NULL } |
| 120 | }; |
| 121 | |
| 122 | enum v110_ta_d_bit_mode { |
| 123 | V110_TA_DBIT_M_ALL_ZERO = 0, /* set all bits to binary '0' */ |
| 124 | V110_TA_DBIT_M_ALL_ONE = 1, /* set all bits to binary '1' */ |
| 125 | V110_TA_DBIT_M_FORWARD, /* forward D-bits to/from DTE */ |
| 126 | }; |
| 127 | |
| 128 | struct v110_ta_state { |
| 129 | /*! V.24 status flags shared between DTE (user) and DCE (TA, us) */ |
| 130 | unsigned int v24_flags; |
| 131 | struct { |
| 132 | /* what kind of D-bits to transmit in V.110 frames */ |
| 133 | enum v110_ta_d_bit_mode d_bit_mode; |
| 134 | /* what to put in S-bits of transmitted V.110 frames */ |
| 135 | ubit_t s_bits; |
| 136 | /* what to put in X-bits of transmitted V.110 frames */ |
| 137 | ubit_t x_bits; |
| 138 | } tx; |
| 139 | struct { |
| 140 | enum v110_ta_d_bit_mode d_bit_mode; |
| 141 | } rx; |
| 142 | }; |
| 143 | |
| 144 | struct osmo_v110_ta { |
| 145 | const char *name; |
| 146 | struct osmo_tdef *Tdefs; |
| 147 | struct osmo_fsm_inst *fi; |
| 148 | struct osmo_v110_ta_cfg *cfg; |
| 149 | struct v110_ta_state state; |
| 150 | }; |
| 151 | |
| 152 | static inline bool v110_df_x_bits_are(const struct osmo_v110_decoded_frame *df, ubit_t cmp) |
| 153 | { |
| 154 | return (df->x_bits[0] == cmp) && (df->x_bits[1] == cmp); |
| 155 | } |
| 156 | |
| 157 | static inline bool v110_df_s_bits_are(const struct osmo_v110_decoded_frame *df, ubit_t cmp) |
| 158 | { |
| 159 | /* ITU-T Table 2/V.110 (see also 5.1.2.3) defines the following S-bits: |
| 160 | * S1, S3, S4, S6, S8, S9 (6 bits total). However, fr->s_bits[] contains |
| 161 | * 9 (MAX_S_BITS) bits, including the undefined bits S2, S5, S7. |
| 162 | * Hence we must skip those undefined bits. */ |
| 163 | static const uint8_t sbit_map[] = { 0, 2, 3, 5, 7, 8 }; |
| 164 | |
| 165 | for (unsigned int i = 0; i < ARRAY_SIZE(sbit_map); i++) { |
| 166 | uint8_t idx = sbit_map[i]; |
| 167 | if (df->s_bits[idx] != cmp) |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | static inline bool v110_df_d_bits_are(const struct osmo_v110_decoded_frame *df, ubit_t cmp) |
| 175 | { |
| 176 | for (unsigned int i = 0; i < MAX_D_BITS; i++) { |
| 177 | if (df->d_bits[i] != cmp) |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | /* handle one V.110 frame and forward user bits to the application */ |
| 185 | static void v110_ta_handle_frame(const struct osmo_v110_ta *ta, |
| 186 | const struct osmo_v110_decoded_frame *df) |
| 187 | { |
| 188 | const struct osmo_v110_ta_cfg *cfg = ta->cfg; |
| 189 | const struct v110_ta_state *ts = &ta->state; |
| 190 | ubit_t user_bits[MAX_D_BITS]; |
| 191 | int num_user_bits; |
| 192 | int rc; |
| 193 | |
| 194 | switch (ts->rx.d_bit_mode) { |
| 195 | case V110_TA_DBIT_M_ALL_ZERO: |
| 196 | case V110_TA_DBIT_M_ALL_ONE: |
| 197 | /* generate as many user bits as needed for the configured rate */ |
| 198 | num_user_bits = osmo_v110_sync_ra1_get_user_data_chunk_bitlen(cfg->rate); |
| 199 | OSMO_ASSERT(num_user_bits > 0); |
| 200 | /* set them all to binary '0' or binary '1' */ |
| 201 | memset(&user_bits[0], (int)ts->rx.d_bit_mode, num_user_bits); |
| 202 | cfg->rx_cb(cfg->priv, &user_bits[0], num_user_bits); |
| 203 | break; |
| 204 | case V110_TA_DBIT_M_FORWARD: |
| 205 | rc = osmo_v110_sync_ra1_ir_to_user(cfg->rate, &user_bits[0], sizeof(user_bits), df); |
| 206 | if (rc > 0) |
| 207 | cfg->rx_cb(cfg->priv, &user_bits[0], rc); |
| 208 | /* XXX else: indicate an error somehow? */ |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* build one V.110 frame to transmit */ |
| 214 | static void v110_ta_build_frame(const struct osmo_v110_ta *ta, |
| 215 | struct osmo_v110_decoded_frame *df) |
| 216 | { |
| 217 | const struct osmo_v110_ta_cfg *cfg = ta->cfg; |
| 218 | const struct v110_ta_state *ts = &ta->state; |
| 219 | ubit_t user_bits[MAX_D_BITS]; |
| 220 | int num_user_bits; |
| 221 | int rc; |
| 222 | |
| 223 | /* E-bits (E1/E2/E3 may be overwritten below) */ |
| 224 | memset(df->e_bits, 1, sizeof(df->e_bits)); |
| 225 | /* S-bits */ |
| 226 | memset(df->s_bits, ts->tx.s_bits, sizeof(df->s_bits)); |
| 227 | /* X-bits */ |
| 228 | memset(df->x_bits, ts->tx.x_bits, sizeof(df->x_bits)); |
| 229 | |
| 230 | /* D-bits */ |
| 231 | switch (ts->tx.d_bit_mode) { |
| 232 | case V110_TA_DBIT_M_ALL_ZERO: |
| 233 | case V110_TA_DBIT_M_ALL_ONE: |
| 234 | /* set them all to binary '0' or binary '1' */ |
| 235 | memset(df->d_bits, (int)ts->tx.d_bit_mode, sizeof(df->d_bits)); |
| 236 | break; |
| 237 | case V110_TA_DBIT_M_FORWARD: |
| 238 | /* how many user bits to retrieve */ |
| 239 | num_user_bits = osmo_v110_sync_ra1_get_user_data_chunk_bitlen(cfg->rate); |
| 240 | OSMO_ASSERT(num_user_bits > 0); |
| 241 | /* retrieve user bits from the application */ |
| 242 | cfg->tx_cb(cfg->priv, &user_bits[0], num_user_bits); |
| 243 | /* convert user bits to intermediate rate (store to df) */ |
| 244 | rc = osmo_v110_sync_ra1_user_to_ir(cfg->rate, df, &user_bits[0], num_user_bits); |
| 245 | OSMO_ASSERT(rc == 0); |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 250 | static void v110_ta_flags_update(struct osmo_v110_ta *ta, unsigned int v24_flags) |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 251 | { |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 252 | struct osmo_v110_ta_cfg *cfg = ta->cfg; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 253 | |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 254 | if (ta->state.v24_flags == v24_flags) |
| 255 | return; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 256 | if (cfg->status_update_cb != NULL) |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 257 | cfg->status_update_cb(cfg->priv, v24_flags); |
| 258 | ta->state.v24_flags = v24_flags; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static const struct osmo_tdef_state_timeout v110_ta_fsm_timeouts[32] = { |
| 262 | [V110_TA_ST_RESYNCING] = { .T = OSMO_V110_TA_TIMER_X1 }, |
| 263 | [V110_TA_ST_CON_TA_TO_LINE] = { .T = OSMO_V110_TA_TIMER_T1 }, |
| 264 | [V110_TA_ST_DISCONNECTING] = { .T = OSMO_V110_TA_TIMER_T2 }, |
| 265 | }; |
| 266 | |
| 267 | #define v110_ta_fsm_state_chg(state) \ |
| 268 | osmo_tdef_fsm_inst_state_chg(fi, state, \ |
| 269 | v110_ta_fsm_timeouts, \ |
| 270 | ((struct osmo_v110_ta *)(fi->priv))->Tdefs, \ |
| 271 | 0) |
| 272 | |
| 273 | /* ITU-T V.110 Section 7.1.1 */ |
| 274 | static void v110_ta_fsm_idle_ready_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 275 | { |
| 276 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 277 | struct v110_ta_state *ts = &ta->state; |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 278 | unsigned int v24_flags = ta->state.v24_flags; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 279 | |
| 280 | /* 7.1.1.2 During the idle (or ready) state the TA will transmit continuous binary 1s into the B-channel */ |
| 281 | ts->tx.d_bit_mode = V110_TA_DBIT_M_ALL_ONE; /* circuit 103: continuous binary '1' */ |
| 282 | ts->tx.s_bits = V110_SX_BIT_OFF; /* OFF is binary '1' */ |
| 283 | ts->tx.x_bits = V110_SX_BIT_OFF; /* OFF is binary '1' */ |
| 284 | |
| 285 | /* 7.1.1.3 During the idle (or ready) state the TA (DCE) will transmit the following toward the DTE: */ |
| 286 | /* - circuit 104: continuous binary '1' */ |
| 287 | ts->rx.d_bit_mode = V110_TA_DBIT_M_ALL_ONE; |
| 288 | /* - circuits 107, 106, 109 = OFF */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 289 | V24_FLAGMASK_SET_OFF(v24_flags, OSMO_V110_TA_C_106); |
| 290 | V24_FLAGMASK_SET_OFF(v24_flags, OSMO_V110_TA_C_107); |
| 291 | V24_FLAGMASK_SET_OFF(v24_flags, OSMO_V110_TA_C_109); |
| 292 | v110_ta_flags_update(ta, v24_flags); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* ITU-T V.110 Section 7.1.1 */ |
| 296 | static void v110_ta_fsm_idle_ready(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 297 | { |
| 298 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 299 | struct v110_ta_state *ts = &ta->state; |
| 300 | |
| 301 | switch (event) { |
| 302 | case V110_TA_EV_V24_STATUS_CHG: |
| 303 | /* When the TA is to be switched to the data mode, circuit 108 must be ON */ |
| 304 | if (V24_FLAGMASK_IS_ON(ts->v24_flags, OSMO_V110_TA_C_108)) { |
| 305 | /* 7.12.2: Start timer T1 when switching to CON_TA_LINE */ |
| 306 | v110_ta_fsm_state_chg(V110_TA_ST_CON_TA_TO_LINE); |
| 307 | } |
| 308 | break; |
| 309 | case V110_TA_EV_RX_FRAME_IND: |
| 310 | v110_ta_handle_frame(ta, (const struct osmo_v110_decoded_frame *)data); |
| 311 | break; |
| 312 | case V110_TA_EV_TX_FRAME_RTS: |
| 313 | v110_ta_build_frame(ta, (struct osmo_v110_decoded_frame *)data); |
| 314 | break; |
| 315 | default: |
| 316 | OSMO_ASSERT(0); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* ITU-T V.110 Section 7.1.2 */ |
| 321 | static void v110_ta_fsm_connect_ta_to_line_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 322 | { |
| 323 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 324 | struct v110_ta_state *ts = &ta->state; |
| 325 | |
| 326 | /* 7.1.2.1 Switching to the data mode causes the TA to transmit the following towards the ISDN: */ |
| 327 | /* a) frame synchronization pattern as described in 5.1.3.1 and 5.2.1 (done by the API user) */ |
| 328 | /* b) circuit 103: continuous binary '1' */ |
| 329 | ts->tx.d_bit_mode = V110_TA_DBIT_M_ALL_ONE; |
| 330 | /* c) status bits S = OFF and X = OFF */ |
| 331 | ts->tx.s_bits = V110_SX_BIT_OFF; /* OFF is binary '1' */ |
| 332 | ts->tx.x_bits = V110_SX_BIT_OFF; /* OFF is binary '1' */ |
| 333 | |
| 334 | /* 7.1.2.2 ... the receiver in the TA will begin to search for the frame synchronization |
| 335 | * pattern in the received bit stream (see 5.1.3.1 and 5.2.1) and start timer T1. */ |
| 336 | OSMO_ASSERT(fi->T == OSMO_V110_TA_TIMER_T1); |
| 337 | } |
| 338 | |
| 339 | /* ITU-T V.110 Section 7.1.2 */ |
| 340 | static void v110_ta_fsm_connect_ta_to_line(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 341 | { |
| 342 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 343 | struct v110_ta_state *ts = &ta->state; |
| 344 | |
| 345 | switch (event) { |
| 346 | case V110_TA_EV_V24_STATUS_CHG: |
| 347 | /* If circuit 108 is OFF, we go back to IDLE/READY */ |
| 348 | if (V24_FLAGMASK_IS_OFF(ts->v24_flags, OSMO_V110_TA_C_108)) |
| 349 | v110_ta_fsm_state_chg(V110_TA_ST_IDLE_READY); |
| 350 | break; |
| 351 | case V110_TA_EV_SYNC_IND: |
| 352 | /* 7.1.2.3 When the receiver recognizes the frame synchronization pattern, it causes the S- |
| 353 | * and X-bits in the transmitted frames to be turned ON (provided that circuit 108 is ON). */ |
| 354 | OSMO_ASSERT(V24_FLAGMASK_IS_ON(ts->v24_flags, OSMO_V110_TA_C_108)); |
| 355 | ts->tx.s_bits = V110_SX_BIT_ON; |
| 356 | ts->tx.x_bits = V110_SX_BIT_ON; |
| 357 | break; |
| 358 | case V110_TA_EV_RX_FRAME_IND: |
| 359 | { |
| 360 | const struct osmo_v110_decoded_frame *df = data; |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 361 | unsigned int v24_flags = ta->state.v24_flags; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 362 | |
| 363 | /* 7.1.2.4 When the receiver recognizes that the status of bits S and X are ON */ |
| 364 | if (v110_df_s_bits_are(df, V110_SX_BIT_ON) && |
| 365 | v110_df_x_bits_are(df, V110_SX_BIT_ON)) { |
| 366 | /* ... it will perform the following functions: */ |
| 367 | /* a) Turn ON circuit 107 toward the DTE and stop timer T1 */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 368 | V24_FLAGMASK_SET_ON(v24_flags, OSMO_V110_TA_C_107); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 369 | osmo_timer_del(&fi->timer); |
| 370 | /* b) Then, circuit 103 may be connected to the data bits in the frame; however, the |
| 371 | * DTE must maintain a binary 1 condition on circuit 103 until circuit 106 is turned |
| 372 | * ON in the next portion of the sequence. */ |
| 373 | /* c) Turn ON circuit 109 and connect the data bits to circuit 104. */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 374 | V24_FLAGMASK_SET_ON(v24_flags, OSMO_V110_TA_C_109); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 375 | ts->rx.d_bit_mode = V110_TA_DBIT_M_FORWARD; |
| 376 | /* d) After an interval of N bits (see 6.3), it will turn ON circuit 106. */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 377 | V24_FLAGMASK_SET_ON(v24_flags, OSMO_V110_TA_C_106); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 378 | ts->tx.d_bit_mode = V110_TA_DBIT_M_FORWARD; |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 379 | v110_ta_flags_update(ta, v24_flags); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 380 | /* Circuit 106 transitioning from OFF to ON will cause the transmitted data to |
| 381 | * transition from binary 1 to the data mode. */ |
| 382 | v110_ta_fsm_state_chg(V110_TA_ST_DATA_TRANSFER); |
| 383 | } |
| 384 | |
| 385 | v110_ta_handle_frame(ta, df); |
| 386 | break; |
| 387 | } |
| 388 | case V110_TA_EV_TX_FRAME_RTS: |
| 389 | v110_ta_build_frame(ta, (struct osmo_v110_decoded_frame *)data); |
| 390 | break; |
| 391 | case V110_TA_EV_TIMEOUT: |
| 392 | v110_ta_fsm_state_chg(V110_TA_ST_IDLE_READY); |
| 393 | break; |
| 394 | default: |
| 395 | OSMO_ASSERT(0); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* ITU-T V.110 Section 7.1.3 */ |
| 400 | static void v110_ta_fsm_data_transfer_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 401 | { |
| 402 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 403 | struct v110_ta_state *ts = &ta->state; |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 404 | unsigned int v24_flags = ta->state.v24_flags; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 405 | |
| 406 | /* 7.1.3.1 While in the data transfer state, the following circuit conditions exist: |
| 407 | * a): 105, 107, 108, and 109 are in the ON condition */ |
| 408 | /* XXX: OSMO_ASSERT(V24_FLAGMASK_IS_ON(ts->v24_flags, OSMO_V110_TA_C_105)); */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 409 | V24_FLAGMASK_SET_ON(v24_flags, OSMO_V110_TA_C_107); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 410 | /* XXX: OSMO_ASSERT(V24_FLAGMASK_IS_ON(ts->v24_flags, OSMO_V110_TA_C_108)); */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 411 | V24_FLAGMASK_SET_ON(v24_flags, OSMO_V110_TA_C_109); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 412 | /* b) data is being transmitted on circuit 103 and received on circuit 104 */ |
| 413 | ts->rx.d_bit_mode = V110_TA_DBIT_M_FORWARD; |
| 414 | ts->tx.d_bit_mode = V110_TA_DBIT_M_FORWARD; |
| 415 | /* c) circuits 133 (when implemented) and 106 are in the ON condition unless local out-of-band |
| 416 | * flow control is being used, either or both circuits may be in the ON or the OFF condition. */ |
| 417 | if (!ta->cfg->flow_ctrl.end_to_end) { |
| 418 | /* XXX: OSMO_ASSERT(V24_FLAGMASK_IS_ON(ts->v24_flags, OSMO_V110_TA_C_133)); */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 419 | V24_FLAGMASK_SET_ON(v24_flags, OSMO_V110_TA_C_106); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 420 | } |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 421 | v110_ta_flags_update(ta, v24_flags); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 422 | |
| 423 | /* 7.1.3.2 While in the data transfer state, the following status bit conditions exist: */ |
| 424 | /* a) status bits S in both directions are in the ON condition; */ |
| 425 | ts->tx.s_bits = V110_SX_BIT_ON; |
| 426 | /* b) status bits X in both directions are in the ON condition unless end-to-end flow control |
| 427 | * is being used, in which case status bit X in either or both directions may be in the |
| 428 | * ON or the OFF condition. */ |
| 429 | if (!ta->cfg->flow_ctrl.end_to_end) |
| 430 | ts->tx.x_bits = V110_SX_BIT_ON; |
| 431 | } |
| 432 | |
| 433 | /* ITU-T V.110 Section 7.1.3 */ |
| 434 | static void v110_ta_fsm_data_transfer(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 435 | { |
| 436 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 437 | struct v110_ta_state *ts = &ta->state; |
| 438 | |
| 439 | /* 7.1.3.3 While in the data transfer state: */ |
| 440 | /* a) the S status bits shall *not* be mapped to/from the interchange circuits */ |
| 441 | /* b) the X status bits shall *not* be mapped according to Table 3, |
| 442 | * unless end-to-end flow control is implemented */ |
| 443 | /* TODO: if (ta->cfg->flow_ctrl.end_to_end) { ... } */ |
| 444 | |
| 445 | switch (event) { |
| 446 | case V110_TA_EV_V24_STATUS_CHG: |
| 447 | /* 7.1.4.1 At the completion of the data transfer phase, the local DTE will indicate a |
| 448 | * disconnect request by turning OFF circuit 108 */ |
| 449 | if (V24_FLAGMASK_IS_ON(ts->v24_flags, OSMO_V110_TA_C_108)) |
| 450 | break; |
| 451 | v110_ta_fsm_state_chg(V110_TA_ST_DISCONNECTING); |
| 452 | break; |
| 453 | case V110_TA_EV_DESYNC_IND: |
| 454 | v110_ta_fsm_state_chg(V110_TA_ST_RESYNCING); |
| 455 | break; |
| 456 | case V110_TA_EV_TX_FRAME_RTS: |
| 457 | v110_ta_build_frame(ta, (struct osmo_v110_decoded_frame *)data); |
| 458 | break; |
| 459 | case V110_TA_EV_RX_FRAME_IND: |
| 460 | { |
| 461 | const struct osmo_v110_decoded_frame *df = data; |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 462 | unsigned int v24_flags = ta->state.v24_flags; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 463 | |
| 464 | /* 7.1.4.2 ... this TA will recognize the transition of the status bits S from |
| 465 | * ON to OFF and the data bits from data to binary 0 as a disconnect request */ |
| 466 | if (v110_df_s_bits_are(df, V110_SX_BIT_OFF) && v110_df_d_bits_are(df, 0)) { |
| 467 | /* ... and it will turn OFF circuits 107 and 109. */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 468 | V24_FLAGMASK_SET_OFF(v24_flags, OSMO_V110_TA_C_107); |
| 469 | V24_FLAGMASK_SET_OFF(v24_flags, OSMO_V110_TA_C_109); |
| 470 | v110_ta_flags_update(ta, v24_flags); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 471 | /* DTE should respond by turning OFF circuit 108 */ |
| 472 | break; /* XXX: shall we forward D-bits to DTE anyway? */ |
| 473 | } |
| 474 | |
| 475 | v110_ta_handle_frame(ta, df); |
| 476 | break; |
| 477 | } |
| 478 | default: |
| 479 | OSMO_ASSERT(0); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* ITU-T V.110 Section 7.1.4 */ |
| 484 | static void v110_ta_fsm_disconnect_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 485 | { |
| 486 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 487 | struct v110_ta_state *ts = &ta->state; |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 488 | unsigned int v24_flags = ta->state.v24_flags; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 489 | |
| 490 | /* 7.1.4.1 At the completion of the data transfer phase, the local DTE will indicate a |
| 491 | * disconnect request by turning OFF circuit 108. This will cause the following to occur: */ |
| 492 | /* a) the status bits S in the frame toward ISDN will turn OFF, status bits X are kept ON */ |
| 493 | ts->tx.s_bits = V110_SX_BIT_OFF; |
| 494 | /* b) circuit 106 will be turned OFF */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 495 | V24_FLAGMASK_SET_OFF(v24_flags, OSMO_V110_TA_C_106); |
| 496 | v110_ta_flags_update(ta, v24_flags); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 497 | /* c) the data bits in the frame will be set to binary 0. */ |
| 498 | ts->tx.d_bit_mode = V110_TA_DBIT_M_ALL_ZERO; |
| 499 | |
| 500 | /* To guard against the failure of the remote TA to respond to the disconnect request, |
| 501 | * the local TA may start a timer T2 (suggested value 5 s) which is stopped by the |
| 502 | * reception or transmission of any D-channel clearing message (DISCONNECT, RELEASE, |
| 503 | * RELEASE COMPLETE). */ |
| 504 | OSMO_ASSERT(fi->T == OSMO_V110_TA_TIMER_T2); |
| 505 | } |
| 506 | |
| 507 | /* ITU-T V.110 Section 7.1.4 */ |
| 508 | static void v110_ta_fsm_disconnect(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 509 | { |
| 510 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 511 | |
| 512 | switch (event) { |
| 513 | case V110_TA_EV_V24_STATUS_CHG: |
| 514 | break; /* nothing to do */ |
| 515 | case V110_TA_EV_TX_FRAME_RTS: |
| 516 | v110_ta_build_frame(ta, (struct osmo_v110_decoded_frame *)data); |
| 517 | break; |
| 518 | case V110_TA_EV_RX_FRAME_IND: |
| 519 | { |
| 520 | const struct osmo_v110_decoded_frame *df = data; |
| 521 | |
| 522 | /* 7.1.4.3 The TA at the station that originated the disconnect request will |
| 523 | * recognize reception of S = OFF or the loss of framing signals as a disconnect |
| 524 | * acknowledgement and turn OFF circuits 107 and 109. */ |
| 525 | if (v110_df_s_bits_are(df, V110_SX_BIT_OFF)) { |
| 526 | /* circuits 107 and 109 set to off in .onenter() */ |
| 527 | v110_ta_fsm_state_chg(V110_TA_ST_IDLE_READY); |
| 528 | } |
| 529 | |
| 530 | v110_ta_handle_frame(ta, df); |
| 531 | break; |
| 532 | } |
| 533 | case V110_TA_EV_DESYNC_IND: |
| 534 | case V110_TA_EV_TIMEOUT: |
| 535 | /* circuits 107 and 109 set to off in .onenter() */ |
| 536 | v110_ta_fsm_state_chg(V110_TA_ST_IDLE_READY); |
| 537 | break; |
| 538 | default: |
| 539 | OSMO_ASSERT(0); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* ITU-T V.110 Section 7.1.5 */ |
| 544 | static void v110_ta_fsm_resyncing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 545 | { |
| 546 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 547 | struct v110_ta_state *ts = &ta->state; |
| 548 | |
| 549 | /* 7.1.5 In the event of loss of frame synchronization, the (local) TA should |
| 550 | * attempt to resynchronize as follows: */ |
| 551 | /* a) Place circuit 104 in binary 1 condition (passes from the data mode) */ |
| 552 | ts->rx.d_bit_mode = V110_TA_DBIT_M_ALL_ONE; |
| 553 | /* b) Turn OFF status bit X in the transmitted frame */ |
| 554 | ts->tx.x_bits = V110_SX_BIT_OFF; |
| 555 | |
| 556 | /* guard timeout, see 7.1.5 e) */ |
| 557 | OSMO_ASSERT(fi->T == OSMO_V110_TA_TIMER_X1); |
| 558 | } |
| 559 | |
| 560 | /* ITU-T V.110 Section 7.1.5 */ |
| 561 | static void v110_ta_fsm_resyncing(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 562 | { |
| 563 | struct osmo_v110_ta *ta = (struct osmo_v110_ta *)fi->priv; |
| 564 | struct v110_ta_state *ts = &ta->state; |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 565 | unsigned int v24_flags = ta->state.v24_flags; |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 566 | |
| 567 | switch (event) { |
| 568 | case V110_TA_EV_V24_STATUS_CHG: |
| 569 | break; /* TODO: handle circuit 108 being set to OFF? */ |
| 570 | case V110_TA_EV_TX_FRAME_RTS: |
| 571 | v110_ta_build_frame(ta, (struct osmo_v110_decoded_frame *)data); |
| 572 | break; |
| 573 | case V110_TA_EV_SYNC_IND: |
| 574 | /* f) If resynchronization is achieved, the local TA should turn ON status bit X */ |
| 575 | ts->tx.x_bits = V110_SX_BIT_ON; |
| 576 | v110_ta_fsm_state_chg(V110_TA_ST_DATA_TRANSFER); |
| 577 | break; |
| 578 | case V110_TA_EV_TIMEOUT: |
| 579 | /* e) If after an interval of X1 seconds the local TA cannot attain synchronization, |
| 580 | * it should send a disconnect request by turning OFF all of the status bits for several |
| 581 | * (at least three) frames with data bits set to binary 0 and then disconnect by turning |
| 582 | * OFF circuit 107 and transferring to the disconnected mode as discussed in 7.1.4.2. */ |
| 583 | ts->tx.s_bits = V110_SX_BIT_OFF; |
| 584 | ts->tx.x_bits = V110_SX_BIT_OFF; |
| 585 | ts->tx.d_bit_mode = V110_TA_DBIT_M_ALL_ZERO; |
| 586 | /* TODO: actually Tx those frames (delay state transition) */ |
Vadim Yanitskiy | 0042b25 | 2024-01-20 08:40:05 +0700 | [diff] [blame] | 587 | V24_FLAGMASK_SET_OFF(v24_flags, OSMO_V110_TA_C_107); |
| 588 | v110_ta_flags_update(ta, v24_flags); |
Vadim Yanitskiy | 85554db | 2023-03-14 20:33:51 +0100 | [diff] [blame] | 589 | v110_ta_fsm_state_chg(V110_TA_ST_DISCONNECTING); |
| 590 | break; |
| 591 | default: |
| 592 | OSMO_ASSERT(0); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static int v110_ta_timer_cb(struct osmo_fsm_inst *fi) |
| 597 | { |
| 598 | osmo_fsm_inst_dispatch(fi, V110_TA_EV_TIMEOUT, NULL); |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static const struct osmo_fsm_state v110_ta_states[] = { |
| 603 | [V110_TA_ST_IDLE_READY] = { |
| 604 | .name = "IDLE_READY", |
| 605 | .in_event_mask = S(V110_TA_EV_V24_STATUS_CHG) |
| 606 | | S(V110_TA_EV_TX_FRAME_RTS) |
| 607 | | S(V110_TA_EV_RX_FRAME_IND), |
| 608 | .out_state_mask = S(V110_TA_ST_IDLE_READY) |
| 609 | | S(V110_TA_ST_CON_TA_TO_LINE), |
| 610 | .action = &v110_ta_fsm_idle_ready, |
| 611 | .onenter = &v110_ta_fsm_idle_ready_onenter, |
| 612 | }, |
| 613 | [V110_TA_ST_CON_TA_TO_LINE] = { |
| 614 | .name = "CONNECT_TA_TO_LINE", |
| 615 | .in_event_mask = S(V110_TA_EV_V24_STATUS_CHG) |
| 616 | | S(V110_TA_EV_TIMEOUT) |
| 617 | | S(V110_TA_EV_SYNC_IND) |
| 618 | | S(V110_TA_EV_TX_FRAME_RTS) |
| 619 | | S(V110_TA_EV_RX_FRAME_IND), |
| 620 | .out_state_mask = S(V110_TA_ST_DATA_TRANSFER) |
| 621 | | S(V110_TA_ST_IDLE_READY), |
| 622 | .action = &v110_ta_fsm_connect_ta_to_line, |
| 623 | .onenter = &v110_ta_fsm_connect_ta_to_line_onenter, |
| 624 | }, |
| 625 | [V110_TA_ST_DATA_TRANSFER] = { |
| 626 | .name = "DATA_TRANSFER", |
| 627 | .in_event_mask = S(V110_TA_EV_V24_STATUS_CHG) |
| 628 | | S(V110_TA_EV_DESYNC_IND) |
| 629 | | S(V110_TA_EV_TX_FRAME_RTS) |
| 630 | | S(V110_TA_EV_RX_FRAME_IND), |
| 631 | .out_state_mask = S(V110_TA_ST_RESYNCING) |
| 632 | | S(V110_TA_ST_DISCONNECTING), |
| 633 | .action = &v110_ta_fsm_data_transfer, |
| 634 | .onenter = &v110_ta_fsm_data_transfer_onenter, |
| 635 | }, |
| 636 | [V110_TA_ST_DISCONNECTING] = { |
| 637 | .name = "DISCONNECTING", |
| 638 | .in_event_mask = S(V110_TA_EV_V24_STATUS_CHG) |
| 639 | | S(V110_TA_EV_TIMEOUT) |
| 640 | | S(V110_TA_EV_TX_FRAME_RTS) |
| 641 | | S(V110_TA_EV_RX_FRAME_IND) |
| 642 | | S(V110_TA_EV_DESYNC_IND), |
| 643 | .out_state_mask = S(V110_TA_ST_IDLE_READY), |
| 644 | .action = &v110_ta_fsm_disconnect, |
| 645 | .onenter = &v110_ta_fsm_disconnect_onenter, |
| 646 | }, |
| 647 | [V110_TA_ST_RESYNCING] = { |
| 648 | .name = "RESYNCING", |
| 649 | .in_event_mask = S(V110_TA_EV_V24_STATUS_CHG) |
| 650 | | S(V110_TA_EV_TIMEOUT) |
| 651 | | S(V110_TA_EV_TX_FRAME_RTS) |
| 652 | | S(V110_TA_EV_SYNC_IND), |
| 653 | .out_state_mask = S(V110_TA_ST_IDLE_READY) |
| 654 | | S(V110_TA_ST_DATA_TRANSFER), |
| 655 | .action = &v110_ta_fsm_resyncing, |
| 656 | .onenter = &v110_ta_fsm_resyncing_onenter, |
| 657 | }, |
| 658 | }; |
| 659 | |
| 660 | static struct osmo_fsm osmo_v110_ta_fsm = { |
| 661 | .name = "V110-TA", |
| 662 | .states = v110_ta_states, |
| 663 | .num_states = ARRAY_SIZE(v110_ta_states), |
| 664 | .timer_cb = v110_ta_timer_cb, |
| 665 | .log_subsys = DLGLOBAL, |
| 666 | .event_names = v110_ta_fsm_event_names, |
| 667 | }; |
| 668 | |
| 669 | static __attribute__((constructor)) void on_dso_load(void) |
| 670 | { |
| 671 | OSMO_ASSERT(osmo_fsm_register(&osmo_v110_ta_fsm) == 0); |
| 672 | } |
| 673 | |
| 674 | /*! Allocate a V.110 TA (Terminal Adapter) instance. |
| 675 | * \param[in] ctx parent talloc context. |
| 676 | * \param[in] name name of the TA instance. |
| 677 | * \param[in] cfg initial configuration of the TA instance. |
| 678 | * \returns pointer to allocated TA instance; NULL on error. */ |
| 679 | struct osmo_v110_ta *osmo_v110_ta_alloc(void *ctx, const char *name, |
| 680 | const struct osmo_v110_ta_cfg *cfg) |
| 681 | { |
| 682 | struct osmo_v110_ta *ta; |
| 683 | |
| 684 | OSMO_ASSERT(cfg != NULL); |
| 685 | OSMO_ASSERT(cfg->rx_cb != NULL); |
| 686 | OSMO_ASSERT(cfg->tx_cb != NULL); |
| 687 | |
| 688 | /* local (TE-TA) flow control is not implemented */ |
| 689 | if (cfg->flow_ctrl.local != OSMO_V110_LOCAL_FLOW_CTRL_NONE) { |
| 690 | LOGP(DLGLOBAL, LOGL_ERROR, "Local (TE-TA) flow control is not implemented\n"); |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | ta = talloc_zero(ctx, struct osmo_v110_ta); |
| 695 | if (ta == NULL) |
| 696 | return NULL; |
| 697 | |
| 698 | ta->name = talloc_strdup(ta, name); |
| 699 | ta->cfg = talloc_memdup(ta, cfg, sizeof(*cfg)); |
| 700 | if (ta->name == NULL || ta->cfg == NULL) |
| 701 | goto exit_free; |
| 702 | |
| 703 | ta->Tdefs = talloc_memdup(ta, v110_ta_tdef, sizeof(v110_ta_tdef)); |
| 704 | if (ta->Tdefs == NULL) |
| 705 | goto exit_free; |
| 706 | osmo_tdefs_reset(ta->Tdefs); /* apply default values */ |
| 707 | |
| 708 | ta->fi = osmo_fsm_inst_alloc(&osmo_v110_ta_fsm, ta, ta, LOGL_DEBUG, name); |
| 709 | if (ta->fi == NULL) |
| 710 | goto exit_free; |
| 711 | |
| 712 | /* perform a loop transition to init the internal state */ |
| 713 | osmo_fsm_inst_state_chg(ta->fi, V110_TA_ST_IDLE_READY, 0, 0); |
| 714 | |
| 715 | return ta; |
| 716 | |
| 717 | exit_free: |
| 718 | if (ta->fi != NULL) |
| 719 | osmo_fsm_inst_free(ta->fi); |
| 720 | talloc_free(ta); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
| 724 | /*! Release memory taken by the given V.110 TA instance. |
| 725 | * \param[in] ta TA instance to be free()d. */ |
| 726 | void osmo_v110_ta_free(struct osmo_v110_ta *ta) |
| 727 | { |
| 728 | if (ta == NULL) |
| 729 | return; |
| 730 | if (ta->fi != NULL) |
| 731 | osmo_fsm_inst_free(ta->fi); |
| 732 | talloc_free(ta); /* also free()s name and cfg */ |
| 733 | } |
| 734 | |
| 735 | /*! Configure a timer of the given V.110 TA instance. |
| 736 | * \param[in] ta TA instance to be configured. |
| 737 | * \param[in] timer a timer to be configured. |
| 738 | * \param[in] val_ms the new timeout value to set (in milliseconds). |
| 739 | * \returns 0 in case of success; negative on error. */ |
| 740 | int osmo_v110_ta_set_timer_val_ms(struct osmo_v110_ta *ta, |
| 741 | enum osmo_v110_ta_timer timer, |
| 742 | unsigned long val_ms) |
| 743 | { |
| 744 | return osmo_tdef_set(ta->Tdefs, (int)timer, val_ms, OSMO_TDEF_MS); |
| 745 | } |
| 746 | |
| 747 | /*! Feed a [decoded] V.110 frame into the given TA instance. |
| 748 | * |
| 749 | * This function, like its _out counterpart, is intended to be used by the lower layers |
| 750 | * receiving V.110 frames over some medium. The caller of this function is responsible |
| 751 | * for finding the synchronization pattern (if needed), aligning to the frame boundaries, |
| 752 | * and decoding frames using osmo_v110_decode_frame() or osmo_csd_*_decode_frame(). |
| 753 | * |
| 754 | * Bits E1/E2/E3 are expected to be set by the caller (if not being transmitted |
| 755 | * over the medium) in accordance with the configured synchronous user rate. |
| 756 | * |
| 757 | * Bits D1..D48 are passed to the bit rate adaption function RA1. The resulting output |
| 758 | * is then passed to the upper layer (application) via the configured .rx_cb(). Though, |
| 759 | * in certain states of the TA's FSM, bits D1..D48 are ignored and the upper layer |
| 760 | * gets a sequence of binary '0' or '1'. |
| 761 | * |
| 762 | * \param[in] ta TA instance to feed the given frame into. |
| 763 | * \param[in] in pointer to a [decoded] V.110 frame. |
| 764 | * \returns 0 in case of success; negative on error. */ |
| 765 | int osmo_v110_ta_frame_in(struct osmo_v110_ta *ta, const struct osmo_v110_decoded_frame *in) |
| 766 | { |
| 767 | return osmo_fsm_inst_dispatch(ta->fi, V110_TA_EV_RX_FRAME_IND, (void *)in); |
| 768 | } |
| 769 | |
| 770 | /*! Pull a [decoded] V.110 frame out of the given TA instance. |
| 771 | * |
| 772 | * This function, like its _in counterpart, is intended to be used by the lower layers |
| 773 | * transmitting V.110 frames over some medium. The caller of this function is responsible |
| 774 | * for encoding the output frame using osmo_v110_encode_frame() or osmo_csd_*_encode_frame(). |
| 775 | * |
| 776 | * Bits E1/E2/E3 are set in accordance with the configured synchronous user rate. |
| 777 | * Bits E4/E5/E6/E7 are unconditionally set to binary '1'. |
| 778 | * |
| 779 | * Bits D1..D48 are set depending on the state of TA's FSM: |
| 780 | * |
| 781 | * - In data transfer mode, the user bits are obtained from the upper layer (application) |
| 782 | * via the configured .tx_cb(), and then passed to the bit rate adaption function RA1, |
| 783 | * which generates bits D1..D48. |
| 784 | * - In other modes, bits D1..D48 are all set to binary '0' or '1'. |
| 785 | * |
| 786 | * \param[in] ta TA instance to pull a frame from. |
| 787 | * \param[out] out where to store a [decoded] V.110 frame. |
| 788 | * \returns 0 in case of success; negative on error. */ |
| 789 | int osmo_v110_ta_frame_out(struct osmo_v110_ta *ta, struct osmo_v110_decoded_frame *out) |
| 790 | { |
| 791 | return osmo_fsm_inst_dispatch(ta->fi, V110_TA_EV_TX_FRAME_RTS, (void *)out); |
| 792 | } |
| 793 | |
| 794 | /*! Indicate a synchronization establishment event. |
| 795 | * |
| 796 | * This function is intended to be called when the lower layer |
| 797 | * achieves synchronization to the frame clock. |
| 798 | * |
| 799 | * \param[in] ta TA instance to indicate the event to. |
| 800 | * \returns 0 in case of success; negative on error. */ |
| 801 | int osmo_v110_ta_sync_ind(struct osmo_v110_ta *ta) |
| 802 | { |
| 803 | return osmo_fsm_inst_dispatch(ta->fi, V110_TA_EV_SYNC_IND, NULL); |
| 804 | } |
| 805 | |
| 806 | /*! Indicate a synchronization loss event. |
| 807 | * |
| 808 | * This function is intended to be called when the lower layer |
| 809 | * experiences a loss of synchronization with the frame clock. |
| 810 | * |
| 811 | * \param[in] ta TA instance to indicate the event to. |
| 812 | * \returns 0 in case of success; negative on error. */ |
| 813 | int osmo_v110_ta_desync_ind(struct osmo_v110_ta *ta) |
| 814 | { |
| 815 | return osmo_fsm_inst_dispatch(ta->fi, V110_TA_EV_DESYNC_IND, NULL); |
| 816 | } |
| 817 | |
| 818 | /*! Get the V.24 status bit-mask of the given TA instance. |
| 819 | * \param[in] ta TA instance to get the circuit bit-mask. |
| 820 | * \returns bitmask of OSMO_V110_TA_C_*. */ |
| 821 | unsigned int osmo_v110_ta_get_status(const struct osmo_v110_ta *ta) |
| 822 | { |
| 823 | return ta->state.v24_flags; |
| 824 | } |
| 825 | |
| 826 | /*! Set the V.24 status bit-mask of the given TA instance. |
| 827 | * \param[in] ta TA instance to update the circuit state. |
| 828 | * \param[in] status bit-mask of OSMO_V110_TA_C_*. |
| 829 | * \returns 0 on success; negative on error. */ |
| 830 | static int v110_ta_set_status(struct osmo_v110_ta *ta, unsigned int status) |
| 831 | { |
| 832 | const unsigned int old_status = ta->state.v24_flags; |
| 833 | int rc = 0; |
| 834 | |
| 835 | ta->state.v24_flags = status; |
| 836 | if (status != old_status) |
| 837 | rc = osmo_fsm_inst_dispatch(ta->fi, V110_TA_EV_V24_STATUS_CHG, NULL); |
| 838 | |
| 839 | return rc; |
| 840 | } |
| 841 | |
| 842 | /*! Get state of a V.24 circuit of the given TA instance. |
| 843 | * \param[in] ta TA instance to get the circuit state. |
| 844 | * \param[in] circuit a V.24 circuit, one of OSMO_V110_TA_C_*. |
| 845 | * \returns circuit state: active (true) or inactive (false). */ |
| 846 | bool osmo_v110_ta_get_circuit(const struct osmo_v110_ta *ta, |
| 847 | enum osmo_v110_ta_circuit circuit) |
| 848 | { |
| 849 | return V24_FLAGMASK_IS_ON(ta->state.v24_flags, circuit); |
| 850 | } |
| 851 | |
| 852 | /*! Activate/deactivate a V.24 circuit of the given TA instance. |
| 853 | * \param[in] ta TA instance to update the circuit state. |
| 854 | * \param[in] circuit a V.24 circuit, one of OSMO_V110_TA_C_* (DTE->DCE). |
| 855 | * \param[in] active activate (true) or deactivate (false) the circuit. |
| 856 | * \returns 0 on success; negative on error. */ |
| 857 | int osmo_v110_ta_set_circuit(struct osmo_v110_ta *ta, |
| 858 | enum osmo_v110_ta_circuit circuit, bool active) |
| 859 | { |
| 860 | unsigned int status = ta->state.v24_flags; |
| 861 | |
| 862 | /* permit setting only DTE->DCE circuits */ |
| 863 | switch (circuit) { |
| 864 | case OSMO_V110_TA_C_105: |
| 865 | case OSMO_V110_TA_C_108: |
| 866 | case OSMO_V110_TA_C_133: |
| 867 | break; |
| 868 | default: |
| 869 | LOGPFSML(ta->fi, LOGL_ERROR, |
| 870 | "Setting circuit %s is not permitted (wrong direction?)\n", |
| 871 | osmo_v110_ta_circuit_name(circuit)); |
| 872 | return -EACCES; |
| 873 | } |
| 874 | |
| 875 | if (active) |
| 876 | V24_FLAGMASK_SET_ON(status, circuit); |
| 877 | else |
| 878 | V24_FLAGMASK_SET_OFF(status, circuit); |
| 879 | |
| 880 | return v110_ta_set_status(ta, status); |
| 881 | } |