4e44bf9769738334160ebae9028f61740f511600
[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 <osmocore/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
43 /*
44  * support functions
45  */
46
47 /* DTMF timer */
48 static void start_dtmf_timer(struct gsm_call *call, uint16_t ms)
49 {
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);
54 }
55
56 static void stop_dtmf_timer(struct gsm_call *call)
57 {
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);
61         }
62 }
63
64 /* free call instance */
65 static void free_call(struct gsm_call *call)
66 {
67         stop_dtmf_timer(call);
68
69         llist_del(&call->entry);
70         DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
71         talloc_free(call);
72 }
73
74
75 struct gsm_call *get_call_ref(uint32_t callref)
76 {
77         struct gsm_call *callt;
78
79         llist_for_each_entry(callt, &call_list, entry) {
80                 if (callt->callref == callref)
81                         return callt;
82         }
83         return NULL;
84 }
85
86 static int8_t mncc_get_bearer(struct gsm_settings *set, uint8_t speech_ver)
87 {
88         switch (speech_ver) {
89         case 4:
90                 if (set->full_v3)
91                         LOGP(DMNCC, LOGL_INFO, " net suggests full rate v3\n");
92                 else {
93                         LOGP(DMNCC, LOGL_INFO, " full rate v3 not supported\n");
94                         speech_ver = -1;
95                 }
96                 break;
97         case 2:
98                 if (set->full_v2)
99                         LOGP(DMNCC, LOGL_INFO, " net suggests full rate v2\n");
100                 else {
101                         LOGP(DMNCC, LOGL_INFO, " full rate v2 not supported\n");
102                         speech_ver = -1;
103                 }
104                 break;
105         case 0: /* mandatory */
106                 if (set->full_v1)
107                         LOGP(DMNCC, LOGL_INFO, " net suggests full rate v1\n");
108                 else {
109                         LOGP(DMNCC, LOGL_INFO, " full rate v1 not supported\n");
110                         speech_ver = -1;
111                 }
112                 break;
113         case 5:
114                 if (set->half_v3)
115                         LOGP(DMNCC, LOGL_INFO, " net suggests half rate v3\n");
116                 else {
117                         LOGP(DMNCC, LOGL_INFO, " half rate v3 not supported\n");
118                         speech_ver = -1;
119                 }
120                 break;
121         case 1:
122                 if (set->half_v1)
123                         LOGP(DMNCC, LOGL_INFO, " net suggests half rate v1\n");
124                 else {
125                         LOGP(DMNCC, LOGL_INFO, " half rate v1 not supported\n");
126                         speech_ver = -1;
127                 }
128                 break;
129         default:
130                 LOGP(DMNCC, LOGL_INFO, " net suggests unknown speech version "
131                         "%d\n", speech_ver);
132                 speech_ver = -1;
133         }
134
135         return speech_ver;
136 }
137
138 static void mncc_set_bearer(struct osmocom_ms *ms, int8_t speech_ver,
139         struct gsm_mncc *mncc)
140 {
141         struct gsm_settings *set = &ms->settings;
142         int i = 0;
143
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");
150         } else {
151                 mncc->bearer_cap.radio = 1;
152                 LOGP(DMNCC, LOGL_INFO, " support TCH/F only\n");
153         }
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");
161                 }
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");
165                 }
166                 /* if full rate is supported */
167                 if (set->full_v3) {
168                         mncc->bearer_cap.speech_ver[i++] = 4;
169                         LOGP(DMNCC, LOGL_INFO, " support full rate v3\n");
170                 }
171                 if (set->full_v2) {
172                         mncc->bearer_cap.speech_ver[i++] = 2;
173                         LOGP(DMNCC, LOGL_INFO, " support full rate v2\n");
174                 }
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");
178                 }
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");
183                 }
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");
187                 }
188         /* if specific speech_ver is given (it must be supported) */
189         } else 
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;
194 }
195
196 /*
197  * MNCCms dummy application
198  */
199
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)
202 {
203         struct gsm_mncc *data = arg;
204         uint32_t callref = data->callref;
205         struct gsm_mncc rel;
206
207         if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
208                 return 0;
209
210         LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
211
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);
217
218         return mncc_send(ms, MNCC_REL_REQ, &rel);
219 }
220
221 /*
222  * MNCCms basic call application
223  */
224
225 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
226 {
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;
231         uint8_t cause;
232         int8_t  speech_ver = -1, speech_ver_half = -1, temp;
233         int first_call = 0;
234
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)
240                         return 0;
241                 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
242                 release:
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);
247         }
248
249         /* setup without call */
250         if (!call) {
251                 if (llist_empty(&call_list))
252                         first_call = 1;
253                 call = talloc_zero(l23_ctx, struct gsm_call);
254                 if (!call)
255                         return -ENOMEM;
256                 call->ms = ms;
257                 call->callref = data->callref;
258                 llist_add_tail(&call->entry, &call_list);
259         }
260
261         /* not in initiated state anymore */
262         call->init = 0;
263
264         switch (msg_type) {
265         case MNCC_DISC_IND:
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");
270                         break;
271                 case GSM48_CC_CAUSE_NO_ROUTE:
272                         vty_notify(ms, "Call: Destination unreachable\n");
273                         break;
274                 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
275                         vty_notify(ms, "Call: Remote hangs up\n");
276                         break;
277                 case GSM48_CC_CAUSE_USER_BUSY:
278                         vty_notify(ms, "Call: Remote busy\n");
279                         break;
280                 case GSM48_CC_CAUSE_USER_NOTRESPOND:
281                         vty_notify(ms, "Call: Remote not responding\n");
282                         break;
283                 case GSM48_CC_CAUSE_USER_ALERTING_NA:
284                         vty_notify(ms, "Call: Remote not answering\n");
285                         break;
286                 case GSM48_CC_CAUSE_CALL_REJECTED:
287                         vty_notify(ms, "Call has been rejected\n");
288                         break;
289                 case GSM48_CC_CAUSE_NUMBER_CHANGED:
290                         vty_notify(ms, "Call: Number changed\n");
291                         break;
292                 case GSM48_CC_CAUSE_PRE_EMPTION:
293                         vty_notify(ms, "Call: Cleared due to pre-emption\n");
294                         break;
295                 case GSM48_CC_CAUSE_DEST_OOO:
296                         vty_notify(ms, "Call: Remote out of order\n");
297                         break;
298                 case GSM48_CC_CAUSE_INV_NR_FORMAT:
299                         vty_notify(ms, "Call: Number invalid or imcomplete\n");
300                         break;
301                 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
302                         vty_notify(ms, "Call: No channel available\n");
303                         break;
304                 case GSM48_CC_CAUSE_NETWORK_OOO:
305                         vty_notify(ms, "Call: Network out of order\n");
306                         break;
307                 case GSM48_CC_CAUSE_TEMP_FAILURE:
308                         vty_notify(ms, "Call: Temporary failure\n");
309                         break;
310                 case GSM48_CC_CAUSE_SWITCH_CONG:
311                         vty_notify(ms, "Congestion\n");
312                         break;
313                 default:
314                         vty_notify(ms, "Call has been disconnected "
315                                 "(clear cause %d)\n", data->cause.value);
316                 }
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");
322                         break;
323                 }
324                 free_call(call);
325                 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
326                 goto release;
327         case MNCC_REL_IND:
328         case MNCC_REL_CNF:
329                 vty_notify(ms, NULL);
330                 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
331                         vty_notify(ms, "Call has been rejected\n");
332                 else
333                         vty_notify(ms, "Call has been released\n");
334                 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
335                         data->cause.value);
336                 free_call(call);
337                 break;
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]);
345                 }
346                 break;
347         case MNCC_ALERT_IND:
348                 vty_notify(ms, NULL);
349                 vty_notify(ms, "Call is aleriting\n");
350                 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
351                 break;
352         case MNCC_SETUP_CNF:
353                 vty_notify(ms, NULL);
354                 vty_notify(ms, "Call is answered\n");
355                 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
356                 break;
357         case MNCC_SETUP_IND:
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;
363                         goto release;
364                 }
365                 /* select first supported speech_ver */
366                 if ((data->fields & MNCC_F_BEARER_CAP)) {
367                         int i;
368
369                         for (i = 0; data->bearer_cap.speech_ver[i] >= 0; i++) {
370
371                                 temp = mncc_get_bearer(set,
372                                         data->bearer_cap.speech_ver[i]);
373                                 if (temp < 0)
374                                         continue;
375                                 if (temp == 5 || temp == 1) { /* half */
376                                         /* only the first half rate */
377                                         if (speech_ver_half < 0)
378                                                 speech_ver_half = temp;
379                                 } else {
380                                         /* only the first full rate */
381                                         if (speech_ver < 0)
382                                                 speech_ver = temp;
383                                 }
384                         }
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;
392                                 } else
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 "
398                                         "rate is given\n");
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 */
405                         } else {
406                                 LOGP(DMNCC, LOGL_INFO, " only supported full "
407                                         "rate codec is given, using it\n");
408                         }
409                 }
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);
419                 else
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
430                  */
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
434                       || speech_ver != 0)
435                         mncc_set_bearer(ms, speech_ver, &mncc);
436                 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
437                 if (first_call)
438                         LOGP(DMNCC, LOGL_INFO, "Ring!\n");
439                 else {
440                         LOGP(DMNCC, LOGL_INFO, "Knock!\n");
441                         call->hold = 1;
442                 }
443                 call->ring = 1;
444                 memset(&mncc, 0, sizeof(struct gsm_mncc));
445                 mncc.callref = call->callref;
446                 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
447                 break;
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");
452                 break;
453         case MNCC_HOLD_CNF:
454                 vty_notify(ms, NULL);
455                 vty_notify(ms, "Call is on hold\n");
456                 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
457                 call->hold = 1;
458                 break;
459         case MNCC_HOLD_REJ:
460                 vty_notify(ms, NULL);
461                 vty_notify(ms, "Call hold was rejected\n");
462                 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
463                 break;
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");
468                 call->hold = 0;
469                 break;
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");
474                 break;
475         case MNCC_FACILITY_IND:
476                 LOGP(DMNCC, LOGL_INFO, "Facility info not displayed, "
477                         "unsupported\n");
478                 break;
479         case MNCC_START_DTMF_RSP:
480         case MNCC_START_DTMF_REJ:
481         case MNCC_STOP_DTMF_RSP:
482                 dtmf_statemachine(call, data);
483                 break;
484         default:
485                 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
486                         msg_type);
487                 return -EINVAL;
488         }
489
490         return 0;
491 }
492
493 int mncc_call(struct osmocom_ms *ms, char *number)
494 {
495         struct gsm_call *call;
496         struct gsm_mncc setup;
497
498         llist_for_each_entry(call, &call_list, entry) {
499                 if (!call->hold) {
500                         vty_notify(ms, NULL);
501                         vty_notify(ms, "Please put active call on hold "
502                                 "first!\n");
503                         LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
504                         return -EBUSY;
505                 }
506         }
507
508         call = talloc_zero(l23_ctx, struct gsm_call);
509         if (!call)
510                 return -ENOMEM;
511         call->ms = ms;
512         call->callref = new_callref++;
513         call->init = 1;
514         llist_add_tail(&call->entry, &call_list);
515
516         memset(&setup, 0, sizeof(struct gsm_mncc));
517         setup.callref = call->callref;
518
519         if (!strncasecmp(number, "emerg", 5)) {
520                 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
521                 /* emergency */
522                 setup.emergency = 1;
523         } else {
524                 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
525                 /* called number */
526                 setup.fields |= MNCC_F_CALLED;
527                 if (number[0] == '+') {
528                         number++;
529                         setup.called.type = 1; /* international */
530                 } else
531                         setup.called.type = 0; /* auto/unknown - prefix must be
532                                                   used */
533                 setup.called.plan = 1; /* ISDN */
534                 strncpy(setup.called.number, number,
535                         sizeof(setup.called.number) - 1);
536                 
537                 /* bearer capability (mandatory) */
538                 mncc_set_bearer(ms, -1, &setup);
539                 if (ms->settings.clir)
540                         setup.clir.sup = 1;
541                 else if (ms->settings.clip)
542                         setup.clir.inv = 1;
543
544                 /* CC capabilities (optional) */
545                 if (ms->settings.cc_dtmf) {
546                         setup.fields |= MNCC_F_CCCAP;
547                         setup.cccap.dtmf = 1;
548                 }
549         }
550
551         return mncc_send(ms, MNCC_SETUP_REQ, &setup);
552 }
553
554 int mncc_hangup(struct osmocom_ms *ms)
555 {
556         struct gsm_call *call, *found = NULL;
557         struct gsm_mncc disc;
558
559         llist_for_each_entry(call, &call_list, entry) {
560                 if (!call->hold) {
561                         found = call;
562                         break;
563                 }
564         }
565         if (!found) {
566                 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
567                 vty_notify(ms, NULL);
568                 vty_notify(ms, "No active call\n");
569                 return -EINVAL;
570         }
571
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,
577                 &disc);
578 }
579
580 int mncc_answer(struct osmocom_ms *ms)
581 {
582         struct gsm_call *call, *alerting = NULL;
583         struct gsm_mncc rsp;
584         int active = 0;
585
586         llist_for_each_entry(call, &call_list, entry) {
587                 if (call->ring)
588                         alerting = call;
589                 else if (!call->hold)
590                         active = 1;
591         }
592         if (!alerting) {
593                 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
594                 vty_notify(ms, NULL);
595                 vty_notify(ms, "No alerting call\n");
596                 return -EBUSY;
597         }
598         if (active) {
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");
602                 return -EBUSY;
603         }
604         alerting->ring = 0;
605         alerting->hold = 0;
606
607         memset(&rsp, 0, sizeof(struct gsm_mncc));
608         rsp.callref = alerting->callref;
609         return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
610 }
611
612 int mncc_hold(struct osmocom_ms *ms)
613 {
614         struct gsm_call *call, *found = NULL;
615         struct gsm_mncc hold;
616
617         llist_for_each_entry(call, &call_list, entry) {
618                 if (!call->hold) {
619                         found = call;
620                         break;
621                 }
622         }
623         if (!found) {
624                 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
625                 vty_notify(ms, NULL);
626                 vty_notify(ms, "No active call\n");
627                 return -EINVAL;
628         }
629
630         memset(&hold, 0, sizeof(struct gsm_mncc));
631         hold.callref = found->callref;
632         return mncc_send(ms, MNCC_HOLD_REQ, &hold);
633 }
634
635 int mncc_retrieve(struct osmocom_ms *ms, int number)
636 {
637         struct gsm_call *call;
638         struct gsm_mncc retr;
639         int holdnum = 0, active = 0, i = 0;
640
641         llist_for_each_entry(call, &call_list, entry) {
642                 if (call->hold)
643                         holdnum++;
644                 if (!call->hold)
645                         active = 1;
646         }
647         if (active) {
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");
651                 return -EINVAL;
652         }
653         if (holdnum == 0) {
654                 vty_notify(ms, NULL);
655                 vty_notify(ms, "No call on hold!\n");
656                 return -EINVAL;
657         }
658         if (holdnum > 1 && number <= 0) {
659                 vty_notify(ms, NULL);
660                 vty_notify(ms, "Select call 1..%d\n", holdnum);
661                 return -EINVAL;
662         }
663         if (holdnum == 1 && number <= 0)
664                 number = 1;
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);
669                 return -EINVAL;
670         }
671
672         llist_for_each_entry(call, &call_list, entry) {
673                 i++;
674                 if (i == number)
675                         break;
676         }
677
678         memset(&retr, 0, sizeof(struct gsm_mncc));
679         retr.callref = call->callref;
680         return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);
681 }
682
683 /*
684  * DTMF
685  */
686
687 static int dtmf_statemachine(struct gsm_call *call, struct gsm_mncc *mncc)
688 {
689         struct osmocom_ms *ms = call->ms;
690         struct gsm_mncc dtmf;
691
692         switch (call->dtmf_state) {
693         case DTMF_ST_SPACE:
694         case DTMF_ST_IDLE:
695                 /* end of string */
696                 if (!call->dtmf[call->dtmf_index]) {
697                         LOGP(DMNCC, LOGL_INFO, "done with DTMF\n");
698                         call->dtmf_state = DTMF_ST_IDLE;
699                         return -EOF;
700                 }
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",
706                         dtmf.keypad);
707                 return mncc_send(ms, MNCC_START_DTMF_REQ, &dtmf);
708         case DTMF_ST_START:
709                 if (mncc->msg_type != MNCC_START_DTMF_RSP) {
710                         LOGP(DMNCC, LOGL_INFO, "DTMF was rejected\n");
711                         return -ENOTSUP;
712                 }
713                 start_dtmf_timer(call, 70);
714                 call->dtmf_state = DTMF_ST_MARK;
715                 LOGP(DMNCC, LOGL_INFO, "DTMF is on\n");
716                 break;
717         case DTMF_ST_MARK:
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);
723         case DTMF_ST_STOP:
724                 start_dtmf_timer(call, 120);
725                 call->dtmf_state = DTMF_ST_SPACE;
726                 LOGP(DMNCC, LOGL_INFO, "DTMF is off\n");
727                 break;
728         }
729
730         return 0;
731 }
732
733 static void timeout_dtmf(void *arg)
734 {
735         struct gsm_call *call = arg;
736
737         LOGP(DCC, LOGL_INFO, "DTMF timer has fired\n");
738         dtmf_statemachine(call, NULL);
739 }
740
741 int mncc_dtmf(struct osmocom_ms *ms, char *dtmf)
742 {
743         struct gsm_call *call, *found = NULL;
744
745         llist_for_each_entry(call, &call_list, entry) {
746                 if (!call->hold) {
747                         found = call;
748                         break;
749                 }
750         }
751         if (!found) {
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");
755                 return -EINVAL;
756         }
757
758         if (call->dtmf_state != DTMF_ST_IDLE) {
759                 LOGP(DMNCC, LOGL_INFO, "sending DTMF already\n");
760                 return -EINVAL;
761         }
762
763         call->dtmf_index = 0;
764         strncpy(call->dtmf, dtmf, sizeof(call->dtmf) - 1);
765         return dtmf_statemachine(call, NULL);
766 }
767