Пример #1
0
static int flash_exec(struct ast_channel *chan, void *data)
{
	int res = -1;
	int x;
	struct dahdi_params dahdip;

	if (strcasecmp(chan->tech->type, "DAHDI")) {
		ast_log(LOG_WARNING, "%s is not a DAHDI channel\n", chan->name);
		return -1;
	}
	
	memset(&dahdip, 0, sizeof(dahdip));
	res = ioctl(chan->fds[0], DAHDI_GET_PARAMS, &dahdip);
	if (!res) {
		if (dahdip.sigtype & __DAHDI_SIG_FXS) {
			x = DAHDI_FLASH;
			res = ioctl(chan->fds[0], DAHDI_HOOK, &x);
			if (!res || (errno == EINPROGRESS)) {
				if (res) {
					/* Wait for the event to finish */
					dahdi_wait_event(chan->fds[0]);
				}
				res = ast_safe_sleep(chan, 1000);
				ast_verb(3, "Flashed channel %s\n", chan->name);
			} else
				ast_log(LOG_WARNING, "Unable to flash channel %s: %s\n", chan->name, strerror(errno));
		} else
			ast_log(LOG_WARNING, "%s is not an FXO Channel\n", chan->name);
	} else
		ast_log(LOG_WARNING, "Unable to get parameters of %s: %s\n", chan->name, strerror(errno));

	return res;
}
Пример #2
0
static int flash_exec(struct ast_channel *chan, void *data)
{
	int res = -1;
	int x;
	struct localuser *u;
	struct zt_params ztp;
	LOCAL_USER_ADD(u);
	if (!strcasecmp(chan->type, "Zap")) {
		memset(&ztp, 0, sizeof(ztp));
		res = ioctl(chan->fds[0], ZT_GET_PARAMS, &ztp);
		if (!res) {
			if (ztp.sigtype & __ZT_SIG_FXS) {
				x = ZT_FLASH;
				res = ioctl(chan->fds[0], ZT_HOOK, &x);
				if (!res || (errno == EINPROGRESS)) {
					if (res) {
						/* Wait for the event to finish */
						zt_wait_event(chan->fds[0]);
					}
					res = ast_safe_sleep(chan, 1000);
					if (option_verbose > 2)
						ast_verbose(VERBOSE_PREFIX_3 "Flashed channel %s\n", chan->name);
				} else
					ast_log(LOG_WARNING, "Unable to flash channel %s: %s\n", chan->name, strerror(errno));
			} else
				ast_log(LOG_WARNING, "%s is not an FXO Channel\n", chan->name);
		} else
			ast_log(LOG_WARNING, "Unable to get parameters of %s: %s\n", chan->name, strerror(errno));
	} else
		ast_log(LOG_WARNING, "%s is not a Zap channel\n", chan->name);
	LOCAL_USER_REMOVE(u);
	return res;
}
Пример #3
0
static int waituntil_exec(struct ast_channel *chan, void *data)
{
	int res;
	double fraction;
	struct timeval future = { 0, };
	struct timeval tv = ast_tvnow();
	int msec;

	if (ast_strlen_zero(data)) {
		ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
		pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
		return 0;
	}

	if (sscanf(data, "%ld%lf", (long *)&future.tv_sec, &fraction) == 0) {
		ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
		pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
		return 0;
	}

	future.tv_usec = fraction * 1000000;

	if ((msec = ast_tvdiff_ms(future, tv)) < 0) {
		ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)tv.tv_sec, (long)future.tv_sec);
		pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
		return 0;
	}

	if ((res = ast_safe_sleep(chan, msec)))
		pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
	else
		pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");

	return res;
}
Пример #4
0
static void playtone(struct ast_channel *chan, int tone, int len)
{
	char dtmf[20];
	snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
	ast_playtones_start(chan, 0, dtmf, 0);
	ast_safe_sleep(chan, len);
	ast_playtones_stop(chan);
}
Пример #5
0
/*!
 * \brief Send a single tone burst for a specified duration and frequency.
 * \since 11.0
 *
 * \param chan Asterisk Channel
 * \param tone_freq Frequency of the tone to send
 * \param tone_duration Tone duration in ms
 * \param delay Delay before sending the tone
 *
 * \retval 0 success
 * \retval -1 failure
 */
