]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/power/DeferredPowerManagerP.nc
Merge TinyOS 2.1.1 into master.
[tinyos-2.x.git] / tos / lib / power / DeferredPowerManagerP.nc
index c3184bac13e9f8d57c6ef271259a8f62e09ad703..209b5ffcbad38767825644d862454b77ac431a87 100644 (file)
@@ -29,6 +29,8 @@
  */
  
 /**
+ * Please refer to TEP 115 for more information about this component and its
+ * intended use.<br><br>
  *
  * This is the internal implementation of the deffered power management
  * policy for managing the power states of non-virtualized devices.
  *                        before shutting down the device once it is free.
  * 
  * @author Kevin Klues (klueska@cs.wustl.edu)
- * @see  Please refer to TEP 115 for more information about this component and its
- *          intended use.
  */
  
 generic module DeferredPowerManagerP(uint32_t delay) {
-  provides {
-    interface Init;
-  }
   uses {
     interface StdControl;
     interface SplitControl;
 
     interface PowerDownCleanup;
-    interface Init as ArbiterInit;
-    interface ResourceController;
+    interface ResourceDefaultOwner;
     interface ArbiterInfo;
     interface Timer<TMilli> as TimerMilli;
   }
 }
 implementation {
 
-  norace struct {
-   uint8_t stopping :1;
-   uint8_t requested :1;
-  } f; //for flags
+  norace bool stopping = FALSE;
+  norace bool requested  = FALSE;
+  norace bool stopTimer = FALSE;
 
-  task void startTask() { 
+  task void startTask() {
+    call TimerMilli.stop();
+    stopTimer = FALSE;
     call StdControl.start();
-    call SplitControl.start();
+    if (call SplitControl.start()==EALREADY)
+      call ResourceDefaultOwner.release();
   }
+
   task void timerTask() { 
     call TimerMilli.startOneShot(delay); 
   }
 
-  command error_t Init.init() {
-    f.stopping = FALSE;
-    f.requested = FALSE;
-    call ArbiterInit.init();
-    call ResourceController.immediateRequest();
-    return SUCCESS;
+  async event void ResourceDefaultOwner.requested() {
+    if(stopping == FALSE) {
+      stopTimer = TRUE;
+      post startTask();
+    }
+    else requested = TRUE;
   }
 
-  async event void ResourceController.requested() {
-    if(f.stopping == FALSE)
-      post startTask();
-    else atomic f.requested = TRUE;
+  async event void ResourceDefaultOwner.immediateRequested() {
   }
 
   default command error_t StdControl.start() {
@@ -102,37 +98,36 @@ implementation {
   }
 
   event void SplitControl.startDone(error_t error) {
-    call ResourceController.release();
+    call ResourceDefaultOwner.release();
   }
 
-  async event void ResourceController.idle() {
-    if(!(call ArbiterInfo.inUse()))
-      post timerTask();
+  async event void ResourceDefaultOwner.granted() {
+    post timerTask();
   }
 
   event void TimerMilli.fired() {
-    if(call ResourceController.immediateRequest() == SUCCESS) {
-      f.stopping = TRUE;
-      call PowerDownCleanup.cleanup();
-      call StdControl.stop();
-      call SplitControl.stop();
+    atomic {
+      if(stopTimer == FALSE) {
+        stopping = TRUE;
+        call PowerDownCleanup.cleanup();
+        call StdControl.stop();
+        if (call SplitControl.stop()==EALREADY)
+          signal SplitControl.stopDone(SUCCESS);
+      }
     }
   }
 
   event void SplitControl.stopDone(error_t error) {
-    if(f.requested == TRUE) {
+    if(requested == TRUE) {
       call StdControl.start();
       call SplitControl.start();
     }
     atomic {
-      f.requested = FALSE;
-      f.stopping = FALSE;
+      requested = FALSE;
+      stopping = FALSE;
     }
   }
 
-  event void ResourceController.granted() {
-  }
-
   default command error_t StdControl.stop() {
     return SUCCESS;
   }