]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/net/Deluge/extra/NetProgM.nc
Merge TinyOS 2.1.1 into master.
[tinyos-2.x.git] / tos / lib / net / Deluge / extra / NetProgM.nc
index 998a5fdb925ad75569f9e1f1ce7a58a017b312cf..fc054cff8fc0f13e00d657a1ca6da95241129eff 100644 (file)
@@ -25,8 +25,8 @@
 
 /**
  * @author Jonathan Hui <jwhui@cs.berkeley.edu>
- * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
  * @author Razvan Musaloiu-E. <razvanm@cs.jhu.edu>
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
  */
 
 #include "AM.h"
@@ -37,91 +37,86 @@ module NetProgM {
     interface Init;
   }
   uses {
-    interface StorageMap[uint8_t img_num];
     interface InternalFlash as IFlash;
     interface Crc;
-    interface DelugeMetadata;
+    interface Leds;
+#if !defined(PLATFORM_TINYNODE)
+    interface CC2420Config;
+#endif
+    async command void setAmAddress(am_addr_t a);
+    interface ReprogramGuard;
   }
 }
 
 implementation {
 
-  uint16_t computeTosInfoCrc(NetProg_TOSInfo* tosInfo)
-  {
-    return call Crc.crc16(tosInfo, sizeof(NetProg_TOSInfo)-2);
-  }
-
-  void writeTOSinfo()
-  {
-    NetProg_TOSInfo tosInfo;
-    uint16_t crc;
-    call IFlash.read((uint8_t*)IFLASH_TOS_INFO_ADDR, &tosInfo, sizeof(tosInfo));
-    tosInfo.addr = TOS_NODE_ID;
-    tosInfo.groupId = TOS_AM_GROUP;
-    crc = computeTosInfoCrc(&tosInfo);
-    // don't write if data is already correct
-    if (tosInfo.crc == crc)
-      return;
-    tosInfo.crc = crc;
-    call IFlash.write((uint8_t*)IFLASH_TOS_INFO_ADDR, &tosInfo, sizeof(tosInfo));
-  }
+  uint32_t reprogramImgAddr;
 
   command error_t Init.init()
   {
+    BootArgs bootArgs;
+    call IFlash.read(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
 
-    NetProg_TOSInfo tosInfo;
-
-    call IFlash.read((uint8_t*)IFLASH_TOS_INFO_ADDR, &tosInfo, sizeof(tosInfo));
-
-    if (tosInfo.crc == computeTosInfoCrc(&tosInfo)) {
-      // TOS_AM_GROUP is not a variable in T2
-      //      TOS_AM_GROUP = tosInfo.groupId;
-      atomic TOS_NODE_ID = tosInfo.addr;
-    }
-    else {
-      writeTOSinfo();
+    // Update the local node ID
+    if (bootArgs.address != 0xFFFF) {
+      TOS_NODE_ID = bootArgs.address;
+      call setAmAddress(bootArgs.address);
     }
+#if !defined(PLATFORM_TINYNODE)
+    call CC2420Config.setShortAddr(bootArgs.address);
+    call CC2420Config.sync();
+#endif    
     return SUCCESS;
   }
   
   command error_t NetProg.reboot()
   {
+    BootArgs bootArgs;
+
     atomic {
-      writeTOSinfo();
+      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(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
+      }
       netprog_reboot();
     }
+
     return FAIL;
   }
   
-  command error_t NetProg.programImgAndReboot(uint8_t img_num)
+  command error_t NetProg.programImageAndReboot(uint32_t imgAddr)
   {
-    tosboot_args_t args;
-    DelugeNodeDesc nodeDesc;
-    DelugeImgDesc *imgDesc;
-    
+    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 {
-      writeTOSinfo();
-      
-      args.imageAddr = call StorageMap.getPhysicalAddress[img_num](0);
-      args.gestureCount = 0xff;
-      args.noReprogram = FALSE;
-      call IFlash.write((uint8_t*)TOSBOOT_ARGS_ADDR, &args, sizeof(args));
-      
-      // Write info about what img to disseminate after reboot
-      imgDesc = call DelugeMetadata.getImgDesc(img_num);
-      nodeDesc.uid = imgDesc->uid;
-      nodeDesc.imgNum = img_num;
-      call IFlash.write((uint8_t*)IFLASH_NODE_DESC_ADDR, &nodeDesc, sizeof(nodeDesc));
+      call IFlash.read(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
       
+      bootArgs.imageAddr = reprogramImgAddr;
+      bootArgs.gestureCount = 0xff;
+      bootArgs.noReprogram = FALSE;
+      bootArgs.address = TOS_NODE_ID;
+
+      call IFlash.write(TCAST(uint8_t* COUNT(sizeof(bootArgs)),TOSBOOT_ARGS_ADDR), &bootArgs, sizeof(bootArgs));
+
       // reboot
       netprog_reboot();
     }
-
-    // couldn't reboot
-    return FAIL;
   }
 
-  default command storage_addr_t StorageMap.getPhysicalAddress[uint8_t img_num](storage_addr_t addr) { return 0xFFFFFFFF; }
-
+#if !defined(PLATFORM_TINYNODE)
+  event void CC2420Config.syncDone(error_t error) {}
+#endif
 }