Make sure other drivers also zero delivery (thanks to steven davies)
[asterisk/asterisk.git] / channels / chan_vofr.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Implementation of Voice over Frame Relay, Adtran Style
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <asterisk/lock.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <sys/socket.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <arpa/inet.h>
30 #include <linux/if_packet.h>
31 #include <linux/if_ether.h>
32 #ifndef OLD_SANGOMA_API
33 #include <linux/if_wanpipe.h>
34 #include <linux/wanpipe.h>
35 #endif
36 #include <sys/signal.h>
37 #include "adtranvofr.h"
38
39 /* #define VOFRDUMPER */
40
41 #define G723_MAX_BUF 2048
42
43 #define FR_API_MESS 16
44
45 static char *desc = "Adtran Voice over Frame Relay";
46 static char *type = "AdtranVoFR";
47 static char *tdesc = "Voice over Frame Relay/Adtran style";
48 static char *config = "adtranvofr.conf";
49
50 static char context[AST_MAX_EXTENSION] = "default";
51
52 static char language[MAX_LANGUAGE] = "";
53
54 static int usecnt =0;
55 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
56
57 /* Protect the interface list (of vofr_pvt's) */
58 AST_MUTEX_DEFINE_STATIC(iflock);
59
60 /* Protect the monitoring thread, so only one process can kill or start it, and not
61    when it's doing something critical. */
62 AST_MUTEX_DEFINE_STATIC(monlock);
63
64 /* This is the thread for the monitor which checks for input on the channels
65    which are not currently in use.  */
66 static pthread_t monitor_thread = AST_PTHREADT_NULL;
67
68 static int restart_monitor(void);
69
70 /* The private structures of the Adtran VoFR channels are linked for
71    selecting outgoing channels */
72    
73 static struct vofr_pvt {
74         int s;                                                  /* Raw socket for this DLCI */
75 #ifdef OLD_SANGOMA_API
76         struct sockaddr_pkt sa;                 /* Sockaddr needed for sending, also has iface name */
77 #else
78         struct wan_sockaddr_ll sa;              /* Wanpipe sockaddr */
79 #endif
80         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
81         int outgoing;                                   /* Does this channel support outgoing calls? */
82         struct vofr_pvt *next;                  /* Next channel in list */
83         struct vofr_hdr *hdr;                           /* VOFR header version of buf */
84         struct vofr_hdr *ohdr;
85         u_int8_t dlcih;                                 /* High two bits of DLCI */
86         u_int8_t dlcil;                                 /* Bottom two bits of DLCI */
87         u_int8_t cid;                                   /* Call ID */
88         char buf[G723_MAX_BUF];                                 /* Static buffer for reading frames */
89         char obuf[G723_MAX_BUF];                                /* Output buffer */
90         char context[AST_MAX_EXTENSION];
91         char language[MAX_LANGUAGE];
92         int ringgothangup;                              /* Have we received exactly one hangup after a ring */
93 } *iflist = NULL;
94
95 #ifdef VOFRDUMPER
96
97 /* Some useful debugging routines */
98
99 static char *set(int val)
100 {
101         return (val ? "Set  " : "Unset");
102 }
103
104 static char *controlstr(int control)
105 {
106         switch(control) {
107         case VOFR_CONTROL_ADTRAN:
108                 return "Adtran Proprietary";
109         case VOFR_CONTROL_VOICE:
110                 return "Voice";
111         case VOFR_CONTROL_RFC1490:
112                 return "RFC 1490";
113         }
114         return "Unknown";
115 }
116
117 static char *dtypestr(int control)
118 {
119         switch(control) {
120         case VOFR_TYPE_SIGNAL:
121                 return "Signal Frame";
122         case VOFR_TYPE_VOICE:
123                 return "Voice Frame";
124         case VOFR_TYPE_ANSWER:
125                 return "Answer Tone";
126         case VOFR_TYPE_FAX:     
127                 return "FAX";
128         case VOFR_TYPE_DTMF:
129                 return "DTMF Digit";
130         }
131         return "Unknown";
132 }
133
134 static char *vflagsstr(int flags)
135 {
136         static char buf[80];
137         buf[0] = '\0';
138         if (!flags)
139                 return "(None)";
140         if (flags & VOFR_ROUTE_LOCAL)
141                 strcat(buf, "Local ");
142         if (flags & VOFR_ROUTE_VOICE)
143                 strcat(buf, "Voice ");
144         if (flags & VOFR_ROUTE_DTE)
145                 strcat(buf, "DTE ");
146         else if (flags & VOFR_ROUTE_DTE1)
147                 strcat(buf, "DTE1 ");
148         else if (flags & VOFR_ROUTE_DTE2)       
149                 strcat(buf, "DTE2 ");
150         return buf;
151 }
152
153 static char *remidstr(int remid)
154 {
155         switch(remid) {
156         case VOFR_CARD_TYPE_UNSPEC:
157                 return "Unspecified";
158         case VOFR_CARD_TYPE_FXS:
159                 return "FXS";
160         case VOFR_CARD_TYPE_FXO:
161                 return "FXO";
162         case VOFR_CARD_TYPE_ENM:        
163                 return "E&M";
164         case VOFR_CARD_TYPE_VCOM:       
165                 return "Atlas/VCOM";
166         }
167         return "Unknown";
168 }
169
170 static char *modulationstr(int modulation)
171 {
172         switch(modulation) {
173         case VOFR_MODULATION_SINGLE:
174                 return "Single Frequency";
175         case VOFR_MODULATION_V21:
176                 return "V.21";
177         case VOFR_MODULATION_V27ter_2:
178                 return "V.27 (2400bps)";
179         case VOFR_MODULATION_V27ter_4:
180                 return "V.27 (4800bps)";
181         case VOFR_MODULATION_V29_7:
182                 return "V.29 (7200bps)";
183         case VOFR_MODULATION_V29_9:
184                 return "V.29 (9600bps)";
185         case VOFR_MODULATION_V33_12:
186                 return "V.33 (12000bps)";
187         case VOFR_MODULATION_V33_14:
188                 return "V.33 (14400BPS)";
189         }
190         return "Unknown";
191 }
192
193 static char *signalstr(int signal)
194 {
195         switch(signal) {
196         case VOFR_SIGNAL_ON_HOOK:
197                 return "On Hook";
198         case VOFR_SIGNAL_OFF_HOOK:
199                 return "Off Hook";
200         case VOFR_SIGNAL_RING:
201                 return "Ring";
202         case VOFR_SIGNAL_SWITCHED_DIAL:
203                 return "Switched Dial";
204         case VOFR_SIGNAL_BUSY:
205                 return "Busy";
206         case VOFR_SIGNAL_TRUNK_BUSY:
207                 return "Trunk Busy";
208         }
209         return "Unknown";
210 }
211
212 static char *vofr_digitstr(int val)
213 {
214         static char num[5];
215         if (val < 10) {
216                 snprintf(num, sizeof(num), "%d", val);
217                 return num;
218         }
219         switch(val) {
220         case 10:
221                 return "*";
222         case 11:
223                 return "#";
224         }
225         return "Unknown";
226 }
227
228
229 static void vofr_dump_packet(struct vofr_hdr *vh, int len)
230 {
231         printf("VoFR Packet Dump\n");
232         printf("================\n");
233         printf("EI: %s ", set(vh->control & VOFR_MASK_EI));
234         printf("LI: %s\n", set(vh->control & VOFR_MASK_LI));
235         printf("Control: %s (0x%02x)\n", 
236                 controlstr(vh->control & VOFR_MASK_CONTROL), vh->control & VOFR_MASK_CONTROL);
237         printf("Data Type: %s (0x%02x)\n", dtypestr(vh->dtype), vh->dtype);
238         if (vh->dtype == VOFR_TYPE_SIGNAL) {
239                 printf(" \\--Signal: %s (0x%02x)\n", signalstr(vh->data[0]), vh->data[0]);
240         }
241         if (vh->dtype == VOFR_TYPE_DTMF) {
242                 printf(" \\--Digit: %s (0x%02x)\n", vofr_digitstr(vh->data[0]), vh->data[0]);
243         }
244         printf("Connect Tag: 0x%02x\n", vh->ctag);
245         printf("Voice Rt Flags: %s\n", vflagsstr(vh->vflags));
246         printf("DLCI X-Ref: %d\n", (vh->dlcih << 8) | (vh->dlcil));
247         printf("Channel ID: %d\n", vh->cid);
248         printf("Remote ID: %s (0x%02x)\n", remidstr(vh->remid), vh->remid);
249         printf("Modulation: %s (0x%02x)\n", modulationstr(vh->mod), vh->mod);
250         printf("\n");
251         fflush(stdout);
252 }
253
254 #endif
255
256 static struct ast_frame  *vofr_read(struct ast_channel *ast);
257
258 static int vofr_xmit(struct vofr_pvt *p, char *data, int len)
259 {
260         int res;
261 #ifdef OLD_SANGOMA_API
262     res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_pkt));
263 #else
264     res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct wan_sockaddr_ll));
265 #endif
266         if (res != len) {
267                 ast_log(LOG_WARNING, "vofr_xmit returned %d\n", res);
268         }
269         return res;
270 }
271
272 static int vofr_digit(struct ast_channel *ast, char digit)
273 {
274         /*
275          * T H I S   I S   T O T A L L Y   U N D O C U M E N T E D
276          *     A N D   D O E S   N O T   S O U N D   R I G H T
277          *   XXX Figure out how to really send a decent digit XXX
278          */
279         struct vofr_pvt *p;
280         struct vofr_hdr *vh;
281         p = ast->pvt->pvt;
282         vh = p->ohdr;
283         vh->control = VOFR_CONTROL_VOICE;
284         vh->dtype = VOFR_TYPE_DTMF;
285         vh->vflags = VOFR_ROUTE_NONE;
286         vh->dlcih = p->dlcih;
287         vh->dlcil = p->dlcil;
288         vh->cid = p->cid;
289         vh->remid = VOFR_CARD_TYPE_ASTERISK;
290         vh->mod = VOFR_MODULATION_SINGLE;
291         if ((digit >= '0') && (digit <= '9'))
292                 vh->data[0] = digit - '0';
293         else if (digit == '*')
294                 vh->data[0] = 10;
295         else if (digit == '#')
296                 vh->data[0] = 11;
297         else {
298                 ast_log(LOG_WARNING, "%s: tried to dial a non digit '%c'\n", ast->name, digit);
299                 return -1;
300         }
301         vh->data[1] = 0x14;
302         vh->data[2] = 0x1f;
303         vh->data[3] = 0x70;
304                 /* We sorta start the digit */
305         vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + 4 + FR_API_MESS);
306         usleep(30000);
307                 /* And terminate with an empty voice frame */
308         vh->control = VOFR_CONTROL_VOICE;
309         vh->dtype = VOFR_TYPE_VOICE;
310         vh->vflags = VOFR_ROUTE_NONE;
311         vh->dlcih = p->dlcih;
312         vh->dlcil = p->dlcil;
313         vh->cid = p->cid;
314         vh->remid = VOFR_CARD_TYPE_ASTERISK;
315         vh->mod = VOFR_MODULATION_SINGLE;
316         vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + FR_API_MESS);
317         return 0;
318 }
319
320 static int vofr_xmit_signal(struct vofr_pvt *p, int signal, int pad)
321 {
322         /* Prepare and transmit outgoing buffer with given signal and
323            pad the end with *pad* bytes of data presumed to already
324            be in the buffer (like DTMF tones, etc) */
325         struct vofr_hdr *vh = p->ohdr;
326         int res;
327     vh->control = VOFR_CONTROL_VOICE;
328     vh->dtype = VOFR_TYPE_SIGNAL;
329     vh->vflags = VOFR_ROUTE_NONE;
330     vh->dlcih = p->dlcih;
331     vh->dlcil = p->dlcil;
332     vh->cid = p->cid;
333     vh->remid = VOFR_CARD_TYPE_ASTERISK;
334     vh->mod = VOFR_MODULATION_SINGLE;
335     vh->data[0] = signal;
336         if (FR_API_MESS)
337                 memset(p->obuf, 0, FR_API_MESS);
338         res = vofr_xmit(p, p->obuf,  VOFR_HDR_SIZE + pad + 1 + FR_API_MESS);
339         return res;
340
341 }
342
343 static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
344 {
345         int res;
346         int otimeout;
347         struct ast_frame *f;
348         struct vofr_pvt *p;
349         p = ast->pvt->pvt;
350         if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
351                 ast_log(LOG_WARNING, "vofr_call called on %s, neither down nor reserved\n", ast->name);
352                 return -1;
353         }
354         /* Take the system off hook */
355         vofr_xmit_signal(p, VOFR_SIGNAL_OFFHOOK, 0);
356         /* Wait for an acknowledgement */
357         otimeout = 1000;
358         while(otimeout) {
359                 otimeout = ast_waitfor(ast, 1000);
360                 if (otimeout < 1) {
361                         ast_log(LOG_WARNING, "Unable to take line '%s' off hook\n", ast->name);
362                         /* Musta gotten hung up, or no ack on off hook */
363                         return -1;      
364                 }
365                 f = vofr_read(ast);
366                 if (!f)
367                         return -1;
368                 if ((f->frametype == AST_FRAME_CONTROL) &&
369                     (f->subclass == AST_CONTROL_OFFHOOK)) 
370                         /* Off hook */
371                                 break;
372         }
373         if (!otimeout) {
374                 ast_log(LOG_WARNING, "Unable to take line off hook\n");
375                 return -1;
376         }
377         /* Send the digits */
378         while(*dest) {
379                 ast->state = AST_STATE_DIALING;
380                 vofr_digit(ast, *dest);
381                 /* Wait .1 seconds before dialing next digit */
382                 usleep(100000);
383                 dest++;
384         }
385         if (timeout) {
386                 /* Wait for the ack that it's ringing */
387                 otimeout = 1000;
388                 while(otimeout) {
389                         otimeout = ast_waitfor(ast, 1000);
390                         if (otimeout < 1) {
391                                 ast_log(LOG_WARNING, "No acknowledgement for ringing\n");
392                                 /* Musta gotten hung up, or no ack on off hook */
393                                 return -1;      
394                         }
395                         f = vofr_read(ast);
396                         if (!f) 
397                                 return -1;
398                         
399                         if (f->frametype == AST_FRAME_CONTROL) {
400                             if (f->subclass == AST_CONTROL_RINGING) {
401                                         ast->state = AST_STATE_RINGING;
402                                         /* We're ringing -- good enough */
403                                         break;
404                                 }
405                                 if (f->subclass == AST_CONTROL_BUSY)
406                                 /* It's busy */
407                                         return -1;
408                         }
409                         ast_frfree(f);
410                 }               
411         }
412         otimeout = timeout;
413         while(timeout) {
414                 /* Wait for an answer, up to timeout... */
415                 res = ast_waitfor(ast, timeout);
416                 if (res < 0)
417                         /* Musta gotten hung up */
418                         return -1;
419                 else
420                         timeout = res;
421                 if (res) {
422                         /* Ooh, read what's there. */
423                         f = vofr_read(ast);
424                         if (!f)
425                                 return -1;
426                         if ((f->frametype == AST_FRAME_CONTROL) && 
427                             (f->subclass == AST_CONTROL_ANSWER)) 
428                                 /* Got an answer -- return the # of ms it took */
429                                 return otimeout - res;
430                                 
431                 }
432         }
433         return 0;
434 }
435
436 static int send_hangup(struct vofr_pvt *p)
437 {
438         /* Just send the hangup sequence */
439         return vofr_xmit_signal(p, 0x80, 0);
440 }
441
442 static int vofr_hangup(struct ast_channel *ast)
443 {
444         int res;
445         if (option_debug)
446                 ast_log(LOG_DEBUG, "vofr_hangup(%s)\n", ast->name);
447         if (!ast->pvt->pvt) {
448                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
449                 return 0;
450         }
451         res = send_hangup(ast->pvt->pvt);
452         if (res < 0) {
453                 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
454                 return -1;
455         }
456         ast->state = AST_STATE_DOWN;
457         ((struct vofr_pvt *)(ast->pvt->pvt))->owner = NULL;
458         ((struct vofr_pvt *)(ast->pvt->pvt))->ringgothangup = 0;
459         ast_mutex_lock(&usecnt_lock);
460         usecnt--;
461         if (usecnt < 0) 
462                 ast_log(LOG_WARNING, "Usecnt < 0???\n");
463         ast_mutex_unlock(&usecnt_lock);
464         ast_update_use_count();
465         if (option_verbose > 2) 
466                 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
467         ast->pvt->pvt = NULL;
468         ast->state = AST_STATE_DOWN;
469         restart_monitor();
470         return 0;
471 }
472
473 static int vofr_answer(struct ast_channel *ast)
474 {
475         int res;
476         int cnt = 1000;
477         char buf[2048];
478         struct vofr_hdr *vh;
479         ast->rings = 0;
480         if (option_debug)
481                 ast_log(LOG_DEBUG, "vofr_answer(%s)\n", ast->name);
482         res = vofr_xmit_signal(ast->pvt->pvt, VOFR_SIGNAL_OFFHOOK, 0);
483         if (res < 0)
484                 ast_log(LOG_WARNING, "Unable to anaswer line %s\n", ast->name);
485         ast->state = AST_STATE_UP;
486         while(cnt > 0) {
487                 cnt = ast_waitfor(ast, cnt);
488                 if (cnt > 0) {
489                         res = read(ast->fds[0], buf, sizeof(buf));
490 #ifdef VOFRDUMPER
491                                 vofr_dump_packet((void *)(buf +FR_API_MESS), res - FR_API_MESS);
492 #endif
493                         res -= FR_API_MESS;
494                         if (res < 0)
495                                 ast_log(LOG_WARNING, "Warning:  read failed (%s) on %s\n", strerror(errno), ast->name);
496                         else {
497                                 /* We're looking for an answer */
498                                 vh = (struct vofr_hdr *)(buf + FR_API_MESS);
499                                 switch(vh->dtype) {
500                                 case VOFR_TYPE_SIGNAL:
501                                         switch(vh->data[0]) {
502                                         case VOFR_SIGNAL_UNKNOWN:
503                                                 switch(vh->data[1]) {
504                                                 case 0x1:
505                                                         if (option_debug) 
506                                                                 ast_log(LOG_DEBUG, "Answered '%s'\n", ast->name);
507                                                         else if (option_verbose > 2) 
508                                                                 ast_verbose( VERBOSE_PREFIX_3 "Answered '%s'\n", ast->name);
509                                                         ast->state = AST_STATE_UP;
510                                                         return 0;
511                                                         break;
512                                                 default:
513                                                         ast_log(LOG_WARNING, "Unexpected 'unknown' frame type %d\n", vh->data[1]);
514                                                 }
515                                                 break;
516                                         case VOFR_SIGNAL_ON_HOOK:
517                                                 /* Ignore onhooks.  */
518                                                 break;
519                                         default:
520                                                 ast_log(LOG_WARNING, "Unexpected signal type %d\n", vh->data[0]);
521                                         }
522                                         break;
523                                 default:
524                                         ast_log(LOG_WARNING, "Unexpected data type %d\n", vh->dtype);
525                                 }
526                         }
527                 }
528         }
529         ast_log(LOG_WARNING, "Did not get acknowledged answer\n");
530         return -1;
531 }
532
533 static char vofr_2digit(char c)
534 {
535         if (c == 11)
536                 return '#';
537         else if (c == 10)
538                 return '*';
539         else if ((c < 10) && (c >= 0))
540                 return '0' + c;
541         else
542                 return '?';
543 }
544
545 static struct ast_frame  *vofr_read(struct ast_channel *ast)
546 {
547         int res;
548         char tone;
549         int timeout,x;
550         struct vofr_pvt *p = ast->pvt->pvt;
551         short *swapping;
552         struct ast_frame *fr = (struct ast_frame *)(p->buf);
553         struct vofr_hdr *vh = (struct vofr_hdr *)(p->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET - sizeof(struct vofr_hdr));
554         /* Read into the right place in the buffer, in case we send this
555            as a voice frame. */
556         CHECK_BLOCKING(ast);
557 retry:
558         res = read(p->s, ((char *)vh)  - FR_API_MESS, 
559                                 G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
560         if (res < 0) {
561                 /*  XXX HUGE BUG IN SANGOMA'S STACK: IT IGNORES O_NONBLOCK XXX */
562                 if (errno == EAGAIN) {
563                         fd_set fds;
564                         FD_ZERO(&fds);
565                         FD_SET(p->s, &fds);
566                         ast_select(p->s + 1, &fds, NULL, NULL, NULL);
567                         goto retry;
568                 }
569                 ast->blocking = 0;
570                 ast_log(LOG_WARNING, "Read error on %s: %s (%d)\n", ast->name, strerror(errno));
571                 return NULL;
572         }
573         ast->blocking = 0;
574                 
575 #ifdef VOFRDUMPER
576         vofr_dump_packet((void *)(vh), res);
577 #endif
578         res -= FR_API_MESS;             
579         if (res < sizeof(struct vofr_hdr)) {
580                 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
581                 return NULL;
582         }
583         /* Some nice norms */
584         fr->datalen = 0;
585         fr->timelen = 0;
586         fr->data =  NULL;
587         fr->src = type;
588         fr->offset = 0;
589         fr->mallocd=0;
590         fr->delivery.tv_sec = 0;
591         fr->delivery.tv_usec = 0;
592         
593         /* Now, what we do depends on what we read */
594         switch(vh->dtype) {
595         case VOFR_TYPE_SIGNAL:
596                 switch(vh->data[0]) {
597                 case VOFR_SIGNAL_ON_HOOK:
598                         /* Hang up this line */
599                         if ((ast->state == AST_STATE_UP) || (p->ringgothangup)) {
600                                 return NULL;
601                         } else {
602                                 fr->frametype = AST_FRAME_NULL;
603                                 fr->subclass = 0;
604                                 p->ringgothangup=1;
605                         }
606                         break;
607                 case VOFR_SIGNAL_RING:
608                         ast->rings++;
609                         p->ringgothangup = 0;
610                         break;
611                 case VOFR_SIGNAL_UNKNOWN:
612                         switch(vh->data[1]) {
613                         case 0x1:
614                                 /* This is a little tricky, because it depends
615                                    on the context of what state we're in */
616                                 switch(ast->state) {
617                                 case AST_STATE_RINGING:
618                                         fr->frametype = AST_FRAME_CONTROL;
619                                         fr->subclass = AST_CONTROL_ANSWER;
620                                         ast->state = AST_STATE_UP;
621                                         break;
622                                 case AST_STATE_DOWN:
623                                 case AST_STATE_UP:
624                                         fr->frametype = AST_FRAME_NULL;
625                                         fr->subclass = 0;
626                                         break;
627                                 }
628                                 break;
629                         case 0x2:
630                                 /* Remote acknowledged off hook */
631                                 fr->frametype = AST_FRAME_CONTROL;
632                                 fr->subclass = AST_CONTROL_OFFHOOK;
633                                 ast->state = AST_STATE_OFFHOOK;
634                                 break;
635                         case 0x3:
636                                 /* Busy signal */
637                                 fr->frametype = AST_FRAME_CONTROL;
638                                 fr->subclass = AST_CONTROL_BUSY;
639                                 ast->state = AST_STATE_BUSY;
640                                 break;
641                         case 0x5:
642                                 /* Ringing -- acknowledged */
643                                 fr->frametype = AST_FRAME_CONTROL;
644                                 fr->subclass = AST_CONTROL_RINGING;
645                                 ast->state = AST_STATE_RINGING;
646                                 break;
647                         case 0x6:
648                                 /* Hang up detected.  Return NULL */
649                                 return NULL;
650                         default:
651                                 ast_log(LOG_WARNING, "Don't know what to do with 'unknown' signal '%d'\n", vh->data[1]);
652                                 fr->frametype = AST_FRAME_NULL;
653                                 fr->subclass = 0;
654                         }
655                         return fr;
656                         break;
657                 default:
658                         ast_log(LOG_WARNING, "Don't know what to do with signal '%d'\n", vh->data[0]);
659                 }
660                 break;
661         case VOFR_TYPE_DTMF:
662                 /* If it's a DTMF tone, then we want to wait until we don't get any more dtmf tones or
663                    the DTMF tone changes.  
664                        XXX Note: We will drop at least one frame here though XXX */
665                 
666                 tone = vofr_2digit(vh->data[0]);
667                 timeout = 50;
668                 do {
669                         if ((timeout = ast_waitfor(ast, timeout)) < 1)
670                                 break;
671                         CHECK_BLOCKING(ast);
672                         res = read(p->s, ((char *)vh)  - FR_API_MESS, 
673                                         G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
674                         ast->blocking = 0;
675                         res -= FR_API_MESS;             
676                         if (res < sizeof(struct vofr_hdr *)) {
677                                 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
678                                 return NULL;
679                         }
680                         if (vh->dtype == VOFR_TYPE_DTMF) {
681                                 /* Reset the timeout */
682                                 timeout = 50;
683                                 if ((tone != vofr_2digit(vh->data[0])) )
684                                         /* Or not...  Something else now.. Just send our first frame */
685                                         break;
686                         }
687                         
688                 } while (timeout);
689                 fr->frametype = AST_FRAME_DTMF;
690                 fr->subclass = tone;
691                 fr->datalen = 0;
692                 fr->data = NULL;
693                 fr->offset = 0;
694                 return fr;
695         case VOFR_TYPE_VOICE:
696                 /* XXX Bug in the Adtran: Sometimes we don't know when calls are picked up, so if we
697                        get voice frames, go ahead and consider it answered even though it probably has
698                            not been answered XXX */
699                 if ((ast->state == AST_STATE_RINGING) || (ast->state == AST_STATE_DIALING))  {
700                         ast_log(LOG_DEBUG, "Adtran bug! (state = %d)\n", ast->state);
701                         fr->frametype = AST_FRAME_CONTROL;
702                         fr->subclass = AST_CONTROL_ANSWER;
703                         ast->state = AST_STATE_UP;
704                         return fr;
705                 } else if (ast->state !=  AST_STATE_UP) {
706                         ast_log(LOG_WARNING, "%s: Voice in weird state %d\n", ast->name, ast->state);
707                 }
708                 fr->frametype = AST_FRAME_VOICE;
709                 fr->subclass = AST_FORMAT_G723_1;
710                 fr->datalen = res - sizeof(struct vofr_hdr);
711                 fr->data = ((char *)vh) + sizeof(struct vofr_hdr);
712                 fr->src = type;
713                 /* XXX Byte swapping is a bug XXX */
714                 swapping = fr->data;
715                 for (x=0;x<fr->datalen/2;x++)
716                         swapping[x] = ntohs(swapping[x]);
717                 fr->offset = AST_FRIENDLY_OFFSET;
718                 /* Thirty ms of sound per frame */
719                 fr->timelen = 30;
720                 return fr;
721         default:
722                 ast_log(LOG_WARNING, "Don't know what to do with data type %d frames\n", vh->dtype);
723         }
724         /* If we don't know what it is, send a NULL frame */
725         fr->frametype = AST_FRAME_NULL;
726         fr->subclass = 0;
727         return fr;
728 }
729
730 static int vofr_write(struct ast_channel *ast, struct ast_frame *frame)
731 {
732         struct vofr_hdr *vh;
733         struct vofr_pvt *p = ast->pvt->pvt;
734         short *swapping;
735     int x;
736         char *start;
737         int res;
738         /* Write a frame of (presumably voice) data */
739         if (frame->frametype != AST_FRAME_VOICE) {
740                 ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
741                 return -1;
742         }
743         if (frame->subclass != AST_FORMAT_G723_1) {
744                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
745                 return -1;
746         }
747         /* If we get here, we have a voice frame of G.723.1 data.  First check to be
748            sure we have enough headroom for the vofr header.  If there isn't enough
749            headroom, we're lazy and just error out rather than copying it into the
750            output buffer, because applications should always leave AST_FRIENDLY_OFFSET
751            bytes just for this reason. */
752         if (frame->offset < sizeof(struct vofr_hdr) + FR_API_MESS) {
753                 ast_log(LOG_WARNING, "Frame source '%s' didn't provide a friendly enough offset\n", (frame->src ? frame->src : "**Unknown**"));
754                 return -1;
755         }
756         /* XXX Byte swapping is a bug XXX */
757         swapping = frame->data;
758         for (x=0;x<frame->datalen/2;x++)
759                 swapping[x] = ntohs(swapping[x]);
760         vh = (struct vofr_hdr *)(frame->data - sizeof(struct vofr_hdr));
761         /* Some versions of the API have some header mess that needs to be
762            zero'd out and acounted for..  */
763         start = ((void *)vh) - FR_API_MESS;
764         if (start)
765                 memset(start, 0, FR_API_MESS);
766         /* Now we fill in the vofr header */
767         vh->control = VOFR_CONTROL_VOICE;
768         vh->dtype = VOFR_TYPE_VOICE;
769         vh->vflags = VOFR_ROUTE_NONE;
770         vh->dlcih = p->dlcih;
771         vh->dlcil = p->dlcil;
772         vh->cid = p->cid;
773         vh->remid = VOFR_CARD_TYPE_ASTERISK;
774         vh->mod = VOFR_MODULATION_SINGLE;
775         res = vofr_xmit(p, start, 
776                                 VOFR_HDR_SIZE + frame->datalen + FR_API_MESS);
777         res -= FR_API_MESS;
778         /* XXX Byte swapping is a bug, but get it back to the right format XXX */
779         swapping = frame->data;
780         for (x=0;x<frame->datalen/2;x++)
781                 swapping[x] = htons(swapping[x]);
782         if (res != VOFR_HDR_SIZE + frame->datalen) {
783                 ast_log(LOG_WARNING, "Unable to write frame correctly\n");
784                 return -1;
785         }
786         return 0;
787 }
788
789 static int vofr_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
790 {
791         struct vofr_pvt *p = newchan->pvt->pvt;
792         if (p->owner != oldchan) {
793                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
794                 return -1;
795         }
796         p->owner = newchan;
797         return 0;
798 }
799
800 static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
801 {
802         struct ast_channel *tmp;
803         tmp = ast_channel_alloc(0);
804         if (tmp) {
805 #ifdef OLD_SANGOMA_API
806                 snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.spkt_device);
807 #else
808                 snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.sll_device);
809 #endif
810                 tmp->type = type;
811                 tmp->fds[0] = i->s;
812                 /* Adtran VoFR supports only G723.1 format data.  G711 (ulaw) would be nice too */
813                 tmp->nativeformats = AST_FORMAT_G723_1;
814                 tmp->state = state;
815                 if (state == AST_STATE_RING)
816                         tmp->rings = 1;
817                 tmp->writeformat = AST_FORMAT_G723_1;
818                 tmp->readformat = AST_FORMAT_G723_1;
819                 tmp->pvt->pvt = i;
820                 tmp->pvt->send_digit = vofr_digit;
821                 tmp->pvt->call = vofr_call;
822                 tmp->pvt->hangup = vofr_hangup;
823                 tmp->pvt->answer = vofr_answer;
824                 tmp->pvt->read = vofr_read;
825                 tmp->pvt->write = vofr_write;
826                 tmp->pvt->fixup = vofr_fixup;
827                 if (strlen(i->language))
828                         strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
829                 i->owner = tmp;
830                 ast_mutex_lock(&usecnt_lock);
831                 usecnt++;
832                 ast_mutex_unlock(&usecnt_lock);
833                 ast_update_use_count();
834                 strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
835                 if (state != AST_STATE_DOWN) {
836                         if (ast_pbx_start(tmp)) {
837                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
838                                 ast_hangup(tmp);
839                                 tmp = NULL;
840                         }
841                 }
842         } else
843                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
844         return tmp;
845 }
846
847 static int vofr_mini_packet(struct vofr_pvt *i, struct vofr_hdr *pkt, int len)
848 {
849         /* Here, we're looking for rings or off hooks -- signals that
850            something is about to happen and we need to start the 
851            PBX thread */
852         switch(pkt->dtype) {
853         case VOFR_TYPE_SIGNAL:
854                 switch(pkt->data[0]) {
855                 case VOFR_SIGNAL_RING:
856                         /* If we get a RING, we definitely want to start a new thread */
857                         if (!i->owner) {
858                                 i->ringgothangup = 0;
859                                 vofr_new(i, AST_STATE_RING);
860                         } else
861                                 ast_log(LOG_WARNING, "Got a ring, but there's an owner?\n");
862                         break;
863                 case VOFR_SIGNAL_OFF_HOOK:
864                         /* Network termination, go off hook */
865 #if 0
866                         ast_log(LOG_DEBUG, "Off hook\n");
867 #endif
868                         vofr_xmit_signal(i, 0x10, 2);
869                         if (!i->owner)
870                                 vofr_new(i, AST_STATE_UP);
871                         else
872                                 ast_log(LOG_WARNING, "Got an offhook, but there's an owner?\n");
873                         break;
874                 case VOFR_SIGNAL_ON_HOOK:
875                         break;
876                 case VOFR_SIGNAL_UNKNOWN:
877                         switch(pkt->data[1]) {
878                         case 0x1:
879                                 /* ignore */
880                                 break;
881                         case 0x6:
882                                 /* A remote hangup request */
883                                 if (option_debug)
884                                         ast_log(LOG_DEBUG, "Sending hangup reply\n");
885                                 send_hangup(i);
886                                 break;
887                         default:
888                                 ast_log(LOG_WARNING, "Unexected 'unknown' signal '%d'\n", pkt->data[1]);
889                         }
890                         break;
891                 default:
892                         ast_log(LOG_DEBUG, "Unknown signal type '%d'\n", pkt->data[0]);
893                 }                       
894                 break;
895         case VOFR_TYPE_VOICE:
896                 break;
897         default:
898                 ast_log(LOG_DEBUG, "Unknown packet type '%d'\n", pkt->dtype);
899         }
900         return 0;
901 }
902
903 static void *do_monitor(void *data)
904 {
905         fd_set rfds;
906         int n, res;
907         struct vofr_pvt *i;
908         /* This thread monitors all the frame relay interfaces which are not yet in use
909            (and thus do not have a separate thread) indefinitely */
910         /* From here on out, we die whenever asked */
911 #if 0
912         if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
913                 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
914                 return NULL;
915         }
916 #endif
917         for(;;) {
918                 /* Don't let anybody kill us right away.  Nobody should lock the interface list
919                    and wait for the monitor list, but the other way around is okay. */
920                 if (ast_mutex_lock(&monlock)) {
921                         ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
922                         return NULL;
923                 }
924                 /* Lock the interface list */
925                 if (ast_mutex_lock(&iflock)) {
926                         ast_log(LOG_ERROR, "Unable to grab interface lock\n");
927                         ast_mutex_unlock(&monlock);
928                         return NULL;
929                 }
930                 /* Build the stuff we're going to select on, that is the socket of every
931                    vofr_pvt that does not have an associated owner channel */
932                 n = -1;
933                 FD_ZERO(&rfds);
934                 i = iflist;
935                 while(i) {
936                         if (FD_ISSET(i->s, &rfds)) 
937 #ifdef OLD_SANGOMA_API
938                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.spkt_device);
939 #else
940                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.sll_device);
941 #endif
942                         if (!i->owner) {
943                                 /* This needs to be watched, as it lacks an owner */
944                                 FD_SET(i->s, &rfds);
945                                 if (i->s > n)
946                                         n = i->s;
947                         }
948                         i = i->next;
949                 }
950                 /* Okay, now that we know what to do, release the interface lock */
951                 ast_mutex_unlock(&iflock);
952                 
953                 /* And from now on, we're okay to be killed, so release the monitor lock as well */
954                 ast_mutex_unlock(&monlock);
955                 pthread_testcancel();
956                 /* Wait indefinitely for something to happen */
957                 res = ast_select(n + 1, &rfds, NULL, NULL, NULL);
958                 pthread_testcancel();
959                 /* Okay, select has finished.  Let's see what happened.  */
960                 if (res < 0) {
961                         if ((errno != EAGAIN) && (errno != EINTR))
962                                 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
963                         continue;
964                 }
965                 /* Alright, lock the interface list again, and let's look and see what has
966                    happened */
967                 if (ast_mutex_lock(&iflock)) {
968                         ast_log(LOG_WARNING, "Unable to lock the interface list\n");
969                         continue;
970                 }
971                 i = iflist;
972                 while(i) {
973                         if (FD_ISSET(i->s, &rfds)) {
974                                 if (i->owner) {
975 #ifdef OLD_SANGOMA_API
976                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->s, i->sa.spkt_device);
977 #else
978                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->s, i->sa.sll_device);
979 #endif
980                                         continue;
981                                 }
982                                 res = read(i->s, i->buf, sizeof(i->buf));
983                                 res -= FR_API_MESS;
984 #ifdef VOFRDUMPER
985                                 vofr_dump_packet(i->hdr, res);
986 #endif
987                                 vofr_mini_packet(i, i->hdr, res);
988                         }
989                         i=i->next;
990                 }
991                 ast_mutex_unlock(&iflock);
992         }
993         /* Never reached */
994         return NULL;
995         
996 }
997
998 static int restart_monitor(void)
999 {
1000         /* If we're supposed to be stopped -- stay stopped */
1001         if (monitor_thread == AST_PTHREADT_STOP)
1002                 return 0;
1003         if (ast_mutex_lock(&monlock)) {
1004                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1005                 return -1;
1006         }
1007         if (monitor_thread == pthread_self()) {
1008                 ast_mutex_unlock(&monlock);
1009                 ast_log(LOG_WARNING, "Cannot kill myself\n");
1010                 return -1;
1011         }
1012         if (monitor_thread != AST_PTHREADT_NULL) {
1013                 /* Wake up the thread */
1014                 pthread_kill(monitor_thread, SIGURG);
1015         } else {
1016                 /* Start a new monitor */
1017                 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1018                         ast_mutex_unlock(&monlock);
1019                         ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1020                         return -1;
1021                 }
1022         }
1023         ast_mutex_unlock(&monlock);
1024         return 0;
1025 }
1026
1027 static struct vofr_pvt *mkif(char *type, char *iface)
1028 {
1029         /* Make a vofr_pvt structure for this interface */
1030         struct vofr_pvt *tmp;
1031         int sndbuf = 4096;
1032
1033         tmp = malloc(sizeof(struct vofr_pvt));
1034         if (tmp) {
1035
1036                 /* Allocate a packet socket */
1037 #ifdef OLD_SANGOMA_API
1038                 tmp->s = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
1039 #else
1040                 /* Why the HELL does Sangoma change their API every damn time
1041                    they make a new driver release?!?!?!  Leave it the hell
1042                    alone this time.  */
1043                 tmp->s = socket(AF_WANPIPE, SOCK_RAW, 0);
1044 #endif          
1045
1046                 if (tmp->s < 0) {
1047                         ast_log(LOG_ERROR, "Unable to create socket: %s\n", strerror(errno));
1048                         free(tmp);
1049                         return NULL;
1050                 }
1051
1052 #ifdef OLD_SANGOMA_API
1053                 /* Prepare sockaddr for binding */
1054                 memset(&tmp->sa, 0, sizeof(tmp->sa));
1055                 strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device)-1);
1056                 tmp->sa.spkt_protocol = htons(0x16);
1057                 tmp->sa.spkt_family = AF_PACKET;
1058                 if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct sockaddr))) {
1059 #else
1060                 /* Prepare sockaddr for binding */
1061                 memset(&tmp->sa, 0, sizeof(tmp->sa));
1062                 tmp->sa.sll_family = AF_WANPIPE;
1063                 tmp->sa.sll_protocol = htons(ETH_P_IP);
1064                 strncpy(tmp->sa.sll_device, iface, sizeof(tmp->sa.sll_device)-1);
1065                 strncpy(tmp->sa.sll_card, "wanpipe1", sizeof(tmp->sa.sll_card)-1);
1066                 tmp->sa.sll_ifindex = 0;
1067                 if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct wan_sockaddr_ll))) {
1068 #endif          
1069                 /* Bind socket to specific interface */
1070 #ifdef OLD_SANGOMA_API
1071                         ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.spkt_device, 
1072 #else
1073                         ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.sll_device, 
1074 #endif
1075                                                                                 strerror(errno));
1076                         free(tmp);
1077                         return NULL;
1078                 }
1079                 
1080                 /* Set magic send buffer size */
1081                 if (setsockopt(tmp->s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
1082                         ast_log(LOG_ERROR, "Unable to set send buffer size to %d: %s\n", sndbuf, strerror(errno));
1083                         free(tmp);
1084                         return NULL;
1085                 }
1086                 tmp->owner =  NULL;
1087                 tmp->hdr = (struct vofr_hdr *)(tmp->buf + FR_API_MESS);
1088                 tmp->ohdr = (struct vofr_hdr *)(tmp->obuf + FR_API_MESS);
1089                 tmp->dlcil = 0;
1090                 tmp->dlcih = 0;
1091                 tmp->cid = 1;
1092                 tmp->ringgothangup = 0;
1093                 strncpy(tmp->language, language, sizeof(tmp->language)-1);
1094                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
1095                 /* User terminations are game for outgoing connections */
1096                 if (!strcasecmp(type, "user")) 
1097                         tmp->outgoing = 1;
1098                 else
1099                         tmp->outgoing = 0;
1100                 tmp->next = NULL;
1101                 /* Hang it up to be sure it's good */
1102                 send_hangup(tmp);
1103                 
1104         }
1105         return tmp;
1106 }
1107
1108 static struct ast_channel *vofr_request(char *type, int format, void *data)
1109 {
1110         int oldformat;
1111         struct vofr_pvt *p;
1112         struct ast_channel *tmp = NULL;
1113         /* We can only support G.723.1 formatted frames, but we should never
1114            be asked to support anything else anyway, since we've published
1115            our capabilities when we registered. */
1116         oldformat = format;
1117         format &= AST_FORMAT_G723_1;
1118         if (!format) {
1119                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1120                 return NULL;
1121         }
1122         /* Search for an unowned channel */
1123         if (ast_mutex_lock(&iflock)) {
1124                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1125                 return NULL;
1126         }
1127         p = iflist;
1128         while(p) {
1129                 if (!p->owner && p->outgoing) {
1130                         tmp = vofr_new(p, AST_STATE_DOWN);
1131                         break;
1132                 }
1133                 p = p->next;
1134         }
1135         ast_mutex_unlock(&iflock);
1136         restart_monitor();
1137         return tmp;
1138 }
1139
1140 static int __unload_module(void)
1141 {
1142         struct vofr_pvt *p, *pl;
1143         /* First, take us out of the channel loop */
1144         ast_channel_unregister(type);
1145         if (!ast_mutex_lock(&iflock)) {
1146                 /* Hangup all interfaces if they have an owner */
1147                 p = iflist;
1148                 while(p) {
1149                         if (p->owner)
1150                                 ast_softhangup(p->owner);
1151                         p = p->next;
1152                 }
1153                 iflist = NULL;
1154                 ast_mutex_unlock(&iflock);
1155         } else {
1156                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1157                 return -1;
1158         }
1159         if (!ast_mutex_lock(&monlock)) {
1160                 if (monitor_thread) {
1161                         pthread_cancel(monitor_thread);
1162                         pthread_kill(monitor_thread, SIGURG);
1163                         pthread_join(monitor_thread, NULL);
1164                 }
1165                 monitor_thread = AST_PTHREADT_STOP;
1166                 ast_mutex_unlock(&monlock);
1167         } else {
1168                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1169                 return -1;
1170         }
1171
1172         if (!ast_mutex_lock(&iflock)) {
1173                 /* Destroy all the interfaces and free their memory */
1174                 p = iflist;
1175                 while(p) {
1176                         /* Close the socket, assuming it's real */
1177                         if (p->s > -1)
1178                                 close(p->s);
1179                         pl = p;
1180                         p = p->next;
1181                         /* Free associated memory */
1182                         free(pl);
1183                 }
1184                 iflist = NULL;
1185                 ast_mutex_unlock(&iflock);
1186         } else {
1187                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1188                 return -1;
1189         }
1190                 
1191         return 0;
1192 }
1193
1194 int unload_module()
1195 {
1196         return __unload_module();
1197 }
1198
1199 int load_module()
1200 {
1201         struct ast_config *cfg;
1202         struct ast_variable *v;
1203         struct vofr_pvt *tmp;
1204         cfg = ast_load(config);
1205
1206         /* We *must* have a config file otherwise stop immediately */
1207         if (!cfg) {
1208                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1209                 return -1;
1210         }
1211         if (ast_mutex_lock(&iflock)) {
1212                 /* It's a little silly to lock it, but we mind as well just to be sure */
1213                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1214                 return -1;
1215         }
1216         v = ast_variable_browse(cfg, "interfaces");
1217         while(v) {
1218                 /* Create the interface list */
1219                 if (!strcasecmp(v->name, "user") ||
1220                         !strcasecmp(v->name, "network")) {
1221                                 tmp = mkif(v->name, v->value);
1222                                 if (tmp) {
1223                                         tmp->next = iflist;
1224                                         iflist = tmp;
1225                                 } else {
1226                                         ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1227                                         ast_destroy(cfg);
1228                                         ast_mutex_unlock(&iflock);
1229                                         __unload_module();
1230                                         return -1;
1231                                 }
1232                 } else if (!strcasecmp(v->name, "context")) {
1233                         strncpy(context, v->value, sizeof(context)-1);
1234                 } else if (!strcasecmp(v->name, "language")) {
1235                         strncpy(language, v->value, sizeof(language)-1);
1236                 }
1237                 v = v->next;
1238         }
1239         ast_mutex_unlock(&iflock);
1240         /* Make sure we can register our AdtranVoFR channel type */
1241         if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
1242                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1243                 ast_destroy(cfg);
1244                 __unload_module();
1245                 return -1;
1246         }
1247         ast_destroy(cfg);
1248         /* And start the monitor for the first time */
1249         restart_monitor();
1250         return 0;
1251 }
1252
1253 int usecount()
1254 {
1255         int res;
1256         ast_mutex_lock(&usecnt_lock);
1257         res = usecnt;
1258         ast_mutex_unlock(&usecnt_lock);
1259         return res;
1260 }
1261
1262 char *key()
1263 {
1264         return ASTERISK_GPL_KEY;
1265 }
1266
1267 char *description()
1268 {
1269         return desc;
1270 }
1271