Let's also include aclocal.m4
[asterisk-bristuff.git] / res / res_adsi.c
blob38648a8d40547d7aaf4b281058a306ed5fb18865
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 <string.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <math.h>
43 #include <errno.h>
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"
56 #define DEFAULT_ADSI_MAX_RETRIES 3
58 #define ADSI_MAX_INTRO 20
59 #define ADSI_MAX_SPEED_DIAL 6
61 #define ADSI_FLAG_DATAMODE (1 << 8)
63 static int maxretries = DEFAULT_ADSI_MAX_RETRIES;
65 /* Asterisk ADSI button definitions */
66 #define ADSI_SPEED_DIAL 10 /* 10-15 are reserved for speed dial */
68 static char intro[ADSI_MAX_INTRO][20];
69 static int aligns[ADSI_MAX_INTRO];
71 static char speeddial[ADSI_MAX_SPEED_DIAL][3][20];
73 static int alignment = 0;
75 static int adsi_generate(unsigned char *buf, int msgtype, unsigned char *msg, int msglen, int msgnum, int last, int codec)
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;
85 if (msglen > 255)
86 msglen = 255;
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;
93 /* Put message type */
94 PUT_CLID(msgtype);
95 sum = msgtype;
97 /* Put message length (plus one for the message number) */
98 PUT_CLID(msglen + 1);
99 sum += msglen + 1;
101 /* Put message number */
102 PUT_CLID(msgnum);
103 sum += msgnum;
105 /* Put actual message */
106 for (x=0;x<msglen;x++) {
107 PUT_CLID(msg[x]);
108 sum += msg[x];
111 /* Put 2's compliment of sum */
112 PUT_CLID(256-(sum & 0xff));
114 #if 0
115 if (last) {
116 /* Put trailing marks */
117 for (x=0;x<50;x++)
118 PUT_CLID_MARKMS;
120 #endif
121 return bytes;
125 static int adsi_careful_send(struct ast_channel *chan, unsigned char *buf, int len, int *remainder)
127 /* Sends carefully on a full duplex channel by using reading for
128 timing */
129 struct ast_frame *inf, outf;
130 int amt;
132 /* Zero out our outgoing frame */
133 memset(&outf, 0, sizeof(outf));
135 if (remainder && *remainder) {
136 amt = len;
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;
152 /* Update pointers and lengths */
153 buf += amt;
154 len -= amt;
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 ast_frfree(inf);
172 return -1;
174 /* Send no more than they sent us */
175 if (amt > inf->datalen)
176 amt = inf->datalen;
177 else if (remainder)
178 *remainder = inf->datalen - amt;
179 outf.frametype = AST_FRAME_VOICE;
180 outf.subclass = AST_FORMAT_ULAW;
181 outf.data = buf;
182 outf.datalen = amt;
183 outf.samples = amt;
184 if (ast_write(chan, &outf)) {
185 ast_log(LOG_WARNING, "Failed to carefully write frame\n");
186 ast_frfree(inf);
187 return -1;
189 /* Update pointers and lengths */
190 buf += amt;
191 len -= amt;
193 ast_frfree(inf);
195 return 0;
198 static int __adsi_transmit_messages(struct ast_channel *chan, unsigned char **msg, int *msglen, int *msgtype)
200 /* msglen must be no more than 256 bits, each */
201 unsigned char buf[24000 * 5];
202 int pos = 0, res;
203 int x;
204 int start=0;
205 int retries = 0;
207 char ack[3];
209 /* Wait up to 500 ms for initial ACK */
210 int waittime;
211 struct ast_frame *f;
212 int rem = 0;
213 int def;
215 if (chan->adsicpe == AST_ADSI_UNAVAILABLE) {
216 /* Don't bother if we know they don't support ADSI */
217 errno = ENOSYS;
218 return -1;
221 while(retries < maxretries) {
222 if (!(chan->adsicpe & ADSI_FLAG_DATAMODE)) {
223 /* Generate CAS (no SAS) */
224 ast_gen_cas(buf, 0, 680, AST_FORMAT_ULAW);
226 /* Send CAS */
227 if (adsi_careful_send(chan, buf, 680, NULL)) {
228 ast_log(LOG_WARNING, "Unable to send CAS\n");
230 /* Wait For DTMF result */
231 waittime = 500;
232 for(;;) {
233 if (((res = ast_waitfor(chan, waittime)) < 1)) {
234 /* Didn't get back DTMF A in time */
235 ast_log(LOG_DEBUG, "No ADSI CPE detected (%d)\n", res);
236 if (!chan->adsicpe)
237 chan->adsicpe = AST_ADSI_UNAVAILABLE;
238 errno = ENOSYS;
239 return -1;
241 waittime = res;
242 f = ast_read(chan);
243 if (!f) {
244 ast_log(LOG_DEBUG, "Hangup in ADSI\n");
245 return -1;
247 if (f->frametype == AST_FRAME_DTMF) {
248 if (f->subclass == 'A') {
249 /* Okay, this is an ADSI CPE. Note this for future reference, too */
250 if (!chan->adsicpe)
251 chan->adsicpe = AST_ADSI_AVAILABLE;
252 break;
253 } else {
254 if (f->subclass == 'D') {
255 ast_log(LOG_DEBUG, "Off-hook capable CPE only, not ADSI\n");
256 } else
257 ast_log(LOG_WARNING, "Unknown ADSI response '%c'\n", f->subclass);
258 if (!chan->adsicpe)
259 chan->adsicpe = AST_ADSI_UNAVAILABLE;
260 errno = ENOSYS;
261 ast_frfree(f);
262 return -1;
265 ast_frfree(f);
268 ast_log(LOG_DEBUG, "ADSI Compatible CPE Detected\n");
269 } else
270 ast_log(LOG_DEBUG, "Already in data mode\n");
272 x = 0;
273 pos = 0;
274 #if 1
275 def= ast_channel_defer_dtmf(chan);
276 #endif
277 while((x < 6) && msg[x]) {
278 res = adsi_generate(buf + pos, msgtype[x], msg[x], msglen[x], x+1 - start, (x == 5) || !msg[x+1], AST_FORMAT_ULAW);
279 if (res < 0) {
280 ast_log(LOG_WARNING, "Failed to generate ADSI message %d on channel %s\n", x + 1, chan->name);
281 return -1;
283 ast_log(LOG_DEBUG, "Message %d, of %d input bytes, %d output bytes\n",
284 x + 1, msglen[x], res);
285 pos += res;
286 x++;
290 rem = 0;
291 res = adsi_careful_send(chan, buf, pos, &rem);
292 if (!def)
293 ast_channel_undefer_dtmf(chan);
294 if (res)
295 return -1;
297 ast_log(LOG_DEBUG, "Sent total spill of %d bytes\n", pos);
299 memset(ack, 0, sizeof(ack));
300 /* Get real result */
301 res = ast_readstring(chan, ack, 2, 1000, 1000, "");
302 /* Check for hangup */
303 if (res < 0)
304 return -1;
305 if (ack[0] == 'D') {
306 ast_log(LOG_DEBUG, "Acked up to message %d\n", atoi(ack + 1));
307 start += atoi(ack + 1);
308 if (start >= x)
309 break;
310 else {
311 retries++;
312 ast_log(LOG_DEBUG, "Retransmitting (%d), from %d\n", retries, start + 1);
314 } else {
315 retries++;
316 ast_log(LOG_WARNING, "Unexpected response to ack: %s (retry %d)\n", ack, retries);
319 if (retries >= maxretries) {
320 ast_log(LOG_WARNING, "Maximum ADSI Retries (%d) exceeded\n", maxretries);
321 errno = ETIMEDOUT;
322 return -1;
324 return 0;
328 int ast_adsi_begin_download(struct ast_channel *chan, char *service, unsigned char *fdn, unsigned char *sec, int version)
330 int bytes;
331 unsigned char buf[256];
332 char ack[2];
333 bytes = 0;
334 /* Setup the resident soft key stuff, a piece at a time */
335 /* Upload what scripts we can for voicemail ahead of time */
336 bytes += ast_adsi_download_connect(buf + bytes, service, fdn, sec, version);
337 if (ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0))
338 return -1;
339 if (ast_readstring(chan, ack, 1, 10000, 10000, ""))
340 return -1;
341 if (ack[0] == 'B')
342 return 0;
343 ast_log(LOG_DEBUG, "Download was denied by CPE\n");
344 return -1;
347 int ast_adsi_end_download(struct ast_channel *chan)
349 int bytes;
350 unsigned char buf[256];
351 bytes = 0;
352 /* Setup the resident soft key stuff, a piece at a time */
353 /* Upload what scripts we can for voicemail ahead of time */
354 bytes += ast_adsi_download_disconnect(buf + bytes);
355 if (ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0))
356 return -1;
357 return 0;
360 int ast_adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait)
362 unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL };
363 int msglens[5];
364 int msgtypes[5];
365 int newdatamode;
366 int res;
367 int x;
368 int writeformat, readformat;
369 int waitforswitch = 0;
371 writeformat = chan->writeformat;
372 readformat = chan->readformat;
374 newdatamode = chan->adsicpe & ADSI_FLAG_DATAMODE;
376 for (x=0;x<msglen;x+=(msg[x+1]+2)) {
377 if (msg[x] == ADSI_SWITCH_TO_DATA) {
378 ast_log(LOG_DEBUG, "Switch to data is sent!\n");
379 waitforswitch++;
380 newdatamode = ADSI_FLAG_DATAMODE;
383 if (msg[x] == ADSI_SWITCH_TO_VOICE) {
384 ast_log(LOG_DEBUG, "Switch to voice is sent!\n");
385 waitforswitch++;
386 newdatamode = 0;
389 msgs[0] = msg;
391 msglens[0] = msglen;
392 msgtypes[0] = msgtype;
394 if (msglen > 253) {
395 ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen);
396 return -1;
399 ast_stopstream(chan);
401 if (ast_set_write_format(chan, AST_FORMAT_ULAW)) {
402 ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
403 return -1;
406 if (ast_set_read_format(chan, AST_FORMAT_ULAW)) {
407 ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
408 if (writeformat) {
409 if (ast_set_write_format(chan, writeformat))
410 ast_log(LOG_WARNING, "Unable to restore write format to %d\n", writeformat);
412 return -1;
414 res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes);
416 if (dowait) {
417 ast_log(LOG_DEBUG, "Wait for switch is '%d'\n", waitforswitch);
418 while(waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) { res = 0; ast_log(LOG_DEBUG, "Waiting for 'B'...\n"); }
421 if (!res)
422 chan->adsicpe = (chan->adsicpe & ~ADSI_FLAG_DATAMODE) | newdatamode;
424 if (writeformat)
425 ast_set_write_format(chan, writeformat);
426 if (readformat)
427 ast_set_read_format(chan, readformat);
429 if (!res)
430 res = ast_safe_sleep(chan, 100 );
431 return res;
434 int ast_adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype)
436 return ast_adsi_transmit_message_full(chan, msg, msglen, msgtype, 1);
439 static inline int ccopy(unsigned char *dst, const unsigned char *src, int max)
441 int x=0;
442 /* Carefully copy the requested data */
443 while ((x < max) && src[x] && (src[x] != 0xff)) {
444 dst[x] = src[x];
445 x++;
447 return x;
450 int ast_adsi_load_soft_key(unsigned char *buf, int key, const char *llabel, const char *slabel, const char *ret, int data)
452 int bytes=0;
454 /* Abort if invalid key specified */
455 if ((key < 2) || (key > 33))
456 return -1;
457 buf[bytes++] = ADSI_LOAD_SOFTKEY;
458 /* Reserve for length */
459 bytes++;
460 /* Which key */
461 buf[bytes++] = key;
463 /* Carefully copy long label */
464 bytes += ccopy(buf + bytes, (const unsigned char *)llabel, 18);
466 /* Place delimiter */
467 buf[bytes++] = 0xff;
469 /* Short label */
470 bytes += ccopy(buf + bytes, (const unsigned char *)slabel, 7);
473 /* If specified, copy return string */
474 if (ret) {
475 /* Place delimiter */
476 buf[bytes++] = 0xff;
477 if (data)
478 buf[bytes++] = ADSI_SWITCH_TO_DATA2;
479 /* Carefully copy return string */
480 bytes += ccopy(buf + bytes, (const unsigned char *)ret, 20);
483 /* Replace parameter length */
484 buf[1] = bytes - 2;
485 return bytes;
489 int ast_adsi_connect_session(unsigned char *buf, unsigned char *fdn, int ver)
491 int bytes=0;
492 int x;
494 /* Message type */
495 buf[bytes++] = ADSI_CONNECT_SESSION;
497 /* Reserve space for length */
498 bytes++;
500 if (fdn) {
501 for (x=0;x<4;x++)
502 buf[bytes++] = fdn[x];
503 if (ver > -1)
504 buf[bytes++] = ver & 0xff;
507 buf[1] = bytes - 2;
508 return bytes;
512 int ast_adsi_download_connect(unsigned char *buf, char *service, unsigned char *fdn, unsigned char *sec, int ver)
514 int bytes=0;
515 int x;
517 /* Message type */
518 buf[bytes++] = ADSI_DOWNLOAD_CONNECT;
520 /* Reserve space for length */
521 bytes++;
523 /* Primary column */
524 bytes+= ccopy(buf + bytes, (unsigned char *)service, 18);
526 /* Delimiter */
527 buf[bytes++] = 0xff;
529 for (x=0;x<4;x++) {
530 buf[bytes++] = fdn[x];
532 for (x=0;x<4;x++)
533 buf[bytes++] = sec[x];
534 buf[bytes++] = ver & 0xff;
536 buf[1] = bytes - 2;
538 return bytes;
542 int ast_adsi_disconnect_session(unsigned char *buf)
544 int bytes=0;
546 /* Message type */
547 buf[bytes++] = ADSI_DISC_SESSION;
549 /* Reserve space for length */
550 bytes++;
552 buf[1] = bytes - 2;
553 return bytes;
557 int ast_adsi_query_cpeid(unsigned char *buf)
559 int bytes = 0;
560 buf[bytes++] = ADSI_QUERY_CPEID;
561 /* Reserve space for length */
562 bytes++;
563 buf[1] = bytes - 2;
564 return bytes;
567 int ast_adsi_query_cpeinfo(unsigned char *buf)
569 int bytes = 0;
570 buf[bytes++] = ADSI_QUERY_CONFIG;
571 /* Reserve space for length */
572 bytes++;
573 buf[1] = bytes - 2;
574 return bytes;
577 int ast_adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen)
579 int bytes = 0;
580 int res;
581 unsigned char current = 0;
582 int gotstar = 0;
583 int pos = 0;
584 memset(buf, 0, sizeof(buf));
585 while(bytes <= maxlen) {
586 /* Wait up to a second for a digit */
587 res = ast_waitfordigit(chan, 1000);
588 if (!res)
589 break;
590 if (res == '*') {
591 gotstar = 1;
592 continue;
594 /* Ignore anything other than a digit */
595 if ((res < '0') || (res > '9'))
596 continue;
597 res -= '0';
598 if (gotstar)
599 res += 9;
600 if (pos) {
601 pos = 0;
602 buf[bytes++] = (res << 4) | current;
603 } else {
604 pos = 1;
605 current = res;
607 gotstar = 0;
609 return bytes;
612 int ast_adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice)
614 unsigned char buf[256];
615 int bytes = 0;
616 int res;
617 bytes += ast_adsi_data_mode(buf);
618 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
620 bytes = 0;
621 bytes += ast_adsi_query_cpeid(buf);
622 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
624 /* Get response */
625 memset(buf, 0, sizeof(buf));
626 res = ast_adsi_read_encoded_dtmf(chan, cpeid, 4);
627 if (res != 4) {
628 ast_log(LOG_WARNING, "Got %d bytes back of encoded DTMF, expecting 4\n", res);
629 res = 0;
630 } else {
631 res = 1;
634 if (voice) {
635 bytes = 0;
636 bytes += ast_adsi_voice_mode(buf, 0);
637 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
638 /* Ignore the resulting DTMF B announcing it's in voice mode */
639 ast_waitfordigit(chan, 1000);
641 return res;
644 int ast_adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice)
646 unsigned char buf[256];
647 int bytes = 0;
648 int res;
649 bytes += ast_adsi_data_mode(buf);
650 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
652 bytes = 0;
653 bytes += ast_adsi_query_cpeinfo(buf);
654 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
656 /* Get width */
657 memset(buf, 0, sizeof(buf));
658 res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "");
659 if (res < 0)
660 return res;
661 if (strlen((char *)buf) != 2) {
662 ast_log(LOG_WARNING, "Got %d bytes of width, expecting 2\n", res);
663 res = 0;
664 } else {
665 res = 1;
667 if (width)
668 *width = atoi((char *)buf);
669 /* Get height */
670 memset(buf, 0, sizeof(buf));
671 if (res) {
672 res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "");
673 if (res < 0)
674 return res;
675 if (strlen((char *)buf) != 2) {
676 ast_log(LOG_WARNING, "Got %d bytes of height, expecting 2\n", res);
677 res = 0;
678 } else {
679 res = 1;
681 if (height)
682 *height= atoi((char *)buf);
684 /* Get buttons */
685 memset(buf, 0, sizeof(buf));
686 if (res) {
687 res = ast_readstring(chan, (char *)buf, 1, 1000, 500, "");
688 if (res < 0)
689 return res;
690 if (strlen((char *)buf) != 1) {
691 ast_log(LOG_WARNING, "Got %d bytes of buttons, expecting 1\n", res);
692 res = 0;
693 } else {
694 res = 1;
696 if (buttons)
697 *buttons = atoi((char *)buf);
699 if (voice) {
700 bytes = 0;
701 bytes += ast_adsi_voice_mode(buf, 0);
702 ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
703 /* Ignore the resulting DTMF B announcing it's in voice mode */
704 ast_waitfordigit(chan, 1000);
706 return res;
709 int ast_adsi_data_mode(unsigned char *buf)
711 int bytes=0;
713 /* Message type */
714 buf[bytes++] = ADSI_SWITCH_TO_DATA;
716 /* Reserve space for length */
717 bytes++;
719 buf[1] = bytes - 2;
720 return bytes;
724 int ast_adsi_clear_soft_keys(unsigned char *buf)
726 int bytes=0;
728 /* Message type */
729 buf[bytes++] = ADSI_CLEAR_SOFTKEY;
731 /* Reserve space for length */
732 bytes++;
734 buf[1] = bytes - 2;
735 return bytes;
739 int ast_adsi_clear_screen(unsigned char *buf)
741 int bytes=0;
743 /* Message type */
744 buf[bytes++] = ADSI_CLEAR_SCREEN;
746 /* Reserve space for length */
747 bytes++;
749 buf[1] = bytes - 2;
750 return bytes;
754 int ast_adsi_voice_mode(unsigned char *buf, int when)
756 int bytes=0;
758 /* Message type */
759 buf[bytes++] = ADSI_SWITCH_TO_VOICE;
761 /* Reserve space for length */
762 bytes++;
764 buf[bytes++] = when & 0x7f;
766 buf[1] = bytes - 2;
767 return bytes;
771 int ast_adsi_available(struct ast_channel *chan)
773 int cpe = chan->adsicpe & 0xff;
774 if ((cpe == AST_ADSI_AVAILABLE) ||
775 (cpe == AST_ADSI_UNKNOWN))
776 return 1;
777 return 0;
780 int ast_adsi_download_disconnect(unsigned char *buf)
782 int bytes=0;
784 /* Message type */
785 buf[bytes++] = ADSI_DOWNLOAD_DISC;
787 /* Reserve space for length */
788 bytes++;
790 buf[1] = bytes - 2;
791 return bytes;
795 int ast_adsi_display(unsigned char *buf, int page, int line, int just, int wrap,
796 char *col1, char *col2)
798 int bytes=0;
800 /* Sanity check line number */
802 if (page) {
803 if (line > 4) return -1;
804 } else {
805 if (line > 33) return -1;
808 if (line < 1)
809 return -1;
810 /* Parameter type */
811 buf[bytes++] = ADSI_LOAD_VIRTUAL_DISP;
813 /* Reserve space for size */
814 bytes++;
816 /* Page and wrap indicator */
817 buf[bytes++] = ((page & 0x1) << 7) | ((wrap & 0x1) << 6) | (line & 0x3f);
819 /* Justification */
820 buf[bytes++] = (just & 0x3) << 5;
822 /* Omit highlight mode definition */
823 buf[bytes++] = 0xff;
825 /* Primary column */
826 bytes+= ccopy(buf + bytes, (unsigned char *)col1, 20);
828 /* Delimiter */
829 buf[bytes++] = 0xff;
831 /* Secondary column */
832 bytes += ccopy(buf + bytes, (unsigned char *)col2, 20);
834 /* Update length */
835 buf[1] = bytes - 2;
837 return bytes;
841 int ast_adsi_input_control(unsigned char *buf, int page, int line, int display, int format, int just)
843 int bytes=0;
845 if (page) {
846 if (line > 4) return -1;
847 } else {
848 if (line > 33) return -1;
851 if (line < 1)
852 return -1;
854 buf[bytes++] = ADSI_INPUT_CONTROL;
855 bytes++;
856 buf[bytes++] = ((page & 1) << 7) | (line & 0x3f);
857 buf[bytes++] = ((display & 1) << 7) | ((just & 0x3) << 4) | (format & 0x7);
859 buf[1] = bytes - 2;
860 return bytes;
864 int ast_adsi_input_format(unsigned char *buf, int num, int dir, int wrap, char *format1, char *format2)
866 int bytes = 0;
868 if (!strlen((char *)format1))
869 return -1;
871 buf[bytes++] = ADSI_INPUT_FORMAT;
872 bytes++;
873 buf[bytes++] = ((dir & 1) << 7) | ((wrap & 1) << 6) | (num & 0x7);
874 bytes += ccopy(buf + bytes, (unsigned char *)format1, 20);
875 buf[bytes++] = 0xff;
876 if (format2 && strlen((char *)format2)) {
877 bytes += ccopy(buf + bytes, (unsigned char *)format2, 20);
879 buf[1] = bytes - 2;
880 return bytes;
883 int ast_adsi_set_keys(unsigned char *buf, unsigned char *keys)
885 int bytes=0;
886 int x;
887 /* Message type */
888 buf[bytes++] = ADSI_INIT_SOFTKEY_LINE;
889 /* Space for size */
890 bytes++;
891 /* Key definitions */
892 for (x=0;x<6;x++)
893 buf[bytes++] = (keys[x] & 0x3f) ? keys[x] : (keys[x] | 0x1);
894 buf[1] = bytes - 2;
895 return bytes;
898 int ast_adsi_set_line(unsigned char *buf, int page, int line)
900 int bytes=0;
902 /* Sanity check line number */
904 if (page) {
905 if (line > 4) return -1;
906 } else {
907 if (line > 33) return -1;
910 if (line < 1)
911 return -1;
912 /* Parameter type */
913 buf[bytes++] = ADSI_LINE_CONTROL;
915 /* Reserve space for size */
916 bytes++;
918 /* Page and line */
919 buf[bytes++] = ((page & 0x1) << 7) | (line & 0x3f);
921 buf[1] = bytes - 2;
922 return bytes;
926 static int total = 0;
927 static int speeds = 0;
929 int ast_adsi_channel_restore(struct ast_channel *chan)
931 unsigned char dsp[256];
932 int bytes;
933 int x;
934 unsigned char keyd[6];
936 memset(dsp, 0, sizeof(dsp));
938 /* Start with initial display setup */
939 bytes = 0;
940 bytes += ast_adsi_set_line(dsp + bytes, ADSI_INFO_PAGE, 1);
942 /* Prepare key setup messages */
944 if (speeds) {
945 memset(keyd, 0, sizeof(keyd));
946 for (x=0;x<speeds;x++) {
947 keyd[x] = ADSI_SPEED_DIAL + x;
949 bytes += ast_adsi_set_keys(dsp + bytes, keyd);
951 ast_adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0);
952 return 0;
956 int ast_adsi_print(struct ast_channel *chan, char **lines, int *aligns, int voice)
958 unsigned char buf[4096];
959 int bytes=0;
960 int res;
961 int x;
962 for(x=0;lines[x];x++)
963 bytes += ast_adsi_display(buf + bytes, ADSI_INFO_PAGE, x+1, aligns[x], 0, lines[x], "");
964 bytes += ast_adsi_set_line(buf + bytes, ADSI_INFO_PAGE, 1);
965 if (voice) {
966 bytes += ast_adsi_voice_mode(buf + bytes, 0);
968 res = ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
969 if (voice) {
970 /* Ignore the resulting DTMF B announcing it's in voice mode */
971 ast_waitfordigit(chan, 1000);
973 return res;
976 int ast_adsi_load_session(struct ast_channel *chan, unsigned char *app, int ver, int data)
978 unsigned char dsp[256];
979 int bytes;
980 int res;
981 char resp[2];
983 memset(dsp, 0, sizeof(dsp));
985 /* Connect to session */
986 bytes = 0;
987 bytes += ast_adsi_connect_session(dsp + bytes, app, ver);
989 if (data)
990 bytes += ast_adsi_data_mode(dsp + bytes);
992 /* Prepare key setup messages */
993 if (ast_adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0))
994 return -1;
995 if (app) {
996 res = ast_readstring(chan, resp, 1, 1200, 1200, "");
997 if (res < 0)
998 return -1;
999 if (res) {
1000 ast_log(LOG_DEBUG, "No response from CPE about version. Assuming not there.\n");
1001 return 0;
1003 if (!strcmp(resp, "B")) {
1004 ast_log(LOG_DEBUG, "CPE has script '%s' version %d already loaded\n", app, ver);
1005 return 1;
1006 } else if (!strcmp(resp, "A")) {
1007 ast_log(LOG_DEBUG, "CPE hasn't script '%s' version %d already loaded\n", app, ver);
1008 } else {
1009 ast_log(LOG_WARNING, "Unexpected CPE response to script query: %s\n", resp);
1011 } else
1012 return 1;
1013 return 0;
1017 int ast_adsi_unload_session(struct ast_channel *chan)
1019 unsigned char dsp[256];
1020 int bytes;
1022 memset(dsp, 0, sizeof(dsp));
1024 /* Connect to session */
1025 bytes = 0;
1026 bytes += ast_adsi_disconnect_session(dsp + bytes);
1027 bytes += ast_adsi_voice_mode(dsp + bytes, 0);
1029 /* Prepare key setup messages */
1030 if (ast_adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0))
1031 return -1;
1032 return 0;
1035 static int str2align(char *s)
1037 if (!strncasecmp(s, "l", 1))
1038 return ADSI_JUST_LEFT;
1039 else if (!strncasecmp(s, "r", 1))
1040 return ADSI_JUST_RIGHT;
1041 else if (!strncasecmp(s, "i", 1))
1042 return ADSI_JUST_IND;
1043 else
1044 return ADSI_JUST_CENT;
1047 static void init_state(void)
1049 int x;
1051 for (x=0;x<ADSI_MAX_INTRO;x++)
1052 aligns[x] = ADSI_JUST_CENT;
1053 ast_copy_string(intro[0], "Welcome to the", sizeof(intro[0]));
1054 ast_copy_string(intro[1], "Asterisk", sizeof(intro[1]));
1055 ast_copy_string(intro[2], "Open Source PBX", sizeof(intro[2]));
1056 total = 3;
1057 speeds = 0;
1058 for (x=3;x<ADSI_MAX_INTRO;x++)
1059 intro[x][0] = '\0';
1060 memset(speeddial, 0, sizeof(speeddial));
1061 alignment = ADSI_JUST_CENT;
1064 static void adsi_load(void)
1066 int x;
1067 struct ast_config *conf;
1068 struct ast_variable *v;
1069 char *name, *sname;
1070 init_state();
1071 conf = ast_config_load("adsi.conf");
1072 if (conf) {
1073 x=0;
1074 for (v = ast_variable_browse(conf, "intro"); v; v = v->next) {
1075 if (!strcasecmp(v->name, "alignment"))
1076 alignment = str2align(v->value);
1077 else if (!strcasecmp(v->name, "greeting")) {
1078 if (x < ADSI_MAX_INTRO) {
1079 aligns[x] = alignment;
1080 ast_copy_string(intro[x], v->value, sizeof(intro[x]));
1081 x++;
1083 } else if (!strcasecmp(v->name, "maxretries")) {
1084 if (atoi(v->value) > 0)
1085 maxretries = atoi(v->value);
1088 if (x)
1089 total = x;
1090 x = 0;
1091 for (v = ast_variable_browse(conf, "speeddial"); v; v = v->next) {
1092 char *stringp = v->value;
1093 name = strsep(&stringp, ",");
1094 sname = strsep(&stringp, ",");
1095 if (!sname)
1096 sname = name;
1097 if (x < ADSI_MAX_SPEED_DIAL) {
1098 ast_copy_string(speeddial[x][0], v->name, sizeof(speeddial[x][0]));
1099 ast_copy_string(speeddial[x][1], name, 18);
1100 ast_copy_string(speeddial[x][2], sname, 7);
1101 x++;
1104 if (x)
1105 speeds = x;
1106 ast_config_destroy(conf);
1110 static int reload(void)
1112 adsi_load();
1113 return 0;
1116 static int load_module(void)
1118 adsi_load();
1119 return 0;
1122 static int unload_module(void)
1124 /* Can't unload this once we're loaded */
1125 return -1;
1128 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ADSI Resource",
1129 .load = load_module,
1130 .unload = unload_module,
1131 .reload = reload,