[layer23] Fixed call answer/reject at mnccms.c
[osmocom-bb.git] / src / host / layer23 / src / 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/logging.h>
31 #include <osmocom/osmocom_data.h>
32 #include <osmocom/mncc.h>
33 #include <osmocom/vty.h>
34
35 void *l23_ctx;
36 static int new_callref = 1;
37 static LLIST_HEAD(call_list);
38
39 /*
40  * support functions
41  */
42
43 void mncc_set_cause(struct gsm_mncc *data, int loc, int val);
44
45 static void free_call(struct gsm_call *call)
46 {
47         llist_del(&call->entry);
48         DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
49         talloc_free(call);
50 }
51
52
53 struct gsm_call *get_call_ref(uint32_t callref)
54 {
55         struct gsm_call *callt;
56
57         llist_for_each_entry(callt, &call_list, entry) {
58                 if (callt->callref == callref)
59                         return callt;
60         }
61         return NULL;
62 }
63
64 /*
65  * MNCCms dummy application
66  */
67
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)
70 {
71         struct gsm_mncc *data = arg;
72         uint32_t callref = data->callref;
73         struct gsm_mncc rel;
74
75         if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
76                 return 0;
77
78         LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
79
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);
85
86         return mncc_send(ms, MNCC_REL_REQ, &rel);
87 }
88
89 /*
90  * MNCCms basic call application
91  */
92
93 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
94 {
95         struct gsm_mncc *data = arg;
96         struct gsm_call *call = get_call_ref(data->callref);
97         struct gsm_mncc mncc;
98         uint8_t cause;
99         int first_call = 0;
100
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)
106                         return 0;
107                 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
108                 release:
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);
113         }
114
115         /* setup without call */
116         if (!call) {
117                 if (llist_empty(&call_list))
118                         first_call = 1;
119                 call = talloc_zero(l23_ctx, struct gsm_call);
120                 if (!call)
121                         return -ENOMEM;
122                 call->callref = new_callref++;
123                 llist_add_tail(&call->entry, &call_list);
124         }
125
126         switch (msg_type) {
127         case MNCC_DISC_IND:
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");
132                         break;
133                 case GSM48_CC_CAUSE_NO_ROUTE:
134                         vty_notify(ms, "Call: Destination unreachable\n");
135                         break;
136                 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
137                         vty_notify(ms, "Call: Remote hangs up\n");
138                         break;
139                 case GSM48_CC_CAUSE_USER_BUSY:
140                         vty_notify(ms, "Call: Remote busy\n");
141                         break;
142                 case GSM48_CC_CAUSE_USER_NOTRESPOND:
143                         vty_notify(ms, "Call: Remote not responding\n");
144                         break;
145                 case GSM48_CC_CAUSE_USER_ALERTING_NA:
146                         vty_notify(ms, "Call: Remote not answering\n");
147                         break;
148                 case GSM48_CC_CAUSE_CALL_REJECTED:
149                         vty_notify(ms, "Call has been rejected\n");
150                         break;
151                 case GSM48_CC_CAUSE_NUMBER_CHANGED:
152                         vty_notify(ms, "Call: Number changed\n");
153                         break;
154                 case GSM48_CC_CAUSE_PRE_EMPTION:
155                         vty_notify(ms, "Call: Cleared due to pre-emption\n");
156                         break;
157                 case GSM48_CC_CAUSE_DEST_OOO:
158                         vty_notify(ms, "Call: Remote out of order\n");
159                         break;
160                 case GSM48_CC_CAUSE_INV_NR_FORMAT:
161                         vty_notify(ms, "Call: Number invalid or imcomplete\n");
162                         break;
163                 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
164                         vty_notify(ms, "Call: No channel available\n");
165                         break;
166                 case GSM48_CC_CAUSE_NETWORK_OOO:
167                         vty_notify(ms, "Call: Network out of order\n");
168                         break;
169                 case GSM48_CC_CAUSE_TEMP_FAILURE:
170                         vty_notify(ms, "Call: Temporary failure\n");
171                         break;
172                 case GSM48_CC_CAUSE_SWITCH_CONG:
173                         vty_notify(ms, "Congestion\n");
174                         break;
175                 default:
176                         vty_notify(ms, "Call has been disconnected\n");
177                 }
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");
183                         break;
184                 }
185                 free_call(call);
186                 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
187                 goto release;
188         case MNCC_REL_IND:
189         case MNCC_REL_CNF:
190                 vty_notify(ms, NULL);
191                 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
192                         vty_notify(ms, "Call has been rejected\n");
193                 else
194                         vty_notify(ms, "Call has been released\n");
195                 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
196                         data->cause.value);
197                 free_call(call);
198                 break;
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");
203                 break;
204         case MNCC_ALERT_IND:
205                 vty_notify(ms, NULL);
206                 vty_notify(ms, "Call is aleriting\n");
207                 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
208                 break;
209         case MNCC_SETUP_CNF:
210                 vty_notify(ms, NULL);
211                 vty_notify(ms, "Call is answered\n");
212                 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
213                 break;
214         case MNCC_SETUP_IND:
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;
220                         goto release;
221                 }
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);
231                 else
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);
238                 if (first_call)
239                         LOGP(DMNCC, LOGL_INFO, "Ring!\n");
240                 else {
241                         LOGP(DMNCC, LOGL_INFO, "Knock!\n");
242                         call->hold = 1;
243                 }
244                 call->ring = 1;
245                 memset(&mncc, 0, sizeof(struct gsm_mncc));
246                 mncc.callref = call->callref;
247                 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
248                 break;
249         case MNCC_HOLD_CNF:
250                 vty_notify(ms, NULL);
251                 vty_notify(ms, "Call is on hold\n");
252                 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
253                 call->hold = 1;
254                 break;
255         case MNCC_HOLD_REJ:
256                 vty_notify(ms, NULL);
257                 vty_notify(ms, "Call hold was rejected\n");
258                 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
259                 break;
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");
264                 call->hold = 0;
265                 break;
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");
270                 break;
271         default:
272                 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
273                         msg_type);
274                 return -EINVAL;
275         }
276
277         return 0;
278 }
279
280 int mncc_call(struct osmocom_ms *ms, char *number)
281 {
282         struct gsm_call *call;
283         struct gsm_mncc setup;
284
285         llist_for_each_entry(call, &call_list, entry) {
286                 if (!call->hold) {
287                         vty_notify(ms, NULL);
288                         vty_notify(ms, "Please put active call on hold "
289                                 "first!\n");
290                         LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
291                         return -EBUSY;
292                 }
293         }
294
295         call = talloc_zero(l23_ctx, struct gsm_call);
296         if (!call)
297                 return -ENOMEM;
298         call->callref = new_callref++;
299         llist_add_tail(&call->entry, &call_list);
300
301         memset(&setup, 0, sizeof(struct gsm_mncc));
302         setup.callref = call->callref;
303
304         if (!strncasecmp(number, "emerg", 5)) {
305                 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
306                 /* emergency */
307                 setup.emergency = 1;
308         } else {
309                 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
310                 /* called number */
311                 setup.fields |= MNCC_F_CALLED;
312                 strncpy(setup.called.number, number,
313                         sizeof(setup.called.number) - 1);
314                 
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)
325                         setup.clir.sup = 1;
326                 else if (ms->settings.clip)
327                         setup.clir.inv = 1;
328         }
329
330         return mncc_send(ms, MNCC_SETUP_REQ, &setup);
331 }
332
333 int mncc_hangup(struct osmocom_ms *ms)
334 {
335         struct gsm_call *call, *found = NULL;
336         struct gsm_mncc disc;
337
338         llist_for_each_entry(call, &call_list, entry) {
339                 if (!call->hold) {
340                         found = call;
341                         break;
342                 }
343         }
344         if (!found) {
345                 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
346                 vty_notify(ms, NULL);
347                 vty_notify(ms, "No active call\n");
348                 return -EINVAL;
349         }
350
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);
356 }
357
358 int mncc_answer(struct osmocom_ms *ms)
359 {
360         struct gsm_call *call, *alerting = NULL;
361         struct gsm_mncc rsp;
362         int active = 0;
363
364         llist_for_each_entry(call, &call_list, entry) {
365                 if (call->ring)
366                         alerting = call;
367                 else if (!call->hold)
368                         active = 1;
369         }
370         if (!alerting) {
371                 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
372                 vty_notify(ms, NULL);
373                 vty_notify(ms, "No alerting call\n");
374                 return -EBUSY;
375         }
376         if (active) {
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");
380                 return -EBUSY;
381         }
382         alerting->ring = 0;
383         alerting->hold = 0;
384
385         memset(&rsp, 0, sizeof(struct gsm_mncc));
386         rsp.callref = alerting->callref;
387         return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
388 }
389
390 int mncc_hold(struct osmocom_ms *ms)
391 {
392         struct gsm_call *call, *found = NULL;
393         struct gsm_mncc hold;
394
395         llist_for_each_entry(call, &call_list, entry) {
396                 if (!call->hold) {
397                         found = call;
398                         break;
399                 }
400         }
401         if (!found) {
402                 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
403                 vty_notify(ms, NULL);
404                 vty_notify(ms, "No active call\n");
405                 return -EINVAL;
406         }
407
408         memset(&hold, 0, sizeof(struct gsm_mncc));
409         hold.callref = found->callref;
410         return mncc_send(ms, MNCC_HOLD_REQ, &hold);
411 }
412
413 int mncc_retrieve(struct osmocom_ms *ms, int number)
414 {
415         struct gsm_call *call;
416         struct gsm_mncc retr;
417         int holdnum = 0, active = 0, i = 0;
418
419         llist_for_each_entry(call, &call_list, entry) {
420                 if (call->hold)
421                         holdnum++;
422                 if (!call->hold)
423                         active = 1;
424         }
425         if (active) {
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");
429                 return -EINVAL;
430         }
431         if (holdnum == 0) {
432                 vty_notify(ms, NULL);
433                 vty_notify(ms, "No call on hold!\n");
434                 return -EINVAL;
435         }
436         if (holdnum > 1 && number <= 0) {
437                 vty_notify(ms, NULL);
438                 vty_notify(ms, "Select call 1..%d\n", holdnum);
439                 return -EINVAL;
440         }
441         if (holdnum == 1 && number <= 0)
442                 number = 1;
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);
447                 return -EINVAL;
448         }
449
450         llist_for_each_entry(call, &call_list, entry) {
451                 i++;
452                 if (i == number)
453                         break;
454         }
455
456         memset(&retr, 0, sizeof(struct gsm_mncc));
457         retr.callref = call->callref;
458         return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);
459 }
460
461
462
463