1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN gsm (MS mode) **
10 \*****************************************************************************/
20 #include <osmocore/select.h>
21 #include <osmocore/talloc.h>
23 #include <osmocom/osmocom_data.h>
24 #include <osmocom/logging.h>
25 #include <osmocom/l1l2_interface.h>
26 #include <osmocom/l23_app.h>
29 const char *openbsc_copyright = "";
30 short vty_port = 4247;
32 struct llist_head ms_list;
33 struct log_target *stderr_target;
35 int (*l23_app_work) (struct osmocom_ms *ms) = NULL;
36 int (*l23_app_exit) (struct osmocom_ms *ms) = NULL;
41 Pgsm_ms::Pgsm_ms(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : Pgsm(type, mISDNport, portname, settings, channel, exclusive, mode)
43 struct osmocom_ms *ms = NULL;
44 char *ms_name = mISDNport->ifport->gsm_ms_name;
46 p_m_g_instance = NULL;
48 llist_for_each_entry(ms, &ms_list, entity) {
49 if (!strcmp(ms->name, ms_name)) {
56 FATAL("MS name %s does not exists. Please fix!");
58 PDEBUG(DEBUG_GSM, "Created new GSMMSPort(%s %s).\n", portname, ms_name);
66 PDEBUG(DEBUG_GSM, "Destroyed GSM MS process(%s).\n", p_name);
70 * handles all indications
72 /* SETUP INDICATION */
73 void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
76 class Endpoint *epoint;
77 struct lcr_msg *message;
79 struct gsm_mncc *mode, *proceeding, *frame;
81 /* process given callref */
82 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
83 add_trace("callref", "new", "0x%x", callref);
85 /* release in case the ID is already in use */
86 add_trace("error", NULL, "callref already in use");
88 mncc = create_mncc(MNCC_REJ_REQ, callref);
89 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
90 mncc->fields |= MNCC_F_CAUSE;
91 mncc->cause.coding = 3;
92 mncc->cause.location = 1;
93 mncc->cause.value = 47;
94 add_trace("cause", "coding", "%d", mncc->cause.coding);
95 add_trace("cause", "location", "%d", mncc->cause.location);
96 add_trace("cause", "value", "%d", mncc->cause.value);
97 add_trace("reason", NULL, "callref already in use");
99 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
100 new_state(PORT_STATE_RELEASE);
101 trigger_work(&p_m_g_delete);
104 p_m_g_callref = callref;
107 /* if blocked, release call with MT_RELEASE_COMPLETE */
108 if (p_m_mISDNport->ifport->block) {
109 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
110 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
111 mncc->fields |= MNCC_F_CAUSE;
112 mncc->cause.coding = 3;
113 mncc->cause.location = 1;
114 mncc->cause.value = 27;
115 add_trace("cause", "coding", "%d", mncc->cause.coding);
116 add_trace("cause", "location", "%d", mncc->cause.location);
117 add_trace("cause", "value", "%d", mncc->cause.value);
118 add_trace("reason", NULL, "port is blocked");
120 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
121 new_state(PORT_STATE_RELEASE);
122 trigger_work(&p_m_g_delete);
126 l1l2l3_trace_header(p_m_mISDNport, this, MNCC_SETUP_IND, DIRECTION_IN);
127 /* caller information */
128 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
129 if (mncc->fields & MNCC_F_CALLING) {
130 switch (mncc->calling.present) {
132 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
135 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
138 p_callerinfo.present = INFO_PRESENT_ALLOWED;
141 switch (mncc->calling.screen) {
143 p_callerinfo.screen = INFO_SCREEN_USER;
146 p_callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
149 p_callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
152 p_callerinfo.screen = INFO_SCREEN_NETWORK;
155 switch (mncc->calling.type) {
157 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
160 p_callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
163 p_callerinfo.ntype = INFO_NTYPE_NATIONAL;
166 p_callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
169 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
172 SCPY(p_callerinfo.id, mncc->calling.number);
173 add_trace("calling", "type", "%d", mncc->calling.type);
174 add_trace("calling", "plan", "%d", mncc->calling.plan);
175 add_trace("calling", "present", "%d", mncc->calling.present);
176 add_trace("calling", "screen", "%d", mncc->calling.screen);
177 add_trace("calling", "number", "%s", mncc->calling.number);
179 p_callerinfo.isdn_port = p_m_portnum;
180 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
181 /* dialing information */
182 if (mncc->fields & MNCC_F_CALLED) {
183 SCAT(p_dialinginfo.id, mncc->called.number);
184 switch (mncc->called.type) {
186 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
189 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
192 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
195 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
198 add_trace("dialing", "type", "%d", mncc->called.type);
199 add_trace("dialing", "plan", "%d", mncc->called.plan);
200 add_trace("dialing", "number", "%s", mncc->called.number);
203 p_redirinfo.ntype = INFO_NTYPE_NOTPRESENT;
204 if (mncc->fields & MNCC_F_REDIRECTING) {
205 switch (mncc->redirecting.present) {
207 p_redirinfo.present = INFO_PRESENT_RESTRICTED;
210 p_redirinfo.present = INFO_PRESENT_NOTAVAIL;
213 p_redirinfo.present = INFO_PRESENT_ALLOWED;
216 switch (mncc->redirecting.screen) {
218 p_redirinfo.screen = INFO_SCREEN_USER;
221 p_redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
224 p_redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
227 p_redirinfo.screen = INFO_SCREEN_NETWORK;
230 switch (mncc->redirecting.type) {
232 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
235 p_redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
238 p_redirinfo.ntype = INFO_NTYPE_NATIONAL;
241 p_redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
244 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
247 SCPY(p_redirinfo.id, mncc->redirecting.number);
248 add_trace("redir", "type", "%d", mncc->redirecting.type);
249 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
250 add_trace("redir", "present", "%d", mncc->redirecting.present);
251 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
252 add_trace("redir", "number", "%s", mncc->redirecting.number);
253 p_redirinfo.isdn_port = p_m_portnum;
255 /* bearer capability */
256 if (mncc->fields & MNCC_F_BEARER_CAP) {
257 switch (mncc->bearer_cap.transfer) {
259 p_capainfo.bearer_capa = INFO_BC_DATAUNRESTRICTED;
263 p_capainfo.bearer_capa = INFO_BC_AUDIO;
264 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
267 p_capainfo.bearer_capa = INFO_BC_SPEECH;
268 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
271 switch (mncc->bearer_cap.mode) {
273 p_capainfo.bearer_mode = INFO_BMODE_PACKET;
276 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
279 add_trace("bearer", "transfer", "%d", mncc->bearer_cap.transfer);
280 add_trace("bearer", "mode", "%d", mncc->bearer_cap.mode);
282 p_capainfo.bearer_capa = INFO_BC_SPEECH;
283 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
284 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
286 /* if packet mode works some day, see dss1.cpp for conditions */
287 p_capainfo.source_mode = B_MODE_TRANSPARENT;
292 ret = channel = hunt_bchannel();
297 ret = seize_bchannel(channel, 1);
300 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
301 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
302 mncc->fields |= MNCC_F_CAUSE;
303 mncc->cause.coding = 3;
304 mncc->cause.location = 1;
305 mncc->cause.value = 34;
306 add_trace("cause", "coding", "%d", mncc->cause.coding);
307 add_trace("cause", "location", "%d", mncc->cause.location);
308 add_trace("cause", "value", "%d", mncc->cause.value);
309 add_trace("reason", NULL, "no channel");
311 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
312 new_state(PORT_STATE_RELEASE);
313 trigger_work(&p_m_g_delete);
316 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
317 if (bchannel_open(p_m_b_index))
320 /* what infos did we got ... */
321 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
322 if (p_callerinfo.id[0])
323 add_trace("calling", "number", "%s", p_callerinfo.id);
325 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
326 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
327 add_trace("dialing", "number", "%s", p_dialinginfo.id);
330 /* create endpoint */
332 FATAL("Incoming call but already got an endpoint.\n");
333 if (!(epoint = new Endpoint(p_serial, 0)))
334 FATAL("No memory for Endpoint instance\n");
335 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
336 FATAL("No memory for Endpoint Application instance\n");
337 epointlist_new(epoint->ep_serial);
339 /* modify lchan to GSM codec V1 */
340 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
341 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
342 mode->lchan_mode = 0x01; /* GSM V1 */
343 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
345 send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
347 /* send call proceeding */
348 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
349 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
350 if (p_m_mISDNport->tones) {
351 proceeding->fields |= MNCC_F_PROGRESS;
352 proceeding->progress.coding = 3; /* GSM */
353 proceeding->progress.location = 1;
354 proceeding->progress.descr = 8;
355 add_trace("progress", "coding", "%d", proceeding->progress.coding);
356 add_trace("progress", "location", "%d", proceeding->progress.location);
357 add_trace("progress", "descr", "%d", proceeding->progress.descr);
360 send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
362 new_state(PORT_STATE_IN_PROCEEDING);
364 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
365 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
367 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
368 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
369 p_m_g_tch_connected = 1;
372 /* send setup message to endpoit */
373 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
374 message->param.setup.isdn_port = p_m_portnum;
375 message->param.setup.port_type = p_type;
376 // message->param.setup.dtmf = 0;
377 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
378 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
379 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
380 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
381 message->param.setup.useruser.len = strlen(mncc->useruser.info);
382 message->param.setup.useruser.protocol = mncc->useruser.proto;
383 message_put(message);
387 * MS sends message to port
389 static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
391 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
392 unsigned int callref = mncc->callref;
394 class Pgsm_ms *pgsm_ms = NULL;
396 struct mISDNport *mISDNport;
398 /* Special messages */
403 callref = mncc->callref;
406 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_MS) {
407 pgsm_ms = (class Pgsm_ms *)port;
408 if (pgsm_ms->p_m_g_callref == callref) {
415 if (msg_type == GSM_TCHF_FRAME) {
417 pgsm_ms->frame_receive((struct gsm_trau_frame *)arg);
422 if (msg_type != MNCC_SETUP_IND)
424 /* find gsm ms port */
425 mISDNport = mISDNport_first;
427 if (mISDNport->gsm_ms && !strcmp(mISDNport->ifport->gsm_ms_name, ms->name))
429 mISDNport = mISDNport->next;
432 struct gsm_mncc *rej;
434 rej = create_mncc(MNCC_REJ_REQ, callref);
435 rej->fields |= MNCC_F_CAUSE;
436 rej->cause.coding = 3;
437 rej->cause.location = 1;
438 rej->cause.value = 27;
439 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
440 add_trace("cause", "coding", "%d", rej->cause.coding);
441 add_trace("cause", "location", "%d", rej->cause.location);
442 add_trace("cause", "value", "%d", rej->cause.value);
444 send_and_free_mncc(ms, rej->msg_type, rej);
447 /* creating port object, transparent until setup with hdlc */
448 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
449 if (!(pgsm_ms = new Pgsm_ms(PORT_TYPE_GSM_MS_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
451 FATAL("Cannot create Port instance.\n");
456 pgsm_ms->setup_ind(msg_type, callref, mncc);
460 pgsm_ms->alert_ind(msg_type, callref, mncc);
464 pgsm_ms->setup_cnf(msg_type, callref, mncc);
467 case MNCC_SETUP_COMPL_IND:
468 pgsm_ms->setup_compl_ind(msg_type, callref, mncc);
472 pgsm_ms->disc_ind(msg_type, callref, mncc);
478 pgsm_ms->rel_ind(msg_type, callref, mncc);
481 case MNCC_NOTIFY_IND:
482 pgsm_ms->notify_ind(msg_type, callref, mncc);
492 void Pgsm_ms::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
494 struct lcr_msg *message;
496 struct epoint_list *epointlist;
497 struct gsm_mncc *mncc;
500 /* copy setup infos to port */
501 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
502 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
503 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
504 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
507 if (!p_dialinginfo.id[0]) {
508 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
509 add_trace("failure", NULL, "No dialed subscriber given.");
511 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
512 message->param.disconnectinfo.cause = 28;
513 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
514 message_put(message);
515 new_state(PORT_STATE_RELEASE);
516 trigger_work(&p_m_g_delete);
520 /* release if port is blocked */
521 if (p_m_mISDNport->ifport->block) {
522 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
523 add_trace("failure", NULL, "Port blocked.");
525 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
526 message->param.disconnectinfo.cause = 27; // temp. unavail.
527 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
528 message_put(message);
529 new_state(PORT_STATE_RELEASE);
530 trigger_work(&p_m_g_delete);
535 ret = channel = hunt_bchannel();
539 ret = seize_bchannel(channel, 1);
542 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
543 add_trace("failure", NULL, "No internal audio channel available.");
545 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
546 message->param.disconnectinfo.cause = 34;
547 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
548 message_put(message);
549 new_state(PORT_STATE_RELEASE);
550 trigger_work(&p_m_g_delete);
553 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
554 if (bchannel_open(p_m_b_index))
557 /* attach only if not already */
558 epointlist = p_epointlist;
560 if (epointlist->epoint_id == epoint_id)
562 epointlist = epointlist->next;
565 epointlist_new(epoint_id);
568 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
569 p_m_g_callref = new_callref++;
570 add_trace("callref", "new", "0x%x", p_m_g_callref);
573 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
574 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
575 if (!strncasecmp(p_dialinginfo.id, "emerg", 5)) {
578 /* caller info (only clir) */
579 switch (p_callerinfo.present) {
580 case INFO_PRESENT_ALLOWED:
586 /* dialing information (mandatory) */
587 mncc->fields |= MNCC_F_CALLED;
588 mncc->called.type = 0; /* unknown */
589 mncc->called.plan = 1; /* isdn */
590 SCPY(mncc->called.number, p_dialinginfo.id);
591 add_trace("dialing", "number", "%s", mncc->called.number);
593 /* bearer capability (mandatory) */
594 mncc->fields |= MNCC_F_BEARER_CAP;
595 mncc->bearer_cap.coding = 0;
596 mncc->bearer_cap.radio = 1;
597 mncc->bearer_cap.speech_ctm = 0;
598 mncc->bearer_cap.speech_ver[0] = 0;
599 mncc->bearer_cap.speech_ver[1] = -1; /* end of list */
600 switch (p_capainfo.bearer_capa) {
601 case INFO_BC_DATAUNRESTRICTED:
602 case INFO_BC_DATARESTRICTED:
603 mncc->bearer_cap.transfer = 1;
606 mncc->bearer_cap.transfer = 0;
609 mncc->bearer_cap.transfer = 2;
612 switch (p_capainfo.bearer_mode) {
613 case INFO_BMODE_PACKET:
614 mncc->bearer_cap.mode = 1;
617 mncc->bearer_cap.mode = 0;
623 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
625 new_state(PORT_STATE_OUT_SETUP);
627 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
628 message_put(message);
630 new_state(PORT_STATE_OUT_PROCEEDING);
634 * endpoint sends messages to the port
636 int Pgsm_ms::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
638 if (Pgsm::message_epoint(epoint_id, message_id, param))
642 case MESSAGE_SETUP: /* dial-out command received from epoint */
643 if (p_state!=PORT_STATE_IDLE)
645 message_setup(epoint_id, message_id, param);
649 PDEBUG(DEBUG_GSM, "Pgsm_ms(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
655 int gsm_ms_exit(int rc)
661 int gsm_ms_init(void)
663 INIT_LLIST_HEAD(&ms_list);
665 stderr_target = log_target_create_stderr();
666 log_add_target(stderr_target);
667 log_set_all_filter(stderr_target, 1);
669 l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
674 /* add a new GSM mobile instance */
675 int gsm_ms_new(const char *name, const char *socket_path)
677 struct osmocom_ms *ms = NULL;
680 PDEBUG(DEBUG_GSM, "GSM: creating new instance '%s' on '%s'\n", name, socket_path);
682 ms = talloc_zero(l23_ctx, struct osmocom_ms);
684 FATAL("Failed to allocate MS\n");
686 /* must add here, because other init processes may search in the list */
687 llist_add_tail(&ms->entity, &ms_list);
690 SPRINT(ms->name, name);
692 rc = layer2_open(ms, socket_path);
694 FATAL("Failed during layer2_open()\n");
697 lapdm_init(&ms->l2_entity.lapdm_dcch, ms);
698 lapdm_init(&ms->l2_entity.lapdm_acch, ms);
700 rc = l23_app_init(ms);
702 FATAL("Failed to init layer23\n");
704 ms->cclayer.mncc_recv = message_ms;
709 int gsm_ms_delete(const char *name)
711 struct osmocom_ms *ms = NULL;
714 PDEBUG(DEBUG_GSM, "GSM: destroying instance '%s'\n", name);
716 llist_for_each_entry(ms, &ms_list, entity) {
717 if (!strcmp(ms->name, name)) {
724 FATAL("Failed delete layer23, instance '%s' not found\n", name);
728 lapdm_exit(&ms->l2_entity.lapdm_dcch);
729 lapdm_exit(&ms->l2_entity.lapdm_acch);
731 llist_del(&ms->entity);
737 * handles bsc select function within LCR's main loop
739 int handle_gsm_ms(void)
741 struct osmocom_ms *ms = NULL;
744 llist_for_each_entry(ms, &ms_list, entity) {
745 if (l23_app_work(ms))
747 // debug_reset_context();
748 if (bsc_select_main(1)) /* polling */