Version 0.1.12 from FTP
[asterisk/asterisk.git] / channels / chan_vofr.c
index f8f171c..cfd669e 100755 (executable)
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <fcntl.h>
 #include <arpa/inet.h>
 #include <linux/if_packet.h>
 #include <linux/if_ether.h>
+#ifndef OLD_SANGOMA_API
+#include <linux/if_wanpipe.h>
+#include <linux/wanpipe.h>
+#endif
 #include <sys/signal.h>
 #include "adtranvofr.h"
 
+/* #define VOFRDUMPER */
+
 #define G723_MAX_BUF 2048
 
 #define FR_API_MESS 16
@@ -42,6 +49,8 @@ static char *config = "adtranvofr.conf";
 
 static char context[AST_MAX_EXTENSION] = "default";
 
+static char language[MAX_LANGUAGE] = "";
+
 static int usecnt =0;
 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
 
@@ -63,7 +72,11 @@ static int restart_monitor(void);
    
 static struct vofr_pvt {
        int s;                                                  /* Raw socket for this DLCI */
+#ifdef OLD_SANGOMA_API
        struct sockaddr_pkt sa;                 /* Sockaddr needed for sending, also has iface name */
+#else
+       struct wan_sockaddr_ll sa;              /* Wanpipe sockaddr */
+#endif
        struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
        int outgoing;                                   /* Does this channel support outgoing calls? */
        struct vofr_pvt *next;                  /* Next channel in list */
@@ -75,6 +88,8 @@ static struct vofr_pvt {
        char buf[G723_MAX_BUF];                                 /* Static buffer for reading frames */
        char obuf[G723_MAX_BUF];                                /* Output buffer */
        char context[AST_MAX_EXTENSION];
+       char language[MAX_LANGUAGE];
+       int ringgothangup;                              /* Have we received exactly one hangup after a ring */
 } *iflist = NULL;
 
 #ifdef VOFRDUMPER
@@ -241,7 +256,11 @@ static void vofr_dump_packet(struct vofr_hdr *vh, int len)
 static int vofr_xmit(struct vofr_pvt *p, char *data, int len)
 {
        int res;
+#ifdef OLD_SANGOMA_API
     res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_pkt));
+#else
+    res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct wan_sockaddr_ll));
+#endif
        if (res != len) {
                ast_log(LOG_WARNING, "vofr_xmit returned %d\n", res);
        }
@@ -337,7 +356,7 @@ static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
        while(otimeout) {
                otimeout = ast_waitfor(ast, 1000);
                if (otimeout < 1) {
-                       ast_log(LOG_WARNING, "Unable to take line off hook\n");
+                       ast_log(LOG_WARNING, "Unable to take line '%s' off hook\n", ast->name);
                        /* Musta gotten hung up, or no ack on off hook */
                        return -1;      
                }
@@ -434,6 +453,7 @@ static int vofr_hangup(struct ast_channel *ast)
        }
        ast->state = AST_STATE_DOWN;
        ((struct vofr_pvt *)(ast->pvt->pvt))->owner = NULL;
+       ((struct vofr_pvt *)(ast->pvt->pvt))->ringgothangup = 0;
        pthread_mutex_lock(&usecnt_lock);
        usecnt--;
        if (usecnt < 0) 
