]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/net/Deluge/extra/NetProgM.nc
A small fix for mulle and some cleanup.
[tinyos-2.x.git] / tos / lib / net / Deluge / extra / NetProgM.nc
index ad39d2542e0d6eb61aa8472e8f2ef4b76a21b5ff..e77ebeedeb4b09780359cfa6b8eb8cc0dbe91055 100644 (file)
@@ -42,15 +42,18 @@ module NetProgM {
     interface Leds;
     interface CC2420Config;
     async command void setAmAddress(am_addr_t a);
+    interface ReprogramGuard;
   }
 }
 
 implementation {
 
+  uint32_t reprogramImgAddr;
+
   command error_t Init.init()
   {
     BootArgs bootArgs;
-    call IFlash.read((uint8_t*)TOSBOOT_ARGS_ADDR, &bootArgs, sizeof(bootArgs));
+    call IFlash.read(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
 
     // Update the local node ID
     if (bootArgs.address != 0xFFFF) {
@@ -68,11 +71,11 @@ implementation {
     BootArgs bootArgs;
 
     atomic {
-      call IFlash.read((uint8_t*)TOSBOOT_ARGS_ADDR, &bootArgs, sizeof(bootArgs));
+      call IFlash.read(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
 
       if (bootArgs.address != TOS_NODE_ID) {
        bootArgs.address = TOS_NODE_ID;
-       call IFlash.write((uint8_t*)TOSBOOT_ARGS_ADDR, &bootArgs, sizeof(bootArgs));
+       call IFlash.write(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
       }
       netprog_reboot();
     }
@@ -81,26 +84,34 @@ implementation {
   }
   
   command error_t NetProg.programImageAndReboot(uint32_t imgAddr)
+  {
+    reprogramImgAddr = imgAddr;
+    return call ReprogramGuard.okToProgram();
+  }
+
+  event void ReprogramGuard.okToProgramDone(bool ok)
   {
     BootArgs bootArgs;
 
+    if (!ok) {
+      // The voltage is too low. Nothing to do.
+      return;
+    }
+
     atomic {
-      call IFlash.read((uint8_t*)TOSBOOT_ARGS_ADDR, &bootArgs, sizeof(bootArgs));
+      call IFlash.read(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
       
-      bootArgs.imageAddr = imgAddr;
+      bootArgs.imageAddr = reprogramImgAddr;
       bootArgs.gestureCount = 0xff;
       bootArgs.noReprogram = FALSE;
+      bootArgs.address = TOS_NODE_ID;
 
-      call IFlash.write((uint8_t*)TOSBOOT_ARGS_ADDR, &bootArgs, sizeof(bootArgs));
+      call IFlash.write(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
 
       // reboot
       netprog_reboot();
     }
-
-    // couldn't reboot
-    return FAIL;
   }
 
   event void CC2420Config.syncDone(error_t error) {}
-
 }