Add FXS support
[lcr.git] / interface.c
index 3c1a792..b9dd275 100644 (file)
@@ -15,6 +15,7 @@ struct interface *interface_first = NULL; /* first interface is current list */
 struct interface *interface_newlist = NULL; /* first interface in new list */
 
 
+#ifdef WITH_MISDN
 /* set default out_channel */
 void default_out_channel(struct interface_port *ifport)
 {
@@ -22,7 +23,7 @@ void default_out_channel(struct interface_port *ifport)
 
        selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
        memuse++;
-       
+
        if (ifport->mISDNport->ntmode)
                selchannel->channel = CHANNEL_FREE;
        else
@@ -53,6 +54,7 @@ void default_in_channel(struct interface_port *ifport)
        
        ifport->in_channel = selchannel;
 }
+#endif
 
 
 /* parse string for a positive number */
@@ -278,6 +280,10 @@ static int inter_port(struct interface *interface, char *filename, int line, cha
 }
 static int inter_portnum(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
+#ifndef WITH_MISDN
+       SPRINT(interface_error, "Error in %s (line %d): mISDN support is not compiled in.\n", filename, line);
+       return(-1);
+#else
        struct interface_port *ifport, **ifportp;
        struct interface *searchif;
        int val;
@@ -312,11 +318,15 @@ static int inter_portnum(struct interface *interface, char *filename, int line,
                ifportp = &((*ifportp)->next);
        *ifportp = ifport;
        return(0);
+#endif
 }
 static int inter_portname(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
+#ifndef WITH_MISDN
+       SPRINT(interface_error, "Error in %s (line %d): mISDN support is not compiled in.\n", filename, line);
+       return(-1);
+#else
        struct interface_port *ifport, **ifportp;
-       struct interface *searchif;
 
        /* goto end of chain */
        ifport = interface->ifport;
@@ -325,22 +335,6 @@ static int inter_portname(struct interface *interface, char *filename, int line,
                        ifport = ifport->next;
        }
 
-       /* check for port already assigned, but not for shared loop interface */
-       searchif = interface_newlist;
-       if (!!strcmp(value, options.loopback_lcr))
-       {
-               while(searchif) {
-                       ifport = searchif->ifport;
-                       while(ifport) {
-                               if (!strcasecmp(ifport->portname, value)) {
-                                       SPRINT(interface_error, "Error in %s (line %d): port '%s' already used above.\n", filename, line, value);
-                                       return(-1);
-                               }
-                               ifport = ifport->next;
-                       }
-                       searchif = searchif->next;
-               }
-       }
        /* alloc port substructure */
        ifport = (struct interface_port *)MALLOC(sizeof(struct interface_port));
        memuse++;
@@ -354,6 +348,7 @@ static int inter_portname(struct interface *interface, char *filename, int line,
                ifportp = &((*ifportp)->next);
        *ifportp = ifport;
        return(0);
+#endif
 }
 static int inter_l1hold(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
@@ -894,31 +889,19 @@ static int inter_gsm_bs(struct interface *interface, char *filename, int line, c
        SPRINT(interface_error, "Error in %s (line %d): GSM BS side not compiled in.\n", filename, line);
        return(-1);
 #else
-       struct interface_port *ifport;
        struct interface *searchif;
 
        searchif = interface_newlist;
        while(searchif) {
-               ifport = searchif->ifport;
-               while(ifport) {
-                       if (ifport->gsm_bs) {
-                               SPRINT(interface_error, "Error in %s (line %d): port '%s' already uses gsm BS side.\n", filename, line, ifport->portname);
-                               return(-1);
-                       }
-                       ifport = ifport->next;
+               if (searchif->gsm_bs) {
+                       SPRINT(interface_error, "Error in %s (line %d): interface '%s' already uses gsm BS side.\n", filename, line, searchif->name);
+                       return(-1);
                }
                searchif = searchif->next;
        }
 
-       /* set portname */
-       if (inter_portname(interface, filename, line, (char *)"portname", options.loopback_lcr))
-               return(-1);
-
        /* goto end of chain again to set gsmflag */
-       ifport = interface->ifport;
-       while(ifport->next)
-               ifport = ifport->next;
-       ifport->gsm_bs = 1;
+       interface->gsm_bs = 1;
 
        return(0);
 #endif
@@ -929,37 +912,23 @@ static int inter_gsm_ms(struct interface *interface, char *filename, int line, c
        SPRINT(interface_error, "Error in %s (line %d): GSM MS side not compiled in.\n", filename, line);
        return(-1);
 #else
-       struct interface_port *ifport, *searchifport;
        struct interface *searchif;
 
-       /* set portname */
-       if (inter_portname(interface, filename, line, (char *)"portname", options.loopback_lcr))
-               return(-1);
-
-       /* goto end of chain again to set gsmflag and socket */
-       ifport = interface->ifport;
-       while(ifport->next)
-               ifport = ifport->next;
-       ifport->gsm_ms = 1;
+       interface->gsm_ms = 1;
 
        /* copy values */
        if (!value || !value[0]) {
                SPRINT(interface_error, "Error in %s (line %d): Missing MS name and socket name.\n", filename, line);
                return(-1);
        }
-       SCPY(ifport->gsm_ms_name, value);
+       SCPY(interface->gsm_ms_name, value);
 
        /* check if name is used multiple times */
        searchif = interface_newlist;
        while(searchif) {
-               searchifport = searchif->ifport;
-               while(searchifport) {
-                       if (searchifport != ifport 
-                        && !strcmp(searchifport->gsm_ms_name, ifport->gsm_ms_name)) {
-                               SPRINT(interface_error, "Error in %s (line %d): mobile '%s' already uses the given MS name '%s', choose a different one.\n", filename, line, ifport->gsm_ms_name, searchifport->gsm_ms_name);
-                               return(-1);
-                       }
-                       searchifport = searchifport->next;
+               if (searchif != interface && !strcmp(searchif->gsm_ms_name, interface->gsm_ms_name)) {
+                       SPRINT(interface_error, "Error in %s (line %d): mobile '%s' already uses the given MS name '%s', choose a different one.\n", filename, line, interface->gsm_ms_name, searchif->gsm_ms_name);
+                       return(-1);
                }
                searchif = searchif->next;
        }
@@ -967,6 +936,81 @@ static int inter_gsm_ms(struct interface *interface, char *filename, int line, c
        return(0);
 #endif
 }
+static int inter_sip(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+#ifndef WITH_SIP
+       SPRINT(interface_error, "Error in %s (line %d): SIP not compiled in.\n", filename, line);
+       return(-1);
+#else
+       char *p;
+
+       interface->sip = 1;
+
+       /* copy values */
+       if (!value || !value[0]) {
+               SPRINT(interface_error, "Error in %s (line %d): Missing SIP local IP.\n", filename, line);
+               return(-1);
+       }
+       p = get_seperated(value);
+       if (!p[0]) {
+               SPRINT(interface_error, "Error in %s (line %d): Missing SIP remote IP.\n", filename, line);
+               return(-1);
+       }
+       SCPY(interface->sip_local_peer, value);
+       SCPY(interface->sip_remote_peer, p);
+
+       return(0);
+#endif
+}
+static int inter_rtp_bridge(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+       int supported = 0;
+
+#ifdef WITH_GSM_BS
+       if (interface->gsm_bs)
+               supported = 1;
+#endif
+#ifdef WITH_SIP
+       if (interface->sip)
+               supported = 1;
+#endif
+       if (!supported) {
+               SPRINT(interface_error, "Error in %s (line %d): Interface does not support RTP\n", filename, line);
+               return(-1);
+       }
+       interface->rtp_bridge = 1;
+
+       return(0);
+}
+#if 0
+static int inter_rtp_payload(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+#ifndef WITH_GSM_BS
+       SPRINT(interface_error, "Error in %s (line %d): GSM BS side not compiled in.\n", filename, line);
+       return(-1);
+#else
+       if (!interface->gsm_bs) {
+               SPRINT(interface_error, "Error in %s (line %d): This parameter only works for GSM BS side interface\n", filename, line);
+               return(-1);
+       }
+       if (!interface->rtp_bridge) {
+               SPRINT(interface_error, "Error in %s (line %d): This parameter only works here, if RTP bridging is enabled\n", filename, line);
+               return(-1);
+       }
+       if (!value[0]) {
+               SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects one payload type\n", filename, line, parameter);
+               return(-1);
+       }
+       if (interface->gsm_bs_payloads == sizeof(interface->gsm_bs_payload_types)) {
+               SPRINT(interface_error, "Error in %s (line %d): Too many payload types defined\n", filename, line);
+               return(-1);
+       }
+       interface->gsm_bs_payload_types[interface->gsm_bs_payloads++] = atoi(value);
+
+       return(0);
+#endif
+}
+#endif
 static int inter_nonotify(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
        struct interface_port *ifport;
@@ -1035,7 +1079,6 @@ static int inter_ss5(struct interface *interface, char *filename, int line, char
 #endif
 static int inter_remote(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
-       struct interface_port *ifport;
        struct interface *searchif;
 
        if (!value[0]) {
@@ -1044,38 +1087,97 @@ static int inter_remote(struct interface *interface, char *filename, int line, c
        }
        searchif = interface_newlist;
        while(searchif) {
-               ifport = searchif->ifport;
-               while(ifport) {
-                       if (ifport->remote && !strcmp(ifport->remote_app, value)) {
-                               SPRINT(interface_error, "Error in %s (line %d): port '%s' already uses remote application '%s'.\n", filename, line, ifport->portname, value);
-                               return(-1);
-                       }
-                       ifport = ifport->next;
+               if (interface->remote && !strcmp(interface->remote_app, value)) {
+                       SPRINT(interface_error, "Error in %s (line %d): interface '%s' already uses remote application '%s'.\n", filename, line, interface->name, value);
+                       return(-1);
                }
                searchif = searchif->next;
        }
 
-       /* set portname */
-       if (inter_portname(interface, filename, line, (char *)"portname", options.loopback_lcr))
+       /* goto end of chain again to set application name */
+       interface->remote = 1;
+       SCPY(interface->remote_app, value);
+
+       return(0);
+}
+static int inter_context(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+       if (!value[0]) {
+               SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects application context as value.\n", filename, line, parameter);
                return(-1);
+       }
+       SCPY(interface->remote_context, value);
 
-       /* goto end of chain again to set application name */
+       return(0);
+}
+static int inter_pots_flash(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+       struct interface_port *ifport;
+
+       /* port in chain ? */
+       if (!interface->ifport) {
+               SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
+               return(-1);
+       }
+       /* goto end of chain */
        ifport = interface->ifport;
        while(ifport->next)
                ifport = ifport->next;
-       ifport->remote = 1;
-       SCPY(ifport->remote_app, value);
 
+       ifport->pots_flash = 1;
+       return(0);
+}
+static int inter_pots_ring(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+       struct interface_port *ifport;
+
+       /* port in chain ? */
+       if (!interface->ifport) {
+               SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
+               return(-1);
+       }
+       /* goto end of chain */
+       ifport = interface->ifport;
+       while(ifport->next)
+               ifport = ifport->next;
 
+       ifport->pots_ring = 1;
        return(0);
 }
+static int inter_pots_transfer(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+       struct interface_port *ifport;
+
+       /* port in chain ? */
+       if (!interface->ifport) {
+               SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
+               return(-1);
+       }
+       /* goto end of chain */
+       ifport = interface->ifport;
+       while(ifport->next)
+               ifport = ifport->next;
 
+       ifport->pots_transfer = 1;
+       return(0);
+}
 static int inter_shutdown(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
        interface->shutdown = 1;
 
        return(0);
 }
+static int inter_bridge(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+       if (!value || !value[0]) {
+               SPRINT(interface_error, "Error in %s (line %d): Missing destination interface name.\n", filename, line);
+               return(-1);
+       }
+       interface->app = EAPP_TYPE_BRIDGE;
+       SCPY(interface->bridge_if, value);
+
+       return(0);
+}
 
 
 /*
@@ -1214,10 +1316,27 @@ struct interface_param interface_param[] = {
        "The name of the MS folows the interface name.\n"
        "The socket is /tmp/osmocom_l2 by default and need to be changed when multiple\n"
        "MS interfaces are used."},
+       {"sip", &inter_sip, "<local IP> <remote IP>",
+       "Sets up SIP interface that represents one SIP endpoint.\n"
+       "Give SIP configuration file."},
+       {"rtp-bridge", &inter_rtp_bridge, "",
+       "Enables RTP bridging directly from this interface.\n"
+       "This only works, if both ends support RTP. (like gsm-bs and sip)"},
+#if 0
+       not needed, since ms defines what is supports and remote (sip) tells what is selected
+       {"rtp-payload", &inter_rtp_payload, "<codec>",
+       "Define RTP payload to use. Only valid in conjuntion with gsm-bs!\n"
+       "If multiple payloads are defined, the first has highest priority.\n"
+       "If none are defined, GSM fullrate V1 (type 3) is assumed.\n"},
+#endif
        {"nonotify", &inter_nonotify, "",
        "Prevents sending notify messages to this interface. A call placed on hold will\n"
        "Not affect the remote end (phone or telcom switch).\n"
        "This parameter must follow a 'port' parameter."},
+       {"bridge", &inter_bridge, "<destination interface>",
+       "Define bridge application for this interface. All calls received on this\n"
+       "interface will be directly bridged to the given destination interface.\n"
+       "There will be no PBX application, nor routing."},
 
 #ifdef WITH_SS5
        {"ccitt5", &inter_ss5, "[<feature> [feature ...]]",
@@ -1238,6 +1357,23 @@ struct interface_param interface_param[] = {
        {"remote", &inter_remote, "<application>",
        "Sets up an interface that communicates with the remote application.\n"
        "Use \"asterisk\" to use chan_lcr as remote application."},
+       {"context", &inter_context, "<context>",
+       "Give context for calls to application."},
+
+       {"pots-flash", &inter_pots_flash, "",
+       "Allow flash button to hold an active call and setup a new call.\n"
+       "Ihis parameter only appies to POTS type of interfaces\n"
+       "This parameter must follow a 'port' parameter.\n"},
+       {"pots-ring-after-hangup", &inter_pots_ring, "",
+       "Allow ringing of last hold call after hangup. Other calls on hold will not be\n"
+       "released.\n"
+       "Ihis parameter only appies to POTS type of interfaces\n"
+       "This parameter must follow a 'port' parameter.\n"},
+       {"pots-transfer-after-hangup", &inter_pots_transfer, "",
+       "If two calls on hold, both are connected after hangup.\n"
+       "If one call is on hold and another one alerting, call on hold is tranfered.\n"
+       "Ihis parameter only appies to POTS type of interfaces\n"
+       "This parameter must follow a 'port' parameter.\n"},
 
        {"shutdown", &inter_shutdown, "",
        "Interface will not be loaded when processing interface.conf"},
@@ -1452,10 +1588,11 @@ void free_interfaces(struct interface *interface)
        }
 }
 
+#ifdef WITH_MISDN
 /*
  * defaults of ports if not specified by config
  */
-static void set_defaults(struct interface_port *ifport)
+static void set_mISDN_defaults(struct interface_port *ifport)
 {
        /* default channel selection list */
        if (!ifport->out_channel)
@@ -1481,6 +1618,7 @@ static void set_defaults(struct interface_port *ifport)
        else
                ifport->mISDNport->locally = 0;
 }
+#endif
 
 
 /*
@@ -1490,24 +1628,77 @@ static void set_defaults(struct interface_port *ifport)
  */
 void relink_interfaces(void)
 {
+#ifdef WITH_MISDN
        struct mISDNport *mISDNport;
-       struct interface *interface;
        struct interface_port *ifport;
+#endif
+       struct interface *interface, *temp, *found;
 
-       /* unlink all mISDNports */
-       mISDNport = mISDNport_first;
-       while(mISDNport) {
-               if (mISDNport->ifport) {
-                       ifport = mISDNport->ifport;
+       interface = interface_first;
+       while(interface) {
+               found = NULL;
+               temp = interface_newlist;
+               while(temp) {
+                       if (!strcmp(temp->name, interface->name))
+                               found = temp;
+                       temp = temp->next;
+               }
+               if (!found) {
 #ifdef WITH_GSM_MS
-                       if (ifport->gsm_ms)
-                               gsm_ms_delete(ifport->gsm_ms_name);
+                       if (interface->gsm_ms)
+                               gsm_ms_delete(interface->gsm_ms_name);
 #endif
 #ifdef WITH_GSM_BS
-                       if (ifport->gsm_bs)
+                       if (interface->gsm_bs)
                                gsm_bs_exit(0);
 #endif
+#ifdef WITH_SIP
+                       if (interface->sip)
+                               sip_exit_inst(interface);
+#endif
+               } else {
+#ifdef WITH_SIP
+                       if (interface->sip) {
+                               /* move sip instance, if we keep interface */
+                               found->sip_inst = interface->sip_inst;
+                               interface->sip_inst = NULL;
+                       }
+#endif
+                       ;
+               }
+               interface = interface->next;
+       }
+
+       interface = interface_newlist;
+       while(interface) {
+               found = NULL;
+               temp = interface_first;
+               while(temp) {
+                       if (!strcmp(temp->name, interface->name))
+                               found = temp;
+                       temp = temp->next;
+               }
+               if (!found) {
+#ifdef WITH_GSM_MS
+                       if (interface->gsm_ms)
+                               gsm_ms_new(interface);
+#endif
+#ifdef WITH_GSM_BS
+                       if (interface->gsm_bs)
+                               gsm_bs_init(interface);
+#endif
+#ifdef WITH_SIP
+                       if (interface->sip)
+                               sip_init_inst(interface);
+#endif
                }
+               interface = interface->next;
+       }
+
+#ifdef WITH_MISDN
+       /* unlink all mISDNports */
+       mISDNport = mISDNport_first;
+       while(mISDNport) {
                mISDNport->ifport = NULL;
                mISDNport = mISDNport->next;
        }
@@ -1525,7 +1716,7 @@ void relink_interfaces(void)
                                        PDEBUG(DEBUG_ISDN, "Port %d:%s relinking!\n", ifport->portnum, ifport->portname);
                                        ifport->mISDNport = mISDNport;
                                        mISDNport->ifport = ifport;
-                                       set_defaults(ifport);
+                                       set_mISDN_defaults(ifport);
                                }
                                mISDNport = mISDNport->next;
                        }
@@ -1554,7 +1745,7 @@ void relink_interfaces(void)
                while(ifport) {
                        if (!ifport->mISDNport) {
                                if (!interface->shutdown) {
-                                       load_port(ifport);
+                                       load_mISDN_port(ifport);
                                } else {
                                        ifport->block = 2;
                                }
@@ -1563,14 +1754,15 @@ void relink_interfaces(void)
                }
                interface = interface->next;
        }
-
+#endif
 }
 
 
+#ifdef WITH_MISDN
 /*
  * load port
  */
-void load_port(struct interface_port *ifport)
+void load_mISDN_port(struct interface_port *ifport)
 {
        struct mISDNport *mISDNport;
 
@@ -1584,21 +1776,14 @@ void load_port(struct interface_port *ifport)
                ifport->portnum = mISDNport->portnum;
                SCPY(ifport->portname, mISDNport->name);
                /* set defaults */
-               set_defaults(ifport);
+               set_mISDN_defaults(ifport);
                /* load static port instances */
                mISDNport_static(mISDNport);
-#ifdef WITH_GSM_MS
-               if (ifport->gsm_ms)
-                       gsm_ms_new(ifport->gsm_ms_name);
-#endif
-#ifdef WITH_GSM_BS
-               if (ifport->gsm_bs)
-                       gsm_bs_init();
-#endif
        } else {
                ifport->block = 2; /* not available */
        }
 }
+#endif
 
 /*
  * give summary of interface syntax
@@ -1632,12 +1817,17 @@ void doc_interface(void)
 /* screen caller id
  * out==0: incoming caller id, out==1: outgoing caller id
  */
-void do_screen(int out, char *id, int idsize, int *type, int *present, struct interface *interface)
+void do_screen(int out, char *id, int idsize, int *type, int *present, const char *interface_name)
 {
        char                    *msn1;
        struct interface_msn    *ifmsn;
        struct interface_screen *ifscreen;
        char suffix[64];
+       struct interface *interface = interface_first;
+
+       interface = getinterfacebyname(interface_name);
+       if (!interface)
+               return;
 
        /* screen incoming caller id */
        if (!out) {
@@ -1762,3 +1952,16 @@ void do_screen(int out, char *id, int idsize, int *type, int *present, struct in
        }
 }
 
+struct interface *getinterfacebyname(const char *name)
+{
+       struct interface *interface = interface_first;
+
+       while (interface) {
+               if (!strcmp(interface->name, name))
+                       return interface;
+               interface = interface->next;
+       }
+
+       return NULL;
+}
+