Fixed CM SERVICE REQUEST message.
[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
34 void *l23_ctx;
35 static int new_callref = 1;
36 static LLIST_HEAD(call_list);
37
38 /*
39  * support functions
40  */
41
42 void mncc_set_cause(struct gsm_mncc *data, int loc, int val);
43
44 static void free_call(struct gsm_call *call)
45 {
46         llist_del(&call->entry);
47         DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
48         talloc_free(call);
49 }
50
51
52 struct gsm_call *get_call_ref(uint32_t callref)
53 {
54         struct gsm_call *callt;
55
56         llist_for_each_entry(callt, &call_list, entry) {
57                 if (callt->callref == callref)
58                         return callt;
59         }
60         return NULL;
61 }
62
63 /*
64  * MNCCms dummy application
65  */
66
67 /* this is a minimal implementation as required by GSM 04.08 */
68 int mncc_recv_dummy(struct osmocom_ms *ms, int msg_type, void *arg)
69 {
70         struct gsm_mncc *data = arg;
71         uint32_t callref = data->callref;
72         struct gsm_mncc rel;
73
74         if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
75                 return 0;
76
77         LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
78
79         /* reject, as we don't support Calls */
80         memset(&rel, 0, sizeof(struct gsm_mncc));
81         rel.callref = callref;
82         mncc_set_cause(&rel, GSM48_CAUSE_LOC_USER,
83                 GSM48_CC_CAUSE_INCOMPAT_DEST);
84
85         return mncc_send(ms, MNCC_REL_REQ, &rel);
86 }
87
88 /*
89  * MNCCms basic call application
90  */
91
92 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
93 {
94         struct gsm_mncc *data = arg;
95         struct gsm_call *call = get_call_ref(data->callref);
96         struct gsm_mncc mncc;
97         uint8_t cause;
98
99         /* call does not exist */
100         if (!call && msg_type != MNCC_SETUP_IND) {
101                 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
102                 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
103                         return 0;
104                 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
105
106                 release:
107                 memset(&mncc, 0, sizeof(struct gsm_mncc));
108                 mncc.callref = data->callref;
109                 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_USER, cause);
110                 return mncc_send(ms, MNCC_REL_REQ, &mncc);
111         }
112
113         /* setup without call */
114         if (!call) {
115                 call = talloc_zero(l23_ctx, struct gsm_call);
116                 if (!call)
117                         return -ENOMEM;
118                 call->callref = new_callref++;
119                 llist_add_tail(&call->entry, &call_list);
120         }
121
122         switch (msg_type) {
123         case MNCC_DISC_IND:
124                 LOGP(DMNCC, LOGL_INFO, "Call has been disconnected\n");
125                 if ((data->fields & MNCC_F_PROGRESS)
126                  && data->progress.descr == 8)
127                         break;
128                 free_call(call);
129                 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
130                 goto release;
131         case MNCC_REL_IND:
132         case MNCC_REL_CNF:
133                 LOGP(DMNCC, LOGL_INFO, "Call has been released\n");
134                 free_call(call);
135                 break;
136         case MNCC_CALL_PROC_IND:
137                 LOGP(DMNCC, LOGL_INFO, "Call is proceeding\n");
138                 break;
139         case MNCC_ALERT_IND:
140                 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
141                 break;
142         case MNCC_SETUP_CNF:
143                 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
144                 break;
145         case MNCC_SETUP_IND:
146                 if (!llist_empty(&call_list)) {
147                         LOGP(DMNCC, LOGL_INFO, "Incomming call but busy\n");
148                         cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
149                         goto release;
150                 }
151                 LOGP(DMNCC, LOGL_INFO, "Incomming call\n");
152                 memset(&mncc, 0, sizeof(struct gsm_mncc));
153                 mncc.callref = call->callref;
154                 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
155                 break;
156                 LOGP(DMNCC, LOGL_INFO, "Ring!\n");
157                 memset(&mncc, 0, sizeof(struct gsm_mncc));
158                 mncc.callref = call->callref;
159                 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
160                 break;
161         default:
162                 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
163                         msg_type);
164         }
165 }
166
167 int mncc_call(struct osmocom_ms *ms, char *number)
168 {
169         struct gsm_call *call;
170         struct gsm_mncc setup;
171
172         if (!llist_empty(&call_list)) {
173                 LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
174                 return -EBUSY;
175         }
176
177         call = talloc_zero(l23_ctx, struct gsm_call);
178         if (!call)
179                 return -ENOMEM;
180         call->callref = new_callref++;
181         llist_add_tail(&call->entry, &call_list);
182
183         memset(&setup, 0, sizeof(struct gsm_mncc));
184         setup.callref = call->callref;
185
186         if (!strncasecmp(number, "emerg", 5)) {
187                 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
188                 /* emergency */
189                 setup.emergency = 1;
190         } else {
191                 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
192                 /* called number */
193                 setup.fields |= MNCC_F_CALLED;
194                 strncpy(setup.called.number, number,
195                         sizeof(setup.called.number) - 1);
196                 
197                 /* bearer capability (mandatory) */
198                 setup.fields |= MNCC_F_BEARER_CAP;
199                 setup.bearer_cap.coding = 0;
200                 setup.bearer_cap.radio = 1;
201                 setup.bearer_cap.speech_ctm = 0;
202                 setup.bearer_cap.speech_ver[0] = 0;
203                 setup.bearer_cap.speech_ver[1] = -1; /* end of list */
204                 setup.bearer_cap.transfer = 0;
205                 setup.bearer_cap.mode = 0;
206         }
207
208         return mncc_send(ms, MNCC_SETUP_REQ, &setup);
209 }
210
211 int mncc_hangup(struct osmocom_ms *ms)
212 {
213         struct gsm_call *call;
214         struct gsm_mncc disc;
215
216         if (llist_empty(&call_list)) {
217                 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
218                 return -EBUSY;
219         }
220         call = llist_entry(call_list.next, struct gsm_call, entry);
221
222         memset(&disc, 0, sizeof(struct gsm_mncc));
223         disc.callref = call->callref;
224         mncc_set_cause(&disc, GSM48_CAUSE_LOC_USER,
225                 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
226         return mncc_send(ms, MNCC_DISC_REQ, &disc);
227 }
228
229 int mncc_answer(struct osmocom_ms *ms)
230 {
231         struct gsm_call *call;
232         struct gsm_mncc rsp;
233
234         if (llist_empty(&call_list)) {
235                 LOGP(DMNCC, LOGL_INFO, "No call to answer\n");
236                 return -EBUSY;
237         }
238         call = llist_entry(call_list.next, struct gsm_call, entry);
239
240         memset(&rsp, 0, sizeof(struct gsm_mncc));
241         rsp.callref = call->callref;
242         mncc_set_cause(&rsp, GSM48_CAUSE_LOC_USER,
243                 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
244         return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
245 }
246
247
248
249