static int send_tone_burst(struct ast_channel *chan, const char *tone_freq, int tone_duration, int delay)
{
	if (delay && ast_safe_sleep(chan, delay)) {
		return -1;
	}

	if (ast_playtones_start(chan, toneloudness, tone_freq, 0)) {
		return -1;
	}

	if (ast_safe_sleep(chan, tone_duration)) {
		return -1;
	}

	ast_playtones_stop(chan);
	return 0;
}
Пример #6
0
static int app_control_dtmf(struct stasis_app_control *control,
                            struct ast_channel *chan, void *data)
{
    RAII_VAR(struct stasis_app_control_dtmf_data *, dtmf_data, data, ast_free);

    if (dtmf_data->before) {
        ast_safe_sleep(chan, dtmf_data->before);
    }

    ast_dtmf_stream(chan, NULL, dtmf_data->dtmf, dtmf_data->between, dtmf_data->duration);

    if (dtmf_data->after) {
        ast_safe_sleep(chan, dtmf_data->after);
    }

    return 0;
}
Пример #7
0
int ast_dtmf_stream(struct ast_channel *chan,struct ast_channel *peer,char *digits,int between)
{
    char *ptr=NULL;
    int res=0;
    struct ast_frame f;
    if (!between)
        between = 100;

    if (peer)
        res = ast_autoservice_start(peer);

    if (!res) {
        res = ast_waitfor(chan,100);
        if (res > -1) {
            for (ptr=digits; *ptr; *ptr++) {
                if (*ptr == 'w') {
                    res = ast_safe_sleep(chan, 500);
                    if (res)
                        break;
                    continue;
                }
                memset(&f, 0, sizeof(f));
                f.frametype = AST_FRAME_DTMF;
                f.subclass = *ptr;
                f.src = "ast_dtmf_stream";
                if (strchr("0123456789*#abcdABCD",*ptr)==NULL) {
                    ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n",*ptr);
                } else {
                    res = ast_write(chan, &f);
                    if (res)
                        break;
                    /* pause between digits */
                    res = ast_safe_sleep(chan,between);
                    if (res)
                        break;
                }
            }
        }
        if (peer)
            res = ast_autoservice_stop(peer);
    }
    return res;
}
Пример #8
0
static int app_control_dtmf(struct stasis_app_control *control,
	struct ast_channel *chan, void *data)
{
	struct stasis_app_control_dtmf_data *dtmf_data = data;

	if (ast_channel_state(chan) != AST_STATE_UP) {
		ast_indicate(chan, AST_CONTROL_PROGRESS);
	}

	if (dtmf_data->before) {
		ast_safe_sleep(chan, dtmf_data->before);
	}

	ast_dtmf_stream(chan, NULL, dtmf_data->dtmf, dtmf_data->between, dtmf_data->duration);

	if (dtmf_data->after) {
		ast_safe_sleep(chan, dtmf_data->after);
	}

