res_adsi.c 29 KB

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