Harald Welte | 78861c0 | 2020-05-14 13:28:07 +0200 | [diff] [blame] | 1 | /* GSM A-bis TRAU frame synchronization as per TS 48.060 / 48.061 */ |
| 2 | |
| 3 | /* (C) 2020 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/bits.h> |
| 27 | #include <osmocom/core/fsm.h> |
| 28 | |
| 29 | #include "ubit_buf.h" |
| 30 | #include <osmocom/trau/trau_sync.h> |
| 31 | |
| 32 | #define S(x) (1 << (x)) |
| 33 | |
| 34 | #define MAX_TRAU_BYTES 40 |
| 35 | |
| 36 | #define T_SYNC 1 |
| 37 | |
| 38 | struct sync_pattern { |
| 39 | /* provided by user */ |
| 40 | const char *name; /*!< human-readable name */ |
| 41 | const uint8_t byte_pattern[MAX_TRAU_BYTES]; /*!< bytes to match against */ |
| 42 | const uint8_t byte_mask[MAX_TRAU_BYTES]; /*!< mask applied before matching */ |
| 43 | uint8_t byte_len; /*!< length of mask in bytes */ |
| 44 | |
| 45 | /* generated by code */ |
| 46 | ubit_t ubit_pattern[MAX_TRAU_BYTES*8]; /*!< bits to match against */ |
| 47 | ubit_t ubit_mask[MAX_TRAU_BYTES*8]; /*!< mask applied before matching */ |
| 48 | uint8_t bitcount; /*!< number of high bits in mask */ |
| 49 | }; |
| 50 | |
| 51 | static struct sync_pattern sync_patterns[] = { |
| 52 | [OSMO_TRAU_SYNCP_16_FR_EFR] = { |
| 53 | /* TS 08.60 Section 4.8.1 */ |
| 54 | .name = "FR/EFR", |
| 55 | .byte_pattern = { |
| 56 | 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 57 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 58 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 59 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 60 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 61 | }, |
| 62 | .byte_mask = { |
| 63 | 0xff, 0xff, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 64 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 65 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 66 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 67 | 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, |
| 68 | }, |
| 69 | .byte_len = 40, |
| 70 | }, |
| 71 | [OSMO_TRAU_SYNCP_8_HR] = { |
| 72 | /* TS 08.61 Section 6.8.2.1.1 */ |
| 73 | .name = "HR8", |
| 74 | .byte_pattern = { |
| 75 | 0x00, 0x80, 0x40, 0x80, |
| 76 | 0x80, 0x80, 0x80, 0x80, |
| 77 | 0x80, 0x80, 0x80, 0x80, |
| 78 | 0x80, 0x80, 0x80, 0x80, |
| 79 | 0x80, 0x80, 0x80, 0x80, |
| 80 | }, |
| 81 | .byte_mask = { |
| 82 | 0xff, 0x80, 0xC0, 0x80, |
| 83 | 0x80, 0x80, 0x80, 0x80, |
| 84 | 0x80, 0x80, 0x80, 0x80, |
| 85 | 0x80, 0x80, 0x80, 0x80, |
| 86 | 0x80, 0x80, 0x80, 0x80, |
| 87 | }, |
| 88 | .byte_len = 20, |
| 89 | }, |
| 90 | [OSMO_TRAU_SYNCP_8_AMR_LOW] = { |
| 91 | /* TS 08.61 Section 6.8.2.1.2 */ |
| 92 | /* The frame synchronisation for No_Speech frames and the speech frames of the three lower codec modes */ |
| 93 | .name = "AMR8_LOW", |
| 94 | .byte_pattern = { |
| 95 | 0x00, 0x80, 0x80, 0x40, |
| 96 | 0x80, 0x80, 0x80, 0x80, |
| 97 | 0x80, 0x80, 0x80, 0x80, |
| 98 | 0x80, 0x80, 0x80, 0x80, |
| 99 | 0x80, 0x80, 0x80, 0x80, |
| 100 | }, |
| 101 | .byte_mask = { |
| 102 | 0xff, 0x80, 0x80, 0xC0, |
| 103 | 0x80, 0x80, 0x80, 0x80, |
| 104 | 0x80, 0x80, 0x80, 0x80, |
| 105 | 0x80, 0x80, 0x80, 0x80, |
| 106 | 0x80, 0x80, 0x80, 0x80, |
| 107 | }, |
| 108 | .byte_len = 20, |
| 109 | }, |
| 110 | [OSMO_TRAU_SYNCP_8_AMR_6K7] = { |
| 111 | /* The frame synchronisation for the speech frames for codec mode 6,70 kBit/s */ |
| 112 | .name = "AMR8_67", |
| 113 | .byte_pattern = { |
| 114 | 0x00, 0x80, 0x80, 0x80, |
| 115 | 0x80, 0x00, 0x80, 0x00, |
| 116 | 0x80, 0x00, 0x80, 0x00, |
| 117 | 0x80, 0x00, 0x80, 0x00, |
| 118 | 0x80, 0x00, 0x80, 0x00, |
| 119 | }, |
| 120 | .byte_mask = { |
| 121 | 0xff, 0x80, 0x80, 0x80, |
| 122 | 0x80, 0x80, 0x80, 0x00, |
| 123 | 0x80, 0x00, 0x80, 0x00, |
| 124 | 0x80, 0x00, 0x80, 0x00, |
| 125 | 0x80, 0x00, 0x80, 0x00, |
| 126 | }, |
| 127 | .byte_len = 20 |
| 128 | }, |
| 129 | [OSMO_TRAU_SYNCP_8_AMR_7K4] = { |
| 130 | /* The frame synchronisation for the speech frames for codec mode 7,40 kBit/s */ |
| 131 | .name = "AMR8_74", |
| 132 | .byte_pattern = { |
| 133 | 0x20, 0x00, 0x80, 0x00, |
| 134 | 0x00, 0x00, 0x00, 0x00, |
| 135 | 0x00, 0x00, 0x00, 0x00, |
| 136 | 0x00, 0x00, 0x00, 0x00, |
| 137 | 0x00, 0x00, 0x00, 0x00, |
| 138 | }, |
| 139 | .byte_mask = { |
| 140 | 0xe0, 0x80, 0x80, 0x80, |
| 141 | 0x00, 0x00, 0x00, 0x00, |
| 142 | 0x00, 0x00, 0x00, 0x00, |
| 143 | 0x00, 0x00, 0x00, 0x00, |
| 144 | 0x00, 0x00, 0x00, 0x00, |
| 145 | }, |
| 146 | .byte_len = 20, |
| 147 | }, |
| 148 | }; |
| 149 | |
| 150 | #if 0 |
| 151 | static struct sync_pattern rbs_ccu_sync_ind_16_pattern = { |
| 152 | .name ="RBS_CCU_SYNC_IND_16", |
| 153 | .byte_pattern = { |
| 154 | 0x00, 0x00, 0x01, 0x00, |
| 155 | 0x00, 0x00, 0x01, 0x00, |
| 156 | 0x01, 0x00, 0x01, 0x00, |
| 157 | }, |
| 158 | .byte_mask = { |
| 159 | 0xff, 0xff, 0x01, 0x00, |
| 160 | 0x01, 0x00, 0x01, 0x00, |
| 161 | 0x01, 0x00, 0x01, 0x00, |
| 162 | }, |
| 163 | }; |
| 164 | |
| 165 | static struct sync_pattern rbs_ccu_data_ind_16_pattern = { |
| 166 | .name ="RBS_CCU_DATA_IND_16", |
| 167 | .byte_pattern = { |
| 168 | 0x00, 0x00, 0x01, 0x00, |
| 169 | }, |
| 170 | .byte_mask = { |
| 171 | 0xff, 0xff, 0x01, 0x00, |
| 172 | }, |
| 173 | }; |
| 174 | #endif |
| 175 | |
| 176 | |
| 177 | static void expand_sync_pattern(struct sync_pattern *pat) |
| 178 | { |
| 179 | osmo_pbit2ubit(pat->ubit_pattern, pat->byte_pattern, pat->byte_len*8); |
| 180 | osmo_pbit2ubit(pat->ubit_mask, pat->byte_mask, pat->byte_len*8); |
| 181 | } |
| 182 | |
| 183 | static unsigned int count_one_bits(const ubit_t *in, unsigned int in_bits) |
| 184 | { |
| 185 | unsigned int i, count = 0; |
| 186 | |
| 187 | for (i = 0; i < in_bits; i++) { |
| 188 | if (in[i]) |
| 189 | count++; |
| 190 | } |
| 191 | return count; |
| 192 | } |
| 193 | |
| 194 | static void sync_pattern_register(struct sync_pattern *p) |
| 195 | { |
| 196 | expand_sync_pattern(p); |
| 197 | p->bitcount = count_one_bits(p->ubit_mask, p->byte_len*8); |
| 198 | } |
| 199 | |
| 200 | #if 0 |
| 201 | /*! correlate pattern with unpacked bits from buffer. |
| 202 | * \param[in] pattern sync_pattern against which we shall compare |
| 203 | * \param[in] bits unpacked bits to compare against pattern |
| 204 | * \param[in] num_bits number of unpacked bits |
| 205 | * \returns number of bits not matching pattern; -1 if insufficient bits available. */ |
| 206 | static int correlate_pattern_ubits(const struct sync_pattern *pattern, |
| 207 | const ubit_t *bits, size_t num_bits) |
| 208 | { |
| 209 | int i, num_wrong = 0; |
| 210 | |
| 211 | if (num_bits < pattern->byte_len*8) |
| 212 | return -1; /* insufficient data */ |
| 213 | |
| 214 | for (i = 0; i < pattern->byte_len *8; i++) { |
| 215 | /* if mask doesn't contain '1', we can skip this octet */ |
| 216 | if (!pattern->ubit_mask) |
| 217 | continue; |
| 218 | if (bits[i] != pattern->ubit_pattern[i]) |
| 219 | num_wrong++; |
| 220 | } |
| 221 | |
| 222 | return num_wrong; |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | struct trau_rx_sync_state { |
| 227 | /*! call-back to be called for every TRAU frame (called with |
| 228 | * bits=NULL in case of frame sync loss */ |
| 229 | frame_out_cb_t out_cb; |
| 230 | /*! opaque user data; passed to out_cb */ |
| 231 | void *user_data; |
| 232 | |
| 233 | /*! history of received bits */ |
| 234 | ubit_t history[MAX_TRAU_BYTES*8+1]; /* +1 not required, but helps to expose bugs */ |
| 235 | /*! index of next-to-be-written ubit in history */ |
| 236 | unsigned int history_idx; |
| 237 | /*! the pattern we are trying to sync to */ |
| 238 | const struct sync_pattern *pattern; |
| 239 | /*! number of consecutive frames without sync */ |
| 240 | unsigned int num_consecutive_errors; |
| 241 | }; |
| 242 | |
| 243 | /* correlate the history (up to the last received bit) against the pattern */ |
| 244 | static int correlate_history_against_pattern(struct trau_rx_sync_state *tss) |
| 245 | { |
| 246 | const struct sync_pattern *pattern = tss->pattern; |
| 247 | int i, start, num_wrong = 0; |
| 248 | |
| 249 | /* compute index of first bit in history array */ |
| 250 | start = (ARRAY_SIZE(tss->history) + tss->history_idx - pattern->byte_len*8) |
| 251 | % ARRAY_SIZE(tss->history); |
| 252 | |
| 253 | OSMO_ASSERT(ARRAY_SIZE(tss->history) >= pattern->byte_len*8); |
| 254 | |
| 255 | for (i = 0; i < pattern->byte_len*8; i++) { |
| 256 | unsigned int pos = (start + i) % ARRAY_SIZE(tss->history); |
| 257 | |
| 258 | /* if mask doesn't contain '1', we can skip this octet */ |
| 259 | if (!pattern->ubit_mask[i]) |
| 260 | continue; |
| 261 | if (tss->history[pos] != pattern->ubit_pattern[i]) |
| 262 | num_wrong++; |
| 263 | } |
| 264 | |
| 265 | return num_wrong; |
| 266 | } |
| 267 | |
| 268 | /* add (append) one ubit to the history; wrap as needed */ |
| 269 | static void rx_history_add_bit(struct trau_rx_sync_state *tss, ubit_t bit) |
| 270 | { |
| 271 | tss->history[tss->history_idx] = bit; |
| 272 | /* simply wrap around at the end */ |
| 273 | tss->history_idx = (tss->history_idx + 1) % ARRAY_SIZE(tss->history); |
| 274 | } |
| 275 | |
| 276 | /* append bits to history. We assume that this does NOT wrap */ |
| 277 | static void rx_history_add_bits(struct trau_rx_sync_state *tss, const ubit_t *bits, size_t n_bits) |
| 278 | { |
| 279 | unsigned int frame_bits_remaining = tss->pattern->byte_len*8 - tss->history_idx; |
| 280 | OSMO_ASSERT(frame_bits_remaining >= n_bits); |
| 281 | memcpy(&tss->history[tss->history_idx], bits, n_bits); |
| 282 | tss->history_idx = tss->history_idx + n_bits; |
| 283 | } |
| 284 | |
| 285 | /* align the history, i.e. next received bit is start of frame */ |
| 286 | static void rx_history_align(struct trau_rx_sync_state *tss) |
| 287 | { |
| 288 | ubit_t tmp[sizeof(tss->history)]; |
| 289 | size_t history_size = sizeof(tss->history); |
| 290 | size_t pattern_bits = tss->pattern->byte_len*8; |
| 291 | size_t first_bit = (history_size + tss->history_idx - pattern_bits) % history_size; |
| 292 | int i; |
| 293 | |
| 294 | /* we need to shift the last received frame to the start of the history buffer; |
| 295 | * do this in two steps: First copy to a local buffer on the stack, using modulo-arithmetic |
| 296 | * as index into the history. Second, copy it back to history */ |
| 297 | |
| 298 | for (i = 0; i < pattern_bits; i++) |
| 299 | tmp[i] = tss->history[(first_bit + i) % history_size]; |
| 300 | |
| 301 | memcpy(tss->history, tmp, history_size); |
| 302 | tss->history_idx = 0; |
| 303 | } |
| 304 | |
| 305 | enum trau_sync_state { |
| 306 | WAIT_FRAME_ALIGN, |
| 307 | FRAME_ALIGNED, |
| 308 | /* if at least 3 consecutive frames with each at least one framing error have been received */ |
| 309 | FRAME_ALIGNMENT_LOST, |
| 310 | }; |
| 311 | |
| 312 | enum trau_sync_event { |
| 313 | TRAUSYNC_E_RESET, |
| 314 | /*! a buffer of bits was received (msgb with ubits) */ |
| 315 | TRAUSYNC_E_RX_BITS, |
| 316 | }; |
| 317 | |
| 318 | static const struct value_string trau_sync_event_names[] = { |
| 319 | { TRAUSYNC_E_RESET, "RESET" }, |
| 320 | { TRAUSYNC_E_RX_BITS, "RX_BITS" }, |
| 321 | { 0, NULL } |
| 322 | }; |
| 323 | |
| 324 | |
| 325 | static void trau_sync_wait_align(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 326 | { |
| 327 | struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv; |
| 328 | struct ubit_buf *ubb; |
| 329 | |
| 330 | switch (event) { |
| 331 | case TRAUSYNC_E_RX_BITS: |
| 332 | ubb = data; |
| 333 | /* append every bit individually + check if we have sync */ |
| 334 | while (ubb_length(ubb) > 0) { |
| 335 | ubit_t bit = ubb_pull_ubit(ubb); |
| 336 | int rc; |
| 337 | |
| 338 | rx_history_add_bit(tss, bit); |
| 339 | rc = correlate_history_against_pattern(tss); |
| 340 | if (!rc) { |
| 341 | osmo_fsm_inst_state_chg(fi, FRAME_ALIGNED, 0, 0); |
| 342 | /* treat remainder of input bits in correct state */ |
| 343 | osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, ubb); |
| 344 | return; |
| 345 | } |
| 346 | } |
| 347 | break; |
| 348 | default: |
| 349 | OSMO_ASSERT(0); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static void trau_sync_aligned_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 354 | { |
| 355 | struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv; |
| 356 | /* dispatch aligned frame to user */ |
| 357 | rx_history_align(tss); |
| 358 | tss->out_cb(tss->user_data, tss->history, tss->pattern->byte_len*8); |
| 359 | } |
| 360 | |
| 361 | static void trau_sync_aligned(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 362 | { |
| 363 | struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv; |
| 364 | struct ubit_buf *ubb; |
| 365 | int rc; |
| 366 | |
| 367 | switch (event) { |
| 368 | case TRAUSYNC_E_RX_BITS: |
| 369 | ubb = data; |
| 370 | while (ubb_length(ubb)) { |
| 371 | unsigned int frame_bits_remaining = tss->pattern->byte_len*8 - tss->history_idx; |
| 372 | if (ubb_length(ubb) < frame_bits_remaining) { |
| 373 | /* frame not filled by this message; just add data */ |
| 374 | rx_history_add_bits(tss, ubb_data(ubb), ubb_length(ubb)); |
| 375 | ubb_pull(ubb, ubb_length(ubb)); |
| 376 | } else { |
| 377 | /* append as many bits as are missing in the current frame */ |
| 378 | rx_history_add_bits(tss, ubb_data(ubb), frame_bits_remaining); |
| 379 | ubb_pull(ubb, frame_bits_remaining); |
| 380 | |
| 381 | /* check if we still have frame sync */ |
| 382 | rc = correlate_history_against_pattern(tss); |
| 383 | if (rc > 0) { |
| 384 | tss->num_consecutive_errors++; |
| 385 | if (tss->num_consecutive_errors >= 3) { |
| 386 | tss->history_idx = 0; |
| 387 | /* send NULL frame to user */ |
| 388 | tss->out_cb(tss->user_data, NULL, 0); |
| 389 | osmo_fsm_inst_state_chg(fi, FRAME_ALIGNMENT_LOST, 1, T_SYNC); |
| 390 | osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, ubb); |
| 391 | return; |
| 392 | } |
| 393 | } else |
| 394 | tss->num_consecutive_errors = 0; |
| 395 | |
| 396 | /* dispatch aligned frame to user */ |
| 397 | tss->out_cb(tss->user_data, tss->history, tss->history_idx); |
| 398 | tss->history_idx = 0; |
| 399 | } |
| 400 | } |
| 401 | break; |
| 402 | default: |
| 403 | OSMO_ASSERT(0); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | static void trau_sync_alignment_lost(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 408 | { |
| 409 | /* we try to restore sync for some amount of time before generating an error */ |
| 410 | |
| 411 | switch (event) { |
| 412 | case TRAUSYNC_E_RX_BITS: |
| 413 | trau_sync_wait_align(fi, event, data); |
| 414 | break; |
| 415 | default: |
| 416 | OSMO_ASSERT(0); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | static void trau_sync_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 421 | { |
| 422 | switch (event) { |
| 423 | case TRAUSYNC_E_RESET: |
| 424 | osmo_fsm_inst_state_chg(fi, WAIT_FRAME_ALIGN, 0, 0); |
| 425 | break; |
| 426 | default: |
| 427 | OSMO_ASSERT(0); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static int trau_sync_timeout(struct osmo_fsm_inst *fi) |
| 432 | { |
| 433 | switch (fi->T) { |
| 434 | case T_SYNC: |
| 435 | /* if Tsync expires before frame synchronization is |
| 436 | * again obtained the TRAU initiates sending of the |
| 437 | * urgent alarm pattern described in clause 4.10.2. */ |
| 438 | osmo_fsm_inst_state_chg(fi, WAIT_FRAME_ALIGN, 0, 0); |
| 439 | break; |
| 440 | default: |
| 441 | OSMO_ASSERT(0); |
| 442 | } |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static const struct osmo_fsm_state trau_sync_states[] = { |
| 447 | [WAIT_FRAME_ALIGN] = { |
| 448 | .name = "WAIT_FRAME_ALIGN", |
| 449 | .in_event_mask = S(TRAUSYNC_E_RX_BITS), |
| 450 | .out_state_mask = S(FRAME_ALIGNED), |
| 451 | .action = trau_sync_wait_align, |
| 452 | }, |
| 453 | [FRAME_ALIGNED] = { |
| 454 | .name = "FRAME_ALIGNED", |
| 455 | .in_event_mask = S(TRAUSYNC_E_RX_BITS), |
| 456 | .out_state_mask = S(FRAME_ALIGNMENT_LOST) | S(WAIT_FRAME_ALIGN), |
| 457 | .action = trau_sync_aligned, |
| 458 | .onenter = trau_sync_aligned_onenter, |
| 459 | }, |
| 460 | [FRAME_ALIGNMENT_LOST] = { |
| 461 | .name = "FRAME_ALIGNMENT_LOST", |
| 462 | .in_event_mask = S(TRAUSYNC_E_RX_BITS), |
| 463 | .out_state_mask = S(WAIT_FRAME_ALIGN) | S(FRAME_ALIGNED), |
| 464 | .action = trau_sync_alignment_lost, |
| 465 | }, |
| 466 | }; |
| 467 | |
| 468 | static struct osmo_fsm trau_sync_fsm = { |
| 469 | .name = "trau_sync", |
| 470 | .states = trau_sync_states, |
| 471 | .num_states = ARRAY_SIZE(trau_sync_states), |
| 472 | .allstate_event_mask = S(TRAUSYNC_E_RESET), |
| 473 | .allstate_action = trau_sync_allstate, |
| 474 | .timer_cb = trau_sync_timeout, |
| 475 | .log_subsys = DLGLOBAL, |
| 476 | .event_names = trau_sync_event_names, |
| 477 | }; |
| 478 | |
| 479 | |
| 480 | struct osmo_fsm_inst * |
| 481 | osmo_trau_sync_alloc(void *ctx, const char *name, frame_out_cb_t frame_out_cb, |
| 482 | enum osmo_tray_sync_pat_id pat_id, void *user_data) |
| 483 | { |
| 484 | struct trau_rx_sync_state *tss; |
| 485 | struct osmo_fsm_inst *fi; |
| 486 | |
| 487 | if (pat_id >= ARRAY_SIZE(sync_patterns)) |
| 488 | return NULL; |
| 489 | |
Keith | bbff304 | 2021-04-28 21:33:00 -0500 | [diff] [blame] | 490 | fi = osmo_fsm_inst_alloc(&trau_sync_fsm, ctx, NULL, LOGL_INFO, name); |
Harald Welte | 78861c0 | 2020-05-14 13:28:07 +0200 | [diff] [blame] | 491 | if (!fi) |
| 492 | return NULL; |
| 493 | tss = talloc_zero(fi, struct trau_rx_sync_state); |
| 494 | if (!tss) { |
| 495 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 496 | return NULL; |
| 497 | } |
| 498 | fi->priv = tss; |
| 499 | |
| 500 | tss->out_cb = frame_out_cb; |
| 501 | tss->user_data = user_data; |
Harald Welte | 78861c0 | 2020-05-14 13:28:07 +0200 | [diff] [blame] | 502 | tss->pattern = &sync_patterns[pat_id]; |
| 503 | |
Philipp Maier | 8eae96f | 2020-07-31 20:01:42 +0200 | [diff] [blame] | 504 | /* An unusued E1 timeslot normally would send an idle signal that |
| 505 | * has all bits set to one. In order to prevent false-positive |
| 506 | * synchronization on startup we set all history bits to 1, to make |
| 507 | * it look like a signal from an unused timeslot. */ |
| 508 | memset(tss->history, 1, sizeof(tss->history)); |
| 509 | |
Harald Welte | 78861c0 | 2020-05-14 13:28:07 +0200 | [diff] [blame] | 510 | return fi; |
| 511 | } |
| 512 | |
Philipp Maier | 8d15001 | 2020-08-07 17:13:57 +0200 | [diff] [blame] | 513 | void osmo_trau_sync_set_pat(struct osmo_fsm_inst *fi, enum osmo_tray_sync_pat_id pat_id) |
| 514 | { |
| 515 | struct trau_rx_sync_state *tss = fi->priv; |
| 516 | |
| 517 | tss->pattern = &sync_patterns[pat_id]; |
| 518 | osmo_fsm_inst_state_chg(fi, FRAME_ALIGNMENT_LOST, 0, 0); |
| 519 | } |
| 520 | |
Harald Welte | 78861c0 | 2020-05-14 13:28:07 +0200 | [diff] [blame] | 521 | void osmo_trau_sync_rx_ubits(struct osmo_fsm_inst *fi, const ubit_t *bits, size_t n_bits) |
| 522 | { |
| 523 | struct ubit_buf ubb; |
| 524 | ubb_init(&ubb, bits, n_bits); |
| 525 | osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, &ubb); |
| 526 | } |
| 527 | |
| 528 | static void __attribute__((constructor)) on_dso_load_sync(void) |
| 529 | { |
| 530 | int i; |
| 531 | |
| 532 | for (i = 0; i < ARRAY_SIZE(sync_patterns); i++) |
| 533 | sync_pattern_register(&sync_patterns[i]); |
Harald Welte | ab8e066 | 2020-08-06 11:56:52 +0200 | [diff] [blame] | 534 | OSMO_ASSERT(osmo_fsm_register(&trau_sync_fsm) == 0); |
Harald Welte | 78861c0 | 2020-05-14 13:28:07 +0200 | [diff] [blame] | 535 | } |