[zaptel-commits] kpfleming: branch kpfleming/battery_alarms r3900 - /team/kpfleming/battery_al...

SVN commits to the Zaptel project zaptel-commits at lists.digium.com
Thu Feb 28 09:02:00 CST 2008


Author: kpfleming
Date: Thu Feb 28 09:01:59 2008
New Revision: 3900

URL: http://svn.digium.com/view/zaptel?view=rev&rev=3900
Log:
simplify battery debouncing code, eliminating repeated setting/decrementing of the debounce timer when no changes are occurring, and also heavily comment debouncing code so the next person doesn't have to figure out how it works :-)

Modified:
    team/kpfleming/battery_alarms/kernel/wctdm.c

Modified: team/kpfleming/battery_alarms/kernel/wctdm.c
URL: http://svn.digium.com/view/zaptel/team/kpfleming/battery_alarms/kernel/wctdm.c?view=diff&rev=3900&r1=3899&r2=3900
==============================================================================
--- team/kpfleming/battery_alarms/kernel/wctdm.c (original)
+++ team/kpfleming/battery_alarms/kernel/wctdm.c Thu Feb 28 09:01:59 2008
@@ -287,7 +287,7 @@
 };
 
 enum battery_state {
-	BATTERY_UNKNOWN,
+	BATTERY_UNKNOWN = 0,
 	BATTERY_PRESENT,
 	BATTERY_LOST,
 };
@@ -923,67 +923,88 @@
 	}
 #endif
 	b = wc->reg1shadow[card];
