add a library of timeval manipulation functions, and change a large number of usses to use the new functions (bug #4504)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6146 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Kevin P. Fleming
2005-07-15 23:00:47 +00:00
parent 60cd1fa57d
commit 22b0f5d306
39 changed files with 420 additions and 635 deletions

View File

@@ -235,14 +235,12 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int
int i = 0;
int r;
struct ast_frame *f;
struct timeval now, lastdigittime;
struct timeval lastdigittime;
gettimeofday(&lastdigittime,NULL);
lastdigittime = ast_tvnow();
for(;;){
gettimeofday(&now,NULL);
/* if outa time, leave */
if (ast_tvdiff_ms(&now, &lastdigittime) >
if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) >
((i > 0) ? sdto : fdto)){
if(option_verbose >= 4)
ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: DTMF Digit Timeout on %s\n", chan->name);
@@ -287,7 +285,7 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int
if(i >= length)
break;
gettimeofday(&lastdigittime,NULL);
lastdigittime = ast_tvnow();
}
digit_string[i] = '\0'; /* Nul terminate the end of the digit string */

View File

@@ -90,15 +90,6 @@ STANDARD_LOCAL_USER;
LOCAL_USER_DECL;
static int ms_diff(struct timeval *tv1, struct timeval *tv2)
{
int ms;
ms = (tv1->tv_sec - tv2->tv_sec) * 1000;
ms += (tv1->tv_usec - tv2->tv_usec) / 1000;
return(ms);
}
static void play_dialtone(struct ast_channel *chan, char *mailbox)
{
const struct tone_zone_sound *ts = NULL;
@@ -121,7 +112,7 @@ static int disa_exec(struct ast_channel *chan, void *data)
char tmp[256],arg2[256]="",exten[AST_MAX_EXTENSION],acctcode[20]="";
char *ourcontext,*ourcallerid,ourcidname[256],ourcidnum[256],*mailbox;
struct ast_frame *f;
struct timeval lastout, now, lastdigittime;
struct timeval lastdigittime;
int res;
time_t rstart;
FILE *fp;
@@ -142,7 +133,6 @@ static int disa_exec(struct ast_channel *chan, void *data)
ast_log(LOG_WARNING, "Unable to set read format to Mu-law on %s\n",chan->name);
return -1;
}
lastout.tv_sec = lastout.tv_usec = 0;
if (!data || !strlen((char *)data)) {
ast_log(LOG_WARNING, "disa requires an argument (passcode/passcode file)\n");
return -1;
@@ -187,15 +177,14 @@ static int disa_exec(struct ast_channel *chan, void *data)
k |= 1; /* We have the password */
ast_log(LOG_DEBUG, "DISA no-password login success\n");
}
gettimeofday(&lastdigittime,NULL);
lastdigittime = ast_tvnow();
play_dialtone(chan, mailbox);
for(;;)
{
gettimeofday(&now,NULL);
/* if outa time, give em reorder */
if (ms_diff(&now,&lastdigittime) >
if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) >
((k&2) ? digittimeout : firstdigittimeout))
{
ast_log(LOG_DEBUG,"DISA %s entry timeout on chan %s\n",
@@ -238,7 +227,7 @@ static int disa_exec(struct ast_channel *chan, void *data)
k|=2; /* We have the first digit */
ast_playtones_stop(chan);
}
gettimeofday(&lastdigittime,NULL);
lastdigittime = ast_tvnow();
/* got a DTMF tone */
if (i < AST_MAX_EXTENSION) /* if still valid number of digits */
{

View File

@@ -51,7 +51,7 @@ static int ast_serialize_showchan(struct ast_channel *c, char *buf, size_t size)
char cgrp[256];
char pgrp[256];
gettimeofday(&now, NULL);
now = ast_tvnow();
memset(buf,0,size);
if (!c)
return 0;

View File

@@ -46,7 +46,7 @@ static void ast_cdr_clone(struct ast_cdr *cdr)
struct ast_cdr *newcdr = ast_cdr_alloc();
memcpy(newcdr,cdr,sizeof(struct ast_cdr));
ast_cdr_append(cdr,newcdr);
gettimeofday(&newcdr->start, NULL);
newcdr->start = ast_tvnow();
memset(&newcdr->answer, 0, sizeof(newcdr->answer));
memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
ast_cdr_copy_vars(newcdr, cdr);

View File

@@ -109,7 +109,7 @@ static int mp3_exec(struct ast_channel *chan, void *data)
int pid = -1;
int owriteformat;
int timeout = 2000;
struct timeval now, next;
struct timeval next;
struct ast_frame *f;
struct myframe {
struct ast_frame f;
@@ -134,35 +134,20 @@ static int mp3_exec(struct ast_channel *chan, void *data)
return -1;
}
gettimeofday(&now, NULL);
res = mp3play((char *)data, fds[1]);
if (!strncasecmp((char *)data, "http://", 7)) {
timeout = 10000;
}
/* Wait 1000 ms first */
next = now;
next = ast_tvnow();
next.tv_sec += 1;
if (res >= 0) {
pid = res;
/* Order is important -- there's almost always going to be mp3... we want to prioritize the
user */
for (;;) {
gettimeofday(&now, NULL);
ms = (next.tv_sec - now.tv_sec) * 1000;
ms += (next.tv_usec - now.tv_usec) / 1000;
#if 0
printf("ms: %d\n", ms);
#endif
ms = ast_tvdiff_ms(next, ast_tvnow());
if (ms <= 0) {
#if 0
{
static struct timeval last;
struct timeval tv;
gettimeofday(&tv, NULL);
printf("Since last: %ld\n", (tv.tv_sec - last.tv_sec) * 1000 + (tv.tv_usec - last.tv_usec) / 1000);
last = tv;
}
#endif
res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
if (res > 0) {
myf.f.frametype = AST_FRAME_VOICE;
@@ -184,14 +169,7 @@ static int mp3_exec(struct ast_channel *chan, void *data)
res = 0;
break;
}
next.tv_usec += res / 2 * 125;
if (next.tv_usec >= 1000000) {
next.tv_usec -= 1000000;
next.tv_sec++;
}
#if 0
printf("Next: %d\n", ms);
#endif
next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
} else {
ms = ast_waitfor(chan, ms);
if (ms < 0) {

View File

@@ -99,7 +99,7 @@ static int NBScat_exec(struct ast_channel *chan, void *data)
int ms = -1;
int pid = -1;
int owriteformat;
struct timeval now, next;
struct timeval next;
struct ast_frame *f;
struct myframe {
struct ast_frame f;
@@ -122,29 +122,15 @@ static int NBScat_exec(struct ast_channel *chan, void *data)
res = NBScatplay(fds[1]);
/* Wait 1000 ms first */
next = now;
next = ast_tvnow();
next.tv_sec += 1;
if (res >= 0) {
pid = res;
/* Order is important -- there's almost always going to be mp3... we want to prioritize the
user */
for (;;) {
gettimeofday(&now, NULL);
ms = (next.tv_sec - now.tv_sec) * 1000;
ms += (next.tv_usec - now.tv_usec) / 1000;
#if 0
printf("ms: %d\n", ms);
#endif
ms = ast_tvdiff_ms(next, ast_tvnow());
if (ms <= 0) {
#if 0
{
static struct timeval last;
struct timeval tv;
gettimeofday(&tv, NULL);
printf("Since last: %ld\n", (tv.tv_sec - last.tv_sec) * 1000 + (tv.tv_usec - last.tv_usec) / 1000);
last = tv;
}
#endif
res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
if (res > 0) {
myf.f.frametype = AST_FRAME_VOICE;
@@ -166,14 +152,7 @@ static int NBScat_exec(struct ast_channel *chan, void *data)
res = 0;
break;
}
next.tv_usec += res / 2 * 125;
if (next.tv_usec >= 1000000) {
next.tv_usec -= 1000000;
next.tv_sec++;
}
#if 0
printf("Next: %d\n", ms);
#endif
next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
} else {
ms = ast_waitfor(chan, ms);
if (ms < 0) {

View File

@@ -83,7 +83,7 @@ static int readfile_exec(struct ast_channel *chan, void *data)
if(len < strlen(returnvar))
returnvar[len]='\0';
else
ast_log(LOG_WARNING,"%s is longer than %d, and %zd \n", file, len, strlen(returnvar));
ast_log(LOG_WARNING,"%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
}
pbx_builtin_setvar_helper(chan, varname, returnvar);
free(returnvar);

View File

@@ -69,7 +69,7 @@ static int sayunixtime_exec(struct ast_channel *chan, void *data)
LOCAL_USER_ADD(u);
gettimeofday(&tv,NULL);
tv = ast_tvnow();
unixtime = (time_t)tv.tv_sec;
if( !strcasecmp(chan->language, "da" ) ) {

View File

@@ -1176,7 +1176,7 @@ static int sms_generate (struct ast_channel *chan, void *data, int len, int samp
int i;
if (len > sizeof (buf)) {
ast_log (LOG_WARNING, "Only doing %zd bytes (%d bytes requested)\n", sizeof (buf) / sizeof (signed short), len);
ast_log (LOG_WARNING, "Only doing %d bytes (%d bytes requested)\n", (int)(sizeof (buf) / sizeof (signed short)), len);
len = sizeof (buf);
#ifdef OUTALAW
samples = len;

View File

@@ -59,7 +59,7 @@ static int background_detect_exec(struct ast_channel *chan, void *data)
char *stringp;
struct ast_frame *fr;
int notsilent=0;
struct timeval start = { 0, 0}, end = {0, 0};
struct timeval start = { 0, 0};
int sil = 1000;
int min = 100;
int max = -1;
@@ -143,9 +143,7 @@ static int background_detect_exec(struct ast_channel *chan, void *data)
/* We've been quiet a little while */
if (notsilent) {
/* We had heard some talking */
gettimeofday(&end, NULL);
ms = (end.tv_sec - start.tv_sec) * 1000;
ms += (end.tv_usec - start.tv_usec) / 1000;
ms = ast_tvdiff_ms(ast_tvnow(), start);
ms -= sil;
if (ms < 0)
ms = 0;
@@ -171,7 +169,7 @@ static int background_detect_exec(struct ast_channel *chan, void *data)
} else {
if (!notsilent) {
/* Heard some audio, mark the begining of the token */
gettimeofday(&start, NULL);
start = ast_tvnow();
ast_log(LOG_DEBUG, "Start of voice token!\n");
notsilent = 1;
}

View File

@@ -55,7 +55,7 @@ static int measurenoise(struct ast_channel *chan, int ms, char *who)
int samples=0;
int x;
short *foo;
struct timeval start, tv;
struct timeval start;
struct ast_frame *f;
int rformat;
rformat = chan->readformat;
@@ -63,11 +63,9 @@ static int measurenoise(struct ast_channel *chan, int ms, char *who)
ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
return -1;
}
gettimeofday(&start, NULL);
start = ast_tvnow();
for(;;) {
gettimeofday(&tv, NULL);
mssofar = (tv.tv_sec - start.tv_sec) * 1000;
mssofar += (tv.tv_usec - start.tv_usec) / 1000;
mssofar = ast_tvdiff_ms(ast_tvnow(), start);
if (mssofar > ms)
break;
res = ast_waitfor(chan, ms - mssofar);

View File

@@ -3295,7 +3295,7 @@ static int play_message_datetime(struct ast_channel *chan, struct ast_vm_user *v
#if 0
/* Set the DIFF_* variables */
localtime_r(&t, &time_now);
gettimeofday(&tv_now,NULL);
tv_now = ast_tvnow();
tnow = tv_now.tv_sec;
localtime_r(&tnow,&time_then);