another round of version tag updates, along with 'show version files' pattern filtering
[asterisk/asterisk.git] / channels / chan_modem_aopen.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
16 #include <string.h>
17 #include <stdlib.h>
18 #include <errno.h>
19
20 #include "asterisk.h"
21
22 ASTERISK_FILE_VERSION("$Revision$")
23
24 #include "asterisk/lock.h"
25 #include "asterisk/vmodem.h"
26 #include "asterisk/module.h"
27 #include "asterisk/frame.h"
28 #include "asterisk/logger.h"
29 #include "asterisk/options.h"
30
31 #define STATE_COMMAND   0
32 #define STATE_VOICE     1
33
34 #define VRA "40"                        /* Number of 100ms of non-ring after a ring cadence after which we consider the lien to be answered */
35 #define VRN "100"                       /* Number of 100ms of non-ring with no cadence after which we assume an answer */
36
37 static char *breakcmd = "\0x10\0x03";
38
39 static char *desc = "A/Open (Rockwell Chipset) ITU-2 VoiceModem Driver";
40
41 static int usecnt;
42 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
43
44 static char *aopen_idents[] = {
45         /* Identify A/Open Modem */
46         "V2.210-V90_2M_DLP",
47         NULL
48 };
49
50 static int aopen_setdev(struct ast_modem_pvt *p, int dev)
51 {
52         char cmd[80];
53         if (ast_modem_send(p, "AT#VLS?", 0)) {
54                 ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
55                 return -1;
56         }
57         if (ast_modem_read_response(p, 5)) {
58                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
59                 return -1;
60         }
61         ast_modem_trim(p->response);
62         strncpy(cmd, p->response, sizeof(cmd)-1);
63         if (ast_modem_expect(p, "OK", 5)) {
64                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
65                 return -1;
66         }
67         if (dev == atoi(cmd)) {
68                 /* We're already in the right mode, don't bother changing for fear of
69                    hanging up */
70                 return 0;
71         }
72         snprintf(cmd, sizeof(cmd), "AT#VLS=%d", dev);
73         if (ast_modem_send(p, cmd, 0))  {
74                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
75                 return -1;
76         }
77         if (ast_modem_read_response(p, 5)) {
78                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
79                 return -1;
80         }
81         ast_modem_trim(p->response);
82         if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) {
83                 ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response);
84                 return -1;
85         }
86         return 0;
87 }
88
89 static int aopen_startrec(struct ast_modem_pvt *p)
90 {
91         if (ast_modem_send(p, "AT#VRX", 0) ||
92              ast_modem_expect(p, "CONNECT", 5)) {
93                 ast_log(LOG_WARNING, "Unable to start recording\n");
94                 return -1;
95         }
96         p->ministate = STATE_VOICE;
97         return 0;
98 }
99
100 static int aopen_break(struct ast_modem_pvt *p)
101 {
102         if (ast_modem_send(p, "\r\n", 2)) {
103                 ast_log(LOG_WARNING, "Failed to send enter?\n");
104                 return -1;
105         }
106         if (ast_modem_send(p, breakcmd, 2)) {
107                 ast_log(LOG_WARNING, "Failed to break\n");
108                 return -1;
109         }
110         if (ast_modem_send(p, "\r\n", 2)) {
111                 ast_log(LOG_WARNING, "Failed to send enter?\n");
112                 return -1;
113         }
114         /* Read any outstanding junk */
115         while(!ast_modem_read_response(p, 1));
116         if (ast_modem_send(p, "AT", 0)) {
117                 /* Modem might be stuck in some weird mode, try to get it out */
118                 ast_modem_send(p, "+++", 3);
119                 if (ast_modem_expect(p, "OK", 10)) {
120                         ast_log(LOG_WARNING, "Modem is not responding\n");
121                         return -1;
122                 }
123                 if (ast_modem_send(p, "AT", 0)) {
124                         ast_log(LOG_WARNING, "Modem is not responding\n");
125                         return -1;
126                 }
127         }
128         if (ast_modem_expect(p, "OK", 5)) {
129                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
130                 return -1;
131         }
132         return 0;
133 }
134
135 static int aopen_init(struct ast_modem_pvt *p)
136 {
137         if (option_debug)
138                 ast_log(LOG_DEBUG, "aopen_init()\n");
139         if (aopen_break(p))
140                 return -1;
141         /* Force into command mode */
142         p->ministate = STATE_COMMAND;
143         if (ast_modem_send(p, "AT#BDR=0", 0) ||
144              ast_modem_expect(p, "OK", 5)) {
145                 ast_log(LOG_WARNING, "Unable to set to auto-baud\n");
146                 return -1;
147         }
148         if (ast_modem_send(p, "AT#CLS=8", 0) ||
149              ast_modem_expect(p, "OK", 5)) {
150                 ast_log(LOG_WARNING, "Unable to set to voice mode\n");
151                 return -1;
152         }
153         if (ast_modem_send(p, "AT#VBS=8", 0) ||
154              ast_modem_expect(p, "OK", 5)) {
155                 ast_log(LOG_WARNING, "Unable to set to 8-bit mode\n");
156                 return -1;
157         }
158         if (ast_modem_send(p, "AT#VSR=8000", 0) ||
159              ast_modem_expect(p, "OK", 5)) {
160                 ast_log(LOG_WARNING, "Unable to set to 8000 Hz sampling\n");
161                 return -1;
162         }
163         if (ast_modem_send(p, "AT#VLS=0", 0) ||
164              ast_modem_expect(p, "OK", 5)) {
165                 ast_log(LOG_WARNING, "Unable to set to telco interface\n");
166                 return -1;
167         }
168         if (ast_modem_send(p, "AT#VRA=" VRA, 0) ||
169              ast_modem_expect(p, "OK", 5)) {
170                 ast_log(LOG_WARNING, "Unable to set to 'ringback goes away' timer\n");
171                 return -1;
172         }
173         if (ast_modem_send(p, "AT#VRN=" VRN, 0) ||
174              ast_modem_expect(p, "OK", 5)) {
175                 ast_log(LOG_WARNING, "Unable to set to 'ringback never came timer'\n");
176                 return -1;
177         }
178         if (ast_modem_send(p, "AT#VTD=3F,3F,3F", 0) ||
179              ast_modem_expect(p, "OK", 5)) {
180                 ast_log(LOG_WARNING, "Unable to set to tone detection\n");
181                 return -1;
182         }
183         
184         return 0;
185 }
186
187 static struct ast_frame *aopen_handle_escape(struct ast_modem_pvt *p, char esc)
188 {
189         /* Handle escaped characters -- but sometimes we call it directly as 
190            a quick way to cause known responses */
191         p->fr.frametype = AST_FRAME_NULL;
192         p->fr.subclass = 0;
193         p->fr.data = NULL;
194         p->fr.datalen = 0;
195         p->fr.samples = 0;
196         p->fr.offset = 0;
197         p->fr.mallocd = 0;
198         p->fr.delivery.tv_sec = 0;
199         p->fr.delivery.tv_usec = 0;
200         if (esc)
201                 ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
202         
203         switch(esc) {
204         case 'R': /* Pseudo ring */
205                 p->fr.frametype = AST_FRAME_CONTROL;
206                 p->fr.subclass = AST_CONTROL_RING;
207                 return &p->fr;
208         case 'X': /* Pseudo connect */
209                 p->fr.frametype = AST_FRAME_CONTROL;
210                 p->fr.subclass = AST_CONTROL_RING;
211                 if (p->owner)
212                         ast_setstate(p->owner, AST_STATE_UP);
213                 if (aopen_startrec(p))
214                         return  NULL;
215                 return &p->fr;
216         case 'b': /* Busy signal */
217                 p->fr.frametype = AST_FRAME_CONTROL;
218                 p->fr.subclass = AST_CONTROL_BUSY;
219                 return &p->fr;
220         case 'o': /* Overrun */
221                 ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n");
222                 if (ast_modem_send(p, "\0x10E", 2)) 
223                         ast_log(LOG_WARNING, "Unable to flush buffers\n");
224                 return &p->fr;  
225         case 'u': /* Underrun */
226                 ast_log(LOG_WARNING, "Data underrun\n");
227                 /* Fall Through */
228         case CHAR_ETX: /* End Transmission */
229         case 'd': /* Dialtone */
230         case 'c': /* Calling Tone */
231         case 'e': /* European version */
232         case 'a': /* Answer Tone */
233         case 'f': /* Bell Answer Tone */
234         case 'T': /* Timing mark */
235         case 't': /* Handset off hook */
236         case 'h': /* Handset hungup */
237         case 0: /* Pseudo signal */
238                 /* Ignore */
239                 return &p->fr;  
240         default:
241                 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
242         }
243         return &p->fr;
244 }
245
246 static struct ast_frame *aopen_read(struct ast_modem_pvt *p)
247 {
248         char result[256];
249         short *b;
250         struct ast_frame *f=NULL;
251         int res;
252         int x;
253         if (p->ministate == STATE_COMMAND) {
254                 /* Read the first two bytes, first, in case it's a control message */
255                 fread(result, 1, 2, p->f);
256                 if (result[0] == CHAR_DLE) {
257                         return aopen_handle_escape(p, result[1]);
258                         
259                 } else {
260                         if ((result[0] == '\n') || (result[0] == '\r'))
261                                 return aopen_handle_escape(p, 0);
262                         /* Read the rest of the line */
263                         fgets(result + 2, sizeof(result) - 2, p->f);
264                         ast_modem_trim(result);
265                         if (!strcasecmp(result, "VCON")) {
266                                 /* If we're in immediate mode, reply now */
267                                 if (p->mode == MODEM_MODE_IMMEDIATE)
268                                         return aopen_handle_escape(p, 'X');
269                         } else
270                         if (!strcasecmp(result, "BUSY")) {
271                                 /* Same as a busy signal */
272                                 return aopen_handle_escape(p, 'b');
273                         } else
274                         if (!strcasecmp(result, "RING")) {
275                                 return aopen_handle_escape(p, 'R');
276                         } else
277                         if (!strcasecmp(result, "NO DIALTONE")) {
278                                 /* There's no dialtone, so the line isn't working */
279                                 ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev);
280                                 return NULL;
281                         }
282                         ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
283                         return aopen_handle_escape(p, 0);
284                 }
285         } else {
286                 /* We have to be more efficient in voice mode */
287                 b = (short *)(p->obuf + p->obuflen);
288                 while (p->obuflen/2 < 240) {
289                         /* Read ahead the full amount */
290                         res = fread(result, 1, 240 - p->obuflen/2, p->f);
291                         if (res < 1) {
292                                 /* If there's nothing there, just continue on */
293                                 if (errno == EAGAIN)
294                                         return aopen_handle_escape(p, 0);
295                                 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
296                         }
297                         for (x=0;x<res;x++) {
298                                 /* Process all the bytes that we've read */
299                                 if (result[x] == CHAR_DLE) {
300                                         /* We assume there is no more than one signal frame among our
301                                            data.  */
302                                         if (f) 
303                                                 ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n");
304                                         f = aopen_handle_escape(p, result[x+1]);
305                                         /* If aopen_handle_escape says NULL, say it now, doesn't matter
306                                            what else is there, the connection is dead. */
307                                         if (!f)
308                                                 return NULL;
309                                 } else {
310                                 /* Generate a 16-bit signed linear value from our 
311                                    unsigned 8-bit value */
312                                         *(b++) = (((short)result[x]) - 127) * 0xff;
313                                         p->obuflen += 2;
314                                 }
315                         }
316                         if (f)
317                                 break;
318                 }
319                 /* If we have a control frame, return it now */
320                 if (f)
321                         return f;
322                 /* If we get here, we have a complete voice frame */
323                 p->fr.frametype = AST_FRAME_VOICE;
324                 p->fr.subclass = AST_FORMAT_SLINEAR;
325                 p->fr.samples = 240;
326                 p->fr.data = p->obuf;
327                 p->fr.datalen = p->obuflen;
328                 p->fr.mallocd = 0;
329                 p->fr.delivery.tv_sec = 0;
330                 p->fr.delivery.tv_usec = 0;
331                 p->fr.offset = AST_FRIENDLY_OFFSET;
332                 p->fr.src = __FUNCTION__;
333                 if (option_debug)
334                         ast_log(LOG_DEBUG, "aopen_read(voice frame)\n");
335                 p->obuflen = 0;
336                 return &p->fr;
337         }
338         return NULL;
339 }
340
341 static int aopen_write(struct ast_modem_pvt *p, struct ast_frame *f)
342 {
343         if (option_debug)
344                 ast_log(LOG_DEBUG, "aopen_write()\n");
345         return 0;
346 }
347
348 static char *aopen_identify(struct ast_modem_pvt *p)
349 {
350         char identity[256];
351         char mfr[80];
352         char mdl[80];
353         char rev[80];
354         ast_modem_send(p, "AT#MDL?", 0);
355         ast_modem_read_response(p, 5);
356         strncpy(mdl, p->response, sizeof(mdl)-1);
357         ast_modem_trim(mdl);
358         ast_modem_expect(p, "OK", 5);
359         ast_modem_send(p, "AT#MFR?", 0);
360         ast_modem_read_response(p, 5);
361         strncpy(mfr, p->response, sizeof(mfr)-1);
362         ast_modem_trim(mfr);
363         ast_modem_expect(p, "OK", 5);
364         ast_modem_send(p, "AT#REV?", 0);
365         ast_modem_read_response(p, 5);
366         strncpy(rev, p->response, sizeof(rev)-1);
367         ast_modem_trim(rev);
368         ast_modem_expect(p, "OK", 5);
369         snprintf(identity, sizeof(identity), "%s Model %s Revision %s", mfr, mdl, rev);
370         return strdup(identity);
371 }
372
373 static void aopen_incusecnt(void)
374 {
375         ast_mutex_lock(&usecnt_lock);
376         usecnt++;
377         ast_mutex_unlock(&usecnt_lock);
378         ast_update_use_count();
379 }
380
381 static void aopen_decusecnt(void)
382 {
383         ast_mutex_lock(&usecnt_lock);
384         usecnt++;
385         ast_mutex_unlock(&usecnt_lock);
386         ast_update_use_count();
387 }
388
389 static int aopen_answer(struct ast_modem_pvt *p)
390 {
391         if (ast_modem_send(p, "ATA", 0) ||
392              ast_modem_expect(p, "VCON", 10)) {
393                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
394                 return -1;
395         }
396         return 0;
397 }
398
399 static int aopen_dialdigit(struct ast_modem_pvt *p, char digit)
400 {
401         char cmd[80];
402         snprintf(cmd, sizeof(cmd), "AT#VTS=%c", digit);
403         if (ast_modem_send(p, cmd, 0) ||
404              ast_modem_expect(p, "VCON", 10)) {
405                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
406                 return -1;
407         }
408         return 0;
409 }
410
411 static int aopen_dial(struct ast_modem_pvt *p, char *stuff)
412 {
413         char cmd[80];
414         snprintf(cmd, sizeof(cmd), "ATD%c %s", p->dialtype,stuff);
415         if (ast_modem_send(p, cmd, 0)) {
416                 ast_log(LOG_WARNING, "Unable to dial\n");
417                 return -1;
418         }
419         return 0;
420 }
421
422 static int aopen_hangup(struct ast_modem_pvt *p)
423 {
424         if (aopen_break(p))
425                 return -1;
426         /* Hangup by switching to data, then back to voice */
427         if (ast_modem_send(p, "ATH", 0) ||
428              ast_modem_expect(p, "OK", 8)) {
429                 ast_log(LOG_WARNING, "Unable to set to data mode\n");
430                 return -1;
431         }
432         if (ast_modem_send(p, "AT#CLS=8", 0) ||
433              ast_modem_expect(p, "OK", 5)) {
434                 ast_log(LOG_WARNING, "Unable to set to voice mode\n");
435                 return -1;
436         }
437         return 0;
438 }
439
440 static struct ast_modem_driver aopen_driver =
441 {
442         "AOpen",
443         aopen_idents,
444         AST_FORMAT_SLINEAR,
445         0,              /* Not full duplex */
446         aopen_incusecnt,        /* incusecnt */
447         aopen_decusecnt,        /* decusecnt */
448         aopen_identify, /* identify */
449         aopen_init,     /* init */
450         aopen_setdev,   /* setdev */
451         aopen_read,
452         aopen_write,
453         aopen_dial,     /* dial */
454         aopen_answer,   /* answer */
455         aopen_hangup,   /* hangup */
456         aopen_startrec, /* start record */
457         NULL,   /* stop record */
458         NULL,   /* start playback */
459         NULL,   /* stop playback */
460         NULL,   /* set silence supression */
461         aopen_dialdigit,        /* dialdigit */
462 };
463
464
465
466 int usecount(void)
467 {
468         int res;
469         ast_mutex_lock(&usecnt_lock);
470         res = usecnt;
471         ast_mutex_unlock(&usecnt_lock);
472         return res;
473 }
474
475 int load_module(void)
476 {
477         return ast_register_modem_driver(&aopen_driver);
478 }
479
480 int unload_module(void)
481 {
482         return ast_unregister_modem_driver(&aopen_driver);
483 }
484
485 char *description()
486 {
487         return desc;
488 }
489
490 char *key()
491 {
492         return ASTERISK_GPL_KEY;
493 }