-#if 0 
-	{
-		static int count = 0;
-		if (!(count++ % 100)) {
-			printk("Card %d: Voltage: %d  Debounce %d\n", card + 1, 
-			       b, fxo->battdebounce);
-		}
-	}
-#endif	
 	if (abs(b) < battthresh) {
-#if 0
-		if (fxo->battery == BATTERY_PRESENT)
-			printk("Battery loss: %d (%d debounce)\n", b, fxo->battdebounce);
+		/* possible existing states:
+		   battery lost, no debounce timer
+		   battery lost, debounce timer (going to battery present)
+		   battery present or unknown, no debounce timer
+		   battery present or unknown, debounce timer (going to battery lost)
+		*/
+
+		if (fxo->battery == BATTERY_LOST) {
+			if (fxo->battdebounce) {
+				/* we were going to BATTERY_PRESENT, but battery was lost again,
+				   so clear the debounce timer */
+				fxo->battdebounce = 0;
+			}
+		} else {
+			if (fxo->battdebounce) {
+				/* going to BATTERY_LOST, see if we are there yet */
+				if (--fxo->battdebounce == 0) {
+					fxo->battery = BATTERY_LOST;
+					if (debug)
+						printk("NO BATTERY on %d/%d!\n", wc->span.spanno, card + 1);
+#ifdef	JAPAN
+					if (!wc->ohdebounce && wc->offhook) {
+						zt_hooksig(&wc->chans[card], ZT_RXSIG_ONHOOK);
+						if (debug)
+							printk("Signalled On Hook\n");
+#ifdef	ZERO_BATT_RING
+						wc->onhook++;
 #endif
-		if ((fxo->battery != BATTERY_LOST) &&
-		    (fxo->battdebounce == 0)) {
-			if (debug)
-				printk("NO BATTERY on %d/%d!\n", wc->span.spanno, card + 1);
-			fxo->battery = BATTERY_LOST;
-#ifdef	JAPAN
-			if ((!wc->ohdebounce) && wc->offhook) {
-				zt_hooksig(&wc->chans[card], ZT_RXSIG_ONHOOK);
-				if (debug)
-					printk("Signalled On Hook\n");
+					}
+#else
+					zt_hooksig(&wc->chans[card], ZT_RXSIG_ONHOOK);
+					/* set the alarm timer, taking into account that part of its time
+					   period has already passed while debouncing occurred */
+					fxo->battalarm = battalarm - battdebounce;
+#endif
+				}
+			} else {
+				/* start the debounce timer to verify that battery has been lost */
+				fxo->battdebounce = battdebounce;
+			}
+		}
+	} else {
+		/* possible existing states:
+		   battery lost or unknown, no debounce timer
+		   battery lost or unknown, debounce timer (going to battery present)
+		   battery present, no debounce timer
+		   battery present, debounce timer (going to battery lost)
+		*/
+
+		if (fxo->battery == BATTERY_PRESENT) {
+			if (fxo->battdebounce) {
+				/* we were going to BATTERY_LOST, but battery appeared again,
+				   so clear the debounce timer */
+				fxo->battdebounce = 0;
+			}
+		} else {
+			if (fxo->battdebounce) {
+				/* going to BATTERY_PRESENT, see if we are there yet */
+				if (--fxo->battdebounce == 0) {
+					fxo->battery = BATTERY_PRESENT;
+					if (debug)
+						printk("BATTERY on %d/%d (%s)!\n", wc->span.spanno, card + 1, 
+						       (b < 0) ? "-" : "+");			    
 #ifdef	ZERO_BATT_RING
-				wc->onhook++;
+					if (wc->onhook) {
+						wc->onhook = 0;
+						zt_hooksig(&wc->chans[card], ZT_RXSIG_OFFHOOK);
+						if (debug)
+							printk("Signalled Off Hook\n");
+					}
+#else
+					zt_hooksig(&wc->chans[card], ZT_RXSIG_OFFHOOK);
 #endif
+					/* set the alarm timer, taking into account that part of its time
+					   period has already passed while debouncing occurred */
+					fxo->battalarm = battalarm - battdebounce;
+				}
+			} else {
+				/* start the debounce timer to verify that battery has appeared */
+				fxo->battdebounce = battdebounce;
 			}
-#else
-			zt_hooksig(&wc->chans[card], ZT_RXSIG_ONHOOK);
-			/* set the alarm timer, taking into account that part of its time
-			   period has already passed while debouncing occurred */
-			fxo->battalarm = battalarm - battdebounce;
-#endif
-			fxo->battdebounce = battdebounce;
-		} else if (fxo->battery == BATTERY_LOST) {
-			fxo->battdebounce = battdebounce;
-		}
-	} else if (abs(b) > battthresh) {
-		if ((fxo->battery != BATTERY_PRESENT) &&
-		    (fxo->battdebounce == 0)) {
-			if (debug)
-				printk("BATTERY on %d/%d (%s)!\n", wc->span.spanno, card + 1, 
-				       (b < 0) ? "-" : "+");			    
-#ifdef	ZERO_BATT_RING
-			if (wc->onhook) {
-				wc->onhook = 0;
-				zt_hooksig(&wc->chans[card], ZT_RXSIG_OFFHOOK);
-				if (debug)
-					printk("Signalled Off Hook\n");
-			}
-#else
-			zt_hooksig(&wc->chans[card], ZT_RXSIG_OFFHOOK);
-#endif
-			fxo->battery = BATTERY_PRESENT;
-			fxo->battdebounce = battdebounce;
-			/* set the alarm timer, taking into account that part of its time
-			   period has already passed while debouncing occurred */
-			fxo->battalarm = battalarm - battdebounce;
-		} else if (fxo->battery == BATTERY_PRESENT) {
-			fxo->battdebounce = battdebounce;
 		}
 
 		if (fxo->lastpol >= 0) {
@@ -998,13 +1019,6 @@
 			fxo->polaritydebounce = POLARITY_DEBOUNCE;
 		    }
 		}
-	} else {
-		/* It's something else... */
-		fxo->battdebounce = battdebounce;
-	}
-
-	if (fxo->battdebounce) {
-		fxo->battdebounce--;
 	}
 
 	if (fxo->battalarm) {
@@ -1635,9 +1649,6 @@
 	if(debug)
 		printk("DEBUG fxotxgain:%i.%i fxorxgain:%i.%i\n", (wctdm_getreg(wc, card, 38)/16)?-(wctdm_getreg(wc, card, 38) - 16) : wctdm_getreg(wc, card, 38), (wctdm_getreg(wc, card, 40)/16)? -(wctdm_getreg(wc, card, 40) - 16):wctdm_getreg(wc, card, 40), (wctdm_getreg(wc, card, 39)/16)? -(wctdm_getreg(wc, card, 39) - 16) : wctdm_getreg(wc, card, 39),(wctdm_getreg(wc, card, 41)/16)?-(wctdm_getreg(wc, card, 41) - 16):wctdm_getreg(wc, card, 41));
 
-	/* battery state still unknown */
-	wc->mod[card].fxo.battery = BATTERY_UNKNOWN;
-	
 	return 0;
 		
 }




More information about the zaptel-commits mailing list