From f147776e47cdf3e0a47f10805e5374c56c5da928 Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Thu, 18 Jan 2024 03:56:01 +0700 Subject: [PATCH] mobile: split voice specific TCH handling into its own file Change-Id: Ib917d95fe77da3e60bc932cce56714d81095c7d4 Related: OS#4396 --- src/host/layer23/src/mobile/Makefile.am | 1 + src/host/layer23/src/mobile/tch.c | 133 ++++----------------------- src/host/layer23/src/mobile/tch_voice.c | 154 ++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 118 deletions(-) create mode 100644 src/host/layer23/src/mobile/tch_voice.c diff --git a/src/host/layer23/src/mobile/Makefile.am b/src/host/layer23/src/mobile/Makefile.am index ed2146fd..06399163 100644 --- a/src/host/layer23/src/mobile/Makefile.am +++ b/src/host/layer23/src/mobile/Makefile.am @@ -31,6 +31,7 @@ libmobile_a_SOURCES = \ mncc_sock.c \ primitives.c \ tch.c \ + tch_voice.c \ transaction.c \ vty_interface.c \ $(NULL) diff --git a/src/host/layer23/src/mobile/tch.c b/src/host/layer23/src/mobile/tch.c index adeaad5a..ad5a7249 100644 --- a/src/host/layer23/src/mobile/tch.c +++ b/src/host/layer23/src/mobile/tch.c @@ -30,82 +30,17 @@ #include #include #include -#include #include #include #include #include -/* Forward a Downlink voice frame to the external MNCC handler */ -static int tch_forward_mncc(struct osmocom_ms *ms, struct msgb *msg) -{ - struct gsm_data_frame *mncc; - - /* Drop the l1ctl_info_dl header */ - msgb_pull_to_l2(msg); - /* push mncc header in front of data */ - mncc = (struct gsm_data_frame *) - msgb_push(msg, sizeof(struct gsm_data_frame)); - mncc->callref = ms->mncc_entity.ref; +int tch_voice_state_init(struct gsm_trans *trans, + struct tch_voice_state *state); +void tch_voice_state_free(struct tch_voice_state *state); - switch (ms->rrlayer.cd_now.mode) { - case GSM48_CMODE_SPEECH_V1: - { - const uint8_t cbits = ms->rrlayer.cd_now.chan_nr >> 3; - if (cbits == ABIS_RSL_CHAN_NR_CBITS_Bm_ACCHs) - mncc->msg_type = GSM_TCHF_FRAME; - else - mncc->msg_type = GSM_TCHH_FRAME; - break; - } - case GSM48_CMODE_SPEECH_EFR: - mncc->msg_type = GSM_TCHF_FRAME_EFR; - break; - case GSM48_CMODE_SPEECH_AMR: /* TODO: no AMR support yet */ - default: - /* TODO: print error message here */ - goto exit_free; - } - - /* distribute and then free */ - if (ms->mncc_entity.sock_state && ms->mncc_entity.ref) - return mncc_sock_from_cc(ms->mncc_entity.sock_state, msg); - -exit_free: - msgb_free(msg); - return 0; -} - -/* Receive a Downlink voice frame from the lower layers */ -static int tch_recv_voice(struct osmocom_ms *ms, struct msgb *msg) -{ - struct tch_state *state = ms->tch_state; - - switch (state->voice.handler) { - case TCH_VOICE_IOH_LOOPBACK: - /* Remove the DL info header */ - msgb_pull_to_l2(msg); - /* Send voice frame back */ - return tch_send_msg(ms, msg); - case TCH_VOICE_IOH_MNCC_SOCK: - return tch_forward_mncc(ms, msg); - case TCH_VOICE_IOH_GAPK: -#ifdef WITH_GAPK_IO - /* Enqueue a frame to the DL TCH buffer */ - if (state->voice.gapk_io != NULL) - gapk_io_enqueue_dl(state->voice.gapk_io, msg); - else - msgb_free(msg); - break; -#endif - case TCH_VOICE_IOH_L1PHY: - case TCH_VOICE_IOH_NONE: - /* Drop voice frame */ - msgb_free(msg); - } - - return 0; -} +int tch_voice_recv(struct osmocom_ms *ms, struct msgb *msg); +int tch_voice_serve_ms(struct osmocom_ms *ms); /* Receive a Downlink traffic (voice/data) frame from the lower layers */ static int tch_recv_cb(struct osmocom_ms *ms, struct msgb *msg) @@ -119,7 +54,7 @@ static int tch_recv_cb(struct osmocom_ms *ms, struct msgb *msg) } if (state->is_voice) - rc = tch_recv_voice(ms, msg); + rc = tch_voice_recv(ms, msg); else /* TODO: tch_recv_data() */ msgb_free(msg); @@ -169,53 +104,15 @@ int tch_send_mncc_frame(struct osmocom_ms *ms, const struct gsm_data_frame *fram int tch_serve_ms(struct osmocom_ms *ms) { struct tch_state *state = ms->tch_state; + int rc = 0; if (state == NULL) return 0; - if (state->is_voice) { -#ifdef WITH_GAPK_IO - if (state->voice.handler == TCH_VOICE_IOH_GAPK) - return gapk_io_serve_ms(ms, state->voice.gapk_io); -#endif - } - - return 0; -} - -static int tch_state_init_voice(struct osmocom_ms *ms, - struct tch_voice_state *state) -{ - const struct gsm48_rr_cd *cd = &ms->rrlayer.cd_now; - - switch (state->handler) { -#ifdef WITH_GAPK_IO - case TCH_VOICE_IOH_GAPK: - if ((cd->chan_nr & RSL_CHAN_NR_MASK) == RSL_CHAN_Bm_ACCHs) - state->gapk_io = gapk_io_state_alloc_mode_rate(ms, cd->mode, true); - else /* RSL_CHAN_Lm_ACCHs */ - state->gapk_io = gapk_io_state_alloc_mode_rate(ms, cd->mode, false); - if (state->gapk_io == NULL) - return -1; - break; -#endif - default: - break; - } - - return 0; -} + if (state->is_voice) + rc = tch_voice_serve_ms(ms); + /* TODO: else tch_data_serve_ms() */ -static void tch_state_free_voice(struct tch_voice_state *state) -{ - switch (state->handler) { -#ifdef WITH_GAPK_IO - case TCH_VOICE_IOH_GAPK: - gapk_io_state_free(state->gapk_io); - break; -#endif - default: - break; - } + return rc; } static void tch_trans_cstate_active_cb(struct gsm_trans *trans) @@ -237,7 +134,7 @@ static void tch_trans_cstate_active_cb(struct gsm_trans *trans) case GSM48_CMODE_SPEECH_AMR: state->is_voice = true; state->voice.handler = ms->settings.tch_voice.io_handler; - if (tch_state_init_voice(ms, &state->voice) != 0) + if (tch_voice_state_init(trans, &state->voice) != 0) goto exit_free; break; case GSM48_CMODE_DATA_14k5: @@ -247,8 +144,8 @@ static void tch_trans_cstate_active_cb(struct gsm_trans *trans) #if 0 state->is_voice = false; state->data.handler = ms->settings.tch_data.io_handler; - /* TODO: tch_state_init_data() */ - if (tch_state_init_data(ms, &state->data) != 0) + /* TODO: tch_data_state_init() */ + if (tch_data_state_init(trans, &state->data) != 0) goto exit_free; break; #endif @@ -272,7 +169,7 @@ static void tch_trans_free_cb(struct gsm_trans *trans) if (state == NULL) return; if (state->is_voice) - tch_state_free_voice(&state->voice); + tch_voice_state_free(&state->voice); #if 0 else /* TODO: tch_state_free_data() */ tch_state_free_data(&state->data); diff --git a/src/host/layer23/src/mobile/tch_voice.c b/src/host/layer23/src/mobile/tch_voice.c new file mode 100644 index 00000000..f4bbcfad --- /dev/null +++ b/src/host/layer23/src/mobile/tch_voice.c @@ -0,0 +1,154 @@ +/* + * (C) 2017-2018 by Vadim Yanitskiy + * (C) 2022-2024 by sysmocom - s.f.m.c. GmbH + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Forward a Downlink voice frame to the external MNCC handler */ +static int tch_forward_mncc(struct osmocom_ms *ms, struct msgb *msg) +{ + struct gsm_data_frame *mncc; + + /* Drop the l1ctl_info_dl header */ + msgb_pull_to_l2(msg); + /* push mncc header in front of data */ + mncc = (struct gsm_data_frame *) + msgb_push(msg, sizeof(struct gsm_data_frame)); + mncc->callref = ms->mncc_entity.ref; + + switch (ms->rrlayer.cd_now.mode) { + case GSM48_CMODE_SPEECH_V1: + { + const uint8_t cbits = ms->rrlayer.cd_now.chan_nr >> 3; + if (cbits == ABIS_RSL_CHAN_NR_CBITS_Bm_ACCHs) + mncc->msg_type = GSM_TCHF_FRAME; + else + mncc->msg_type = GSM_TCHH_FRAME; + break; + } + case GSM48_CMODE_SPEECH_EFR: + mncc->msg_type = GSM_TCHF_FRAME_EFR; + break; + case GSM48_CMODE_SPEECH_AMR: /* TODO: no AMR support yet */ + default: + /* TODO: print error message here */ + goto exit_free; + } + + /* distribute and then free */ + if (ms->mncc_entity.sock_state && ms->mncc_entity.ref) + return mncc_sock_from_cc(ms->mncc_entity.sock_state, msg); + +exit_free: + msgb_free(msg); + return 0; +} + +int tch_voice_recv(struct osmocom_ms *ms, struct msgb *msg) +{ + struct tch_voice_state *state = &ms->tch_state->voice; + + switch (state->handler) { + case TCH_VOICE_IOH_LOOPBACK: + /* Remove the DL info header */ + msgb_pull_to_l2(msg); + /* Send voice frame back */ + return tch_send_msg(ms, msg); + case TCH_VOICE_IOH_MNCC_SOCK: + return tch_forward_mncc(ms, msg); + case TCH_VOICE_IOH_GAPK: +#ifdef WITH_GAPK_IO + /* Enqueue a frame to the DL TCH buffer */ + if (state->gapk_io != NULL) + gapk_io_enqueue_dl(state->gapk_io, msg); + else + msgb_free(msg); + break; +#endif + case TCH_VOICE_IOH_L1PHY: + case TCH_VOICE_IOH_NONE: + /* Drop voice frame */ + msgb_free(msg); + break; + } + + return 0; +} + +int tch_voice_serve_ms(struct osmocom_ms *ms) +{ +#ifdef WITH_GAPK_IO + struct tch_voice_state *state = &ms->tch_state->voice; + + if (state->handler == TCH_VOICE_IOH_GAPK) + return gapk_io_serve_ms(ms, state->gapk_io); +#endif + + return 0; +} + +int tch_voice_state_init(struct gsm_trans *trans, struct tch_voice_state *state) +{ +#ifdef WITH_GAPK_IO + struct osmocom_ms *ms = trans->ms; + const struct gsm48_rr_cd *cd = &ms->rrlayer.cd_now; + + switch (state->handler) { + case TCH_VOICE_IOH_GAPK: + if ((cd->chan_nr & RSL_CHAN_NR_MASK) == RSL_CHAN_Bm_ACCHs) + state->gapk_io = gapk_io_state_alloc_mode_rate(ms, cd->mode, true); + else /* RSL_CHAN_Lm_ACCHs */ + state->gapk_io = gapk_io_state_alloc_mode_rate(ms, cd->mode, false); + if (state->gapk_io == NULL) + return -1; + break; + default: + break; + } +#endif + + return 0; +} + +void tch_voice_state_free(struct tch_voice_state *state) +{ + switch (state->handler) { +#ifdef WITH_GAPK_IO + case TCH_VOICE_IOH_GAPK: + gapk_io_state_free(state->gapk_io); + break; +#endif + default: + break; + } +} -- 2.11.4.GIT