Make groups be 64-bits (bug #3351, with mods)
[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-2004, 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         char *stringp=NULL;
795         strncpy(dev, (char *)data, sizeof(dev)-1);
796         stringp=dev;
797         strsep(&stringp, ":");
798         oldformat = format;
799
800         if (dev[0]=='g' && isdigit(dev[1])) {
801                 /* Retrieve the group number */
802                 if (sscanf(dev+1, "%u", &group) < 1) {
803                         ast_log(LOG_WARNING, "Unable to determine group from [%s]\n", (char *)data);
804                         return NULL;
805                 }
806                 group = 1 << group;
807         }
808
809         /* Search for an unowned channel */
810         if (ast_mutex_lock(&iflock)) {
811                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
812                 return NULL;
813         }
814         p = iflist;
815         while(p) {
816                 if (group) {
817                         /* if it belongs to the proper group, and the format matches
818                          * and it is not in use, we found a candidate! */
819                         if (p->group & group &&
820                             p->mc->formats & format &&
821                             !p->owner) {
822                                 /* XXX Not quite sure that not having an owner is
823                                  * sufficient evidence of beeing a free device XXX */
824                                 tmp = ast_modem_new(p, AST_STATE_DOWN);
825                                 restart_monitor();
826                                 break;
827                         }
828                 } else {
829                         if (!strcmp(dev, p->dev + 5)) {
830                                 if (p->mc->formats & format) {
831                                         if (!p->owner) {
832                                                 tmp = ast_modem_new(p, AST_STATE_DOWN);
833                                                 restart_monitor();
834                                                 break;
835                                         } else
836                                                 ast_log(LOG_WARNING, "Device '%s' is busy\n", p->dev);
837                                 } else 
838                                         ast_log(LOG_WARNING, "Asked for a format %s line on %s\n", ast_getformatname(format), p->dev);
839                                 break;
840                         }
841                 }
842                 p = p->next;
843         }
844         if (!p) 
845                 ast_log(LOG_WARNING, "Requested device '%s' does not exist\n", dev);
846         
847         ast_mutex_unlock(&iflock);
848         return tmp;
849 }
850
851 static ast_group_t get_group(char *s)
852 {
853         char *piece;
854         int start, finish,x;
855         ast_group_t group = 0;
856         char *copy = ast_strdupa(s);
857         char *stringp=NULL;
858         if (!copy) {
859                 ast_log(LOG_ERROR, "Out of memory\n");
860                 return 0;
861         }
862         stringp=copy;
863         piece = strsep(&stringp, ",");
864         while(piece) {
865                 if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
866                         /* Range */
867                 } else if (sscanf(piece, "%d", &start)) {
868                         /* Just one */
869                         finish = start;
870                 } else {
871                         ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'.  Using '0'\n", s,piece);
872                         return 0;
873                 }
874                 piece = strsep(&stringp, ",");
875
876                 for (x=start;x<=finish;x++) {
877                         if ((x > 63) || (x < 0)) {
878                                 ast_log(LOG_WARNING, "Ignoring invalid group %d\n", x);
879                                 break;
880                         }
881                         group |= (1 << x);
882                 }
883         }
884         return group;
885 }
886
887 static int __unload_module(void)
888 {
889         struct ast_modem_pvt *p, *pl;
890         /* First, take us out of the channel loop */
891         ast_channel_unregister(type);
892         if (!ast_mutex_lock(&iflock)) {
893                 /* Hangup all interfaces if they have an owner */
894                 p = iflist;
895                 while(p) {
896                         if (p->owner)
897                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
898                         p = p->next;
899                 }
900                 iflist = NULL;
901                 ast_mutex_unlock(&iflock);
902         } else {
903                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
904                 return -1;
905         }
906         if (!ast_mutex_lock(&monlock)) {
907                 if (monitor_thread != AST_PTHREADT_NULL && monitor_thread != AST_PTHREADT_STOP) {
908                         pthread_cancel(monitor_thread);
909                         pthread_join(monitor_thread, NULL);
910                 }
911                 monitor_thread = AST_PTHREADT_STOP;
912                 ast_mutex_unlock(&monlock);
913         } else {
914                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
915                 return -1;
916         }
917
918         if (!ast_mutex_lock(&iflock)) {
919                 /* Destroy all the interfaces and free their memory */
920                 p = iflist;
921                 while(p) {
922                         /* Close the socket, assuming it's real */
923                         if (p->fd > -1)
924                                 close(p->fd);
925                         pl = p;
926                         p = p->next;
927                         /* Free associated memory */
928                         free(pl);
929                 }
930                 iflist = NULL;
931                 ast_mutex_unlock(&iflock);
932         } else {
933                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
934                 return -1;
935         }
936                 
937         return 0;
938 }
939
940 int unload_module()
941 {
942         return __unload_module();
943 }
944
945 int load_module()
946 {
947         struct ast_config *cfg;
948         struct ast_variable *v;
949         struct ast_modem_pvt *tmp;
950         char driver[80];
951         cfg = ast_load(config);
952
953         /* We *must* have a config file otherwise stop immediately */
954         if (!cfg) {
955                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
956                 return -1;
957         }
958         if (ast_mutex_lock(&iflock)) {
959                 /* It's a little silly to lock it, but we mind as well just to be sure */
960                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
961                 return -1;
962         }
963         v = ast_variable_browse(cfg, "interfaces");
964         while(v) {
965                 /* Create the interface list */
966                 if (!strcasecmp(v->name, "device")) {
967                                 tmp = mkif(v->value);
968                                 if (tmp) {
969                                         tmp->next = iflist;
970                                         iflist = tmp;
971                                         
972                                 } else {
973                                         ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
974                                         ast_destroy(cfg);
975                                         ast_mutex_unlock(&iflock);
976                                         __unload_module();
977                                         return -1;
978                                 }
979                 } else if (!strcasecmp(v->name, "driver")) {
980                         snprintf(driver, sizeof(driver), "chan_modem_%s.so", v->value);
981                         if (option_verbose > 1) 
982                                 ast_verbose(VERBOSE_PREFIX_2 "Loading modem driver %s", driver);
983                                 
984                         if (ast_load_resource(driver)) {
985                                 ast_log(LOG_ERROR, "Failed to load driver %s\n", driver);
986                                 ast_destroy(cfg);
987                                 ast_mutex_unlock(&iflock);
988                                 __unload_module();
989                                 return -1;
990                         }
991                 } else if (!strcasecmp(v->name, "mode")) {
992                         if (!strncasecmp(v->value, "ri", 2)) 
993                                 gmode = MODEM_MODE_WAIT_RING;
994                         else if (!strncasecmp(v->value, "im", 2))
995                                 gmode = MODEM_MODE_IMMEDIATE;
996                         else if (!strncasecmp(v->value, "an", 2))
997                                 gmode = MODEM_MODE_WAIT_ANSWER;
998                         else
999                                 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1000                 } else if (!strcasecmp(v->name, "stripmsd")) {
1001                         stripmsd = atoi(v->value);
1002                 } else if (!strcasecmp(v->name, "type")) {
1003                         strncpy(mtype, v->value, sizeof(mtype)-1);
1004                 } else if (!strcasecmp(v->name, "initstr")) {
1005                         strncpy(initstr, v->value, sizeof(initstr)-1);
1006                 } else if (!strcasecmp(v->name, "dialtype")) {
1007                         dialtype = toupper(v->value[0]);
1008                 } else if (!strcasecmp(v->name, "context")) {
1009                         strncpy(context, v->value, sizeof(context)-1);
1010                 } else if (!strcasecmp(v->name, "msn")) {
1011                         strncpy(msn, v->value, sizeof(msn)-1);
1012                 } else if (!strcasecmp(v->name, "incomingmsn")) {
1013                         strncpy(incomingmsn, v->value, sizeof(incomingmsn)-1);
1014                 } else if (!strcasecmp(v->name, "dtmfmode")) {
1015                         char tmp[80];
1016                         char *alt;
1017                         strncpy(tmp, v->value, sizeof(tmp) - 1);
1018                         alt = strchr(tmp, '/');
1019                         if (!strcasecmp(tmp, "none"))
1020                                 dtmfmode=MODEM_DTMF_NONE;
1021                         else if (!strcasecmp(tmp, "asterisk"))
1022                                 dtmfmode = MODEM_DTMF_AST;
1023                         else if (!strcasecmp(tmp, "i4l"))
1024                                 dtmfmode = MODEM_DTMF_I4L;
1025                         else {
1026                                 ast_log(LOG_WARNING, "Unknown dtmf detection mode '%s', using 'asterisk'\n", v->value);
1027                                 dtmfmode = MODEM_DTMF_AST;
1028                         }
1029                         if (alt) {
1030                                 if (!strcasecmp(alt, "none"))
1031                                         dtmfmodegen=MODEM_DTMF_NONE;
1032                                 else if (!strcasecmp(alt, "asterisk"))
1033                                         dtmfmodegen = MODEM_DTMF_AST;
1034                                 else if (!strcasecmp(alt, "i4l"))
1035                                         dtmfmodegen = MODEM_DTMF_I4L;
1036                                 else if (!strcasecmp(alt, "both"))
1037                                         dtmfmodegen = MODEM_DTMF_I4L | MODEM_DTMF_AST;
1038                                 else {
1039                                         ast_log(LOG_WARNING, "Unknown dtmf generation mode '%s', using 'asterisk'\n", v->value);
1040                                         dtmfmodegen = MODEM_DTMF_AST;
1041                                 }
1042                         } else
1043                                 dtmfmodegen = dtmfmode;
1044                 } else if (!strcasecmp(v->name, "outgoingmsn")) {
1045                         strncpy(outgoingmsn, v->value, sizeof(outgoingmsn)-1);
1046                 } else if (!strcasecmp(v->name, "language")) {
1047                         strncpy(language, v->value, sizeof(language)-1);
1048                 } else if (!strcasecmp(v->name, "group")) {
1049                         cur_group = get_group(v->value);
1050                 }
1051                 v = v->next;
1052         }
1053         ast_mutex_unlock(&iflock);
1054         if (ast_channel_register(type, tdesc, /* XXX Don't know our types -- maybe we should register more than one XXX */ 
1055                                                 AST_FORMAT_SLINEAR, modem_request)) {
1056                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1057                 ast_destroy(cfg);
1058                 __unload_module();
1059                 return -1;
1060         }
1061         ast_destroy(cfg);
1062         /* And start the monitor for the first time */
1063         restart_monitor();
1064         return 0;
1065 }
1066
1067 int usecount(void)
1068 {
1069         int res;
1070         ast_mutex_lock(&usecnt_lock);
1071         res = usecnt;
1072         ast_mutex_unlock(&usecnt_lock);
1073         return res;
1074 }
1075
1076 char *description()
1077 {
1078         return desc;
1079 }
1080
1081 char *key()
1082 {
1083         return ASTERISK_GPL_KEY;
1084 }
1085