Newer
Older
return -1;
}
}
/* Separate commands with 0xff */
sub->data[sub->datalen] = 0xff;
sub->datalen++;
sub->inscount++;
return 0;
}
}
return -1;
}
static int adsi_process(struct adsi_script *state, char *buf, const char *script, int lineno)
char *keyword = get_token(&buf, script, lineno);
char *args, vname[256], tmp[80], tmp2[80];
int lrci, wi, event;
struct adsi_display *disp;
struct adsi_subscript *newsub;
switch(state->state) {
case STATE_NORMAL:
if (!strcasecmp(keyword, "DESCRIPTION")) {
if ((args = get_token(&buf, script, lineno))) {
if (process_token(state->desc, args, sizeof(state->desc) - 1, ARG_STRING))
ast_log(LOG_WARNING, "'%s' is not a valid token for DESCRIPTION at line %d of %s\n", args, lineno, script);
} else
ast_log(LOG_WARNING, "Missing argument for DESCRIPTION at line %d of %s\n", lineno, script);
} else if (!strcasecmp(keyword, "VERSION")) {
if ((args = get_token(&buf, script, lineno))) {
if (process_token(&state->ver, args, sizeof(state->ver) - 1, ARG_NUMBER))
ast_log(LOG_WARNING, "'%s' is not a valid token for VERSION at line %d of %s\n", args, lineno, script);
} else
ast_log(LOG_WARNING, "Missing argument for VERSION at line %d of %s\n", lineno, script);
} else if (!strcasecmp(keyword, "SECURITY")) {
if ((args = get_token(&buf, script, lineno))) {
if (process_token(state->sec, args, sizeof(state->sec) - 1, ARG_STRING | ARG_NUMBER))
ast_log(LOG_WARNING, "'%s' is not a valid token for SECURITY at line %d of %s\n", args, lineno, script);
} else
ast_log(LOG_WARNING, "Missing argument for SECURITY at line %d of %s\n", lineno, script);
} else if (!strcasecmp(keyword, "FDN")) {
if ((args = get_token(&buf, script, lineno))) {
if (process_token(state->fdn, args, sizeof(state->fdn) - 1, ARG_STRING | ARG_NUMBER))
ast_log(LOG_WARNING, "'%s' is not a valid token for FDN at line %d of %s\n", args, lineno, script);
} else
ast_log(LOG_WARNING, "Missing argument for FDN at line %d of %s\n", lineno, script);
} else if (!strcasecmp(keyword, "KEY")) {
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "KEY definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
if (!(state->key = getkeybyname(state, vname, script, lineno))) {
ast_log(LOG_WARNING, "Out of key space at line %d of %s\n", lineno, script);
break;
}
if (state->key->defined) {
ast_log(LOG_WARNING, "Cannot redefine key '%s' at line %d of %s\n", vname, lineno, script);
break;
}
if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) {
ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script);
break;
}
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "KEY definition missing short name at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY short name at line %d of %s\n", args, lineno, script);
if ((args = get_token(&buf, script, lineno))) {
if (strcasecmp(args, "OR")) {
ast_log(LOG_WARNING, "Expecting 'OR' but got '%s' instead at line %d of %s\n", args, lineno, script);
break;
}
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "KEY definition missing optional long name at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp2, args, sizeof(tmp2) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY long name at line %d of %s\n", args, lineno, script);
ast_copy_string(tmp2, tmp, sizeof(tmp2));
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
}
if (strlen(tmp2) > 18) {
ast_log(LOG_WARNING, "Truncating full name to 18 characters at line %d of %s\n", lineno, script);
tmp2[18] = '\0';
}
if (strlen(tmp) > 7) {
ast_log(LOG_WARNING, "Truncating short name to 7 bytes at line %d of %s\n", lineno, script);
tmp[7] = '\0';
}
/* Setup initial stuff */
state->key->retstr[0] = 128;
/* 1 has the length */
state->key->retstr[2] = state->key->id;
/* Put the Full name in */
memcpy(state->key->retstr + 3, tmp2, strlen(tmp2));
/* Update length */
state->key->retstrlen = strlen(tmp2) + 3;
/* Put trailing 0xff */
state->key->retstr[state->key->retstrlen++] = 0xff;
/* Put the short name */
memcpy(state->key->retstr + state->key->retstrlen, tmp, strlen(tmp));
/* Update length */
state->key->retstrlen += strlen(tmp);
/* Put trailing 0xff */
state->key->retstr[state->key->retstrlen++] = 0xff;
/* Record initial length */
state->key->initlen = state->key->retstrlen;
state->state = STATE_INKEY;
} else if (!strcasecmp(keyword, "SUB")) {
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
if (!(state->sub = getsubbyname(state, vname, script, lineno))) {
ast_log(LOG_WARNING, "Out of subroutine space at line %d of %s\n", lineno, script);
break;
}
if (state->sub->defined) {
ast_log(LOG_WARNING, "Cannot redefine subroutine '%s' at line %d of %s\n", vname, lineno, script);
break;
}
/* Setup sub */
state->sub->data[0] = 130;
/* 1 is the length */
state->sub->data[2] = 0x0; /* Clear extensibility bit */
state->sub->datalen = 3;
if (state->sub->id) {
/* If this isn't the main subroutine, make a subroutine label for it */
state->sub->data[3] = 9;
state->sub->data[4] = state->sub->id;
/* 5 is length */
state->sub->data[6] = 0xff;
state->sub->datalen = 7;
}
if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) {
ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script);
break;
}
state->state = STATE_INSUB;
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "STATE definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a STATE name at line %d of %s\n", args, lineno, script);
break;
}
if (getstatebyname(state, vname, script, lineno, 0)) {
ast_log(LOG_WARNING, "State '%s' is already defined at line %d of %s\n", vname, lineno, script);
break;
}
getstatebyname(state, vname, script, lineno, 1);
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "FLAG definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a FLAG name at line %d of %s\n", args, lineno, script);
break;
}
if (getflagbyname(state, vname, script, lineno, 0)) {
ast_log(LOG_WARNING, "Flag '%s' is already defined\n", vname);
break;
}
getflagbyname(state, vname, script, lineno, 1);
} else if (!strcasecmp(keyword, "DISPLAY")) {
lrci = 0;
wi = 0;
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script);
break;
}
if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script);
break;
}
if (getdisplaybyname(state, vname, script, lineno, 0)) {
ast_log(LOG_WARNING, "State '%s' is already defined\n", vname);
break;
}
if (!(disp = getdisplaybyname(state, vname, script, lineno, 1)))
if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) {
ast_log(LOG_WARNING, "Missing 'IS' at line %d of %s\n", lineno, script);
break;
}
if (!(args = get_token(&buf, script, lineno))) {
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
ast_log(LOG_WARNING, "Missing Column 1 text at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "Token '%s' is not valid column 1 text at line %d of %s\n", args, lineno, script);
break;
}
if (strlen(tmp) > 20) {
ast_log(LOG_WARNING, "Truncating column one to 20 characters at line %d of %s\n", lineno, script);
tmp[20] = '\0';
}
memcpy(disp->data + 5, tmp, strlen(tmp));
disp->datalen = strlen(tmp) + 5;
disp->data[disp->datalen++] = 0xff;
args = get_token(&buf, script, lineno);
if (args && !process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
/* Got a column two */
if (strlen(tmp) > 20) {
ast_log(LOG_WARNING, "Truncating column two to 20 characters at line %d of %s\n", lineno, script);
tmp[20] = '\0';
}
memcpy(disp->data + disp->datalen, tmp, strlen(tmp));
disp->datalen += strlen(tmp);
args = get_token(&buf, script, lineno);
}
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
if (!strcasecmp(args, "JUSTIFY")) {
args = get_token(&buf, script, lineno);
if (!args) {
ast_log(LOG_WARNING, "Qualifier 'JUSTIFY' requires an argument at line %d of %s\n", lineno, script);
break;
}
lrci = getjustifybyname(args);
if (lrci < 0) {
ast_log(LOG_WARNING, "'%s' is not a valid justification at line %d of %s\n", args, lineno, script);
break;
}
} else if (!strcasecmp(args, "WRAP")) {
wi = 0x80;
} else {
ast_log(LOG_WARNING, "'%s' is not a known qualifier at line %d of %s\n", args, lineno, script);
break;
}
args = get_token(&buf, script, lineno);
}
if (args) {
/* Something bad happened */
break;
}
disp->data[0] = 129;
disp->data[1] = disp->datalen - 2;
disp->data[2] = ((lrci & 0x3) << 6) | disp->id;
disp->data[3] = wi;
disp->data[4] = 0xff;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in PROGRAM\n", keyword);
}
break;
case STATE_INKEY:
if (process_returncode(state->key, keyword, buf, state, script, lineno)) {
if (!strcasecmp(keyword, "ENDKEY")) {
/* Return to normal operation and increment current key */
state->state = STATE_NORMAL;
state->key->defined = 1;
state->key->retstr[1] = state->key->retstrlen - 2;
state->key = NULL;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SOFTKEY definition at line %d of %s\n", keyword, lineno, script);
}
}
break;
case STATE_INIF:
if (process_opcode(state->sub, keyword, buf, state, script, lineno)) {
if (!strcasecmp(keyword, "ENDIF")) {
/* Return to normal SUB operation and increment current key */
state->state = STATE_INSUB;
state->sub->defined = 1;
/* Store the proper number of instructions */
state->sub->ifdata[2] = state->sub->ifinscount;
} else if (!strcasecmp(keyword, "GOTO")) {
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "GOTO clause missing Subscript name at line %d of %s\n", lineno, script);
break;
}
if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) {
ast_log(LOG_WARNING, "'%s' is not a valid subscript name token at line %d of %s\n", args, lineno, script);
break;
}
if (!(newsub = getsubbyname(state, tmp, script, lineno)))
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
break;
/* Somehow you use GOTO to go to another place */
state->sub->data[state->sub->datalen++] = 0x8;
state->sub->data[state->sub->datalen++] = state->sub->ifdata[1];
state->sub->data[state->sub->datalen++] = newsub->id;
/* Terminate */
state->sub->data[state->sub->datalen++] = 0xff;
/* Increment counters */
state->sub->inscount++;
state->sub->ifinscount++;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in IF clause at line %d of %s\n", keyword, lineno, script);
}
} else
state->sub->ifinscount++;
break;
case STATE_INSUB:
if (process_opcode(state->sub, keyword, buf, state, script, lineno)) {
if (!strcasecmp(keyword, "ENDSUB")) {
/* Return to normal operation and increment current key */
state->state = STATE_NORMAL;
state->sub->defined = 1;
/* Store the proper length */
state->sub->data[1] = state->sub->datalen - 2;
if (state->sub->id) {
/* if this isn't main, store number of instructions, too */
state->sub->data[5] = state->sub->inscount;
}
state->sub = NULL;
} else if (!strcasecmp(keyword, "IFEVENT")) {
if (!(args = get_token(&buf, script, lineno))) {
ast_log(LOG_WARNING, "IFEVENT clause missing Event name at line %d of %s\n", lineno, script);
break;
}
ast_log(LOG_WARNING, "'%s' is not a valid event\n", args);
break;
}
if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "THEN")) {
ast_log(LOG_WARNING, "IFEVENT clause missing 'THEN' at line %d of %s\n", lineno, script);
break;
}
state->sub->ifinscount = 0;
state->sub->ifdata = state->sub->data + state->sub->datalen;
/* Reserve header and insert op codes */
state->sub->ifdata[0] = 0x1;
state->sub->ifdata[1] = event;
/* 2 is for the number of instructions */
state->sub->ifdata[3] = 0xff;
state->sub->datalen += 4;
/* Update Subscript instruction count */
state->sub->inscount++;
state->state = STATE_INIF;
} else {
ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SUB definition at line %d of %s\n", keyword, lineno, script);
}
}
break;
default:
ast_log(LOG_WARNING, "Can't process keyword '%s' in weird state %d\n", keyword, state->state);
}
return 0;
}
static struct adsi_script *compile_script(const char *script)
char fn[256], buf[256], *c;
int lineno = 0, x, err;
ast_copy_string(fn, script, sizeof(fn));
snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, script);
ast_log(LOG_WARNING, "Can't open file '%s'\n", fn);
return NULL;
}
if (!(scr = ast_calloc(1, sizeof(*scr)))) {
/* Create "main" as first subroutine */
getsubbyname(scr, "main", NULL, 0);
Kevin P. Fleming
committed
if (!fgets(buf, sizeof(buf), f)) {
continue;
}
if (!feof(f)) {
lineno++;
/* Trim off trailing return */
buf[strlen(buf) - 1] = '\0';
/* Strip comments */
Mark Spencer
committed
if (!ast_strlen_zero(buf))
adsi_process(scr, buf, script, lineno);
}
}
fclose(f);
/* Make sure we're in the main routine again */
switch(scr->state) {
case STATE_NORMAL:
break;
case STATE_INSUB:
ast_log(LOG_WARNING, "Missing ENDSUB at end of file %s\n", script);
Tilghman Lesher
committed
ast_free(scr);
return NULL;
case STATE_INKEY:
ast_log(LOG_WARNING, "Missing ENDKEY at end of file %s\n", script);
Tilghman Lesher
committed
ast_free(scr);
return NULL;
}
err = 0;
/* Resolve all keys and record their lengths */
if (!scr->keys[x].defined) {
ast_log(LOG_WARNING, "Key '%s' referenced but never defined in file %s\n", scr->keys[x].vname, fn);
err++;
}
}
/* Resolve all subs */
if (!scr->subs[x].defined) {
ast_log(LOG_WARNING, "Subscript '%s' referenced but never defined in file %s\n", scr->subs[x].vname, fn);
err++;
}
if (x == (scr->numsubs - 1)) {
/* Clear out extension bit on last message */
scr->subs[x].data[2] = 0x80;
}
}
if (err) {
Tilghman Lesher
committed
ast_free(scr);
return NULL;
}
return scr;
}
#ifdef DUMP_MESSAGES
static void dump_message(char *type, char *vname, unsigned char *buf, int buflen)
{
int x;
printf("%s %s: [ ", type, vname);
printf("%02x ", buf[x]);
printf("]\n");
}
#endif
static int adsi_prog(struct ast_channel *chan, const char *script)
unsigned char buf[1024];
return -1;
/* Start an empty ADSI Session */
if (ast_adsi_load_session(chan, NULL, 0, 1) < 1)
return -1;
/* Now begin the download attempt */
if (ast_adsi_begin_download(chan, scr->desc, scr->fdn, scr->sec, scr->ver)) {
ast_verb(3, "User rejected download attempt\n");
ast_log(LOG_NOTICE, "User rejected download on channel %s\n", ast_channel_name(chan));
Tilghman Lesher
committed
ast_free(scr);
return -1;
}
bytes = 0;
/* Start with key definitions */
if (bytes + scr->keys[x].retstrlen > 253) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
bytes =0;
}
memcpy(buf + bytes, scr->keys[x].retstr, scr->keys[x].retstrlen);
bytes += scr->keys[x].retstrlen;
#ifdef DUMP_MESSAGES
dump_message("Key", scr->keys[x].vname, scr->keys[x].retstr, scr->keys[x].retstrlen);
#endif
}
if (bytes) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
}
bytes = 0;
/* Continue with the display messages */
if (bytes + scr->displays[x].datalen > 253) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
bytes =0;
}
memcpy(buf + bytes, scr->displays[x].data, scr->displays[x].datalen);
bytes += scr->displays[x].datalen;
#ifdef DUMP_MESSAGES
dump_message("Display", scr->displays[x].vname, scr->displays[x].data, scr->displays[x].datalen);
#endif
}
if (bytes) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
}
bytes = 0;
/* Send subroutines */
if (bytes + scr->subs[x].datalen > 253) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
bytes =0;
}
memcpy(buf + bytes, scr->subs[x].data, scr->subs[x].datalen);
bytes += scr->subs[x].datalen;
#ifdef DUMP_MESSAGES
dump_message("Sub", scr->subs[x].vname, scr->subs[x].data, scr->subs[x].datalen);
#endif
}
if (bytes) {
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) {
ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x);
return -1;
}
}
bytes = 0;
bytes += ast_adsi_display(buf, ADSI_INFO_PAGE, 1, ADSI_JUST_LEFT, 0, "Download complete.", "");
bytes += ast_adsi_set_line(buf, ADSI_INFO_PAGE, 1);
if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY) < 0)
if (ast_adsi_end_download(chan)) {
ast_verb(3, "Download attempt failed\n");
ast_log(LOG_NOTICE, "Download failed on %s\n", ast_channel_name(chan));
Tilghman Lesher
committed
ast_free(scr);
Tilghman Lesher
committed
ast_free(scr);
ast_adsi_unload_session(chan);
static int adsi_exec(struct ast_channel *chan, const char *data)
int res = 0;
Russell Bryant
committed
if (ast_strlen_zero(data))
if (!ast_adsi_available(chan)) {
ast_verb(3, "ADSI Unavailable on CPE. Not bothering to try.\n");
ast_verb(3, "ADSI Available on CPE. Attempting Upload.\n");
static int unload_module(void)
return ast_unregister_application(app);
static int load_module(void)
if (ast_register_application_xml(app, adsi_exec))
return AST_MODULE_LOAD_FAILURE;
return AST_MODULE_LOAD_SUCCESS;
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk ADSI Programming Application",
.load = load_module,
.unload = unload_module,
.nonoptreq = "res_adsi",
);