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/bb/common/logging.h>
31 #include <osmocom/bb/common/osmocom_data.h>
32 #include <osmocom/bb/mobile/mncc.h>
33 #include <osmocom/bb/mobile/vty.h>
36 static uint32_t new_callref = 1;
37 static LLIST_HEAD(call_list);
39 void mncc_set_cause(struct gsm_mncc *data, int loc, int val);
40 static int dtmf_statemachine(struct gsm_call *call, struct gsm_mncc *mncc);
41 static void timeout_dtmf(void *arg);
48 static void start_dtmf_timer(struct gsm_call *call, uint16_t ms)
50 LOGP(DCC, LOGL_INFO, "starting DTMF timer %d ms\n", ms);
51 call->dtmf_timer.cb = timeout_dtmf;
52 call->dtmf_timer.data = call;
53 bsc_schedule_timer(&call->dtmf_timer, 0, ms * 1000);
56 static void stop_dtmf_timer(struct gsm_call *call)
58 if (bsc_timer_pending(&call->dtmf_timer)) {
59 LOGP(DCC, LOGL_INFO, "stopping pending DTMF timer\n");
60 bsc_del_timer(&call->dtmf_timer);
64 /* free call instance */
65 static void free_call(struct gsm_call *call)
67 stop_dtmf_timer(call);
69 llist_del(&call->entry);
70 DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
75 struct gsm_call *get_call_ref(uint32_t callref)
77 struct gsm_call *callt;
79 llist_for_each_entry(callt, &call_list, entry) {
80 if (callt->callref == callref)
86 static int8_t mncc_get_bearer(struct gsm_settings *set, uint8_t speech_ver)
91 LOGP(DMNCC, LOGL_INFO, " net suggests full rate v3\n");
93 LOGP(DMNCC, LOGL_INFO, " full rate v3 not supported\n");
99 LOGP(DMNCC, LOGL_INFO, " net suggests full rate v2\n");
101 LOGP(DMNCC, LOGL_INFO, " full rate v2 not supported\n");
105 case 0: /* mandatory */
107 LOGP(DMNCC, LOGL_INFO, " net suggests full rate v1\n");
109 LOGP(DMNCC, LOGL_INFO, " full rate v1 not supported\n");
115 LOGP(DMNCC, LOGL_INFO, " net suggests half rate v3\n");
117 LOGP(DMNCC, LOGL_INFO, " half rate v3 not supported\n");
123 LOGP(DMNCC, LOGL_INFO, " net suggests half rate v1\n");
125 LOGP(DMNCC, LOGL_INFO, " half rate v1 not supported\n");
130 LOGP(DMNCC, LOGL_INFO, " net suggests unknown speech version "
138 static void mncc_set_bearer(struct osmocom_ms *ms, int8_t speech_ver,
139 struct gsm_mncc *mncc)
141 struct gsm_settings *set = &ms->settings;
144 mncc->fields |= MNCC_F_BEARER_CAP;
145 mncc->bearer_cap.coding = 0;
146 if (set->ch_cap == GSM_CAP_SDCCH_TCHF_TCHH
147 && (set->half_v1 || set->half_v3)) {
148 mncc->bearer_cap.radio = 3;
149 LOGP(DMNCC, LOGL_INFO, " support TCH/H also\n");
151 mncc->bearer_cap.radio = 1;
152 LOGP(DMNCC, LOGL_INFO, " support TCH/F only\n");
154 mncc->bearer_cap.speech_ctm = 0;
155 /* if no specific speech_ver is given */
156 if (speech_ver < 0) {
157 /* if half rate is supported and prefered */
158 if (set->half_v3 && set->half && set->half_prefer) {
159 mncc->bearer_cap.speech_ver[i++] = 5;
160 LOGP(DMNCC, LOGL_INFO, " support half rate v3\n");
162 if (set->half_v1 && set->half && set->half_prefer) {
163 mncc->bearer_cap.speech_ver[i++] = 1;
164 LOGP(DMNCC, LOGL_INFO, " support half rate v1\n");
166 /* if full rate is supported */
168 mncc->bearer_cap.speech_ver[i++] = 4;
169 LOGP(DMNCC, LOGL_INFO, " support full rate v3\n");
172 mncc->bearer_cap.speech_ver[i++] = 2;
173 LOGP(DMNCC, LOGL_INFO, " support full rate v2\n");
175 if (set->full_v1) { /* mandatory, so it's always true */
176 mncc->bearer_cap.speech_ver[i++] = 0;
177 LOGP(DMNCC, LOGL_INFO, " support full rate v1\n");
179 /* if half rate is supported and not prefered */
180 if (set->half_v3 && set->half && !set->half_prefer) {
181 mncc->bearer_cap.speech_ver[i++] = 5;
182 LOGP(DMNCC, LOGL_INFO, " support half rate v3\n");
184 if (set->half_v1 && set->half && !set->half_prefer) {
185 mncc->bearer_cap.speech_ver[i++] = 1;
186 LOGP(DMNCC, LOGL_INFO, " support half rate v1\n");
188 /* if specific speech_ver is given (it must be supported) */
190 mncc->bearer_cap.speech_ver[i++] = speech_ver;
191 mncc->bearer_cap.speech_ver[i] = -1; /* end of list */
192 mncc->bearer_cap.transfer = 0;
193 mncc->bearer_cap.mode = 0;
197 * MNCCms dummy application
200 /* this is a minimal implementation as required by GSM 04.08 */
201 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg)
203 struct gsm_mncc *data = arg;
204 uint32_t callref = data->callref;
207 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
210 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
212 /* reject, as we don't support Calls */
213 memset(&rel, 0, sizeof(struct gsm_mncc));
214 rel.callref = callref;
215 mncc_set_cause(&rel, GSM48_CAUSE_LOC_USER,
216 GSM48_CC_CAUSE_INCOMPAT_DEST);
218 return mncc_send(ms, MNCC_REL_REQ, &rel);
222 * MNCCms basic call application
225 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
227 struct gsm_settings *set = &ms->settings;
228 struct gsm_mncc *data = arg;
229 struct gsm_call *call = get_call_ref(data->callref);
230 struct gsm_mncc mncc;
232 int8_t speech_ver = -1, speech_ver_half = -1, temp;
235 /* call does not exist */
236 if (!call && msg_type != MNCC_SETUP_IND) {
237 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call "
238 "(callref %x)\n", data->callref);
239 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
241 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
243 memset(&mncc, 0, sizeof(struct gsm_mncc));
244 mncc.callref = data->callref;
245 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_USER, cause);
246 return mncc_send(ms, MNCC_REL_REQ, &mncc);
249 /* setup without call */
251 if (llist_empty(&call_list))
253 call = talloc_zero(l23_ctx, struct gsm_call);
257 call->callref = data->callref;
258 llist_add_tail(&call->entry, &call_list);
261 /* not in initiated state anymore */
266 vty_notify(ms, NULL);
267 switch (data->cause.value) {
268 case GSM48_CC_CAUSE_UNASSIGNED_NR:
269 vty_notify(ms, "Call: Number not assigned\n");
271 case GSM48_CC_CAUSE_NO_ROUTE:
272 vty_notify(ms, "Call: Destination unreachable\n");
274 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
275 vty_notify(ms, "Call: Remote hangs up\n");
277 case GSM48_CC_CAUSE_USER_BUSY:
278 vty_notify(ms, "Call: Remote busy\n");
280 case GSM48_CC_CAUSE_USER_NOTRESPOND:
281 vty_notify(ms, "Call: Remote not responding\n");
283 case GSM48_CC_CAUSE_USER_ALERTING_NA:
284 vty_notify(ms, "Call: Remote not answering\n");
286 case GSM48_CC_CAUSE_CALL_REJECTED:
287 vty_notify(ms, "Call has been rejected\n");
289 case GSM48_CC_CAUSE_NUMBER_CHANGED:
290 vty_notify(ms, "Call: Number changed\n");
292 case GSM48_CC_CAUSE_PRE_EMPTION:
293 vty_notify(ms, "Call: Cleared due to pre-emption\n");
295 case GSM48_CC_CAUSE_DEST_OOO:
296 vty_notify(ms, "Call: Remote out of order\n");
298 case GSM48_CC_CAUSE_INV_NR_FORMAT:
299 vty_notify(ms, "Call: Number invalid or imcomplete\n");
301 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
302 vty_notify(ms, "Call: No channel available\n");
304 case GSM48_CC_CAUSE_NETWORK_OOO:
305 vty_notify(ms, "Call: Network out of order\n");
307 case GSM48_CC_CAUSE_TEMP_FAILURE:
308 vty_notify(ms, "Call: Temporary failure\n");
310 case GSM48_CC_CAUSE_SWITCH_CONG:
311 vty_notify(ms, "Congestion\n");
314 vty_notify(ms, "Call has been disconnected "
315 "(clear cause %d)\n", data->cause.value);
317 LOGP(DMNCC, LOGL_INFO, "Call has been disconnected "
318 "(cause %d)\n", data->cause.value);
319 if ((data->fields & MNCC_F_PROGRESS)
320 && data->progress.descr == 8) {
321 vty_notify(ms, "Please hang up!\n");
325 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
329 vty_notify(ms, NULL);
330 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
331 vty_notify(ms, "Call has been rejected\n");
333 vty_notify(ms, "Call has been released\n");
334 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
338 case MNCC_CALL_PROC_IND:
339 vty_notify(ms, NULL);
340 vty_notify(ms, "Call is proceeding\n");
341 LOGP(DMNCC, LOGL_INFO, "Call is proceeding\n");
342 if ((data->fields & MNCC_F_BEARER_CAP)
343 && data->bearer_cap.speech_ver[0] >= 0) {
344 mncc_get_bearer(set, data->bearer_cap.speech_ver[0]);
348 vty_notify(ms, NULL);
349 vty_notify(ms, "Call is aleriting\n");
350 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
353 vty_notify(ms, NULL);
354 vty_notify(ms, "Call is answered\n");
355 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
358 vty_notify(ms, NULL);
359 if (!first_call && !ms->settings.cw) {
360 vty_notify(ms, "Incomming call rejected while busy\n");
361 LOGP(DMNCC, LOGL_INFO, "Incomming call but busy\n");
362 cause = GSM48_CC_CAUSE_USER_BUSY;
365 /* select first supported speech_ver */
366 if ((data->fields & MNCC_F_BEARER_CAP)) {
369 for (i = 0; data->bearer_cap.speech_ver[i] >= 0; i++) {
371 temp = mncc_get_bearer(set,
372 data->bearer_cap.speech_ver[i]);
375 if (temp == 5 || temp == 1) { /* half */
376 /* only the first half rate */
377 if (speech_ver_half < 0)
378 speech_ver_half = temp;
380 /* only the first full rate */
385 /* half and full given */
386 if (speech_ver_half >= 0 && speech_ver >= 0) {
387 if (set->half_prefer) {
388 LOGP(DMNCC, LOGL_INFO, " both supported"
389 " codec rates are given, using "
390 "preferred half rate\n");
391 speech_ver = speech_ver_half;
393 LOGP(DMNCC, LOGL_INFO, " both supported"
394 " codec rates are given, using "
395 "preferred full rate\n");
396 } else if (speech_ver_half < 0 && speech_ver < 0) {
397 LOGP(DMNCC, LOGL_INFO, " no supported codec "
399 /* only half rate is given, use it */
400 } else if (speech_ver_half >= 0) {
401 LOGP(DMNCC, LOGL_INFO, " only supported half "
402 "rate codec is given, using it\n");
403 speech_ver = speech_ver_half;
404 /* only full rate is given, use it */
406 LOGP(DMNCC, LOGL_INFO, " only supported full "
407 "rate codec is given, using it\n");
410 /* presentation allowed if present == 0 */
411 if (data->calling.present || !data->calling.number[0])
412 vty_notify(ms, "Incomming call (anonymous)\n");
413 else if (data->calling.type == 1)
414 vty_notify(ms, "Incomming call (from +%s)\n",
415 data->calling.number);
416 else if (data->calling.type == 2)
417 vty_notify(ms, "Incomming call (from 0-%s)\n",
418 data->calling.number);
420 vty_notify(ms, "Incomming call (from %s)\n",
421 data->calling.number);
422 LOGP(DMNCC, LOGL_INFO, "Incomming call (from %s callref %x)\n",
423 data->calling.number, call->callref);
424 memset(&mncc, 0, sizeof(struct gsm_mncc));
425 mncc.callref = call->callref;
426 /* only include bearer cap, if not given in setup
427 * or if multiple codecs are given
428 * or if not only full rate
429 * or if given codec is unimplemented
431 if (!(data->fields & MNCC_F_BEARER_CAP) || speech_ver < 0)
432 mncc_set_bearer(ms, -1, &mncc);
433 else if (data->bearer_cap.speech_ver[1] >= 0
435 mncc_set_bearer(ms, speech_ver, &mncc);
436 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
438 LOGP(DMNCC, LOGL_INFO, "Ring!\n");
440 LOGP(DMNCC, LOGL_INFO, "Knock!\n");
444 memset(&mncc, 0, sizeof(struct gsm_mncc));
445 mncc.callref = call->callref;
446 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
448 case MNCC_SETUP_COMPL_IND:
449 vty_notify(ms, NULL);
450 vty_notify(ms, "Call is connected\n");
451 LOGP(DMNCC, LOGL_INFO, "Call is connected\n");
454 vty_notify(ms, NULL);
455 vty_notify(ms, "Call is on hold\n");
456 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
460 vty_notify(ms, NULL);
461 vty_notify(ms, "Call hold was rejected\n");
462 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
464 case MNCC_RETRIEVE_CNF:
465 vty_notify(ms, NULL);
466 vty_notify(ms, "Call is retrieved\n");
467 LOGP(DMNCC, LOGL_INFO, "Call is retrieved\n");
470 case MNCC_RETRIEVE_REJ:
471 vty_notify(ms, NULL);
472 vty_notify(ms, "Call retrieve was rejected\n");
473 LOGP(DMNCC, LOGL_INFO, "Call retrieve was rejected\n");
475 case MNCC_FACILITY_IND:
476 LOGP(DMNCC, LOGL_INFO, "Facility info not displayed, "
479 case MNCC_START_DTMF_RSP:
480 case MNCC_START_DTMF_REJ:
481 case MNCC_STOP_DTMF_RSP:
482 dtmf_statemachine(call, data);
485 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
493 int mncc_call(struct osmocom_ms *ms, char *number)
495 struct gsm_call *call;
496 struct gsm_mncc setup;
498 llist_for_each_entry(call, &call_list, entry) {
500 vty_notify(ms, NULL);
501 vty_notify(ms, "Please put active call on hold "
503 LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
508 call = talloc_zero(l23_ctx, struct gsm_call);
512 call->callref = new_callref++;
514 llist_add_tail(&call->entry, &call_list);
516 memset(&setup, 0, sizeof(struct gsm_mncc));
517 setup.callref = call->callref;
519 if (!strncasecmp(number, "emerg", 5)) {
520 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
524 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
526 setup.fields |= MNCC_F_CALLED;
527 if (number[0] == '+') {
529 setup.called.type = 1; /* international */
531 setup.called.type = 0; /* auto/unknown - prefix must be
533 setup.called.plan = 1; /* ISDN */
534 strncpy(setup.called.number, number,
535 sizeof(setup.called.number) - 1);
537 /* bearer capability (mandatory) */
538 mncc_set_bearer(ms, -1, &setup);
539 if (ms->settings.clir)
541 else if (ms->settings.clip)
544 /* CC capabilities (optional) */
545 if (ms->settings.cc_dtmf) {
546 setup.fields |= MNCC_F_CCCAP;
547 setup.cccap.dtmf = 1;
551 return mncc_send(ms, MNCC_SETUP_REQ, &setup);
554 int mncc_hangup(struct osmocom_ms *ms)
556 struct gsm_call *call, *found = NULL;
557 struct gsm_mncc disc;
559 llist_for_each_entry(call, &call_list, entry) {
566 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
567 vty_notify(ms, NULL);
568 vty_notify(ms, "No active call\n");
572 memset(&disc, 0, sizeof(struct gsm_mncc));
573 disc.callref = found->callref;
574 mncc_set_cause(&disc, GSM48_CAUSE_LOC_USER,
575 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
576 return mncc_send(ms, (call->init) ? MNCC_REL_REQ : MNCC_DISC_REQ,
580 int mncc_answer(struct osmocom_ms *ms)
582 struct gsm_call *call, *alerting = NULL;
586 llist_for_each_entry(call, &call_list, entry) {
589 else if (!call->hold)
593 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
594 vty_notify(ms, NULL);
595 vty_notify(ms, "No alerting call\n");
599 LOGP(DMNCC, LOGL_INFO, "Answer but we have an active call\n");
600 vty_notify(ms, NULL);
601 vty_notify(ms, "Please put active call on hold first!\n");
607 memset(&rsp, 0, sizeof(struct gsm_mncc));
608 rsp.callref = alerting->callref;
609 return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
612 int mncc_hold(struct osmocom_ms *ms)
614 struct gsm_call *call, *found = NULL;
615 struct gsm_mncc hold;
617 llist_for_each_entry(call, &call_list, entry) {
624 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
625 vty_notify(ms, NULL);
626 vty_notify(ms, "No active call\n");
630 memset(&hold, 0, sizeof(struct gsm_mncc));
631 hold.callref = found->callref;
632 return mncc_send(ms, MNCC_HOLD_REQ, &hold);
635 int mncc_retrieve(struct osmocom_ms *ms, int number)
637 struct gsm_call *call;
638 struct gsm_mncc retr;
639 int holdnum = 0, active = 0, i = 0;
641 llist_for_each_entry(call, &call_list, entry) {
648 LOGP(DMNCC, LOGL_INFO, "Cannot retrieve during active call\n");
649 vty_notify(ms, NULL);
650 vty_notify(ms, "Hold active call first!\n");
654 vty_notify(ms, NULL);
655 vty_notify(ms, "No call on hold!\n");
658 if (holdnum > 1 && number <= 0) {
659 vty_notify(ms, NULL);
660 vty_notify(ms, "Select call 1..%d\n", holdnum);
663 if (holdnum == 1 && number <= 0)
665 if (number > holdnum) {
666 vty_notify(ms, NULL);
667 vty_notify(ms, "Given number %d out of range!\n", number);
668 vty_notify(ms, "Select call 1..%d\n", holdnum);
672 llist_for_each_entry(call, &call_list, entry) {
678 memset(&retr, 0, sizeof(struct gsm_mncc));
679 retr.callref = call->callref;
680 return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);
687 static int dtmf_statemachine(struct gsm_call *call, struct gsm_mncc *mncc)
689 struct osmocom_ms *ms = call->ms;
690 struct gsm_mncc dtmf;
692 switch (call->dtmf_state) {
696 if (!call->dtmf[call->dtmf_index]) {
697 LOGP(DMNCC, LOGL_INFO, "done with DTMF\n");
698 call->dtmf_state = DTMF_ST_IDLE;
701 memset(&dtmf, 0, sizeof(struct gsm_mncc));
702 dtmf.callref = call->callref;
703 dtmf.keypad = call->dtmf[call->dtmf_index++];
704 call->dtmf_state = DTMF_ST_START;
705 LOGP(DMNCC, LOGL_INFO, "start DTMF (keypad %c)\n",
707 return mncc_send(ms, MNCC_START_DTMF_REQ, &dtmf);
709 if (mncc->msg_type != MNCC_START_DTMF_RSP) {
710 LOGP(DMNCC, LOGL_INFO, "DTMF was rejected\n");
713 start_dtmf_timer(call, 70);
714 call->dtmf_state = DTMF_ST_MARK;
715 LOGP(DMNCC, LOGL_INFO, "DTMF is on\n");
718 memset(&dtmf, 0, sizeof(struct gsm_mncc));
719 dtmf.callref = call->callref;
720 call->dtmf_state = DTMF_ST_STOP;
721 LOGP(DMNCC, LOGL_INFO, "stop DTMF\n");
722 return mncc_send(ms, MNCC_STOP_DTMF_REQ, &dtmf);
724 start_dtmf_timer(call, 120);
725 call->dtmf_state = DTMF_ST_SPACE;
726 LOGP(DMNCC, LOGL_INFO, "DTMF is off\n");
733 static void timeout_dtmf(void *arg)
735 struct gsm_call *call = arg;
737 LOGP(DCC, LOGL_INFO, "DTMF timer has fired\n");
738 dtmf_statemachine(call, NULL);
741 int mncc_dtmf(struct osmocom_ms *ms, char *dtmf)
743 struct gsm_call *call, *found = NULL;
745 llist_for_each_entry(call, &call_list, entry) {
752 LOGP(DMNCC, LOGL_INFO, "No active call to send DTMF\n");
753 vty_notify(ms, NULL);
754 vty_notify(ms, "No active call\n");
758 if (call->dtmf_state != DTMF_ST_IDLE) {
759 LOGP(DMNCC, LOGL_INFO, "sending DTMF already\n");
763 call->dtmf_index = 0;
764 strncpy(call->dtmf, dtmf, sizeof(call->dtmf) - 1);
765 return dtmf_statemachine(call, NULL);