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