app_adsiprog.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Program Asterisk ADSI Scripts into phone
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*! \li \ref app_adsiprog.c uses the configuration file \ref adsi.conf
  27. * \addtogroup configuration_file Configuration Files
  28. */
  29. /*!
  30. * \page adsi.conf adsi.conf
  31. * \verbinclude adsi.conf.sample
  32. */
  33. /*** MODULEINFO
  34. <depend>res_adsi</depend>
  35. <support_level>extended</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <netinet/in.h>
  40. #include <ctype.h>
  41. #include "asterisk/paths.h" /* use ast_config_AST_CONFIG_DIR */
  42. #include "asterisk/file.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/pbx.h"
  45. #include "asterisk/module.h"
  46. #include "asterisk/adsi.h"
  47. #include "asterisk/utils.h"
  48. #include "asterisk/lock.h"
  49. static const char app[] = "ADSIProg";
  50. /*** DOCUMENTATION
  51. <application name="ADSIProg" language="en_US">
  52. <synopsis>
  53. Load Asterisk ADSI Scripts into phone
  54. </synopsis>
  55. <syntax>
  56. <parameter name="script" required="false">
  57. <para>adsi script to use. If not given uses the default script <filename>asterisk.adsi</filename></para>
  58. </parameter>
  59. </syntax>
  60. <description>
  61. <para>This application programs an ADSI Phone with the given script</para>
  62. </description>
  63. <see-also>
  64. <ref type="application">GetCPEID</ref>
  65. <ref type="filename">adsi.conf</ref>
  66. </see-also>
  67. </application>
  68. ***/
  69. /* #define DUMP_MESSAGES */
  70. struct adsi_event {
  71. int id;
  72. const char *name;
  73. };
  74. static const struct adsi_event events[] = {
  75. { 1, "CALLERID" },
  76. { 2, "VMWI" },
  77. { 3, "NEARANSWER" },
  78. { 4, "FARANSWER" },
  79. { 5, "ENDOFRING" },
  80. { 6, "IDLE" },
  81. { 7, "OFFHOOK" },
  82. { 8, "CIDCW" },
  83. { 9, "BUSY" },
  84. { 10, "FARRING" },
  85. { 11, "DIALTONE" },
  86. { 12, "RECALL" },
  87. { 13, "MESSAGE" },
  88. { 14, "REORDER" },
  89. { 15, "DISTINCTIVERING" },
  90. { 16, "RING" },
  91. { 17, "REMINDERRING" },
  92. { 18, "SPECIALRING" },
  93. { 19, "CODEDRING" },
  94. { 20, "TIMER" },
  95. { 21, "INUSE" },
  96. { 22, "EVENT22" },
  97. { 23, "EVENT23" },
  98. { 24, "CPEID" },
  99. };
  100. static const struct adsi_event justify[] = {
  101. { 0, "CENTER" },
  102. { 1, "RIGHT" },
  103. { 2, "LEFT" },
  104. { 3, "INDENT" },
  105. };
  106. #define STATE_NORMAL 0
  107. #define STATE_INKEY 1
  108. #define STATE_INSUB 2
  109. #define STATE_INIF 3
  110. #define MAX_RET_CODE 20
  111. #define MAX_SUB_LEN 255
  112. #define MAX_MAIN_LEN 1600
  113. #define ARG_STRING (1 << 0)
  114. #define ARG_NUMBER (1 << 1)
  115. struct adsi_soft_key {
  116. char vname[40]; /* Which "variable" is associated with it */
  117. int retstrlen; /* Length of return string */
  118. int initlen; /* initial length */
  119. int id;
  120. int defined;
  121. char retstr[80]; /* Return string data */
  122. };
  123. struct adsi_subscript {
  124. char vname[40];
  125. int id;
  126. int defined;
  127. int datalen;
  128. int inscount;
  129. int ifinscount;
  130. char *ifdata;
  131. char data[2048];
  132. };
  133. struct adsi_state {
  134. char vname[40];
  135. int id;
  136. };
  137. struct adsi_flag {
  138. char vname[40];
  139. int id;
  140. };
  141. struct adsi_display {
  142. char vname[40];
  143. int id;
  144. char data[70];
  145. int datalen;
  146. };
  147. struct adsi_script {
  148. int state;
  149. int numkeys;
  150. int numsubs;
  151. int numstates;
  152. int numdisplays;
  153. int numflags;
  154. struct adsi_soft_key *key;
  155. struct adsi_subscript *sub;
  156. /* Pre-defined displays */
  157. struct adsi_display displays[63];
  158. /* ADSI States 1 (initial) - 254 */
  159. struct adsi_state states[256];
  160. /* Keys 2-63 */
  161. struct adsi_soft_key keys[62];
  162. /* Subscripts 0 (main) to 127 */
  163. struct adsi_subscript subs[128];
  164. /* Flags 1-7 */
  165. struct adsi_flag flags[7];
  166. /* Stuff from adsi script */
  167. unsigned char sec[5];
  168. char desc[19];
  169. unsigned char fdn[5];
  170. int ver;
  171. };
  172. static int process_token(void *out, char *src, int maxlen, int argtype)
  173. {
  174. if ((strlen(src) > 1) && src[0] == '\"') {
  175. /* This is a quoted string */
  176. if (!(argtype & ARG_STRING))
  177. return -1;
  178. src++;
  179. /* Don't take more than what's there */
  180. if (maxlen > strlen(src) - 1)
  181. maxlen = strlen(src) - 1;
  182. memcpy(out, src, maxlen);
  183. ((char *)out)[maxlen] = '\0';
  184. } else if (!ast_strlen_zero(src) && (src[0] == '\\')) {
  185. if (!(argtype & ARG_NUMBER))
  186. return -1;
  187. /* Octal value */
  188. if (sscanf(src, "%30o", (unsigned *)out) != 1)
  189. return -1;
  190. if (argtype & ARG_STRING) {
  191. /* Convert */
  192. *((unsigned int *)out) = htonl(*((unsigned int *)out));
  193. }
  194. } else if ((strlen(src) > 2) && (src[0] == '0') && (tolower(src[1]) == 'x')) {
  195. if (!(argtype & ARG_NUMBER))
  196. return -1;
  197. /* Hex value */
  198. if (sscanf(src + 2, "%30x", (unsigned int *)out) != 1)
  199. return -1;
  200. if (argtype & ARG_STRING) {
  201. /* Convert */
  202. *((unsigned int *)out) = htonl(*((unsigned int *)out));
  203. }
  204. } else if ((!ast_strlen_zero(src) && isdigit(src[0]))) {
  205. if (!(argtype & ARG_NUMBER))
  206. return -1;
  207. /* Hex value */
  208. if (sscanf(src, "%30d", (int *)out) != 1)
  209. return -1;
  210. if (argtype & ARG_STRING) {
  211. /* Convert */
  212. *((unsigned int *)out) = htonl(*((unsigned int *)out));
  213. }
  214. } else
  215. return -1;
  216. return 0;
  217. }
  218. static char *get_token(char **buf, const char *script, int lineno)
  219. {
  220. char *tmp = *buf, *keyword;
  221. int quoted = 0;
  222. /* Advance past any white space */
  223. while(*tmp && (*tmp < 33))
  224. tmp++;
  225. if (!*tmp)
  226. return NULL;
  227. keyword = tmp;
  228. while(*tmp && ((*tmp > 32) || quoted)) {
  229. if (*tmp == '\"') {
  230. quoted = !quoted;
  231. }
  232. tmp++;
  233. }
  234. if (quoted) {
  235. ast_log(LOG_WARNING, "Mismatched quotes at line %d of %s\n", lineno, script);
  236. return NULL;
  237. }
  238. *tmp = '\0';
  239. tmp++;
  240. while(*tmp && (*tmp < 33))
  241. tmp++;
  242. /* Note where we left off */
  243. *buf = tmp;
  244. return keyword;
  245. }
  246. static char *validdtmf = "123456789*0#ABCD";
  247. static int send_dtmf(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  248. {
  249. char dtmfstr[80], *a;
  250. int bytes = 0;
  251. if (!(a = get_token(&args, script, lineno))) {
  252. ast_log(LOG_WARNING, "Expecting something to send for SENDDTMF at line %d of %s\n", lineno, script);
  253. return 0;
  254. }
  255. if (process_token(dtmfstr, a, sizeof(dtmfstr) - 1, ARG_STRING)) {
  256. ast_log(LOG_WARNING, "Invalid token for SENDDTMF at line %d of %s\n", lineno, script);
  257. return 0;
  258. }
  259. a = dtmfstr;
  260. while (*a) {
  261. if (strchr(validdtmf, *a)) {
  262. *buf = *a;
  263. buf++;
  264. bytes++;
  265. } else
  266. ast_log(LOG_WARNING, "'%c' is not a valid DTMF tone at line %d of %s\n", *a, lineno, script);
  267. a++;
  268. }
  269. return bytes;
  270. }
  271. static int goto_line(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  272. {
  273. char *page = get_token(&args, script, lineno);
  274. char *gline = get_token(&args, script, lineno);
  275. int line;
  276. unsigned char cmd;
  277. if (!page || !gline) {
  278. ast_log(LOG_WARNING, "Expecting page and line number for GOTOLINE at line %d of %s\n", lineno, script);
  279. return 0;
  280. }
  281. if (!strcasecmp(page, "INFO"))
  282. cmd = 0;
  283. else if (!strcasecmp(page, "COMM"))
  284. cmd = 0x80;
  285. else {
  286. ast_log(LOG_WARNING, "Expecting either 'INFO' or 'COMM' page, got '%s' at line %d of %s\n", page, lineno, script);
  287. return 0;
  288. }
  289. if (process_token(&line, gline, sizeof(line), ARG_NUMBER)) {
  290. ast_log(LOG_WARNING, "Invalid line number '%s' at line %d of %s\n", gline, lineno, script);
  291. return 0;
  292. }
  293. cmd |= line;
  294. buf[0] = 0x8b;
  295. buf[1] = cmd;
  296. return 2;
  297. }
  298. static int goto_line_rel(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  299. {
  300. char *dir = get_token(&args, script, lineno);
  301. char *gline = get_token(&args, script, lineno);
  302. int line;
  303. unsigned char cmd;
  304. if (!dir || !gline) {
  305. ast_log(LOG_WARNING, "Expecting direction and number of lines for GOTOLINEREL at line %d of %s\n", lineno, script);
  306. return 0;
  307. }
  308. if (!strcasecmp(dir, "UP"))
  309. cmd = 0;
  310. else if (!strcasecmp(dir, "DOWN"))
  311. cmd = 0x20;
  312. else {
  313. ast_log(LOG_WARNING, "Expecting either 'UP' or 'DOWN' direction, got '%s' at line %d of %s\n", dir, lineno, script);
  314. return 0;
  315. }
  316. if (process_token(&line, gline, sizeof(line), ARG_NUMBER)) {
  317. ast_log(LOG_WARNING, "Invalid line number '%s' at line %d of %s\n", gline, lineno, script);
  318. return 0;
  319. }
  320. cmd |= line;
  321. buf[0] = 0x8c;
  322. buf[1] = cmd;
  323. return 2;
  324. }
  325. static int send_delay(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  326. {
  327. char *gtime = get_token(&args, script, lineno);
  328. int ms;
  329. if (!gtime) {
  330. ast_log(LOG_WARNING, "Expecting number of milliseconds to wait at line %d of %s\n", lineno, script);
  331. return 0;
  332. }
  333. if (process_token(&ms, gtime, sizeof(ms), ARG_NUMBER)) {
  334. ast_log(LOG_WARNING, "Invalid delay milliseconds '%s' at line %d of %s\n", gtime, lineno, script);
  335. return 0;
  336. }
  337. buf[0] = 0x90;
  338. if (id == 11)
  339. buf[1] = ms / 100;
  340. else
  341. buf[1] = ms / 10;
  342. return 2;
  343. }
  344. static int set_state(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno)
  345. {
  346. char *gstate = get_token(&args, script, lineno);
  347. int state;
  348. if (!gstate) {
  349. ast_log(LOG_WARNING, "Expecting state number at line %d of %s\n", lineno, script);
  350. return 0;
  351. }
  352. if (process_token(&state, gstate, sizeof(state), ARG_NUMBER)) {
  353. ast_log(LOG_WARNING, "Invalid state number '%s' at line %d of %s\n", gstate, lineno, script);
  354. return 0;
  355. }
  356. buf[0] = id;
  357. buf[1] = state;
  358. return 2;
  359. }
  360. static int cleartimer(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno)
  361. {
  362. char *tok = get_token(&args, script, lineno);
  363. if (tok)
  364. ast_log(LOG_WARNING, "Clearing timer requires no arguments ('%s') at line %d of %s\n", tok, lineno, script);
  365. buf[0] = id;
  366. /* For some reason the clear code is different slightly */
  367. if (id == 7)
  368. buf[1] = 0x10;
  369. else
  370. buf[1] = 0x00;
  371. return 2;
  372. }
  373. static struct adsi_flag *getflagbyname(struct adsi_script *state, char *name, const char *script, int lineno, int create)
  374. {
  375. int x;
  376. for (x = 0; x < state->numflags; x++) {
  377. if (!strcasecmp(state->flags[x].vname, name))
  378. return &state->flags[x];
  379. }
  380. /* Return now if we're not allowed to create */
  381. if (!create)
  382. return NULL;
  383. if (state->numflags > 6) {
  384. ast_log(LOG_WARNING, "No more flag space at line %d of %s\n", lineno, script);
  385. return NULL;
  386. }
  387. ast_copy_string(state->flags[state->numflags].vname, name, sizeof(state->flags[state->numflags].vname));
  388. state->flags[state->numflags].id = state->numflags + 1;
  389. state->numflags++;
  390. return &state->flags[state->numflags-1];
  391. }
  392. static int setflag(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  393. {
  394. char *tok = get_token(&args, script, lineno);
  395. char sname[80];
  396. struct adsi_flag *flag;
  397. if (!tok) {
  398. ast_log(LOG_WARNING, "Setting flag requires a flag number at line %d of %s\n", lineno, script);
  399. return 0;
  400. }
  401. if (process_token(sname, tok, sizeof(sname) - 1, ARG_STRING)) {
  402. ast_log(LOG_WARNING, "Invalid flag '%s' at line %d of %s\n", tok, lineno, script);
  403. return 0;
  404. }
  405. if (!(flag = getflagbyname(state, sname, script, lineno, 0))) {
  406. ast_log(LOG_WARNING, "Flag '%s' is undeclared at line %d of %s\n", sname, lineno, script);
  407. return 0;
  408. }
  409. buf[0] = id;
  410. buf[1] = ((flag->id & 0x7) << 4) | 1;
  411. return 2;
  412. }
  413. static int clearflag(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  414. {
  415. char *tok = get_token(&args, script, lineno);
  416. struct adsi_flag *flag;
  417. char sname[80];
  418. if (!tok) {
  419. ast_log(LOG_WARNING, "Clearing flag requires a flag number at line %d of %s\n", lineno, script);
  420. return 0;
  421. }
  422. if (process_token(sname, tok, sizeof(sname) - 1, ARG_STRING)) {
  423. ast_log(LOG_WARNING, "Invalid flag '%s' at line %d of %s\n", tok, lineno, script);
  424. return 0;
  425. }
  426. if (!(flag = getflagbyname(state, sname, script, lineno, 0))) {
  427. ast_log(LOG_WARNING, "Flag '%s' is undeclared at line %d of %s\n", sname, lineno, script);
  428. return 0;
  429. }
  430. buf[0] = id;
  431. buf[1] = ((flag->id & 0x7) << 4);
  432. return 2;
  433. }
  434. static int starttimer(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno)
  435. {
  436. char *tok = get_token(&args, script, lineno);
  437. int secs;
  438. if (!tok) {
  439. ast_log(LOG_WARNING, "Missing number of seconds at line %d of %s\n", lineno, script);
  440. return 0;
  441. }
  442. if (process_token(&secs, tok, sizeof(secs), ARG_NUMBER)) {
  443. ast_log(LOG_WARNING, "Invalid number of seconds '%s' at line %d of %s\n", tok, lineno, script);
  444. return 0;
  445. }
  446. buf[0] = id;
  447. buf[1] = 0x1;
  448. buf[2] = secs;
  449. return 3;
  450. }
  451. static int geteventbyname(char *name)
  452. {
  453. int x;
  454. for (x = 0; x < ARRAY_LEN(events); x++) {
  455. if (!strcasecmp(events[x].name, name))
  456. return events[x].id;
  457. }
  458. return 0;
  459. }
  460. static int getjustifybyname(char *name)
  461. {
  462. int x;
  463. for (x = 0; x < ARRAY_LEN(justify); x++) {
  464. if (!strcasecmp(justify[x].name, name))
  465. return justify[x].id;
  466. }
  467. return -1;
  468. }
  469. static struct adsi_soft_key *getkeybyname(struct adsi_script *state, char *name, const char *script, int lineno)
  470. {
  471. int x;
  472. for (x = 0; x < state->numkeys; x++) {
  473. if (!strcasecmp(state->keys[x].vname, name))
  474. return &state->keys[x];
  475. }
  476. if (state->numkeys > 61) {
  477. ast_log(LOG_WARNING, "No more key space at line %d of %s\n", lineno, script);
  478. return NULL;
  479. }
  480. ast_copy_string(state->keys[state->numkeys].vname, name, sizeof(state->keys[state->numkeys].vname));
  481. state->keys[state->numkeys].id = state->numkeys + 2;
  482. state->numkeys++;
  483. return &state->keys[state->numkeys-1];
  484. }
  485. static struct adsi_subscript *getsubbyname(struct adsi_script *state, char *name, const char *script, int lineno)
  486. {
  487. int x;
  488. for (x = 0; x < state->numsubs; x++) {
  489. if (!strcasecmp(state->subs[x].vname, name))
  490. return &state->subs[x];
  491. }
  492. if (state->numsubs > 127) {
  493. ast_log(LOG_WARNING, "No more subscript space at line %d of %s\n", lineno, script);
  494. return NULL;
  495. }
  496. ast_copy_string(state->subs[state->numsubs].vname, name, sizeof(state->subs[state->numsubs].vname));
  497. state->subs[state->numsubs].id = state->numsubs;
  498. state->numsubs++;
  499. return &state->subs[state->numsubs-1];
  500. }
  501. static struct adsi_state *getstatebyname(struct adsi_script *state, char *name, const char *script, int lineno, int create)
  502. {
  503. int x;
  504. for (x = 0; x <state->numstates; x++) {
  505. if (!strcasecmp(state->states[x].vname, name))
  506. return &state->states[x];
  507. }
  508. /* Return now if we're not allowed to create */
  509. if (!create)
  510. return NULL;
  511. if (state->numstates > 253) {
  512. ast_log(LOG_WARNING, "No more state space at line %d of %s\n", lineno, script);
  513. return NULL;
  514. }
  515. ast_copy_string(state->states[state->numstates].vname, name, sizeof(state->states[state->numstates].vname));
  516. state->states[state->numstates].id = state->numstates + 1;
  517. state->numstates++;
  518. return &state->states[state->numstates-1];
  519. }
  520. static struct adsi_display *getdisplaybyname(struct adsi_script *state, char *name, const char *script, int lineno, int create)
  521. {
  522. int x;
  523. for (x = 0; x < state->numdisplays; x++) {
  524. if (!strcasecmp(state->displays[x].vname, name))
  525. return &state->displays[x];
  526. }
  527. /* Return now if we're not allowed to create */
  528. if (!create)
  529. return NULL;
  530. if (state->numdisplays > 61) {
  531. ast_log(LOG_WARNING, "No more display space at line %d of %s\n", lineno, script);
  532. return NULL;
  533. }
  534. ast_copy_string(state->displays[state->numdisplays].vname, name, sizeof(state->displays[state->numdisplays].vname));
  535. state->displays[state->numdisplays].id = state->numdisplays + 1;
  536. state->numdisplays++;
  537. return &state->displays[state->numdisplays-1];
  538. }
  539. static int showkeys(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  540. {
  541. char *tok, newkey[80];
  542. int bytes, x, flagid = 0;
  543. unsigned char keyid[6];
  544. struct adsi_soft_key *key;
  545. struct adsi_flag *flag;
  546. for (x = 0; x < 7; x++) {
  547. /* Up to 6 key arguments */
  548. if (!(tok = get_token(&args, script, lineno)))
  549. break;
  550. if (!strcasecmp(tok, "UNLESS")) {
  551. /* Check for trailing UNLESS flag */
  552. if (!(tok = get_token(&args, script, lineno)))
  553. ast_log(LOG_WARNING, "Missing argument for UNLESS clause at line %d of %s\n", lineno, script);
  554. else if (process_token(newkey, tok, sizeof(newkey) - 1, ARG_STRING))
  555. ast_log(LOG_WARNING, "Invalid flag name '%s' at line %d of %s\n", tok, lineno, script);
  556. else if (!(flag = getflagbyname(state, newkey, script, lineno, 0)))
  557. ast_log(LOG_WARNING, "Flag '%s' is undeclared at line %d of %s\n", newkey, lineno, script);
  558. else
  559. flagid = flag->id;
  560. if ((tok = get_token(&args, script, lineno)))
  561. ast_log(LOG_WARNING, "Extra arguments after UNLESS clause: '%s' at line %d of %s\n", tok, lineno, script);
  562. break;
  563. }
  564. if (x > 5) {
  565. ast_log(LOG_WARNING, "Only 6 keys can be defined, ignoring '%s' at line %d of %s\n", tok, lineno, script);
  566. break;
  567. }
  568. if (process_token(newkey, tok, sizeof(newkey) - 1, ARG_STRING)) {
  569. ast_log(LOG_WARNING, "Invalid token for key name: %s\n", tok);
  570. continue;
  571. }
  572. if (!(key = getkeybyname(state, newkey, script, lineno)))
  573. break;
  574. keyid[x] = key->id;
  575. }
  576. buf[0] = id;
  577. buf[1] = (flagid & 0x7) << 3 | (x & 0x7);
  578. for (bytes = 0; bytes < x; bytes++)
  579. buf[bytes + 2] = keyid[bytes];
  580. return 2 + x;
  581. }
  582. static int showdisplay(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  583. {
  584. char *tok, dispname[80];
  585. int line = 0, flag = 0, cmd = 3;
  586. struct adsi_display *disp;
  587. /* Get display */
  588. if (!(tok = get_token(&args, script, lineno)) || process_token(dispname, tok, sizeof(dispname) - 1, ARG_STRING)) {
  589. ast_log(LOG_WARNING, "Invalid display name: %s at line %d of %s\n", tok ? tok : "<nothing>", lineno, script);
  590. return 0;
  591. }
  592. if (!(disp = getdisplaybyname(state, dispname, script, lineno, 0))) {
  593. ast_log(LOG_WARNING, "Display '%s' is undefined at line %d of %s\n", dispname, lineno, script);
  594. return 0;
  595. }
  596. if (!(tok = get_token(&args, script, lineno)) || strcasecmp(tok, "AT")) {
  597. ast_log(LOG_WARNING, "Missing token 'AT' at line %d of %s\n", lineno, script);
  598. return 0;
  599. }
  600. /* Get line number */
  601. if (!(tok = get_token(&args, script, lineno)) || process_token(&line, tok, sizeof(line), ARG_NUMBER)) {
  602. ast_log(LOG_WARNING, "Invalid line: '%s' at line %d of %s\n", tok ? tok : "<nothing>", lineno, script);
  603. return 0;
  604. }
  605. if ((tok = get_token(&args, script, lineno)) && !strcasecmp(tok, "NOUPDATE")) {
  606. cmd = 1;
  607. tok = get_token(&args, script, lineno);
  608. }
  609. if (tok && !strcasecmp(tok, "UNLESS")) {
  610. /* Check for trailing UNLESS flag */
  611. if (!(tok = get_token(&args, script, lineno)))
  612. ast_log(LOG_WARNING, "Missing argument for UNLESS clause at line %d of %s\n", lineno, script);
  613. else if (process_token(&flag, tok, sizeof(flag), ARG_NUMBER))
  614. ast_log(LOG_WARNING, "Invalid flag number '%s' at line %d of %s\n", tok, lineno, script);
  615. if ((tok = get_token(&args, script, lineno)))
  616. ast_log(LOG_WARNING, "Extra arguments after UNLESS clause: '%s' at line %d of %s\n", tok, lineno, script);
  617. }
  618. buf[0] = id;
  619. buf[1] = (cmd << 6) | (disp->id & 0x3f);
  620. buf[2] = ((line & 0x1f) << 3) | (flag & 0x7);
  621. return 3;
  622. }
  623. static int cleardisplay(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno)
  624. {
  625. char *tok = get_token(&args, script, lineno);
  626. if (tok)
  627. ast_log(LOG_WARNING, "Clearing display requires no arguments ('%s') at line %d of %s\n", tok, lineno, script);
  628. buf[0] = id;
  629. buf[1] = 0x00;
  630. return 2;
  631. }
  632. static int digitdirect(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno)
  633. {
  634. char *tok = get_token(&args, script, lineno);
  635. if (tok)
  636. ast_log(LOG_WARNING, "Digitdirect requires no arguments ('%s') at line %d of %s\n", tok, lineno, script);
  637. buf[0] = id;
  638. buf[1] = 0x7;
  639. return 2;
  640. }
  641. static int clearcbone(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno)
  642. {
  643. char *tok = get_token(&args, script, lineno);
  644. if (tok)
  645. ast_log(LOG_WARNING, "CLEARCB1 requires no arguments ('%s') at line %d of %s\n", tok, lineno, script);
  646. buf[0] = id;
  647. buf[1] = 0;
  648. return 2;
  649. }
  650. static int digitcollect(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno)
  651. {
  652. char *tok = get_token(&args, script, lineno);
  653. if (tok)
  654. ast_log(LOG_WARNING, "Digitcollect requires no arguments ('%s') at line %d of %s\n", tok, lineno, script);
  655. buf[0] = id;
  656. buf[1] = 0xf;
  657. return 2;
  658. }
  659. static int subscript(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  660. {
  661. char *tok = get_token(&args, script, lineno);
  662. char subscr[80];
  663. struct adsi_subscript *sub;
  664. if (!tok) {
  665. ast_log(LOG_WARNING, "Missing subscript to call at line %d of %s\n", lineno, script);
  666. return 0;
  667. }
  668. if (process_token(subscr, tok, sizeof(subscr) - 1, ARG_STRING)) {
  669. ast_log(LOG_WARNING, "Invalid number of seconds '%s' at line %d of %s\n", tok, lineno, script);
  670. return 0;
  671. }
  672. if (!(sub = getsubbyname(state, subscr, script, lineno)))
  673. return 0;
  674. buf[0] = 0x9d;
  675. buf[1] = sub->id;
  676. return 2;
  677. }
  678. static int onevent(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno)
  679. {
  680. char *tok = get_token(&args, script, lineno);
  681. char subscr[80], sname[80];
  682. int sawin = 0, event, snums[8], scnt = 0, x;
  683. struct adsi_subscript *sub;
  684. if (!tok) {
  685. ast_log(LOG_WARNING, "Missing event for 'ONEVENT' at line %d of %s\n", lineno, script);
  686. return 0;
  687. }
  688. if ((event = geteventbyname(tok)) < 1) {
  689. ast_log(LOG_WARNING, "'%s' is not a valid event name, at line %d of %s\n", args, lineno, script);
  690. return 0;
  691. }
  692. tok = get_token(&args, script, lineno);
  693. while ((!sawin && !strcasecmp(tok, "IN")) || (sawin && !strcasecmp(tok, "OR"))) {
  694. sawin = 1;
  695. if (scnt > 7) {
  696. ast_log(LOG_WARNING, "No more than 8 states may be specified for inclusion at line %d of %s\n", lineno, script);
  697. return 0;
  698. }
  699. /* Process 'in' things */
  700. tok = get_token(&args, script, lineno);
  701. if (process_token(sname, tok, sizeof(sname), ARG_STRING)) {
  702. ast_log(LOG_WARNING, "'%s' is not a valid state name at line %d of %s\n", tok, lineno, script);
  703. return 0;
  704. }
  705. if ((snums[scnt] = getstatebyname(state, sname, script, lineno, 0) == NULL)) {
  706. ast_log(LOG_WARNING, "State '%s' not declared at line %d of %s\n", sname, lineno, script);
  707. return 0;
  708. }
  709. scnt++;
  710. if (!(tok = get_token(&args, script, lineno)))
  711. break;
  712. }
  713. if (!tok || strcasecmp(tok, "GOTO")) {
  714. if (!tok)
  715. tok = "<nothing>";
  716. if (sawin)
  717. ast_log(LOG_WARNING, "Got '%s' while looking for 'GOTO' or 'OR' at line %d of %s\n", tok, lineno, script);
  718. else
  719. ast_log(LOG_WARNING, "Got '%s' while looking for 'GOTO' or 'IN' at line %d of %s\n", tok, lineno, script);
  720. }
  721. if (!(tok = get_token(&args, script, lineno))) {
  722. ast_log(LOG_WARNING, "Missing subscript to call at line %d of %s\n", lineno, script);
  723. return 0;
  724. }
  725. if (process_token(subscr, tok, sizeof(subscr) - 1, ARG_STRING)) {
  726. ast_log(LOG_WARNING, "Invalid subscript '%s' at line %d of %s\n", tok, lineno, script);
  727. return 0;
  728. }
  729. if (!(sub = getsubbyname(state, subscr, script, lineno)))
  730. return 0;
  731. buf[0] = 8;
  732. buf[1] = event;
  733. buf[2] = sub->id | 0x80;
  734. for (x = 0; x < scnt; x++)
  735. buf[3 + x] = snums[x];
  736. return 3 + scnt;
  737. }
  738. struct adsi_key_cmd {
  739. char *name;
  740. int id;
  741. int (*add_args)(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno);
  742. };
  743. static const struct adsi_key_cmd kcmds[] = {
  744. { "SENDDTMF", 0, send_dtmf },
  745. /* Encoded DTMF would go here */
  746. { "ONHOOK", 0x81 },
  747. { "OFFHOOK", 0x82 },
  748. { "FLASH", 0x83 },
  749. { "WAITDIALTONE", 0x84 },
  750. /* Send line number */
  751. { "BLANK", 0x86 },
  752. { "SENDCHARS", 0x87 },
  753. { "CLEARCHARS", 0x88 },
  754. { "BACKSPACE", 0x89 },
  755. /* Tab column */
  756. { "GOTOLINE", 0x8b, goto_line },
  757. { "GOTOLINEREL", 0x8c, goto_line_rel },
  758. { "PAGEUP", 0x8d },
  759. { "PAGEDOWN", 0x8e },
  760. /* Extended DTMF */
  761. { "DELAY", 0x90, send_delay },
  762. { "DIALPULSEONE", 0x91 },
  763. { "DATAMODE", 0x92 },
  764. { "VOICEMODE", 0x93 },
  765. /* Display call buffer 'n' */
  766. /* Clear call buffer 'n' */
  767. { "CLEARCB1", 0x95, clearcbone },
  768. { "DIGITCOLLECT", 0x96, digitcollect },
  769. { "DIGITDIRECT", 0x96, digitdirect },
  770. { "CLEAR", 0x97 },
  771. { "SHOWDISPLAY", 0x98, showdisplay },
  772. { "CLEARDISPLAY", 0x98, cleardisplay },
  773. { "SHOWKEYS", 0x99, showkeys },
  774. { "SETSTATE", 0x9a, set_state },
  775. { "TIMERSTART", 0x9b, starttimer },
  776. { "TIMERCLEAR", 0x9b, cleartimer },
  777. { "SETFLAG", 0x9c, setflag },
  778. { "CLEARFLAG", 0x9c, clearflag },
  779. { "GOTO", 0x9d, subscript },
  780. { "EVENT22", 0x9e },
  781. { "EVENT23", 0x9f },
  782. { "EXIT", 0xa0 },
  783. };
  784. static const struct adsi_key_cmd opcmds[] = {
  785. /* 1 - Branch on event -- handled specially */
  786. { "SHOWKEYS", 2, showkeys },
  787. /* Display Control */
  788. { "SHOWDISPLAY", 3, showdisplay },
  789. { "CLEARDISPLAY", 3, cleardisplay },
  790. { "CLEAR", 5 },
  791. { "SETSTATE", 6, set_state },
  792. { "TIMERSTART", 7, starttimer },
  793. { "TIMERCLEAR", 7, cleartimer },
  794. { "ONEVENT", 8, onevent },
  795. /* 9 - Subroutine label, treated specially */
  796. { "SETFLAG", 10, setflag },
  797. { "CLEARFLAG", 10, clearflag },
  798. { "DELAY", 11, send_delay },
  799. { "EXIT", 12 },
  800. };
  801. static int process_returncode(struct adsi_soft_key *key, char *code, char *args, struct adsi_script *state, const char *script, int lineno)
  802. {
  803. int x, res;
  804. char *unused;
  805. for (x = 0; x < ARRAY_LEN(kcmds); x++) {
  806. if ((kcmds[x].id > -1) && !strcasecmp(kcmds[x].name, code)) {
  807. if (kcmds[x].add_args) {
  808. res = kcmds[x].add_args(key->retstr + key->retstrlen,
  809. code, kcmds[x].id, args, state, script, lineno);
  810. if ((key->retstrlen + res - key->initlen) <= MAX_RET_CODE)
  811. key->retstrlen += res;
  812. else
  813. ast_log(LOG_WARNING, "No space for '%s' code in key '%s' at line %d of %s\n", kcmds[x].name, key->vname, lineno, script);
  814. } else {
  815. if ((unused = get_token(&args, script, lineno)))
  816. ast_log(LOG_WARNING, "'%s' takes no arguments at line %d of %s (token is '%s')\n", kcmds[x].name, lineno, script, unused);
  817. if ((key->retstrlen + 1 - key->initlen) <= MAX_RET_CODE) {
  818. key->retstr[key->retstrlen] = kcmds[x].id;
  819. key->retstrlen++;
  820. } else
  821. ast_log(LOG_WARNING, "No space for '%s' code in key '%s' at line %d of %s\n", kcmds[x].name, key->vname, lineno, script);
  822. }
  823. return 0;
  824. }
  825. }
  826. return -1;
  827. }
  828. static int process_opcode(struct adsi_subscript *sub, char *code, char *args, struct adsi_script *state, const char *script, int lineno)
  829. {
  830. int x, res, max = sub->id ? MAX_SUB_LEN : MAX_MAIN_LEN;
  831. char *unused;
  832. for (x = 0; x < ARRAY_LEN(opcmds); x++) {
  833. if ((opcmds[x].id > -1) && !strcasecmp(opcmds[x].name, code)) {
  834. if (opcmds[x].add_args) {
  835. res = opcmds[x].add_args(sub->data + sub->datalen,
  836. code, opcmds[x].id, args, state, script, lineno);
  837. if ((sub->datalen + res + 1) <= max)
  838. sub->datalen += res;
  839. else {
  840. ast_log(LOG_WARNING, "No space for '%s' code in subscript '%s' at line %d of %s\n", opcmds[x].name, sub->vname, lineno, script);
  841. return -1;
  842. }
  843. } else {
  844. if ((unused = get_token(&args, script, lineno)))
  845. ast_log(LOG_WARNING, "'%s' takes no arguments at line %d of %s (token is '%s')\n", opcmds[x].name, lineno, script, unused);
  846. if ((sub->datalen + 2) <= max) {
  847. sub->data[sub->datalen] = opcmds[x].id;
  848. sub->datalen++;
  849. } else {
  850. ast_log(LOG_WARNING, "No space for '%s' code in key '%s' at line %d of %s\n", opcmds[x].name, sub->vname, lineno, script);
  851. return -1;
  852. }
  853. }
  854. /* Separate commands with 0xff */
  855. sub->data[sub->datalen] = 0xff;
  856. sub->datalen++;
  857. sub->inscount++;
  858. return 0;
  859. }
  860. }
  861. return -1;
  862. }
  863. static int adsi_process(struct adsi_script *state, char *buf, const char *script, int lineno)
  864. {
  865. char *keyword = get_token(&buf, script, lineno);
  866. char *args, vname[256], tmp[80], tmp2[80];
  867. int lrci, wi, event;
  868. struct adsi_display *disp;
  869. struct adsi_subscript *newsub;
  870. if (!keyword)
  871. return 0;
  872. switch(state->state) {
  873. case STATE_NORMAL:
  874. if (!strcasecmp(keyword, "DESCRIPTION")) {
  875. if ((args = get_token(&buf, script, lineno))) {
  876. if (process_token(state->desc, args, sizeof(state->desc) - 1, ARG_STRING))
  877. ast_log(LOG_WARNING, "'%s' is not a valid token for DESCRIPTION at line %d of %s\n", args, lineno, script);
  878. } else
  879. ast_log(LOG_WARNING, "Missing argument for DESCRIPTION at line %d of %s\n", lineno, script);
  880. } else if (!strcasecmp(keyword, "VERSION")) {
  881. if ((args = get_token(&buf, script, lineno))) {
  882. if (process_token(&state->ver, args, sizeof(state->ver) - 1, ARG_NUMBER))
  883. ast_log(LOG_WARNING, "'%s' is not a valid token for VERSION at line %d of %s\n", args, lineno, script);
  884. } else
  885. ast_log(LOG_WARNING, "Missing argument for VERSION at line %d of %s\n", lineno, script);
  886. } else if (!strcasecmp(keyword, "SECURITY")) {
  887. if ((args = get_token(&buf, script, lineno))) {
  888. if (process_token(state->sec, args, sizeof(state->sec) - 1, ARG_STRING | ARG_NUMBER))
  889. ast_log(LOG_WARNING, "'%s' is not a valid token for SECURITY at line %d of %s\n", args, lineno, script);
  890. } else
  891. ast_log(LOG_WARNING, "Missing argument for SECURITY at line %d of %s\n", lineno, script);
  892. } else if (!strcasecmp(keyword, "FDN")) {
  893. if ((args = get_token(&buf, script, lineno))) {
  894. if (process_token(state->fdn, args, sizeof(state->fdn) - 1, ARG_STRING | ARG_NUMBER))
  895. ast_log(LOG_WARNING, "'%s' is not a valid token for FDN at line %d of %s\n", args, lineno, script);
  896. } else
  897. ast_log(LOG_WARNING, "Missing argument for FDN at line %d of %s\n", lineno, script);
  898. } else if (!strcasecmp(keyword, "KEY")) {
  899. if (!(args = get_token(&buf, script, lineno))) {
  900. ast_log(LOG_WARNING, "KEY definition missing name at line %d of %s\n", lineno, script);
  901. break;
  902. }
  903. if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
  904. ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
  905. break;
  906. }
  907. if (!(state->key = getkeybyname(state, vname, script, lineno))) {
  908. ast_log(LOG_WARNING, "Out of key space at line %d of %s\n", lineno, script);
  909. break;
  910. }
  911. if (state->key->defined) {
  912. ast_log(LOG_WARNING, "Cannot redefine key '%s' at line %d of %s\n", vname, lineno, script);
  913. break;
  914. }
  915. if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) {
  916. ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script);
  917. break;
  918. }
  919. if (!(args = get_token(&buf, script, lineno))) {
  920. ast_log(LOG_WARNING, "KEY definition missing short name at line %d of %s\n", lineno, script);
  921. break;
  922. }
  923. if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
  924. ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY short name at line %d of %s\n", args, lineno, script);
  925. break;
  926. }
  927. if ((args = get_token(&buf, script, lineno))) {
  928. if (strcasecmp(args, "OR")) {
  929. ast_log(LOG_WARNING, "Expecting 'OR' but got '%s' instead at line %d of %s\n", args, lineno, script);
  930. break;
  931. }
  932. if (!(args = get_token(&buf, script, lineno))) {
  933. ast_log(LOG_WARNING, "KEY definition missing optional long name at line %d of %s\n", lineno, script);
  934. break;
  935. }
  936. if (process_token(tmp2, args, sizeof(tmp2) - 1, ARG_STRING)) {
  937. ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY long name at line %d of %s\n", args, lineno, script);
  938. break;
  939. }
  940. } else {
  941. ast_copy_string(tmp2, tmp, sizeof(tmp2));
  942. }
  943. if (strlen(tmp2) > 18) {
  944. ast_log(LOG_WARNING, "Truncating full name to 18 characters at line %d of %s\n", lineno, script);
  945. tmp2[18] = '\0';
  946. }
  947. if (strlen(tmp) > 7) {
  948. ast_log(LOG_WARNING, "Truncating short name to 7 bytes at line %d of %s\n", lineno, script);
  949. tmp[7] = '\0';
  950. }
  951. /* Setup initial stuff */
  952. state->key->retstr[0] = 0x80;
  953. /* 1 has the length */
  954. state->key->retstr[2] = state->key->id;
  955. /* Put the Full name in */
  956. memcpy(state->key->retstr + 3, tmp2, strlen(tmp2));
  957. /* Update length */
  958. state->key->retstrlen = strlen(tmp2) + 3;
  959. /* Put trailing 0xff */
  960. state->key->retstr[state->key->retstrlen++] = 0xff;
  961. /* Put the short name */
  962. memcpy(state->key->retstr + state->key->retstrlen, tmp, strlen(tmp));
  963. /* Update length */
  964. state->key->retstrlen += strlen(tmp);
  965. /* Put trailing 0xff */
  966. state->key->retstr[state->key->retstrlen++] = 0xff;
  967. /* Record initial length */
  968. state->key->initlen = state->key->retstrlen;
  969. state->state = STATE_INKEY;
  970. } else if (!strcasecmp(keyword, "SUB")) {
  971. if (!(args = get_token(&buf, script, lineno))) {
  972. ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script);
  973. break;
  974. }
  975. if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
  976. ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
  977. break;
  978. }
  979. if (!(state->sub = getsubbyname(state, vname, script, lineno))) {
  980. ast_log(LOG_WARNING, "Out of subroutine space at line %d of %s\n", lineno, script);
  981. break;
  982. }
  983. if (state->sub->defined) {
  984. ast_log(LOG_WARNING, "Cannot redefine subroutine '%s' at line %d of %s\n", vname, lineno, script);
  985. break;
  986. }
  987. /* Setup sub */
  988. state->sub->data[0] = 0x82;
  989. /* 1 is the length */
  990. state->sub->data[2] = 0x0; /* Clear extensibility bit */
  991. state->sub->datalen = 3;
  992. if (state->sub->id) {
  993. /* If this isn't the main subroutine, make a subroutine label for it */
  994. state->sub->data[3] = 9;
  995. state->sub->data[4] = state->sub->id;
  996. /* 5 is length */
  997. state->sub->data[6] = 0xff;
  998. state->sub->datalen = 7;
  999. }
  1000. if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) {
  1001. ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script);
  1002. break;
  1003. }
  1004. state->state = STATE_INSUB;
  1005. } else if (!strcasecmp(keyword, "STATE")) {
  1006. if (!(args = get_token(&buf, script, lineno))) {
  1007. ast_log(LOG_WARNING, "STATE definition missing name at line %d of %s\n", lineno, script);
  1008. break;
  1009. }
  1010. if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
  1011. ast_log(LOG_WARNING, "'%s' is not a valid token for a STATE name at line %d of %s\n", args, lineno, script);
  1012. break;
  1013. }
  1014. if (getstatebyname(state, vname, script, lineno, 0)) {
  1015. ast_log(LOG_WARNING, "State '%s' is already defined at line %d of %s\n", vname, lineno, script);
  1016. break;
  1017. }
  1018. getstatebyname(state, vname, script, lineno, 1);
  1019. } else if (!strcasecmp(keyword, "FLAG")) {
  1020. if (!(args = get_token(&buf, script, lineno))) {
  1021. ast_log(LOG_WARNING, "FLAG definition missing name at line %d of %s\n", lineno, script);
  1022. break;
  1023. }
  1024. if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
  1025. ast_log(LOG_WARNING, "'%s' is not a valid token for a FLAG name at line %d of %s\n", args, lineno, script);
  1026. break;
  1027. }
  1028. if (getflagbyname(state, vname, script, lineno, 0)) {
  1029. ast_log(LOG_WARNING, "Flag '%s' is already defined\n", vname);
  1030. break;
  1031. }
  1032. getflagbyname(state, vname, script, lineno, 1);
  1033. } else if (!strcasecmp(keyword, "DISPLAY")) {
  1034. lrci = 0;
  1035. wi = 0;
  1036. if (!(args = get_token(&buf, script, lineno))) {
  1037. ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script);
  1038. break;
  1039. }
  1040. if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
  1041. ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
  1042. break;
  1043. }
  1044. if (getdisplaybyname(state, vname, script, lineno, 0)) {
  1045. ast_log(LOG_WARNING, "State '%s' is already defined\n", vname);
  1046. break;
  1047. }
  1048. if (!(disp = getdisplaybyname(state, vname, script, lineno, 1)))
  1049. break;
  1050. if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) {
  1051. ast_log(LOG_WARNING, "Missing 'IS' at line %d of %s\n", lineno, script);
  1052. break;
  1053. }
  1054. if (!(args = get_token(&buf, script, lineno))) {
  1055. ast_log(LOG_WARNING, "Missing Column 1 text at line %d of %s\n", lineno, script);
  1056. break;
  1057. }
  1058. if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
  1059. ast_log(LOG_WARNING, "Token '%s' is not valid column 1 text at line %d of %s\n", args, lineno, script);
  1060. break;
  1061. }
  1062. if (strlen(tmp) > 20) {
  1063. ast_log(LOG_WARNING, "Truncating column one to 20 characters at line %d of %s\n", lineno, script);
  1064. tmp[20] = '\0';
  1065. }
  1066. memcpy(disp->data + 5, tmp, strlen(tmp));
  1067. disp->datalen = strlen(tmp) + 5;
  1068. disp->data[disp->datalen++] = 0xff;
  1069. args = get_token(&buf, script, lineno);
  1070. if (args && !process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
  1071. /* Got a column two */
  1072. if (strlen(tmp) > 20) {
  1073. ast_log(LOG_WARNING, "Truncating column two to 20 characters at line %d of %s\n", lineno, script);
  1074. tmp[20] = '\0';
  1075. }
  1076. memcpy(disp->data + disp->datalen, tmp, strlen(tmp));
  1077. disp->datalen += strlen(tmp);
  1078. args = get_token(&buf, script, lineno);
  1079. }
  1080. while (args) {
  1081. if (!strcasecmp(args, "JUSTIFY")) {
  1082. args = get_token(&buf, script, lineno);
  1083. if (!args) {
  1084. ast_log(LOG_WARNING, "Qualifier 'JUSTIFY' requires an argument at line %d of %s\n", lineno, script);
  1085. break;
  1086. }
  1087. lrci = getjustifybyname(args);
  1088. if (lrci < 0) {
  1089. ast_log(LOG_WARNING, "'%s' is not a valid justification at line %d of %s\n", args, lineno, script);
  1090. break;
  1091. }
  1092. } else if (!strcasecmp(args, "WRAP")) {
  1093. wi = 0x80;
  1094. } else {
  1095. ast_log(LOG_WARNING, "'%s' is not a known qualifier at line %d of %s\n", args, lineno, script);
  1096. break;
  1097. }
  1098. args = get_token(&buf, script, lineno);
  1099. }
  1100. if (args) {
  1101. /* Something bad happened */
  1102. break;
  1103. }
  1104. disp->data[0] = 0x81;
  1105. disp->data[1] = disp->datalen - 2;
  1106. disp->data[2] = ((lrci & 0x3) << 6) | disp->id;
  1107. disp->data[3] = wi;
  1108. disp->data[4] = 0xff;
  1109. } else {
  1110. ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in PROGRAM\n", keyword);
  1111. }
  1112. break;
  1113. case STATE_INKEY:
  1114. if (process_returncode(state->key, keyword, buf, state, script, lineno)) {
  1115. if (!strcasecmp(keyword, "ENDKEY")) {
  1116. /* Return to normal operation and increment current key */
  1117. state->state = STATE_NORMAL;
  1118. state->key->defined = 1;
  1119. state->key->retstr[1] = state->key->retstrlen - 2;
  1120. state->key = NULL;
  1121. } else {
  1122. ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SOFTKEY definition at line %d of %s\n", keyword, lineno, script);
  1123. }
  1124. }
  1125. break;
  1126. case STATE_INIF:
  1127. if (process_opcode(state->sub, keyword, buf, state, script, lineno)) {
  1128. if (!strcasecmp(keyword, "ENDIF")) {
  1129. /* Return to normal SUB operation and increment current key */
  1130. state->state = STATE_INSUB;
  1131. state->sub->defined = 1;
  1132. /* Store the proper number of instructions */
  1133. state->sub->ifdata[2] = state->sub->ifinscount;
  1134. } else if (!strcasecmp(keyword, "GOTO")) {
  1135. if (!(args = get_token(&buf, script, lineno))) {
  1136. ast_log(LOG_WARNING, "GOTO clause missing Subscript name at line %d of %s\n", lineno, script);
  1137. break;
  1138. }
  1139. if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
  1140. ast_log(LOG_WARNING, "'%s' is not a valid subscript name token at line %d of %s\n", args, lineno, script);
  1141. break;
  1142. }
  1143. if (!(newsub = getsubbyname(state, tmp, script, lineno)))
  1144. break;
  1145. /* Somehow you use GOTO to go to another place */
  1146. state->sub->data[state->sub->datalen++] = 0x8;
  1147. state->sub->data[state->sub->datalen++] = state->sub->ifdata[1];
  1148. state->sub->data[state->sub->datalen++] = newsub->id;
  1149. /* Terminate */
  1150. state->sub->data[state->sub->datalen++] = 0xff;
  1151. /* Increment counters */
  1152. state->sub->inscount++;
  1153. state->sub->ifinscount++;
  1154. } else {
  1155. ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in IF clause at line %d of %s\n", keyword, lineno, script);
  1156. }
  1157. } else
  1158. state->sub->ifinscount++;
  1159. break;
  1160. case STATE_INSUB:
  1161. if (process_opcode(state->sub, keyword, buf, state, script, lineno)) {
  1162. if (!strcasecmp(keyword, "ENDSUB")) {
  1163. /* Return to normal operation and increment current key */
  1164. state->state = STATE_NORMAL;
  1165. state->sub->defined = 1;
  1166. /* Store the proper length */
  1167. state->sub->data[1] = state->sub->datalen - 2;
  1168. if (state->sub->id) {
  1169. /* if this isn't main, store number of instructions, too */
  1170. state->sub->data[5] = state->sub->inscount;
  1171. }
  1172. state->sub = NULL;
  1173. } else if (!strcasecmp(keyword, "IFEVENT")) {
  1174. if (!(args = get_token(&buf, script, lineno))) {
  1175. ast_log(LOG_WARNING, "IFEVENT clause missing Event name at line %d of %s\n", lineno, script);
  1176. break;
  1177. }
  1178. if ((event = geteventbyname(args)) < 1) {
  1179. ast_log(LOG_WARNING, "'%s' is not a valid event\n", args);
  1180. break;
  1181. }
  1182. if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "THEN")) {
  1183. ast_log(LOG_WARNING, "IFEVENT clause missing 'THEN' at line %d of %s\n", lineno, script);
  1184. break;
  1185. }
  1186. state->sub->ifinscount = 0;
  1187. state->sub->ifdata = state->sub->data + state->sub->datalen;
  1188. /* Reserve header and insert op codes */
  1189. state->sub->ifdata[0] = 0x1;
  1190. state->sub->ifdata[1] = event;
  1191. /* 2 is for the number of instructions */
  1192. state->sub->ifdata[3] = 0xff;
  1193. state->sub->datalen += 4;
  1194. /* Update Subscript instruction count */
  1195. state->sub->inscount++;
  1196. state->state = STATE_INIF;
  1197. } else {
  1198. ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SUB definition at line %d of %s\n", keyword, lineno, script);
  1199. }
  1200. }
  1201. break;
  1202. default:
  1203. ast_log(LOG_WARNING, "Can't process keyword '%s' in weird state %d\n", keyword, state->state);
  1204. }
  1205. return 0;
  1206. }
  1207. static struct adsi_script *compile_script(const char *script)
  1208. {
  1209. FILE *f;
  1210. char fn[256], buf[256], *c;
  1211. int lineno = 0, x, err;
  1212. struct adsi_script *scr;
  1213. if (script[0] == '/')
  1214. ast_copy_string(fn, script, sizeof(fn));
  1215. else
  1216. snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, script);
  1217. if (!(f = fopen(fn, "r"))) {
  1218. ast_log(LOG_WARNING, "Can't open file '%s'\n", fn);
  1219. return NULL;
  1220. }
  1221. if (!(scr = ast_calloc(1, sizeof(*scr)))) {
  1222. fclose(f);
  1223. return NULL;
  1224. }
  1225. /* Create "main" as first subroutine */
  1226. getsubbyname(scr, "main", NULL, 0);
  1227. while (!feof(f)) {
  1228. if (!fgets(buf, sizeof(buf), f)) {
  1229. continue;
  1230. }
  1231. if (!feof(f)) {
  1232. lineno++;
  1233. /* Trim off trailing return */
  1234. buf[strlen(buf) - 1] = '\0';
  1235. /* Strip comments */
  1236. if ((c = strchr(buf, ';')))
  1237. *c = '\0';
  1238. if (!ast_strlen_zero(buf))
  1239. adsi_process(scr, buf, script, lineno);
  1240. }
  1241. }
  1242. fclose(f);
  1243. /* Make sure we're in the main routine again */
  1244. switch(scr->state) {
  1245. case STATE_NORMAL:
  1246. break;
  1247. case STATE_INSUB:
  1248. ast_log(LOG_WARNING, "Missing ENDSUB at end of file %s\n", script);
  1249. ast_free(scr);
  1250. return NULL;
  1251. case STATE_INKEY:
  1252. ast_log(LOG_WARNING, "Missing ENDKEY at end of file %s\n", script);
  1253. ast_free(scr);
  1254. return NULL;
  1255. }
  1256. err = 0;
  1257. /* Resolve all keys and record their lengths */
  1258. for (x = 0; x < scr->numkeys; x++) {
  1259. if (!scr->keys[x].defined) {
  1260. ast_log(LOG_WARNING, "Key '%s' referenced but never defined in file %s\n", scr->keys[x].vname, fn);
  1261. err++;
  1262. }
  1263. }
  1264. /* Resolve all subs */
  1265. for (x = 0; x < scr->numsubs; x++) {
  1266. if (!scr->subs[x].defined) {
  1267. ast_log(LOG_WARNING, "Subscript '%s' referenced but never defined in file %s\n", scr->subs[x].vname, fn);
  1268. err++;
  1269. }
  1270. if (x == (scr->numsubs - 1)) {
  1271. /* Clear out extension bit on last message */
  1272. scr->subs[x].data[2] = 0x80;
  1273. }
  1274. }
  1275. if (err) {
  1276. ast_free(scr);
  1277. return NULL;
  1278. }
  1279. return scr;
  1280. }
  1281. #ifdef DUMP_MESSAGES
  1282. static void dump_message(char *type, char *vname, unsigned char *buf, int buflen)
  1283. {
  1284. int x;
  1285. printf("%s %s: [ ", type, vname);
  1286. for (x = 0; x < buflen; x++)
  1287. printf("%02hhx ", buf[x]);
  1288. printf("]\n");
  1289. }
  1290. #endif
  1291. static int adsi_prog(struct ast_channel *chan, const char *script)
  1292. {
  1293. struct adsi_script *scr;
  1294. int x, bytes;
  1295. unsigned char buf[1024];
  1296. if (!(scr = compile_script(script)))
  1297. return -1;
  1298. /* Start an empty ADSI Session */
  1299. if (ast_adsi_load_session(chan, NULL, 0, 1) < 1)
  1300. return -1;
  1301. /* Now begin the download attempt */
  1302. if (ast_adsi_begin_download(chan, scr->desc, scr->fdn, scr->sec, scr->ver)) {
  1303. /* User rejected us for some reason */
  1304. ast_verb(3, "User rejected download attempt\n");
  1305. ast_log(LOG_NOTICE, "User rejected download on channel %s\n", ast_channel_name(chan));
  1306. ast_free(scr);
  1307. return -1;
  1308. }
  1309. bytes = 0;
  1310. /* Start with key definitions */
  1311. for (x = 0; x < scr->numkeys; x++) {
  1312. if (bytes + scr->keys[x].retstrlen > 253) {
  1313. /* Send what we've collected so far */
  1314. if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
  1315. ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
  1316. return -1;
  1317. }
  1318. bytes =0;
  1319. }
  1320. memcpy(buf + bytes, scr->keys[x].retstr, scr->keys[x].retstrlen);
  1321. bytes += scr->keys[x].retstrlen;
  1322. #ifdef DUMP_MESSAGES
  1323. dump_message("Key", scr->keys[x].vname, scr->keys[x].retstr, scr->keys[x].retstrlen);
  1324. #endif
  1325. }
  1326. if (bytes) {
  1327. if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
  1328. ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
  1329. return -1;
  1330. }
  1331. }
  1332. bytes = 0;
  1333. /* Continue with the display messages */
  1334. for (x = 0; x < scr->numdisplays; x++) {
  1335. if (bytes + scr->displays[x].datalen > 253) {
  1336. /* Send what we've collected so far */
  1337. if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
  1338. ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
  1339. return -1;
  1340. }
  1341. bytes =0;
  1342. }
  1343. memcpy(buf + bytes, scr->displays[x].data, scr->displays[x].datalen);
  1344. bytes += scr->displays[x].datalen;
  1345. #ifdef DUMP_MESSAGES
  1346. dump_message("Display", scr->displays[x].vname, scr->displays[x].data, scr->displays[x].datalen);
  1347. #endif
  1348. }
  1349. if (bytes) {
  1350. if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
  1351. ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
  1352. return -1;
  1353. }
  1354. }
  1355. bytes = 0;
  1356. /* Send subroutines */
  1357. for (x = 0; x < scr->numsubs; x++) {
  1358. if (bytes + scr->subs[x].datalen > 253) {
  1359. /* Send what we've collected so far */
  1360. if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
  1361. ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
  1362. return -1;
  1363. }
  1364. bytes =0;
  1365. }
  1366. memcpy(buf + bytes, scr->subs[x].data, scr->subs[x].datalen);
  1367. bytes += scr->subs[x].datalen;
  1368. #ifdef DUMP_MESSAGES
  1369. dump_message("Sub", scr->subs[x].vname, scr->subs[x].data, scr->subs[x].datalen);
  1370. #endif
  1371. }
  1372. if (bytes) {
  1373. if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
  1374. ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
  1375. return -1;
  1376. }
  1377. }
  1378. bytes = 0;
  1379. bytes += ast_adsi_display(buf, ADSI_INFO_PAGE, 1, ADSI_JUST_LEFT, 0, "Download complete.", "");
  1380. bytes += ast_adsi_set_line(buf, ADSI_INFO_PAGE, 1);
  1381. if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY) < 0)
  1382. return -1;
  1383. if (ast_adsi_end_download(chan)) {
  1384. /* Download failed for some reason */
  1385. ast_verb(3, "Download attempt failed\n");
  1386. ast_log(LOG_NOTICE, "Download failed on %s\n", ast_channel_name(chan));
  1387. ast_free(scr);
  1388. return -1;
  1389. }
  1390. ast_free(scr);
  1391. ast_adsi_unload_session(chan);
  1392. return 0;
  1393. }
  1394. static int adsi_exec(struct ast_channel *chan, const char *data)
  1395. {
  1396. int res = 0;
  1397. if (ast_strlen_zero(data))
  1398. data = "asterisk.adsi";
  1399. if (!ast_adsi_available(chan)) {
  1400. ast_verb(3, "ADSI Unavailable on CPE. Not bothering to try.\n");
  1401. } else {
  1402. ast_verb(3, "ADSI Available on CPE. Attempting Upload.\n");
  1403. res = adsi_prog(chan, data);
  1404. }
  1405. return res;
  1406. }
  1407. static int unload_module(void)
  1408. {
  1409. return ast_unregister_application(app);
  1410. }
  1411. /*!
  1412. * \brief Load the module
  1413. *
  1414. * Module loading including tests for configuration or dependencies.
  1415. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1416. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1417. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1418. * configuration file or other non-critical problem return
  1419. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1420. */
  1421. static int load_module(void)
  1422. {
  1423. if (ast_register_application_xml(app, adsi_exec))
  1424. return AST_MODULE_LOAD_DECLINE;
  1425. return AST_MODULE_LOAD_SUCCESS;
  1426. }
  1427. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk ADSI Programming Application",
  1428. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  1429. .load = load_module,
  1430. .unload = unload_module,
  1431. );