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>
33 #include <osmocom/vty.h>
36 static int new_callref = 1;
37 static LLIST_HEAD(call_list);
43 void mncc_set_cause(struct gsm_mncc *data, int loc, int val);
45 static void free_call(struct gsm_call *call)
47 llist_del(&call->entry);
48 DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
53 struct gsm_call *get_call_ref(uint32_t callref)
55 struct gsm_call *callt;
57 llist_for_each_entry(callt, &call_list, entry) {
58 if (callt->callref == callref)
65 * MNCCms dummy application
68 /* this is a minimal implementation as required by GSM 04.08 */
69 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg)
71 struct gsm_mncc *data = arg;
72 uint32_t callref = data->callref;
75 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
78 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
80 /* reject, as we don't support Calls */
81 memset(&rel, 0, sizeof(struct gsm_mncc));
82 rel.callref = callref;
83 mncc_set_cause(&rel, GSM48_CAUSE_LOC_USER,
84 GSM48_CC_CAUSE_INCOMPAT_DEST);
86 return mncc_send(ms, MNCC_REL_REQ, &rel);
90 * MNCCms basic call application
93 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
95 struct gsm_mncc *data = arg;
96 struct gsm_call *call = get_call_ref(data->callref);
101 /* call does not exist */
102 if (!call && msg_type != MNCC_SETUP_IND) {
103 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call "
104 "(callref %d)\n", data->callref);
105 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
107 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
109 memset(&mncc, 0, sizeof(struct gsm_mncc));
110 mncc.callref = data->callref;
111 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_USER, cause);
112 return mncc_send(ms, MNCC_REL_REQ, &mncc);
115 /* setup without call */
117 if (llist_empty(&call_list))
119 call = talloc_zero(l23_ctx, struct gsm_call);
122 call->callref = new_callref++;
123 llist_add_tail(&call->entry, &call_list);
128 vty_notify(ms, NULL);
129 switch (data->cause.value) {
130 case GSM48_CC_CAUSE_UNASSIGNED_NR:
131 vty_notify(ms, "Call: Number not assigned\n");
133 case GSM48_CC_CAUSE_NO_ROUTE:
134 vty_notify(ms, "Call: Destination unreachable\n");
136 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
137 vty_notify(ms, "Call: Remote hangs up\n");
139 case GSM48_CC_CAUSE_USER_BUSY:
140 vty_notify(ms, "Call: Remote busy\n");
142 case GSM48_CC_CAUSE_USER_NOTRESPOND:
143 vty_notify(ms, "Call: Remote not responding\n");
145 case GSM48_CC_CAUSE_USER_ALERTING_NA:
146 vty_notify(ms, "Call: Remote not answering\n");
148 case GSM48_CC_CAUSE_CALL_REJECTED:
149 vty_notify(ms, "Call has been rejected\n");
151 case GSM48_CC_CAUSE_NUMBER_CHANGED:
152 vty_notify(ms, "Call: Number changed\n");
154 case GSM48_CC_CAUSE_PRE_EMPTION:
155 vty_notify(ms, "Call: Cleared due to pre-emption\n");
157 case GSM48_CC_CAUSE_DEST_OOO:
158 vty_notify(ms, "Call: Remote out of order\n");
160 case GSM48_CC_CAUSE_INV_NR_FORMAT:
161 vty_notify(ms, "Call: Number invalid or imcomplete\n");
163 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
164 vty_notify(ms, "Call: No channel available\n");
166 case GSM48_CC_CAUSE_NETWORK_OOO:
167 vty_notify(ms, "Call: Network out of order\n");
169 case GSM48_CC_CAUSE_TEMP_FAILURE:
170 vty_notify(ms, "Call: Temporary failure\n");
172 case GSM48_CC_CAUSE_SWITCH_CONG:
173 vty_notify(ms, "Congestion\n");
176 vty_notify(ms, "Call has been disconnected\n");
178 LOGP(DMNCC, LOGL_INFO, "Call has been disconnected "
179 "(cause %d)\n", data->cause.value);
180 if ((data->fields & MNCC_F_PROGRESS)
181 && data->progress.descr == 8) {
182 vty_notify(ms, "Please hang up!\n");
186 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
190 vty_notify(ms, NULL);
191 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
192 vty_notify(ms, "Call has been rejected\n");
194 vty_notify(ms, "Call has been released\n");
195 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
199 case MNCC_CALL_PROC_IND:
200 vty_notify(ms, NULL);
201 vty_notify(ms, "Call is proceeding\n");
202 LOGP(DMNCC, LOGL_INFO, "Call is proceeding\n");
205 vty_notify(ms, NULL);
206 vty_notify(ms, "Call is aleriting\n");
207 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
210 vty_notify(ms, NULL);
211 vty_notify(ms, "Call is answered\n");
212 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
215 vty_notify(ms, NULL);
216 if (!first_call && !ms->settings.cw) {
217 vty_notify(ms, "Incomming call rejected while busy\n");
218 LOGP(DMNCC, LOGL_INFO, "Incomming call but busy\n");
219 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
222 /* presentation allowed if present == 0 */
223 if (data->calling.present || !data->calling.number[0])
224 vty_notify(ms, "Incomming call\n");
225 else if (data->calling.type == 1)
226 vty_notify(ms, "Incomming call from +%s\n",
227 data->calling.number);
228 else if (data->calling.type == 2)
229 vty_notify(ms, "Incomming call from 0-%s\n",
230 data->calling.number);
232 vty_notify(ms, "Incomming call from %s\n",
233 data->calling.number);
234 LOGP(DMNCC, LOGL_INFO, "Incomming call\n");
235 memset(&mncc, 0, sizeof(struct gsm_mncc));
236 mncc.callref = call->callref;
237 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
239 LOGP(DMNCC, LOGL_INFO, "Ring!\n");
241 LOGP(DMNCC, LOGL_INFO, "Knock!\n");
245 memset(&mncc, 0, sizeof(struct gsm_mncc));
246 mncc.callref = call->callref;
247 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
250 vty_notify(ms, NULL);
251 vty_notify(ms, "Call is on hold\n");
252 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
256 vty_notify(ms, NULL);
257 vty_notify(ms, "Call hold was rejected\n");
258 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
260 case MNCC_RETRIEVE_CNF:
261 vty_notify(ms, NULL);
262 vty_notify(ms, "Call is retrieved\n");
263 LOGP(DMNCC, LOGL_INFO, "Call is retrieved\n");
266 case MNCC_RETRIEVE_REJ:
267 vty_notify(ms, NULL);
268 vty_notify(ms, "Call retrieve was rejected\n");
269 LOGP(DMNCC, LOGL_INFO, "Call retrieve was rejected\n");
272 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
280 int mncc_call(struct osmocom_ms *ms, char *number)
282 struct gsm_call *call;
283 struct gsm_mncc setup;
285 llist_for_each_entry(call, &call_list, entry) {
287 vty_notify(ms, NULL);
288 vty_notify(ms, "Please put active call on hold "
290 LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
295 call = talloc_zero(l23_ctx, struct gsm_call);
298 call->callref = new_callref++;
299 llist_add_tail(&call->entry, &call_list);
301 memset(&setup, 0, sizeof(struct gsm_mncc));
302 setup.callref = call->callref;
304 if (!strncasecmp(number, "emerg", 5)) {
305 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
309 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
311 setup.fields |= MNCC_F_CALLED;
312 strncpy(setup.called.number, number,
313 sizeof(setup.called.number) - 1);
315 /* bearer capability (mandatory) */
316 setup.fields |= MNCC_F_BEARER_CAP;
317 setup.bearer_cap.coding = 0;
318 setup.bearer_cap.radio = 1;
319 setup.bearer_cap.speech_ctm = 0;
320 setup.bearer_cap.speech_ver[0] = 0;
321 setup.bearer_cap.speech_ver[1] = -1; /* end of list */
322 setup.bearer_cap.transfer = 0;
323 setup.bearer_cap.mode = 0;
324 if (ms->settings.clir)
326 else if (ms->settings.clip)
330 return mncc_send(ms, MNCC_SETUP_REQ, &setup);
333 int mncc_hangup(struct osmocom_ms *ms)
335 struct gsm_call *call, *found = NULL;
336 struct gsm_mncc disc;
338 llist_for_each_entry(call, &call_list, entry) {
345 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
346 vty_notify(ms, NULL);
347 vty_notify(ms, "No active call\n");
351 memset(&disc, 0, sizeof(struct gsm_mncc));
352 disc.callref = found->callref;
353 mncc_set_cause(&disc, GSM48_CAUSE_LOC_USER,
354 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
355 return mncc_send(ms, MNCC_DISC_REQ, &disc);
358 int mncc_answer(struct osmocom_ms *ms)
360 struct gsm_call *call, *alerting = NULL;
364 llist_for_each_entry(call, &call_list, entry) {
367 else if (!call->hold)
371 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
372 vty_notify(ms, NULL);
373 vty_notify(ms, "No alerting call\n");
377 LOGP(DMNCC, LOGL_INFO, "Answer but we have an active call\n");
378 vty_notify(ms, NULL);
379 vty_notify(ms, "Please put active call on hold first!\n");
385 memset(&rsp, 0, sizeof(struct gsm_mncc));
386 rsp.callref = alerting->callref;
387 return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
390 int mncc_hold(struct osmocom_ms *ms)
392 struct gsm_call *call, *found = NULL;
393 struct gsm_mncc hold;
395 llist_for_each_entry(call, &call_list, entry) {
402 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
403 vty_notify(ms, NULL);
404 vty_notify(ms, "No active call\n");
408 memset(&hold, 0, sizeof(struct gsm_mncc));
409 hold.callref = found->callref;
410 return mncc_send(ms, MNCC_HOLD_REQ, &hold);
413 int mncc_retrieve(struct osmocom_ms *ms, int number)
415 struct gsm_call *call;
416 struct gsm_mncc retr;
417 int holdnum = 0, active = 0, i = 0;
419 llist_for_each_entry(call, &call_list, entry) {
426 LOGP(DMNCC, LOGL_INFO, "Cannot retrieve during active call\n");
427 vty_notify(ms, NULL);
428 vty_notify(ms, "Hold active call first!\n");
432 vty_notify(ms, NULL);
433 vty_notify(ms, "No call on hold!\n");
436 if (holdnum > 1 && number <= 0) {
437 vty_notify(ms, NULL);
438 vty_notify(ms, "Select call 1..%d\n", holdnum);
441 if (holdnum == 1 && number <= 0)
443 if (number > holdnum) {
444 vty_notify(ms, NULL);
445 vty_notify(ms, "Given number %d out of range!\n", number);
446 vty_notify(ms, "Select call 1..%d\n", holdnum);
450 llist_for_each_entry(call, &call_list, entry) {
456 memset(&retr, 0, sizeof(struct gsm_mncc));
457 retr.callref = call->callref;
458 return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);