Merged revisions 114708 via svnmerge from
[asterisk-bristuff.git] / res / res_adsi.c
blob2df5324aadde2202535b993a8533cf1e1e82f052
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Includes code and algorithms from the Zapata library.
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.
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.
21 /*! \file
23 * \brief ADSI support
25 * \author Mark Spencer <markster@digium.com>
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
33 #include "asterisk.h"
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <time.h>
38 #include <math.h>
40 #include "asterisk/adsi.h"
41 #include "asterisk/ulaw.h"
42 #include "asterisk/alaw.h"
43 #include "asterisk/callerid.h"
44 #include "asterisk/fskmodem.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/module.h"
47 #include "asterisk/config.h"
48 #include "asterisk/file.h"
50 #define DEFAULT_ADSI_MAX_RETRIES 3
52 #define ADSI_MAX_INTRO 20
53 #define ADSI_MAX_SPEED_DIAL 6
55 #define ADSI_FLAG_DATAMODE (1 << 8)
57 static int maxretries = DEFAULT_ADSI_MAX_RETRIES;
59 /* Asterisk ADSI button definitions */
60 #define ADSI_SPEED_DIAL 10 /* 10-15 are reserved for speed dial */
62 static char intro[ADSI_MAX_INTRO][20];
63 static int aligns[ADSI_MAX_INTRO];
65 #define SPEEDDIAL_MAX_LEN 20
66 static char speeddial[ADSI_MAX_SPEED_DIAL][3][SPEEDDIAL_MAX_LEN];
68 static int alignment = 0;
70 static int adsi_generate(unsigned char *buf, int msgtype, unsigned char *msg, int msglen, int msgnum, int last, int codec)
72 int sum, x, bytes = 0;
73 /* Initial carrier (imaginary) */
74 float cr = 1.0, ci = 0.0, scont = 0.0;
76 if (msglen > 255)
77 msglen = 255;
79 /* If first message, Send 150ms of MARK's */
80 if (msgnum == 1) {
81 for (x = 0; x < 150; x++) /* was 150 */
82 PUT_CLID_MARKMS;
85 /* Put message type */
86 PUT_CLID(msgtype);
87 sum = msgtype;
89 /* Put message length (plus one for the message number) */
90 PUT_CLID(msglen + 1);
91 sum += msglen + 1;
93 /* Put message number */
94 PUT_CLID(msgnum);
95 sum += msgnum;
97 /* Put actual message */
98 for (x = 0; x < msglen; x++) {
99 PUT_CLID(msg[x]);
100 sum += msg[x];
103 /* Put 2's compliment of sum */
104 PUT_CLID(256-(sum & 0xff));
106 #if 0
107 if (last) {
108 /* Put trailing marks */
109 for (x = 0; x < 50; x++)
110 PUT_CLID_MARKMS;
112 #endif
113 return bytes;
117 static int adsi_careful_send(struct ast_channel *chan, unsigned char *buf, int len, int *remainder)
119 /* Sends carefully on a full duplex channel by using reading for
120 timing */
121 struct ast_frame *inf, outf;
122 int amt;
124 /* Zero out our outgoing frame */
125 memset(&outf, 0, sizeof(outf));
127 if (remainder && *remainder) {
128 amt = len;
130 /* Send remainder if provided */
131 if (amt > *remainder)
132 amt = *remainder;
133 else
134 *remainder = *remainder - amt;
135 outf.frametype = AST_FRAME_VOICE;
136 outf.subclass = AST_FORMAT_ULAW;
137 outf.data = buf;
138 outf.datalen = amt;
139 outf.samples = amt;
140 if (ast_write(chan, &outf)) {
141 ast_log(LOG_WARNING, "Failed to carefully write frame\n");
142 return -1;
144 /* Update pointers and lengths */
145 buf += amt;
146 len -= amt;
149 while(len) {
150 amt = len;
151 /* If we don't get anything at all back in a second, forget
152 about it */
153 if (ast_waitfor(chan, 1000) < 1)
154 return -1;
155 /* Detect hangup */
156 if (!(inf = ast_read(chan)))
157 return -1;
159 /* Drop any frames that are not voice */
160 if (inf->frametype != AST_FRAME_VOICE) {
161 ast_frfree(inf);
162 continue;
165 if (inf->subclass != AST_FORMAT_ULAW) {
166 ast_log(LOG_WARNING, "Channel not in ulaw?\n");
167 ast_frfree(inf);
168 return -1;
170 /* Send no more than they sent us */
171 if (amt > inf->datalen)
172 amt = inf->datalen;
173 else if (remainder)
174 *remainder = inf->datalen - amt;
175 outf.frametype = AST_FRAME_VOICE;
176 outf.subclass = AST_FORMAT_ULAW;
177 outf.data = buf;
178 outf.datalen = amt;
179 outf.samples = amt;
180 if (ast_write(chan, &outf)) {
181 ast_log(LOG_WARNING, "Failed to carefully write frame\n");
182 ast_frfree(inf);
183 return -1;
185 /* Update pointers and lengths */
186 buf += amt;
187 len -= amt;
188 ast_frfree(inf);
190 return 0;
193 static int __adsi_transmit_messages(struct ast_channel *chan, unsigned char **msg, int *msglen, int *msgtype)
195 /* msglen must be no more than 256 bits, each */
196 unsigned char buf[24000 * 5];
197 int pos = 0, res, x, start = 0, retries = 0, waittime, rem = 0, def;
198 char ack[3];
199 struct ast_frame *f;
201 if (chan->adsicpe == AST_ADSI_UNAVAILABLE) {
202 /* Don't bother if we know they don't support ADSI */
203 errno = ENOSYS;
204 return -1;
207 while(retries < maxretries) {
208 if (!(chan->adsicpe & ADSI_FLAG_DATAMODE)) {
209 /* Generate CAS (no SAS) */
210 ast_gen_cas(buf, 0, 680, AST_FORMAT_ULAW);
212 /* Send CAS */
213 if (adsi_careful_send(chan, buf, 680, NULL))
214 ast_log(LOG_WARNING, "Unable to send CAS\n");
216 /* Wait For DTMF result */
217 waittime = 500;
218 for(;;) {
219 if (((res = ast_waitfor(chan, waittime)) < 1)) {
220 /* Didn't get back DTMF A in time */
221 ast_debug(1, "No ADSI CPE detected (%d)\n", res);
222 if (!chan->adsicpe)
223 chan->adsicpe = AST_ADSI_UNAVAILABLE;
224 errno = ENOSYS;
225 return -1;
227 waittime = res;
228 if (!(f = ast_read(chan))) {
229 ast_debug(1, "Hangup in ADSI\n");
230 return -1;
232 if (f->frametype == AST_FRAME_DTMF) {
233 if (f->subclass == 'A') {
234 /* Okay, this is an ADSI CPE. Note this for future reference, too */
235 if (!chan->adsicpe)
236 chan->adsicpe = AST_ADSI_AVAILABLE;
237 break;
238 } else {
239 if (f->subclass == 'D')
240 ast_debug(1, "Off-hook capable CPE only, not ADSI\n");
241 else
242 ast_log(LOG_WARNING, "Unknown ADSI response '%c'\n", f->subclass);
243 if (!chan->adsicpe)
244 chan->adsicpe = AST_ADSI_UNAVAILABLE;
245 errno = ENOSYS;
246 ast_frfree(f);
247 return -1;
250 ast_frfree(f);
253 ast_debug(1, "ADSI Compatible CPE Detected\n");
254 } else {
255 ast_debug(1, "Already in data mode\n");
258 x = 0;
259 pos = 0;
260 #if 1
261 def= ast_channel_defer_dtmf(chan);
262 #endif
263 while ((x < 6) && msg[x]) {
264 if ((res = adsi_generate(buf + pos, msgtype[x], msg[x], msglen[x], x+1 - start, (x == 5) || !msg[x+1], AST_FORMAT_ULAW)) < 0) {
265 ast_log(LOG_WARNING, "Failed to generate ADSI message %d on channel %s\n", x + 1, chan->name);
266 return -1;
268 ast_debug(1, "Message %d, of %d input bytes, %d output bytes\n", x + 1, msglen[x], res);
269 pos += res;
270 x++;
274 rem = 0;
275 res = adsi_careful_send(chan, buf, pos, &rem);
276 if (!def)
277 ast_channel_undefer_dtmf(chan);
278 if (res)
279 return -1;
281 ast_debug(1, "Sent total spill of %d bytes\n", pos);
283 memset(ack, 0, sizeof(ack));
284 /* Get real result and check for hangup */
285 if ((res = ast_readstring(chan, ack, 2, 1000, 1000, "")) < 0)
286 return -1;
287 if (ack[0] == 'D') {
288 ast_debug(1, "Acked up to message %d\n", atoi(ack + 1)); start += atoi(ack + 1);
289 if (start >= x)
290 break;
291 else {
292 retries++;
293 ast_debug(1, "Retransmitting (%d), from %d\n", retries, start + 1);
295 } else {
296 retries++;
297 ast_log(LOG_WARNING, "Unexpected response to ack: %s (retry %d)\n", ack, retries);
300 if (retries >= maxretries) {
301 ast_log(LOG_WARNING, "Maximum ADSI Retries (%d) exceeded\n", maxretries);
302 errno = ETIMEDOUT;
303 return -1;
305 return 0;
309 static int _ast_adsi_begin_download(struct ast_channel *chan, char *service, unsigned char *fdn, unsigned char *sec, int version)
311 int bytes = 0;
312 unsigned char buf[256];
313 char ack[2];
315 /* Setup the resident soft key stuff, a piece at a time */
316 /* Upload what scripts we can for voicemail ahead of time */
317 bytes += ast_adsi_download_connect(buf + bytes, service, fdn, sec, version);
318 if (ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0))
319 return -1;
320 if (ast_readstring(chan, ack, 1, 10000, 10000, ""))
321 return -1;
322 if (ack[0] == 'B')
323 return 0;
324 ast_debug(1, "Download was denied by CPE\n");
325 return -1;
328 static int _ast_adsi_end_download(struct ast_channel *chan)
330 int bytes = 0;
331 unsigned char buf[256];
333 /* Setup the resident soft key stuff, a piece at a time */
334 /* Upload what scripts we can for voicemail ahead of time */
335 bytes += ast_adsi_download_disconnect(buf + bytes);
336 if (ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0))
337 return -1;
338 return 0;
341 static int _ast_adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait)
343 unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL };
344 int msglens[5], msgtypes[5], newdatamode = (chan->adsicpe & ADSI_FLAG_DATAMODE), res, x, writeformat = chan->writeformat, readformat = chan->readformat, waitforswitch = 0;
346 for (x = 0; x < msglen; x += (msg[x+1]+2)) {
347 if (msg[x] == ADSI_SWITCH_TO_DATA) {
348 ast_debug(1, "Switch to data is sent!\n");
349 waitforswitch++;
350 newdatamode = ADSI_FLAG_DATAMODE;
353 if (msg[x] == ADSI_SWITCH_TO_VOICE) {
354 ast_debug(1, "Switch to voice is sent!\n");
355 waitforswitch++;
356 newdatamode = 0;
359 msgs[0] = msg;
361 msglens[0] = msglen;
362 msgtypes[0] = msgtype;
364 if (msglen > 253) {
365 ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen);
366 return -1;
369 ast_stopstream(chan);
371 if (ast_set_write_format(chan, AST_FORMAT_ULAW)) {
372 ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
373 return -1;
376 if (ast_set_read_format(chan, AST_FORMAT_ULAW)) {
377 ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
378 if (writeformat) {
379 if (ast_set_write_format(chan, writeformat))
380 ast_log(LOG_WARNING, "Unable to restore write format to %d\n", writeformat);
382 return -1;
384 res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes);
386 if (dowait) {
387 ast_debug(1, "Wait for switch is '%d'\n", waitforswitch);
388 while (waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) {
389 res = 0;
390 ast_debug(1, "Waiting for 'B'...\n");
394 if (!res)
395 chan->adsicpe = (chan->adsicpe & ~ADSI_FLAG_DATAMODE) | newdatamode;
397 if (writeformat)
398 ast_set_write_format(chan, writeformat);
399 if (readformat)
400 ast_set_read_format(chan, readformat);
402 if (!res)
403 res = ast_safe_sleep(chan, 100 );
404 return res;
407 static int _ast_adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype)
409 return ast_adsi_transmit_message_full(chan, msg, msglen, msgtype, 1);
412 static inline int ccopy(unsigned char *dst, const unsigned char *src, int max)
414 int x = 0;
415 /* Carefully copy the requested data */
416 while ((x < max) && src[x] && (src[x] != 0xff)) {
417 dst[x] = src[x];
418 x++;
420 return x;
423 static int _ast_adsi_load_soft_key(unsigned char *buf, int key, const char *llabel, const char *slabel, char *ret, int data)
425 int bytes = 0;
427 /* Abort if invalid key specified */
428 if ((key < 2) || (key > 33))
429 return -1;
431 buf[bytes++] = ADSI_LOAD_SOFTKEY;
432 /* Reserve for length */
433 bytes++;
434 /* Which key */
435 buf[bytes++] = key;
437 /* Carefully copy long label */
438 bytes += ccopy(buf + bytes, (const unsigned char *)llabel, 18);
440 /* Place delimiter */
441 buf[bytes++] = 0xff;
443 /* Short label */
444 bytes += ccopy(buf + bytes, (const unsigned char *)slabel, 7);
447 /* If specified, copy return string */
448 if (ret) {
449 /* Place delimiter */
450 buf[bytes++] = 0xff;
451 if (data)
452 buf[bytes++] = ADSI_SWITCH_TO_DATA2;
453 /* Carefully copy return string */
454 bytes += ccopy(buf + bytes, (const unsigned char *)ret, 20);
457 /* Replace parameter length */
458 buf[1] = bytes - 2;
459 return bytes;
463 static int _ast_adsi_connect_session(unsigned char *buf, unsigned char *fdn, int ver)
465 int bytes = 0, x;
467 /* Message type */
468 buf[bytes++] = ADSI_CONNECT_SESSION;
470 /* Reserve space for length */
471 bytes++;
473 if (fdn) {
474 for (x = 0; x < 4; x++)
475 buf[bytes++] = fdn[x];
476 if (ver > -1)
477 buf[bytes++] = ver & 0xff;
480 buf[1] = bytes - 2;
481 return bytes;
485 static int _ast_adsi_download_connect(unsigned char *buf, char *service, unsigned char *fdn, unsigned char *sec, int ver)
487 int bytes = 0, x;
489 /* Message type */
490 buf[bytes++] = ADSI_DOWNLOAD_CONNECT;
492 /* Reserve space for length */
493 bytes++;
495 /* Primary column */
496 bytes+= ccopy(buf + bytes, (unsigned char *)service, 18);
498 /* Delimiter */
499 buf[bytes++] = 0xff;
501 for (x = 0; x < 4; x++)
502 buf[bytes++] = fdn[x];
504 for (x = 0; x < 4; x++)
505 buf[bytes++] = sec[x];
507 buf[bytes++] = ver & 0xff;
509 buf[1] = bytes - 2;
511 return bytes;
515 static int _ast_adsi_disconnect_session(unsigned char *buf)
517 int bytes = 0;
519 /* Message type */
520 buf[bytes++] = ADSI_DISC_SESSION;
522 /* Reserve space for length */
523 bytes++;
525 buf[1] = bytes - 2;
526 return bytes;
530 static int _ast_adsi_query_cpeid(unsigned char *buf)
532 int bytes = 0;
533 buf[bytes++] = ADSI_QUERY_CPEID;
534 /* Reserve space for length */
535 bytes++;
536 buf[1] = bytes - 2;
537 return bytes;
540 static int _ast_adsi_query_cpeinfo(unsigned char *buf)
542 int bytes = 0;
543 buf[bytes++] = ADSI_QUERY_CONFIG;
544 /* Reserve space for length */
545 bytes++;
546 buf[1] = bytes - 2;
547 return bytes;
550 static int _ast_adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen)
552 int bytes = 0, res, gotstar = 0, pos = 0;
553 unsigned char current = 0;
555 memset(buf, 0, sizeof(buf));
557 while(bytes <= maxlen) {
558 /* Wait up to a second for a digit */
559 if (!(res = ast_waitfordigit(chan, 1000)))
560 break;
561 if (res == '*') {
562 gotstar = 1;
563 continue;
565 /* Ignore anything other than a digit */
566 if ((res < '0') || (res > '9'))
567 continue;
568 res -= '0';
569 if (gotstar)
570 res += 9;
571 if (pos) {
572 pos = 0;
573 buf[bytes++] = (res << 4) | current;
574 } else {
575 pos = 1;
576 current = res;
578 gotstar = 0;
581 return bytes;
584 static int _ast_adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice)
586 unsigned char buf[256] = "";
587 int bytes = 0, res;
589 bytes += ast_adsi_data_mode(buf);
590 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
592 bytes = 0;
593 bytes += ast_adsi_query_cpeid(buf);
594 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
596 /* Get response */
597 res = ast_adsi_read_encoded_dtmf(chan, cpeid, 4);
598 if (res != 4) {
599 ast_log(LOG_WARNING, "Got %d bytes back of encoded DTMF, expecting 4\n", res);
600 res = 0;
601 } else {
602 res = 1;
605 if (voice) {
606 bytes = 0;
607 bytes += ast_adsi_voice_mode(buf, 0);
608 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
609 /* Ignore the resulting DTMF B announcing it's in voice mode */
610 ast_waitfordigit(chan, 1000);
612 return res;
615 static int _ast_adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice)
617 unsigned char buf[256] = "";
618 int bytes = 0, res;
620 bytes += ast_adsi_data_mode(buf);
621 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
623 bytes = 0;
624 bytes += ast_adsi_query_cpeinfo(buf);
625 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
627 /* Get width */
628 if ((res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "")) < 0)
629 return res;
630 if (strlen((char *)buf) != 2) {
631 ast_log(LOG_WARNING, "Got %d bytes of width, expecting 2\n", res);
632 res = 0;
633 } else {
634 res = 1;
636 if (width)
637 *width = atoi((char *)buf);
638 /* Get height */
639 memset(buf, 0, sizeof(buf));
640 if (res) {
641 if ((res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "")) < 0)
642 return res;
643 if (strlen((char *)buf) != 2) {
644 ast_log(LOG_WARNING, "Got %d bytes of height, expecting 2\n", res);
645 res = 0;
646 } else {
647 res = 1;
649 if (height)
650 *height= atoi((char *)buf);
652 /* Get buttons */
653 memset(buf, 0, sizeof(buf));
654 if (res) {
655 if ((res = ast_readstring(chan, (char *)buf, 1, 1000, 500, "")) < 0)
656 return res;
657 if (strlen((char *)buf) != 1) {
658 ast_log(LOG_WARNING, "Got %d bytes of buttons, expecting 1\n", res);
659 res = 0;
660 } else {
661 res = 1;
663 if (buttons)
664 *buttons = atoi((char *)buf);
666 if (voice) {
667 bytes = 0;
668 bytes += ast_adsi_voice_mode(buf, 0);
669 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
670 /* Ignore the resulting DTMF B announcing it's in voice mode */
671 ast_waitfordigit(chan, 1000);
673 return res;
676 static int _ast_adsi_data_mode(unsigned char *buf)
678 int bytes = 0;
680 /* Message type */
681 buf[bytes++] = ADSI_SWITCH_TO_DATA;
683 /* Reserve space for length */
684 bytes++;
686 buf[1] = bytes - 2;
687 return bytes;
691 static int _ast_adsi_clear_soft_keys(unsigned char *buf)
693 int bytes = 0;
695 /* Message type */
696 buf[bytes++] = ADSI_CLEAR_SOFTKEY;
698 /* Reserve space for length */
699 bytes++;
701 buf[1] = bytes - 2;
702 return bytes;
706 static int _ast_adsi_clear_screen(unsigned char *buf)
708 int bytes = 0;
710 /* Message type */
711 buf[bytes++] = ADSI_CLEAR_SCREEN;
713 /* Reserve space for length */
714 bytes++;
716 buf[1] = bytes - 2;
717 return bytes;
721 static int _ast_adsi_voice_mode(unsigned char *buf, int when)
723 int bytes = 0;
725 /* Message type */
726 buf[bytes++] = ADSI_SWITCH_TO_VOICE;
728 /* Reserve space for length */
729 bytes++;
731 buf[bytes++] = when & 0x7f;
733 buf[1] = bytes - 2;
734 return bytes;
738 static int _ast_adsi_available(struct ast_channel *chan)
740 int cpe = chan->adsicpe & 0xff;
741 if ((cpe == AST_ADSI_AVAILABLE) ||
742 (cpe == AST_ADSI_UNKNOWN))
743 return 1;
744 return 0;
747 static int _ast_adsi_download_disconnect(unsigned char *buf)
749 int bytes = 0;
751 /* Message type */
752 buf[bytes++] = ADSI_DOWNLOAD_DISC;
754 /* Reserve space for length */
755 bytes++;
757 buf[1] = bytes - 2;
758 return bytes;
762 static int _ast_adsi_display(unsigned char *buf, int page, int line, int just, int wrap,
763 char *col1, char *col2)
765 int bytes = 0;
767 /* Sanity check line number */
769 if (page) {
770 if (line > 4) return -1;
771 } else {
772 if (line > 33) return -1;
775 if (line < 1)
776 return -1;
777 /* Parameter type */
778 buf[bytes++] = ADSI_LOAD_VIRTUAL_DISP;
780 /* Reserve space for size */
781 bytes++;
783 /* Page and wrap indicator */
784 buf[bytes++] = ((page & 0x1) << 7) | ((wrap & 0x1) << 6) | (line & 0x3f);
786 /* Justification */
787 buf[bytes++] = (just & 0x3) << 5;
789 /* Omit highlight mode definition */
790 buf[bytes++] = 0xff;
792 /* Primary column */
793 bytes+= ccopy(buf + bytes, (unsigned char *)col1, 20);
795 /* Delimiter */
796 buf[bytes++] = 0xff;
798 /* Secondary column */
799 bytes += ccopy(buf + bytes, (unsigned char *)col2, 20);
801 /* Update length */
802 buf[1] = bytes - 2;
804 return bytes;
808 static int _ast_adsi_input_control(unsigned char *buf, int page, int line, int display, int format, int just)
810 int bytes = 0;
812 if (page) {
813 if (line > 4) return -1;
814 } else {
815 if (line > 33) return -1;
818 if (line < 1)
819 return -1;
821 buf[bytes++] = ADSI_INPUT_CONTROL;
822 bytes++;
823 buf[bytes++] = ((page & 1) << 7) | (line & 0x3f);
824 buf[bytes++] = ((display & 1) << 7) | ((just & 0x3) << 4) | (format & 0x7);
826 buf[1] = bytes - 2;
827 return bytes;
831 static int _ast_adsi_input_format(unsigned char *buf, int num, int dir, int wrap, char *format1, char *format2)
833 int bytes = 0;
835 if (!strlen((char *)format1))
836 return -1;
838 buf[bytes++] = ADSI_INPUT_FORMAT;
839 bytes++;
840 buf[bytes++] = ((dir & 1) << 7) | ((wrap & 1) << 6) | (num & 0x7);
841 bytes += ccopy(buf + bytes, (unsigned char *)format1, 20);
842 buf[bytes++] = 0xff;
843 if (format2 && strlen((char *)format2)) {
844 bytes += ccopy(buf + bytes, (unsigned char *)format2, 20);
846 buf[1] = bytes - 2;
847 return bytes;
850 static int _ast_adsi_set_keys(unsigned char *buf, unsigned char *keys)
852 int bytes = 0, x;
854 /* Message type */
855 buf[bytes++] = ADSI_INIT_SOFTKEY_LINE;
856 /* Space for size */
857 bytes++;
858 /* Key definitions */
859 for (x = 0; x < 6; x++)
860 buf[bytes++] = (keys[x] & 0x3f) ? keys[x] : (keys[x] | 0x1);
861 buf[1] = bytes - 2;
862 return bytes;
865 static int _ast_adsi_set_line(unsigned char *buf, int page, int line)
867 int bytes = 0;
869 /* Sanity check line number */
871 if (page) {
872 if (line > 4) return -1;
873 } else {
874 if (line > 33) return -1;
877 if (line < 1)
878 return -1;
879 /* Parameter type */
880 buf[bytes++] = ADSI_LINE_CONTROL;
882 /* Reserve space for size */
883 bytes++;
885 /* Page and line */
886 buf[bytes++] = ((page & 0x1) << 7) | (line & 0x3f);
888 buf[1] = bytes - 2;
889 return bytes;
893 static int total = 0;
894 static int speeds = 0;
896 static int _ast_adsi_channel_restore(struct ast_channel *chan)
898 unsigned char dsp[256] = "", keyd[6] = "";
899 int bytes, x;
901 /* Start with initial display setup */
902 bytes = 0;
903 bytes += ast_adsi_set_line(dsp + bytes, ADSI_INFO_PAGE, 1);
905 /* Prepare key setup messages */
907 if (speeds) {
908 for (x = 0; x < speeds; x++)
909 keyd[x] = ADSI_SPEED_DIAL + x;
910 bytes += ast_adsi_set_keys(dsp + bytes, keyd);
912 ast_adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0);
913 return 0;
917 static int _ast_adsi_print(struct ast_channel *chan, char **lines, int *aligns, int voice)
919 unsigned char buf[4096];
920 int bytes = 0, res, x;
922 for(x = 0; lines[x]; x++)
923 bytes += ast_adsi_display(buf + bytes, ADSI_INFO_PAGE, x+1, aligns[x], 0, lines[x], "");
924 bytes += ast_adsi_set_line(buf + bytes, ADSI_INFO_PAGE, 1);
925 if (voice)
926 bytes += ast_adsi_voice_mode(buf + bytes, 0);
927 res = ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
928 if (voice)
929 /* Ignore the resulting DTMF B announcing it's in voice mode */
930 ast_waitfordigit(chan, 1000);
931 return res;
934 static int _ast_adsi_load_session(struct ast_channel *chan, unsigned char *app, int ver, int data)
936 unsigned char dsp[256] = "";
937 int bytes = 0, res;
938 char resp[2];
940 /* Connect to session */
941 bytes += ast_adsi_connect_session(dsp + bytes, app, ver);
943 if (data)
944 bytes += ast_adsi_data_mode(dsp + bytes);
946 /* Prepare key setup messages */
947 if (ast_adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0))
948 return -1;
949 if (app) {
950 if ((res = ast_readstring(chan, resp, 1, 1200, 1200, "")) < 0)
951 return -1;
952 if (res) {
953 ast_debug(1, "No response from CPE about version. Assuming not there.\n");
954 return 0;
956 if (!strcmp(resp, "B")) {
957 ast_debug(1, "CPE has script '%s' version %d already loaded\n", app, ver);
958 return 1;
959 } else if (!strcmp(resp, "A")) {
960 ast_debug(1, "CPE hasn't script '%s' version %d already loaded\n", app, ver);
961 } else {
962 ast_log(LOG_WARNING, "Unexpected CPE response to script query: %s\n", resp);
964 } else
965 return 1;
966 return 0;
970 static int _ast_adsi_unload_session(struct ast_channel *chan)
972 unsigned char dsp[256] = "";
973 int bytes = 0;
975 /* Connect to session */
976 bytes += ast_adsi_disconnect_session(dsp + bytes);
977 bytes += ast_adsi_voice_mode(dsp + bytes, 0);
979 /* Prepare key setup messages */
980 if (ast_adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0))
981 return -1;
983 return 0;
986 static int str2align(const char *s)
988 if (!strncasecmp(s, "l", 1))
989 return ADSI_JUST_LEFT;
990 else if (!strncasecmp(s, "r", 1))
991 return ADSI_JUST_RIGHT;
992 else if (!strncasecmp(s, "i", 1))
993 return ADSI_JUST_IND;
994 else
995 return ADSI_JUST_CENT;
998 static void init_state(void)
1000 int x;
1002 for (x = 0; x < ADSI_MAX_INTRO; x++)
1003 aligns[x] = ADSI_JUST_CENT;
1004 ast_copy_string(intro[0], "Welcome to the", sizeof(intro[0]));
1005 ast_copy_string(intro[1], "Asterisk", sizeof(intro[1]));
1006 ast_copy_string(intro[2], "Open Source PBX", sizeof(intro[2]));
1007 total = 3;
1008 speeds = 0;
1009 for (x = 3; x < ADSI_MAX_INTRO; x++)
1010 intro[x][0] = '\0';
1011 memset(speeddial, 0, sizeof(speeddial));
1012 alignment = ADSI_JUST_CENT;
1015 static void adsi_load(int reload)
1017 int x = 0;
1018 struct ast_config *conf = NULL;
1019 struct ast_variable *v;
1020 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1021 char *name, *sname;
1022 init_state();
1024 if (!(conf = ast_config_load("adsi.conf", config_flags)))
1025 return;
1026 else if (conf == CONFIG_STATUS_FILEUNCHANGED)
1027 return;
1028 for (v = ast_variable_browse(conf, "intro"); v; v = v->next) {
1029 if (!strcasecmp(v->name, "alignment"))
1030 alignment = str2align(v->value);
1031 else if (!strcasecmp(v->name, "greeting")) {
1032 if (x < ADSI_MAX_INTRO) {
1033 aligns[x] = alignment;
1034 ast_copy_string(intro[x], v->value, sizeof(intro[x]));
1035 x++;
1037 } else if (!strcasecmp(v->name, "maxretries")) {
1038 if (atoi(v->value) > 0)
1039 maxretries = atoi(v->value);
1042 if (x)
1043 total = x;
1045 x = 0;
1046 for (v = ast_variable_browse(conf, "speeddial"); v; v = v->next) {
1047 char buf[3 * SPEEDDIAL_MAX_LEN];
1048 char *stringp = buf;
1049 ast_copy_string(buf, v->value, sizeof(buf));
1050 name = strsep(&stringp, ",");
1051 sname = strsep(&stringp, ",");
1052 if (!sname)
1053 sname = name;
1054 if (x < ADSI_MAX_SPEED_DIAL) {
1055 ast_copy_string(speeddial[x][0], v->name, sizeof(speeddial[x][0]));
1056 ast_copy_string(speeddial[x][1], name, 18);
1057 ast_copy_string(speeddial[x][2], sname, 7);
1058 x++;
1061 if (x)
1062 speeds = x;
1063 ast_config_destroy(conf);
1065 return;
1068 static int reload(void)
1070 adsi_load(1);
1071 return 0;
1074 static int load_module(void)
1076 adsi_load(0);
1078 ast_adsi_begin_download = _ast_adsi_begin_download;
1079 ast_adsi_end_download = _ast_adsi_end_download;
1080 ast_adsi_channel_restore = _ast_adsi_channel_restore;
1081 ast_adsi_print = _ast_adsi_print;
1082 ast_adsi_load_session = _ast_adsi_load_session;
1083 ast_adsi_unload_session = _ast_adsi_unload_session;
1084 ast_adsi_transmit_message = _ast_adsi_transmit_message;
1085 ast_adsi_transmit_message_full = _ast_adsi_transmit_message_full;
1086 ast_adsi_read_encoded_dtmf = _ast_adsi_read_encoded_dtmf;
1087 ast_adsi_connect_session = _ast_adsi_connect_session;
1088 ast_adsi_query_cpeid = _ast_adsi_query_cpeid;
1089 ast_adsi_query_cpeinfo = _ast_adsi_query_cpeinfo;
1090 ast_adsi_get_cpeid = _ast_adsi_get_cpeid;
1091 ast_adsi_get_cpeinfo = _ast_adsi_get_cpeinfo;
1092 ast_adsi_download_connect = _ast_adsi_download_connect;
1093 ast_adsi_disconnect_session = _ast_adsi_disconnect_session;
1094 ast_adsi_download_disconnect = _ast_adsi_download_disconnect;
1095 ast_adsi_data_mode = _ast_adsi_data_mode;
1096 ast_adsi_clear_soft_keys = _ast_adsi_clear_soft_keys;
1097 ast_adsi_clear_screen = _ast_adsi_clear_screen;
1098 ast_adsi_voice_mode = _ast_adsi_voice_mode;
1099 ast_adsi_available = _ast_adsi_available;
1100 ast_adsi_display = _ast_adsi_display;
1101 ast_adsi_set_line = _ast_adsi_set_line;
1102 ast_adsi_load_soft_key = _ast_adsi_load_soft_key;
1103 ast_adsi_set_keys = _ast_adsi_set_keys;
1104 ast_adsi_input_control = _ast_adsi_input_control;
1105 ast_adsi_input_format = _ast_adsi_input_format;
1107 return AST_MODULE_LOAD_SUCCESS;
1110 static int unload_module(void)
1112 /* Can't unload this once we're loaded */
1113 return -1;
1116 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ADSI Resource",
1117 .load = load_module,
1118 .unload = unload_module,
1119 .reload = reload,