[Asterisk-cvs] asterisk/channels chan_agent.c, 1.144, 1.145 chan_iax2.c, 1.319, 1.320 chan_phone.c, 1.54, 1.55 chan_sip.c, 1.780, 1.781 chan_vpb.c, 1.89, 1.90 chan_zap.c, 1.476, 1.477

kpfleming at lists.digium.com kpfleming at lists.digium.com
Fri Jul 15 18:52:58 CDT 2005


Update of /usr/cvsroot/asterisk/channels
In directory mongoose.digium.com:/tmp/cvs-serv13236/channels

Modified Files:
	chan_agent.c chan_iax2.c chan_phone.c chan_sip.c chan_vpb.c 
	chan_zap.c 
Log Message:
add a library of timeval manipulation functions, and change a large number of usses to use the new functions (bug #4504)


Index: chan_agent.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_agent.c,v
retrieving revision 1.144
retrieving revision 1.145
diff -u -d -r1.144 -r1.145
--- chan_agent.c	15 Jul 2005 16:21:41 -0000	1.144
+++ chan_agent.c	15 Jul 2005 23:00:46 -0000	1.145
@@ -316,8 +316,8 @@
 	/* If someone reduces the wrapuptime and reloads, we want it
 	 * to change the wrapuptime immediately on all calls */
 	if (p->wrapuptime > wrapuptime) {
-		struct timeval now;
-		gettimeofday(&now, NULL);
+		struct timeval now = ast_tvnow();
+		/* XXX check what is this exactly */
 
 		/* We won't be pedantic and check the tv_usec val */
 		if (p->lastdisc.tv_sec > (now.tv_sec + wrapuptime/1000)) {
@@ -420,15 +420,8 @@
 				if (p->chan)
 					ast_log(LOG_DEBUG, "Bridge on '%s' being cleared (2)\n", p->chan->name);
 				ast_hangup(p->chan);
-				if (p->wrapuptime && p->acknowledged) {
-					gettimeofday(&p->lastdisc, NULL);
-					p->lastdisc.tv_usec += (p->wrapuptime % 1000) * 1000;
-					if (p->lastdisc.tv_usec > 1000000) {
-						p->lastdisc.tv_usec -= 1000000;
-						p->lastdisc.tv_sec++;
-					}
-					p->lastdisc.tv_sec += (p->wrapuptime / 1000);
-				}
+				if (p->wrapuptime && p->acknowledged)
+					p->lastdisc = ast_tvadd(ast_tvnow(), ast_samp2tv(p->wrapuptime, 1000));
 			}
 			p->chan = NULL;
 			p->acknowledged = 0;
@@ -699,16 +692,10 @@
 		/* If they're dead, go ahead and hang up on the agent now */
 		if (!ast_strlen_zero(p->loginchan)) {
 			/* Store last disconnect time */
-			if (p->wrapuptime && p->acknowledged) {
-				gettimeofday(&p->lastdisc, NULL);
-				p->lastdisc.tv_usec += (p->wrapuptime % 1000) * 1000;
-				if (p->lastdisc.tv_usec >= 1000000) {
-					p->lastdisc.tv_usec -= 1000000;
-					p->lastdisc.tv_sec++;
-				}
-				p->lastdisc.tv_sec += (p->wrapuptime / 1000);
-			} else
-				memset(&p->lastdisc, 0, sizeof(p->lastdisc));
+			if (p->wrapuptime && p->acknowledged)
+				p->lastdisc = ast_tvadd(ast_tvnow(), ast_samp2tv(p->wrapuptime, 1000));
+			else
+				p->lastdisc = ast_tv(0,0);
 			if (p->chan) {
 				/* Recognize the hangup and pass it along immediately */
 				ast_hangup(p->chan);
@@ -779,7 +766,7 @@
 			/* Not dead -- check availability now */
 			ast_mutex_lock(&p->lock);
 			/* Store last disconnect time */
-			gettimeofday(&p->lastdisc, NULL);
+			p->lastdisc = ast_tvnow();
 			ast_mutex_unlock(&p->lock);
 		}
 		/* Release ownership of the agent to other threads (presumably running the login app). */
@@ -791,7 +778,6 @@
 static int agent_cont_sleep( void *data )
 {
 	struct agent_pvt *p;
-	struct timeval tv;
 	int res;
 
 	p = (struct agent_pvt *)data;
@@ -799,9 +785,7 @@
 	ast_mutex_lock(&p->lock);
 	res = p->app_sleep_cond;
 	if (p->lastdisc.tv_sec) {
-		gettimeofday(&tv, NULL);
-		if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 + 
-			(tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) 
+		if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > p->wrapuptime) 
 			res = 1;
 	}
 	ast_mutex_unlock(&p->lock);
@@ -1264,12 +1248,12 @@
 			if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
 				if (p->chan || !ast_strlen_zero(p->loginchan))
 					hasagent++;
-				gettimeofday(&tv, NULL);
+				tv = ast_tvnow();
 #if 0
 				ast_log(LOG_NOTICE, "Time now: %ld, Time of lastdisc: %ld\n", tv.tv_sec, p->lastdisc.tv_sec);
 #endif
 				if (!p->lastdisc.tv_sec || (tv.tv_sec > p->lastdisc.tv_sec)) {
-					memset(&p->lastdisc, 0, sizeof(p->lastdisc));
+					p->lastdisc = ast_tv(0, 0);
 					/* Agent must be registered, but not have any active call, and not be in a waiting state */
 					if (!p->owner && p->chan) {
 						/* Could still get a fixed agent */
@@ -1558,7 +1542,6 @@
 	int max_login_tries = maxlogintries;
 	struct agent_pvt *p;
 	struct localuser *u;
-	struct timeval tv;
 	int login_state = 0;
 	char user[AST_MAX_AGENT] = "";
 	char pass[AST_MAX_AGENT];
@@ -1883,12 +1866,10 @@
 								ast_mutex_lock(&agentlock);
 								ast_mutex_lock(&p->lock);
 								if (p->lastdisc.tv_sec) {
-									gettimeofday(&tv, NULL);
-									if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 + 
-										(tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
+									if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > p->wrapuptime) {
 											if (option_debug)
 												ast_log(LOG_DEBUG, "Wrapup time for %s expired!\n", p->agent);
-										memset(&p->lastdisc, 0, sizeof(p->lastdisc));
+										p->lastdisc = ast_tv(0, 0);
 										if (p->ackcall > 1)
 											check_beep(p, 0);
 										else

Index: chan_iax2.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_iax2.c,v
retrieving revision 1.319
retrieving revision 1.320
diff -u -d -r1.319 -r1.320
--- chan_iax2.c	12 Jul 2005 14:46:20 -0000	1.319
+++ chan_iax2.c	15 Jul 2005 23:00:46 -0000	1.320
@@ -2084,12 +2084,8 @@
 
 static void update_jbsched(struct chan_iax2_pvt *pvt) {
     int when;
-    struct timeval tv;
-
-    gettimeofday(&tv,NULL);
 
-    when = (tv.tv_sec - pvt->rxcore.tv_sec) * 1000 +
-	  (tv.tv_usec - pvt->rxcore.tv_usec) / 1000;
+    when = ast_tvdiff_ms(ast_tvnow(), pvt->rxcore);
 
     /*    fprintf(stderr, "now = %d, next=%d\n", when, jb_next(pvt->jb)); */
 
@@ -2126,8 +2122,7 @@
     /* to catch up with runq's now */
     tv.tv_usec += 1000;
 
-    now = (tv.tv_sec - pvt->rxcore.tv_sec) * 1000 +
-	  (tv.tv_usec - pvt->rxcore.tv_usec) / 1000;
+    now = ast_tvdiff_ms(tv, pvt->rxcore);
 
     if(now >= (next = jb_next(pvt->jb))) {
 	ret = jb_get(pvt->jb,&frame,now,ast_codec_interp_len(pvt->voiceformat));
@@ -2152,15 +2147,8 @@
 		af.mallocd  = 0;
 		af.src  = "IAX2 JB interpolation";
 		af.data  = NULL;
-		af.delivery.tv_sec = pvt->rxcore.tv_sec;
-		af.delivery.tv_usec = pvt->rxcore.tv_usec;
-		af.delivery.tv_sec += next / 1000;
-		af.delivery.tv_usec += (next % 1000) * 1000;
+		af.delivery = ast_tvadd(pvt->rxcore, ast_samp2tv(next, 1000));
 		af.offset=AST_FRIENDLY_OFFSET;
-		if (af.delivery.tv_usec >= 1000000) {
-			af.delivery.tv_usec -= 1000000;
-			af.delivery.tv_sec += 1;
-		}
 
 		/* queue the frame:  For consistency, we would call __do_deliver here, but __do_deliver wants an iax_frame,
 		 * which we'd need to malloc, and then it would free it.  That seems like a drag */
@@ -2233,8 +2221,7 @@
 				ast_log(LOG_DEBUG, "schedule_delivery: call=%d: TS jumped.  resyncing rxcore (ts=%d, last=%d)\n",
 							fr->callno, fr->ts, iaxs[fr->callno]->last);
 			/* zap rxcore - calc_rxstamp will make a new one based on this frame */
-			iaxs[fr->callno]->rxcore.tv_sec = 0;
-			iaxs[fr->callno]->rxcore.tv_usec = 0;
+			iaxs[fr->callno]->rxcore = ast_tv(0, 0);
 			/* wipe "last" if stamps have jumped backwards */
 			if (x<0)
 				iaxs[fr->callno]->last = 0;
@@ -2261,22 +2248,13 @@
 
 
 	/* delivery time is sender's sent timestamp converted back into absolute time according to our clock */
-	if ( (!fromtrunk) && (iaxs[fr->callno]->rxcore.tv_sec || iaxs[fr->callno]->rxcore.tv_usec) ) {
-		fr->af.delivery.tv_sec = iaxs[fr->callno]->rxcore.tv_sec;
-		fr->af.delivery.tv_usec = iaxs[fr->callno]->rxcore.tv_usec;
-		fr->af.delivery.tv_sec += fr->ts / 1000;
-		fr->af.delivery.tv_usec += (fr->ts % 1000) * 1000;
-		if (fr->af.delivery.tv_usec >= 1000000) {
-			fr->af.delivery.tv_usec -= 1000000;
-			fr->af.delivery.tv_sec += 1;
-		}
-	}
+	if ( !fromtrunk && !ast_tvzero(iaxs[fr->callno]->rxcore))
+		fr->af.delivery = ast_tvadd(iaxs[fr->callno]->rxcore, ast_samp2tv(fr->ts, 1000));
 	else {
 #if 0
 		ast_log(LOG_DEBUG, "schedule_delivery: set delivery to 0 as we don't have an rxcore yet, or frame is from trunk.\n");
 #endif
-		fr->af.delivery.tv_sec = 0;
-		fr->af.delivery.tv_usec = 0;
+		fr->af.delivery = ast_tv(0,0);
 	}
 
 #ifndef NEWJB
@@ -3138,9 +3116,8 @@
 		if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
 			/* Call has been transferred.  We're no longer involved */
 			gettimeofday(&tv, NULL);
-			if (!waittimer.tv_sec && !waittimer.tv_usec) {
-				waittimer.tv_sec = tv.tv_sec;
-				waittimer.tv_usec = tv.tv_usec;
+			if (ast_tvzero(waittimer)) {
+				waittimer = tv;
 			} else if (tv.tv_sec - waittimer.tv_sec > IAX_LINGER_TIMEOUT) {
 				c0->_softhangup |= AST_SOFTHANGUP_DEV;
 				c1->_softhangup |= AST_SOFTHANGUP_DEV;
@@ -3359,21 +3336,17 @@
 	long int ms, pred;
 
 	tpeer->trunkact = *tv;
-	mssincetx = (tv->tv_sec - tpeer->lasttxtime.tv_sec) * 1000 +
-			(1000000 + tv->tv_usec - tpeer->lasttxtime.tv_usec) / 1000 - 1000;
-	if (mssincetx > 5000 || (!tpeer->txtrunktime.tv_sec && !tpeer->txtrunktime.tv_usec)) {
+	mssincetx = ast_tvdiff_ms(*tv, tpeer->lasttxtime);
+	if (mssincetx > 5000 || ast_tvzero(tpeer->txtrunktime)) {
 		/* If it's been at least 5 seconds since the last time we transmitted on this trunk, reset our timers */
-		tpeer->txtrunktime.tv_sec = tv->tv_sec;
-		tpeer->txtrunktime.tv_usec = tv->tv_usec;
+		tpeer->txtrunktime = *tv;
 		tpeer->lastsent = 999999;
 	}
 	/* Update last transmit time now */
-	tpeer->lasttxtime.tv_sec = tv->tv_sec;
-	tpeer->lasttxtime.tv_usec = tv->tv_usec;
+	tpeer->lasttxtime = *tv;
 	
 	/* Calculate ms offset */
-	ms = (tv->tv_sec - tpeer->txtrunktime.tv_sec) * 1000 +
-		(1000000 + tv->tv_usec - tpeer->txtrunktime.tv_usec) / 1000 - 1000;
+	ms = ast_tvdiff_ms(*tv, tpeer->txtrunktime);
 	/* Predict from last value */
 	pred = tpeer->lastsent + sampms;
 	if (abs(ms - pred) < MAX_TIMESTAMP_SKEW)
@@ -3389,34 +3362,20 @@
 static unsigned int fix_peerts(struct timeval *tv, int callno, unsigned int ts)
 {
 	long ms;	/* NOT unsigned */
-	if (!iaxs[callno]->rxcore.tv_sec && !iaxs[callno]->rxcore.tv_usec) {
+	if (ast_tvzero(iaxs[callno]->rxcore)) {
 		/* Initialize rxcore time if appropriate */
 		gettimeofday(&iaxs[callno]->rxcore, NULL);
 		/* Round to nearest 20ms so traces look pretty */
 		iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
 	}
 	/* Calculate difference between trunk and channel */
-	ms = (tv->tv_sec - iaxs[callno]->rxcore.tv_sec) * 1000 + 
-		(1000000 + tv->tv_usec - iaxs[callno]->rxcore.tv_usec) / 1000 - 1000;
+	ms = ast_tvdiff_ms(*tv, iaxs[callno]->rxcore);
 	/* Return as the sum of trunk time and the difference between trunk and real time */
 	return ms + ts;
 }
 
-static void add_ms(struct timeval *tv, int ms) {
-	tv->tv_usec += ms * 1000;
-	if(tv->tv_usec > 1000000) {
-		tv->tv_usec -= 1000000;
-		tv->tv_sec++;
-	}
-	if(tv->tv_usec < 0) {
-		tv->tv_usec += 1000000;
-		tv->tv_sec--;
-	}
-}
-
 static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, struct ast_frame *f)
 {
-	struct timeval tv;
 	int ms;
 	int voice = 0;
 	int genuine = 0;
@@ -3439,7 +3398,7 @@
 			p->notsilenttx = 0;	
 		}
 	}
-	if (!p->offset.tv_sec && !p->offset.tv_usec) {
+	if (ast_tvzero(p->offset)) {
 		gettimeofday(&p->offset, NULL);
 		/* Round to nearest 20ms for nice looking traces */
 		p->offset.tv_usec -= p->offset.tv_usec % 20000;
@@ -3448,15 +3407,12 @@
 	if (ts)
 		return ts;
 	/* If we have a time that the frame arrived, always use it to make our timestamp */
-	if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
-		ms = (delivery->tv_sec - p->offset.tv_sec) * 1000 +
-			(1000000 + delivery->tv_usec - p->offset.tv_usec) / 1000 - 1000;
+	if (delivery && !ast_tvzero(*delivery)) {
+		ms = ast_tvdiff_ms(*delivery, p->offset);
 		if (option_debug > 2)
 			ast_log(LOG_DEBUG, "calc_timestamp: call %d/%d: Timestamp slaved to delivery time\n", p->callno, iaxs[p->callno]->peercallno);
 	} else {
-		gettimeofday(&tv, NULL);
-		ms = (tv.tv_sec - p->offset.tv_sec) * 1000 +
-			(1000000 + tv.tv_usec - p->offset.tv_usec) / 1000 - 1000;
+		ms = ast_tvdiff_ms(ast_tvnow(), p->offset);
 		if (ms < 0)
 			ms = 0;
 		if (voice) {
@@ -3464,7 +3420,8 @@
 			if (p->notsilenttx && abs(ms - p->nextpred) <= MAX_TIMESTAMP_SKEW) {
 				/* Adjust our txcore, keeping voice and 
 					non-voice synchronized */
-				add_ms(&p->offset, (int)(ms - p->nextpred)/10);
+				p->offset = ast_tvadd(p->offset,
+						ast_samp2tv((ms - p->nextpred)/10, 1000)); /* XXX what scale is this ??? */
 
 				if (!p->nextpred) {
 					p->nextpred = ms; /*f->samples / 8;*/
@@ -3525,19 +3482,18 @@
 	/* Receive from p1, send to p2 */
 	
 	/* Setup rxcore if necessary on outgoing channel */
-	if (!p1->rxcore.tv_sec && !p1->rxcore.tv_usec)
-		gettimeofday(&p1->rxcore, NULL);
+	if (ast_tvzero(p1->rxcore))
+		p1->rxcore = ast_tvnow();
 
 	/* Setup txcore if necessary on outgoing channel */
-	if (!p2->offset.tv_sec && !p2->offset.tv_usec)
-		gettimeofday(&p2->offset, NULL);
+	if (ast_tvzero(p2->offset))
+		p2->offset = ast_tvnow();
 	
 	/* Now, ts is the timestamp of the original packet in the orignal context.
 	   Adding rxcore to it gives us when we would want the packet to be delivered normally.
 	   Subtracting txcore of the outgoing channel gives us what we'd expect */
 	
-	ms = (p1->rxcore.tv_sec - p2->offset.tv_sec) * 1000 +
-		(1000000 + p1->rxcore.tv_usec - p2->offset.tv_usec) / 1000 - 1000;
+	ms = ast_tvdiff_ms(p1->rxcore, p2->offset);
 	fakets += ms;
 
 	/* FIXME? SLD would rather remove this and leave it to the end system to deal with */
@@ -3552,23 +3508,17 @@
 {
 	/* Returns where in "receive time" we are.  That is, how many ms
 	   since we received (or would have received) the frame with timestamp 0 */
-	struct timeval tv;
 	int ms;
 #ifdef IAXTESTS
 	int jit;
 #endif /* IAXTESTS */
 	/* Setup rxcore if necessary */
-	if (!p->rxcore.tv_sec && !p->rxcore.tv_usec) {
-		gettimeofday(&p->rxcore, NULL);
+	if (ast_tvzero(p->rxcore)) {
+		p->rxcore = ast_tvnow();
 		if (option_debug)
 			ast_log(LOG_DEBUG, "calc_rxstamp: call=%d: rxcore set to %d.%6.6d - %dms\n",
 					p->callno, (int)(p->rxcore.tv_sec), (int)(p->rxcore.tv_usec), offset);
-		p->rxcore.tv_sec -= offset / 1000;
-		p->rxcore.tv_usec -= (offset % 1000) * 1000;
-		if (p->rxcore.tv_usec < 0) {
-			p->rxcore.tv_usec += 1000000;
-			p->rxcore.tv_sec -= 1;
-		}
+		p->rxcore = ast_tvsub(p->rxcore, ast_samp2tv(offset, 1000));
 #if 1
 		if (option_debug)
 			ast_log(LOG_DEBUG, "calc_rxstamp: call=%d: works out as %d.%6.6d\n",
@@ -3576,9 +3526,7 @@
 #endif
 	}
 
-	gettimeofday(&tv, NULL);
-	ms = (tv.tv_sec - p->rxcore.tv_sec) * 1000 +
-		(1000000 + tv.tv_usec - p->rxcore.tv_usec) / 1000 - 1000;
+	ms = ast_tvdiff_ms(ast_tvnow(), p->rxcore);
 #ifdef IAXTESTS
 	if (test_jit) {
 		if (!test_jitpct || ((100.0 * rand() / (RAND_MAX + 1.0)) < test_jitpct)) {
@@ -3618,7 +3566,7 @@
 			ast_mutex_init(&tpeer->lock);
 			tpeer->lastsent = 9999;
 			memcpy(&tpeer->addr, sin, sizeof(tpeer->addr));
-			gettimeofday(&tpeer->trunkact, NULL);
+			tpeer->trunkact = ast_tvnow();
 			ast_mutex_lock(&tpeer->lock);
 			tpeer->next = tpeers;
 			tpeer->sockfd = fd;
@@ -6225,11 +6173,9 @@
 				ast_log(LOG_WARNING, "Unable to accept trunked packet from '%s:%d': No matching peer\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
 				return 1;
 			}
-			if (!ts || (!tpeer->rxtrunktime.tv_sec && !tpeer->rxtrunktime.tv_usec)) {
-				gettimeofday(&tpeer->rxtrunktime, NULL);
-				tpeer->trunkact = tpeer->rxtrunktime;
-			} else
-				gettimeofday(&tpeer->trunkact, NULL);
+			tpeer->trunkact = ast_tvnow();
+			if (!ts || ast_tvzero(tpeer->rxtrunktime))
+				tpeer->rxtrunktime = tpeer->trunkact;
 			rxtrunktime = tpeer->rxtrunktime;
 			ast_mutex_unlock(&tpeer->lock);
 			while(res >= sizeof(struct ast_iax2_meta_trunk_entry)) {
@@ -8755,8 +8701,7 @@
 	while(dp) {
 		next = dp->next;
 		/* Expire old caches */
-		if ((tv.tv_sec > dp->expirey.tv_sec) ||
-				((tv.tv_sec == dp->expirey.tv_sec) && (tv.tv_usec > dp->expirey.tv_usec)))  {
+		if (ast_tvcmp(tv, dp->expirey) > 0) {
 				/* It's expired, let it disappear */
 				if (prev)
 					prev->next = dp->next;

Index: chan_phone.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_phone.c,v
retrieving revision 1.54
retrieving revision 1.55
diff -u -d -r1.54 -r1.55
--- chan_phone.c	24 Jun 2005 02:15:04 -0000	1.54
+++ chan_phone.c	15 Jul 2005 23:00:46 -0000	1.55
@@ -446,8 +446,7 @@
 	p->fr.src = type;
 	p->fr.offset = 0;
 	p->fr.mallocd=0;
-	p->fr.delivery.tv_sec = 0;
-	p->fr.delivery.tv_usec = 0;
+	p->fr.delivery = ast_tv(0,0);
 	
 	phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
 	if (phonee.bits.dtmf_ready)  {
@@ -509,8 +508,7 @@
 	p->fr.src = type;
 	p->fr.offset = 0;
 	p->fr.mallocd=0;
-	p->fr.delivery.tv_sec = 0;
-	p->fr.delivery.tv_usec = 0;
+	p->fr.delivery = ast_tv(0,0);
 
 	/* Try to read some data... */
 	CHECK_BLOCKING(ast);
@@ -993,7 +991,7 @@
 				if (i->dialtone) {
 					/* Remember we're going to have to come back and play
 					   more dialtones */
-					if (!tv.tv_usec && !tv.tv_sec) {
+					if (ast_tvzero(tv)) {
 						/* If we're due for a dialtone, play one */
 						if (write(i->fd, DialTone + tonepos, 240) != 240)
 							ast_log(LOG_WARNING, "Dial tone write error\n");
@@ -1016,15 +1014,13 @@
 			tonepos += 240;
 			if (tonepos >= sizeof(DialTone))
 					tonepos = 0;
-			if (!tv.tv_usec && !tv.tv_sec) {
-				tv.tv_usec = 30000;
-				tv.tv_sec = 0;
+			if (ast_tvzero(tv)) {
+				tv = ast_tv(30000, 0);
 			}
 			res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
 		} else {
 			res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
-			tv.tv_usec = 0;
-			tv.tv_sec = 0;
+			tv = ast_tv(0,0);
 			tonepos = 0;
 		}
 		/* Okay, select has finished.  Let's see what happened.  */

Index: chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.780
retrieving revision 1.781
diff -u -d -r1.780 -r1.781
--- chan_sip.c	15 Jul 2005 22:06:15 -0000	1.780
+++ chan_sip.c	15 Jul 2005 23:00:46 -0000	1.781
@@ -8351,8 +8351,7 @@
 		int newstate = 0;
 		peer = p->peerpoke;
 		gettimeofday(&tv, NULL);
-		pingtime = (tv.tv_sec - peer->ps.tv_sec) * 1000 +
-					(tv.tv_usec - peer->ps.tv_usec) / 1000;
+		pingtime = ast_tvdiff_ms(tv, peer->ps);
 		if (pingtime < 1)
 			pingtime = 1;
 		if ((peer->lastms < 0)  || (peer->lastms > peer->maxms)) {

Index: chan_vpb.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_vpb.c,v
retrieving revision 1.89
retrieving revision 1.90
diff -u -d -r1.89 -r1.90
--- chan_vpb.c	24 Jun 2005 02:15:04 -0000	1.89
+++ chan_vpb.c	15 Jul 2005 23:00:46 -0000	1.90
@@ -293,7 +293,7 @@
 
 	struct ast_dsp *vad;			/* AST  Voice Activation Detection dsp */
 
-	double lastgrunt;			/* time stamp (secs since epoc) of last grunt event */
+	struct timeval lastgrunt;			/* time stamp of last grunt event */
 
 	ast_mutex_t lock;			/* This one just protects bridge ptr below */
 	vpb_bridge_t *bridge;
@@ -584,13 +584,6 @@
 	return (res==VPB_OK)?0:-1;
 }
 
-static double get_time_in_ms()
-{
-	struct timeval tv;
-	gettimeofday(&tv, NULL);
-	return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000);
-}
-
 /* Caller ID can be located in different positions between the rings depending on your Telco
  * Australian (Telstra) callerid starts 700ms after 1st ring and finishes 1.5s after first ring
  * Use ANALYSE_CID to record rings and determine location of callerid
@@ -602,7 +595,7 @@
 static void get_callerid(struct vpb_pvt *p)
 {
 	short buf[CID_MSECS*8]; /* 8kHz sampling rate */
-	double cid_record_time;
+	struct timeval cid_record_time;
 	int rc;
 	struct ast_channel *owner = p->owner;
 /*
@@ -616,7 +609,7 @@
 
 	if( ast_mutex_trylock(&p->record_lock) == 0 ) {
 
-		cid_record_time = get_time_in_ms();
+		cid_record_time = ast_tvnow();
 		if (option_verbose>3) 
 			ast_verbose(VERBOSE_PREFIX_4 "CID record - start\n");
 
@@ -624,9 +617,9 @@
 		vpb_sleep(RING_SKIP);
 
 		if (option_verbose>3) 
-			ast_verbose(VERBOSE_PREFIX_4 "CID record - skipped %fms trailing ring\n",
-				 get_time_in_ms() - cid_record_time);
-		cid_record_time = get_time_in_ms();
+			ast_verbose(VERBOSE_PREFIX_4 "CID record - skipped %ldms trailing ring\n",
+				 ast_tvdiff_ms(ast_tvnow(), cid_record_time));
+		cid_record_time = ast_tvnow();
 
 		/* Record bit between the rings which contains the callerid */
 		vpb_record_buf_start(p->handle, VPB_LINEAR);
@@ -639,8 +632,8 @@
 #endif
 
 		if (option_verbose>3) 
-			ast_verbose(VERBOSE_PREFIX_4 "CID record - recorded %fms between rings\n", 
-				get_time_in_ms() - cid_record_time);
+			ast_verbose(VERBOSE_PREFIX_4 "CID record - recorded %ldms between rings\n", 
+				 ast_tvdiff_ms(ast_tvnow(), cid_record_time));
 
 		ast_mutex_unlock(&p->record_lock);
 
@@ -911,7 +904,7 @@
 
 			} 
 			else if (e->data == VPB_GRUNT) {
-				if( ( get_time_in_ms() - p->lastgrunt ) > gruntdetect_timeout ) {
+				if( ( ast_tvdiff_ms(ast_tvnow(), p->lastgrunt) > gruntdetect_timeout ) {
 					/* Nothing heard on line for a very long time
 					 * Timeout connection */
 					if (option_verbose > 2) 
@@ -919,7 +912,7 @@
 					ast_log(LOG_NOTICE,"%s: Line hangup due of lack of conversation\n",p->dev); 
 					f.subclass = AST_CONTROL_HANGUP;
 				} else {
-					p->lastgrunt = get_time_in_ms();
+					p->lastgrunt = ast_tvnow();
 					f.frametype = -1;
 				}
 			} 
@@ -2193,7 +2186,7 @@
 {
 	struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt; 
 	int res = 0, fmt = 0;
-	struct timeval play_buf_time_start,play_buf_time_finish;
+	struct timeval play_buf_time_start;
 /*	ast_mutex_lock(&p->lock); */
 	if(option_verbose>5) 
 		ast_verbose("%s: vpb_write: Writing to channel\n", p->dev);
@@ -2249,25 +2242,14 @@
 
 /*	ast_log(LOG_DEBUG, "%s: vpb_write: Applied gain..\n", p->dev); */
 
-/*	gettimeofday(&tv, NULL); */
-/*	return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000); */
-
 	if ((p->read_state == 1)&&(p->play_buf_time<5)){
-		gettimeofday(&play_buf_time_start,NULL);
+		play_buf_time_start = ast_tvnow();
 		res = vpb_play_buf_sync(p->handle, (char*)frame->data, frame->datalen);
-		if( (res == VPB_OK) && (option_verbose > 5) ) {
+		if( res == VPB_OK && option_verbose > 5 ) {
 			short * data = (short*)frame->data;
 			ast_verbose("%s: vpb_write: Wrote chan (codec=%d) %d %d\n", p->dev, fmt, data[0],data[1]);
 		}
-		gettimeofday(&play_buf_time_finish,NULL);
-		if (play_buf_time_finish.tv_sec == play_buf_time_start.tv_sec){
-			p->play_buf_time=(int)((play_buf_time_finish.tv_usec-play_buf_time_start.tv_usec)/1000);
-	/*		ast_log(LOG_DEBUG, "%s: vpb_write: Timing start(%d) finish(%d)\n", p->dev,play_buf_time_start.tv_usec,play_buf_time_finish.tv_usec); */
-		}
-		else {
-			p->play_buf_time=(int)((play_buf_time_finish.tv_sec - play_buf_time_start.tv_sec)*100)+(int)((play_buf_time_finish.tv_usec-play_buf_time_start.tv_usec)/1000);
-		}
-	/*	ast_log(LOG_DEBUG, "%s: vpb_write: Wrote data [%d](%d=>%s) to play_buf in [%d]ms..\n", p->dev,frame->datalen,fmt,ast2vpbformatname(frame->subclass),p->play_buf_time); */
+		p->play_buf_time = ast_tvdiff_ms(ast_tvnow(), play_buf_time_start);
 	}
 	else {
 		p->chuck_count++;
@@ -2603,7 +2585,7 @@
 		me->play_dtmf[0] = '\0';
 		me->faxhandled =0;
 		
-		me->lastgrunt  = get_time_in_ms(); /* Assume at least one grunt tone seen now. */
+		me->lastgrunt  = ast_tvnow(); /* Assume at least one grunt tone seen now. */
 
 		ast_mutex_lock(&usecnt_lock);
 		usecnt++;

Index: chan_zap.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_zap.c,v
retrieving revision 1.476
retrieving revision 1.477
diff -u -d -r1.476 -r1.477
--- chan_zap.c	15 Jul 2005 16:13:26 -0000	1.476
+++ chan_zap.c	15 Jul 2005 23:00:46 -0000	1.477
@@ -3458,7 +3458,6 @@
 							p->dialing = 1;
 						zt_ring_phone(p);
 					} else if (p->subs[SUB_THREEWAY].owner) {
-						struct timeval tv;
 						unsigned int mssinceflash;
 						/* Here we have to retain the lock on both the main channel, the 3-way channel, and
 						   the private structure -- not especially easy or clean */
@@ -3481,8 +3480,7 @@
 							ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
 							return NULL;
 						}
-						gettimeofday(&tv, NULL);
-						mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
+						mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
 						ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
 						if (mssinceflash < MIN_MS_SINCE_FLASH) {
 							/* It hasn't been long enough since the last flashook.  This is probably a bounce on 
@@ -3978,10 +3976,8 @@
 				(p->polarityonanswerdelay > 0) &&
 			        (p->polarity == POLARITY_REV) &&
 				(ast->_state == AST_STATE_UP)) {
-				struct timeval tv;
-				gettimeofday(&tv, NULL);
 
-				if((((tv.tv_sec - p->polaritydelaytv.tv_sec) * 1000) + ((tv.tv_usec - p->polaritydelaytv.tv_usec)/1000)) > p->polarityonanswerdelay) {
+				if(ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
 					ast_log(LOG_DEBUG, "Hangup due to Reverse Polarity on channel %d\n", p->channel);
 					ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
 					p->polarity = POLARITY_IDLE;
@@ -4018,8 +4014,7 @@
 	p->subs[index].f.mallocd = 0;
 	p->subs[index].f.offset = 0;
 	p->subs[index].f.subclass = 0;
-	p->subs[index].f.delivery.tv_sec = 0;
-	p->subs[index].f.delivery.tv_usec = 0;
+	p->subs[index].f.delivery = ast_tv(0,0);
 	p->subs[index].f.src = "zt_exception";
 	p->subs[index].f.data = NULL;
 	
@@ -4144,8 +4139,7 @@
 	p->subs[index].f.mallocd = 0;
 	p->subs[index].f.offset = 0;
 	p->subs[index].f.subclass = 0;
-	p->subs[index].f.delivery.tv_sec = 0;
-	p->subs[index].f.delivery.tv_usec = 0;
+	p->subs[index].f.delivery = ast_tv(0,0);
 	p->subs[index].f.src = "zt_read";
 	p->subs[index].f.data = NULL;
 	
@@ -7776,15 +7770,11 @@
 #if 0
 			printf("nextidle: %d, haveidles: %d, minunsed: %d\n",
 				nextidle, haveidles, minunused);
-			gettimeofday(&tv, NULL);
 			printf("nextidle: %d, haveidles: %d, ms: %ld, minunsed: %d\n",
-				nextidle, haveidles, (tv.tv_sec - lastidle.tv_sec) * 1000 +
-				    (tv.tv_usec - lastidle.tv_usec) / 1000, minunused);
+				nextidle, haveidles, ast_tvdiff_ms(ast_tvnow(), lastidle), minunused);
 #endif
 			if (nextidle > -1) {
-				gettimeofday(&tv, NULL);
-				if (((tv.tv_sec - lastidle.tv_sec) * 1000 +
-				    (tv.tv_usec - lastidle.tv_usec) / 1000) > 1000) {
+				if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
 					/* Don't create a new idle call more than once per second */
 					snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
 					idle = zt_request("Zap", AST_FORMAT_ULAW, idlen, &cause);
@@ -7817,49 +7807,36 @@
 			}
 		}
 		/* Start with reasonable max */
-		lowest.tv_sec = 60;
-		lowest.tv_usec = 0;
+		lowest = ast_tv(60, 0);
 		for (i=0; i<NUM_DCHANS; i++) {
 			/* Find lowest available d-channel */
 			if (!pri->dchannels[i])
 				break;
 			if ((next = pri_schedule_next(pri->dchans[i]))) {
 				/* We need relative time here */
-				gettimeofday(&tv, NULL);
-				tv.tv_sec = next->tv_sec - tv.tv_sec;
-				tv.tv_usec = next->tv_usec - tv.tv_usec;
-				if (tv.tv_usec < 0) {
-					tv.tv_usec += 1000000;
-					tv.tv_sec -= 1;
-				}
+				tv = ast_tvsub(*next, ast_tvnow());
 				if (tv.tv_sec < 0) {
-					tv.tv_sec = 0;
-					tv.tv_usec = 0;
+					tv = ast_tv(0,0);
 				}
 				if (doidling || pri->resetting) {
 					if (tv.tv_sec > 1) {
-						tv.tv_sec = 1;
-						tv.tv_usec = 0;
+						tv = ast_tv(1, 0);
 					}
 				} else {
 					if (tv.tv_sec > 60) {
-						tv.tv_sec = 60;
-						tv.tv_usec = 0;
+						tv = ast_tv(60, 0);
 					}
 				}
 			} else if (doidling || pri->resetting) {
 				/* Make sure we stop at least once per second if we're
 				   monitoring idle channels */
-				tv.tv_sec = 1;
-				tv.tv_usec = 0;
+				tv = ast_tv(1,0);
 			} else {
 				/* Don't poll for more than 60 seconds */
-				tv.tv_sec = 60;
-				tv.tv_usec = 0;
+				tv = ast_tv(60, 0);
 			}
-			if (!i || (tv.tv_sec < lowest.tv_sec) || ((tv.tv_sec == lowest.tv_sec) && (tv.tv_usec < lowest.tv_usec))) {
-				lowest.tv_sec = tv.tv_sec;
-				lowest.tv_usec = tv.tv_usec;
+			if (!i || ast_tvcmp(tv, lowest) < 0) {
+				lowest = tv;
 			}
 		}
 		ast_mutex_unlock(&pri->lock);




More information about the svn-commits mailing list