fix build issue for FreeBSD (bug #4802)
[asterisk/asterisk.git] / channels / chan_modem.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * A/Open ITU-56/2 Voice Modem Driver (Rockwell, IS-101, and others)
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
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 <stdlib.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <sys/time.h>
19 #include <errno.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <sys/termios.h>
26 #include <sys/signal.h>
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/lock.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/config.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/module.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/options.h"
39 #include "asterisk/vmodem.h"
40 #include "asterisk/utils.h"
41
42 /* Up to 10 seconds for an echo to arrive */
43 #define ECHO_TIMEOUT 10
44
45 static const char desc[] = "Generic Voice Modem Driver";
46 static const char tdesc[] = "Generic Voice Modem Channel Driver";
47 static const char type[] = "Modem";
48 static const char config[] = "modem.conf";
49 static char dialtype = 'T';
50 static int gmode = MODEM_MODE_IMMEDIATE;
51
52 /* Default modem type */
53 static char mtype[80] = "autodetect";
54 /* Default context for incoming calls */
55 static char context[AST_MAX_EXTENSION]= "default";
56
57 /* Default language */
58 static char language[MAX_LANGUAGE] = "";
59
60 /* Initialization String */
61 static char initstr[AST_MAX_INIT_STR] = "ATE0Q0";
62
63 /* Default MSN */
64 static char msn[AST_MAX_EXTENSION]="";
65
66 /* Default Listen */
67 static char incomingmsn[AST_MAX_EXTENSION]="";
68
69 /* Default DTMF-detection mode (i4l/asterisk) */
70 static int dtmfmode = MODEM_DTMF_AST;
71 /* Default DTMF-generation mode (i4l (outband) / asterisk (inband) */
72 static int dtmfmodegen = MODEM_DTMF_AST;
73
74 struct ast_dsp *dsp = NULL;
75
76 /* Default valid outgoing MSN */
77 static char outgoingmsn[AST_MAX_EXTENSION]="";
78
79 /* Default group */
80 static ast_group_t cur_group = 0;
81
82 static int usecnt =0;
83
84 static int baudrate = 115200;
85
86 static int stripmsd = 0;
87
88 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
89
90 /* Protect the interface list (of ast_modem_pvt's) */
91 AST_MUTEX_DEFINE_STATIC(iflock);
92
93 /* Protect the monitoring thread, so only one process can kill or start it, and not
94    when it's doing something critical. */
95 AST_MUTEX_DEFINE_STATIC(monlock);
96
97 /* This is the thread for the monitor which checks for input on the channels
98    which are not currently in use.  */
99 static pthread_t monitor_thread = AST_PTHREADT_NULL;
100
101 static int restart_monitor(void);
102
103 static struct ast_channel *modem_request(const char *type, int format, void *data, int *cause);
104 static int modem_digit(struct ast_channel *ast, char digit);
105 static int modem_call(struct ast_channel *ast, char *idest, int timeout);
106 static int modem_hangup(struct ast_channel *ast);
107 static int modem_answer(struct ast_channel *ast);
108 static struct ast_frame *modem_read(struct ast_channel *);
109 static int modem_write(struct ast_channel *ast, struct ast_frame *frame);
110 static int modem_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
111
112 static const struct ast_channel_tech modem_tech = {
113         .type = type,
114         .description = tdesc,
115         .capabilities = AST_FORMAT_SLINEAR,
116         .requester = modem_request,
117         .send_digit = modem_digit,
118         .call = modem_call,
119         .hangup = modem_hangup,
120         .answer = modem_answer,
121         .read = modem_read,
122         .write = modem_write,
123         .fixup = modem_fixup,
124 };
125
126 /* The private structures of the Phone Jack channels are linked for
127    selecting outgoing channels */
128    
129 static struct ast_modem_pvt  *iflist = NULL;
130
131 static int modem_digit(struct ast_channel *ast, char digit)
132 {
133         struct ast_modem_pvt *p;
134         p = ast->tech_pvt;
135         if (p->mc->dialdigit)
136                 return p->mc->dialdigit(p, digit);
137         ast_log(LOG_DEBUG, "Channel %s lacks digit dialing\n", ast->name);
138         return -1;
139 }
140
141 static struct ast_modem_driver *drivers = NULL;
142
143 static struct ast_modem_driver *find_capability(char *ident)
144 {
145         struct ast_modem_driver *mc;
146         int x;
147         mc = drivers;
148         while(mc) {
149                 for (x=0;mc->idents[x];x++) {
150                         if (!strcmp(ident, mc->idents[x])) 
151                                 break;
152                 }
153                 if (mc->idents[x])
154                         break;
155                 mc = mc->next;
156         }
157         if (mc) {
158                 if (mc->incusecnt)
159                         mc->incusecnt();
160         }
161         return mc;
162 }
163
164 static struct ast_modem_driver *find_driver(char *drv)
165 {
166         struct ast_modem_driver *mc;
167         mc = drivers;
168         while(mc) {
169                 if (!strcasecmp(mc->name, drv)) 
170                         break;
171                 mc = mc->next;
172         }
173         if (mc) {
174                 if (mc->incusecnt)
175                         mc->incusecnt();
176         }
177         return mc;
178 }
179
180 int ast_register_modem_driver(struct ast_modem_driver *mc)
181 {
182         mc->next = drivers;
183         drivers = mc;
184         return 0;
185 }
186
187 int ast_unregister_modem_driver(struct ast_modem_driver *mc)
188 {
189         struct ast_modem_driver *last = NULL, *cur;
190         cur = drivers;
191         while(cur) {
192                 if (cur == mc) {
193                         if (last)
194                                 last->next = mc->next;
195                         else
196                                 drivers = mc->next;
197                         return 0;
198                 }
199                 cur = cur->next;
200         }
201         return -1;
202 }
203
204 static int modem_call(struct ast_channel *ast, char *idest, int timeout)
205 {
206         struct ast_modem_pvt *p;
207         int ms = timeout;
208         char rdest[80], *where, dstr[100] = "";
209         char *stringp=NULL;
210         strncpy(rdest, idest, sizeof(rdest)-1);
211         stringp=rdest;
212         strsep(&stringp, ":");
213         where = strsep(&stringp, ":");
214         if (!where) {
215                 ast_log(LOG_WARNING, "Destination %s requres a real destination (device:destination)\n", idest);
216                 return -1;
217         }
218         p = ast->tech_pvt;
219         strncpy(dstr, where + p->stripmsd, sizeof(dstr) - 1);
220         /* if not a transfer or just sending tones, must be in correct state */
221         if (strcasecmp(rdest, "transfer") && strcasecmp(rdest,"sendtones")) {
222                 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
223                         ast_log(LOG_WARNING, "modem_call called on %s, neither down nor reserved\n", ast->name);
224                         return -1;
225                 }
226         } 
227         if (!strcasecmp(rdest,"transfer")) /* if a transfer, put in transfer stuff */
228         {
229                 snprintf(dstr, sizeof(dstr), "!,%s", where + p->stripmsd);
230         }
231         if (!strcasecmp(where, "handset")) {
232                 if (p->mc->setdev)
233                         if (p->mc->setdev(p, MODEM_DEV_HANDSET))
234                                 return -1;
235                 /* Should be immediately up */
236                 ast_setstate(ast, AST_STATE_UP);
237         } else {
238                 if (p->mc->setdev)
239                         if (p->mc->setdev(p, MODEM_DEV_TELCO_SPK))
240                                 return -1;
241                 if (p->mc->dial)
242                         p->mc->dial(p, dstr);
243                 ast_setstate(ast, AST_STATE_DIALING);
244                 while((ast->_state != AST_STATE_UP) && (ms > 0)) {
245                         ms = ast_waitfor(ast, ms);
246                         /* Just read packets and watch what happens */
247                         if (ms > 0) {
248                                 if (!modem_read(ast))
249                                         return -1;
250                         }
251                 }
252                 if (ms < 0)     
253                         return -1;
254         }
255         return 0;
256 }
257
258 int ast_modem_send(struct ast_modem_pvt *p, char *cmd, int len)
259 {
260         int i;
261         usleep(5000);
262         if (!len) {
263                 for(i = 0; cmd[i];)
264                    {
265                         if (fwrite(cmd + i,1,1,p->f) != 1)
266                            {
267                                 if (errno == EWOULDBLOCK) continue;
268                                 return -1;
269                            }
270                         i++;
271                    }
272                 tcdrain(fileno(p->f)); 
273                 fprintf(p->f,"\r\n");
274                 return 0;
275         } else {
276                 if (fwrite(cmd, 1, len, p->f) < len)
277                         return -1;
278                 return 0;
279         }
280 }
281
282 int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
283 {
284         int res = -1,c,i;
285         timeout *= 1000;
286         p->response[0] = 0;
287         c = i = 0;
288         do {
289                 res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
290                 if (res < 0) {
291                         strncpy(p->response, "(No Response)", sizeof(p->response)-1);
292                         return -1;
293                 }
294                   /* get no more then buffer length */
295                 while(i < sizeof(p->response) - 1)
296                 {
297                         c = fgetc(p->f);  /* get a char */
298                         if (c < 1) /* if error */
299                         {
300                                   /* if nothing in buffer, go back into timeout stuff */
301                                 if (errno == EWOULDBLOCK) break;
302                                 /* return as error */
303                                 strncpy(p->response, "(No Response)", sizeof(p->response)-1);
304                                 return -1;
305                         }
306                           /* save char */
307                         p->response[i++] = c;
308                         p->response[i] = 0;                     
309                           /* if end of input */
310                         if (c == '\n') break;
311                 }
312                 if (c >= 0)  /* if input terminated normally */
313                 {
314                           /* ignore just CR/LF */
315                         if (!strcmp(p->response,"\r\n"))
316                         {
317                                   /* reset input buffer stuff */
318                                 i = 0; 
319                                 p->response[0] = 0;
320                         }
321                         else /* otherwise return with info in buffer */
322                         {
323                                 return 0;
324                         }
325                 }
326         } while(timeout > 0);
327         strncpy(p->response, "(No Response)", sizeof(p->response)-1);
328         return -1;
329 }
330
331 int ast_modem_expect(struct ast_modem_pvt *p, char *result, int timeout)
332 {
333         int res = -1;
334         timeout *= 1000;
335         strncpy(p->response, "(No Response)", sizeof(p->response)-1);
336         do {
337                 res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
338                 if (res < 0) {
339                         return -1;
340                 }
341                 /* Read a response */
342                 fgets(p->response, sizeof(p->response), p->f);
343 #if     0
344                 fprintf(stderr, "Modem said: %s", p->response);
345 #endif
346                 if (!strncasecmp(p->response, result, strlen(result))) 
347                         return 0;
348         } while(timeout > 0);
349         return -1;
350 }
351
352 void ast_modem_trim(char *s)
353 {
354         int x;
355         x = strlen(s) - 1;
356         while(x >= 0) {
357                 if ((s[x] != '\r') && (s[x] != '\n') && (s[x] != ' '))
358                         break;
359                 s[x] = '\0';
360                 x--;
361         }
362 }
363
364 static int modem_setup(struct ast_modem_pvt *p, int baudrate)
365 {
366
367         /* Make sure there's a modem there and that it's in a reasonable 
368            mode.  Set the baud rate, etc.  */
369         char identity[256];
370         char *ident = NULL;
371         char etx[2] = { 0x10, '!' }; 
372         if (option_debug)
373                 ast_log(LOG_DEBUG, "Setting up modem %s\n", p->dev);
374         if (ast_modem_send(p, etx, 2)) {
375                 ast_log(LOG_WARNING, "Failed to send ETX?\n");
376                 return -1;
377         }
378         if (ast_modem_send(p, "\r\n", 2)) {
379                 ast_log(LOG_WARNING, "Failed to send enter?\n");
380                 return -1;
381         }
382         usleep(10000);
383         /* Read any outstanding stuff */
384         while(!ast_modem_read_response(p, 0));
385         if (ast_modem_send(p, "ATZ", 0)) {
386                 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
387                 return -1;
388         }
389         if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
390                 ast_log(LOG_WARNING, "Modem reset failed: %s\n", p->response);
391                 return -1;
392         }
393         if (ast_modem_send(p, p->initstr, 0)) {
394                 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
395                 return -1;
396         }
397         if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
398                 ast_log(LOG_WARNING, "Modem initialization failed: %s\n", p->response);
399                 return -1;
400         }
401         if (ast_modem_send(p, "ATI3", 0)) {
402                 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
403                 return -1;
404         }
405         if (ast_modem_read_response(p, ECHO_TIMEOUT)) {
406                 ast_log(LOG_WARNING, "Modem did not provide identification\n");
407                 return -1;
408         }
409         strncpy(identity, p->response, sizeof(identity)-1);
410         ast_modem_trim(identity);
411         if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
412                 ast_log(LOG_WARNING, "Modem did not provide identification\n");
413                 return -1;
414         }
415         if (!strcasecmp(mtype, "autodetect")) {
416                 p->mc = find_capability(identity);
417                 if (!p->mc) {
418                         ast_log(LOG_WARNING, "Unable to autodetect modem.  You'll need to specify a driver in modem.conf.  Please report modem identification (%s) and which driver works to markster@linux-support.net.\n", identity); 
419                         return -1;
420                 }
421         } else {
422                 p->mc = find_driver(mtype);
423                 if (!p->mc) {
424                         ast_log(LOG_WARNING, "No driver for modem type '%s'\n", mtype);
425                         return -1;
426                 }
427         }
428         if (p->mc->init) {
429                 if (p->mc->init(p)) {
430                         ast_log(LOG_WARNING, "Modem Initialization Failed on '%s', driver %s.\n", p->dev, p->mc->name);
431                         p->mc->decusecnt();
432                         return -1;
433                 }
434         }                       
435         if (option_verbose > 2) {
436                 ast_verbose(VERBOSE_PREFIX_3 "Configured modem %s with driver %s (%s)\n", p->dev, p->mc->name, p->mc->identify ? (ident = p->mc->identify(p)) : "No identification");
437         }
438         if (ident)
439                 free(ident);
440         return 0;
441 }
442
443 static int modem_hangup(struct ast_channel *ast)
444 {
445         struct ast_modem_pvt *p;
446         if (option_debug)
447                 ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
448         p = ast->tech_pvt;
449         /* Hang up */
450         if (p->mc->hangup)
451                 p->mc->hangup(p);
452         /* Re-initialize */
453         if (p->mc->init)
454                 p->mc->init(p);
455         ast_setstate(ast, AST_STATE_DOWN);
456         memset(p->cid_num, 0, sizeof(p->cid_num));
457         memset(p->cid_name, 0, sizeof(p->cid_name));
458         memset(p->dnid, 0, sizeof(p->dnid));
459         ((struct ast_modem_pvt *)(ast->tech_pvt))->owner = NULL;
460         ast_mutex_lock(&usecnt_lock);
461         usecnt--;
462         if (usecnt < 0) 
463                 ast_log(LOG_WARNING, "Usecnt < 0???\n");
464         ast_mutex_unlock(&usecnt_lock);
465         ast_update_use_count();
466         if (option_verbose > 2) 
467                 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
468         ast->tech_pvt = NULL;
469         ast_setstate(ast, AST_STATE_DOWN);
470         restart_monitor();
471         return 0;
472 }
473
474 static int modem_answer(struct ast_channel *ast)
475 {
476         struct ast_modem_pvt *p;
477         int res=0;
478         if (option_debug)
479                 ast_log(LOG_DEBUG, "modem_answer(%s)\n", ast->name);
480         p = ast->tech_pvt;
481         if (p->mc->answer) {
482                 res = p->mc->answer(p);
483         }
484         if (!res) {
485                 ast->rings = 0;
486                 ast_setstate(ast, AST_STATE_UP);
487         }
488         return res;
489 }
490
491 #if     0
492 static char modem_2digit(char c)
493 {
494         if (c == 12)
495                 return '#';
496         else if (c == 11)
497                 return '*';
498         else if ((c < 10) && (c >= 0))
499                 return '0' + c - 1;
500         else
501                 return '?';
502 }
503 #endif
504 static struct ast_frame *modem_read(struct ast_channel *ast)
505 {
506         struct ast_modem_pvt *p = ast->tech_pvt;
507         struct ast_frame *fr=NULL;
508         if (p->mc->read)
509                 fr = p->mc->read(p);
510         return fr;
511 }
512
513 static int modem_write(struct ast_channel *ast, struct ast_frame *frame)
514 {
515         int res=0;
516         long flags;
517         struct ast_modem_pvt *p = ast->tech_pvt;
518
519         /* Modems tend to get upset when they receive data whilst in
520          * command mode. This makes esp. dial commands short lived.
521          *     Pauline Middelink - 2002-09-24 */
522         if (ast->_state != AST_STATE_UP)
523                 return 0;
524
525         /* Temporarily make non-blocking */
526         flags = fcntl(ast->fds[0], F_GETFL);
527         fcntl(ast->fds[0], F_SETFL, flags | O_NONBLOCK);
528
529         if (p->mc->write)
530                 res = p->mc->write(p, frame);
531
532         /* Block again */
533         fcntl(ast->fds[0], F_SETFL, flags);
534         return res;
535 }
536
537 static int modem_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
538 {
539         struct ast_modem_pvt *p = newchan->tech_pvt;
540 ast_log(LOG_WARNING, "fixup called\n");
541         if (p->owner!=oldchan) {
542             ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n",oldchan,p->owner);
543             return -1;
544         }
545         p->owner = newchan;
546         return 0; 
547 }
548
549 struct ast_channel *ast_modem_new(struct ast_modem_pvt *i, int state)
550 {
551         struct ast_channel *tmp;
552         tmp = ast_channel_alloc(1);
553         if (tmp) {
554                 tmp->tech = &modem_tech;
555                 snprintf(tmp->name, sizeof(tmp->name), "Modem[%s]/%s", i->mc->name, i->dev + 5);
556                 tmp->type = type;
557                 tmp->fds[0] = i->fd;
558                 tmp->nativeformats = i->mc->formats;
559                 ast_setstate(tmp, state);
560                 if (state == AST_STATE_RING)
561                         tmp->rings = 1;
562                 tmp->tech_pvt = i;
563                 strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
564
565                 if (!ast_strlen_zero(i->cid_num))
566                         tmp->cid.cid_num = strdup(i->cid_num);
567                 if (!ast_strlen_zero(i->cid_name))
568                         tmp->cid.cid_name = strdup(i->cid_name);
569
570                 if (strlen(i->language))
571                         strncpy(tmp->language,i->language, sizeof(tmp->language)-1);
572                 if (strlen(i->dnid))
573                         strncpy(tmp->exten, i->dnid, sizeof(tmp->exten) - 1);
574                 i->owner = tmp;
575                 ast_mutex_lock(&usecnt_lock);
576                 usecnt++;
577                 ast_mutex_unlock(&usecnt_lock);
578                 ast_update_use_count();
579                 if (state != AST_STATE_DOWN) {
580                         if (ast_pbx_start(tmp)) {
581                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
582                                 ast_hangup(tmp);
583                                 tmp = NULL;
584                         }
585                 }
586         } else
587                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
588         return tmp;
589 }
590
591 static void modem_mini_packet(struct ast_modem_pvt *i)
592 {
593         struct ast_frame *fr;
594         fr = i->mc->read(i);
595         if (!fr) return;
596         if (fr->frametype == AST_FRAME_CONTROL) {
597                 if (fr->subclass == AST_CONTROL_RING) {
598                         ast_modem_new(i, AST_STATE_RING);
599                 }
600         }
601 }
602
603 static void *do_monitor(void *data)
604 {
605         fd_set rfds, efds;
606         int n, res;
607         struct ast_modem_pvt *i;
608         /* This thread monitors all the frame relay interfaces which are not yet in use
609            (and thus do not have a separate thread) indefinitely */
610         /* From here on out, we die whenever asked */
611 #if 0
612         if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
613                 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
614                 return NULL;
615         }
616 #endif
617         for(;;) {
618                 /* Don't let anybody kill us right away.  Nobody should lock the interface list
619                    and wait for the monitor list, but the other way around is okay. */
620                 if (ast_mutex_lock(&monlock)) {
621                         ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
622                         return NULL;
623                 }
624                 /* Lock the interface list */
625                 if (ast_mutex_lock(&iflock)) {
626                         ast_log(LOG_ERROR, "Unable to grab interface lock\n");
627                         ast_mutex_unlock(&monlock);
628                         return NULL;
629                 }
630                 /* Build the stuff we're going to select on, that is the socket of every
631                    ast_modem_pvt that does not have an associated owner channel */
632                 n = -1;
633                 FD_ZERO(&rfds);
634                 FD_ZERO(&efds);
635                 i = iflist;
636                 while(i) {
637                         if (FD_ISSET(i->fd, &rfds)) 
638                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
639                         if (!i->owner) {
640                                 /* This needs to be watched, as it lacks an owner */
641                                 FD_SET(i->fd, &rfds);
642                                 FD_SET(i->fd, &efds);
643                                 if (i->fd > n)
644                                         n = i->fd;
645                         }
646                         
647                         i = i->next;
648                 }
649                 /* Okay, now that we know what to do, release the interface lock */
650                 ast_mutex_unlock(&iflock);
651                 
652                 /* And from now on, we're okay to be killed, so release the monitor lock as well */
653                 ast_mutex_unlock(&monlock);
654 #if 0
655                 ast_log(LOG_DEBUG, "In monitor, n=%d, pid=%d\n", n, getpid());
656 #endif
657                 /* Wait indefinitely for something to happen */
658                 pthread_testcancel();
659                 res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
660                 pthread_testcancel();
661                 /* Okay, select has finished.  Let's see what happened.  */
662                 if (res < 1) {
663                         if ((errno != EINTR) && (errno != EAGAIN))
664                                 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
665                         continue;
666                 }
667                 /* Alright, lock the interface list again, and let's look and see what has
668                    happened */
669                 if (ast_mutex_lock(&iflock)) {
670                         ast_log(LOG_WARNING, "Unable to lock the interface list\n");
671                         continue;
672                 }
673                 i = iflist;
674                 while(i) {
675                         if (FD_ISSET(i->fd, &rfds) || FD_ISSET(i->fd, &efds)) {
676                                 if (i->owner) {
677                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
678                                         i = i->next;
679                                         continue;
680                                 }
681                                 modem_mini_packet(i);
682                         }
683                         i=i->next;
684                 }
685                 ast_mutex_unlock(&iflock);
686         }
687         /* Never reached */
688         return NULL;
689         
690 }
691
692 static int restart_monitor()
693 {
694         /* If we're supposed to be stopped -- stay stopped */
695         if (monitor_thread == AST_PTHREADT_STOP)
696                 return 0;
697         if (ast_mutex_lock(&monlock)) {
698                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
699                 return -1;
700         }
701         if (monitor_thread == pthread_self()) {
702                 ast_mutex_unlock(&monlock);
703                 ast_log(LOG_WARNING, "Cannot kill myself\n");
704                 return -1;
705         }
706         if (monitor_thread != AST_PTHREADT_NULL) {
707                 pthread_cancel(monitor_thread);
708                 /* Nudge it a little, as it's probably stuck in select */
709                 pthread_kill(monitor_thread, SIGURG);
710                 pthread_join(monitor_thread, NULL);
711         }
712         /* Start a new monitor */
713         if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
714                 ast_mutex_unlock(&monlock);
715                 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
716                 return -1;
717         }
718         ast_mutex_unlock(&monlock);
719         return 0;
720 }
721
722 static void stty(struct ast_modem_pvt *p)
723 {
724         struct termios mode;
725         memset(&mode, 0, sizeof(mode));
726         if (tcgetattr(p->fd, &mode)) {
727                 ast_log(LOG_WARNING, "Unable to get serial parameters on %s: %s\n", p->dev, strerror(errno));
728                 return;
729         }
730 #ifndef SOLARIS
731         cfmakeraw(&mode);
732 #else
733         mode.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
734                         |INLCR|IGNCR|ICRNL|IXON);
735         mode.c_oflag &= ~OPOST;
736         mode.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
737         mode.c_cflag &= ~(CSIZE|PARENB);
738         mode.c_cflag |= CS8;
739 #endif
740
741         cfsetispeed(&mode, B115200);
742         cfsetospeed(&mode, B115200);
743         if (tcsetattr(p->fd, TCSANOW, &mode)) 
744                 ast_log(LOG_WARNING, "Unable to set serial parameters on %s: %s\n", p->dev, strerror(errno));
745         
746 }
747
748 static struct ast_modem_pvt *mkif(char *iface)
749 {
750         /* Make a ast_modem_pvt structure for this interface */
751         struct ast_modem_pvt *tmp;
752 #if 0
753         int flags;      
754 #endif
755         
756         tmp = malloc(sizeof(struct ast_modem_pvt));
757         if (tmp) {
758                 memset(tmp, 0, sizeof(struct ast_modem_pvt));
759                 tmp->fd = open(iface, O_RDWR | O_NONBLOCK);
760                 if (tmp->fd < 0) {
761                         ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
762                         free(tmp);
763                         return NULL;
764                 }
765                 strncpy(tmp->language, language, sizeof(tmp->language)-1);
766                 strncpy(tmp->msn, msn, sizeof(tmp->msn)-1);
767                 strncpy(tmp->incomingmsn, incomingmsn, sizeof(tmp->incomingmsn)-1);
768                 tmp->dtmfmode = dtmfmode;
769                 tmp->dtmfmodegen = dtmfmodegen;
770                 snprintf(tmp->outgoingmsn, sizeof(tmp->outgoingmsn), ",%s,", outgoingmsn);
771                 strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
772                 /* Maybe in the future we want to allow variable
773                    serial settings */
774                 stty(tmp);
775                 tmp->f = fdopen(tmp->fd, "w+");
776                 /* Disable buffering */
777                 setvbuf(tmp->f, NULL, _IONBF,0);
778                 if (tmp->f < 0) {
779                         ast_log(LOG_WARNING, "Unable to fdopen '%s'\n", iface);
780                         free(tmp);
781                         return NULL;
782                 }
783                 tmp->owner = NULL;
784                 tmp->ministate = 0;
785                 tmp->stripmsd = stripmsd;
786                 tmp->dialtype = dialtype;
787                 tmp->mode = gmode;
788                 tmp->group = cur_group;
789                 memset(tmp->cid_num, 0, sizeof(tmp->cid_num));
790                 memset(tmp->cid_name, 0, sizeof(tmp->cid_name));
791                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
792                 strncpy(tmp->initstr, initstr, sizeof(tmp->initstr)-1);
793                 tmp->next = NULL;
794                 tmp->obuflen = 0;
795                 
796                 if (modem_setup(tmp, baudrate) < 0) {
797                         ast_log(LOG_WARNING, "Unable to configure modem '%s'\n", iface);
798                         free(tmp);
799                         return NULL;
800                 }
801         }
802         return tmp;
803 }
804
805 static struct ast_channel *modem_request(const char *type, int format, void *data, int *cause)
806 {
807         int oldformat;
808         struct ast_modem_pvt *p;
809         struct ast_channel *tmp = NULL;
810         char dev[80];
811         ast_group_t group = 0;
812         int groupint;
813         char *stringp=NULL;
814         strncpy(dev, (char *)data, sizeof(dev)-1);
815         stringp=dev;
816         strsep(&stringp, ":");
817         oldformat = format;
818
819         if (dev[0]=='g' && isdigit(dev[1])) {
820                 /* Retrieve the group number */
821                 if (sscanf(dev+1, "%u", &groupint) < 1) {
822                         ast_log(LOG_WARNING, "Unable to determine group from [%s]\n", (char *)data);
823                         return NULL;
824                 }
825                 group = 1 << groupint;
826         }
827
828         /* Search for an unowned channel */
829         if (ast_mutex_lock(&iflock)) {
830                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
831                 return NULL;
832         }
833         p = iflist;
834         while(p) {
835                 if (group) {
836                         /* if it belongs to the proper group, and the format matches
837                          * and it is not in use, we found a candidate! */
838                         if (p->group & group &&
839                             p->mc->formats & format &&
840                             !p->owner) {
841                                 /* XXX Not quite sure that not having an owner is
842                                  * sufficient evidence of beeing a free device XXX */
843                                 tmp = ast_modem_new(p, AST_STATE_DOWN);
844                                 restart_monitor();
845                                 break;
846                         }
847                 } else {
848                         if (!strcmp(dev, p->dev + 5)) {
849                                 if (p->mc->formats & format) {
850                                         if (!p->owner) {
851                                                 tmp = ast_modem_new(p, AST_STATE_DOWN);
852                                                 restart_monitor();
853                                                 break;
854                                         } else
855                                                 ast_log(LOG_WARNING, "Device '%s' is busy\n", p->dev);
856                                 } else 
857                                         ast_log(LOG_WARNING, "Asked for a format %s line on %s\n", ast_getformatname(format), p->dev);
858                                 break;
859                         }
860                 }
861                 p = p->next;
862         }
863         if (!p) 
864                 ast_log(LOG_WARNING, "Requested device '%s' does not exist\n", dev);
865         
866         ast_mutex_unlock(&iflock);
867         return tmp;
868 }
869
870 static ast_group_t get_group(char *s)
871 {
872         char *piece;
873         int start, finish,x;
874         ast_group_t group = 0;
875         char *copy = ast_strdupa(s);
876         char *stringp=NULL;
877         if (!copy) {
878                 ast_log(LOG_ERROR, "Out of memory\n");
879                 return 0;
880         }
881         stringp=copy;
882         piece = strsep(&stringp, ",");
883         while(piece) {
884                 if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
885                         /* Range */
886                 } else if (sscanf(piece, "%d", &start)) {
887                         /* Just one */
888                         finish = start;
889                 } else {
890                         ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'.  Using '0'\n", s,piece);
891                         return 0;
892                 }
893                 piece = strsep(&stringp, ",");
894
895                 for (x=start;x<=finish;x++) {
896                         if ((x > 63) || (x < 0)) {
897                                 ast_log(LOG_WARNING, "Ignoring invalid group %d\n", x);
898                                 break;
899                         }
900                         group |= (1 << x);
901                 }
902         }
903         return group;
904 }
905
906 static int __unload_module(void)
907 {
908         struct ast_modem_pvt *p, *pl;
909         /* First, take us out of the channel loop */
910         ast_channel_unregister(&modem_tech);
911         if (!ast_mutex_lock(&iflock)) {
912                 /* Hangup all interfaces if they have an owner */
913                 p = iflist;
914                 while(p) {
915                         if (p->owner)
916                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
917                         p = p->next;
918                 }
919                 iflist = NULL;
920                 ast_mutex_unlock(&iflock);
921         } else {
922                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
923                 return -1;
924         }
925         if (!ast_mutex_lock(&monlock)) {
926                 if (monitor_thread != AST_PTHREADT_NULL && monitor_thread != AST_PTHREADT_STOP) {
927                         pthread_cancel(monitor_thread);
928                         pthread_join(monitor_thread, NULL);
929                 }
930                 monitor_thread = AST_PTHREADT_STOP;
931                 ast_mutex_unlock(&monlock);
932         } else {
933                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
934                 return -1;
935         }
936
937         if (!ast_mutex_lock(&iflock)) {
938                 /* Destroy all the interfaces and free their memory */
939                 p = iflist;
940                 while(p) {
941                         /* Close the socket, assuming it's real */
942                         if (p->fd > -1)
943                                 close(p->fd);
944                         pl = p;
945                         p = p->next;
946                         /* Free associated memory */
947                         free(pl);
948                 }
949                 iflist = NULL;
950                 ast_mutex_unlock(&iflock);
951         } else {
952                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
953                 return -1;
954         }
955                 
956         return 0;
957 }
958
959 int unload_module()
960 {
961         return __unload_module();
962 }
963
964 int load_module()
965 {
966         struct ast_config *cfg;
967         struct ast_variable *v;
968         struct ast_modem_pvt *tmp;
969         char driver[80];
970         cfg = ast_config_load(config);
971
972         /* We *must* have a config file otherwise stop immediately */
973         if (!cfg) {
974                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
975                 return -1;
976         }
977         if (ast_mutex_lock(&iflock)) {
978                 /* It's a little silly to lock it, but we mind as well just to be sure */
979                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
980                 return -1;
981         }
982         v = ast_variable_browse(cfg, "interfaces");
983         while(v) {
984                 /* Create the interface list */
985                 if (!strcasecmp(v->name, "device")) {
986                                 tmp = mkif(v->value);
987                                 if (tmp) {
988                                         tmp->next = iflist;
989                                         iflist = tmp;
990                                         
991                                 } else {
992                                         ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
993                                         ast_config_destroy(cfg);
994                                         ast_mutex_unlock(&iflock);
995                                         __unload_module();
996                                         return -1;
997                                 }
998                 } else if (!strcasecmp(v->name, "driver")) {
999                         snprintf(driver, sizeof(driver), "chan_modem_%s.so", v->value);
1000                         if (option_verbose > 1) 
1001                                 ast_verbose(VERBOSE_PREFIX_2 "Loading modem driver %s", driver);
1002                                 
1003                         if (ast_load_resource(driver)) {
1004                                 ast_log(LOG_ERROR, "Failed to load driver %s\n", driver);
1005                                 ast_config_destroy(cfg);
1006                                 ast_mutex_unlock(&iflock);
1007                                 __unload_module();
1008                                 return -1;
1009                         }
1010                 } else if (!strcasecmp(v->name, "mode")) {
1011                         if (!strncasecmp(v->value, "ri", 2)) 
1012                                 gmode = MODEM_MODE_WAIT_RING;
1013                         else if (!strncasecmp(v->value, "im", 2))
1014                                 gmode = MODEM_MODE_IMMEDIATE;
1015                         else if (!strncasecmp(v->value, "an", 2))
1016                                 gmode = MODEM_MODE_WAIT_ANSWER;
1017                         else
1018                                 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1019                 } else if (!strcasecmp(v->name, "stripmsd")) {
1020                         stripmsd = atoi(v->value);
1021                 } else if (!strcasecmp(v->name, "type")) {
1022                         strncpy(mtype, v->value, sizeof(mtype)-1);
1023                 } else if (!strcasecmp(v->name, "initstr")) {
1024                         strncpy(initstr, v->value, sizeof(initstr)-1);
1025                 } else if (!strcasecmp(v->name, "dialtype")) {
1026                         dialtype = toupper(v->value[0]);
1027                 } else if (!strcasecmp(v->name, "context")) {
1028                         strncpy(context, v->value, sizeof(context)-1);
1029                 } else if (!strcasecmp(v->name, "msn")) {
1030                         strncpy(msn, v->value, sizeof(msn)-1);
1031                 } else if (!strcasecmp(v->name, "incomingmsn")) {
1032                         strncpy(incomingmsn, v->value, sizeof(incomingmsn)-1);
1033                 } else if (!strcasecmp(v->name, "dtmfmode")) {
1034                         char tmp[80];
1035                         char *alt;
1036                         strncpy(tmp, v->value, sizeof(tmp) - 1);
1037                         alt = strchr(tmp, '/');
1038                         if (!strcasecmp(tmp, "none"))
1039                                 dtmfmode=MODEM_DTMF_NONE;
1040                         else if (!strcasecmp(tmp, "asterisk"))
1041                                 dtmfmode = MODEM_DTMF_AST;
1042                         else if (!strcasecmp(tmp, "i4l"))
1043                                 dtmfmode = MODEM_DTMF_I4L;
1044                         else {
1045                                 ast_log(LOG_WARNING, "Unknown dtmf detection mode '%s', using 'asterisk'\n", v->value);
1046                                 dtmfmode = MODEM_DTMF_AST;
1047                         }
1048                         if (alt) {
1049                                 if (!strcasecmp(alt, "none"))
1050                                         dtmfmodegen=MODEM_DTMF_NONE;
1051                                 else if (!strcasecmp(alt, "asterisk"))
1052                                         dtmfmodegen = MODEM_DTMF_AST;
1053                                 else if (!strcasecmp(alt, "i4l"))
1054                                         dtmfmodegen = MODEM_DTMF_I4L;
1055                                 else if (!strcasecmp(alt, "both"))
1056                                         dtmfmodegen = MODEM_DTMF_I4L | MODEM_DTMF_AST;
1057                                 else {
1058                                         ast_log(LOG_WARNING, "Unknown dtmf generation mode '%s', using 'asterisk'\n", v->value);
1059                                         dtmfmodegen = MODEM_DTMF_AST;
1060                                 }
1061                         } else
1062                                 dtmfmodegen = dtmfmode;
1063                 } else if (!strcasecmp(v->name, "outgoingmsn")) {
1064                         strncpy(outgoingmsn, v->value, sizeof(outgoingmsn)-1);
1065                 } else if (!strcasecmp(v->name, "language")) {
1066                         strncpy(language, v->value, sizeof(language)-1);
1067                 } else if (!strcasecmp(v->name, "group")) {
1068                         cur_group = get_group(v->value);
1069                 }
1070                 v = v->next;
1071         }
1072         ast_mutex_unlock(&iflock);
1073         if (ast_channel_register(&modem_tech)) {
1074                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1075                 ast_config_destroy(cfg);
1076                 __unload_module();
1077                 return -1;
1078         }
1079         ast_config_destroy(cfg);
1080         /* And start the monitor for the first time */
1081         restart_monitor();
1082         return 0;
1083 }
1084
1085 int usecount(void)
1086 {
1087         return usecnt;
1088 }
1089
1090 char *description()
1091 {
1092         return (char *) desc;
1093 }
1094
1095 char *key()
1096 {
1097         return ASTERISK_GPL_KEY;
1098 }
1099