Adding basic SIP support, using Sofia-SIP stack
[lcr.git] / main.c
diff --git a/main.c b/main.c
index 1aa2d8b..07cfb8f 100644 (file)
--- a/main.c
+++ b/main.c
 \*****************************************************************************/ 
 
 #include "main.h"
-#include "config.h"
-#ifdef WITH_GSM_MS
-extern "C" {
-#include <osmocore/signal.h>
-}
+#ifdef PACKAGE_VERSION
+#undef PACKAGE_VERSION
 #endif
+#include "config.h"
 
 //MESSAGES
 
@@ -86,6 +84,7 @@ void debug(const char *function, int line, const char *prefix, char *buffer)
                                fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
                        else
                                fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
+                       fflush(debug_fp);
                }
        }
 
@@ -143,9 +142,6 @@ void _printerror(const char *function, int line, const char *fmt, ...)
 void sighandler(int sigset)
 {
        struct sched_param schedp;
-#ifdef WITH_GSM_MS
-       int wait_ms = 0;
-#endif
 
        if (sigset == SIGHUP)
                return;
@@ -153,13 +149,11 @@ void sighandler(int sigset)
                return;
        fprintf(stderr, "LCR: Signal received: %d\n", sigset);
        PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
-#ifdef WITH_GSM_MS
-       if (!wait_ms) {
-               wait_ms = 1;
-               dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
-               return;
-       }
-#endif
+       /* reset signals */
+       signal(SIGINT,SIG_DFL);
+       signal(SIGHUP,SIG_DFL);
+       signal(SIGTERM,SIG_DFL);
+       signal(SIGPIPE,SIG_DFL);
        if (!quit) {
                quit = sigset;
                /* set scheduler & priority */
@@ -177,7 +171,7 @@ void sighandler(int sigset)
  */
 int main(int argc, char *argv[])
 {
-#if defined WITH_GSM_BS || defined WITH_GSM_MS
+#if 0
        double                  now_d, last_d;
        int                     all_idle;
 #endif
@@ -191,6 +185,7 @@ int main(int argc, char *argv[])
                                created_misdn = 0;
        char                    tracetext[256], lock[128];
        char                    options_error[256];
+       int                     polling = 0;
 
 #if 0
        /* init fdset */
@@ -271,6 +266,7 @@ int main(int argc, char *argv[])
                PERROR("%s", options_error);
                goto free;
        }
+       polling = options.polling;
 
        /* init mISDN */
        if (mISDN_initialize() < 0)
@@ -366,6 +362,12 @@ int main(int argc, char *argv[])
        /* generate alaw / ulaw tables */
        generate_tables(options.law);
 
+#ifdef WITH_SIP
+       /* init SIP globals */
+       sip_init();
+       polling = 1; /* must poll, because of SIP events */
+#endif
+
 #ifdef WITH_SS5
        /* init ss5 sine tables */
        ss5_sine_generate();
@@ -380,24 +382,22 @@ int main(int argc, char *argv[])
 
 #if defined WITH_GSM_BS || defined WITH_GSM_MS
        /* init gsm */
-       if (options.gsm && gsm_init()) {
+       if (gsm_init()) {
                fprintf(stderr, "GSM initialization failed.\n");
                goto free;
        }
-#else
-       if (options.gsm) {
-               fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm-bs or --with-gsm-ms while configure!\n");
-               goto free;
-       }
 #endif
+#if 0
+init is done when interface is up
 #ifdef WITH_GSM_BS
-       if (options.gsm && gsm_bs_init()) {
+       if (gsm_bs_init()) {
                fprintf(stderr, "GSM BS initialization failed.\n");
                goto free;
        }
 #endif
+#endif
 #ifdef WITH_GSM_MS
-       if (options.gsm && gsm_ms_init()) {
+       if (gsm_ms_init()) {
                fprintf(stderr, "GSM MS initialization failed.\n");
                goto free;
        }
@@ -464,11 +464,11 @@ int main(int argc, char *argv[])
        printf("%s\n", tracetext);
        end_trace();
        quit = 0;
-#if defined WITH_GSM_BS || defined WITH_GSM_MS
+#if 0
        GET_NOW();
 #endif
        while(!quit) {
-#if defined WITH_GSM_BS || defined WITH_GSM_MS
+#if 0
                last_d = now_d;
                GET_NOW();
                if (now_d-last_d > 1.0) {
@@ -480,23 +480,19 @@ int main(int argc, char *argv[])
                /* must be processed after all queues, so they are empty */
                if (select_main(1, NULL, NULL, NULL))
                        all_idle = 0;
-               /* handle gsm */
-               if (options.gsm) {
-                       if (handle_gsm())
-                               all_idle = 0;
-#ifdef WITH_GSM_MS
-                       if (handle_gsm_ms(&quit))
-                               all_idle = 0;
-#endif
-               }
                if (all_idle) {
                        usleep(10000);
                }
 #else
-               if (options.polling)
-                       if (!select_main(1, NULL, NULL, NULL))
+               if (options.polling) {
+                       if (!select_main(1, NULL, NULL, NULL)) {
+#ifdef WITH_SIP
+                               /* FIXME: check if work was done */
+                               sip_handle();
+#endif
                                usleep(10000);
-               else
+                       }
+               } else
                        select_main(0, NULL, NULL, NULL);
 #endif
        }
@@ -601,17 +597,23 @@ free:
                mISDN_deinitialize();
 
        /* free gsm */
-       if (options.gsm) {
+#if 0
+exit is done when interface is down
 #ifdef WITH_GSM_BS
-               gsm_bs_exit(0);
+       gsm_bs_exit(0);
+#endif
 #endif
 #ifdef WITH_GSM_MS
-               gsm_ms_exit(0);
+       gsm_ms_exit(0);
 #endif
 #if defined WITH_GSM_BS || defined WITH_GSM_MS
-               gsm_exit(0);
+       gsm_exit(0);
+#endif
+
+#ifdef WITH_SIP
+       /* cleanup SIP globals */
+       sip_exit();
 #endif
-       }
 
        /* close loopback, if used by GSM or remote */
        if (mISDNloop.sock > -1)