]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - apps/tutorials/BlinkConfig/BlinkConfigC.nc
use the same data structure to calculate lengths in transmit and in receive
[tinyos-2.x.git] / apps / tutorials / BlinkConfig / BlinkConfigC.nc
index 91853c7da908484474f6db6a4549fe25ffbcfa14..928e89820d783fba1b182542358fdf4826f6ec29 100644 (file)
  */
 
 /**
- * Application to demonstrate the ConfigStorageC abstraction.  A value
- * is written to, and read from, the flash storage. A successful test
- * will turn on both the green and blue (yellow) LEDs.  A failed test
- * is any other LED combination..
+ * Application to demonstrate the ConfigStorageC abstraction.  A timer
+ * period is read from flash, divided by two, and written back to
+ * flash.  An LED is toggled each time the timer fires.
  *
- * @author Prabal Dutta
+ * @author Prabal Dutta <prabal@cs.berkeley.edu>
  */
+#include <Timer.h>
+
 module BlinkConfigC {
   uses {
     interface Boot;
     interface Leds;
     interface ConfigStorage as Config;
-    interface AMSend;
-    interface SplitControl as AMControl;
     interface Mount as Mount;
+    interface Timer<TMilli> as Timer0;
   }
 }
 implementation {
-  uint16_t period = 2048;
-  uint16_t period2 = 1024;
+
+  typedef struct config_t {
+    uint16_t version;
+    uint16_t period;
+  } config_t;
 
   enum {
     CONFIG_ADDR = 0,
+    CONFIG_VERSION = 1,
+    DEFAULT_PERIOD = 1024,
+    MIN_PERIOD     = 128,
+    MAX_PERIOD     = 1024
   };
 
+  uint8_t state;
+  config_t conf;
+
   event void Boot.booted() {
-    call AMControl.start();
-  }
+    conf.period = DEFAULT_PERIOD;
 
-  event void AMControl.startDone(error_t error) {
-    if (error != SUCCESS) {
-      call AMControl.start();
-    }
     if (call Mount.mount() != SUCCESS) {
       // Handle failure
     }
   }
 
   event void Mount.mountDone(error_t error) {
-    if (error != SUCCESS) {
-      // Handle failure
+    if (error == SUCCESS) {
+      if (call Config.valid() == TRUE) {
+        if (call Config.read(CONFIG_ADDR, &conf, sizeof(conf)) != SUCCESS) {
+          // Handle failure
+       }
+      }
+      else {
+       // Invalid volume.  Commit to make valid.
+       call Leds.led1On();
+       if (call Config.commit() == SUCCESS) {
+         call Leds.led0On();
+       }
+       else {
+         // Handle failure
+       }
+      }
     }
     else{
-      call Config.write(CONFIG_ADDR, &period, sizeof(period));
+      // Handle failure
     }
   }
 
-  event void Config.writeDone(storage_addr_t addr, void *buf, 
-    storage_len_t len, error_t result) {
-    // Verify addr and len
+  event void Config.readDone(storage_addr_t addr, void* buf, 
+    storage_len_t len, error_t err) __attribute__((noinline)) {
 
-    if (result == SUCCESS) {
-      // Note success
+    if (err == SUCCESS) {
+      memcpy(&conf, buf, len);
+      if (conf.version == CONFIG_VERSION) {
+        conf.period = conf.period/2;
+       conf.period = conf.period > MAX_PERIOD ? MAX_PERIOD : conf.period;
+        conf.period = conf.period < MIN_PERIOD ? MAX_PERIOD : conf.period;
+      }
+      else {
+        // Version mismatch. Restore default.
+       call Leds.led1On();
+        conf.version = CONFIG_VERSION;
+        conf.period = DEFAULT_PERIOD;
+      }
+      call Leds.led0On();
+      call Config.write(CONFIG_ADDR, &conf, sizeof(conf));
     }
     else {
-      // Handle failure
-    }
-    if (call Config.commit() != SUCCESS) {
-      // Handle failure
+      // Handle failure.
     }
   }
 
-  event void Config.commitDone(error_t error) {
-    if (call Config.read(CONFIG_ADDR, &period2, sizeof(period2)) != SUCCESS) {
-      // Handle failure
-    }
-  }
-
-  event void Config.readDone(storage_addr_t addr, void* buf, 
-    storage_len_t len, error_t result) __attribute__((noinline)) {
-    memcpy(&period2, buf, len);
+  event void Config.writeDone(storage_addr_t addr, void *buf, 
+    storage_len_t len, error_t err) {
+    // Verify addr and len
 
-    if (period == period2) {
-      call Leds.led2On();
+    if (err == SUCCESS) {
+      if (call Config.commit() != SUCCESS) {
+        // Handle failure
+      }
     }
-
-    if (len == 2 && addr == CONFIG_ADDR) {
-      call Leds.led1On();
+    else {
+      // Handle failure
     }
   }
 
-  event void AMSend.sendDone(message_t* msg, error_t error) {
-    if (error != SUCCESS) {
-      call Leds.led0On();
+  event void Config.commitDone(error_t err) {
+    call Leds.led0Off();
+    call Timer0.startPeriodic(conf.period);
+    if (err == SUCCESS) {
+      // Handle failure
     }
   }
 
-  event void AMControl.stopDone(error_t error) {
+  event void Timer0.fired() {
+    call Leds.led2Toggle();
   }
 }