Fixed mncc.h for GSM. Audio works again with latest OpenBSC.
[lcr.git] / gsm_ms.cpp
index f8541fa..b966ad8 100644 (file)
 \*****************************************************************************/ 
 
 #include "main.h"
+#include "mncc.h"
 
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-extern "C" {
-#include <getopt.h>
-
-#include <osmocore/select.h>
-#include <osmocore/talloc.h>
 
-#include <osmocom/osmocom_data.h>
-#include <osmocom/logging.h>
-#include <osmocom/l1l2_interface.h>
-#include <osmocom/l23_app.h>
-}
+struct lcr_gsm *gsm_ms_first = NULL;
 
-const char *openbsc_copyright = "";
-short vty_port = 4247;
+static int dtmf_timeout(struct lcr_timer *timer, void *instance, int index);
 
-struct llist_head ms_list;
-struct log_target *stderr_target;
-void *l23_ctx = NULL;
-int (*l23_app_work) (struct osmocom_ms *ms) = NULL;
-int (*l23_app_exit) (struct osmocom_ms *ms) = NULL;
+#define DTMF_ST_IDLE           0       /* no DTMF active */
+#define DTMF_ST_START          1       /* DTMF started, waiting for resp. */
+#define DTMF_ST_MARK           2       /* wait tone duration */
+#define DTMF_ST_STOP           3       /* DTMF stopped, waiting for resp. */
+#define DTMF_ST_SPACE          4       /* wait space between tones */
 
 /*
  * constructor
  */
 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)
 {
-       struct osmocom_ms *ms = NULL;
+       struct lcr_gsm *gsm_ms = gsm_ms_first;
        char *ms_name = mISDNport->ifport->gsm_ms_name;
 
-       p_m_g_instance = NULL;
+       p_m_g_lcr_gsm = NULL;
 
-       llist_for_each_entry(ms, &ms_list, entity) {
-               if (!strcmp(ms->name, ms_name)) {
-                       p_m_g_instance = ms;
+       while (gsm_ms) {
+               if (gsm_ms->type == LCR_GSM_TYPE_MS && !strcmp(gsm_ms->name, ms_name)) {
+                       p_m_g_lcr_gsm = gsm_ms;
                        break;
                }
+               gsm_ms = gsm_ms->gsm_ms_next;
        }
 
-       if (!p_m_g_instance)
-               FATAL("MS name %s does not exists. Please fix!");
+       p_m_g_dtmf_state = DTMF_ST_IDLE;
+       p_m_g_dtmf_index = 0;
+       p_m_g_dtmf[0] = '\0';
+       memset(&p_m_g_dtmf_timer, 0, sizeof(p_m_g_dtmf_timer));
+       add_timer(&p_m_g_dtmf_timer, dtmf_timeout, this, 0);
 
        PDEBUG(DEBUG_GSM, "Created new GSMMSPort(%s %s).\n", portname, ms_name);
 }
@@ -64,6 +56,7 @@ Pgsm_ms::Pgsm_ms(int type, struct mISDNport *mISDNport, char *portname, struct p
 Pgsm_ms::~Pgsm_ms()
 {
        PDEBUG(DEBUG_GSM, "Destroyed GSM MS process(%s).\n", p_name);
+       del_timer(&p_m_g_dtmf_timer);
 }
 
 /*
@@ -96,7 +89,7 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
                add_trace("cause", "value", "%d", mncc->cause.value);
                add_trace("reason", NULL, "callref already in use");
                end_trace();
-               send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
+               send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
                new_state(PORT_STATE_RELEASE);
                trigger_work(&p_m_g_delete);
                return;
@@ -117,13 +110,13 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
                add_trace("cause", "value", "%d", mncc->cause.value);
                add_trace("reason", NULL, "port is blocked");
                end_trace();
-               send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
+               send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
                new_state(PORT_STATE_RELEASE);
                trigger_work(&p_m_g_delete);
                return;
        }
 
-       l1l2l3_trace_header(p_m_mISDNport, this, MNCC_SETUP_IND, DIRECTION_IN);
+       gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_IND, DIRECTION_IN);
        /* caller information */
        p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
        if (mncc->fields & MNCC_F_CALLING) {
@@ -199,6 +192,7 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
                add_trace("dialing", "plan", "%d", mncc->called.plan);
                add_trace("dialing", "number", "%s", mncc->called.number);
        }
+       p_dialinginfo.sending_complete = 1;
        /* redir info */
        p_redirinfo.ntype = INFO_NTYPE_NOTPRESENT;
        if (mncc->fields & MNCC_F_REDIRECTING) {
@@ -308,7 +302,7 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
                add_trace("cause", "value", "%d", mncc->cause.value);
                add_trace("reason", NULL, "no channel");
                end_trace();
-               send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
+               send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
                new_state(PORT_STATE_RELEASE);
                trigger_work(&p_m_g_delete);
                return;
@@ -317,16 +311,6 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
        if (bchannel_open(p_m_b_index))
                goto no_channel;
 
-       /* what infos did we got ... */
-       gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
-       if (p_callerinfo.id[0])
-               add_trace("calling", "number", "%s", p_callerinfo.id);
-       else
-               SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
-       add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
-       add_trace("dialing", "number", "%s", p_dialinginfo.id);
-       end_trace();
-
        /* create endpoint */
        if (p_epointlist)
                FATAL("Incoming call but already got an endpoint.\n");
@@ -340,24 +324,20 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
        gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
        mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
        mode->lchan_mode = 0x01; /* GSM V1 */
+       mode->lchan_type = 0x02;
        add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
        end_trace();
-       send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
+       send_and_free_mncc(p_m_g_lcr_gsm, mode->msg_type, mode);
 
        /* send call proceeding */
-       gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
-       proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
-       if (p_m_mISDNport->tones) {
-               proceeding->fields |= MNCC_F_PROGRESS;
-               proceeding->progress.coding = 3; /* GSM */
-               proceeding->progress.location = 1;
-               proceeding->progress.descr = 8;
-               add_trace("progress", "coding", "%d", proceeding->progress.coding);
-               add_trace("progress", "location", "%d", proceeding->progress.location);
-               add_trace("progress", "descr", "%d", proceeding->progress.descr);
-       }
+       gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_CONF_REQ, DIRECTION_OUT);
+       proceeding = create_mncc(MNCC_CALL_CONF_REQ, p_m_g_callref);
+       // FIXME: bearer
+       /* DTMF supported */
+       proceeding->fields |= MNCC_F_CCCAP;
+       proceeding->cccap.dtmf = 1;
        end_trace();
-       send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
+       send_and_free_mncc(p_m_g_lcr_gsm, proceeding->msg_type, proceeding);
 
        new_state(PORT_STATE_IN_PROCEEDING);
 
@@ -365,7 +345,7 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
                gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
                end_trace();
                frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
-               send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
+               send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
                p_m_g_tch_connected = 1;
        }
 
@@ -386,7 +366,7 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
 /*
  * MS sends message to port
  */
-static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
+int message_ms(struct lcr_gsm *gsm_ms, int msg_type, void *arg)
 {
        struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
        unsigned int callref = mncc->callref;
@@ -396,7 +376,7 @@ static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
        struct mISDNport *mISDNport;
 
        /* Special messages */
-       if (msg_type) {
+       switch (msg_type) {
        }
 
        /* find callref */
@@ -414,7 +394,7 @@ static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
 
        if (msg_type == GSM_TCHF_FRAME) {
                if (port)
-                       pgsm_ms->frame_receive((struct gsm_trau_frame *)arg);
+                       pgsm_ms->frame_receive(arg);
                return 0;
        }
 
@@ -424,7 +404,7 @@ static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
                /* find gsm ms port */
                mISDNport = mISDNport_first;
                while(mISDNport) {
-                       if (mISDNport->gsm_ms && !strcmp(mISDNport->ifport->gsm_ms_name, ms->name))
+                       if (mISDNport->gsm_ms && !strcmp(mISDNport->ifport->gsm_ms_name, gsm_ms->name))
                                break;
                        mISDNport = mISDNport->next;
                }
@@ -441,7 +421,7 @@ static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
                        add_trace("cause", "location", "%d", rej->cause.location);
                        add_trace("cause", "value", "%d", rej->cause.value);
                        end_trace();
-                       send_and_free_mncc(ms, rej->msg_type, rej);
+                       send_and_free_mncc(gsm_ms, rej->msg_type, rej);
                        return 0;
                }
                /* creating port object, transparent until setup with hdlc */
@@ -456,6 +436,10 @@ static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
                pgsm_ms->setup_ind(msg_type, callref, mncc);
                break;
 
+               case MNCC_CALL_PROC_IND:
+               pgsm_ms->call_proc_ind(msg_type, callref, mncc);
+               break;
+
                case MNCC_ALERT_IND:
                pgsm_ms->alert_ind(msg_type, callref, mncc);
                break;
@@ -482,6 +466,12 @@ static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
                pgsm_ms->notify_ind(msg_type, callref, mncc);
                break;
 
+               case MNCC_START_DTMF_RSP:
+               case MNCC_START_DTMF_REJ:
+               case MNCC_STOP_DTMF_RSP:
+               pgsm_ms->dtmf_statemachine(mncc);
+               break;
+
                default:
                ;
        }
@@ -503,6 +493,20 @@ void Pgsm_ms::message_setup(unsigned int epoint_id, int message_id, union parame
        memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
        memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
 
+       /* no instance */
+       if (!p_m_g_lcr_gsm || p_m_g_lcr_gsm->mncc_lfd.fd < 0) {
+               gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
+               add_trace("failure", NULL, "MS %s instance is unavailable", p_m_mISDNport->ifport->gsm_ms_name);
+               end_trace();
+               message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
+               message->param.disconnectinfo.cause = 41;
+               message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
+               message_put(message);
+               new_state(PORT_STATE_RELEASE);
+               trigger_work(&p_m_g_delete);
+               return;
+       }
+       
        /* no number */
        if (!p_dialinginfo.id[0]) {
                gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
@@ -617,10 +621,17 @@ void Pgsm_ms::message_setup(unsigned int epoint_id, int message_id, union parame
                        mncc->bearer_cap.mode = 0;
                        break;
                }
+               /* DTMF supported */
+               mncc->fields |= MNCC_F_CCCAP;
+               mncc->cccap.dtmf = 1;
+
+               /* request keypad from remote */
+               message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ENABLEKEYPAD);
+               message_put(message);
        }
 
        end_trace();
-       send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
+       send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
 
        new_state(PORT_STATE_OUT_SETUP);
 
@@ -630,11 +641,119 @@ void Pgsm_ms::message_setup(unsigned int epoint_id, int message_id, union parame
        new_state(PORT_STATE_OUT_PROCEEDING);
 }
 
+void Pgsm_ms::dtmf_statemachine(struct gsm_mncc *mncc)
+{
+       struct gsm_mncc *dtmf;
+
+       switch (p_m_g_dtmf_state) {
+       case DTMF_ST_SPACE:
+       case DTMF_ST_IDLE:
+               /* end of string */
+               if (!p_m_g_dtmf[p_m_g_dtmf_index]) {
+                       PDEBUG(DEBUG_GSM, "done with DTMF\n");
+                       p_m_g_dtmf_state = DTMF_ST_IDLE;
+                       return;
+               }
+               gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_REQ, DIRECTION_OUT);
+               dtmf = create_mncc(MNCC_START_DTMF_REQ, p_m_g_callref);
+               dtmf->keypad = p_m_g_dtmf[p_m_g_dtmf_index++];
+               p_m_g_dtmf_state = DTMF_ST_START;
+               PDEBUG(DEBUG_GSM, "start DTMF (keypad %c)\n",
+                       dtmf->keypad);
+               end_trace();
+               send_and_free_mncc(p_m_g_lcr_gsm, dtmf->msg_type, dtmf);
+               return;
+       case DTMF_ST_START:
+               if (mncc->msg_type != MNCC_START_DTMF_RSP) {
+                       PDEBUG(DEBUG_GSM, "DTMF was rejected\n");
+                       return;
+               }
+               schedule_timer(&p_m_g_dtmf_timer, 0, 70 * 1000);
+               p_m_g_dtmf_state = DTMF_ST_MARK;
+               PDEBUG(DEBUG_GSM, "DTMF is on\n");
+               break;
+       case DTMF_ST_MARK:
+               gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_REQ, DIRECTION_OUT);
+               dtmf = create_mncc(MNCC_STOP_DTMF_REQ, p_m_g_callref);
+               p_m_g_dtmf_state = DTMF_ST_STOP;
+               end_trace();
+               send_and_free_mncc(p_m_g_lcr_gsm, dtmf->msg_type, dtmf);
+               return;
+       case DTMF_ST_STOP:
+               schedule_timer(&p_m_g_dtmf_timer, 0, 120 * 1000);
+               p_m_g_dtmf_state = DTMF_ST_SPACE;
+               PDEBUG(DEBUG_GSM, "DTMF is off\n");
+               break;
+       }
+}
+
+static int dtmf_timeout(struct lcr_timer *timer, void *instance, int index)
+{
+       class Pgsm_ms *pgsm_ms = (class Pgsm_ms *)instance;
+
+       PDEBUG(DEBUG_GSM, "DTMF timer has fired\n");
+       pgsm_ms->dtmf_statemachine(NULL);
+
+       return 0;
+}
+
+/* MESSAGE_DTMF */
+void Pgsm_ms::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
+{
+       char digit = param->dtmf;
+
+       if (digit >= 'a' && digit <= 'c')
+               digit = digit - 'a' + 'A';
+       if (!strchr("01234567890*#ABC", digit))
+               return;
+
+       /* schedule */
+       if (p_m_g_dtmf_state == DTMF_ST_IDLE) {
+               p_m_g_dtmf_index = 0;
+               p_m_g_dtmf[0] = '\0';
+       }
+       SCCAT(p_m_g_dtmf, digit);
+       if (p_m_g_dtmf_state == DTMF_ST_IDLE)
+               dtmf_statemachine(NULL);
+}
+
+/* MESSAGE_INFORMATION */
+void Pgsm_ms::message_information(unsigned int epoint_id, int message_id, union parameter *param)
+{
+       char digit;
+       int i;
+
+       for (i = 0; i < (int)strlen(param->information.id); i++) {
+               digit = param->information.id[i];
+               if (digit >= 'a' && digit <= 'c')
+                       digit = digit - 'a' + 'A';
+               if (!strchr("01234567890*#ABC", digit))
+                       continue;
+
+               /* schedule */
+               if (p_m_g_dtmf_state == DTMF_ST_IDLE) {
+                       p_m_g_dtmf_index = 0;
+                       p_m_g_dtmf[0] = '\0';
+               }
+               SCCAT(p_m_g_dtmf, digit);
+               if (p_m_g_dtmf_state == DTMF_ST_IDLE)
+                       dtmf_statemachine(NULL);
+       }
+}
+
 /*
  * endpoint sends messages to the port
  */
 int Pgsm_ms::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
 {
+       struct lcr_msg *message;
+
+       if (message_id == MESSAGE_CONNECT) {
+               /* request keypad from remote */
+               message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ENABLEKEYPAD);
+               message_put(message);
+       }
+
        if (Pgsm::message_epoint(epoint_id, message_id, param))
                return(1);
 
