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/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>
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);
126 /* not in initiated state anymore */
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");
136 case GSM48_CC_CAUSE_NO_ROUTE:
137 vty_notify(ms, "Call: Destination unreachable\n");
139 case GSM48_CC_CAUSE_NORM_CALL_CLEAR:
140 vty_notify(ms, "Call: Remote hangs up\n");
142 case GSM48_CC_CAUSE_USER_BUSY:
143 vty_notify(ms, "Call: Remote busy\n");
145 case GSM48_CC_CAUSE_USER_NOTRESPOND:
146 vty_notify(ms, "Call: Remote not responding\n");
148 case GSM48_CC_CAUSE_USER_ALERTING_NA:
149 vty_notify(ms, "Call: Remote not answering\n");
151 case GSM48_CC_CAUSE_CALL_REJECTED:
152 vty_notify(ms, "Call has been rejected\n");
154 case GSM48_CC_CAUSE_NUMBER_CHANGED:
155 vty_notify(ms, "Call: Number changed\n");
157 case GSM48_CC_CAUSE_PRE_EMPTION:
158 vty_notify(ms, "Call: Cleared due to pre-emption\n");
160 case GSM48_CC_CAUSE_DEST_OOO:
161 vty_notify(ms, "Call: Remote out of order\n");
163 case GSM48_CC_CAUSE_INV_NR_FORMAT:
164 vty_notify(ms, "Call: Number invalid or imcomplete\n");
166 case GSM48_CC_CAUSE_NO_CIRCUIT_CHAN:
167 vty_notify(ms, "Call: No channel available\n");
169 case GSM48_CC_CAUSE_NETWORK_OOO:
170 vty_notify(ms, "Call: Network out of order\n");
172 case GSM48_CC_CAUSE_TEMP_FAILURE:
173 vty_notify(ms, "Call: Temporary failure\n");
175 case GSM48_CC_CAUSE_SWITCH_CONG:
176 vty_notify(ms, "Congestion\n");
179 vty_notify(ms, "Call has been disconnected\n");
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");
189 cause = GSM48_CC_CAUSE_NORM_CALL_CLEAR;
193 vty_notify(ms, NULL);
194 if (data->cause.value == GSM48_CC_CAUSE_CALL_REJECTED)
195 vty_notify(ms, "Call has been rejected\n");
197 vty_notify(ms, "Call has been released\n");
198 LOGP(DMNCC, LOGL_INFO, "Call has been released (cause %d)\n",
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");
208 vty_notify(ms, NULL);
209 vty_notify(ms, "Call is aleriting\n");
210 LOGP(DMNCC, LOGL_INFO, "Call is alerting\n");
213 vty_notify(ms, NULL);
214 vty_notify(ms, "Call is answered\n");
215 LOGP(DMNCC, LOGL_INFO, "Call is answered\n");
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;
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);
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);
243 LOGP(DMNCC, LOGL_INFO, "Ring!\n");
245 LOGP(DMNCC, LOGL_INFO, "Knock!\n");
249 memset(&mncc, 0, sizeof(struct gsm_mncc));
250 mncc.callref = call->callref;
251 mncc_send(ms, MNCC_ALERT_REQ, &mncc);
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");
259 vty_notify(ms, NULL);
260 vty_notify(ms, "Call is on hold\n");
261 LOGP(DMNCC, LOGL_INFO, "Call is on hold\n");
265 vty_notify(ms, NULL);
266 vty_notify(ms, "Call hold was rejected\n");
267 LOGP(DMNCC, LOGL_INFO, "Call hold was rejected\n");
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");
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");
281 LOGP(DMNCC, LOGL_INFO, "Message 0x%02x unsupported\n",
289 int mncc_call(struct osmocom_ms *ms, char *number)
291 struct gsm_call *call;
292 struct gsm_mncc setup;
294 llist_for_each_entry(call, &call_list, entry) {
296 vty_notify(ms, NULL);
297 vty_notify(ms, "Please put active call on hold "
299 LOGP(DMNCC, LOGL_INFO, "Cannot make a call, busy!\n");
304 call = talloc_zero(l23_ctx, struct gsm_call);
307 call->callref = new_callref++;
309 llist_add_tail(&call->entry, &call_list);
311 memset(&setup, 0, sizeof(struct gsm_mncc));
312 setup.callref = call->callref;
314 if (!strncasecmp(number, "emerg", 5)) {
315 LOGP(DMNCC, LOGL_INFO, "Make emergency call\n");
319 LOGP(DMNCC, LOGL_INFO, "Make call to %s\n", number);
321 setup.fields |= MNCC_F_CALLED;
322 strncpy(setup.called.number, number,
323 sizeof(setup.called.number) - 1);
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)
336 else if (ms->settings.clip)
340 return mncc_send(ms, MNCC_SETUP_REQ, &setup);
343 int mncc_hangup(struct osmocom_ms *ms)
345 struct gsm_call *call, *found = NULL;
346 struct gsm_mncc disc;
348 llist_for_each_entry(call, &call_list, entry) {
355 LOGP(DMNCC, LOGL_INFO, "No active call to hangup\n");
356 vty_notify(ms, NULL);
357 vty_notify(ms, "No active call\n");
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,
369 int mncc_answer(struct osmocom_ms *ms)
371 struct gsm_call *call, *alerting = NULL;
375 llist_for_each_entry(call, &call_list, entry) {
378 else if (!call->hold)
382 LOGP(DMNCC, LOGL_INFO, "No call alerting\n");
383 vty_notify(ms, NULL);
384 vty_notify(ms, "No alerting call\n");
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");
396 memset(&rsp, 0, sizeof(struct gsm_mncc));
397 rsp.callref = alerting->callref;
398 return mncc_send(ms, MNCC_SETUP_RSP, &rsp);
401 int mncc_hold(struct osmocom_ms *ms)
403 struct gsm_call *call, *found = NULL;
404 struct gsm_mncc hold;
406 llist_for_each_entry(call, &call_list, entry) {
413 LOGP(DMNCC, LOGL_INFO, "No active call to hold\n");
414 vty_notify(ms, NULL);
415 vty_notify(ms, "No active call\n");
419 memset(&hold, 0, sizeof(struct gsm_mncc));
420 hold.callref = found->callref;
421 return mncc_send(ms, MNCC_HOLD_REQ, &hold);
424 int mncc_retrieve(struct osmocom_ms *ms, int number)
426 struct gsm_call *call;
427 struct gsm_mncc retr;
428 int holdnum = 0, active = 0, i = 0;
430 llist_for_each_entry(call, &call_list, entry) {
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");
443 vty_notify(ms, NULL);
444 vty_notify(ms, "No call on hold!\n");
447 if (holdnum > 1 && number <= 0) {
448 vty_notify(ms, NULL);
449 vty_notify(ms, "Select call 1..%d\n", holdnum);
452 if (holdnum == 1 && number <= 0)
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);
461 llist_for_each_entry(call, &call_list, entry) {
467 memset(&retr, 0, sizeof(struct gsm_mncc));
468 retr.callref = call->callref;
469 return mncc_send(ms, MNCC_RETRIEVE_REQ, &retr);