]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/net/Deluge/ObjectTransferP.nc
Workaround the erase problems with AT45DB
[tinyos-2.x.git] / tos / lib / net / Deluge / ObjectTransferP.nc
index fec6cd29e16fd2c0c668398ef5810bdc55c0eb4a..d3ef9f3a7ae2441507e85ae316d115e956d61552 100644 (file)
@@ -55,6 +55,7 @@ implementation
   // States
   enum {
     S_ERASE,
+    S_SYNC,
     S_INITIALIZING_PUB,
     S_INITIALIZING_RECV,
     S_STARTED,
@@ -62,7 +63,6 @@ implementation
   };
   
   DelugeAdvTimer advTimers;
-  //DelugeNodeDesc nodeDesc;
   uint8_t state = S_STOPPED;
   
   object_id_t cont_receive_new_objid;
@@ -110,15 +110,12 @@ implementation
       resetTimer();
     } else {
       call DelugePageTransfer.setWorkingPage(DELUGE_INVALID_OBJID, DELUGE_INVALID_PGNUM);
-      post signalObjRecvDone();
+      call ObjectTransfer.stop();
+      state = S_SYNC;
+      call BlockWrite.sync[cont_receive_img_num]();
     }
   }
   
-//  bool isNodeDescValid(DelugeNodeDesc* tmpNodeDesc)
-//  {
-//    return (tmpNodeDesc->crc == crc16(tmpNodeDesc, 4 + sizeof(object_id_t) + 1));
-//  }
-  
   bool isObjDescValid(DelugeObjDesc* tmpObjDesc)
   {
     return (tmpObjDesc->crc == call Crc.crc16(tmpObjDesc, sizeof(object_id_t) + sizeof(page_num_t))
@@ -127,30 +124,19 @@ implementation
   
   void sendAdvMsg(uint16_t addr)
   {
-    DelugeAdvMsg *pMsg = (DelugeAdvMsg *)(call SendAdvMsg.getPayload(&pMsgBuf));
-    
+    DelugeAdvMsg *pMsg = (DelugeAdvMsg *)(call SendAdvMsg.getPayload(&pMsgBuf, sizeof(DelugeAdvMsg)));
+    if (pMsg == NULL) {
+      return;
+    }
     if (isBusy_pMsgBuf == FALSE) {
       pMsg->sourceAddr = TOS_NODE_ID;
       pMsg->version = DELUGE_VERSION;
-      //pMsg->type = (imagesLoaded) ? DELUGE_ADV_NORMAL : DELUGE_ADV_ERROR;
       pMsg->type = DELUGE_ADV_NORMAL;
-      
-      // make sure node desc is valid
-//      if (!isNodeDescValid(&nodeDesc)) {
-//        memset(&nodeDesc, 0xff, sizeof(nodeDesc));
-//      }
-//      memcpy(&pMsg->nodeDesc, &nodeDesc, sizeof(DelugeNodeDesc));
-      
-      // make sure obj desc is valid
-//      if (!isObjDescValid(&curObjDesc)) {
-//        //curObjDesc.objid = objid;
-//      }
+     
       memcpy(&(pMsg->objDesc), &curObjDesc, sizeof(DelugeObjDesc));
       
       if (call SendAdvMsg.send(addr, &pMsgBuf, sizeof(DelugeAdvMsg)) == SUCCESS) {
-        //dbg(DBG_USR1, "DELUGE: Sent ADV_MSG(imgNum=%d)\n", imgDesc->imgNum);
 //call StatsCollector.msg_bcastReq();
-call Leds.led0Toggle();
         isBusy_pMsgBuf = TRUE;
       }
     }
@@ -166,7 +152,7 @@ call Leds.led0Toggle();
     
     state = S_INITIALIZING_PUB;
     curObjDesc.objid = new_objid;
-    curObjDesc.numPgs = ((new_size - 1) / DELUGE_BYTES_PER_PAGE) + 1;   // Number of pages to transmit
+    curObjDesc.numPgs = ((new_size - 1) / DELUGET2_BYTES_PER_PAGE) + 1;   // Number of pages to transmit
     curObjDesc.numPgsComplete = curObjDesc.numPgs;   // Publisher doesn't really care about this
     curObjDesc.crc = call Crc.crc16(&curObjDesc, sizeof(object_id_t) + sizeof(page_num_t));
     
@@ -187,7 +173,7 @@ call Leds.led0Toggle();
   void cont_receive() {
     state = S_INITIALIZING_RECV;
     curObjDesc.objid = cont_receive_new_objid;
-    curObjDesc.numPgs = ((cont_receive_new_size - 1) / DELUGE_BYTES_PER_PAGE) + 1;   // Number of pages to receive
+    curObjDesc.numPgs = ((cont_receive_new_size - 1) / DELUGET2_BYTES_PER_PAGE) + 1;   // Number of pages to receive
     curObjDesc.numPgsComplete = 0;
     curObjDesc.crc = call Crc.crc16(&curObjDesc, sizeof(object_id_t) + sizeof(page_num_t));
     
@@ -208,14 +194,13 @@ call Leds.led0Toggle();
     error_t error;
     
     call ObjectTransfer.stop();
-    state = S_STOPPED;
 //call StatsCollector.startStatsCollector();
     
     cont_receive_new_objid = new_objid;
     cont_receive_new_size = new_size;
     cont_receive_img_num = img_num;
     
-    error = call BlockWrite.erase[img_num]();
+    error = call BlockWrite.erase[cont_receive_img_num]();
     if (error == SUCCESS) {
       state = S_ERASE;
     }
@@ -227,18 +212,20 @@ call Leds.led0Toggle();
   {
     call Timer.stop();
     call DelugePageTransfer.stop();
+    state = S_STOPPED;
 //call StatsCollector.stopStatsCollector();
     
-    state = S_STOPPED;
     curObjDesc.objid = DELUGE_INVALID_OBJID;
     curObjDesc.numPgs = DELUGE_INVALID_PGNUM;
     curObjDesc.numPgsComplete = DELUGE_INVALID_PGNUM;
+    advTimers.periodLog2 = 0;
     
     return SUCCESS;
   }
   
   event void DelugePageTransfer.receivedPage(object_id_t new_objid, page_num_t new_pgNum)
   {
+//    printf("R: %08lx %d\n", new_objid, new_pgNum);
     if (new_objid == curObjDesc.objid && new_pgNum == curObjDesc.numPgsComplete) {
       curObjDesc.numPgsComplete++;
       curObjDesc.crc = call Crc.crc16(&curObjDesc, sizeof(object_id_t) + sizeof(page_num_t));
@@ -246,11 +233,21 @@ call Leds.led0Toggle();
         setNextPage();
       } else {
         call DelugePageTransfer.setWorkingPage(curObjDesc.objid, curObjDesc.numPgsComplete);
-        post signalObjRecvDone();
+        state = S_SYNC;
+        if (call BlockWrite.sync[cont_receive_img_num]() != SUCCESS) {
+          post signalObjRecvDone();
+        }
       }
     }
   }
   
+  event void BlockWrite.syncDone[uint8_t img_num](error_t error)
+  {
+    if (state == S_SYNC) {
+      post signalObjRecvDone();
+    }
+  }
+  
   event void DelugePageTransfer.suppressMsgs(object_id_t new_objid)
   {
     if (new_objid == curObjDesc.objid) {
@@ -273,9 +270,7 @@ call Leds.led0Toggle();
       return msg;
     }
     
-    if (rxAdvMsg->version != DELUGE_VERSION 
-        //|| !isNodeDescValid(&rxAdvMsg->nodeDesc)
-        || state != S_STARTED) {
+    if (rxAdvMsg->version != DELUGE_VERSION || state != S_STARTED) {
       return msg;
     }
     
@@ -305,7 +300,7 @@ call Leds.led0Toggle();
   }
     
   event void Timer.fired()
-  {
+  {    
     updateTimers();
     
     if (advTimers.overheard == 0) {
@@ -326,6 +321,8 @@ call Leds.led0Toggle();
   }
   
   default command error_t BlockWrite.erase[uint8_t img_num]() { return FAIL; }
+  default command error_t BlockWrite.sync[uint8_t img_num]() { return FAIL; }
+  
   event void BlockWrite.writeDone[uint8_t img_num](storage_addr_t addr, void* buf, storage_len_t len, error_t error) {}
   event void BlockWrite.eraseDone[uint8_t img_num](error_t error)
   {
@@ -333,5 +330,4 @@ call Leds.led0Toggle();
       cont_receive();
     }
   }
-  event void BlockWrite.syncDone[uint8_t img_num](error_t error) {}
 }