[mobile] Adding security warning, if default IMEI is not changed
[osmocom-bb.git] / src / host / layer23 / src / mobile / mnccms.c
1 /*
2  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
3  *
4  * All Rights Reserved
5  *
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.
10  *
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.
15  *
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.
19  *
20  */
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <osmocom/core/talloc.h>
29
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>
34
35 void *l23_ctx;
36 static uint32_t new_callref = 1;
37 static LLIST_HEAD(call_list);
38
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);
42 int mncc_answer(struct osmocom_ms *ms);
43
44 /*
45  * support functions
46  */
47
48 /* DTMF timer */
49 static void start_dtmf_timer(struct gsm_call *call, uint16_t ms)
50 {
51         LOGP(DCC, LOGL_INFO, "starting DTMF timer %d ms\n", ms);
52         call->dtmf_timer.cb = timeout_dtmf;
53         call->dtmf_timer.data = call;
54         osmo_timer_schedule(&call->dtmf_timer, 0, ms * 1000);
55 }
56
57 static void stop_dtmf_timer(struct gsm_call *call)
58 {
59         if (osmo_timer_pending(&call->dtmf_timer)) {
60                 LOGP(DCC, LOGL_INFO, "stopping pending DTMF timer\n");
61                 osmo_timer_del(&call->dtmf_timer);
62         }
63 }
64
65 /* free call instance */
66 static void free_call(struct gsm_call *call)
67 {
68         stop_dtmf_timer(call);
69
70         llist_del(&call->entry);
71         DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
72         talloc_free(call);
73 }
74
75
76 struct gsm_call *get_call_ref(uint32_t callref)
77 {
78         struct gsm_call *callt;
79
80         llist_for_each_entry(callt, &call_list, entry) {
81                 if (callt->callref == callref)
82                         return callt;
83         }
84         return NULL;
85 }
86
87 static int8_t mncc_get_bearer(struct gsm_settings *set, uint8_t speech_ver)
88 {
89         switch (speech_ver) {
90         case 4:
91                 if (set->full_v3)
92                         LOGP(DMNCC, LOGL_INFO, " net suggests full rate v3\n");
93                 else {
94                         LOGP(DMNCC, LOGL_INFO, " full rate v3 not supported\n");
95                         speech_ver = -1;
96                 }
97                 break;
98         case 2:
99                 if (set->full_v2)
100                         LOGP(DMNCC, LOGL_INFO, " net suggests full rate v2\n");
101                 else {
102                         LOGP(DMNCC, LOGL_INFO, " full rate v2 not supported\n");
103                         speech_ver = -1;
104                 }
105                 break;
106         case 0: /* mandatory */
107                 if (set->full_v1)
108                         LOGP(DMNCC, LOGL_INFO, " net suggests full rate v1\n");
109                 else {
110                         LOGP(DMNCC, LOGL_INFO, " full rate v1 not supported\n");
111                         speech_ver = -1;
112                 }
113                 break;
114         case 5:
115                 if (set->half_v3)
116                         LOGP(DMNCC, LOGL_INFO, " net suggests half rate v3\n");
117                 else {
118                         LOGP(DMNCC, LOGL_INFO, " half rate v3 not supported\n");
119                         speech_ver = -1;
120                 }
121                 break;
122         case 1:
123                 if (set->half_v1)
124                         LOGP(DMNCC, LOGL_INFO, " net suggests half rate v1\n");
125                 else {
126                         LOGP(DMNCC, LOGL_INFO, " half rate v1 not supported\n");
127                         speech_ver = -1;
128                 }
129                 break;
130         default:
131                 LOGP(DMNCC, LOGL_INFO, " net suggests unknown speech version "
132                         "%d\n", speech_ver);
133                 speech_ver = -1;
134         }
135
136         return speech_ver;
137 }
138
139 static void mncc_set_bearer(struct osmocom_ms *ms, int8_t speech_ver,
140         struct gsm_mncc *mncc)
141 {
142         struct gsm_settings *set = &ms->settings;
143         int i = 0;
144
145         mncc->fields |= MNCC_F_BEARER_CAP;
146         mncc->bearer_cap.coding = 0;
147         if (set->ch_cap == GSM_CAP_SDCCH_TCHF_TCHH
148          && (set->half_v1 || set->half_v3)) {
149                 mncc->bearer_cap.radio = 3;
150                 LOGP(DMNCC, LOGL_INFO, " support TCH/H also\n");
151         } else {
152                 mncc->bearer_cap.radio = 1;
153                 LOGP(DMNCC, LOGL_INFO, " support TCH/F only\n");
154         }
155         mncc->bearer_cap.speech_ctm = 0;
156         /* if no specific speech_ver is given */
157         if (speech_ver < 0) {
158                 /* if half rate is supported and prefered */
159                 if (set->half_v3 && set->half && set->half_prefer) {
160                         mncc->bearer_cap.speech_ver[i++] = 5;
161                         LOGP(DMNCC, LOGL_INFO, " support half rate v3\n");
162                 }
163                 if (set->half_v1 && set->half && set->half_prefer) {
164                         mncc->bearer_cap.speech_ver[i++] = 1;
165                         LOGP(DMNCC, LOGL_INFO, " support half rate v1\n");
166                 }
167                 /* if full rate is supported */
168                 if (set->full_v3) {
169                         mncc->bearer_cap.speech_ver[i++] = 4;
170                         LOGP(DMNCC, LOGL_INFO, " support full rate v3\n");
171                 }
172                 if (set->full_v2) {
173                         mncc->bearer_cap.speech_ver[i++] = 2;
174                         LOGP(DMNCC, LOGL_INFO, " support full rate v2\n");
175                 }
176                 if (set->full_v1) { /* mandatory, so it's always true */
177                         mncc->bearer_cap.speech_ver[i++] = 0;
178                         LOGP(DMNCC, LOGL_INFO, " support full rate v1\n");
179                 }
180                 /* if half rate is supported and not prefered */
181                 if (set->half_v3 && set->half && !set->half_prefer) {
182                         mncc->bearer_cap.speech_ver[i++] = 5;
183                         LOGP(DMNCC, LOGL_INFO, " support half rate v3\n");
184                 }
185                 if (set->half_v1 && set->half && !set->half_prefer) {
186                         mncc->bearer_cap.speech_ver[i++] = 1;
187                         LOGP(DMNCC, LOGL_INFO, " support half rate v1\n");
188                 }
189         /* if specific speech_ver is given (it must be supported) */
190         } else 
191                 mncc->bearer_cap.speech_ver[i++] = speech_ver;
192         mncc->bearer_cap.speech_ver[i] = -1; /* end of list */
193         mncc->bearer_cap.transfer = 0;
194         mncc->bearer_cap.mode = 0;
195 }
196
197 /*
198  * MNCCms dummy application
199  */
200
201 /* this is a minimal implementation as required by GSM 04.08 */
202 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg)
203 {
204         struct gsm_mncc *data = arg;
205         uint32_t callref = data->callref;
206         struct gsm_mncc rel;
207
208         if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
209                 return 0;
210
211         LOGP(DMNCC, LOGL_INFO, "Rejecting incoming call\n");
212
213         /* reject, as we don't support Calls */
214         memset(&rel, 0, sizeof(struct gsm_mncc));
215         rel.callref = callref;
216         mncc_set_cause(&rel, GSM48_CAUSE_LOC_USER,
217                 GSM48_CC_CAUSE_INCOMPAT_DEST);
218
219         return mncc_send(ms, MNCC_REL_REQ, &rel);
220 }
221
222 /*
223  * MNCCms basic call application
224  */
225
226 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
227 {
228         struct gsm_settings *set = &ms->settings;
229         struct gsm_mncc *data = arg;
230         struct gsm_call *call = get_call_ref(data->callref);
231         struct gsm_mncc mncc;
232         uint8_t cause;
233         int8_t  speech_ver = -1, speech_ver_half = -1, temp;
234         int first_call = 0;
235
236         /* call does not exist */
237         if (!call && msg_type != MNCC_SETUP_IND) {
238                 LOGP(DMNCC, LOGL_INFO, "Rejecting incoming call "
239                         "(callref %x)\n", data->callref);
240                 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
241                         return 0;
242                 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
243                 release:
244                 memset(&mncc, 0, sizeof(struct gsm_mncc));
245                 mncc.callref = data->callref;
246                 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_USER, cause);
247                 return mncc_send(ms, MNCC_REL_REQ, &mncc);
248         }
249
250         /* setup without call */
251         if (!call) {
252                 if (llist_empty(&call_list))
253                         first_call = 1;
254                 call = talloc_zero(l23_ctx, struct gsm_call);
255                 if (!call)
256                         return -ENOMEM;
257                 call->ms = ms;
258                 call->callref = data->callref;
259                 llist_add_tail(&call->entry, &call_list);
260         }
261
262         /* not in initiated state anymore */
263         call->init = 0;
264
265         switch (msg_type) {
266         case MNCC_DISC_IND:
267                 vty_notify(ms, NULL);
268                 switch (data->cause.value) {
269                 case GSM48_CC_CAUSE_UNASSIGNED_NR:
270                         vty_notify(ms, "Call: Number not assigned\n");
271                         break;
272                 case GSM48_CC_CAUSE_NO_ROUTE:
273                         vty_notify(ms, "Call: Destination unreachable\n");
274                         break;
275                 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
276                         vty_notify(ms, "Call: Remote hangs up\n");
277                         break;
278                 case GSM48_CC_CAUSE_USER_BUSY:
279                         vty_notify(ms, "Call: Remote busy\n");
280                         break;
281                 case GSM48_CC_CAUSE_USER_NOTRESPOND:
282                         vty_notify(ms, "Call: Remote not responding\n");
283                         break;
284                 case GSM48_CC_CAUSE_USER_ALERTING_NA:
285                         vty_notify(ms, "Call: Remote not answering\n");
286                         break;
287                 case GSM48_CC_CAUSE_CALL_REJECTED:
288                         vty_notify(ms, "Call has been rejected\n");
289                         break;
290                 case GSM48_CC_CAUSE_NUMBER_CHANGED:
291                         vty_notify(ms, "Call: Number changed\n");
292                         break;
293                 case GSM48_CC_CAUSE_PRE_EMPTION:
294                         vty_notify(ms, "Call: Cleared due to pre-emption\n");
295                         break;
296                 case GSM48_CC_CAUSE_DEST_OOO:
297                         vty_notify(ms, "Call: Remote out of order\n");
298                         break;
299                 case GSM48_CC_CAUSE_INV_NR_FORMAT:
300                         vty_notify(ms, "Call: Number invalid or imcomplete\n");
301                         break;
302                 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
303                         vty_notify(ms, "Call: No channel available\n");
304                         break;
305                 case GSM48_CC_CAUSE_NETWORK_OOO:
306                         vty_notify(ms, "Call: Network out of order\n");
307                         break;
308                 case GSM48_CC_CAUSE_TEMP_FAILURE:
309                         vty_notify(ms, "Call: Temporary failure\n");
310                         break;
311                 case GSM48_CC_CAUSE_SWITCH_CONG:
312                         vty_notify(ms, "Congestion\n");
313                         break;
314                 default:
315                         vty_notify(ms, "Call has been disconnected "
316                                 "(clear cause %d)\n", data->cause.value);
317                 }
318                 LOGP(DMNCC, LOGL_INFO, "Call has been disconnected "
319                         "(cause %d)\n", data->cause.value);
320                 if ((data->fields & MNCC_F_PROGRESS)
321                  && data->progress.descr == 8) {
322                         vty_notify(ms, "Please hang up!\n");
323                         break;
324                 }
325                 free_call(call);
326                 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
327                 goto release;
328         case MNCC_REL_IND:
329         case MNCC_REL_CNF:
330                 vty_notify(ms, NULL);
331                 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
332                         vty_notify(ms, "Call has been rejected\n");
333                 else
334                         vty_notify(ms, "Call has been released\n");
335                 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
336                         data->cause.value);
337                 free_call(call);
338                 break;
339         case MNCC_CALL_PROC_IND:
340                 vty_notify(ms, NULL);
341                 vty_notify(ms, "Call is proceeding\n");
342                 LOGP(DMNCC, LOGL_INFO, "Call is proceeding\n");
343                 if ((data->fields & MNCC_F_BEARER_CAP)
344                  && data->bearer_cap.speech_ver[0] >= 0) {
345                         mncc_get_bearer(set, data->bearer_cap.speech_ver[0]);
346                 }
347                 break;
348         case MNCC_ALERT_IND:
349                 vty_notify(ms, NULL);
350                 vty_notify(ms, "Call is alerting\n");
351                 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
352                 break;
353         case MNCC_SETUP_CNF:
354                 vty_notify(ms, NULL);
355                 vty_notify(ms, "Call is answered\n");
356                 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
357                 break;
358         case MNCC_SETUP_IND:
359                 vty_notify(ms, NULL);
360                 if (!first_call && !ms->settings.cw) {
361                         vty_notify(ms, "Incoming call rejected while busy\n");
362                         LOGP(DMNCC, LOGL_INFO, "Incoming call but busy\n");
363                         cause = GSM48_CC_CAUSE_USER_BUSY;
364                         goto release;
365                 }
366                 /* select first supported speech_ver */
367                 if ((data->fields & MNCC_F_BEARER_CAP)) {
368                         int i;
369
370                         for (i = 0; data->bearer_cap.speech_ver[i] >= 0; i++) {
371
372                                 temp = mncc_get_bearer(set,
373                                         data->bearer_cap.speech_ver[i]);
374                                 if (temp < 0)
375                                         continue;
376                                 if (temp == 5 || temp == 1) { /* half */
377                                         /* only the first half rate */
378                                         if (speech_ver_half < 0)
379                                                 speech_ver_half = temp;
380                                 } else {
381                                         /* only the first full rate */
382                                         if (speech_ver < 0)
383                                                 speech_ver = temp;
384                                 }
385                         }
386                         /* half and full given */
387                         if (speech_ver_half >= 0 && speech_ver >= 0) {
388                                 if (set->half_prefer) {
389                                         LOGP(DMNCC, LOGL_INFO, " both supported"
390                                                 " codec rates are given, using "
391                                                 "preferred half rate\n");
392                                         speech_ver = speech_ver_half;
393                                 } else
394                                         LOGP(DMNCC, LOGL_INFO, " both supported"
395                                                 " codec rates are given, using "
396                                                 "preferred full rate\n");
397                         } else if (speech_ver_half < 0 && speech_ver < 0) {
398                                 LOGP(DMNCC, LOGL_INFO, " no supported codec "
399                                         "rate is given\n");
400                         /* only half rate is given, use it */
401                         } else if (speech_ver_half >= 0) {
402                                 LOGP(DMNCC, LOGL_INFO, " only supported half "
403                                         "rate codec is given, using it\n");
404                                 speech_ver = speech_ver_half;
405                         /* only full rate is given, use it */
406                         } else {
407                                 LOGP(DMNCC, LOGL_INFO, " only supported full "
408                                         "rate codec is given, using it\n");
409                         }
410                 }
411                 /* presentation allowed if present == 0 */
412                 if (data->calling.present || !data->calling.number[0])
413                         vty_notify(ms, "Incoming call (anonymous)\n");
414                 else if (data->calling.type == 1)
415                         vty_notify(ms, "Incoming call (from +%s)\n",
416                                 data->calling.number);
417                 else if (data->calling.type == 2)
418                         vty_notify(ms, "Incoming call (from 0-%s)\n",
419                                 data->calling.number);
420                 else
421                         vty_notify(ms, "Incoming call (from %s)\n",
422                                 data->calling.number);
423                 LOGP(DMNCC, LOGL_INFO, "Incoming call (from %s callref %x)\n",
424                         data->calling.number, call->callref);
425                 memset(&mncc, 0, sizeof(struct gsm_mncc));
426                 mncc.callref = call->callref;
427                 /* only include bearer cap, if not given in setup
428                  * or if multiple codecs are given
429                  * or if not only full rate
430                  * or if given codec is unimplemented
431                  */
432                 if (!(data->fields & MNCC_F_BEARER_CAP) || speech_ver < 0)
433                         mncc_set_bearer(ms, -1, &mncc);
434                 else if (data->bearer_cap.speech_ver[1] >= 0
435                       || speech_ver != 0)
436                         mncc_set_bearer(ms, speech_ver, &mncc);
437                 /* CC capabilities (optional) */
438                 if (ms->settings.cc_dtmf) {
439                         mncc.fields |= MNCC_F_CCCAP;
440                         mncc.cccap.dtmf = 1;
441                 }
442                 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
443                 if (first_call)
444                         LOGP(DMNCC, LOGL_INFO, "Ring!\n");
445                 else {
446                         LOGP(DMNCC, LOGL_INFO, "Knock!\n");
447                         call->hold = 1;
448                 }
449                 call->ring = 1;
450                 memset(&mncc, 0, sizeof(struct gsm_mncc));
451                 mncc.callref = call->callref;
452                 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
453                 if (ms->settings.auto_answer) {
454                         LOGP(DMNCC, LOGL_INFO, "Auto-answering call\n");
455                         mncc_answer(ms);
456                 }
457                 break;
458         case MNCC_SETUP_COMPL_IND:
459                 vty_notify(ms, NULL);
460                 vty_notify(ms, "Call is connected\n");
461                 LOGP(DMNCC, LOGL_INFO, "Call is connected\n");
462                 break;
463         case MNCC_HOLD_CNF:
464                 vty_notify(ms, NULL);
465                 vty_notify(ms, "Call is on hold\n");
466                 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
467                 call->hold = 1;
468                 break;
469         case MNCC_HOLD_REJ:
470                 vty_notify(ms, NULL);
471                 vty_notify(ms, "Call hold was rejected\n");
472                 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
473                 break;
474         case MNCC_RETRIEVE_CNF:
475                 vty_notify(ms, NULL);
476                 vty_notify(ms, "Call is retrieved\n");
477                 LOGP(DMNCC, LOGL_INFO, "Call is retrieved\n");
478                 call->hold = 0;
479                 break;
480         case MNCC_RETRIEVE_REJ:
481                 vty_notify(ms, NULL);
482                 vty_notify(ms, "Call retrieve was rejected\n");
483                 LOGP(DMNCC, LOGL_INFO, "Call retrieve was rejected\n");
484                 break;
485         case MNCC_FACILITY_IND:
486                 LOGP(DMNCC, LOGL_INFO, "Facility info not displayed, "
487                         "unsupported\n");
488                 break;
489         case MNCC_START_DTMF_RSP:
490         case MNCC_START_DTMF_REJ:
491         case MNCC_STOP_DTMF_RSP:
492                 dtmf_statemachine(call, data);
493                 break;
494         default:
495                 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
496                         msg_type);
497                 return -EINVAL;
498         }
499
500         return 0;
501 }
502
503 int mncc_call(struct osmocom_ms *ms, char *number)
504 {
505         struct gsm_call *call;
506         struct gsm_mncc setup;
507
508         llist_for_each_entry(call, &call_list, entry) {
509                 if (!call->hold) {
510                         vty_notify(ms, NULL);
511                         vty_notify(ms, "Please put active call on hold "
512                                 "first!\n");
513                         LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
514                         return -EBUSY;
515                 }
516         }
517
518         call = talloc_zero(l23_ctx, struct gsm_call);
519         if (!call)
520                 return -ENOMEM;
521         call->ms = ms;
522         call->callref = new_callref++;
523         call->init = 1;
524         llist_add_tail(&call->entry, &call_list);
525
526         memset(&setup, 0, sizeof(struct gsm_mncc));
527         setup.callref = call->callref;
528
529         if (!strncasecmp(number, "emerg", 5)) {
530                 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
531                 /* emergency */
532                 setup.emergency = 1;
533         } else {
534                 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
535                 /* called number */
536                 setup.fields |= MNCC_F_CALLED;
537                 if (number[0] == '+') {
538                         number++;
539                         setup.called.type = 1; /* international */
540                 } else
541                         setup.called.type = 0; /* auto/unknown - prefix must be
542                                                   used */
543                 setup.called.plan = 1; /* ISDN */
544                 strncpy(setup.called.number, number,
545                         sizeof(setup.called.number) - 1);
546                 
547                 /* bearer capability (mandatory) */
548                 mncc_set_bearer(ms, -1, &setup);
549                 if (ms->settings.clir)
550                         setup.clir.sup = 1;
551                 else if (ms->settings.clip)
552                         setup.clir.inv = 1;
553
554                 /* CC capabilities (optional) */
555                 if (ms->settings.cc_dtmf) {
556                         setup.fields |= MNCC_F_CCCAP;
557                         setup.cccap.dtmf = 1;
558                 }
559         }
560
561         return mncc_send(ms, MNCC_SETUP_REQ, &setup);
562 }
563
564 int mncc_hangup(struct osmocom_ms *ms)
565 {
566         struct gsm_call *call, *found = NULL;
567         struct gsm_mncc disc;
568
569         llist_for_each_entry(call, &call_list, entry) {
570                 if (!call->hold) {
571                         found = call;
572                         break;
573                 }
574         }
575         if (!found) {
576                 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
577                 vty_notify(ms, NULL);
578                 vty_notify(ms, "No active call\n");
579                 return -EINVAL;
580         }
581
582         memset(&disc, 0, sizeof(struct gsm_mncc));
583         disc.callref = found->callref;
584         mncc_set_cause(&disc, GSM48_CAUSE_LOC_USER,
585                 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
586         return mncc_send(ms, (call->init) ? MNCC_REL_REQ : MNCC_DISC_REQ,
587                 &disc);
588 }
589
590 int mncc_answer(struct osmocom_ms *ms)
591 {
592         struct gsm_call *call, *alerting = NULL;
593         struct gsm_mncc rsp;
594         int active = 0;
595
596         llist_for_each_entry(call, &call_list, entry) {
597                 if (call->ring)
598                         alerting = call;
599                 else if (!call->hold)
600                         active = 1;
601         }
602         if (!alerting) {
603                 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
604                 vty_notify(ms, NULL);
605                 vty_notify(ms, "No alerting call\n");
606                 return -EBUSY;
607         }
608         if (active) {
609                 LOGP(DMNCC, LOGL_INFO, "Answer but we have an active call\n");
610                 vty_notify(ms, NULL);
611                 vty_notify(ms, "Please put active call on hold first!\n");
612                 return -EBUSY;
613         }
614         alerting->ring = 0;
615         alerting->hold = 0;
616
617         memset(&rsp, 0, sizeof(struct gsm_mncc));
618         rsp.callref = alerting->callref;
619         return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
620 }
621
622 int mncc_hold(struct osmocom_ms *ms)
623 {
624         struct gsm_call *call, *found = NULL;
625         struct gsm_mncc hold;
626
627         llist_for_each_entry(call, &call_list, entry) {
628                 if (!call->hold) {
629                         found = call;
630                         break;
631                 }
632         }
633         if (!found) {
634                 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
635                 vty_notify(ms, NULL);
636                 vty_notify(ms, "No active call\n");
637                 return -EINVAL;
638         }
639
640         memset(&hold, 0, sizeof(struct gsm_mncc));
641         hold.callref = found->callref;
642         return mncc_send(ms, MNCC_HOLD_REQ, &hold);
643 }
644
645 int mncc_retrieve(struct osmocom_ms *ms, int number)
646 {
647         struct gsm_call *call;
648         struct gsm_mncc retr;
649         int holdnum = 0, active = 0, i = 0;
650
651         llist_for_each_entry(call, &call_list, entry) {
652                 if (call->hold)
653                         holdnum++;
654                 if (!call->hold)
655                         active = 1;
656         }
657         if (active) {
658                 LOGP(DMNCC, LOGL_INFO, "Cannot retrieve during active call\n");
659                 vty_notify(ms, NULL);
660                 vty_notify(ms, "Hold active call first!\n");
661                 return -EINVAL;
662         }
663         if (holdnum == 0) {
664                 vty_notify(ms, NULL);
665                 vty_notify(ms, "No call on hold!\n");
666                 return -EINVAL;
667         }
668         if (holdnum > 1 && number <= 0) {
669                 vty_notify(ms, NULL);
670                 vty_notify(ms, "Select call 1..%d\n", holdnum);
671                 return -EINVAL;
672         }
673         if (holdnum == 1 && number <= 0)
674                 number = 1;
675         if (number > holdnum) {
676                 vty_notify(ms, NULL);
677                 vty_notify(ms, "Given number %d out of range!\n", number);
678                 vty_notify(ms, "Select call 1..%d\n", holdnum);
679                 return -EINVAL;
680         }
681
682         llist_for_each_entry(call, &call_list, entry) {
683                 i++;
684                 if (i == number)
685                         break;
686         }
687
688         memset(&retr, 0, sizeof(struct gsm_mncc));
689         retr.callref = call->callref;
690         return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);
691 }
692
693 /*
694  * DTMF
695  */
696
697 static int dtmf_statemachine(struct gsm_call *call, struct gsm_mncc *mncc)
698 {
699         struct osmocom_ms *ms = call->ms;
700         struct gsm_mncc dtmf;
701
702         switch (call->dtmf_state) {
703         case DTMF_ST_SPACE:
704         case DTMF_ST_IDLE:
705                 /* end of string */
706                 if (!call->dtmf[call->dtmf_index]) {
707                         LOGP(DMNCC, LOGL_INFO, "done with DTMF\n");
708                         call->dtmf_state = DTMF_ST_IDLE;
709                         return -EOF;
710                 }
711                 memset(&dtmf, 0, sizeof(struct gsm_mncc));
712                 dtmf.callref = call->callref;
713                 dtmf.keypad = call->dtmf[call->dtmf_index++];
714                 call->dtmf_state = DTMF_ST_START;
715                 LOGP(DMNCC, LOGL_INFO, "start DTMF (keypad %c)\n",
716                         dtmf.keypad);
717                 return mncc_send(ms, MNCC_START_DTMF_REQ, &dtmf);
718         case DTMF_ST_START:
719                 if (mncc->msg_type != MNCC_START_DTMF_RSP) {
720                         LOGP(DMNCC, LOGL_INFO, "DTMF was rejected\n");
721                         return -ENOTSUP;
722                 }
723                 start_dtmf_timer(call, 70);
724                 call->dtmf_state = DTMF_ST_MARK;
725                 LOGP(DMNCC, LOGL_INFO, "DTMF is on\n");
726                 break;
727         case DTMF_ST_MARK:
728                 memset(&dtmf, 0, sizeof(struct gsm_mncc));
729                 dtmf.callref = call->callref;
730                 call->dtmf_state = DTMF_ST_STOP;
731                 LOGP(DMNCC, LOGL_INFO, "stop DTMF\n");
732                 return mncc_send(ms, MNCC_STOP_DTMF_REQ, &dtmf);
733         case DTMF_ST_STOP:
734                 start_dtmf_timer(call, 120);
735                 call->dtmf_state = DTMF_ST_SPACE;
736                 LOGP(DMNCC, LOGL_INFO, "DTMF is off\n");
737                 break;
738         }
739
740         return 0;
741 }
742
743 static void timeout_dtmf(void *arg)
744 {
745         struct gsm_call *call = arg;
746
747         LOGP(DCC, LOGL_INFO, "DTMF timer has fired\n");
748         dtmf_statemachine(call, NULL);
749 }
750
751 int mncc_dtmf(struct osmocom_ms *ms, char *dtmf)
752 {
753         struct gsm_call *call, *found = NULL;
754
755         llist_for_each_entry(call, &call_list, entry) {
756                 if (!call->hold) {
757                         found = call;
758                         break;
759                 }
760         }
761         if (!found) {
762                 LOGP(DMNCC, LOGL_INFO, "No active call to send DTMF\n");
763                 vty_notify(ms, NULL);
764                 vty_notify(ms, "No active call\n");
765                 return -EINVAL;
766         }
767
768         if (call->dtmf_state != DTMF_ST_IDLE) {
769                 LOGP(DMNCC, LOGL_INFO, "sending DTMF already\n");
770                 return -EINVAL;
771         }
772
773         call->dtmf_index = 0;
774         strncpy(call->dtmf, dtmf, sizeof(call->dtmf) - 1);
775         return dtmf_statemachine(call, NULL);
776 }
777