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