Asterisk - The Open Source Telephony Project  21.4.1
res_adsi.c
Go to the documentation of this file.
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 /*** MODULEINFO
34  <support_level>deprecated</support_level>
35  ***/
36 
37 #include "asterisk.h"
38 
39 #include <time.h>
40 #include <math.h>
41 
42 #include "asterisk/ulaw.h"
43 #include "asterisk/alaw.h"
44 #include "asterisk/callerid.h"
45 #include "asterisk/fskmodem.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/module.h"
48 #include "asterisk/config.h"
49 #include "asterisk/file.h"
50 #include "asterisk/adsi.h"
51 #include "asterisk/format_cache.h"
52 
53 #define DEFAULT_ADSI_MAX_RETRIES 3
54 
55 #define ADSI_MAX_INTRO 20
56 #define ADSI_MAX_SPEED_DIAL 6
57 
58 #define ADSI_FLAG_DATAMODE (1 << 8)
59 
60 static int maxretries = DEFAULT_ADSI_MAX_RETRIES;
61 
62 /* Asterisk ADSI button definitions */
63 #define ADSI_SPEED_DIAL 10 /* 10-15 are reserved for speed dial */
64 
65 static char intro[ADSI_MAX_INTRO][20];
66 static int aligns[ADSI_MAX_INTRO];
67 
68 #define SPEEDDIAL_MAX_LEN 20
69 static char speeddial[ADSI_MAX_SPEED_DIAL][3][SPEEDDIAL_MAX_LEN];
70 
71 static int alignment = 0;
72 
73 static int adsi_begin_download(struct ast_channel *chan, char *service, unsigned char *fdn, unsigned char *sec, int version);
74 static int adsi_end_download(struct ast_channel *chan);
75 static int adsi_channel_restore(struct ast_channel *chan);
76 static int adsi_print(struct ast_channel *chan, char **lines, int *align, int voice);
77 static int adsi_load_session(struct ast_channel *chan, unsigned char *app, int ver, int data);
78 static int adsi_unload_session(struct ast_channel *chan);
79 static int adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype);
80 static int adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait);
81 static int adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen);
82 static int adsi_connect_session(unsigned char *buf, unsigned char *fdn, int ver);
83 static int adsi_query_cpeid(unsigned char *buf);
84 static int adsi_query_cpeinfo(unsigned char *buf);
85 static int adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice);
86 static int adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice);
87 static int adsi_download_connect(unsigned char *buf, char *service, unsigned char *fdn, unsigned char *sec, int ver);
88 static int adsi_disconnect_session(unsigned char *buf);
89 static int adsi_download_disconnect(unsigned char *buf);
90 static int adsi_data_mode(unsigned char *buf);
91 static int adsi_clear_soft_keys(unsigned char *buf);
92 static int adsi_clear_screen(unsigned char *buf);
93 static int adsi_voice_mode(unsigned char *buf, int when);
94 static int adsi_available(struct ast_channel *chan);
95 static int adsi_display(unsigned char *buf, int page, int line, int just, int wrap, char *col1, char *col2);
96 static int adsi_set_line(unsigned char *buf, int page, int line);
97 static int adsi_load_soft_key(unsigned char *buf, int key, const char *llabel, const char *slabel, char *ret, int data);
98 static int adsi_set_keys(unsigned char *buf, unsigned char *keys);
99 static int adsi_input_control(unsigned char *buf, int page, int line, int display, int format, int just);
100 static int adsi_input_format(unsigned char *buf, int num, int dir, int wrap, char *format1, char *format2);
101 
102 static int adsi_generate(unsigned char *buf, int msgtype, unsigned char *msg, int msglen, int msgnum, int last, struct ast_format *codec)
103 {
104  int sum, x, bytes = 0;
105  /* Initial carrier (imaginary) */
106  float cr = 1.0, ci = 0.0, scont = 0.0;
107 
108  if (msglen > 255) {
109  msglen = 255;
110  }
111 
112  /* If first message, Send 150ms of MARK's */
113  if (msgnum == 1) {
114  for (x = 0; x < 150; x++) { /* was 150 */
115  PUT_CLID_MARKMS;
116  }
117  }
118 
119  /* Put message type */
120  PUT_CLID(msgtype);
121  sum = msgtype;
122 
123  /* Put message length (plus one for the message number) */
124  PUT_CLID(msglen + 1);
125  sum += msglen + 1;
126 
127  /* Put message number */
128  PUT_CLID(msgnum);
129  sum += msgnum;
130 
131  /* Put actual message */
132  for (x = 0; x < msglen; x++) {
133  PUT_CLID(msg[x]);
134  sum += msg[x];
135  }
136 
137  /* Put 2's compliment of sum */
138  PUT_CLID(256-(sum & 0xff));
139 
140 #if 0
141  if (last) {
142  /* Put trailing marks */
143  for (x = 0; x < 50; x++) {
144  PUT_CLID_MARKMS;
145  }
146  }
147 #endif
148  return bytes;
149 
150 }
151 
152 static int adsi_careful_send(struct ast_channel *chan, unsigned char *buf, int len, int *remain)
153 {
154  /* Sends carefully on a full duplex channel by using reading for
155  timing */
156  struct ast_frame *inf;
157  struct ast_frame outf = {
159  .subclass.format = ast_format_ulaw,
160  };
161  int amt;
162 
163  if (remain && *remain) {
164  amt = len;
165 
166  /* Send remainder if provided */
167  if (amt > *remain) {
168  amt = *remain;
169  } else {
170  *remain = *remain - amt;
171  }
172 
173  outf.data.ptr = buf;
174  outf.datalen = amt;
175  outf.samples = amt;
176  if (ast_write(chan, &outf)) {
177  ast_log(LOG_WARNING, "Failed to carefully write frame\n");
178  return -1;
179  }
180  /* Update pointers and lengths */
181  buf += amt;
182  len -= amt;
183  }
184 
185  while (len) {
186  amt = len;
187  /* If we don't get anything at all back in a second, forget
188  about it */
189  if (ast_waitfor(chan, 1000) < 1) {
190  return -1;
191  }
192  /* Detect hangup */
193  if (!(inf = ast_read(chan))) {
194  return -1;
195  }
196 
197  /* Drop any frames that are not voice */
198  if (inf->frametype != AST_FRAME_VOICE) {
199  ast_frfree(inf);
200  continue;
201  }
202 
204  ast_log(LOG_WARNING, "Channel not in ulaw?\n");
205  ast_frfree(inf);
206  return -1;
207  }
208  /* Send no more than they sent us */
209  if (amt > inf->datalen) {
210  amt = inf->datalen;
211  } else if (remain) {
212  *remain = inf->datalen - amt;
213  }
214  outf.data.ptr = buf;
215  outf.datalen = amt;
216  outf.samples = amt;
217  if (ast_write(chan, &outf)) {
218  ast_log(LOG_WARNING, "Failed to carefully write frame\n");
219  ast_frfree(inf);
220  return -1;
221  }
222  /* Update pointers and lengths */
223  buf += amt;
224  len -= amt;
225  ast_frfree(inf);
226  }
227  return 0;
228 }
229 
230 static int __adsi_transmit_messages(struct ast_channel *chan, unsigned char **msg, int *msglen, int *msgtype)
231 {
232  /* msglen must be no more than 256 bits, each */
233  unsigned char buf[24000 * 5];
234  int pos = 0, res, x, start = 0, retries = 0, waittime, rem = 0, def;
235  char ack[3];
236  struct ast_frame *f;
237 
238  if (ast_channel_adsicpe(chan) == AST_ADSI_UNAVAILABLE) {
239  /* Don't bother if we know they don't support ADSI */
240  ast_log(LOG_WARNING, "ADSI is not supported for %s\n", ast_channel_name(chan));
241  errno = ENOSYS;
242  return -1;
243  }
244 
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);
249 
250  /* Send CAS */
251  if (adsi_careful_send(chan, buf, 680, NULL)) {
252  ast_log(LOG_WARNING, "Unable to send CAS\n");
253  }
254 
255  /* Wait For DTMF result */
256  waittime = 500;
257  for (;;) {
258  if (((res = ast_waitfor(chan, waittime)) < 1)) {
259  /* Didn't get back DTMF A in time */
260  ast_verb(4, "No ADSI CPE detected (%d)\n", res);
261  if (!ast_channel_adsicpe(chan)) {
262  ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
263  }
264  errno = ENOSYS;
265  return -1;
266  }
267  waittime = res;
268  if (!(f = ast_read(chan))) {
269  ast_debug(1, "Hangup in ADSI\n");
270  return -1;
271  }
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);
277  }
278  break;
279  } else {
280  if (f->subclass.integer == 'D') {
281  ast_debug(1, "Off-hook capable CPE only, not ADSI\n");
282  } else {
283  ast_log(LOG_WARNING, "Unknown ADSI response '%c'\n", f->subclass.integer);
284  }
285  if (!ast_channel_adsicpe(chan)) {
286  ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
287  }
288  errno = ENOSYS;
289  ast_frfree(f);
290  return -1;
291  }
292  }
293  ast_frfree(f);
294  }
295 
296  ast_verb(4, "ADSI Compatible CPE Detected\n");
297  } else {
298  ast_debug(1, "Already in data mode\n");
299  }
300 
301  x = 0;
302  pos = 0;
303 #if 1
304  def= ast_channel_defer_dtmf(chan);
305 #endif
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));
309  return -1;
310  }
311  ast_debug(1, "Message %d, of %d input bytes, %d output bytes\n", x + 1, msglen[x], res);
312  pos += res;
313  x++;
314  }
315 
316 
317  rem = 0;
318  res = adsi_careful_send(chan, buf, pos, &rem);
319  if (!def) {
321  }
322  if (res) {
323  return -1;
324  }
325 
326  ast_debug(1, "Sent total spill of %d bytes\n", pos);
327 
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) {
331  return -1;
332  }
333  if (ack[0] == 'D') {
334  ast_debug(1, "Acked up to message %d\n", atoi(ack + 1)); start += atoi(ack + 1);
335  if (start >= x) {
336  break;
337  } else {
338  retries++;
339  ast_debug(1, "Retransmitting (%d), from %d\n", retries, start + 1);
340  }
341  } else {
342  retries++;
343  ast_log(LOG_WARNING, "Unexpected response to ack: %s (retry %d)\n", ack, retries);
344  }
345  }
346  if (retries >= maxretries) {
347  ast_log(LOG_WARNING, "Maximum ADSI Retries (%d) exceeded\n", maxretries);
348  errno = ETIMEDOUT;
349  return -1;
350  }
351  return 0;
352 }
353 
354 static int adsi_begin_download(struct ast_channel *chan, char *service, unsigned char *fdn, unsigned char *sec, int version)
355 {
356  int bytes = 0;
357  unsigned char buf[256];
358  char ack[2];
359 
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)) {
364  return -1;
365  }
366  if (ast_readstring(chan, ack, 1, 10000, 10000, "")) {
367  return -1;
368  }
369  if (ack[0] == 'B') {
370  return 0;
371  }
372  ast_debug(1, "Download was denied by CPE\n");
373  return -1;
374 }
375 
376 static int adsi_end_download(struct ast_channel *chan)
377 {
378  int bytes = 0;
379  unsigned char buf[256];
380 
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)) {
385  return -1;
386  }
387  return 0;
388 }
389 
390 static int adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait)
391 {
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);
396 
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");
400  waitforswitch++;
401  newdatamode = ADSI_FLAG_DATAMODE;
402  }
403 
404  if (msg[x] == ADSI_SWITCH_TO_VOICE) {
405  ast_debug(1, "Switch to voice is sent!\n");
406  waitforswitch++;
407  newdatamode = 0;
408  }
409  }
410  msgs[0] = msg;
411 
412  msglens[0] = msglen;
413  msgtypes[0] = msgtype;
414 
415  if (msglen > 253) {
416  ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen);
417  return -1;
418  }
419 
420  ast_stopstream(chan);
421 
422  writeformat = ao2_bump(ast_channel_writeformat(chan));
423  readformat = ao2_bump(ast_channel_readformat(chan));
424 
426  ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
427  return -1;
428  }
429 
431  ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
432  if (writeformat) {
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));
435  }
436  }
437  return -1;
438  }
439  res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes);
440 
441  if (dowait) {
442  ast_debug(1, "Wait for switch is '%d'\n", waitforswitch);
443  while (waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) {
444  res = 0;
445  ast_debug(1, "Waiting for 'B'...\n");
446  }
447  }
448 
449  if (!res) {
450  ast_channel_adsicpe_set(chan, (ast_channel_adsicpe(chan) & ~ADSI_FLAG_DATAMODE) | newdatamode);
451  }
452 
453  if (writeformat) {
454  ast_set_write_format(chan, writeformat);
455  }
456  if (readformat) {
457  ast_set_read_format(chan, readformat);
458  }
459 
460  if (!res) {
461  res = ast_safe_sleep(chan, 100 );
462  }
463  return res;
464 }
465 
466 static int adsi_transmit_message(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype)
467 {
468  return adsi_transmit_message_full(chan, msg, msglen, msgtype, 1);
469 }
470 
471 static inline int ccopy(unsigned char *dst, const unsigned char *src, int max)
472 {
473  int x = 0;
474  /* Carefully copy the requested data */
475  while ((x < max) && src[x] && (src[x] != 0xff)) {
476  dst[x] = src[x];
477  x++;
478  }
479  return x;
480 }
481 
482 static int adsi_load_soft_key(unsigned char *buf, int key, const char *llabel, const char *slabel, char *ret, int data)
483 {
484  int bytes = 0;
485 
486  /* Abort if invalid key specified */
487  if ((key < 2) || (key > 33)) {
488  return -1;
489  }
490 
491  buf[bytes++] = ADSI_LOAD_SOFTKEY;
492  /* Reserve for length */
493  bytes++;
494  /* Which key */
495  buf[bytes++] = key;
496 
497  /* Carefully copy long label */
498  bytes += ccopy(buf + bytes, (const unsigned char *)llabel, 18);
499 
500  /* Place delimiter */
501  buf[bytes++] = 0xff;
502 
503  /* Short label */
504  bytes += ccopy(buf + bytes, (const unsigned char *)slabel, 7);
505 
506 
507  /* If specified, copy return string */
508  if (ret) {
509  /* Place delimiter */
510  buf[bytes++] = 0xff;
511  if (data) {
512  buf[bytes++] = ADSI_SWITCH_TO_DATA2;
513  }
514  /* Carefully copy return string */
515  bytes += ccopy(buf + bytes, (const unsigned char *)ret, 20);
516 
517  }
518  /* Replace parameter length */
519  buf[1] = bytes - 2;
520  return bytes;
521 }
522 
523 static int adsi_connect_session(unsigned char *buf, unsigned char *fdn, int ver)
524 {
525  int bytes = 0, x;
526 
527  /* Message type */
528  buf[bytes++] = ADSI_CONNECT_SESSION;
529 
530  /* Reserve space for length */
531  bytes++;
532 
533  if (fdn) {
534  for (x = 0; x < 4; x++) {
535  buf[bytes++] = fdn[x];
536  }
537  if (ver > -1) {
538  buf[bytes++] = ver & 0xff;
539  }
540  }
541 
542  buf[1] = bytes - 2;
543  return bytes;
544 
545 }
546 
547 static int adsi_download_connect(unsigned char *buf, char *service, unsigned char *fdn, unsigned char *sec, int ver)
548 {
549  int bytes = 0, x;
550 
551  /* Message type */
552  buf[bytes++] = ADSI_DOWNLOAD_CONNECT;
553 
554  /* Reserve space for length */
555  bytes++;
556 
557  /* Primary column */
558  bytes+= ccopy(buf + bytes, (unsigned char *)service, 18);
559 
560  /* Delimiter */
561  buf[bytes++] = 0xff;
562 
563  for (x = 0; x < 4; x++) {
564  buf[bytes++] = fdn[x];
565  }
566 
567  for (x = 0; x < 4; x++) {
568  buf[bytes++] = sec[x];
569  }
570 
571  buf[bytes++] = ver & 0xff;
572 
573  buf[1] = bytes - 2;
574 
575  return bytes;
576 
577 }
578 
579 static int adsi_disconnect_session(unsigned char *buf)
580 {
581  int bytes = 0;
582 
583  /* Message type */
584  buf[bytes++] = ADSI_DISC_SESSION;
585 
586  /* Reserve space for length */
587  bytes++;
588 
589  buf[1] = bytes - 2;
590  return bytes;
591 
592 }
593 
594 static int adsi_query_cpeid(unsigned char *buf)
595 {
596  int bytes = 0;
597  buf[bytes++] = ADSI_QUERY_CPEID;
598  /* Reserve space for length */
599  bytes++;
600  buf[1] = bytes - 2;
601  return bytes;
602 }
603 
604 static int adsi_query_cpeinfo(unsigned char *buf)
605 {
606  int bytes = 0;
607  buf[bytes++] = ADSI_QUERY_CONFIG;
608  /* Reserve space for length */
609  bytes++;
610  buf[1] = bytes - 2;
611  return bytes;
612 }
613 
614 static int adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen)
615 {
616  int bytes = 0, res, gotstar = 0, pos = 0;
617  unsigned char current = 0;
618 
619  memset(buf, 0, maxlen);
620 
621  while (bytes <= maxlen) {
622  /* Wait up to a second for a digit */
623  if (!(res = ast_waitfordigit(chan, 1000))) {
624  break;
625  }
626  if (res == '*') {
627  gotstar = 1;
628  continue;
629  }
630  /* Ignore anything other than a digit */
631  if ((res < '0') || (res > '9')) {
632  continue;
633  }
634  res -= '0';
635  if (gotstar) {
636  res += 9;
637  }
638  if (pos) {
639  pos = 0;
640  buf[bytes++] = (res << 4) | current;
641  } else {
642  pos = 1;
643  current = res;
644  }
645  gotstar = 0;
646  }
647 
648  return bytes;
649 }
650 
651 static int adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice)
652 {
653  unsigned char buf[256] = "";
654  int bytes = 0, res;
655 
656  bytes += adsi_data_mode(buf);
657  adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
658 
659  bytes = 0;
660  bytes += adsi_query_cpeid(buf);
661  adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
662 
663  /* Get response */
664  res = adsi_read_encoded_dtmf(chan, cpeid, 4);
665  if (res != 4) {
666  ast_log(LOG_WARNING, "Got %d bytes back of encoded DTMF, expecting 4\n", res);
667  res = 0;
668  } else {
669  res = 1;
670  }
671 
672  if (voice) {
673  bytes = 0;
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);
678  }
679  return res;
680 }
681 
682 static int adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice)
683 {
684  unsigned char buf[256] = "";
685  int bytes = 0, res;
686 
687  bytes += adsi_data_mode(buf);
688  adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
689 
690  bytes = 0;
691  bytes += adsi_query_cpeinfo(buf);
692  adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
693 
694  /* Get width */
695  if ((res = ast_readstring(chan, (char *) buf, 2, 1000, 500, "")) < 0) {
696  return res;
697  }
698  if (strlen((char *) buf) != 2) {
699  ast_log(LOG_WARNING, "Got %d bytes of width, expecting 2\n", res);
700  res = 0;
701  } else {
702  res = 1;
703  }
704  if (width) {
705  *width = atoi((char *) buf);
706  }
707  /* Get height */
708  memset(buf, 0, sizeof(buf));
709  if (res) {
710  if ((res = ast_readstring(chan, (char *) buf, 2, 1000, 500, "")) < 0) {
711  return res;
712  }
713  if (strlen((char *) buf) != 2) {
714  ast_log(LOG_WARNING, "Got %d bytes of height, expecting 2\n", res);
715  res = 0;
716  } else {
717  res = 1;
718  }
719  if (height) {
720  *height = atoi((char *) buf);
721  }
722  }
723  /* Get buttons */
724  memset(buf, 0, sizeof(buf));
725  if (res) {
726  if ((res = ast_readstring(chan, (char *) buf, 1, 1000, 500, "")) < 0) {
727  return res;
728  }
729  if (strlen((char *) buf) != 1) {
730  ast_log(LOG_WARNING, "Got %d bytes of buttons, expecting 1\n", res);
731  res = 0;
732  } else {
733  res = 1;
734  }
735  if (buttons) {
736  *buttons = atoi((char *) buf);
737  }
738  }
739  if (voice) {
740  bytes = 0;
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);
745  }
746  return res;
747 }
748 
749 static int adsi_data_mode(unsigned char *buf)
750 {
751  int bytes = 0;
752 
753  /* Message type */
754  buf[bytes++] = ADSI_SWITCH_TO_DATA;
755 
756  /* Reserve space for length */
757  bytes++;
758 
759  buf[1] = bytes - 2;
760  return bytes;
761 
762 }
763 
764 static int adsi_clear_soft_keys(unsigned char *buf)
765 {
766  int bytes = 0;
767 
768  /* Message type */
769  buf[bytes++] = ADSI_CLEAR_SOFTKEY;
770 
771  /* Reserve space for length */
772  bytes++;
773 
774  buf[1] = bytes - 2;
775  return bytes;
776 
777 }
778 
779 static int adsi_clear_screen(unsigned char *buf)
780 {
781  int bytes = 0;
782 
783  /* Message type */
784  buf[bytes++] = ADSI_CLEAR_SCREEN;
785 
786  /* Reserve space for length */
787  bytes++;
788 
789  buf[1] = bytes - 2;
790  return bytes;
791 
792 }
793 
794 static int adsi_voice_mode(unsigned char *buf, int when)
795 {
796  int bytes = 0;
797 
798  /* Message type */
799  buf[bytes++] = ADSI_SWITCH_TO_VOICE;
800 
801  /* Reserve space for length */
802  bytes++;
803 
804  buf[bytes++] = when & 0x7f;
805 
806  buf[1] = bytes - 2;
807  return bytes;
808 
809 }
810 
811 static int adsi_available(struct ast_channel *chan)
812 {
813  int cpe = ast_channel_adsicpe(chan) & 0xff;
814  if ((cpe == AST_ADSI_AVAILABLE) ||
815  (cpe == AST_ADSI_UNKNOWN)) {
816  return 1;
817  }
818  return 0;
819 }
820 
821 static int adsi_download_disconnect(unsigned char *buf)
822 {
823  int bytes = 0;
824 
825  /* Message type */
826  buf[bytes++] = ADSI_DOWNLOAD_DISC;
827 
828  /* Reserve space for length */
829  bytes++;
830 
831  buf[1] = bytes - 2;
832  return bytes;
833 
834 }
835 
836 static int adsi_display(unsigned char *buf, int page, int line, int just, int wrap,
837  char *col1, char *col2)
838 {
839  int bytes = 0;
840 
841  /* Sanity check line number */
842 
843  if (page) {
844  if (line > 4) return -1;
845  } else {
846  if (line > 33) return -1;
847  }
848 
849  if (line < 1) {
850  return -1;
851  }
852  /* Parameter type */
853  buf[bytes++] = ADSI_LOAD_VIRTUAL_DISP;
854 
855  /* Reserve space for size */
856  bytes++;
857 
858  /* Page and wrap indicator */
859  buf[bytes++] = ((page & 0x1) << 7) | ((wrap & 0x1) << 6) | (line & 0x3f);
860 
861  /* Justification */
862  buf[bytes++] = (just & 0x3) << 5;
863 
864  /* Omit highlight mode definition */
865  buf[bytes++] = 0xff;
866 
867  /* Primary column */
868  bytes+= ccopy(buf + bytes, (unsigned char *)col1, 20);
869 
870  /* Delimiter */
871  buf[bytes++] = 0xff;
872 
873  /* Secondary column */
874  bytes += ccopy(buf + bytes, (unsigned char *)col2, 20);
875 
876  /* Update length */
877  buf[1] = bytes - 2;
878 
879  return bytes;
880 
881 }
882 
883 static int adsi_input_control(unsigned char *buf, int page, int line, int display, int format, int just)
884 {
885  int bytes = 0;
886 
887  if (page) {
888  if (line > 4) return -1;
889  } else {
890  if (line > 33) return -1;
891  }
892 
893  if (line < 1) {
894  return -1;
895  }
896 
897  buf[bytes++] = ADSI_INPUT_CONTROL;
898  bytes++;
899  buf[bytes++] = ((page & 1) << 7) | (line & 0x3f);
900  buf[bytes++] = ((display & 1) << 7) | ((just & 0x3) << 4) | (format & 0x7);
901 
902  buf[1] = bytes - 2;
903  return bytes;
904 }
905 
906 static int adsi_input_format(unsigned char *buf, int num, int dir, int wrap, char *format1, char *format2)
907 {
908  int bytes = 0;
909 
910  if (ast_strlen_zero((char *) format1)) {
911  return -1;
912  }
913 
914  buf[bytes++] = ADSI_INPUT_FORMAT;
915  bytes++;
916  buf[bytes++] = ((dir & 1) << 7) | ((wrap & 1) << 6) | (num & 0x7);
917  bytes += ccopy(buf + bytes, (unsigned char *) format1, 20);
918  buf[bytes++] = 0xff;
919  if (!ast_strlen_zero(format2)) {
920  bytes += ccopy(buf + bytes, (unsigned char *) format2, 20);
921  }
922  buf[1] = bytes - 2;
923  return bytes;
924 }
925 
926 static int adsi_set_keys(unsigned char *buf, unsigned char *keys)
927 {
928  int bytes = 0, x;
929 
930  /* Message type */
931  buf[bytes++] = ADSI_INIT_SOFTKEY_LINE;
932  /* Space for size */
933  bytes++;
934  /* Key definitions */
935  for (x = 0; x < 6; x++) {
936  buf[bytes++] = (keys[x] & 0x3f) ? keys[x] : (keys[x] | 0x1);
937  }
938  buf[1] = bytes - 2;
939  return bytes;
940 }
941 
942 static int adsi_set_line(unsigned char *buf, int page, int line)
943 {
944  int bytes = 0;
945 
946  /* Sanity check line number */
947 
948  if (page) {
949  if (line > 4) return -1;
950  } else {
951  if (line > 33) return -1;
952  }
953 
954  if (line < 1) {
955  return -1;
956  }
957  /* Parameter type */
958  buf[bytes++] = ADSI_LINE_CONTROL;
959 
960  /* Reserve space for size */
961  bytes++;
962 
963  /* Page and line */
964  buf[bytes++] = ((page & 0x1) << 7) | (line & 0x3f);
965 
966  buf[1] = bytes - 2;
967  return bytes;
968 }
969 
970 static int total = 0;
971 static int speeds = 0;
972 
973 static int adsi_channel_restore(struct ast_channel *chan)
974 {
975  unsigned char dsp[256] = "", keyd[6] = "";
976  int bytes, x;
977 
978  /* Start with initial display setup */
979  bytes = 0;
980  bytes += adsi_set_line(dsp + bytes, ADSI_INFO_PAGE, 1);
981 
982  /* Prepare key setup messages */
983 
984  if (speeds) {
985  for (x = 0; x < speeds; x++) {
986  keyd[x] = ADSI_SPEED_DIAL + x;
987  }
988  bytes += adsi_set_keys(dsp + bytes, keyd);
989  }
990  adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0);
991  return 0;
992 
993 }
994 
995 static int adsi_print(struct ast_channel *chan, char **lines, int *alignments, int voice)
996 {
997  unsigned char buf[4096];
998  int bytes = 0, res, x;
999 
1000  for (x = 0; lines[x]; x++) {
1001  bytes += adsi_display(buf + bytes, ADSI_INFO_PAGE, x+1, alignments[x], 0, lines[x], "");
1002  }
1003  bytes += adsi_set_line(buf + bytes, ADSI_INFO_PAGE, 1);
1004  if (voice) {
1005  bytes += adsi_voice_mode(buf + bytes, 0);
1006  }
1007  res = adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0);
1008  if (voice) {
1009  /* Ignore the resulting DTMF B announcing it's in voice mode */
1010  ast_waitfordigit(chan, 1000);
1011  }
1012  return res;
1013 }
1014 
1015 static int adsi_load_session(struct ast_channel *chan, unsigned char *app, int ver, int data)
1016 {
1017  unsigned char dsp[256] = "";
1018  int bytes = 0, res;
1019  char resp[2];
1020 
1021  /* Connect to session */
1022  bytes += adsi_connect_session(dsp + bytes, app, ver);
1023 
1024  if (data) {
1025  bytes += adsi_data_mode(dsp + bytes);
1026  }
1027 
1028  /* Prepare key setup messages */
1029  if (adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0)) {
1030  return -1;
1031  }
1032  if (app) {
1033  if ((res = ast_readstring(chan, resp, 1, 1200, 1200, "")) < 0) {
1034  return -1;
1035  }
1036  if (res) {
1037  ast_debug(1, "No response from CPE about version. Assuming not there.\n");
1038  return 0;
1039  }
1040  if (!strcmp(resp, "B")) {
1041  ast_debug(1, "CPE has script '%s' version %d already loaded\n", app, ver);
1042  return 1;
1043  } else if (!strcmp(resp, "A")) {
1044  ast_debug(1, "CPE hasn't script '%s' version %d already loaded\n", app, ver);
1045  } else {
1046  ast_log(LOG_WARNING, "Unexpected CPE response to script query: %s\n", resp);
1047  }
1048  } else
1049  return 1;
1050  return 0;
1051 
1052 }
1053 
1054 static int adsi_unload_session(struct ast_channel *chan)
1055 {
1056  unsigned char dsp[256] = "";
1057  int bytes = 0;
1058 
1059  /* Connect to session */
1060  bytes += adsi_disconnect_session(dsp + bytes);
1061  bytes += adsi_voice_mode(dsp + bytes, 0);
1062 
1063  /* Prepare key setup messages */
1064  if (adsi_transmit_message_full(chan, dsp, bytes, ADSI_MSG_DISPLAY, 0)) {
1065  return -1;
1066  }
1067 
1068  return 0;
1069 }
1070 
1071 static int str2align(const char *s)
1072 {
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;
1079  } else {
1080  return ADSI_JUST_CENT;
1081  }
1082 }
1083 
1084 static void init_state(void)
1085 {
1086  int x;
1087 
1088  for (x = 0; x < ADSI_MAX_INTRO; x++) {
1089  aligns[x] = ADSI_JUST_CENT;
1090  }
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]));
1094  total = 3;
1095  speeds = 0;
1096  for (x = 3; x < ADSI_MAX_INTRO; x++) {
1097  intro[x][0] = '\0';
1098  }
1099  memset(speeddial, 0, sizeof(speeddial));
1100  alignment = ADSI_JUST_CENT;
1101 }
1102 
1103 static void adsi_load(int reload)
1104 {
1105  int x = 0;
1106  struct ast_config *conf = NULL;
1107  struct ast_variable *v;
1108  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1109  char *name, *sname;
1110  init_state();
1111 
1112  conf = ast_config_load("adsi.conf", config_flags);
1113  if (conf == CONFIG_STATUS_FILEMISSING || conf == CONFIG_STATUS_FILEUNCHANGED || conf == CONFIG_STATUS_FILEINVALID) {
1114  return;
1115  }
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]));
1123  x++;
1124  }
1125  } else if (!strcasecmp(v->name, "maxretries")) {
1126  if (atoi(v->value) > 0) {
1127  maxretries = atoi(v->value);
1128  }
1129  }
1130  }
1131  if (x) {
1132  total = x;
1133  }
1134 
1135  x = 0;
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, ",");
1142  if (!sname) {
1143  sname = name;
1144  }
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);
1149  x++;
1150  }
1151  }
1152  if (x) {
1153  speeds = x;
1154  }
1155  ast_config_destroy(conf);
1156 
1157  return;
1158 }
1159 
1160 static int reload(void)
1161 {
1162  adsi_load(1);
1163  return 0;
1164 }
1165 
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,
1196 };
1197 
1198 static int load_module(void)
1199 {
1200  adsi_load(0);
1201  ast_adsi_install_funcs(&res_adsi_funcs);
1202  return AST_MODULE_LOAD_SUCCESS;
1203 }
1204 
1205 static int unload_module(void)
1206 {
1207  /* Can't unload this once we're loaded */
1208  ast_adsi_install_funcs(NULL);
1209  return -1;
1210 }
1211 
1212 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ADSI Resource",
1213  .support_level = AST_MODULE_SUPPORT_DEPRECATED,
1214  .load = load_module,
1215  .unload = unload_module,
1216  .reload = reload,
1217  .load_pri = AST_MODPRI_APP_DEPEND,
1218 );
A-Law to Signed linear conversion.
struct ast_variable * next
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
Time-related functions and macros.
struct ast_format * ast_format_ulaw
Built-in cached ulaw format.
Definition: format_cache.c:86
Structure for variables, used for configurations and for channel variables.
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
Definition of a media format.
Definition: format.c:43
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
ADSI Support (built upon Caller*ID)
struct ast_frame_subclass subclass
All configuration options for http media cache.
int ast_channel_defer_dtmf(struct ast_channel *chan)
Defers DTMF so that you only read things like hangups and audio.
Definition: channel.c:1257
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
Configuration File Parser.
u-Law to Signed linear conversion
#define ast_config_load(filename, flags)
Load a config file.
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
General Asterisk PBX channel definitions.
const char * src
void ast_channel_undefer_dtmf(struct ast_channel *chan)
Unset defer DTMF flag on channel.
Definition: channel.c:1271
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5762
#define ast_debug(level,...)
Log a DEBUG message.
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5803
union ast_frame::@224 data
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5144
Structure used to handle boolean flags.
Definition: utils.h:199
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3175
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3162
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Data structure associated with a single frame of data.
FSK Modem Support.
enum ast_frame_type frametype
int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders)
Reads multiple digits.
Definition: channel.c:6558
struct ast_format * format
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:222
Media Format Cache API.
int ast_gen_cas(unsigned char *outbuf, int sas, int len, struct ast_format *codec)
Generate a CAS (CPE Alert Signal) tone for 'n' samples.
Definition: callerid.c:271