2 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
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.
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.
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.
28 #include <osmocore/talloc.h>
30 #include <osmocom/logging.h>
31 #include <osmocom/osmocom_data.h>
32 #include <osmocom/mncc.h>
33 #include <osmocom/vty.h>
36 static uint32_t new_callref = 1;
37 static LLIST_HEAD(call_list);
43 void mncc_set_cause(struct gsm_mncc *data, int loc, int val);
45 static void free_call(struct gsm_call *call)
47 llist_del(&call->entry);
48 DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
53 struct gsm_call *get_call_ref(uint32_t callref)
55 struct gsm_call *callt;
57 llist_for_each_entry(callt, &call_list, entry) {
58 if (callt->callref == callref)
65 * MNCCms dummy application
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)
71 struct gsm_mncc *data = arg;
72 uint32_t callref = data->callref;
75 if (msg_type == MNCC_REL_IND || msg_type == MNCC_REL_CNF)
78 LOGP(DMNCC, LOGL_INFO, "Rejecting incomming call\n");
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);
86 return mncc_send(ms, MNCC_REL_REQ, &rel);
90 * MNCCms basic call application
93 int mncc_recv_mobile(struct osmocom_ms *ms, int msg_type, void *arg)
95 struct gsm_mncc *data = arg;
96 struct gsm_call *call = get_call_ref(data->callref);
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)
107 cause = GSM48_CC_CAUSE_INCOMPAT_DEST;
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);
115 /* setup without call */
117 if (llist_empty(&call_list))
119 call = talloc_zero(l23_ctx, struct gsm_call);
122 call->callref = data->callref;
123 llist_add_tail(&call->entry, &call_list);
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");
133 case GSM48_CC_CAUSE_NO_ROUTE:
134 vty_notify(ms, "Call: Destination unreachable\n");
136 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
137 vty_notify(ms, "Call: Remote hangs up\n");
139 case GSM48_CC_CAUSE_USER_BUSY:
140 vty_notify(ms, "Call: Remote busy\n");
142 case GSM48_CC_CAUSE_USER_NOTRESPOND:
143 vty_notify(ms, "Call: Remote not responding\n");
145 case GSM48_CC_CAUSE_USER_ALERTING_NA:
146 vty_notify(ms, "Call: Remote not answering\n");
148 case GSM48_CC_CAUSE_CALL_REJECTED:
149 vty_notify(ms, "Call has been rejected\n");
151 case GSM48_CC_CAUSE_NUMBER_CHANGED:
152 vty_notify(ms, "Call: Number changed\n");
154 case GSM48_CC_CAUSE_PRE_EMPTION:
155 vty_notify(ms, "Call: Cleared due to pre-emption\n");
157 case GSM48_CC_CAUSE_DEST_OOO:
158 vty_notify(ms, "Call: Remote out of order\n");
160 case GSM48_CC_CAUSE_INV_NR_FORMAT:
161 vty_notify(ms, "Call: Number invalid or imcomplete\n");
163 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
164 vty_notify(ms, "Call: No channel available\n");
166 case GSM48_CC_CAUSE_NETWORK_OOO:
167 vty_notify(ms, "Call: Network out of order\n");
169 case GSM48_CC_CAUSE_TEMP_FAILURE:
170 vty_notify(ms, "Call: Temporary failure\n");
172 case GSM48_CC_CAUSE_SWITCH_CONG:
173 vty_notify(ms, "Congestion\n");
176 vty_notify(ms, "Call has been disconnected\n");
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");
186 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
190 vty_notify(ms, NULL);
191 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
192 vty_notify(ms, "Call has been rejected\n");
194 vty_notify(ms, "Call has been released\n");
195 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
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");
205 vty_notify(ms, NULL);
206 vty_notify(ms, "Call is aleriting\n");
207 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
210 vty_notify(ms, NULL);
211 vty_notify(ms, "Call is answered\n");
212 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
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;
222 /* presentation allowed if present == 0 */
223 if (data->calling.present || !data->calling.number[0])
224 vty_notify(ms, "Incomming call (callref %x)\n",
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);
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",
237 memset(&mncc, 0, sizeof(struct gsm_mncc));
238 mncc.callref = call->callref;
239 mncc_send(ms, MNCC_CALL_CONF_REQ, &mncc);
241 LOGP(DMNCC, LOGL_INFO, "Ring!\n");
243 LOGP(DMNCC, LOGL_INFO, "Knock!\n");
247 memset(&mncc, 0, sizeof(struct gsm_mncc));
248 mncc.callref = call->callref;
249 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
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");
257 vty_notify(ms, NULL);
258 vty_notify(ms, "Call is on hold\n");
259 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
263 vty_notify(ms, NULL);
264 vty_notify(ms, "Call hold was rejected\n");
265 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
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");
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");
279 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
287 int mncc_call(struct osmocom_ms *ms, char *number)
289 struct gsm_call *call;
290 struct gsm_mncc setup;
292 llist_for_each_entry(call, &call_list, entry) {
294 vty_notify(ms, NULL);
295 vty_notify(ms, "Please put active call on hold "
297 LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
302 call = talloc_zero(l23_ctx, struct gsm_call);
305 call->callref = new_callref++;
306 llist_add_tail(&call->entry, &call_list);
308 memset(&setup, 0, sizeof(struct gsm_mncc));
309 setup.callref = call->callref;
311 if (!strncasecmp(number, "emerg", 5)) {
312 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
316 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
318 setup.fields |= MNCC_F_CALLED;
319 strncpy(setup.called.number, number,
320 sizeof(setup.called.number) - 1);
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)
333 else if (ms->settings.clip)
337 return mncc_send(ms, MNCC_SETUP_REQ, &setup);
340 int mncc_hangup(struct osmocom_ms *ms)
342 struct gsm_call *call, *found = NULL;
343 struct gsm_mncc disc;
345 llist_for_each_entry(call, &call_list, entry) {
352 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
353 vty_notify(ms, NULL);
354 vty_notify(ms, "No active call\n");
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);
365 int mncc_answer(struct osmocom_ms *ms)
367 struct gsm_call *call, *alerting = NULL;
371 llist_for_each_entry(call, &call_list, entry) {
374 else if (!call->hold)
378 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
379 vty_notify(ms, NULL);
380 vty_notify(ms, "No alerting call\n");
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");
392 memset(&rsp, 0, sizeof(struct gsm_mncc));
393 rsp.callref = alerting->callref;
394 return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
397 int mncc_hold(struct osmocom_ms *ms)
399 struct gsm_call *call, *found = NULL;
400 struct gsm_mncc hold;
402 llist_for_each_entry(call, &call_list, entry) {
409 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
410 vty_notify(ms, NULL);
411 vty_notify(ms, "No active call\n");
415 memset(&hold, 0, sizeof(struct gsm_mncc));
416 hold.callref = found->callref;
417 return mncc_send(ms, MNCC_HOLD_REQ, &hold);
420 int mncc_retrieve(struct osmocom_ms *ms, int number)
422 struct gsm_call *call;
423 struct gsm_mncc retr;
424 int holdnum = 0, active = 0, i = 0;
426 llist_for_each_entry(call, &call_list, entry) {
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");
439 vty_notify(ms, NULL);
440 vty_notify(ms, "No call on hold!\n");
443 if (holdnum > 1 && number <= 0) {
444 vty_notify(ms, NULL);
445 vty_notify(ms, "Select call 1..%d\n", holdnum);
448 if (holdnum == 1 && number <= 0)
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);
457 llist_for_each_entry(call, &call_list, entry) {
463 memset(&retr, 0, sizeof(struct gsm_mncc));
464 retr.callref = call->callref;
465 return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);