Tested and fixed the new OpenBSC API.
[lcr.git] / mISDN.cpp
index 035b78b..ee50bf1 100644 (file)
--- a/mISDN.cpp
+++ b/mISDN.cpp
 #include "main.h"
 #include "myisdn.h"
 
-extern "C" {
-#define MISDN_OLD_AF_COMPATIBILITY 1
-#include <compat_af_isdn.h>
-}
-#include <q931.h>
+#include <mISDN/mISDNcompat.h>
+int __af_isdn = MISDN_AF_ISDN;
+#include <mISDN/q931.h>
 
 #undef offsetof
 #ifdef __compiler_offsetof
@@ -35,6 +33,21 @@ extern "C" {
 #define B_TIMER_ACTIVATING 1 // seconds
 #define B_TIMER_DEACTIVATING 1 // seconds
 
+/* GSM condition */
+#if defined WITH_GSM_BS && defined WITH_GSM_MS
+ #define MISDNPORT_GSM (mISDNport->gsm_bs || mISDNport->gsm_ms)
+#else
+ #ifdef WITH_GSM_BS
+  #define MISDNPORT_GSM (mISDNport->gsm_bs)
+ #endif
+ #ifdef WITH_GSM_MS
+  #define MISDNPORT_GSM (mISDNport->gsm_ms)
+ #endif
+#endif
+#ifndef MISDNPORT_GSM
+ #define MISDNPORT_GSM (0)
+#endif
+
 /* list of mISDN ports */
 struct mISDNport *mISDNport_first;
 
@@ -56,12 +69,10 @@ int mISDN_initialize(void)
 {
        char filename[256];
 
-       init_af_isdn();
-
        /* try to open raw socket to check kernel */
        mISDNsocket = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
        if (mISDNsocket < 0) {
-               fprintf(stderr, "Cannot open mISDN due to '%s'. (Does your Kernel support socket based mISDN?)\n", strerror(errno));
+               fprintf(stderr, "Cannot open mISDN due to '%s'. (Does your Kernel support socket based mISDN? Protocol family is %d.)\n", strerror(errno), PF_ISDN);
                return(-1);
        }
 
@@ -82,7 +93,7 @@ int mISDN_initialize(void)
 
        if (pipe(upqueue_pipe) < 0)
                FATAL("Failed to open pipe\n");
-       memset(&upqueue_fd, 0, sizeof(upqueue_fd.fd));
+       memset(&upqueue_fd, 0, sizeof(upqueue_fd));
        upqueue_fd.fd = upqueue_pipe[0];
        register_fd(&upqueue_fd, LCR_FD_READ, mISDN_upqueue, NULL, 0);
 
@@ -1832,19 +1843,20 @@ static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, i
        struct mbuffer *mb;
        struct l3_msg *l3m;
        char byte;
+       int ret;
 
        /* unset global semaphore */
        upqueue_avail = 0;
        // with a very small incident, upqueue_avail may be set by mISDN thread and
        // another byte may be sent to the pipe, which causes a call to this function
        // again with nothing in the upqueue. this is no problem.
-       read(fd->fd, &byte, 1);
+       ret = read(fd->fd, &byte, 1);
 
        /* process all ports */
        mISDNport = mISDNport_first;
        while(mISDNport) {
                /* handle queued up-messages (d-channel) */
-               if (!mISDNport->gsm) {
+               if (!MISDNPORT_GSM) {
                        while ((mb = mdequeue(&mISDNport->upqueue))) {
                                l3m = &mb->l3;
                                switch(l3m->type) {
@@ -1920,7 +1932,7 @@ static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, i
                                                if (!mISDNport->ntmode || mISDNport->ptp)
                                                        mISDNport->l2link = 0;
                                        }
-                                       if (!mISDNport->gsm && (!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
+                                       if (!MISDNPORT_GSM && (!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
                                                if (!mISDNport->l2establish.active && mISDNport->l2hold) {
                                                        PDEBUG(DEBUG_ISDN, "set timer and establish.\n");
                                                        schedule_timer(&mISDNport->l2establish, 5, 0);
@@ -1947,7 +1959,7 @@ static int l2establish_timeout(struct lcr_timer *timer, void *instance, int i)
 {
        struct mISDNport *mISDNport = (struct mISDNport *)instance;
 
-       if (!mISDNport->gsm && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
+       if (!MISDNPORT_GSM && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
 //             PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link portnum=%d.\n", mISDNport->portnum);
                mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
                schedule_timer(&mISDNport->l2establish, 5, 0); /* 5 seconds */
@@ -2068,7 +2080,8 @@ int do_layer3(struct mlayer3 *ml3, unsigned int cmd, unsigned int pid, struct l3
                // this is no problem.
                upqueue_avail = 1;
                char byte = 0;
-               write(upqueue_pipe[1], &byte, 1);
+               int ret;
+               ret = write(upqueue_pipe[1], &byte, 1);
        }
        return 0;
 }
@@ -2106,7 +2119,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        int force_nt = ifport->nt;
        int l1hold = ifport->l1hold;
        int l2hold = ifport->l2hold;
-       int gsm = ifport->gsm;
+       int gsm = 0;
        int ss5 = ifport->ss5;
        int i, cnt;
        int pri, bri, pots;
@@ -2115,6 +2128,17 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        struct mISDN_devinfo devinfo;
        unsigned int protocol, prop;
 
+#if defined WITH_GSM_BS && defined WITH_GSM_MS
+       gsm = ifport->gsm_ms | ifport->gsm_bs;
+#else
+#ifdef WITH_GSM_BS
+       gsm = ifport->gsm_bs;
+#endif
+#ifdef WITH_GSM_MS
+       gsm = ifport->gsm_ms;
+#endif
+#endif
+
        /* check port counts */
        ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
        if (ret < 0) {
@@ -2254,7 +2278,12 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
                mISDNport->l1link = -1;
                mISDNport->l2link = -1;
        }
-       mISDNport->gsm = gsm;
+#ifdef WITH_GSM_BS
+       mISDNport->gsm_bs = ifport->gsm_bs;
+#endif
+#ifdef WITH_GSM_MS
+       mISDNport->gsm_ms = ifport->gsm_ms;
+#endif
        pmemuse++;
        *mISDNportp = mISDNport;
 
@@ -2297,6 +2326,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
               prop |= (1 << MISDN_FLG_L2_HOLD);
        /* open layer 3 and init upqueue */
        if (gsm) {
+#if defined WITH_GSM_BS || defined WITH_GSM_MS
                unsigned long on = 1;
                struct sockaddr_mISDN addr;
 
@@ -2327,6 +2357,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
                        mISDNport_close(mISDNport);
                        return(NULL);
                }
+#endif
        } else {
                /* queue must be initializes, because l3-thread may send messages during open_layer3() */
                mqueue_init(&mISDNport->upqueue);
@@ -2367,7 +2398,7 @@ struct mISDNport *mISDNport_open(struct interface_port *ifport)
        }
 
        /* if ptp, pull up the link */
-       if (!mISDNport->gsm && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
+       if (!MISDNPORT_GSM && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
                mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
                l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_REQ, DIRECTION_OUT);
                add_trace("tei", NULL, "%d", 0);
@@ -2480,17 +2511,19 @@ void mISDNport_close(struct mISDNport *mISDNport)
        del_timer(&mISDNport->l2establish);
 
        /* close layer 3, if open */
-       if (!mISDNport->gsm && mISDNport->ml3) {
+       if (!MISDNPORT_GSM && mISDNport->ml3) {
                close_layer3(mISDNport->ml3);
        }
 
+#if defined WITH_GSM_BS || defined WITH_GSM_MS
        /* close gsm socket, if open */
-       if (mISDNport->gsm && mISDNport->lcr_sock > -1) {
+       if (MISDNPORT_GSM && mISDNport->lcr_sock > -1) {
                close(mISDNport->lcr_sock);
        }
+#endif
 
        /* purge upqueue */
-       if (!mISDNport->gsm)
+       if (!MISDNPORT_GSM)
                mqueue_purge(&mISDNport->upqueue);
 
        /* remove from list */