	return 0;
}
Пример #9
0
static int milliwatt_exec(struct ast_channel *chan, const char *data)
{
	const char *options = data;
	int res = -1;

	if (!ast_strlen_zero(options) && strchr(options, 'o')) {
		return old_milliwatt_exec(chan);
	}

	res = ast_playtones_start(chan, 23255, "1004/1000", 0);

	while (!res) {
		res = ast_safe_sleep(chan, 10000);
	}

	return res;
}
Пример #10
0
static int zapateller_exec(struct ast_channel *chan, void *data)
{
	int res = 0;
	struct localuser *u;
	int answer = 0, nocallerid = 0;
	char *c;
	char *stringp=NULL;
	
	LOCAL_USER_ADD(u);

	stringp=data;
        c = strsep(&stringp, "|");
        while(!ast_strlen_zero(c)) {
		if (!strcasecmp(c, "answer"))
			answer = 1;
		else if (!strcasecmp(c, "nocallerid"))
			nocallerid = 1;

                c = strsep(&stringp, "|");
        }

	ast_stopstream(chan);
	if (chan->_state != AST_STATE_UP) {

		if (answer) 
			res = ast_answer(chan);
		if (!res) {
			res = ast_safe_sleep(chan, 500);
		}
	}
	if (chan->cid.cid_num && nocallerid) {
		LOCAL_USER_REMOVE(u);
		return res;
	} 
	if (!res) 
		res = ast_tonepair(chan, 950, 0, 330, 0);
	if (!res) 
		res = ast_tonepair(chan, 1400, 0, 330, 0);
	if (!res) 
		res = ast_tonepair(chan, 1800, 0, 330, 0);
	if (!res) 
		res = ast_tonepair(chan, 0, 0, 1000, 0);
	LOCAL_USER_REMOVE(u);
	return res;
}
Пример #11
0
static int milliwatt_exec(struct ast_channel *chan, void *data)
{

	struct localuser *u;
	LOCAL_USER_ADD(u);
	ast_set_write_format(chan, AST_FORMAT_ULAW);
	ast_set_read_format(chan, AST_FORMAT_ULAW);
	if (chan->_state != AST_STATE_UP)
	{
		ast_answer(chan);
	}
	if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0)
	{
		ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
		return -1;
	}
	while(!ast_safe_sleep(chan, 10000));
	ast_deactivate_generator(chan);
	LOCAL_USER_REMOVE(u);
	return -1;
}
Пример #12
0
static int old_milliwatt_exec(struct ast_channel *chan)
{
	ast_set_write_format_by_id(chan, AST_FORMAT_ULAW);
	ast_set_read_format_by_id(chan, AST_FORMAT_ULAW);

	if (ast_channel_state(chan) != AST_STATE_UP) {
		ast_answer(chan);
	}

	if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
		ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan));
		return -1;
	}

	while (!ast_safe_sleep(chan, 10000))
		;

	ast_deactivate_generator(chan);

	return -1;
}
Пример #13
0
static int testserver_exec(struct ast_channel *chan, const char *data)
{
    int res = 0;
    char testid[80]="";
    char fn[80];
    FILE *f;
    if (ast_channel_state(chan) != AST_STATE_UP)
        res = ast_answer(chan);
    /* Read version */
    ast_debug(1, "Read client version\n");
    if (!res)
        res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
    if (res > 0)
        res = 0;

    ast_debug(1, "client version: %s\n", testid);
    ast_debug(1, "Transmit server version\n");

    res = ast_safe_sleep(chan, 1000);
    if (!res)
        res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
    if (res > 0)
        res = 0;

    if (!res)
        res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
    ast_debug(1, "read test identifier: %s\n", testid);
    /* Check for sneakyness */
    if (strchr(testid, '/'))
        res = -1;
    if ((res >=0) && (!ast_strlen_zero(testid))) {
        /* Got a Test ID!  Whoo hoo! */
        /* Make the directory to hold the test results in case it's not there */
        snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
        ast_mkdir(fn, 0777);
        snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
        if ((f = fopen(fn, "w+"))) {
            setlinebuf(f);
            fprintf(f, "SERVERCHAN:    %s\n", ast_channel_name(chan));
            fprintf(f, "SERVERTEST ID: %s\n", testid);
            fprintf(f, "ANSWER:        PASS\n");
            ast_debug(1, "Processing Test ID '%s'\n", testid);
            res = ast_safe_sleep(chan, 1000);
            if (!res) {
                /* Step 1: Send "1" */
                ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
                res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
                fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 2: Wait for "2" */
                ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
                res = ast_waitfordigit(chan, 3000);
                fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
                if (res == '2')
                    res = 0;
                else
                    res = -1;
            }
            if (!res) {
                /* Step 3: Measure noise */
                ast_debug(1, "TestServer: 3.  Measure noise\n");
                res = measurenoise(chan, 6000, "TestServer");
                fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 4: Send "4" */
                ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
                res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
                fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 5: Wait one second */
                ast_debug(1, "TestServer: 5.  Wait one second\n");
                res = ast_safe_sleep(chan, 1000);
                fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 6: Measure noise */
                ast_debug(1, "TestServer: 6.  Measure tone\n");
                res = measurenoise(chan, 4000, "TestServer");
                fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 7: Send "5" */
                ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
                res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
                fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 8: Transmit tone noise */
                ast_debug(1, "TestServer: 8.  Transmit tone\n");
                res = sendnoise(chan, 6000);
                fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
            }

            if (!res || (res == '7')) {
                /* Step 9: Wait for "7" */
                ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
                if (!res)
                    res = ast_waitfordigit(chan, 3000);
                fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
                if (res == '7')
                    res = 0;
                else
                    res = -1;
            }
            if (!res) {
                res = ast_safe_sleep(chan, 1000);
            }
            if (!res) {
                /* Step 10: Send "8" */
                ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
                res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
                fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 11: Wait for hangup to arrive! */
                ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
                res = ast_safe_sleep(chan, 10000);
                fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
            }

            ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
            fprintf(f, "-- END TEST--\n");
            fclose(f);
            res = -1;
        } else
            res = -1;
    } else {
        ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
        res = -1;
    }
    return res;
}
Пример #14
0
static int testclient_exec(struct ast_channel *chan, const char *data)
{
    int res = 0;
    const char *testid=data;
    char fn[80];
    char serverver[80];
    FILE *f;

    /* Check for test id */
    if (ast_strlen_zero(testid)) {
        ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
        return -1;
    }

    if (ast_channel_state(chan) != AST_STATE_UP)
        res = ast_answer(chan);

    /* Wait a few just to be sure things get started */
    res = ast_safe_sleep(chan, 3000);
    /* Transmit client version */
    if (!res)
        res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
    ast_debug(1, "Transmit client version\n");

    /* Read server version */
    ast_debug(1, "Read server version\n");
    if (!res)
        res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
    if (res > 0)
        res = 0;
    ast_debug(1, "server version: %s\n", serverver);

    if (res > 0)
        res = 0;

    if (!res)
        res = ast_safe_sleep(chan, 1000);
    /* Send test id */
    if (!res)
        res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
    if (!res)
        res = ast_dtmf_stream(chan, NULL, "#", 0, 0);
    ast_debug(1, "send test identifier: %s\n", testid);

    if ((res >=0) && (!ast_strlen_zero(testid))) {
        /* Make the directory to hold the test results in case it's not there */
        snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
        ast_mkdir(fn, 0777);
        snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
        if ((f = fopen(fn, "w+"))) {
            setlinebuf(f);
            fprintf(f, "CLIENTCHAN:    %s\n", ast_channel_name(chan));
            fprintf(f, "CLIENTTEST ID: %s\n", testid);
            fprintf(f, "ANSWER:        PASS\n");
            res = 0;

            if (!res) {
                /* Step 1: Wait for "1" */
                ast_debug(1, "TestClient: 2.  Wait DTMF 1\n");
                res = ast_waitfordigit(chan, 3000);
                fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
                if (res == '1')
                    res = 0;
                else
                    res = -1;
            }
            if (!res) {
                res = ast_safe_sleep(chan, 1000);
            }
            if (!res) {
                /* Step 2: Send "2" */
                ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
                res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
                fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 3: Wait one second */
                ast_debug(1, "TestClient: 3.  Wait one second\n");
                res = ast_safe_sleep(chan, 1000);
                fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 4: Measure noise */
                ast_debug(1, "TestClient: 4.  Measure noise\n");
                res = measurenoise(chan, 5000, "TestClient");
                fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 5: Wait for "4" */
                ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
                res = ast_waitfordigit(chan, 3000);
                fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
                if (res == '4')
                    res = 0;
                else
                    res = -1;
            }
            if (!res) {
                /* Step 6: Transmit tone noise */
                ast_debug(1, "TestClient: 6.  Transmit tone\n");
                res = sendnoise(chan, 6000);
                fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
            }
            if (!res || (res == '5')) {
                /* Step 7: Wait for "5" */
                ast_debug(1, "TestClient: 7.  Wait DTMF 5\n");
                if (!res)
                    res = ast_waitfordigit(chan, 3000);
                fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
                if (res == '5')
                    res = 0;
                else
                    res = -1;
            }
            if (!res) {
                /* Step 8: Wait one second */
                ast_debug(1, "TestClient: 8.  Wait one second\n");
                res = ast_safe_sleep(chan, 1000);
                fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 9: Measure noise */
                ast_debug(1, "TestClient: 9.  Measure tone\n");
                res = measurenoise(chan, 4000, "TestClient");
                fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                if (res > 0)
                    res = 0;
            }
            if (!res) {
                /* Step 10: Send "7" */
                ast_debug(1, "TestClient: 10.  Send DTMF 7\n");
                res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
                fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
                if (res > 0)
                    res =0;
            }
            if (!res) {
                /* Step 11: Wait for "8" */
                ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
                res = ast_waitfordigit(chan, 3000);
                fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
                if (res == '8')
                    res = 0;
                else
                    res = -1;
            }
            if (!res) {
                res = ast_safe_sleep(chan, 1000);
            }
            if (!res) {
                /* Step 12: Hangup! */
                ast_debug(1, "TestClient: 12.  Hangup\n");
            }

            ast_debug(1, "-- TEST COMPLETE--\n");
            fprintf(f, "-- END TEST--\n");
            fclose(f);
            res = -1;
        } else
            res = -1;
    } else {
        ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
        res = -1;
    }
    return res;
}
Пример #15
0
/*!
 * \brief This is the main function called by Asterisk Core whenever the App is invoked in the extension logic.
 *
 * \param chan Asterisk Channel
 * \param data Application data
 *
 * \retval 0 success
 * \retval -1 failure
 */
