remove leading space in Packet manager event header
[asterisk/asterisk.git] / res / res_adsi.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Includes code and algorithms from the Zapata library.
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief ADSI support 
24  *
25  * \author Mark Spencer <markster@digium.com> 
26  *
27  * \note this module is required by app_voicemail and app_getcpeid
28  * \todo Move app_getcpeid into this module
29  * \todo Create a core layer so that app_voicemail does not require
30  *      res_adsi to load
31  */
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <time.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <math.h>
43 #include <errno.h>
44
45 #include "asterisk/ulaw.h"
46 #include "asterisk/alaw.h"
47 #include "asterisk/callerid.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/fskmodem.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/adsi.h"
52 #include "asterisk/module.h"
53 #include "asterisk/config.h"
54 #include "asterisk/file.h"
55
56 #define DEFAULT_ADSI_MAX_RETRIES 3
57
58 #define ADSI_MAX_INTRO 20
59 #define ADSI_MAX_SPEED_DIAL 6
60
61 #define ADSI_FLAG_DATAMODE      (1 << 8)
62
63 static int maxretries = DEFAULT_ADSI_MAX_RETRIES;
64
65 /* Asterisk ADSI button definitions */
66 #define ADSI_SPEED_DIAL         10      /* 10-15 are reserved for speed dial */
67
68 static char intro[ADSI_MAX_INTRO][20];
69 static int aligns[ADSI_MAX_INTRO];
70
71 static char speeddial[ADSI_MAX_SPEED_DIAL][3][20];
72
73 static int alignment = 0;
74
75 static int adsi_generate(unsigned char *buf, int msgtype, unsigned char *msg, int msglen, int msgnum, int last, int codec)
76 {
77         int sum;
78         int x;  
79         int bytes=0;
80         /* Initial carrier (imaginary) */
81         float cr = 1.0;
82         float ci = 0.0;
83         float scont = 0.0;
84
85         if (msglen > 255)
86                 msglen = 255;
87
88         /* If first message, Send 150ms of MARK's */
89         if (msgnum == 1) {
90                 for (x=0;x<150;x++)     /* was 150 */
91                         PUT_CLID_MARKMS;
92         }
93         /* Put message type */
94         PUT_CLID(msgtype);
95         sum = msgtype;
96
97         /* Put message length (plus one  for the message number) */
98         PUT_CLID(msglen + 1);
99         sum += msglen + 1;
100
101         /* Put message number */
102         PUT_CLID(msgnum);
103         sum += msgnum;
104
105         /* Put actual message */
106         for (x=0;x<msglen;x++) {
107                 PUT_CLID(msg[x]);
108                 sum += msg[x];
109         }
110
111         /* Put 2's compliment of sum */
112         PUT_CLID(256-(sum & 0xff));
113
114 #if 0
115         if (last) {
116                 /* Put trailing marks */
117                 for (x=0;x<50;x++)
118                         PUT_CLID_MARKMS;
119         }
120 #endif
121         return bytes;
122
123 }
124
125 static int adsi_careful_send(struct ast_channel *chan, unsigned char *buf, int len, int *remainder)
126 {
127         /* Sends carefully on a full duplex channel by using reading for
128            timing */
129         struct ast_frame *inf, outf;
130         int amt;
131
132         /* Zero out our outgoing frame */
133         memset(&outf, 0, sizeof(outf));
134
135         if (remainder && *remainder) {
136                 amt = len;
137
138                 /* Send remainder if provided */
139                 if (amt > *remainder)
140                         amt = *remainder;
141                 else
142                         *remainder = *remainder - amt;
143                 outf.frametype = AST_FRAME_VOICE;
144                 outf.subclass = AST_FORMAT_ULAW;
145                 outf.data = buf;
146                 outf.datalen = amt;
147                 outf.samples = amt;
148                 if (ast_write(chan, &outf)) {
149                         ast_log(LOG_WARNING, "Failed to carefully write frame\n");
150                         return -1;
151                 }
152                 /* Update pointers and lengths */
153                 buf += amt;
154                 len -= amt;
155         }
156
157         while(len) {
158                 amt = len;
159                 /* If we don't get anything at all back in a second, forget
160                    about it */
161                 if (ast_waitfor(chan, 1000) < 1)
162                         return -1;
163                 inf = ast_read(chan);
164                 /* Detect hangup */
165                 if (!inf)
166                         return -1;
167                 if (inf->frametype == AST_FRAME_VOICE) {
168                         /* Read a voice frame */
169                         if (inf->subclass != AST_FORMAT_ULAW) {
170                                 ast_log(LOG_WARNING, "Channel not in ulaw?\n");
171                                 return -1;
172                         }
173                         /* Send no more than they sent us */
174                         if (amt > inf->datalen)
175                                 amt = inf->datalen;
176                         else if (remainder)
177                                 *remainder = inf->datalen - amt;
178                         outf.frametype = AST_FRAME_VOICE;
179                         outf.subclass = AST_FORMAT_ULAW;
180                         outf.data = buf;
181                         outf.datalen = amt;
182                         outf.samples = amt;
183                         if (ast_write(chan, &outf)) {
184                                 ast_log(LOG_WARNING, "Failed to carefully write frame\n");
185                                 return -1;
186                         }
187                         /* Update pointers and lengths */
188                         buf += amt;
189                         len -= amt;
190                 }
191                 ast_frfree(inf);
192         }
193         return 0;
194 }
195
196 static int __adsi_transmit_messages(struct ast_channel *chan, unsigned char **msg, int *msglen, int *msgtype)
197 {
198         /* msglen must be no more than 256 bits, each */
199         unsigned char buf[24000 * 5];
200         int pos = 0, res;
201         int x;
202         int start=0;
203         int retries = 0;
204
205         char ack[3];
206
207         /* Wait up to 500 ms for initial ACK */
208         int waittime;
209         struct ast_frame *f;
210         int rem = 0;
211         int def;
212
213         if (chan->adsicpe == AST_ADSI_UNAVAILABLE) {
214                 /* Don't bother if we know they don't support ADSI */
215                 errno = ENOSYS;
216                 return -1;
217         }
218
219         while(retries < maxretries) {
220                 if (!(chan->adsicpe & ADSI_FLAG_DATAMODE)) {
221                         /* Generate CAS (no SAS) */
222                         ast_gen_cas(buf, 0, 680, AST_FORMAT_ULAW);
223                 
224                         /* Send CAS */
225                         if (adsi_careful_send(chan, buf, 680, NULL)) {
226                                 ast_log(LOG_WARNING, "Unable to send CAS\n");
227                         }
228                         /* Wait For DTMF result */
229                         waittime = 500;
230                         for(;;) {
231                                 if (((res = ast_waitfor(chan, waittime)) < 1)) {
232                                         /* Didn't get back DTMF A in time */
233                                         ast_log(LOG_DEBUG, "No ADSI CPE detected (%d)\n", res);
234                                         if (!chan->adsicpe)
235                                                 chan->adsicpe = AST_ADSI_UNAVAILABLE;
236                                         errno = ENOSYS;
237                                         return -1;
238                                 }
239                                 waittime = res;
240                                 f = ast_read(chan);
241                                 if (!f) {
242                                         ast_log(LOG_DEBUG, "Hangup in ADSI\n");
243                                         return -1;
244                                 }
245                                 if (f->frametype == AST_FRAME_DTMF) {
246                                         if (f->subclass == 'A') {
247                                                 /* Okay, this is an ADSI CPE.  Note this for future reference, too */
248                                                 if (!chan->adsicpe)
249                                                         chan->adsicpe = AST_ADSI_AVAILABLE;
250                                                 break;
251                                         } else {
252                                                 if (f->subclass == 'D')  {
253                                                         ast_log(LOG_DEBUG, "Off-hook capable CPE only, not ADSI\n");
254                                                 } else
255                                                         ast_log(LOG_WARNING, "Unknown ADSI response '%c'\n", f->subclass);
256                                                 if (!chan->adsicpe)
257                                                         chan->adsicpe = AST_ADSI_UNAVAILABLE;
258                                                 errno = ENOSYS;
259                                                 return -1;
260                                         }
261                                 }
262                                 ast_frfree(f);
263                         }
264
265                         ast_log(LOG_DEBUG, "ADSI Compatible CPE Detected\n");
266                 } else
267                         ast_log(LOG_DEBUG, "Already in data mode\n");
268
269                 x = 0;
270                 pos = 0;
271 #if 1
272                 def= ast_channel_defer_dtmf(chan);
273 #endif
274                 while((x < 6) && msg[x]) {
275                         res = adsi_generate(buf + pos, msgtype[x], msg[x], msglen[x], x+1 - start, (x == 5) || !msg[x+1], AST_FORMAT_ULAW);
276                         if (res < 0) {
277                                 ast_log(LOG_WARNING, "Failed to generate ADSI message %d on channel %s\n", x + 1, chan->name);
278                                 return -1;
279                         }
280                         ast_log(LOG_DEBUG, "Message %d, of %d input bytes, %d output bytes\n", 
281                                         x + 1, msglen[x], res);
282                         pos += res; 
283                         x++;
284                 }
285
286
287                 rem = 0;
288                 res = adsi_careful_send(chan, buf, pos, &rem); 
289                 if (!def)
290                         ast_channel_undefer_dtmf(chan);
291                 if (res)
292                         return -1;
293
294                 ast_log(LOG_DEBUG, "Sent total spill of %d bytes\n", pos);
295
296                 memset(ack, 0, sizeof(ack));
297                 /* Get real result */
298                 res = ast_readstring(chan, ack, 2, 1000, 1000, "");
299                 /* Check for hangup */
300                 if (res < 0)
301                         return -1;
302                 if (ack[0] == 'D') {
303                         ast_log(LOG_DEBUG, "Acked up to message %d\n", atoi(ack + 1));
304                         start += atoi(ack + 1);
305                         if (start >= x)
306                                 break;
307                         else {
308                                 retries++;
309                                 ast_log(LOG_DEBUG, "Retransmitting (%d), from %d\n", retries, start + 1);
310                         }
311                 } else {
312                         retries++;
313                         ast_log(LOG_WARNING, "Unexpected response to ack: %s (retry %d)\n", ack, retries);
314                 } 
315         }
316         if (retries >= maxretries) {
317                 ast_log(LOG_WARNING, "Maximum ADSI Retries (%d) exceeded\n", maxretries);
318                 errno = ETIMEDOUT;
319                 return -1;
320         }
321         return 0;
322         
323 }
324
325 int adsi_begin_download(struct ast_channel *chan, char *service, unsigned char *fdn, unsigned char *sec, int version)
326 {
327         int bytes;
328         unsigned char buf[256];
329         char ack[2];
330         bytes = 0;
331         /* Setup the resident soft key stuff, a piece at a time */
332         /* Upload what scripts we can for voicemail ahead of time */
333         bytes += adsi_download_connect(buf + bytes, service, fdn, sec, version);
334         if (adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0))
335                 return -1;
336         if (ast_readstring(chan, ack, 1, 10000, 10000, ""))
337                 return -1;
338         if (ack[0] == 'B')
339                 return 0;
340         ast_log(LOG_DEBUG, "Download was denied by CPE\n");
341         return -1;
342 }
343
344 int adsi_end_download(struct ast_channel *chan)
345 {
346         int bytes;
347         unsigned char buf[256];
348         bytes = 0;
349         /* Setup the resident soft key stuff, a piece at a time */
350         /* Upload what scripts we can for voicemail ahead of time */
351         bytes += adsi_download_disconnect(buf + bytes);
352         if (adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0))
353                 return -1;
354         return 0;
355 }
356
357 int adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait)
358 {
359         unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL };
360         int msglens[5];
361         int msgtypes[5];
362         int newdatamode;
363         int res;
364         int x;
365         int writeformat, readformat;
366         int waitforswitch = 0;
367
368         writeformat = chan->writeformat;
369         readformat = chan->readformat;
370
371         newdatamode = chan->adsicpe & ADSI_FLAG_DATAMODE;
372
373         for (x=0;x<msglen;x+=(msg[x+1]+2)) {
374                 if (msg[x] == ADSI_SWITCH_TO_DATA) {
375                         ast_log(LOG_DEBUG, "Switch to data is sent!\n");
376                         waitforswitch++;
377                         newdatamode = ADSI_FLAG_DATAMODE;
378                 }
379                 
380                 if (msg[x] == ADSI_SWITCH_TO_VOICE) {
381                         ast_log(LOG_DEBUG, "Switch to voice is sent!\n");
382                         waitforswitch++;
383                         newdatamode = 0;
384                 }
385         }
386         msgs[0] = msg;
387
388         msglens[0] = msglen;
389         msgtypes[0] = msgtype;
390
391         if (msglen > 253) {
392                 ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen);
393                 return -1;
394         }
395
396         ast_stopstream(chan);
397
398         if (ast_set_write_format(chan, AST_FORMAT_ULAW)) {
399                 ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
400                 return -1;
401         }
402
403         if (ast_set_read_format(chan, AST_FORMAT_ULAW)) {
404                 ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
405                 if (writeformat) {
406                         if (ast_set_write_format(chan, writeformat)) 
407                                 ast_log(LOG_WARNING, "Unable to restore write format to %d\n", writeformat);
408                 }
409                 return -1;
410         }
411         res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes);
412
413         if (dowait) {
414                 ast_log(LOG_DEBUG, "Wait for switch is '%d'\n", waitforswitch);
415                 while(waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) { res = 0; ast_log(LOG_DEBUG, "Waiting for 'B'...\n"); }
416         }
417         
418         if (!res)
419                 chan->adsicpe = (chan->adsicpe & ~ADSI_FLAG_DATAMODE) | newdatamode;
420
421         if (writeformat)
422                 ast_set_write_format(chan, writeformat);
423         if (readformat)
424                 ast_set_read_format(chan, readformat);
425
426         if (!res)
427                 res = ast_safe_sleep(chan, 100 );
428         return res;
429 }
430
431 int adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype)
432 {
433         return adsi_transmit_message_full(chan, msg, msglen, msgtype, 1);
434 }
435
436 static inline int ccopy(unsigned char *dst, const unsigned char *src, int max)
437 {
438         int x=0;
439         /* Carefully copy the requested data */
440         while ((x < max) && src[x] && (src[x] != 0xff)) {
441                 dst[x] = src[x];
442                 x++;
443         }
444         return x;
445 }
446
447 int adsi_load_soft_key(unsigned char *buf, int key, const char *llabel, const char *slabel, const char *ret, int data)
448 {
449         int bytes=0;
450
451         /* Abort if invalid key specified */
452         if ((key < 2) || (key > 33))
453                 return -1;
454         buf[bytes++] = ADSI_LOAD_SOFTKEY;
455         /* Reserve for length */
456         bytes++;
457         /* Which key */
458         buf[bytes++] = key;
459
460         /* Carefully copy long label */
461         bytes += ccopy(buf + bytes, (const unsigned char *)llabel, 18);
462
463         /* Place delimiter */
464         buf[bytes++] = 0xff;
465
466         /* Short label */
467         bytes += ccopy(buf + bytes, (const unsigned char *)slabel, 7);
468
469
470         /* If specified, copy return string */
471         if (ret) {
472                 /* Place delimiter */
473                 buf[bytes++] = 0xff;
474                 if (data)
475                         buf[bytes++] = ADSI_SWITCH_TO_DATA2;
476                 /* Carefully copy return string */
477                 bytes += ccopy(buf + bytes, (const unsigned char *)ret, 20);
478
479         }
480         /* Replace parameter length */
481         buf[1] = bytes - 2;
482         return bytes;
483         
484 }
485
486 int adsi_connect_session(unsigned char *buf, unsigned char *fdn, int ver)
487 {
488         int bytes=0;
489         int x;
490
491         /* Message type */
492         buf[bytes++] = ADSI_CONNECT_SESSION;
493
494         /* Reserve space for length */
495         bytes++;
496
497         if (fdn) {
498                 for (x=0;x<4;x++)
499                         buf[bytes++] = fdn[x];
500                 if (ver > -1)
501                         buf[bytes++] = ver & 0xff;
502         }
503
504         buf[1] = bytes - 2;
505         return bytes;
506
507 }
508
509 int adsi_download_connect(unsigned char *buf, char *service,  unsigned char *fdn, unsigned char *sec, int ver)
510 {
511         int bytes=0;
512         int x;
513
514         /* Message type */
515         buf[bytes++] = ADSI_DOWNLOAD_CONNECT;
516
517         /* Reserve space for length */
518         bytes++;
519
520         /* Primary column */
521         bytes+= ccopy(buf + bytes, (unsigned char *)service, 18);
522
523         /* Delimiter */
524         buf[bytes++] = 0xff;
525         
526         for (x=0;x<4;x++) {
527                 buf[bytes++] = fdn[x];
528         }
529         for (x=0;x<4;x++)
530                 buf[bytes++] = sec[x];
531         buf[bytes++] = ver & 0xff;
532
533         buf[1] = bytes - 2;
534
535         return bytes;
536
537 }
538
539 int adsi_disconnect_session(unsigned char *buf)
540 {
541         int bytes=0;
542
543         /* Message type */
544         buf[bytes++] = ADSI_DISC_SESSION;
545
546         /* Reserve space for length */
547         bytes++;
548
549         buf[1] = bytes - 2;
550         return bytes;
551
552 }
553
554 int adsi_query_cpeid(unsigned char *buf)
555 {
556         int bytes = 0;
557         buf[bytes++] = ADSI_QUERY_CPEID;
558         /* Reserve space for length */
559         bytes++;
560         buf[1] = bytes - 2;
561         return bytes;
562 }
563
564 int adsi_query_cpeinfo(unsigned char *buf)
565 {
566         int bytes = 0;
567         buf[bytes++] = ADSI_QUERY_CONFIG;
568         /* Reserve space for length */
569         bytes++;
570         buf[1] = bytes - 2;
571         return bytes;
572 }
573
574 int adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen)
575 {
576         int bytes = 0;
577         int res;
578         unsigned char current = 0;
579         int gotstar = 0;
580         int pos = 0;
581         memset(buf, 0, sizeof(buf));
582         while(bytes <= maxlen) {
583                 /* Wait up to a second for a digit */
584                 res = ast_waitfordigit(chan, 1000);
585                 if (!res)
586                         break;
587                 if (res == '*') {
588                         gotstar = 1;    
589                         continue;
590                 }
591                 /* Ignore anything other than a digit */
592                 if ((res < '0') || (res > '9'))
593                         continue;
594                 res -= '0';
595                 if (gotstar)
596                         res += 9;
597                 if (pos)  {
598                         pos = 0;
599                         buf[bytes++] = (res << 4) | current;
600                 } else {
601                         pos = 1;
602                         current = res;
603                 }
604                 gotstar = 0;
605         }
606         return bytes;
607 }
608
609 int adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice)
610 {
611         unsigned char buf[256];
612         int bytes = 0;
613         int res;
614         bytes += adsi_data_mode(buf);
615         adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
616
617         bytes = 0;
618         bytes += adsi_query_cpeid(buf);
619         adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
620
621         /* Get response */
622         memset(buf, 0, sizeof(buf));
623         res = adsi_read_encoded_dtmf(chan, cpeid, 4);
624         if (res != 4) {
625                 ast_log(LOG_WARNING, "Got %d bytes back of encoded DTMF, expecting 4\n", res);
626                 res = 0;
627         } else {
628                 res = 1;
629         }
630
631         if (voice) {
632                 bytes = 0;
633                 bytes += adsi_voice_mode(buf, 0);
634                 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
635                 /* Ignore the resulting DTMF B announcing it's in voice mode */
636                 ast_waitfordigit(chan, 1000);
637         }
638         return res;
639 }
640
641 int adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice)
642 {
643         unsigned char buf[256];
644         int bytes = 0;
645         int res;
646         bytes += adsi_data_mode(buf);
647         adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
648
649         bytes = 0;
650         bytes += adsi_query_cpeinfo(buf);
651         adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
652
653         /* Get width */
654         memset(buf, 0, sizeof(buf));
655         res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "");
656         if (res < 0)
657                 return res;
658         if (strlen((char *)buf) != 2) {
659                 ast_log(LOG_WARNING, "Got %d bytes of width, expecting 2\n", res);
660                 res = 0;
661         } else {
662                 res = 1;
663         }
664         if (width)
665                 *width = atoi((char *)buf);
666         /* Get height */
667         memset(buf, 0, sizeof(buf));
668         if (res) {
669                 res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "");
670                 if (res < 0)
671                         return res;
672                 if (strlen((char *)buf) != 2) {
673                         ast_log(LOG_WARNING, "Got %d bytes of height, expecting 2\n", res);
674                         res = 0;
675                 } else {
676                         res = 1;
677                 }       
678                 if (height)
679                         *height= atoi((char *)buf);
680         }
681         /* Get buttons */
682         memset(buf, 0, sizeof(buf));
683         if (res) {
684                 res = ast_readstring(chan, (char *)buf, 1, 1000, 500, "");
685                 if (res < 0)
686                         return res;
687                 if (strlen((char *)buf) != 1) {
688                         ast_log(LOG_WARNING, "Got %d bytes of buttons, expecting 1\n", res);
689                         res = 0;
690                 } else {
691                         res = 1;
692                 }       
693                 if (buttons)
694                         *buttons = atoi((char *)buf);
695         }
696         if (voice) {
697                 bytes = 0;
698                 bytes += adsi_voice_mode(buf, 0);
699                 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
700                 /* Ignore the resulting DTMF B announcing it's in voice mode */
701                 ast_waitfordigit(chan, 1000);
702         }
703         return res;
704 }
705
706 int adsi_data_mode(unsigned char *buf)
707 {
708         int bytes=0;
709
710         /* Message type */
711         buf[bytes++] = ADSI_SWITCH_TO_DATA;
712
713         /* Reserve space for length */
714         bytes++;
715
716         buf[1] = bytes - 2;
717         return bytes;
718
719 }
720
721 int adsi_clear_soft_keys(unsigned char *buf)
722 {
723         int bytes=0;
724
725         /* Message type */
726         buf[bytes++] = ADSI_CLEAR_SOFTKEY;
727
728         /* Reserve space for length */
729         bytes++;
730
731         buf[1] = bytes - 2;
732         return bytes;
733
734 }
735
736 int adsi_clear_screen(unsigned char *buf)
737 {
738         int bytes=0;
739
740         /* Message type */
741         buf[bytes++] = ADSI_CLEAR_SCREEN;
742
743         /* Reserve space for length */
744         bytes++;
745
746         buf[1] = bytes - 2;
747         return bytes;
748
749 }
750
751 int adsi_voice_mode(unsigned char *buf, int when)
752 {
753         int bytes=0;
754
755         /* Message type */
756         buf[bytes++] = ADSI_SWITCH_TO_VOICE;
757
758         /* Reserve space for length */
759         bytes++;
760
761         buf[bytes++] = when & 0x7f;
762
763         buf[1] = bytes - 2;
764         return bytes;
765
766 }
767
768 int adsi_available(struct ast_channel *chan)
769 {
770         int cpe = chan->adsicpe & 0xff;
771         if ((cpe == AST_ADSI_AVAILABLE) ||
772             (cpe == AST_ADSI_UNKNOWN))
773                 return 1;
774         return 0;
775 }
776
777 int adsi_download_disconnect(unsigned char *buf)
778 {
779         int bytes=0;
780
781         /* Message type */
782         buf[bytes++] = ADSI_DOWNLOAD_DISC;
783
784         /* Reserve space for length */
785         bytes++;
786
787         buf[1] = bytes - 2;
788         return bytes;
789
790 }
791
792 int adsi_display(unsigned char *buf, int page, int line, int just, int wrap, 
793                  char *col1, char *col2)
794 {
795         int bytes=0;
796
797         /* Sanity check line number */
798
799         if (page) {
800                 if (line > 4) return -1;
801         } else {
802                 if (line > 33) return -1;
803         }
804
805         if (line < 1)
806                 return -1;
807         /* Parameter type */
808         buf[bytes++] = ADSI_LOAD_VIRTUAL_DISP;
809         
810         /* Reserve space for size */
811         bytes++;
812
813         /* Page and wrap indicator */
814         buf[bytes++] = ((page & 0x1) << 7) | ((wrap & 0x1) << 6) | (line & 0x3f);
815
816         /* Justification */
817         buf[bytes++] = (just & 0x3) << 5;
818
819         /* Omit highlight mode definition */
820         buf[bytes++] = 0xff;
821
822         /* Primary column */
823         bytes+= ccopy(buf + bytes, (unsigned char *)col1, 20);
824
825         /* Delimiter */
826         buf[bytes++] = 0xff;
827         
828         /* Secondary column */
829         bytes += ccopy(buf + bytes, (unsigned char *)col2, 20);
830
831         /* Update length */
832         buf[1] = bytes - 2;
833         
834         return bytes;
835
836 }
837
838 int adsi_input_control(unsigned char *buf, int page, int line, int display, int format, int just)
839 {
840         int bytes=0;
841
842         if (page) {
843                 if (line > 4) return -1;
844         } else {
845                 if (line > 33) return -1;
846         }
847
848         if (line < 1)
849                 return -1;
850
851         buf[bytes++] = ADSI_INPUT_CONTROL;
852         bytes++;
853         buf[bytes++] = ((page & 1) << 7) | (line & 0x3f);
854         buf[bytes++] = ((display & 1) << 7) | ((just & 0x3) << 4) | (format & 0x7);
855         
856         buf[1] = bytes - 2;
857         return bytes;
858
859 }
860
861 int adsi_input_format(unsigned char *buf, int num, int dir, int wrap, char *format1, char *format2)
862 {
863         int bytes = 0;
864
865         if (!strlen((char *)format1))
866                 return -1;
867
868         buf[bytes++] = ADSI_INPUT_FORMAT;
869         bytes++;
870         buf[bytes++] = ((dir & 1) << 7) | ((wrap & 1) << 6) | (num & 0x7);
871         bytes += ccopy(buf + bytes, (unsigned char *)format1, 20);
872         buf[bytes++] = 0xff;
873         if (format2 && strlen((char *)format2)) {
874                 bytes += ccopy(buf + bytes, (unsigned char *)format2, 20);
875         }
876         buf[1] = bytes - 2;
877         return bytes;
878 }
879
880 int adsi_set_keys(unsigned char *buf, unsigned char *keys)
881 {
882         int bytes=0;
883         int x;
884         /* Message type */
885         buf[bytes++] = ADSI_INIT_SOFTKEY_LINE;
886         /* Space for size */
887         bytes++;
888         /* Key definitions */
889         for (x=0;x<6;x++)
890                 buf[bytes++] = (keys[x] & 0x3f) ? keys[x] : (keys[x] | 0x1);
891         buf[1] = bytes - 2;
892         return bytes;
893 }
894
895 int adsi_set_line(unsigned char *buf, int page, int line)
896 {
897         int bytes=0;
898
899         /* Sanity check line number */
900
901         if (page) {
902                 if (line > 4) return -1;
903         } else {
904                 if (line > 33) return -1;
905         }
906
907         if (line < 1)
908                 return -1;
909         /* Parameter type */
910         buf[bytes++] = ADSI_LINE_CONTROL;
911         
912         /* Reserve space for size */
913         bytes++;
914
915         /* Page and line */
916         buf[bytes++] = ((page & 0x1) << 7) | (line & 0x3f);
917
918         buf[1] = bytes - 2;
919         return bytes;
920
921 };
922
923 static int total = 0;
924 static int speeds = 0;
925
926 int adsi_channel_restore(struct ast_channel *chan)
927 {
928         unsigned char dsp[256];
929         int bytes;
930         int x;
931         unsigned char keyd[6];
932
933         memset(dsp, 0, sizeof(dsp));
934
935         /* Start with initial display setup */
936         bytes = 0;
937         bytes += adsi_set_line(dsp + bytes, ADSI_INFO_PAGE, 1);
938
939         /* Prepare key setup messages */
940
941         if (speeds) {
942                 memset(keyd, 0, sizeof(keyd));
943                 for (x=0;x<speeds;x++) {
944                         keyd[x] = ADSI_SPEED_DIAL + x;
945                 }
946                 bytes += adsi_set_keys(dsp + bytes, keyd);
947         }
948         adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0);
949         return 0;
950
951 }
952
953 int adsi_print(struct ast_channel *chan, char **lines, int *aligns, int voice)
954 {
955         unsigned char buf[4096];
956         int bytes=0;
957         int res;
958         int x;
959         for(x=0;lines[x];x++) 
960                 bytes += adsi_display(buf + bytes, ADSI_INFO_PAGE, x+1, aligns[x], 0, lines[x], "");
961         bytes += adsi_set_line(buf + bytes, ADSI_INFO_PAGE, 1);
962         if (voice) {
963                 bytes += adsi_voice_mode(buf + bytes, 0);
964         }
965         res = adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
966         if (voice) {
967                 /* Ignore the resulting DTMF B announcing it's in voice mode */
968                 ast_waitfordigit(chan, 1000);
969         }
970         return res;
971 }
972
973 int adsi_load_session(struct ast_channel *chan, unsigned char *app, int ver, int data)
974 {
975         unsigned char dsp[256];
976         int bytes;
977         int res;
978         char resp[2];
979
980         memset(dsp, 0, sizeof(dsp));
981
982         /* Connect to session */
983         bytes = 0;
984         bytes += adsi_connect_session(dsp + bytes, app, ver);
985
986         if (data)
987                 bytes += adsi_data_mode(dsp + bytes);
988
989         /* Prepare key setup messages */
990         if (adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0))
991                 return -1;
992         if (app) {
993                 res = ast_readstring(chan, resp, 1, 1200, 1200, "");
994                 if (res < 0)
995                         return -1;
996                 if (res) {
997                         ast_log(LOG_DEBUG, "No response from CPE about version.  Assuming not there.\n");
998                         return 0;
999                 }
1000                 if (!strcmp(resp, "B")) {
1001                         ast_log(LOG_DEBUG, "CPE has script '%s' version %d already loaded\n", app, ver);
1002                         return 1;
1003                 } else if (!strcmp(resp, "A")) {
1004                         ast_log(LOG_DEBUG, "CPE hasn't script '%s' version %d already loaded\n", app, ver);
1005                 } else {
1006                         ast_log(LOG_WARNING, "Unexpected CPE response to script query: %s\n", resp);
1007                 }
1008         } else
1009                 return 1;
1010         return 0;
1011
1012 }
1013
1014 int adsi_unload_session(struct ast_channel *chan)
1015 {
1016         unsigned char dsp[256];
1017         int bytes;
1018
1019         memset(dsp, 0, sizeof(dsp));
1020
1021         /* Connect to session */
1022         bytes = 0;
1023         bytes += adsi_disconnect_session(dsp + bytes);
1024         bytes += adsi_voice_mode(dsp + bytes, 0);
1025
1026         /* Prepare key setup messages */
1027         if (adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0))
1028                 return -1;
1029         return 0;
1030 }
1031
1032 static int str2align(char *s)
1033 {
1034         if (!strncasecmp(s, "l", 1))
1035                 return ADSI_JUST_LEFT;
1036         else if (!strncasecmp(s, "r", 1))
1037                 return ADSI_JUST_RIGHT;
1038         else if (!strncasecmp(s, "i", 1))
1039                 return ADSI_JUST_IND;
1040         else
1041                 return ADSI_JUST_CENT;
1042 }
1043
1044 static void init_state(void)
1045 {
1046         int x;
1047
1048         for (x=0;x<ADSI_MAX_INTRO;x++)
1049                 aligns[x] = ADSI_JUST_CENT;
1050         ast_copy_string(intro[0], "Welcome to the", sizeof(intro[0]));
1051         ast_copy_string(intro[1], "Asterisk", sizeof(intro[1]));
1052         ast_copy_string(intro[2], "Open Source PBX", sizeof(intro[2]));
1053         total = 3;
1054         speeds = 0;
1055         for (x=3;x<ADSI_MAX_INTRO;x++)
1056                 intro[x][0] = '\0';
1057         memset(speeddial, 0, sizeof(speeddial));
1058         alignment = ADSI_JUST_CENT;
1059 }
1060
1061 static void adsi_load(void)
1062 {
1063         int x;
1064         struct ast_config *conf;
1065         struct ast_variable *v;
1066         char *name, *sname;
1067         init_state();
1068         conf = ast_config_load("adsi.conf");
1069         if (conf) {
1070                 x=0;
1071                 for (v = ast_variable_browse(conf, "intro"); v; v = v->next) {
1072                         if (!strcasecmp(v->name, "alignment"))
1073                                 alignment = str2align(v->value);
1074                         else if (!strcasecmp(v->name, "greeting")) {
1075                                 if (x < ADSI_MAX_INTRO) {
1076                                         aligns[x] = alignment;
1077                                         ast_copy_string(intro[x], v->value, sizeof(intro[x]));
1078                                         x++;
1079                                 }
1080                         } else if (!strcasecmp(v->name, "maxretries")) {
1081                                 if (atoi(v->value) > 0)
1082                                         maxretries = atoi(v->value);
1083                         }
1084                 }
1085                 if (x)
1086                         total = x;
1087                 x = 0;
1088                 for (v = ast_variable_browse(conf, "speeddial"); v; v = v->next) {
1089                         char *stringp = v->value;
1090                         name = strsep(&stringp, ",");
1091                         sname = strsep(&stringp, ",");
1092                         if (!sname) 
1093                                 sname = name;
1094                         if (x < ADSI_MAX_SPEED_DIAL) {
1095                                 /* Up to 20 digits */
1096                                 ast_copy_string(speeddial[x][0], v->name, sizeof(speeddial[x][0]));
1097                                 strncpy(speeddial[x][1], name, 18);
1098                                 strncpy(speeddial[x][2], sname, 7);
1099                                 x++;
1100                         }
1101                 }
1102                 if (x)
1103                         speeds = x;
1104                 ast_config_destroy(conf);
1105         }
1106 }
1107
1108 static int reload(void)
1109 {
1110         adsi_load();
1111         return 0;
1112 }
1113
1114 static int load_module(void)
1115 {
1116         adsi_load();
1117         return 0;
1118 }
1119
1120 static int unload_module(void)
1121 {
1122         /* Can't unload this once we're loaded */
1123         return -1;
1124 }
1125
1126 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ADSI Resource",
1127                 .load = load_module,
1128                 .unload = unload_module,
1129                 .reload = reload,
1130                );