Version 0.2.0 from FTP
[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, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
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 <pthread.h>
16 #include <string.h>
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <asterisk/vmodem.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 <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 char *desc = "Generic Voice Modem Driver";
42 static char *tdesc = "Generic Voice Modem Channel Driver";
43 static char *type = "Modem";
44 static 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 static int usecnt =0;
63
64 static int baudrate = 115200;
65
66 static int stripmsd = 0;
67
68 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
69
70 /* Protect the interface list (of ast_modem_pvt's) */
71 static pthread_mutex_t iflock = AST_MUTEX_INITIALIZER;
72
73 /* Protect the monitoring thread, so only one process can kill or start it, and not
74    when it's doing something critical. */
75 static pthread_mutex_t monlock = AST_MUTEX_INITIALIZER;
76
77 /* This is the thread for the monitor which checks for input on the channels
78    which are not currently in use.  */
79 static pthread_t monitor_thread = -1;
80
81 static int restart_monitor(void);
82
83 /* The private structures of the Phone Jack channels are linked for
84    selecting outgoing channels */
85    
86 static struct ast_modem_pvt  *iflist = NULL;
87
88 static int modem_digit(struct ast_channel *ast, char digit)
89 {
90         struct ast_modem_pvt *p;
91         p = ast->pvt->pvt;
92         if (p->mc->dialdigit)
93                 return p->mc->dialdigit(p, digit);
94         else ast_log(LOG_DEBUG, "Channel %s lacks digit dialing\n", ast->name);
95         return 0;
96 }
97
98 static struct ast_modem_driver *drivers = NULL;
99
100 static struct ast_frame *modem_read(struct ast_channel *);
101
102 static struct ast_modem_driver *find_capability(char *ident)
103 {
104         struct ast_modem_driver *mc;
105         int x;
106         mc = drivers;
107         while(mc) {
108                 for (x=0;mc->idents[x];x++) {
109                         if (!strcmp(ident, mc->idents[x])) 
110                                 break;
111                 }
112                 if (mc->idents[x])
113                         break;
114                 mc = mc->next;
115         }
116         if (mc) {
117                 if (mc->incusecnt)
118                         mc->incusecnt();
119         }
120         return mc;
121 }
122
123 static struct ast_modem_driver *find_driver(char *drv)
124 {
125         struct ast_modem_driver *mc;
126         mc = drivers;
127         while(mc) {
128                 if (!strcasecmp(mc->name, drv)) 
129                         break;
130                 mc = mc->next;
131         }
132         if (mc) {
133                 if (mc->incusecnt)
134                         mc->incusecnt();
135         }
136         return mc;
137 }
138
139 int ast_register_modem_driver(struct ast_modem_driver *mc)
140 {
141         mc->next = drivers;
142         drivers = mc;
143         return 0;
144 }
145
146 int ast_unregister_modem_driver(struct ast_modem_driver *mc)
147 {
148         struct ast_modem_driver *last = NULL, *cur;
149         cur = drivers;
150         while(cur) {
151                 if (cur == mc) {
152                         if (last)
153                                 last->next = mc->next;
154                         else
155                                 drivers = mc->next;
156                         return 0;
157                 }
158                 cur = cur->next;
159         }
160         return -1;
161 }
162
163 static int modem_call(struct ast_channel *ast, char *idest, int timeout)
164 {
165         static int modem_hangup(struct ast_channel *ast);
166         struct ast_modem_pvt *p;
167         int ms = timeout;
168         char rdest[80], *where, dstr[100];
169         strncpy(rdest, idest, sizeof(rdest)-1);
170         strtok(rdest, ":");
171         where = strtok(NULL, ":");
172         if (!where) {
173                 ast_log(LOG_WARNING, "Destination %s requres a real destination (device:destination)\n", idest);
174                 return -1;
175         }
176         p = ast->pvt->pvt;
177         strcpy(dstr,where + p->stripmsd);
178         /* if not a transfer or just sending tones, must be in correct state */
179         if (strcasecmp(rdest, "transfer") && strcasecmp(rdest,"sendtones")) {
180                 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
181                         ast_log(LOG_WARNING, "modem_call called on %s, neither down nor reserved\n", ast->name);
182                         return -1;
183                 }
184         } 
185         if (!strcasecmp(rdest,"transfer")) /* if a transfer, put in transfer stuff */
186         {
187                 sprintf(dstr,"!,%s",where + p->stripmsd);
188         }
189         if (!strcasecmp(where, "handset")) {
190                 if (p->mc->setdev)
191                         if (p->mc->setdev(p, MODEM_DEV_HANDSET))
192                                 return -1;
193                 /* Should be immediately up */
194                 ast_setstate(ast, AST_STATE_UP);
195         } else {
196                 if (p->mc->setdev)
197                         if (p->mc->setdev(p, MODEM_DEV_TELCO_SPK))
198                                 return -1;
199                 if (p->mc->dial)
200                         p->mc->dial(p, dstr);
201                 ast_setstate(ast, AST_STATE_DIALING);
202                 while((ast->_state != AST_STATE_UP) && (ms > 0)) {
203                         ms = ast_waitfor(ast, ms);
204                         /* Just read packets and watch what happens */
205                         if (ms > 0) {
206                                 if (!modem_read(ast))
207                                         return -1;
208                         }
209                 }
210                 if (ms < 0)     
211                         return -1;
212         }
213         return 0;
214 }
215
216 int ast_modem_send(struct ast_modem_pvt *p, char *cmd, int len)
217 {
218         int i;
219         usleep(5000);
220         if (!len) {
221                 for(i = 0; cmd[i];)
222                    {
223                         if (fwrite(cmd + i,1,1,p->f) != 1)
224                            {
225                                 if (errno == EWOULDBLOCK) continue;
226                                 return -1;
227                            }
228                         i++;
229                    }
230                 tcdrain(fileno(p->f)); 
231                 fprintf(p->f,"\r\n");
232                 return 0;
233         } else {
234                 if (fwrite(cmd, 1, len, p->f) < len)
235                         return -1;
236                 return 0;
237         }
238 }
239
240 int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
241 {
242         int res = -1,c,i;
243         timeout *= 1000;
244         p->response[0] = 0;
245         c = i = 0;
246         do {
247                 res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
248                 if (res < 0) {
249                         strncpy(p->response, "(No Response)", sizeof(p->response)-1);
250                         return -1;
251                 }
252                   /* get no more then buffer length */
253                 while(i < sizeof(p->response) - 1)
254                 {
255                         c = fgetc(p->f);  /* get a char */
256                         if (c < 1) /* if error */
257                         {
258                                   /* if nothing in buffer, go back into timeout stuff */
259                                 if (errno == EWOULDBLOCK) break;
260                                 /* return as error */
261                                 strncpy(p->response, "(No Response)", sizeof(p->response)-1);
262                                 return -1;
263                         }
264                           /* save char */
265                         p->response[i++] = c;
266                         p->response[i] = 0;                     
267                           /* if end of input */
268                         if (c == '\n') break;
269                 }
270                 if (c >= 0)  /* if input terminated normally */
271                 {
272                           /* ignore just CR/LF */
273                         if (!strcmp(p->response,"\r\n"))
274                         {
275                                   /* reset input buffer stuff */
276                                 i = 0; 
277                                 p->response[0] = 0;
278                         }
279                         else /* otherwise return with info in buffer */
280                         {
281                                 return 0;
282                         }
283                 }
284         } while(timeout > 0);
285         strncpy(p->response, "(No Response)", sizeof(p->response)-1);
286         return -1;
287 }
288
289 int ast_modem_expect(struct ast_modem_pvt *p, char *result, int timeout)
290 {
291         int res = -1;
292         timeout *= 1000;
293         strncpy(p->response, "(No Response)", sizeof(p->response)-1);
294         do {
295                 res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
296                 if (res < 0) {
297                         return -1;
298                 }
299                 /* Read a response */
300                 fgets(p->response, sizeof(p->response), p->f);
301 #if     0
302                 fprintf(stderr, "Modem said: %s", p->response);
303 #endif
304                 if (!strncasecmp(p->response, result, strlen(result))) 
305                         return 0;
306         } while(timeout > 0);
307         return -1;
308 }
309
310 void ast_modem_trim(char *s)
311 {
312         int x;
313         x = strlen(s) - 1;
314         while(x >= 0) {
315                 if ((s[x] != '\r') && (s[x] != '\n') && (s[x] != ' '))
316                         break;
317                 s[x] = '\0';
318                 x--;
319         }
320 }
321
322 static int modem_setup(struct ast_modem_pvt *p, int baudrate)
323 {
324
325         /* Make sure there's a modem there and that it's in a reasonable 
326            mode.  Set the baud rate, etc.  */
327         char identity[256];
328         char *ident = NULL;
329         char etx[2] = { 0x10, '!' }; 
330         if (option_debug)
331                 ast_log(LOG_DEBUG, "Setting up modem %s\n", p->dev);
332         if (ast_modem_send(p, etx, 2)) {
333                 ast_log(LOG_WARNING, "Failed to send ETX?\n");
334                 return -1;
335         }
336         if (ast_modem_send(p, "\r\n", 2)) {
337                 ast_log(LOG_WARNING, "Failed to send enter?\n");
338                 return -1;
339         }
340         usleep(10000);
341         /* Read any outstanding stuff */
342         while(!ast_modem_read_response(p, 0));
343         if (ast_modem_send(p, "ATZ", 0)) {
344                 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
345                 return -1;
346         }
347         if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
348                 ast_log(LOG_WARNING, "Modem reset failed: %s\n", p->response);
349                 return -1;
350         }
351         if (ast_modem_send(p, p->initstr, 0)) {
352                 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
353                 return -1;
354         }
355         if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
356                 ast_log(LOG_WARNING, "Modem initialization failed: %s\n", p->response);
357                 return -1;
358         }
359         if (ast_modem_send(p, "ATI3", 0)) {
360                 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
361                 return -1;
362         }
363         if (ast_modem_read_response(p, ECHO_TIMEOUT)) {
364                 ast_log(LOG_WARNING, "Modem did not provide identification\n");
365                 return -1;
366         }
367         strncpy(identity, p->response, sizeof(identity)-1);
368         ast_modem_trim(identity);
369         if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
370                 ast_log(LOG_WARNING, "Modem did not provide identification\n");
371                 return -1;
372         }
373         if (!strcasecmp(mtype, "autodetect")) {
374                 p->mc = find_capability(identity);
375                 if (!p->mc) {
376                         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); 
377                         return -1;
378                 }
379         } else {
380                 p->mc = find_driver(mtype);
381                 if (!p->mc) {
382                         ast_log(LOG_WARNING, "No driver for modem type '%s'\n", mtype);
383                         return -1;
384                 }
385         }
386         if (p->mc->init) {
387                 if (p->mc->init(p)) {
388                         ast_log(LOG_WARNING, "Modem Initialization Failed on '%s', driver %s.\n", p->dev, p->mc->name);
389                         p->mc->decusecnt();
390                         return -1;
391                 }
392         }                       
393         if (option_verbose > 2) {
394                 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");
395         }
396         if (ident)
397                 free(ident);
398         return 0;
399 }
400
401 static int modem_hangup(struct ast_channel *ast)
402 {
403         struct ast_modem_pvt *p;
404         if (option_debug)
405                 ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
406         p = ast->pvt->pvt;
407         /* Hang up */
408         if (p->mc->hangup)
409                 p->mc->hangup(p);
410         /* Re-initialize */
411         if (p->mc->init)
412                 p->mc->init(p);
413         ast_setstate(ast, AST_STATE_DOWN);
414         memset(p->cid, 0, sizeof(p->cid));
415         memset(p->dnid, 0, sizeof(p->dnid));
416         ((struct ast_modem_pvt *)(ast->pvt->pvt))->owner = NULL;
417         ast_pthread_mutex_lock(&usecnt_lock);
418         usecnt--;
419         if (usecnt < 0) 
420                 ast_log(LOG_WARNING, "Usecnt < 0???\n");
421         ast_pthread_mutex_unlock(&usecnt_lock);
422         ast_update_use_count();
423         if (option_verbose > 2) 
424                 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
425         ast->pvt->pvt = NULL;
426         ast_setstate(ast, AST_STATE_DOWN);
427         restart_monitor();
428         return 0;
429 }
430
431 static int modem_answer(struct ast_channel *ast)
432 {
433         struct ast_modem_pvt *p;
434         int res=0;
435         if (option_debug)
436                 ast_log(LOG_DEBUG, "modem_answer(%s)\n", ast->name);
437         p = ast->pvt->pvt;
438         if (p->mc->answer) {
439                 res = p->mc->answer(p);
440         }
441         if (!res) {
442                 ast->rings = 0;
443                 ast_setstate(ast, AST_STATE_UP);
444         }
445         return res;
446 }
447
448 #if     0
449 static char modem_2digit(char c)
450 {
451         if (c == 12)
452                 return '#';
453         else if (c == 11)
454                 return '*';
455         else if ((c < 10) && (c >= 0))
456                 return '0' + c - 1;
457         else
458                 return '?';
459 }
460 #endif
461 static struct ast_frame *modem_read(struct ast_channel *ast)
462 {
463         struct ast_modem_pvt *p = ast->pvt->pvt;
464         struct ast_frame *fr=NULL;
465         if (p->mc->read)
466                 fr = p->mc->read(p);
467         return fr;
468 }
469
470 static int modem_write(struct ast_channel *ast, struct ast_frame *frame)
471 {
472         int res=0;
473         long flags;
474         struct ast_modem_pvt *p = ast->pvt->pvt;
475
476         /* Temporarily make non-blocking */
477         flags = fcntl(ast->fds[0], F_GETFL);
478         fcntl(ast->fds[0], F_SETFL, flags | O_NONBLOCK);
479
480         if (p->mc->write)
481                 res = p->mc->write(p, frame);
482
483         /* Block again */
484         fcntl(ast->fds[0], F_SETFL, flags);
485         return 0;
486 }
487
488 struct ast_channel *ast_modem_new(struct ast_modem_pvt *i, int state)
489 {
490         struct ast_channel *tmp;
491         tmp = ast_channel_alloc(1);
492         if (tmp) {
493                 snprintf(tmp->name, sizeof(tmp->name), "Modem[%s]/%s", i->mc->name, i->dev + 5);
494                 tmp->type = type;
495                 tmp->fds[0] = i->fd;
496                 tmp->nativeformats = i->mc->formats;
497                 ast_setstate(tmp, state);
498                 if (state == AST_STATE_RING)
499                         tmp->rings = 1;
500                 tmp->pvt->pvt = i;
501                 tmp->pvt->send_digit = modem_digit;
502                 tmp->pvt->call = modem_call;
503                 tmp->pvt->hangup = modem_hangup;
504                 tmp->pvt->answer = modem_answer;
505                 tmp->pvt->read = modem_read;
506                 tmp->pvt->write = modem_write;
507                 strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
508                 if (strlen(i->cid))
509                         tmp->callerid = strdup(i->cid);
510                 if (strlen(i->language))
511                         strncpy(tmp->language,i->language, sizeof(tmp->language)-1);
512                 if (strlen(i->dnid))
513                         strncpy(tmp->exten, i->dnid, sizeof(tmp->exten) - 1);
514                 i->owner = tmp;
515                 ast_pthread_mutex_lock(&usecnt_lock);
516                 usecnt++;
517                 ast_pthread_mutex_unlock(&usecnt_lock);
518                 ast_update_use_count();
519                 if (state != AST_STATE_DOWN) {
520                         if (ast_pbx_start(tmp)) {
521                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
522                                 ast_hangup(tmp);
523                                 tmp = NULL;
524                         }
525                 }
526         } else
527                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
528         return tmp;
529 }
530
531 static void modem_mini_packet(struct ast_modem_pvt *i)
532 {
533         struct ast_frame *fr;
534         fr = i->mc->read(i);
535         if (!fr) return;
536         if (fr->frametype == AST_FRAME_CONTROL) {
537                 if (fr->subclass == AST_CONTROL_RING) {
538                         ast_modem_new(i, AST_STATE_RING);
539                 }
540         }
541 }
542
543 static void *do_monitor(void *data)
544 {
545         fd_set rfds, efds;
546         int n, res;
547         struct ast_modem_pvt *i;
548         /* This thread monitors all the frame relay interfaces which are not yet in use
549            (and thus do not have a separate thread) indefinitely */
550         /* From here on out, we die whenever asked */
551 #if 0
552         if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
553                 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
554                 return NULL;
555         }
556 #endif
557         for(;;) {
558                 /* Don't let anybody kill us right away.  Nobody should lock the interface list
559                    and wait for the monitor list, but the other way around is okay. */
560                 if (ast_pthread_mutex_lock(&monlock)) {
561                         ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
562                         return NULL;
563                 }
564                 /* Lock the interface list */
565                 if (ast_pthread_mutex_lock(&iflock)) {
566                         ast_log(LOG_ERROR, "Unable to grab interface lock\n");
567                         ast_pthread_mutex_unlock(&monlock);
568                         return NULL;
569                 }
570                 /* Build the stuff we're going to select on, that is the socket of every
571                    ast_modem_pvt that does not have an associated owner channel */
572                 n = -1;
573                 FD_ZERO(&rfds);
574                 FD_ZERO(&efds);
575                 i = iflist;
576                 while(i) {
577                         if (FD_ISSET(i->fd, &rfds)) 
578                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
579                         if (!i->owner) {
580                                 /* This needs to be watched, as it lacks an owner */
581                                 FD_SET(i->fd, &rfds);
582                                 FD_SET(i->fd, &efds);
583                                 if (i->fd > n)
584                                         n = i->fd;
585                         }
586                         
587                         i = i->next;
588                 }
589                 /* Okay, now that we know what to do, release the interface lock */
590                 ast_pthread_mutex_unlock(&iflock);
591                 
592                 /* And from now on, we're okay to be killed, so release the monitor lock as well */
593                 ast_pthread_mutex_unlock(&monlock);
594 #if 0
595                 ast_log(LOG_DEBUG, "In monitor, n=%d, pid=%d\n", n, getpid());
596 #endif
597                 /* Wait indefinitely for something to happen */
598                 pthread_testcancel();
599                 res = select(n + 1, &rfds, NULL, &efds, NULL);
600                 pthread_testcancel();
601                 /* Okay, select has finished.  Let's see what happened.  */
602                 if (res < 1) {
603                         ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
604                         continue;
605                 }
606                 /* Alright, lock the interface list again, and let's look and see what has
607                    happened */
608                 if (ast_pthread_mutex_lock(&iflock)) {
609                         ast_log(LOG_WARNING, "Unable to lock the interface list\n");
610                         continue;
611                 }
612                 i = iflist;
613                 while(i) {
614                         if (FD_ISSET(i->fd, &rfds) || FD_ISSET(i->fd, &efds)) {
615                                 if (i->owner) {
616                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
617                                         i = i->next;
618                                         continue;
619                                 }
620                                 modem_mini_packet(i);
621                         }
622                         i=i->next;
623                 }
624                 ast_pthread_mutex_unlock(&iflock);
625         }
626         /* Never reached */
627         return NULL;
628         
629 }
630
631 static int restart_monitor()
632 {
633         /* If we're supposed to be stopped -- stay stopped */
634         if (monitor_thread == -2)
635                 return 0;
636         if (ast_pthread_mutex_lock(&monlock)) {
637                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
638                 return -1;
639         }
640         if (monitor_thread == pthread_self()) {
641                 ast_pthread_mutex_unlock(&monlock);
642                 ast_log(LOG_WARNING, "Cannot kill myself\n");
643                 return -1;
644         }
645         if (monitor_thread != -1) {
646                 pthread_cancel(monitor_thread);
647                 /* Nudge it a little, as it's probably stuck in select */
648                 pthread_kill(monitor_thread, SIGURG);
649                 pthread_join(monitor_thread, NULL);
650         }
651         /* Start a new monitor */
652         if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
653                 ast_pthread_mutex_unlock(&monlock);
654                 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
655                 return -1;
656         }
657         ast_pthread_mutex_unlock(&monlock);
658         return 0;
659 }
660
661 static void stty(struct ast_modem_pvt *p)
662 {
663         struct termios mode;
664         memset(&mode, 0, sizeof(mode));
665         if (tcgetattr(p->fd, &mode)) {
666                 ast_log(LOG_WARNING, "Unable to get serial parameters on %s: %s\n", p->dev, strerror(errno));
667                 return;
668         }
669         cfmakeraw(&mode);
670         cfsetspeed(&mode, B115200);
671         if (tcsetattr(p->fd, TCSANOW, &mode)) 
672                 ast_log(LOG_WARNING, "Unable to set serial parameters on %s: %s\n", p->dev, strerror(errno));
673         
674 }
675
676 static struct ast_modem_pvt *mkif(char *iface)
677 {
678         /* Make a ast_modem_pvt structure for this interface */
679         struct ast_modem_pvt *tmp;
680 #if 0
681         int flags;      
682 #endif
683         
684         tmp = malloc(sizeof(struct ast_modem_pvt));
685         memset(tmp, 0, sizeof(struct ast_modem_pvt));
686         if (tmp) {
687                 tmp->fd = open(iface, O_RDWR | O_NONBLOCK);
688                 if (tmp->fd < 0) {
689                         ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
690                         free(tmp);
691                         return NULL;
692                 }
693                 strncpy(tmp->language, language, sizeof(tmp->language)-1);
694                 strncpy(tmp->msn, msn, sizeof(tmp->msn)-1);
695                 strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
696                 /* Maybe in the future we want to allow variable
697                    serial settings */
698                 stty(tmp);
699                 tmp->f = fdopen(tmp->fd, "w+");
700                 /* Disable buffering */
701                 setvbuf(tmp->f, NULL, _IONBF,0);
702                 if (tmp->f < 0) {
703                         ast_log(LOG_WARNING, "Unable to fdopen '%s'\n", iface);
704                         free(tmp);
705                         return NULL;
706                 }
707                 tmp->owner = NULL;
708                 tmp->ministate = 0;
709                 tmp->stripmsd = stripmsd;
710                 tmp->dialtype = dialtype;
711                 tmp->mode = gmode;
712                 memset(tmp->cid, 0, sizeof(tmp->cid));
713                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
714                 strncpy(tmp->initstr, initstr, sizeof(tmp->initstr)-1);
715                 tmp->next = NULL;
716                 tmp->obuflen = 0;
717                 
718                 if (modem_setup(tmp, baudrate) < 0) {
719                         ast_log(LOG_WARNING, "Unable to configure modem '%s'\n", iface);
720                         free(tmp);
721                         return NULL;
722                 }
723         }
724         return tmp;
725 }
726
727 static struct ast_channel *modem_request(char *type, int format, void *data)
728 {
729         int oldformat;
730         struct ast_modem_pvt *p;
731         struct ast_channel *tmp = NULL;
732         char dev[80];
733         strncpy(dev, (char *)data, sizeof(dev)-1);
734         strtok(dev, ":");
735         oldformat = format;
736         /* Search for an unowned channel */
737         if (ast_pthread_mutex_lock(&iflock)) {
738                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
739                 return NULL;
740         }
741         p = iflist;
742         while(p) {
743                 if (!strcmp(dev, p->dev + 5)) {
744                         if (p->mc->formats & format) {
745                                 if (!p->owner) {
746                                         tmp = ast_modem_new(p, AST_STATE_DOWN);
747                                         restart_monitor();
748                                         break;
749                                 } else
750                                         ast_log(LOG_WARNING, "Device '%s' is busy\n", p->dev);
751                         } else 
752                                 ast_log(LOG_WARNING, "Asked for a format %d line on %s\n", format, p->dev);
753                         break;
754                 }
755                 p = p->next;
756         }
757         if (!p) 
758                 ast_log(LOG_WARNING, "Requested device '%s' does not exist\n", dev);
759         
760         ast_pthread_mutex_unlock(&iflock);
761         return tmp;
762 }
763
764 int load_module()
765 {
766         struct ast_config *cfg;
767         struct ast_variable *v;
768         struct ast_modem_pvt *tmp;
769         char driver[80];
770         cfg = ast_load(config);
771
772         /* We *must* have a config file otherwise stop immediately */
773         if (!cfg) {
774                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
775                 return -1;
776         }
777         if (ast_pthread_mutex_lock(&iflock)) {
778                 /* It's a little silly to lock it, but we mind as well just to be sure */
779                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
780                 return -1;
781         }
782         v = ast_variable_browse(cfg, "interfaces");
783         while(v) {
784                 /* Create the interface list */
785                 if (!strcasecmp(v->name, "device")) {
786                                 tmp = mkif(v->value);
787                                 if (tmp) {
788                                         tmp->next = iflist;
789                                         iflist = tmp;
790                                         
791                                 } else {
792                                         ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
793                                         ast_destroy(cfg);
794                                         ast_pthread_mutex_unlock(&iflock);
795                                         unload_module();
796                                         ast_pthread_mutex_unlock(&iflock);
797                                         return -1;
798                                 }
799                 } else if (!strcasecmp(v->name, "driver")) {
800                         snprintf(driver, sizeof(driver), "chan_modem_%s.so", v->value);
801                         if (option_verbose > 1) 
802                                 ast_verbose(VERBOSE_PREFIX_2 "Loading modem driver %s", driver);
803                                 
804                         if (ast_load_resource(driver)) {
805                                 ast_log(LOG_ERROR, "Failed to load driver %s\n", driver);
806                                 ast_destroy(cfg);
807                                 ast_pthread_mutex_unlock(&iflock);
808                                 unload_module();
809                                 return -1;
810                         }
811                 } else if (!strcasecmp(v->name, "mode")) {
812                         if (!strncasecmp(v->value, "ri", 2)) 
813                                 gmode = MODEM_MODE_WAIT_RING;
814                         else if (!strncasecmp(v->value, "im", 2))
815                                 gmode = MODEM_MODE_IMMEDIATE;
816                         else if (!strncasecmp(v->value, "an", 2))
817                                 gmode = MODEM_MODE_WAIT_ANSWER;
818                         else
819                                 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
820                 } else if (!strcasecmp(v->name, "stripmsd")) {
821                         stripmsd = atoi(v->value);
822                 } else if (!strcasecmp(v->name, "type")) {
823                         strncpy(mtype, v->value, sizeof(mtype)-1);
824                 } else if (!strcasecmp(v->name, "initstr")) {
825                         strncpy(initstr, v->value, sizeof(initstr)-1);
826                 } else if (!strcasecmp(v->name, "dialtype")) {
827                         dialtype = toupper(v->value[0]);
828                 } else if (!strcasecmp(v->name, "context")) {
829                         strncpy(context, v->value, sizeof(context)-1);
830                 } else if (!strcasecmp(v->name, "msn")) {
831                         strncpy(msn, v->value, sizeof(msn)-1);
832                 } else if (!strcasecmp(v->name, "language")) {
833                         strncpy(language, v->value, sizeof(language)-1);
834                 }
835                 v = v->next;
836         }
837         ast_pthread_mutex_unlock(&iflock);
838         if (ast_channel_register(type, tdesc, /* XXX Don't know our types -- maybe we should register more than one XXX */ 
839                                                 AST_FORMAT_SLINEAR, modem_request)) {
840                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
841                 ast_destroy(cfg);
842                 unload_module();
843                 return -1;
844         }
845         ast_destroy(cfg);
846         /* And start the monitor for the first time */
847         restart_monitor();
848         return 0;
849 }
850
851 int unload_module()
852 {
853         struct ast_modem_pvt *p, *pl;
854         /* First, take us out of the channel loop */
855         ast_channel_unregister(type);
856         if (!ast_pthread_mutex_lock(&iflock)) {
857                 /* Hangup all interfaces if they have an owner */
858                 p = iflist;
859                 while(p) {
860                         if (p->owner)
861                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
862                         p = p->next;
863                 }
864                 iflist = NULL;
865                 ast_pthread_mutex_unlock(&iflock);
866         } else {
867                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
868                 return -1;
869         }
870         if (!ast_pthread_mutex_lock(&monlock)) {
871                 if (monitor_thread > -1) {
872                         pthread_cancel(monitor_thread);
873                         pthread_join(monitor_thread, NULL);
874                 }
875                 monitor_thread = -2;
876                 ast_pthread_mutex_unlock(&monlock);
877         } else {
878                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
879                 return -1;
880         }
881
882         if (!ast_pthread_mutex_lock(&iflock)) {
883                 /* Destroy all the interfaces and free their memory */
884                 p = iflist;
885                 while(p) {
886                         /* Close the socket, assuming it's real */
887                         if (p->fd > -1)
888                                 close(p->fd);
889                         pl = p;
890                         p = p->next;
891                         /* Free associated memory */
892                         free(pl);
893                 }
894                 iflist = NULL;
895                 ast_pthread_mutex_unlock(&iflock);
896         } else {
897                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
898                 return -1;
899         }
900                 
901         return 0;
902 }
903
904 int usecount(void)
905 {
906         int res;
907         ast_pthread_mutex_lock(&usecnt_lock);
908         res = usecnt;
909         ast_pthread_mutex_unlock(&usecnt_lock);
910         return res;
911 }
912
913 char *description()
914 {
915         return desc;
916 }
917
918 char *key()
919 {
920         return ASTERISK_GPL_KEY;
921 }
922