app_adsiprog.c 47 KB

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