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