Version 0.1.3 from FTP
[asterisk/asterisk.git] / channels / chan_ixj.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * QuickNet Internet Phone Jack Channel
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 <pthread.h>
16 #include <string.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 <sys/time.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <arpa/inet.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <linux/if_packet.h>
33 #include <linux/if_ether.h>
34 #include "ixjuser.h"
35 #include "DialTone.h"
36
37 #define IXJ_MAX_BUF 480
38
39 static char *desc = "QuickNet Internet Phone Jack";
40 static char *type = "PhoneJack";
41 static char *tdesc = "QuickNet Internet Phone Jack";
42 static char *config = "ixj.conf";
43
44 /* Default context for dialtone mode */
45 static char context[AST_MAX_EXTENSION] = "default";
46
47 char *ignore_rcs_id_for_chan_ixj = ixjuser_h_rcsid;
48
49 static int usecnt =0;
50 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
51
52 /* Protect the interface list (of ixj_pvt's) */
53 static pthread_mutex_t iflock = PTHREAD_MUTEX_INITIALIZER;
54
55 /* Protect the monitoring thread, so only one process can kill or start it, and not
56    when it's doing something critical. */
57 static pthread_mutex_t monlock = PTHREAD_MUTEX_INITIALIZER;
58
59 /* This is the thread for the monitor which checks for input on the channels
60    which are not currently in use.  */
61 static pthread_t monitor_thread = -1;
62
63 static int restart_monitor(void);
64
65 /* The private structures of the Phone Jack channels are linked for
66    selecting outgoing channels */
67    
68 #define MODE_DIALTONE   1
69 #define MODE_IMMEDIATE  2
70    
71 static struct ixj_pvt {
72         int fd;                                                 /* Raw file descriptor for this device */
73         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
74         int mode;                                               /* Is this in the  */
75         int lastformat;                                 /* Last output format */
76         int lastinput;                                  /* Last input format */
77         int ministate;                                  /* Miniature state, for dialtone mode */
78         char dev[256];                                  /* Device name */
79         struct ixj_pvt *next;                   /* Next channel in list */
80         struct ast_frame fr;                    /* Frame */
81         char offset[AST_FRIENDLY_OFFSET];
82         char buf[IXJ_MAX_BUF];                                  /* Static buffer for reading frames */
83         int obuflen;
84         int dialtone;
85         char context[AST_MAX_EXTENSION];
86         char obuf[IXJ_MAX_BUF * 2];
87         char ext[AST_MAX_EXTENSION];
88 } *iflist = NULL;
89
90 static int ixj_digit(struct ast_channel *ast, char digit)
91 {
92         struct ixj_pvt *p;
93         int outdigit;
94         p = ast->pvt->pvt;
95         switch(digit) {
96         case '0':
97         case '1':
98         case '2':
99         case '3':
100         case '4':
101         case '5':
102         case '6':
103         case '7':
104         case '8':
105         case '9':
106                 outdigit = digit - '0' + 1;
107                 break;
108         case '*':
109                 outdigit = 11;
110                 break;
111         case '#':
112                 outdigit = 12;
113                 break;
114         default:
115                 ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
116                 return -1;
117         }
118         ioctl(p->fd, IXJCTL_PLAY_TONE, digit);
119         return 0;
120 }
121
122 static int ixj_call(struct ast_channel *ast, char *dest, int timeout)
123 {
124         struct ixj_pvt *p;
125         p = ast->pvt->pvt;
126         if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
127                 ast_log(LOG_WARNING, "ixj_call called on %s, neither down nor reserved\n", ast->name);
128                 return -1;
129         }
130         /* When we call, it just works, really, there's no destination...  Just
131            ring the phone and wait for someone to answer */
132         if (option_debug)
133                 ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fd);
134         ioctl(p->fd, IXJCTL_RING_START);
135         ast->state = AST_STATE_RINGING;
136         return 0;
137 }
138
139 static int ixj_hangup(struct ast_channel *ast)
140 {
141         struct ixj_pvt *p;
142         p = ast->pvt->pvt;
143         if (option_debug)
144                 ast_log(LOG_DEBUG, "ixj_hangup(%s)\n", ast->name);
145         if (!ast->pvt->pvt) {
146                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
147                 return 0;
148         }
149         /* XXX Is there anything we can do to really hang up except stop recording? */
150         ast->state = AST_STATE_DOWN;
151         if (ioctl(p->fd, IXJCTL_REC_STOP))
152                 ast_log(LOG_WARNING, "Failed to stop recording\n");
153         if (ioctl(p->fd, IXJCTL_PLAY_STOP))
154                 ast_log(LOG_WARNING, "Failed to stop playing\n");
155         if (ioctl(p->fd, IXJCTL_RING_STOP))
156                 ast_log(LOG_WARNING, "Failed to stop ringing\n");
157         if (ioctl(p->fd, IXJCTL_CPT_STOP))
158                 ast_log(LOG_WARNING, "Failed to stop sounds\n");
159         /* If they're off hook, give a busy signal */
160         if (ioctl(p->fd, IXJCTL_HOOKSTATE))
161                 ioctl(p->fd, IXJCTL_BUSY);
162         p->lastformat = -1;
163         p->lastinput = -1;
164         p->ministate = 0;
165         p->obuflen = 0;
166         p->dialtone = 0;
167         memset(p->ext, 0, sizeof(p->ext));
168         ((struct ixj_pvt *)(ast->pvt->pvt))->owner = NULL;
169         pthread_mutex_lock(&usecnt_lock);
170         usecnt--;
171         if (usecnt < 0) 
172                 ast_log(LOG_WARNING, "Usecnt < 0???\n");
173         pthread_mutex_unlock(&usecnt_lock);
174         ast_update_use_count();
175         if (option_verbose > 2) 
176                 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
177         ast->pvt->pvt = NULL;
178         ast->state = AST_STATE_DOWN;
179         restart_monitor();
180         return 0;
181 }
182
183 static int ixj_setup(struct ast_channel *ast)
184 {
185         struct ixj_pvt *p;
186         p = ast->pvt->pvt;
187         ioctl(p->fd, IXJCTL_CPT_STOP);
188         /* Nothing to answering really, just start recording */
189         if (ast->format & AST_FORMAT_G723_1) {
190                 /* Prefer g723 */
191                 ioctl(p->fd, IXJCTL_REC_STOP);
192                 if (p->lastinput != AST_FORMAT_G723_1) {
193                         p->lastinput = AST_FORMAT_G723_1;
194                         if (ioctl(p->fd, IXJCTL_REC_CODEC, G723_63)) {
195                                 ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
196                                 return -1;
197                         }
198                 }
199         } else if (ast->format & AST_FORMAT_SLINEAR) {
200                 ioctl(p->fd, IXJCTL_REC_STOP);
201                 if (p->lastinput != AST_FORMAT_SLINEAR) {
202                         p->lastinput = AST_FORMAT_SLINEAR;
203                         if (ioctl(p->fd, IXJCTL_REC_CODEC, LINEAR16)) {
204                                 ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
205                                 return -1;
206                         }
207                 }
208         } else {
209                 ast_log(LOG_WARNING, "Can't do format %d\n", ast->format);
210                 return -1;
211         }
212         if (ioctl(p->fd, IXJCTL_REC_START)) {
213                 ast_log(LOG_WARNING, "Failed to start recording\n");
214                 return -1;
215         }
216         return 0;
217 }
218
219 static int ixj_answer(struct ast_channel *ast)
220 {
221         ixj_setup(ast);
222         if (option_debug)
223                 ast_log(LOG_DEBUG, "ixj_answer(%s)\n", ast->name);
224         ast->rings = 0;
225         ast->state = AST_STATE_UP;
226         return 0;
227 }
228
229 static char ixj_2digit(char c)
230 {
231         if (c == 12)
232                 return '#';
233         else if (c == 11)
234                 return '*';
235         else if ((c < 10) && (c >= 0))
236                 return '0' + c - 1;
237         else
238                 return '?';
239 }
240
241 static struct ast_frame  *ixj_read(struct ast_channel *ast)
242 {
243         int res;
244         IXJ_EXCEPTION ixje;
245         struct ixj_pvt *p = ast->pvt->pvt;
246         char digit;
247
248         /* Some nice norms */
249         p->fr.datalen = 0;
250         p->fr.timelen = 0;
251         p->fr.data =  NULL;
252         p->fr.src = type;
253         p->fr.offset = 0;
254         p->fr.mallocd=0;
255
256         ixje.bytes = ioctl(p->fd, IXJCTL_EXCEPTION);
257         if (ixje.bits.dtmf_ready)  {
258                 /* We've got a digit -- Just handle this nicely and easily */
259                 digit =  ioctl(p->fd, IXJCTL_GET_DTMF_ASCII);
260                 p->fr.subclass = digit;
261                 p->fr.frametype = AST_FRAME_DTMF;
262                 return &p->fr;
263         }
264         if (ixje.bits.hookstate) {
265                 res = ioctl(p->fd, IXJCTL_HOOKSTATE);
266                 /* See if we've gone on hook, if so, notify by returning NULL */
267                 if (!res)
268                         return NULL;
269                 else {
270                         if (ast->state == AST_STATE_RINGING) {
271                                 /* They've picked up the phone */
272                                 p->fr.frametype = AST_FRAME_CONTROL;
273                                 p->fr.subclass = AST_CONTROL_ANSWER;
274                                 ixj_setup(ast);
275                                 ast->state = AST_STATE_UP;
276                                 return &p->fr;
277                         }  else 
278                                 ast_log(LOG_WARNING, "Got off hook in weird state\n");
279                 }
280         }
281 #if 0
282         if (ixje.bits.pstn_ring)
283                 ast_verbose("Unit is ringing\n");
284         if (ixje.bits.caller_id) {
285                 ast_verbose("We have caller ID: %s\n");
286         }
287 #endif
288         /* Try to read some data... */
289         CHECK_BLOCKING(ast);
290         res = read(p->fd, p->buf, IXJ_MAX_BUF);
291         ast->blocking = 0;
292         if (res < 0) {
293 #if 0
294                 if (errno == EAGAIN) {
295                         ast_log(LOG_WARNING, "Null frame received\n");
296                         p->fr.frametype = AST_FRAME_NULL;
297                         p->fr.subclass = 0;
298                         return &p->fr;
299                 }
300 #endif
301                 ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
302                 return NULL;
303         }
304         p->fr.data = p->buf;
305         p->fr.datalen = res;
306         p->fr.frametype = AST_FRAME_VOICE;
307         p->fr.subclass = p->lastinput;
308         p->fr.offset = AST_FRIENDLY_OFFSET;
309         return &p->fr;
310 }
311
312 static int ixj_write_buf(struct ixj_pvt *p, char *buf, int len, int frlen)
313 {
314         int res;
315         /* Store as much of the buffer as we can, then write fixed frames */
316         int space = sizeof(p->obuf) - p->obuflen;
317         /* Make sure we have enough buffer space to store the frame */
318         if (space < len)
319                 len = space;
320         memcpy(p->obuf + p->obuflen, buf, len);
321         p->obuflen += len;
322         while(p->obuflen > frlen) {
323 #if 0
324                 res = frlen;
325                 ast_log(LOG_DEBUG, "Wrote %d bytes\n", res);
326 #else
327                 res = write(p->fd, p->obuf, frlen);
328 #endif
329                 if (res != frlen) {
330                         if (res < 1) {
331                                 ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
332                                 return -1;
333                         } else {
334                                 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
335                         }
336                 }
337                 p->obuflen -= frlen;
338                 /* Move memory if necessary */
339                 if (p->obuflen) 
340                         memmove(p->obuf, p->obuf + frlen, p->obuflen);
341         }
342         return len;
343 }
344
345 static int ixj_write(struct ast_channel *ast, struct ast_frame *frame)
346 {
347         struct ixj_pvt *p = ast->pvt->pvt;
348         int res;
349         int maxfr=0;
350         char *pos;
351         int sofar;
352         int expected;
353         /* Write a frame of (presumably voice) data */
354         if (frame->frametype != AST_FRAME_VOICE) {
355                 ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
356                 ast_frfree(frame);
357                 return -1;
358         }
359         if (!(frame->subclass & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR))) {
360                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
361                 ast_frfree(frame);
362                 return -1;
363         }
364         if (frame->subclass == AST_FORMAT_G723_1) {
365                 if (p->lastformat != AST_FORMAT_G723_1) {
366                         ioctl(p->fd, IXJCTL_PLAY_STOP);
367                         if (ioctl(p->fd, IXJCTL_PLAY_CODEC, G723_63)) {
368                                 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
369                                 return -1;
370                         }
371                         p->lastformat = AST_FORMAT_G723_1;
372                         /* Reset output buffer */
373                         p->obuflen = 0;
374                 }
375                 if (frame->datalen > 24) {
376                         ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
377                         return -1;
378                 }
379                 maxfr = 24;
380         } else if (frame->subclass == AST_FORMAT_SLINEAR) {
381                 if (p->lastformat != AST_FORMAT_SLINEAR) {
382                         ioctl(p->fd, IXJCTL_PLAY_STOP);
383                         if (ioctl(p->fd, IXJCTL_PLAY_CODEC, LINEAR16)) {
384                                 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
385                                 return -1;
386                         }
387                         p->lastformat = AST_FORMAT_SLINEAR;
388                         /* Reset output buffer */
389                         p->obuflen = 0;
390                 }
391                 maxfr = 480;
392         }
393         if (ioctl(p->fd, IXJCTL_PLAY_START)) {
394                 ast_log(LOG_WARNING, "Failed to start recording\n");
395                 return -1;
396         }
397         /* If we get here, we have a voice frame of Appropriate data */
398         sofar = 0;
399         pos = frame->data;
400         while(sofar < frame->datalen) {
401                 /* Write in no more than maxfr sized frames */
402                 expected = frame->datalen - sofar;
403                 if (maxfr < expected)
404                         expected = maxfr;
405                 /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX */
406                 if (frame->datalen != 4) {
407                         res = ixj_write_buf(p, pos, expected, maxfr);
408                         if (res != expected) {
409                                 if (res < 0) 
410                                         ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
411                                 else
412                                         ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
413                                 return -1;
414                         }
415                         sofar += res;
416                         pos += res;
417                 } else
418                         sofar += 4;
419         }
420         return 0;
421 }
422
423 static struct ast_channel *ixj_new(struct ixj_pvt *i, int state)
424 {
425         struct ast_channel *tmp;
426         tmp = ast_channel_alloc();
427         if (tmp) {
428                 snprintf(tmp->name, sizeof(tmp->name), "PhoneJack/%s", i->dev + 5);
429                 tmp->type = type;
430                 tmp->fd = i->fd;
431                 /* XXX Switching formats silently causes kernel panics XXX */
432                 tmp->format = AST_FORMAT_G723_1 /* | AST_FORMAT_SLINEAR */;
433                 tmp->state = state;
434                 if (state == AST_STATE_RING)
435                         tmp->rings = 1;
436                 tmp->pvt->pvt = i;
437                 tmp->pvt->send_digit = ixj_digit;
438                 tmp->pvt->call = ixj_call;
439                 tmp->pvt->hangup = ixj_hangup;
440                 tmp->pvt->answer = ixj_answer;
441                 tmp->pvt->read = ixj_read;
442                 tmp->pvt->write = ixj_write;
443                 strncpy(tmp->context, i->context, sizeof(tmp->context));
444                 if (strlen(i->ext))
445                         strncpy(tmp->exten, i->ext, sizeof(tmp->exten));
446                 i->owner = tmp;
447                 pthread_mutex_lock(&usecnt_lock);
448                 usecnt++;
449                 pthread_mutex_unlock(&usecnt_lock);
450                 ast_update_use_count();
451                 if (state != AST_STATE_DOWN) {
452                         if (state == AST_STATE_RING)
453                                 ioctl(tmp->fd, IXJCTL_RINGBACK);
454                         if (ast_pbx_start(tmp)) {
455                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
456                                 ast_hangup(tmp);
457                         }
458                 }
459         } else
460                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
461         return tmp;
462 }
463
464 static void ixj_mini_packet(struct ixj_pvt *i)
465 {
466         int res;
467         char buf[1024];
468         /* Ignore stuff we read... */
469         res = read(i->fd, buf, sizeof(buf));
470         if (res < 1) {
471                 ast_log(LOG_WARNING, "Read returned %d\n", res);
472                 return;
473         }
474 }
475
476 static void ixj_check_exception(struct ixj_pvt *i)
477 {
478         int offhook=0;
479         char digit[2] = {0 , 0};
480         IXJ_EXCEPTION ixje;
481         /* XXX Do something XXX */
482 #if 0
483         ast_log(LOG_DEBUG, "Exception!\n");
484 #endif
485         ixje.bytes = ioctl(i->fd, IXJCTL_EXCEPTION);
486         if (ixje.bits.dtmf_ready)  {
487                 digit[0] = ioctl(i->fd, IXJCTL_GET_DTMF_ASCII);
488                 if (i->mode == MODE_DIALTONE) {
489                         ioctl(i->fd, IXJCTL_PLAY_STOP);
490                         ioctl(i->fd, IXJCTL_REC_STOP);
491                         ioctl(i->fd, IXJCTL_CPT_STOP);
492                         i->dialtone = 0;
493                         if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
494                                 strcat(i->ext, digit);
495                         if (ast_exists_extension(NULL, i->context, i->ext, 1)) {
496                                 /* It's a valid extension in its context, get moving! */
497                                 ixj_new(i, AST_STATE_UP);
498                                 /* No need to restart monitor, we are the monitor */
499                                 if (i->owner) {
500                                         pthread_mutex_lock(&usecnt_lock);
501                                         usecnt--;
502                                         pthread_mutex_unlock(&usecnt_lock);
503                                         ast_update_use_count();
504                                         ixj_setup(i->owner);
505                                 }
506                         } else if (ast_exists_extension(NULL, "default", i->ext, 1)) {
507                                 /* Check the default, too... */
508                                 /* XXX This should probably be justified better XXX */
509                                 strncpy(i->context, "default", sizeof(i->context));
510                                 ixj_new(i, AST_STATE_UP);
511                                 if (i->owner) {
512                                         pthread_mutex_lock(&usecnt_lock);
513                                         usecnt--;
514                                         pthread_mutex_unlock(&usecnt_lock);
515                                         ast_update_use_count();
516                                         ixj_setup(i->owner);
517                                 }
518                         } else if ((strlen(i->ext) >= ast_pbx_longest_extension(i->context)) &&
519                                            (strlen(i->ext) >= ast_pbx_longest_extension("default"))) {
520                                 if (option_debug)
521                                         ast_log(LOG_DEBUG, "%s is too long\n", i->ext);
522                                 /* It's not a valid extension, give a busy signal */
523                                 ioctl(i->fd, IXJCTL_BUSY);
524                         }
525 #if 0
526                         ast_verbose("Extension is %s\n", i->ext);
527 #endif
528                 }
529         }
530         if (ixje.bits.hookstate) {
531                 offhook = ioctl(i->fd, IXJCTL_HOOKSTATE);
532                 if (offhook) {
533                         if (i->mode == MODE_IMMEDIATE) {
534                                 ixj_new(i, AST_STATE_RING);
535                         } else if (i->mode == MODE_DIALTONE) {
536                                 pthread_mutex_lock(&usecnt_lock);
537                                 usecnt++;
538                                 pthread_mutex_unlock(&usecnt_lock);
539                                 ast_update_use_count();
540                                 /* Play the dialtone */
541                                 i->dialtone++;
542                                 ioctl(i->fd, IXJCTL_PLAY_STOP);
543                                 ioctl(i->fd, IXJCTL_PLAY_CODEC, ULAW);
544                                 ioctl(i->fd, IXJCTL_PLAY_START);
545                         }
546                 } else {
547                         if (i->dialtone) {
548                                 pthread_mutex_lock(&usecnt_lock);
549                                 usecnt--;
550                                 pthread_mutex_unlock(&usecnt_lock);
551                                 ast_update_use_count();
552                         }
553                         memset(i->ext, 0, sizeof(i->ext));
554                         ioctl(i->fd, IXJCTL_CPT_STOP);
555                         ioctl(i->fd, IXJCTL_PLAY_STOP);
556                         ioctl(i->fd, IXJCTL_REC_STOP);
557                         i->dialtone = 0;
558                 }
559         }
560         if (ixje.bits.pstn_ring)
561                 ast_verbose("Unit is ringing\n");
562         if (ixje.bits.caller_id)
563                 ast_verbose("We have caller ID\n");
564         
565         
566 }
567
568 static void *do_monitor(void *data)
569 {
570         fd_set rfds, efds;
571         int n, res;
572         struct ixj_pvt *i;
573         int tonepos = 0;
574         /* The tone we're playing this round */
575         struct timeval tv = {0,0};
576         int dotone;
577         /* This thread monitors all the frame relay interfaces which are not yet in use
578            (and thus do not have a separate thread) indefinitely */
579         /* From here on out, we die whenever asked */
580         if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
581                 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
582                 return NULL;
583         }
584         for(;;) {
585                 /* Don't let anybody kill us right away.  Nobody should lock the interface list
586                    and wait for the monitor list, but the other way around is okay. */
587                 if (pthread_mutex_lock(&monlock)) {
588                         ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
589                         return NULL;
590                 }
591                 /* Lock the interface list */
592                 if (pthread_mutex_lock(&iflock)) {
593                         ast_log(LOG_ERROR, "Unable to grab interface lock\n");
594                         pthread_mutex_unlock(&monlock);
595                         return NULL;
596                 }
597                 /* Build the stuff we're going to select on, that is the socket of every
598                    ixj_pvt that does not have an associated owner channel */
599                 n = -1;
600                 FD_ZERO(&rfds);
601                 FD_ZERO(&efds);
602                 i = iflist;
603                 dotone = 0;
604                 while(i) {
605                         if (FD_ISSET(i->fd, &rfds)) 
606                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
607                         if (!i->owner) {
608                                 /* This needs to be watched, as it lacks an owner */
609                                 FD_SET(i->fd, &rfds);
610                                 FD_SET(i->fd, &efds);
611                                 if (i->fd > n)
612                                         n = i->fd;
613                                 if (i->dialtone) {
614                                         /* Remember we're going to have to come back and play
615                                            more dialtones */
616                                         if (!tv.tv_usec && !tv.tv_sec) {
617                                                 /* If we're due for a dialtone, play one */
618                                                 if (write(i->fd, DialTone + tonepos, 240) != 240)
619                                                         ast_log(LOG_WARNING, "Dial tone write error\n");
620                                         }
621                                         dotone++;
622                                 }
623                         }
624                         
625                         i = i->next;
626                 }
627                 /* Okay, now that we know what to do, release the interface lock */
628                 pthread_mutex_unlock(&iflock);
629                 
630                 /* And from now on, we're okay to be killed, so release the monitor lock as well */
631                 pthread_mutex_unlock(&monlock);
632                 /* Wait indefinitely for something to happen */
633                 if (dotone) {
634                         /* If we're ready to recycle the time, set it to 30 ms */
635                         tonepos += 240;
636                         if (tonepos >= sizeof(DialTone))
637                                         tonepos = 0;
638                         if (!tv.tv_usec && !tv.tv_sec) {
639                                 tv.tv_usec = 30000;
640                                 tv.tv_sec = 0;
641                         }
642                         res = select(n + 1, &rfds, NULL, &efds, &tv);
643                 } else {
644                         res = select(n + 1, &rfds, NULL, &efds, NULL);
645                         tv.tv_usec = 0;
646                         tv.tv_sec = 0;
647                         tonepos = 0;
648                 }
649                 /* Okay, select has finished.  Let's see what happened.  */
650                 if (res < 0) {
651                         ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
652                         continue;
653                 }
654                 /* If there are no fd's changed, just continue, it's probably time
655                    to play some more dialtones */
656                 if (!res)
657                         continue;
658                 /* Alright, lock the interface list again, and let's look and see what has
659                    happened */
660                 if (pthread_mutex_lock(&iflock)) {
661                         ast_log(LOG_WARNING, "Unable to lock the interface list\n");
662                         continue;
663                 }
664                 i = iflist;
665                 while(i) {
666                         if (FD_ISSET(i->fd, &rfds)) {
667                                 if (i->owner) {
668                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
669                                         continue;
670                                 }
671                                 ixj_mini_packet(i);
672                         }
673                         if (FD_ISSET(i->fd, &efds)) {
674                                 if (i->owner) {
675                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
676                                         continue;
677                                 }
678                                 ixj_check_exception(i);
679                         }
680                         i=i->next;
681                 }
682                 pthread_mutex_unlock(&iflock);
683         }
684         /* Never reached */
685         return NULL;
686         
687 }
688
689 static int restart_monitor()
690 {
691         /* If we're supposed to be stopped -- stay stopped */
692         if (monitor_thread == -2)
693                 return 0;
694         if (pthread_mutex_lock(&monlock)) {
695                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
696                 return -1;
697         }
698         if (monitor_thread == pthread_self()) {
699                 pthread_mutex_unlock(&monlock);
700                 ast_log(LOG_WARNING, "Cannot kill myself\n");
701                 return -1;
702         }
703         if (monitor_thread != -1) {
704                 pthread_cancel(monitor_thread);
705 #if 0
706                 pthread_join(monitor_thread, NULL);
707 #endif
708         }
709         /* Start a new monitor */
710         if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
711                 pthread_mutex_unlock(&monlock);
712                 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
713                 return -1;
714         }
715         pthread_mutex_unlock(&monlock);
716         return 0;
717 }
718
719 static struct ixj_pvt *mkif(char *iface, int mode)
720 {
721         /* Make a ixj_pvt structure for this interface */
722         struct ixj_pvt *tmp;
723 #if 0
724         int flags;      
725 #endif
726         
727         tmp = malloc(sizeof(struct ixj_pvt));
728         if (tmp) {
729                 tmp->fd = open(iface, O_RDWR);
730                 if (tmp->fd < 0) {
731                         ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
732                         free(tmp);
733                         return NULL;
734                 }
735                 ioctl(tmp->fd, IXJCTL_PLAY_STOP);
736                 ioctl(tmp->fd, IXJCTL_REC_STOP);
737                 ioctl(tmp->fd, IXJCTL_RING_STOP);
738                 ioctl(tmp->fd, IXJCTL_CPT_STOP);
739                 tmp->mode = mode;
740 #if 0
741                 flags = fcntl(tmp->fd, F_GETFL);
742                 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
743 #endif
744                 tmp->owner = NULL;
745                 tmp->lastformat = -1;
746                 tmp->lastinput = -1;
747                 tmp->ministate = 0;
748                 memset(tmp->ext, 0, sizeof(tmp->ext));
749                 strncpy(tmp->dev, iface, sizeof(tmp->dev));
750                 strncpy(tmp->context, context, sizeof(tmp->context));
751                 tmp->next = NULL;
752                 tmp->obuflen = 0;
753                 tmp->dialtone = 0;
754         }
755         return tmp;
756 }
757
758 static struct ast_channel *ixj_request(char *type, int format, void *data)
759 {
760         int oldformat;
761         struct ixj_pvt *p;
762         struct ast_channel *tmp = NULL;
763         char *name = data;
764         
765         oldformat = format;
766         format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR);
767         if (!format) {
768                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
769                 return NULL;
770         }
771         /* Search for an unowned channel */
772         if (pthread_mutex_lock(&iflock)) {
773                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
774                 return NULL;
775         }
776         p = iflist;
777         while(p) {
778                 if (!strcmp(name, p->dev + 5)) {
779                         if (!p->owner) {
780                                 tmp = ixj_new(p, AST_STATE_DOWN);
781                                 break;
782                         }
783                 }
784                 p = p->next;
785         }
786         pthread_mutex_unlock(&iflock);
787         restart_monitor();
788         return tmp;
789 }
790
791 int load_module()
792 {
793         struct ast_config *cfg;
794         struct ast_variable *v;
795         struct ixj_pvt *tmp;
796         int mode = MODE_IMMEDIATE;
797         cfg = ast_load(config);
798
799         /* We *must* have a config file otherwise stop immediately */
800         if (!cfg) {
801                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
802                 return -1;
803         }
804         if (pthread_mutex_lock(&iflock)) {
805                 /* It's a little silly to lock it, but we mind as well just to be sure */
806                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
807                 return -1;
808         }
809         v = ast_variable_browse(cfg, "interfaces");
810         while(v) {
811                 /* Create the interface list */
812                 if (!strcasecmp(v->name, "device")) {
813                                 tmp = mkif(v->value, mode);
814                                 if (tmp) {
815                                         tmp->next = iflist;
816                                         iflist = tmp;
817                                         
818                                 } else {
819                                         ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
820                                         ast_destroy(cfg);
821                                         pthread_mutex_unlock(&iflock);
822                                         unload_module();
823                                         return -1;
824                                 }
825                 } else if (!strcasecmp(v->name, "mode")) {
826                         if (!strncasecmp(v->value, "di", 2)) 
827                                 mode = MODE_DIALTONE;
828                         else if (!strncasecmp(v->value, "im", 2))
829                                 mode = MODE_IMMEDIATE;
830                         else
831                                 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
832                 } else if (!strcasecmp(v->name, "context")) {
833                         strncpy(context, v->value, sizeof(context));
834                 }
835                 v = v->next;
836         }
837         pthread_mutex_unlock(&iflock);
838         /* Make sure we can register our Adtranixj channel type */
839         if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, ixj_request)) {
840                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
841                 ast_destroy(cfg);
842                 unload_module();
843                 return -1;
844         }
845         ast_destroy(cfg);
846         /* And start the monitor for the first time */
847         restart_monitor();
848         return 0;
849 }
850
851
852
853 int unload_module()
854 {
855         struct ixj_pvt *p, *pl;
856         /* First, take us out of the channel loop */
857         ast_channel_unregister(type);
858         if (!pthread_mutex_lock(&iflock)) {
859                 /* Hangup all interfaces if they have an owner */
860                 p = iflist;
861                 while(p) {
862                         if (p->owner)
863                                 ast_softhangup(p->owner);
864                         p = p->next;
865                 }
866                 iflist = NULL;
867                 pthread_mutex_unlock(&iflock);
868         } else {
869                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
870                 return -1;
871         }
872         if (!pthread_mutex_lock(&monlock)) {
873                 if (monitor_thread > -1) {
874                         pthread_cancel(monitor_thread);
875                         pthread_join(monitor_thread, NULL);
876                 }
877                 monitor_thread = -2;
878                 pthread_mutex_unlock(&monlock);
879         } else {
880                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
881                 return -1;
882         }
883
884         if (!pthread_mutex_lock(&iflock)) {
885                 /* Destroy all the interfaces and free their memory */
886                 p = iflist;
887                 while(p) {
888                         /* Close the socket, assuming it's real */
889                         if (p->fd > -1)
890                                 close(p->fd);
891                         pl = p;
892                         p = p->next;
893                         /* Free associated memory */
894                         free(pl);
895                 }
896                 iflist = NULL;
897                 pthread_mutex_unlock(&iflock);
898         } else {
899                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
900                 return -1;
901         }
902                 
903         return 0;
904 }
905
906 int usecount()
907 {
908         int res;
909         pthread_mutex_lock(&usecnt_lock);
910         res = usecnt;
911         pthread_mutex_unlock(&usecnt_lock);
912         return res;
913 }
914
915 char *description()
916 {
917         return desc;
918 }
919