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