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.
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
34 <support_level>core</support_level>
39 ASTERISK_REGISTER_FILE()
44 #include "asterisk/ulaw.h"
45 #include "asterisk/alaw.h"
46 #include "asterisk/callerid.h"
47 #include "asterisk/fskmodem.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/module.h"
50 #include "asterisk/config.h"
51 #include "asterisk/file.h"
52 #include "asterisk/adsi.h"
53 #include "asterisk/format_cache.h"
55 #define DEFAULT_ADSI_MAX_RETRIES 3
57 #define ADSI_MAX_INTRO 20
58 #define ADSI_MAX_SPEED_DIAL 6
60 #define ADSI_FLAG_DATAMODE (1 << 8)
62 static int maxretries = DEFAULT_ADSI_MAX_RETRIES;
64 /* Asterisk ADSI button definitions */
65 #define ADSI_SPEED_DIAL 10 /* 10-15 are reserved for speed dial */
67 static char intro[ADSI_MAX_INTRO][20];
68 static int aligns[ADSI_MAX_INTRO];
70 #define SPEEDDIAL_MAX_LEN 20
71 static char speeddial[ADSI_MAX_SPEED_DIAL][3][SPEEDDIAL_MAX_LEN];
73 static int alignment = 0;
75 static int adsi_begin_download(struct ast_channel *chan, char *service, unsigned char *fdn, unsigned char *sec, int version);
76 static int adsi_end_download(struct ast_channel *chan);
77 static int adsi_channel_restore(struct ast_channel *chan);
78 static int adsi_print(struct ast_channel *chan, char **lines, int *align, int voice);
79 static int adsi_load_session(struct ast_channel *chan, unsigned char *app, int ver, int data);
80 static int adsi_unload_session(struct ast_channel *chan);
81 static int adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype);
82 static int adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait);
83 static int adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen);
84 static int adsi_connect_session(unsigned char *buf, unsigned char *fdn, int ver);
85 static int adsi_query_cpeid(unsigned char *buf);
86 static int adsi_query_cpeinfo(unsigned char *buf);
87 static int adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice);
88 static int adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice);
89 static int adsi_download_connect(unsigned char *buf, char *service, unsigned char *fdn, unsigned char *sec, int ver);
90 static int adsi_disconnect_session(unsigned char *buf);
91 static int adsi_download_disconnect(unsigned char *buf);
92 static int adsi_data_mode(unsigned char *buf);
93 static int adsi_clear_soft_keys(unsigned char *buf);
94 static int adsi_clear_screen(unsigned char *buf);
95 static int adsi_voice_mode(unsigned char *buf, int when);
96 static int adsi_available(struct ast_channel *chan);
97 static int adsi_display(unsigned char *buf, int page, int line, int just, int wrap, char *col1, char *col2);
98 static int adsi_set_line(unsigned char *buf, int page, int line);
99 static int adsi_load_soft_key(unsigned char *buf, int key, const char *llabel, const char *slabel, char *ret, int data);
100 static int adsi_set_keys(unsigned char *buf, unsigned char *keys);
101 static int adsi_input_control(unsigned char *buf, int page, int line, int display, int format, int just);
102 static int adsi_input_format(unsigned char *buf, int num, int dir, int wrap, char *format1, char *format2);
104 static int adsi_generate(unsigned char *buf, int msgtype, unsigned char *msg, int msglen, int msgnum, int last, struct ast_format *codec)
106 int sum, x, bytes = 0;
107 /* Initial carrier (imaginary) */
108 float cr = 1.0, ci = 0.0, scont = 0.0;
114 /* If first message, Send 150ms of MARK's */
116 for (x = 0; x < 150; x++) { /* was 150 */
121 /* Put message type */
125 /* Put message length (plus one for the message number) */
126 PUT_CLID(msglen + 1);
129 /* Put message number */
133 /* Put actual message */
134 for (x = 0; x < msglen; x++) {
139 /* Put 2's compliment of sum */
140 PUT_CLID(256-(sum & 0xff));
144 /* Put trailing marks */
145 for (x = 0; x < 50; x++) {
154 static int adsi_careful_send(struct ast_channel *chan, unsigned char *buf, int len, int *remain)
156 /* Sends carefully on a full duplex channel by using reading for
158 struct ast_frame *inf;
159 struct ast_frame outf = {
160 .frametype = AST_FRAME_VOICE,
161 .subclass.format = ast_format_ulaw,
166 if (remain && *remain) {
169 /* Send remainder if provided */
173 *remain = *remain - amt;
178 if (ast_write(chan, &outf)) {
179 ast_log(LOG_WARNING, "Failed to carefully write frame\n");
182 /* Update pointers and lengths */
189 /* If we don't get anything at all back in a second, forget
191 if (ast_waitfor(chan, 1000) < 1) {
195 if (!(inf = ast_read(chan))) {
199 /* Drop any frames that are not voice */
200 if (inf->frametype != AST_FRAME_VOICE) {
205 if (ast_format_cmp(inf->subclass.format, ast_format_ulaw) != AST_FORMAT_CMP_EQUAL) {
206 ast_log(LOG_WARNING, "Channel not in ulaw?\n");
210 /* Send no more than they sent us */
211 if (amt > inf->datalen) {
214 *remain = inf->datalen - amt;
218 if (ast_write(chan, &outf)) {
219 ast_log(LOG_WARNING, "Failed to carefully write frame\n");
223 /* Update pointers and lengths */
231 static int __adsi_transmit_messages(struct ast_channel *chan, unsigned char **msg, int *msglen, int *msgtype)
233 /* msglen must be no more than 256 bits, each */
234 unsigned char buf[24000 * 5];
235 int pos = 0, res, x, start = 0, retries = 0, waittime, rem = 0, def;
239 if (ast_channel_adsicpe(chan) == AST_ADSI_UNAVAILABLE) {
240 /* Don't bother if we know they don't support ADSI */
245 while (retries < maxretries) {
246 if (!(ast_channel_adsicpe(chan) & ADSI_FLAG_DATAMODE)) {
247 /* Generate CAS (no SAS) */
248 ast_gen_cas(buf, 0, 680, ast_format_ulaw);
251 if (adsi_careful_send(chan, buf, 680, NULL)) {
252 ast_log(LOG_WARNING, "Unable to send CAS\n");
255 /* Wait For DTMF result */
258 if (((res = ast_waitfor(chan, waittime)) < 1)) {
259 /* Didn't get back DTMF A in time */
260 ast_debug(1, "No ADSI CPE detected (%d)\n", res);
261 if (!ast_channel_adsicpe(chan)) {
262 ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
268 if (!(f = ast_read(chan))) {
269 ast_debug(1, "Hangup in ADSI\n");
272 if (f->frametype == AST_FRAME_DTMF) {
273 if (f->subclass.integer == 'A') {
274 /* Okay, this is an ADSI CPE. Note this for future reference, too */
275 if (!ast_channel_adsicpe(chan)) {
276 ast_channel_adsicpe_set(chan, AST_ADSI_AVAILABLE);
280 if (f->subclass.integer == 'D') {
281 ast_debug(1, "Off-hook capable CPE only, not ADSI\n");
283 ast_log(LOG_WARNING, "Unknown ADSI response '%c'\n", f->subclass.integer);
285 if (!ast_channel_adsicpe(chan)) {
286 ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
296 ast_debug(1, "ADSI Compatible CPE Detected\n");
298 ast_debug(1, "Already in data mode\n");
304 def= ast_channel_defer_dtmf(chan);
306 while ((x < 6) && msg[x]) {
307 if ((res = adsi_generate(buf + pos, msgtype[x], msg[x], msglen[x], x+1 - start, (x == 5) || !msg[x+1], ast_format_ulaw)) < 0) {
308 ast_log(LOG_WARNING, "Failed to generate ADSI message %d on channel %s\n", x + 1, ast_channel_name(chan));
311 ast_debug(1, "Message %d, of %d input bytes, %d output bytes\n", x + 1, msglen[x], res);
318 res = adsi_careful_send(chan, buf, pos, &rem);
320 ast_channel_undefer_dtmf(chan);
326 ast_debug(1, "Sent total spill of %d bytes\n", pos);
328 memset(ack, 0, sizeof(ack));
329 /* Get real result and check for hangup */
330 if ((res = ast_readstring(chan, ack, 2, 1000, 1000, "")) < 0) {
334 ast_debug(1, "Acked up to message %d\n", atoi(ack + 1)); start += atoi(ack + 1);
339 ast_debug(1, "Retransmitting (%d), from %d\n", retries, start + 1);
343 ast_log(LOG_WARNING, "Unexpected response to ack: %s (retry %d)\n", ack, retries);
346 if (retries >= maxretries) {
347 ast_log(LOG_WARNING, "Maximum ADSI Retries (%d) exceeded\n", maxretries);
354 static int adsi_begin_download(struct ast_channel *chan, char *service, unsigned char *fdn, unsigned char *sec, int version)
357 unsigned char buf[256];
360 /* Setup the resident soft key stuff, a piece at a time */
361 /* Upload what scripts we can for voicemail ahead of time */
362 bytes += adsi_download_connect(buf + bytes, service, fdn, sec, version);
363 if (adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0)) {
366 if (ast_readstring(chan, ack, 1, 10000, 10000, "")) {
372 ast_debug(1, "Download was denied by CPE\n");
376 static int adsi_end_download(struct ast_channel *chan)
379 unsigned char buf[256];
381 /* Setup the resident soft key stuff, a piece at a time */
382 /* Upload what scripts we can for voicemail ahead of time */
383 bytes += adsi_download_disconnect(buf + bytes);
384 if (adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DOWNLOAD, 0)) {
390 static int adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait)
392 unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL };
393 int msglens[5], msgtypes[5], newdatamode = (ast_channel_adsicpe(chan) & ADSI_FLAG_DATAMODE), res, x, waitforswitch = 0;
394 RAII_VAR(struct ast_format *, writeformat, NULL, ao2_cleanup);
395 RAII_VAR(struct ast_format *, readformat, NULL, ao2_cleanup);
397 for (x = 0; x < msglen; x += (msg[x+1]+2)) {
398 if (msg[x] == ADSI_SWITCH_TO_DATA) {
399 ast_debug(1, "Switch to data is sent!\n");
401 newdatamode = ADSI_FLAG_DATAMODE;
404 if (msg[x] == ADSI_SWITCH_TO_VOICE) {
405 ast_debug(1, "Switch to voice is sent!\n");
413 msgtypes[0] = msgtype;
416 ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen);
420 ast_stopstream(chan);
422 writeformat = ao2_bump(ast_channel_writeformat(chan));
423 readformat = ao2_bump(ast_channel_readformat(chan));
425 if (ast_set_write_format(chan, ast_format_ulaw)) {
426 ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
430 if (ast_set_read_format(chan, ast_format_ulaw)) {
431 ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
433 if (ast_set_write_format(chan, writeformat)) {
434 ast_log(LOG_WARNING, "Unable to restore write format to %s\n", ast_format_get_name(writeformat));
439 res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes);
442 ast_debug(1, "Wait for switch is '%d'\n", waitforswitch);
443 while (waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) {
445 ast_debug(1, "Waiting for 'B'...\n");
450 ast_channel_adsicpe_set(chan, (ast_channel_adsicpe(chan) & ~ADSI_FLAG_DATAMODE) | newdatamode);
454 ast_set_write_format(chan, writeformat);
457 ast_set_read_format(chan, readformat);
461 res = ast_safe_sleep(chan, 100 );
466 static int adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype)
468 return adsi_transmit_message_full(chan, msg, msglen, msgtype, 1);
471 static inline int ccopy(unsigned char *dst, const unsigned char *src, int max)
474 /* Carefully copy the requested data */
475 while ((x < max) && src[x] && (src[x] != 0xff)) {
482 static int adsi_load_soft_key(unsigned char *buf, int key, const char *llabel, const char *slabel, char *ret, int data)
486 /* Abort if invalid key specified */
487 if ((key < 2) || (key > 33)) {
491 buf[bytes++] = ADSI_LOAD_SOFTKEY;
492 /* Reserve for length */
497 /* Carefully copy long label */
498 bytes += ccopy(buf + bytes, (const unsigned char *)llabel, 18);
500 /* Place delimiter */
504 bytes += ccopy(buf + bytes, (const unsigned char *)slabel, 7);
507 /* If specified, copy return string */
509 /* Place delimiter */
512 buf[bytes++] = ADSI_SWITCH_TO_DATA2;
514 /* Carefully copy return string */
515 bytes += ccopy(buf + bytes, (const unsigned char *)ret, 20);
518 /* Replace parameter length */
523 static int adsi_connect_session(unsigned char *buf, unsigned char *fdn, int ver)
528 buf[bytes++] = ADSI_CONNECT_SESSION;
530 /* Reserve space for length */
534 for (x = 0; x < 4; x++) {
535 buf[bytes++] = fdn[x];
538 buf[bytes++] = ver & 0xff;
547 static int adsi_download_connect(unsigned char *buf, char *service, unsigned char *fdn, unsigned char *sec, int ver)
552 buf[bytes++] = ADSI_DOWNLOAD_CONNECT;
554 /* Reserve space for length */
558 bytes+= ccopy(buf + bytes, (unsigned char *)service, 18);
563 for (x = 0; x < 4; x++) {
564 buf[bytes++] = fdn[x];
567 for (x = 0; x < 4; x++) {
568 buf[bytes++] = sec[x];
571 buf[bytes++] = ver & 0xff;
579 static int adsi_disconnect_session(unsigned char *buf)
584 buf[bytes++] = ADSI_DISC_SESSION;
586 /* Reserve space for length */
594 static int adsi_query_cpeid(unsigned char *buf)
597 buf[bytes++] = ADSI_QUERY_CPEID;
598 /* Reserve space for length */
604 static int adsi_query_cpeinfo(unsigned char *buf)
607 buf[bytes++] = ADSI_QUERY_CONFIG;
608 /* Reserve space for length */
614 static int adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen)
616 int bytes = 0, res, gotstar = 0, pos = 0;
617 unsigned char current = 0;
619 memset(buf, 0, maxlen);
621 while (bytes <= maxlen) {
622 /* Wait up to a second for a digit */
623 if (!(res = ast_waitfordigit(chan, 1000))) {
630 /* Ignore anything other than a digit */
631 if ((res < '0') || (res > '9')) {
640 buf[bytes++] = (res << 4) | current;
651 static int adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice)
653 unsigned char buf[256] = "";
656 bytes += adsi_data_mode(buf);
657 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
660 bytes += adsi_query_cpeid(buf);
661 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
664 res = adsi_read_encoded_dtmf(chan, cpeid, 4);
666 ast_log(LOG_WARNING, "Got %d bytes back of encoded DTMF, expecting 4\n", res);
674 bytes += adsi_voice_mode(buf, 0);
675 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
676 /* Ignore the resulting DTMF B announcing it's in voice mode */
677 ast_waitfordigit(chan, 1000);
682 static int adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice)
684 unsigned char buf[256] = "";
687 bytes += adsi_data_mode(buf);
688 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
691 bytes += adsi_query_cpeinfo(buf);
692 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
695 if ((res = ast_readstring(chan, (char *) buf, 2, 1000, 500, "")) < 0) {
698 if (strlen((char *) buf) != 2) {
699 ast_log(LOG_WARNING, "Got %d bytes of width, expecting 2\n", res);
705 *width = atoi((char *) buf);
708 memset(buf, 0, sizeof(buf));
710 if ((res = ast_readstring(chan, (char *) buf, 2, 1000, 500, "")) < 0) {
713 if (strlen((char *) buf) != 2) {
714 ast_log(LOG_WARNING, "Got %d bytes of height, expecting 2\n", res);
720 *height = atoi((char *) buf);
724 memset(buf, 0, sizeof(buf));
726 if ((res = ast_readstring(chan, (char *) buf, 1, 1000, 500, "")) < 0) {
729 if (strlen((char *) buf) != 1) {
730 ast_log(LOG_WARNING, "Got %d bytes of buttons, expecting 1\n", res);
736 *buttons = atoi((char *) buf);
741 bytes += adsi_voice_mode(buf, 0);
742 adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
743 /* Ignore the resulting DTMF B announcing it's in voice mode */
744 ast_waitfordigit(chan, 1000);
749 static int adsi_data_mode(unsigned char *buf)
754 buf[bytes++] = ADSI_SWITCH_TO_DATA;
756 /* Reserve space for length */
764 static int adsi_clear_soft_keys(unsigned char *buf)
769 buf[bytes++] = ADSI_CLEAR_SOFTKEY;
771 /* Reserve space for length */
779 static int adsi_clear_screen(unsigned char *buf)
784 buf[bytes++] = ADSI_CLEAR_SCREEN;
786 /* Reserve space for length */
794 static int adsi_voice_mode(unsigned char *buf, int when)
799 buf[bytes++] = ADSI_SWITCH_TO_VOICE;
801 /* Reserve space for length */
804 buf[bytes++] = when & 0x7f;
811 static int adsi_available(struct ast_channel *chan)
813 int cpe = ast_channel_adsicpe(chan) & 0xff;
814 if ((cpe == AST_ADSI_AVAILABLE) ||
815 (cpe == AST_ADSI_UNKNOWN)) {
821 static int adsi_download_disconnect(unsigned char *buf)
826 buf[bytes++] = ADSI_DOWNLOAD_DISC;
828 /* Reserve space for length */
836 static int adsi_display(unsigned char *buf, int page, int line, int just, int wrap,
837 char *col1, char *col2)
841 /* Sanity check line number */
844 if (line > 4) return -1;
846 if (line > 33) return -1;
853 buf[bytes++] = ADSI_LOAD_VIRTUAL_DISP;
855 /* Reserve space for size */
858 /* Page and wrap indicator */
859 buf[bytes++] = ((page & 0x1) << 7) | ((wrap & 0x1) << 6) | (line & 0x3f);
862 buf[bytes++] = (just & 0x3) << 5;
864 /* Omit highlight mode definition */
868 bytes+= ccopy(buf + bytes, (unsigned char *)col1, 20);
873 /* Secondary column */
874 bytes += ccopy(buf + bytes, (unsigned char *)col2, 20);
883 static int adsi_input_control(unsigned char *buf, int page, int line, int display, int format, int just)
888 if (line > 4) return -1;
890 if (line > 33) return -1;
897 buf[bytes++] = ADSI_INPUT_CONTROL;
899 buf[bytes++] = ((page & 1) << 7) | (line & 0x3f);
900 buf[bytes++] = ((display & 1) << 7) | ((just & 0x3) << 4) | (format & 0x7);
906 static int adsi_input_format(unsigned char *buf, int num, int dir, int wrap, char *format1, char *format2)
910 if (ast_strlen_zero((char *) format1)) {
914 buf[bytes++] = ADSI_INPUT_FORMAT;
916 buf[bytes++] = ((dir & 1) << 7) | ((wrap & 1) << 6) | (num & 0x7);
917 bytes += ccopy(buf + bytes, (unsigned char *) format1, 20);
919 if (!ast_strlen_zero(format2)) {
920 bytes += ccopy(buf + bytes, (unsigned char *) format2, 20);
926 static int adsi_set_keys(unsigned char *buf, unsigned char *keys)
931 buf[bytes++] = ADSI_INIT_SOFTKEY_LINE;
934 /* Key definitions */
935 for (x = 0; x < 6; x++) {
936 buf[bytes++] = (keys[x] & 0x3f) ? keys[x] : (keys[x] | 0x1);
942 static int adsi_set_line(unsigned char *buf, int page, int line)
946 /* Sanity check line number */
949 if (line > 4) return -1;
951 if (line > 33) return -1;
958 buf[bytes++] = ADSI_LINE_CONTROL;
960 /* Reserve space for size */
964 buf[bytes++] = ((page & 0x1) << 7) | (line & 0x3f);
970 static int total = 0;
971 static int speeds = 0;
973 static int adsi_channel_restore(struct ast_channel *chan)
975 unsigned char dsp[256] = "", keyd[6] = "";
978 /* Start with initial display setup */
980 bytes += adsi_set_line(dsp + bytes, ADSI_INFO_PAGE, 1);
982 /* Prepare key setup messages */
985 for (x = 0; x < speeds; x++) {
986 keyd[x] = ADSI_SPEED_DIAL + x;
988 bytes += adsi_set_keys(dsp + bytes, keyd);
990 adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0);
995 static int adsi_print(struct ast_channel *chan, char **lines, int *alignments, int voice)
997 unsigned char buf[4096];
998 int bytes = 0, res, x;
1000 for (x = 0; lines[x]; x++) {
1001 bytes += adsi_display(buf + bytes, ADSI_INFO_PAGE, x+1, alignments[x], 0, lines[x], "");
1003 bytes += adsi_set_line(buf + bytes, ADSI_INFO_PAGE, 1);
1005 bytes += adsi_voice_mode(buf + bytes, 0);
1007 res = adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
1009 /* Ignore the resulting DTMF B announcing it's in voice mode */
1010 ast_waitfordigit(chan, 1000);
1015 static int adsi_load_session(struct ast_channel *chan, unsigned char *app, int ver, int data)
1017 unsigned char dsp[256] = "";
1021 /* Connect to session */
1022 bytes += adsi_connect_session(dsp + bytes, app, ver);
1025 bytes += adsi_data_mode(dsp + bytes);
1028 /* Prepare key setup messages */
1029 if (adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0)) {
1033 if ((res = ast_readstring(chan, resp, 1, 1200, 1200, "")) < 0) {
1037 ast_debug(1, "No response from CPE about version. Assuming not there.\n");
1040 if (!strcmp(resp, "B")) {
1041 ast_debug(1, "CPE has script '%s' version %d already loaded\n", app, ver);
1043 } else if (!strcmp(resp, "A")) {
1044 ast_debug(1, "CPE hasn't script '%s' version %d already loaded\n", app, ver);
1046 ast_log(LOG_WARNING, "Unexpected CPE response to script query: %s\n", resp);
1054 static int adsi_unload_session(struct ast_channel *chan)
1056 unsigned char dsp[256] = "";
1059 /* Connect to session */
1060 bytes += adsi_disconnect_session(dsp + bytes);
1061 bytes += adsi_voice_mode(dsp + bytes, 0);
1063 /* Prepare key setup messages */
1064 if (adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0)) {
1071 static int str2align(const char *s)
1073 if (!strncasecmp(s, "l", 1)) {
1074 return ADSI_JUST_LEFT;
1075 } else if (!strncasecmp(s, "r", 1)) {
1076 return ADSI_JUST_RIGHT;
1077 } else if (!strncasecmp(s, "i", 1)) {
1078 return ADSI_JUST_IND;
1080 return ADSI_JUST_CENT;
1084 static void init_state(void)
1088 for (x = 0; x < ADSI_MAX_INTRO; x++) {
1089 aligns[x] = ADSI_JUST_CENT;
1091 ast_copy_string(intro[0], "Welcome to the", sizeof(intro[0]));
1092 ast_copy_string(intro[1], "Asterisk", sizeof(intro[1]));
1093 ast_copy_string(intro[2], "Open Source PBX", sizeof(intro[2]));
1096 for (x = 3; x < ADSI_MAX_INTRO; x++) {
1099 memset(speeddial, 0, sizeof(speeddial));
1100 alignment = ADSI_JUST_CENT;
1103 static void adsi_load(int reload)
1106 struct ast_config *conf = NULL;
1107 struct ast_variable *v;
1108 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1112 conf = ast_config_load("adsi.conf", config_flags);
1113 if (conf == CONFIG_STATUS_FILEMISSING || conf == CONFIG_STATUS_FILEUNCHANGED || conf == CONFIG_STATUS_FILEINVALID) {
1116 for (v = ast_variable_browse(conf, "intro"); v; v = v->next) {
1117 if (!strcasecmp(v->name, "alignment")) {
1118 alignment = str2align(v->value);
1119 } else if (!strcasecmp(v->name, "greeting")) {
1120 if (x < ADSI_MAX_INTRO) {
1121 aligns[x] = alignment;
1122 ast_copy_string(intro[x], v->value, sizeof(intro[x]));
1125 } else if (!strcasecmp(v->name, "maxretries")) {
1126 if (atoi(v->value) > 0) {
1127 maxretries = atoi(v->value);
1136 for (v = ast_variable_browse(conf, "speeddial"); v; v = v->next) {
1137 char buf[3 * SPEEDDIAL_MAX_LEN];
1138 char *stringp = buf;
1139 ast_copy_string(buf, v->value, sizeof(buf));
1140 name = strsep(&stringp, ",");
1141 sname = strsep(&stringp, ",");
1145 if (x < ADSI_MAX_SPEED_DIAL) {
1146 ast_copy_string(speeddial[x][0], v->name, sizeof(speeddial[x][0]));
1147 ast_copy_string(speeddial[x][1], name, 18);
1148 ast_copy_string(speeddial[x][2], sname, 7);
1155 ast_config_destroy(conf);
1160 static int reload(void)
1166 static struct adsi_funcs res_adsi_funcs = {
1167 .version = AST_ADSI_VERSION,
1168 .begin_download = adsi_begin_download,
1169 .end_download = adsi_end_download,
1170 .channel_restore = adsi_channel_restore,
1171 .print = adsi_print,
1172 .load_session = adsi_load_session,
1173 .unload_session = adsi_unload_session,
1174 .transmit_message = adsi_transmit_message,
1175 .transmit_message_full = adsi_transmit_message_full,
1176 .read_encoded_dtmf = adsi_read_encoded_dtmf,
1177 .connect_session = adsi_connect_session,
1178 .query_cpeid = adsi_query_cpeid,
1179 .query_cpeinfo = adsi_query_cpeinfo,
1180 .get_cpeid = adsi_get_cpeid,
1181 .get_cpeinfo = adsi_get_cpeinfo,
1182 .download_connect = adsi_download_connect,
1183 .disconnect_session = adsi_disconnect_session,
1184 .download_disconnect = adsi_download_disconnect,
1185 .data_mode = adsi_data_mode,
1186 .clear_soft_keys = adsi_clear_soft_keys,
1187 .clear_screen = adsi_clear_screen,
1188 .voice_mode = adsi_voice_mode,
1189 .available = adsi_available,
1190 .display = adsi_display,
1191 .set_line = adsi_set_line,
1192 .load_soft_key = adsi_load_soft_key,
1193 .set_keys = adsi_set_keys,
1194 .input_control = adsi_input_control,
1195 .input_format = adsi_input_format,
1198 static int load_module(void)
1201 ast_adsi_install_funcs(&res_adsi_funcs);
1202 return AST_MODULE_LOAD_SUCCESS;
1205 static int unload_module(void)
1207 /* Can't unload this once we're loaded */
1208 ast_adsi_install_funcs(NULL);
1212 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ADSI Resource",
1213 .support_level = AST_MODULE_SUPPORT_CORE,
1214 .load = load_module,
1215 .unload = unload_module,
1217 .load_pri = AST_MODPRI_APP_DEPEND,