res_adsi.c 29 KB

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