@@ -465,6 +485,9 @@ static int vofr_answer(struct ast_channel *ast)
                cnt = ast_waitfor(ast, cnt);
                if (cnt > 0) {
                        res = read(ast->fd, buf, sizeof(buf));
+#ifdef VOFRDUMPER
+                               vofr_dump_packet((void *)(buf +FR_API_MESS), res - FR_API_MESS);
+#endif
                        res -= FR_API_MESS;
                        if (res < 0)
                                ast_log(LOG_WARNING, "Warning:  read failed (%s) on %s\n", strerror(errno), ast->name);
@@ -529,15 +552,32 @@ static struct ast_frame  *vofr_read(struct ast_channel *ast)
        /* Read into the right place in the buffer, in case we send this
           as a voice frame. */
        CHECK_BLOCKING(ast);
+retry:
        res = read(p->s, ((char *)vh)  - FR_API_MESS, 
                                G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
+       if (res < 0) {
+               /*  XXX HUGE BUG IN SANGOMA'S STACK: IT IGNORES O_NONBLOCK XXX */
+               if (errno == EAGAIN) {
+                       fd_set fds;
+                       FD_ZERO(&fds);
+                       FD_SET(p->s, &fds);
+                       select(p->s + 1, &fds, NULL, NULL, NULL);
+                       goto retry;
+               }
+               ast->blocking = 0;
+               ast_log(LOG_WARNING, "Read error on %s: %s (%d)\n", ast->name, strerror(errno));
+               return NULL;
+       }
        ast->blocking = 0;
+               
+#ifdef VOFRDUMPER
+       vofr_dump_packet((void *)(vh), res);
+#endif
        res -= FR_API_MESS;             
-       if (res < sizeof(struct vofr_hdr *)) {
+       if (res < sizeof(struct vofr_hdr)) {
                ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
                return NULL;
        }
-
        /* Some nice norms */
        fr->datalen = 0;
        fr->timelen = 0;
@@ -552,15 +592,17 @@ static struct ast_frame  *vofr_read(struct ast_channel *ast)
                switch(vh->data[0]) {
                case VOFR_SIGNAL_ON_HOOK:
                        /* Hang up this line */
-                       if (ast->state == AST_STATE_UP)
+                       if ((ast->state == AST_STATE_UP) || (p->ringgothangup)) {
                                return NULL;
-                       else {
+                       } else {
                                fr->frametype = AST_FRAME_NULL;
                                fr->subclass = 0;
-                               break;
+                               p->ringgothangup=1;
                        }
+                       break;
                case VOFR_SIGNAL_RING:
                        ast->rings++;
+                       p->ringgothangup = 0;
                        break;
                case VOFR_SIGNAL_UNKNOWN:
                        switch(vh->data[1]) {
@@ -657,7 +699,7 @@ static struct ast_frame  *vofr_read(struct ast_channel *ast)
                        ast->state = AST_STATE_UP;
                        return fr;
                } else if (ast->state !=  AST_STATE_UP) {
-                       ast_log(LOG_WARNING, "Voice in weird state %d\n", ast->state);
+                       ast_log(LOG_WARNING, "%s: Voice in weird state %d\n", ast->name, ast->state);
                }
                fr->frametype = AST_FRAME_VOICE;
                fr->subclass = AST_FORMAT_G723_1;
@@ -745,11 +787,15 @@ static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
        struct ast_channel *tmp;
        tmp = ast_channel_alloc();
        if (tmp) {
+#ifdef OLD_SANGOMA_API
                snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.spkt_device);
+#else
+               snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.sll_device);
+#endif
                tmp->type = type;
                tmp->fd = i->s;
                /* Adtran VoFR supports only G723.1 format data.  G711 (ulaw) would be nice too */
-               tmp->format = AST_FORMAT_G723_1;
+               tmp->nativeformats = AST_FORMAT_G723_1;
                tmp->state = state;
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
@@ -760,6 +806,8 @@ static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
                tmp->pvt->answer = vofr_answer;
                tmp->pvt->read = vofr_read;
                tmp->pvt->write = vofr_write;
+               if (strlen(i->language))
+                       strncpy(tmp->language, i->language, sizeof(tmp->language));
                i->owner = tmp;
                pthread_mutex_lock(&usecnt_lock);
                usecnt++;
@@ -788,9 +836,10 @@ static int vofr_mini_packet(struct vofr_pvt *i, struct vofr_hdr *pkt, int len)
                switch(pkt->data[0]) {
                case VOFR_SIGNAL_RING:
                        /* If we get a RING, we definitely want to start a new thread */
-                       if (!i->owner)
+                       if (!i->owner) {
+                               i->ringgothangup = 0;
                                vofr_new(i, AST_STATE_RING);
-                       else
+                       } else
                                ast_log(LOG_WARNING, "Got a ring, but there's an owner?\n");
                        break;
                case VOFR_SIGNAL_OFF_HOOK:
@@ -867,7 +916,11 @@ static void *do_monitor(void *data)
                i = iflist;
                while(i) {
                        if (FD_ISSET(i->s, &rfds)) 
+#ifdef OLD_SANGOMA_API
                                ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.spkt_device);
+#else
+                               ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.sll_device);
+#endif
                        if (!i->owner) {
                                /* This needs to be watched, as it lacks an owner */
                                FD_SET(i->s, &rfds);
@@ -901,7 +954,11 @@ static void *do_monitor(void *data)
                while(i) {
                        if (FD_ISSET(i->s, &rfds)) {
                                if (i->owner) {
+#ifdef OLD_SANGOMA_API
                                        ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->s, i->sa.spkt_device);
+#else
+                                       ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->s, i->sa.sll_device);
+#endif
                                        continue;
                                }
                                res = read(i->s, i->buf, sizeof(i->buf));
@@ -959,27 +1016,49 @@ static struct vofr_pvt *mkif(char *type, char *iface)
        /* Make a vofr_pvt structure for this interface */
        struct vofr_pvt *tmp;
        int sndbuf = 4096;
-       
+
        tmp = malloc(sizeof(struct vofr_pvt));
        if (tmp) {
 
                /* Allocate a packet socket */
+#ifdef OLD_SANGOMA_API
                tmp->s = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
+#else
+               /* Why the HELL does Sangoma change their API every damn time
+                  they make a new driver release?!?!?!  Leave it the hell
+                  alone this time.  */
+               tmp->s = socket(AF_WANPIPE, SOCK_RAW, 0);
+#endif         
+
                if (tmp->s < 0) {
                        ast_log(LOG_ERROR, "Unable to create socket: %s\n", strerror(errno));
                        free(tmp);
                        return NULL;
                }
 
+#ifdef OLD_SANGOMA_API
                /* Prepare sockaddr for binding */
                memset(&tmp->sa, 0, sizeof(tmp->sa));
                strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device));
                tmp->sa.spkt_protocol = htons(0x16);
                tmp->sa.spkt_family = AF_PACKET;
-               
-               /* Bind socket to specific interface */
                if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct sockaddr))) {
+#else
+               /* Prepare sockaddr for binding */
+               memset(&tmp->sa, 0, sizeof(tmp->sa));
+               tmp->sa.sll_family = AF_WANPIPE;
+               tmp->sa.sll_protocol = htons(ETH_P_IP);
+               strncpy(tmp->sa.sll_device, iface, sizeof(tmp->sa.sll_device));
+               strncpy(tmp->sa.sll_card, "wanpipe1", sizeof(tmp->sa.sll_card));
+               tmp->sa.sll_ifindex = 0;
+               if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct wan_sockaddr_ll))) {
+#endif         
+               /* Bind socket to specific interface */
+#ifdef OLD_SANGOMA_API
                        ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.spkt_device, 
+#else
+                       ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.sll_device, 
+#endif
                                                                                strerror(errno));
                        free(tmp);
                        return NULL;
