Merge commit '47ee693170d589f760c4a9c7a5c4ad0b289aa65d'
[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 /*
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         /* not in initiated state anymore */
127         call->init = 0;
128
129         switch (msg_type) {
130         case MNCC_DISC_IND:
131                 vty_notify(ms, NULL);
132                 switch (data->cause.value) {
133                 case GSM48_CC_CAUSE_UNASSIGNED_NR:
134                         vty_notify(ms, "Call: Number not assigned\n");
135                         break;
136                 case GSM48_CC_CAUSE_NO_ROUTE:
137                         vty_notify(ms, "Call: Destination unreachable\n");
138                         break;
139                 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
140                         vty_notify(ms, "Call: Remote hangs up\n");
141                         break;
142                 case GSM48_CC_CAUSE_USER_BUSY:
143                         vty_notify(ms, "Call: Remote busy\n");
144                         break;
145                 case GSM48_CC_CAUSE_USER_NOTRESPOND:
146                         vty_notify(ms, "Call: Remote not responding\n");
147                         break;
148                 case GSM48_CC_CAUSE_USER_ALERTING_NA:
149                         vty_notify(ms, "Call: Remote not answering\n");
150                         break;
151                 case GSM48_CC_CAUSE_CALL_REJECTED:
152                         vty_notify(ms, "Call has been rejected\n");
153                         break;
154                 case GSM48_CC_CAUSE_NUMBER_CHANGED:
155                         vty_notify(ms, "Call: Number changed\n");
156                         break;
157                 case GSM48_CC_CAUSE_PRE_EMPTION:
158                         vty_notify(ms, "Call: Cleared due to pre-emption\n");
159                         break;
160                 case GSM48_CC_CAUSE_DEST_OOO:
161                         vty_notify(ms, "Call: Remote out of order\n");
162                         break;
163                 case GSM48_CC_CAUSE_INV_NR_FORMAT:
164                         vty_notify(ms, "Call: Number invalid or imcomplete\n");
165                         break;
166                 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
167                         vty_notify(ms, "Call: No channel available\n");
168                         break;
169                 case GSM48_CC_CAUSE_NETWORK_OOO:
170                         vty_notify(ms, "Call: Network out of order\n");
171                         break;
172                 case GSM48_CC_CAUSE_TEMP_FAILURE:
173                         vty_notify(ms, "Call: Temporary failure\n");
174                         break;
175                 case GSM48_CC_CAUSE_SWITCH_CONG:
176                         vty_notify(ms, "Congestion\n");
177                         break;
178                 default:
179                         vty_notify(ms, "Call has been disconnected\n");
180                 }
181                 LOGP(DMNCC, LOGL_INFO, "Call has been disconnected "
182                         "(cause %d)\n", data->cause.value);
183                 if ((data->fields & MNCC_F_PROGRESS)
184                  && data->progress.descr == 8) {
185                         vty_notify(ms, "Please hang up!\n");
186                         break;
187                 }
188                 free_call(call);
189                 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
190                 goto release;
191         case MNCC_REL_IND:
192         case MNCC_REL_CNF:
193                 vty_notify(ms, NULL);
194                 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
195                         vty_notify(ms, "Call has been rejected\n");
196                 else
197                         vty_notify(ms, "Call has been released\n");
198                 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
199                         data->cause.value);
200                 free_call(call);
201                 break;
202         case MNCC_CALL_PROC_IND:
203                 vty_notify(ms, NULL);
204                 vty_notify(ms, "Call is proceeding\n");
205                 LOGP(DMNCC, LOGL_INFO, "Call is proceeding\n");
206                 break;
207         case MNCC_ALERT_IND:
208                 vty_notify(ms, NULL);
209                 vty_notify(ms, "Call is aleriting\n");
210                 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
211                 break;
212         case MNCC_SETUP_CNF:
213                 vty_notify(ms, NULL);
214                 vty_notify(ms, "Call is answered\n");
215                 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
216                 break;
217         case MNCC_SETUP_IND:
218                 vty_notify(ms, NULL);
219                 if (!first_call && !ms->settings.cw) {
220                         vty_notify(ms, "Incomming call rejected while busy\n");
221                         LOGP(DMNCC, LOGL_INFO, "Incomming call but busy\n");
222                         cause = GSM48_CC_CAUSE_USER_BUSY;
223                         goto release;
224                 }
225                 /* presentation allowed if present == 0 */
226                 if (data->calling.present || !data->calling.number[0])
227                         vty_notify(ms, "Incomming call (anonymous)\n");
228                 else if (data->calling.type == 1)
229                         vty_notify(ms, "Incomming call (from +%s)\n",
230                                 data->calling.number);
231                 else if (data->calling.type == 2)
232                         vty_notify(ms, "Incomming call (from 0-%s)\n",
233                                 data->calling.number);
234                 else
235                         vty_notify(ms, "Incomming call (from %s)\n",
236                                 data->calling.number);
237                 LOGP(DMNCC, LOGL_INFO, "Incomming call (from %s callref %x)\n",
238                         data->calling.number, call->callref);
239                 memset(&mncc, 0, sizeof(struct gsm_mncc));
240                 mncc.callref = call->callref;
241                 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
242                 if (first_call)
243                         LOGP(DMNCC, LOGL_INFO, "Ring!\n");
244                 else {
245                         LOGP(DMNCC, LOGL_INFO, "Knock!\n");
246                         call->hold = 1;
247                 }
248                 call->ring = 1;
249                 memset(&mncc, 0, sizeof(struct gsm_mncc));
250                 mncc.callref = call->callref;
251                 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
252                 break;
253         case MNCC_SETUP_COMPL_IND:
254                 vty_notify(ms, NULL);
255                 vty_notify(ms, "Call is connected\n");
256                 LOGP(DMNCC, LOGL_INFO, "Call is connected\n");
257                 break;
258         case MNCC_HOLD_CNF:
259                 vty_notify(ms, NULL);
260                 vty_notify(ms, "Call is on hold\n");
261                 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
262                 call->hold = 1;
263                 break;
264         case MNCC_HOLD_REJ:
265                 vty_notify(ms, NULL);
266                 vty_notify(ms, "Call hold was rejected\n");
267                 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
268                 break;
269         case MNCC_RETRIEVE_CNF:
270                 vty_notify(ms, NULL);
271                 vty_notify(ms, "Call is retrieved\n");
272                 LOGP(DMNCC, LOGL_INFO, "Call is retrieved\n");
273                 call->hold = 0;
274                 break;
275         case MNCC_RETRIEVE_REJ:
276                 vty_notify(ms, NULL);
277                 vty_notify(ms, "Call retrieve was rejected\n");
278                 LOGP(DMNCC, LOGL_INFO, "Call retrieve was rejected\n");
279                 break;
280         default:
281                 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
282                         msg_type);
283                 return -EINVAL;
284         }
285
286         return 0;
287 }
288
289 int mncc_call(struct osmocom_ms *ms, char *number)
290 {
291         struct gsm_call *call;
292         struct gsm_mncc setup;
293
294         llist_for_each_entry(call, &call_list, entry) {
295                 if (!call->hold) {
296                         vty_notify(ms, NULL);
297                         vty_notify(ms, "Please put active call on hold "
298                                 "first!\n");
299                         LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
300                         return -EBUSY;
301                 }
302         }
303
304         call = talloc_zero(l23_ctx, struct gsm_call);
305         if (!call)
306                 return -ENOMEM;
307         call->callref = new_callref++;
308         call->init = 1;
309         llist_add_tail(&call->entry, &call_list);
310
311         memset(&setup, 0, sizeof(struct gsm_mncc));
312         setup.callref = call->callref;
313
314         if (!strncasecmp(number, "emerg", 5)) {
315                 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
316                 /* emergency */
317                 setup.emergency = 1;
318         } else {
319                 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
320                 /* called number */
321                 setup.fields |= MNCC_F_CALLED;
322                 strncpy(setup.called.number, number,
323                         sizeof(setup.called.number) - 1);
324                 
325                 /* bearer capability (mandatory) */
326                 setup.fields |= MNCC_F_BEARER_CAP;
327                 setup.bearer_cap.coding = 0;
328                 setup.bearer_cap.radio = 1;
329                 setup.bearer_cap.speech_ctm = 0;
330                 setup.bearer_cap.speech_ver[0] = 0;
331                 setup.bearer_cap.speech_ver[1] = -1; /* end of list */
332                 setup.bearer_cap.transfer = 0;
333                 setup.bearer_cap.mode = 0;
334                 if (ms->settings.clir)
335                         setup.clir.sup = 1;
336                 else if (ms->settings.clip)
337                         setup.clir.inv = 1;
338         }
339
340         return mncc_send(ms, MNCC_SETUP_REQ, &setup);
341 }
342
343 int mncc_hangup(struct osmocom_ms *ms)
344 {
345         struct gsm_call *call, *found = NULL;
346         struct gsm_mncc disc;
347
348         llist_for_each_entry(call, &call_list, entry) {
349                 if (!call->hold) {
350                         found = call;
351                         break;
352                 }
353         }
354         if (!found) {
355                 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
356                 vty_notify(ms, NULL);
357                 vty_notify(ms, "No active call\n");
358                 return -EINVAL;
359         }
360
361         memset(&disc, 0, sizeof(struct gsm_mncc));
362         disc.callref = found->callref;
363         mncc_set_cause(&disc, GSM48_CAUSE_LOC_USER,
364                 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
365         return mncc_send(ms, (call->init) ? MNCC_REL_REQ : MNCC_DISC_REQ,
366                 &disc);
367 }
368
369 int mncc_answer(struct osmocom_ms *ms)
370 {
371         struct gsm_call *call, *alerting = NULL;
372         struct gsm_mncc rsp;
373         int active = 0;
374
375         llist_for_each_entry(call, &call_list, entry) {
376                 if (call->ring)
377                         alerting = call;
378                 else if (!call->hold)
379                         active = 1;
380         }
381         if (!alerting) {
382                 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
383                 vty_notify(ms, NULL);
384                 vty_notify(ms, "No alerting call\n");
385                 return -EBUSY;
386         }
387         if (active) {
388                 LOGP(DMNCC, LOGL_INFO, "Answer but we have an active call\n");
389                 vty_notify(ms, NULL);
390                 vty_notify(ms, "Please put active call on hold first!\n");
391                 return -EBUSY;
392         }
393         alerting->ring = 0;
394         alerting->hold = 0;
395
396         memset(&rsp, 0, sizeof(struct gsm_mncc));
397         rsp.callref = alerting->callref;
398         return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
399 }
400
401 int mncc_hold(struct osmocom_ms *ms)
402 {
403         struct gsm_call *call, *found = NULL;
404         struct gsm_mncc hold;
405
406         llist_for_each_entry(call, &call_list, entry) {
407                 if (!call->hold) {
408                         found = call;
409                         break;
410                 }
411         }
412         if (!found) {
413                 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
414                 vty_notify(ms, NULL);
415                 vty_notify(ms, "No active call\n");
416                 return -EINVAL;
417         }
418
419         memset(&hold, 0, sizeof(struct gsm_mncc));
420         hold.callref = found->callref;
421         return mncc_send(ms, MNCC_HOLD_REQ, &hold);
422 }
423
424 int mncc_retrieve(struct osmocom_ms *ms, int number)
425 {
426         struct gsm_call *call;
427         struct gsm_mncc retr;
428         int holdnum = 0, active = 0, i = 0;
429
430         llist_for_each_entry(call, &call_list, entry) {
431                 if (call->hold)
432                         holdnum++;
433                 if (!call->hold)
434                         active = 1;
435         }
436         if (active) {
437                 LOGP(DMNCC, LOGL_INFO, "Cannot retrieve during active call\n");
438                 vty_notify(ms, NULL);
439                 vty_notify(ms, "Hold active call first!\n");
440                 return -EINVAL;
441         }
442         if (holdnum == 0) {
443                 vty_notify(ms, NULL);
444                 vty_notify(ms, "No call on hold!\n");
445                 return -EINVAL;
446         }
447         if (holdnum > 1 && number <= 0) {
448                 vty_notify(ms, NULL);
449                 vty_notify(ms, "Select call 1..%d\n", holdnum);
450                 return -EINVAL;
451         }
452         if (holdnum == 1 && number <= 0)
453                 number = 1;
454         if (number > holdnum) {
455                 vty_notify(ms, NULL);
456                 vty_notify(ms, "Given number %d out of range!\n", number);
457                 vty_notify(ms, "Select call 1..%d\n", holdnum);
458                 return -EINVAL;
459         }
460
461         llist_for_each_entry(call, &call_list, entry) {
462                 i++;
463                 if (i == number)
464                         break;
465         }
466
467         memset(&retr, 0, sizeof(struct gsm_mncc));
468         retr.callref = call->callref;
469         return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);
470 }
471
472
473
474