static int alarmreceiver_exec(struct ast_channel *chan, const char *data)
{
	int res = 0;
	int no_checksum = 0;
	event_node_t *elp, *efree;
	char signalling_type[64] = "";
	event_node_t *event_head = NULL;

	if ((ast_format_cmp(ast_channel_writeformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) &&
		(ast_format_cmp(ast_channel_writeformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) {
		ast_verb(4, "AlarmReceiver: Setting write format to Mu-law\n");
		if (ast_set_write_format(chan, ast_format_ulaw)) {
			ast_log(LOG_WARNING, "AlarmReceiver: Unable to set write format to Mu-law on %s\n",ast_channel_name(chan));
			return -1;
		}
	}

	if ((ast_format_cmp(ast_channel_readformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) &&
		(ast_format_cmp(ast_channel_readformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) {
		ast_verb(4, "AlarmReceiver: Setting read format to Mu-law\n");
		if (ast_set_read_format(chan, ast_format_ulaw)) {
			ast_log(LOG_WARNING, "AlarmReceiver: Unable to set read format to Mu-law on %s\n",ast_channel_name(chan));
			return -1;
		}
	}

	/* Set default values for this invocation of the application */
	ast_copy_string(signalling_type, UNKNOWN_FORMAT, sizeof(signalling_type));
	call_start_time = ast_tvnow();

	/* Answer the channel if it is not already */
	if (ast_channel_state(chan) != AST_STATE_UP) {
		ast_verb(4, "AlarmReceiver: Answering channel\n");
		if (ast_answer(chan)) {
			return -1;
		}
	}

	/* Wait for the connection to settle post-answer */
	ast_verb(4, "AlarmReceiver: Waiting for connection to stabilize\n");
	if (ast_safe_sleep(chan, answait)) {
		return -1;
	}

	/* Attempt to receive the events */
	receive_ademco_event(chan, &event_head, signalling_type, &no_checksum);

	/* Events queued by receiver, write them all out here if so configured */
	if (!log_individual_events) {
		res = log_events(chan, signalling_type, event_head, no_checksum);
	}

	/* Do we exec a command line at the end? */
	if ((!res) && (!ast_strlen_zero(event_app)) && (event_head)) {
		ast_debug(1,"Alarmreceiver: executing: %s\n", event_app);
		ast_safe_system(event_app);
	}

	/* Free up the data allocated in our linked list */
	for (elp = event_head; (elp != NULL);) {
		efree = elp;
		elp = elp->next;
		ast_free(efree);
	}

	return 0;
}
Пример #16
0
int AST_OPTIONAL_API_NAME(ast_adsi_transmit_message_full)(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait)
{
	unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL };
	int msglens[5], msgtypes[5], newdatamode = (ast_channel_adsicpe(chan) & ADSI_FLAG_DATAMODE), res, x, waitforswitch = 0;
	struct ast_format writeformat;
	struct ast_format readformat;

	ast_format_copy(&writeformat, ast_channel_writeformat(chan));
	ast_format_copy(&readformat, ast_channel_readformat(chan));

	for (x = 0; x < msglen; x += (msg[x+1]+2)) {
		if (msg[x] == ADSI_SWITCH_TO_DATA) {
			ast_debug(1, "Switch to data is sent!\n");
			waitforswitch++;
			newdatamode = ADSI_FLAG_DATAMODE;
		}

		if (msg[x] == ADSI_SWITCH_TO_VOICE) {
			ast_debug(1, "Switch to voice is sent!\n");
			waitforswitch++;
			newdatamode = 0;
		}
	}
	msgs[0] = msg;

	msglens[0] = msglen;
	msgtypes[0] = msgtype;

	if (msglen > 253) {
		ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen);
		return -1;
	}

	ast_stopstream(chan);

	if (ast_set_write_format_by_id(chan, AST_FORMAT_ULAW)) {
		ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
		return -1;
	}

	if (ast_set_read_format_by_id(chan, AST_FORMAT_ULAW)) {
		ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
		if (writeformat.id) {
			if (ast_set_write_format(chan, &writeformat)) {
				ast_log(LOG_WARNING, "Unable to restore write format to %s\n", ast_getformatname(&writeformat));
			}
		}
		return -1;
	}
	res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes);

	if (dowait) {
		ast_debug(1, "Wait for switch is '%d'\n", waitforswitch);
		while (waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) {
			res = 0;
			ast_debug(1, "Waiting for 'B'...\n");
		}
	}

	if (!res) {
		ast_channel_adsicpe_set(chan, (ast_channel_adsicpe(chan) & ~ADSI_FLAG_DATAMODE) | newdatamode);
	}

	if (writeformat.id) {
		ast_set_write_format(chan, &writeformat);
	}
	if (readformat.id) {
		ast_set_read_format(chan, &readformat);
	}

	if (!res) {
		res = ast_safe_sleep(chan, 100 );
	}
	return res;
}
Пример #17
0
/*
* This is the main function called by Asterisk Core whenever the App is invoked in the extension logic.
* This function will always return 0.
*/
static int alarmreceiver_exec(struct ast_channel *chan, const char *data)
{
	int res = 0;
	event_node_t *elp, *efree;
	char signalling_type[64] = "";
	event_node_t *event_head = NULL;

	/* Set write and read formats to ULAW */
	ast_verb(4, "AlarmReceiver: Setting read and write formats to ULAW\n");

	if (ast_set_write_format_by_id(chan,AST_FORMAT_ULAW)) {
		ast_log(LOG_WARNING, "AlarmReceiver: Unable to set write format to Mu-law on %s\n",ast_channel_name(chan));
		return -1;
	}

	if (ast_set_read_format_by_id(chan,AST_FORMAT_ULAW)) {
		ast_log(LOG_WARNING, "AlarmReceiver: Unable to set read format to Mu-law on %s\n",ast_channel_name(chan));
		return -1;
	}

	/* Set default values for this invocation of the application */
	ast_copy_string(signalling_type, ADEMCO_CONTACT_ID, sizeof(signalling_type));

	/* Answer the channel if it is not already */
	ast_verb(4, "AlarmReceiver: Answering channel\n");
	if (ast_channel_state(chan) != AST_STATE_UP) {
		if ((res = ast_answer(chan)))
			return -1;
	}

	/* Wait for the connection to settle post-answer */
	ast_verb(4, "AlarmReceiver: Waiting for connection to stabilize\n");
	res = ast_safe_sleep(chan, 1250);

	/* Attempt to receive the events */
	if (!res) {
		/* Determine the protocol to receive in advance */
		/* Note: Ademco contact is the only one supported at this time */
		/* Others may be added later */
		if(!strcmp(signalling_type, ADEMCO_CONTACT_ID))
			receive_ademco_contact_id(chan, data, fdtimeout, sdtimeout, toneloudness, &event_head);
		else
			res = -1;
	}

	/* Events queued by receiver, write them all out here if so configured */
	if ((!res) && (log_individual_events == 0))
		res = log_events(chan, signalling_type, event_head);

	/*
	* Do we exec a command line at the end?
	*/
	if ((!res) && (!ast_strlen_zero(event_app)) && (event_head)) {
		ast_debug(1,"Alarmreceiver: executing: %s\n", event_app);
		ast_safe_system(event_app);
	}

	/*
	* Free up the data allocated in our linked list
	*/
	for (elp = event_head; (elp != NULL);) {
		efree = elp;
		elp = elp->next;
		ast_free(efree);
	}

	return 0;
}
Пример #18
0
/*
* This function implements the logic to receive the Ademco contact ID  format.
*
* The function will return 0 when the caller hangs up, else a -1 if there was a problem.
*/
static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, int fdto, int sdto, int tldn, event_node_t **ehead)
{
	int i, j;
	int res = 0;
	int checksum;
	char event[17];
	event_node_t *enew, *elp;
	int got_some_digits = 0;
	int events_received = 0;
	int ack_retries = 0;
	
	static char digit_map[15] = "0123456789*#ABC";
	static unsigned char digit_weights[15] = {10,1,2,3,4,5,6,7,8,9,11,12,13,14,15};

	database_increment("calls-received");

	/* Wait for first event */
	ast_verb(4, "AlarmReceiver: Waiting for first event from panel\n");

	while (res >= 0) {
		if (got_some_digits == 0) {
			/* Send ACK tone sequence */
			ast_verb(4, "AlarmReceiver: Sending 1400Hz 100ms burst (ACK)\n");
			res = send_tone_burst(chan, 1400.0, 100, tldn);
			if (!res)
				res = ast_safe_sleep(chan, 100);
			if (!res) {
				ast_verb(4, "AlarmReceiver: Sending 2300Hz 100ms burst (ACK)\n");
				res = send_tone_burst(chan, 2300.0, 100, tldn);
			}
		}
		if ( res >= 0)
			res = receive_dtmf_digits(chan, event, sizeof(event) - 1, fdto, sdto);
		if (res < 0) {
			if (events_received == 0) {
				/* Hangup with no events received should be logged in the DB */
				database_increment("no-events-received");
			} else {
				if (ack_retries) {
					ast_verb(4, "AlarmReceiver: ACK retries during this call: %d\n", ack_retries);
					database_increment("ack-retries");
				}
			}
			ast_verb(4, "AlarmReceiver: App exiting...\n");
			res = -1;
			break;
		}

		if (res != 0) {
			/* Didn't get all of the digits */
			ast_verb(2, "AlarmReceiver: Incomplete string: %s, trying again...\n", event);

			if (!got_some_digits) {
				got_some_digits = (!ast_strlen_zero(event)) ? 1 : 0;
				ack_retries++;
			}
			continue;
		}

		got_some_digits = 1;

		ast_verb(2, "AlarmReceiver: Received Event %s\n", event);
		ast_debug(1, "AlarmReceiver: Received event: %s\n", event);

		/* Calculate checksum */

		for (j = 0, checksum = 0; j < 16; j++) {
			for (i = 0; i < sizeof(digit_map); i++) {
				if (digit_map[i] == event[j])
					break;
			}

			if (i == 16)
				break;

			checksum += digit_weights[i];
		}
		if (i == 16) {
			ast_verb(2, "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]);
			continue; /* Bad character */
		}

		/* Checksum is mod(15) of the total */

		checksum = checksum % 15;

		if (checksum) {
			database_increment("checksum-errors");
			ast_verb(2, "AlarmReceiver: Nonzero checksum\n");
			ast_debug(1, "AlarmReceiver: Nonzero checksum\n");
			continue;
		}

		/* Check the message type for correctness */

		if (strncmp(event + 4, "18", 2)) {
			if (strncmp(event + 4, "98", 2)) {
				database_increment("format-errors");
				ast_verb(2, "AlarmReceiver: Wrong message type\n");
				ast_debug(1, "AlarmReceiver: Wrong message type\n");
			continue;
			}
		}

		events_received++;

		/* Queue the Event */
		if (!(enew = ast_calloc(1, sizeof(*enew)))) {
			res = -1;
			break;
		}

		enew->next = NULL;
		ast_copy_string(enew->data, event, sizeof(enew->data));

		/*
		* Insert event onto end of list
		*/
		if (*ehead == NULL)
			*ehead = enew;
		else {
			for(elp = *ehead; elp->next != NULL; elp = elp->next)
			;
			elp->next = enew;
		}

		if (res > 0)
			res = 0;

		/* Let the user have the option of logging the single event before sending the kissoff tone */
		if ((res == 0) && (log_individual_events))
			res = log_events(chan, ADEMCO_CONTACT_ID, enew);
		/* Wait 200 msec before sending kissoff */
		if (res == 0)
			res = ast_safe_sleep(chan, 200);

		/* Send the kissoff tone */
		if (res == 0)
			res = send_tone_burst(chan, 1400.0, 900, tldn);
	}

	return res;
}
Пример #19
0
int adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait)
{
	unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL };
	int msglens[5];
	int msgtypes[5];
	int newdatamode;
	int res;
	int x;
	int writeformat, readformat;
	int waitforswitch = 0;

	writeformat = chan->writeformat;
	readformat = chan->readformat;

	newdatamode = chan->adsicpe & ADSI_FLAG_DATAMODE;

	for (x=0;x<msglen;x+=(msg[x+1]+2)) {
		if (msg[x] == ADSI_SWITCH_TO_DATA) {
			ast_log(LOG_DEBUG, "Switch to data is sent!\n");
			waitforswitch++;
			newdatamode = ADSI_FLAG_DATAMODE;
		}
		
		if (msg[x] == ADSI_SWITCH_TO_VOICE) {
			ast_log(LOG_DEBUG, "Switch to voice is sent!\n");
			waitforswitch++;
			newdatamode = 0;
		}
	}
	msgs[0] = msg;

	msglens[0] = msglen;
	msgtypes[0] = msgtype;

	if (msglen > 253) {
		ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen);
		return -1;
	}

	ast_stopstream(chan);

	if (ast_set_write_format(chan, AST_FORMAT_ULAW)) {
		ast_log(LOG_WARNING, "Unable to set write format to ULAW\n");
		return -1;
	}

	if (ast_set_read_format(chan, AST_FORMAT_ULAW)) {
		ast_log(LOG_WARNING, "Unable to set read format to ULAW\n");
		if (writeformat) {
			if (ast_set_write_format(chan, writeformat)) 
				ast_log(LOG_WARNING, "Unable to restore write format to %d\n", writeformat);
		}
		return -1;
	}
	res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes);

	if (dowait) {
		ast_log(LOG_DEBUG, "Wait for switch is '%d'\n", waitforswitch);
		while(waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) { res = 0; ast_log(LOG_DEBUG, "Waiting for 'B'...\n"); }
	}
	
	if (!res)
		chan->adsicpe = (chan->adsicpe & ~ADSI_FLAG_DATAMODE) | newdatamode;

	if (writeformat)
		ast_set_write_format(chan, writeformat);
	if (readformat)
		ast_set_read_format(chan, readformat);

	if (!res)
		res = ast_safe_sleep(chan, 100 );
	return res;
}