summaryrefslogtreecommitdiffstats
path: root/src/host/layer23/src/mobile/voice.c
blob: ddecb82ae23426d2e43ac3e7e1a3250de9caff48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
 * (C) 2017-2018 by Vadim Yanitskiy <axilirator@gmail.com>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <string.h>
#include <errno.h>

#include <osmocom/core/msgb.h>

#include <osmocom/bb/common/osmocom_data.h>
#include <osmocom/bb/mobile/settings.h>
#include <osmocom/bb/mobile/gapk_io.h>
#include <osmocom/bb/mobile/mncc.h>
#include <osmocom/bb/mobile/voice.h>

/*
 * TCH frame (voice) router
 */
static int gsm_recv_voice(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_data_frame *frame;

	/* Make sure that a MNCC handler is set */
	if (!ms->mncc_entity.mncc_recv) {
		msgb_free(msg);
		return -ENOTSUP;
	}

	/* TODO: Make sure there is an active call */

	/* Route a frame according to settings */
	switch (ms->settings.audio.io_target) {
	/* External MNCC application (e.g. LCR) */
	case AUDIO_IO_SOCKET:
		/* Push MNCC header in front of data */
		frame = (struct gsm_data_frame *)
			msgb_push(msg, sizeof(struct gsm_data_frame));

		/* FIXME: set proper msg_type */
		frame->msg_type = GSM_TCHF_FRAME;
		frame->callref = ms->mncc_entity.ref;

		/* Forward to an MNCC-handler */
		ms->mncc_entity.mncc_recv(ms, frame->msg_type, frame);

		/* Release memory */
		msgb_free(msg);
		break;

	/* Build-in GAPK-based audio back-end */
	case AUDIO_IO_GAPK:
		/* Prevent null pointer dereference */
		if (!ms->gapk_io) {
			msgb_free(msg);
			break;
		}

		/* Push a frame to the DL frame buffer */
		msgb_enqueue(&ms->gapk_io->tch_fb_dl, msg);
		break;

	/* Drop frame and release memory */
	case AUDIO_IO_HARDWARE:
	case AUDIO_IO_NONE:
	default:
		msgb_free(msg);
	}

	return 0;
}

/*
 * Send voice to the lower layers
 */
int gsm_send_voice(struct osmocom_ms *ms, struct msgb *msg)
{
	/**
	 * Nothing to do for now...
	 * TODO: compose l1ctl_traffic_ind header here
	 */

	/* Forward to RR */
	return gsm48_rr_tx_voice(ms, msg);
}

int gsm_send_voice_mncc(struct osmocom_ms *ms, struct gsm_data_frame *frame)
{
	struct msgb *nmsg;

	nmsg = msgb_alloc_headroom(33 + 64, 64, "TCH/F");
	if (!nmsg)
		return -ENOMEM;
	nmsg->l2h = msgb_put(nmsg, 33);
	memcpy(nmsg->l2h, frame->data, 33);

	return gsm_send_voice(ms, nmsg);
}

/*
 * Init TCH frame (voice) router
 */
int gsm_voice_init(struct osmocom_ms *ms)
{
	ms->l1_entity.l1_traffic_ind = gsm_recv_voice;
	return 0;
}