@@ -645,6 +764,14 @@ int Pgsm_ms::message_epoint(unsigned int epoint_id, int message_id, union parame
                message_setup(epoint_id, message_id, param);
                break;
 
+               case MESSAGE_DTMF:
+               message_dtmf(epoint_id, message_id, param);
+               break;
+
+               case MESSAGE_INFORMATION:
+               message_information(epoint_id, message_id, param);
+               break;
+
                default:
                PDEBUG(DEBUG_GSM, "Pgsm_ms(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
        }
@@ -654,101 +781,108 @@ int Pgsm_ms::message_epoint(unsigned int epoint_id, int message_id, union parame
 
 int gsm_ms_exit(int rc)
 {
+       /* destroy all instances */
+       while (gsm_ms_first)
+               gsm_ms_delete(gsm_ms_first->name);
 
-       return(rc);
+       return rc;
 }
 
 int gsm_ms_init(void)
 {
-       INIT_LLIST_HEAD(&ms_list);
-       log_init(&log_info);
-       stderr_target = log_target_create_stderr();
-       log_add_target(stderr_target);
-       log_set_all_filter(stderr_target, 1);
-
-       l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
-
        return 0;
 }
 
 /* add a new GSM mobile instance */
-int gsm_ms_new(const char *name, const char *socket_path)
+int gsm_ms_new(const char *name)
 {
-       struct osmocom_ms *ms = NULL;
-       int rc;
-
-       PDEBUG(DEBUG_GSM, "GSM: creating new instance '%s' on '%s'\n", name, socket_path);
+       struct lcr_gsm *gsm_ms = gsm_ms_first, **gsm_ms_p = &gsm_ms_first;
 
-       ms = talloc_zero(l23_ctx, struct osmocom_ms);
-       if (!ms) {
-               FATAL("Failed to allocate MS\n");
+       while (gsm_ms) {
+               gsm_ms_p = &gsm_ms->gsm_ms_next;
+               gsm_ms = gsm_ms->gsm_ms_next;
        }
-       /* must add here, because other init processes may search in the list */
-       llist_add_tail(&ms->entity, &ms_list);
 
+       PDEBUG(DEBUG_GSM, "GSM: interface for MS '%s' is created\n", name);
 
-       SPRINT(ms->name, name);
+       /* create gsm instance */
+       gsm_ms = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
 
-       rc = layer2_open(ms, socket_path);
-       if (rc < 0) {
-               FATAL("Failed during layer2_open()\n");
-       }
+       gsm_ms->type = LCR_GSM_TYPE_MS;
+       SCPY(gsm_ms->name, name);
+       gsm_ms->sun.sun_family = AF_UNIX;
+       SPRINT(gsm_ms->sun.sun_path, "/tmp/ms_mncc_%s", name);
 
-       lapdm_init(&ms->l2_entity.lapdm_dcch, ms);
-       lapdm_init(&ms->l2_entity.lapdm_acch, ms);
+       memset(&gsm_ms->socket_retry, 0, sizeof(gsm_ms->socket_retry));
+       add_timer(&gsm_ms->socket_retry, mncc_socket_retry_cb, gsm_ms, 0);
 
-       rc = l23_app_init(ms);
-       if (rc < 0) {
-               FATAL("Failed to init layer23\n");
-       }
-       ms->cclayer.mncc_recv = message_ms;
+       /* do the initial connect */
+       mncc_socket_retry_cb(&gsm_ms->socket_retry, gsm_ms, 0);
+
+       *gsm_ms_p = gsm_ms;
 
        return 0;
 }
 
 int gsm_ms_delete(const char *name)
 {
-       struct osmocom_ms *ms = NULL;
-       int found = 0;
+       struct lcr_gsm *gsm_ms = gsm_ms_first, **gsm_ms_p = &gsm_ms_first;
+//     class Port *port;
+//     class Pgsm_ms *pgsm_ms = NULL;
 
-       PDEBUG(DEBUG_GSM, "GSM: destroying instance '%s'\n", name);
+       PDEBUG(DEBUG_GSM, "GSM: interface for MS '%s' is deleted\n", name);
 
-       llist_for_each_entry(ms, &ms_list, entity) {
-               if (!strcmp(ms->name, name)) {
-                       found = 1;
+       while (gsm_ms) {
+               if (gsm_ms->type == LCR_GSM_TYPE_MS && !strcmp(gsm_ms->name, name))
                        break;
-               }
+               gsm_ms_p = &gsm_ms->gsm_ms_next;
+               gsm_ms = gsm_ms->gsm_ms_next;
        }
 
-       if (!found) {
-               FATAL("Failed delete layer23, instance '%s' not found\n", name);
+       if (!gsm_ms)
+               return 0;
+
+/* not needed, because:
+ * - shutdown of interface will destry port instances locally
+ * - closing of socket will make remote socket destroy calls locally
+ */
+#if 0
+       port = port_first;
+       while(port) {
+               if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_MS) {
+                       pgsm_ms = (class Pgsm_ms *)port;
+                       if (pgsm_ms->p_m_g_lcr_gsm == gsm_ms && pgsm_ms->p_m_g_callref) {
+                               struct gsm_mncc *rej;
+
+                               rej = create_mncc(MNCC_REL_REQ, pgsm_ms->p_m_g_callref);
+                               rej->fields |= MNCC_F_CAUSE;
+                               rej->cause.coding = 3;
+                               rej->cause.location = 1;
+                               rej->cause.value = 27;
+                               gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
+                               add_trace("cause", "coding", "%d", rej->cause.coding);
+                               add_trace("cause", "location", "%d", rej->cause.location);
+                               add_trace("cause", "value", "%d", rej->cause.value);
+                               end_trace();
+                               send_and_free_mncc(gsm_ms, rej->msg_type, rej);
+                               pgsm_ms->new_state(PORT_STATE_RELEASE);
+                               trigger_work(&pgsm_ms->p_m_g_delete);
+                       }
+               }
        }
+#endif
 
-       l23_app_exit(ms);
-       lapdm_exit(&ms->l2_entity.lapdm_dcch);
-       lapdm_exit(&ms->l2_entity.lapdm_acch);
+       if (gsm_ms->mncc_lfd.fd > -1) {
+               close(gsm_ms->mncc_lfd.fd);
+               unregister_fd(&gsm_ms->mncc_lfd);
+       }
+       del_timer(&gsm_ms->socket_retry);
 
-       llist_del(&ms->entity);
+       /* remove instance from list */
+       *gsm_ms_p = gsm_ms->gsm_ms_next;
+       FREE(gsm_ms, sizeof(struct lcr_gsm));
 
        return 0;
 }
 
-/*
- * handles bsc select function within LCR's main loop
- */
-int handle_gsm_ms(void)
-{
-       struct osmocom_ms *ms = NULL;
-       int work = 0;
-
-       llist_for_each_entry(ms, &ms_list, entity) {
-               if (l23_app_work(ms))
-                       work = 1;
-//             debug_reset_context();
-               if (bsc_select_main(1)) /* polling */
-                       work = 1;
-       }
-
-       return work;
-}