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