2 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <osmocore/talloc.h>
30 #include <osmocom/logging.h>
31 #include <osmocom/osmocom_data.h>
32 #include <osmocom/mncc.h>
35 static int new_callref = 1;
36 static LLIST_HEAD(call_list);
42 void mncc_set_cause(struct gsm_mncc *data, int loc, int val);
44 static void free_call(struct gsm_call *call)
46 llist_del(&call->entry);
47 DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
52 struct gsm_call *get_call_ref(uint32_t callref)
54 struct gsm_call *callt;
56 llist_for_each_entry(callt, &call_list, entry) {
57 if (callt->callref == callref)
64 * MNCCms dummy application
67 /* this is a minimal implementation as required by GSM 04.08 */
68 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg)
70 struct gsm_mncc *data = arg;
71 uint32_t callref = data->callref;
74 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
77 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
79 /* reject, as we don't support Calls */
80 memset(&rel, 0, sizeof(struct gsm_mncc));
81 rel.callref = callref;
82 mncc_set_cause(&rel, GSM48_CAUSE_LOC_USER,
83 GSM48_CC_CAUSE_INCOMPAT_DEST);
85 return mncc_send(ms, MNCC_REL_REQ, &rel);
89 * MNCCms basic call application
92 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
94 struct gsm_mncc *data = arg;
95 struct gsm_call *call = get_call_ref(data->callref);
99 /* call does not exist */
100 if (!call && msg_type != MNCC_SETUP_IND) {
101 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
102 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
104 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
107 memset(&mncc, 0, sizeof(struct gsm_mncc));
108 mncc.callref = data->callref;
109 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_USER, cause);
110 return mncc_send(ms, MNCC_REL_REQ, &mncc);
113 /* setup without call */
115 call = talloc_zero(l23_ctx, struct gsm_call);
118 call->callref = new_callref++;
119 llist_add_tail(&call->entry, &call_list);
124 LOGP(DMNCC, LOGL_INFO, "Call has been disconnected\n");
125 if ((data->fields & MNCC_F_PROGRESS)
126 && data->progress.descr == 8)
129 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
133 LOGP(DMNCC, LOGL_INFO, "Call has been released\n");
136 case MNCC_CALL_PROC_IND:
137 LOGP(DMNCC, LOGL_INFO, "Call is proceeding\n");
140 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
143 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
146 if (!llist_empty(&call_list)) {
147 LOGP(DMNCC, LOGL_INFO, "Incomming call but busy\n");
148 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
151 LOGP(DMNCC, LOGL_INFO, "Incomming call\n");
152 memset(&mncc, 0, sizeof(struct gsm_mncc));
153 mncc.callref = call->callref;
154 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
156 LOGP(DMNCC, LOGL_INFO, "Ring!\n");
157 memset(&mncc, 0, sizeof(struct gsm_mncc));
158 mncc.callref = call->callref;
159 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
162 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
167 int mncc_call(struct osmocom_ms *ms, char *number)
169 struct gsm_call *call;
170 struct gsm_mncc setup;
172 if (!llist_empty(&call_list)) {
173 LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
177 call = talloc_zero(l23_ctx, struct gsm_call);
180 call->callref = new_callref++;
181 llist_add_tail(&call->entry, &call_list);
183 memset(&setup, 0, sizeof(struct gsm_mncc));
184 setup.callref = call->callref;
186 if (!strncasecmp(number, "emerg", 5)) {
187 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
191 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
193 setup.fields |= MNCC_F_CALLED;
194 strncpy(setup.called.number, number,
195 sizeof(setup.called.number) - 1);
197 /* bearer capability (mandatory) */
198 setup.fields |= MNCC_F_BEARER_CAP;
199 setup.bearer_cap.coding = 0;
200 setup.bearer_cap.radio = 1;
201 setup.bearer_cap.speech_ctm = 0;
202 setup.bearer_cap.speech_ver[0] = 0;
203 setup.bearer_cap.speech_ver[1] = -1; /* end of list */
204 setup.bearer_cap.transfer = 0;
205 setup.bearer_cap.mode = 0;
208 return mncc_send(ms, MNCC_SETUP_REQ, &setup);
211 int mncc_hangup(struct osmocom_ms *ms)
213 struct gsm_call *call;
214 struct gsm_mncc disc;
216 if (llist_empty(&call_list)) {
217 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
220 call = llist_entry(call_list.next, struct gsm_call, entry);
222 memset(&disc, 0, sizeof(struct gsm_mncc));
223 disc.callref = call->callref;
224 mncc_set_cause(&disc, GSM48_CAUSE_LOC_USER,
225 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
226 return mncc_send(ms, MNCC_DISC_REQ, &disc);
229 int mncc_answer(struct osmocom_ms *ms)
231 struct gsm_call *call;
234 if (llist_empty(&call_list)) {
235 LOGP(DMNCC, LOGL_INFO, "No call to answer\n");
238 call = llist_entry(call_list.next, struct gsm_call, entry);
240 memset(&rsp, 0, sizeof(struct gsm_mncc));
241 rsp.callref = call->callref;
242 mncc_set_cause(&rsp, GSM48_CAUSE_LOC_USER,
243 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
244 return mncc_send(ms, MNCC_SETUP_RSP, &rsp);