[layer23] Fixed call reference for incomming calls. (mobile terminated)
[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 uint32_t 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 %x)\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 = data->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 (callref %x)\n",
225                                 call->callref);
226                 else if (data->calling.type == 1)
227                         vty_notify(ms, "Incomming call from +%s (callref %x)\n",
228                                 data->calling.number, call->callref);
229                 else if (data->calling.type == 2)
230                         vty_notify(ms, "Incomming call from 0-%s (callref "
231                                 "%x)\n", data->calling.number, call->callref);
232                 else
233                         vty_notify(ms, "Incomming call from %s (callref %x)\n",
234                                 data->calling.number, call->callref);
235                 LOGP(DMNCC, LOGL_INFO, "Incomming call (callref %x)\n",
236                         call->callref);
237                 memset(&mncc, 0, sizeof(struct gsm_mncc));
238                 mncc.callref = call->callref;
239                 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
240                 if (first_call)
241                         LOGP(DMNCC, LOGL_INFO, "Ring!\n");
242                 else {
243                         LOGP(DMNCC, LOGL_INFO, "Knock!\n");
244                         call->hold = 1;
245                 }
246                 call->ring = 1;
247                 memset(&mncc, 0, sizeof(struct gsm_mncc));
248                 mncc.callref = call->callref;
249                 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
250                 break;
251         case MNCC_SETUP_COMPL_IND:
252                 vty_notify(ms, NULL);
253                 vty_notify(ms, "Call is connected\n");
254                 LOGP(DMNCC, LOGL_INFO, "Call is connected\n");
255                 break;
256         case MNCC_HOLD_CNF:
257                 vty_notify(ms, NULL);
258                 vty_notify(ms, "Call is on hold\n");
259                 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
260                 call->hold = 1;
261                 break;
262         case MNCC_HOLD_REJ:
263                 vty_notify(ms, NULL);
264                 vty_notify(ms, "Call hold was rejected\n");
265                 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
266                 break;
267         case MNCC_RETRIEVE_CNF:
268                 vty_notify(ms, NULL);
269                 vty_notify(ms, "Call is retrieved\n");
270                 LOGP(DMNCC, LOGL_INFO, "Call is retrieved\n");
271                 call->hold = 0;
272                 break;
273         case MNCC_RETRIEVE_REJ:
274                 vty_notify(ms, NULL);
275                 vty_notify(ms, "Call retrieve was rejected\n");
276                 LOGP(DMNCC, LOGL_INFO, "Call retrieve was rejected\n");
277                 break;
278         default:
279                 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
280                         msg_type);
281                 return -EINVAL;
282         }
283
284         return 0;
285 }
286
287 int mncc_call(struct osmocom_ms *ms, char *number)
288 {
289         struct gsm_call *call;
290         struct gsm_mncc setup;
291
292         llist_for_each_entry(call, &call_list, entry) {
293                 if (!call->hold) {
294                         vty_notify(ms, NULL);
295                         vty_notify(ms, "Please put active call on hold "
296                                 "first!\n");
297                         LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
298                         return -EBUSY;
299                 }
300         }
301
302         call = talloc_zero(l23_ctx, struct gsm_call);
303         if (!call)
304                 return -ENOMEM;
305         call->callref = new_callref++;
306         llist_add_tail(&call->entry, &call_list);
307
308         memset(&setup, 0, sizeof(struct gsm_mncc));
309         setup.callref = call->callref;
310
311         if (!strncasecmp(number, "emerg", 5)) {
312                 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
313                 /* emergency */
314                 setup.emergency = 1;
315         } else {
316                 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
317                 /* called number */
318                 setup.fields |= MNCC_F_CALLED;
319                 strncpy(setup.called.number, number,
320                         sizeof(setup.called.number) - 1);
321                 
322                 /* bearer capability (mandatory) */
323                 setup.fields |= MNCC_F_BEARER_CAP;
324                 setup.bearer_cap.coding = 0;
325                 setup.bearer_cap.radio = 1;
326                 setup.bearer_cap.speech_ctm = 0;
327                 setup.bearer_cap.speech_ver[0] = 0;
328                 setup.bearer_cap.speech_ver[1] = -1; /* end of list */
329                 setup.bearer_cap.transfer = 0;
330                 setup.bearer_cap.mode = 0;
331                 if (ms->settings.clir)
332                         setup.clir.sup = 1;
333                 else if (ms->settings.clip)
334                         setup.clir.inv = 1;
335         }
336
337         return mncc_send(ms, MNCC_SETUP_REQ, &setup);
338 }
339
340 int mncc_hangup(struct osmocom_ms *ms)
341 {
342         struct gsm_call *call, *found = NULL;
343         struct gsm_mncc disc;
344
345         llist_for_each_entry(call, &call_list, entry) {
346                 if (!call->hold) {
347                         found = call;
348                         break;
349                 }
350         }
351         if (!found) {
352                 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
353                 vty_notify(ms, NULL);
354                 vty_notify(ms, "No active call\n");
355                 return -EINVAL;
356         }
357
358         memset(&disc, 0, sizeof(struct gsm_mncc));
359         disc.callref = found->callref;
360         mncc_set_cause(&disc, GSM48_CAUSE_LOC_USER,
361                 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
362         return mncc_send(ms, MNCC_DISC_REQ, &disc);
363 }
364
365 int mncc_answer(struct osmocom_ms *ms)
366 {
367         struct gsm_call *call, *alerting = NULL;
368         struct gsm_mncc rsp;
369         int active = 0;
370
371         llist_for_each_entry(call, &call_list, entry) {
372                 if (call->ring)
373                         alerting = call;
374                 else if (!call->hold)
375                         active = 1;
376         }
377         if (!alerting) {
378                 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
379                 vty_notify(ms, NULL);
380                 vty_notify(ms, "No alerting call\n");
381                 return -EBUSY;
382         }
383         if (active) {
384                 LOGP(DMNCC, LOGL_INFO, "Answer but we have an active call\n");
385                 vty_notify(ms, NULL);
386                 vty_notify(ms, "Please put active call on hold first!\n");
387                 return -EBUSY;
388         }
389         alerting->ring = 0;
390         alerting->hold = 0;
391
392         memset(&rsp, 0, sizeof(struct gsm_mncc));
393         rsp.callref = alerting->callref;
394         return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
395 }
396
397 int mncc_hold(struct osmocom_ms *ms)
398 {
399         struct gsm_call *call, *found = NULL;
400         struct gsm_mncc hold;
401
402         llist_for_each_entry(call, &call_list, entry) {
403                 if (!call->hold) {
404                         found = call;
405                         break;
406                 }
407         }
408         if (!found) {
409                 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
410                 vty_notify(ms, NULL);
411                 vty_notify(ms, "No active call\n");
412                 return -EINVAL;
413         }
414
415         memset(&hold, 0, sizeof(struct gsm_mncc));
416         hold.callref = found->callref;
417         return mncc_send(ms, MNCC_HOLD_REQ, &hold);
418 }
419
420 int mncc_retrieve(struct osmocom_ms *ms, int number)
421 {
422         struct gsm_call *call;
423         struct gsm_mncc retr;
424         int holdnum = 0, active = 0, i = 0;
425
426         llist_for_each_entry(call, &call_list, entry) {
427                 if (call->hold)
428                         holdnum++;
429                 if (!call->hold)
430                         active = 1;
431         }
432         if (active) {
433                 LOGP(DMNCC, LOGL_INFO, "Cannot retrieve during active call\n");
434                 vty_notify(ms, NULL);
435                 vty_notify(ms, "Hold active call first!\n");
436                 return -EINVAL;
437         }
438         if (holdnum == 0) {
439                 vty_notify(ms, NULL);
440                 vty_notify(ms, "No call on hold!\n");
441                 return -EINVAL;
442         }
443         if (holdnum > 1 && number <= 0) {
444                 vty_notify(ms, NULL);
445                 vty_notify(ms, "Select call 1..%d\n", holdnum);
446                 return -EINVAL;
447         }
448         if (holdnum == 1 && number <= 0)
449                 number = 1;
450         if (number > holdnum) {
451                 vty_notify(ms, NULL);
452                 vty_notify(ms, "Given number %d out of range!\n", number);
453                 vty_notify(ms, "Select call 1..%d\n", holdnum);
454                 return -EINVAL;
455         }
456
457         llist_for_each_entry(call, &call_list, entry) {
458                 i++;
459                 if (i == number)
460                         break;
461         }
462
463         memset(&retr, 0, sizeof(struct gsm_mncc));
464         retr.callref = call->callref;
465         return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);
466 }
467
468
469
470