@@ -997,6 +1076,8 @@ static struct vofr_pvt *mkif(char *type, char *iface)
                tmp->dlcil = 0;
                tmp->dlcih = 0;
                tmp->cid = 1;
+               tmp->ringgothangup = 0;
+               strncpy(tmp->language, language, sizeof(tmp->language));
                strncpy(tmp->context, context, sizeof(tmp->context));
                /* User terminations are game for outgoing connections */
                if (!strcasecmp(type, "user")) 
@@ -1032,7 +1113,7 @@ static struct ast_channel *vofr_request(char *type, int format, void *data)
        }
        p = iflist;
        while(p) {
-               if (!p->owner) {
+               if (!p->owner && p->outgoing) {
                        tmp = vofr_new(p, AST_STATE_DOWN);
                        break;
                }
@@ -1078,6 +1159,8 @@ int load_module()
                                }
                } else if (!strcasecmp(v->name, "context")) {
                        strncpy(context, v->value, sizeof(context));
+               } else if (!strcasecmp(v->name, "language")) {
+                       strncpy(language, v->value, sizeof(language));
                }
                v = v->next;
        }
@@ -1158,6 +1241,11 @@ int usecount()
        return res;
 }
 
+char *key()
+{
+       return ASTERISK_GPL_KEY;
+}
+
 char *description()
 {
        return desc;