delugemetadatam.nc

来自「tinyos最新版」· NC 代码 · 共 295 行

NC
295
字号
// $Id: DelugeMetadataM.nc,v 1.25 2004/11/04 13:51:21 jwhui Exp $/*									tab:4 * * * "Copyright (c) 2000-2004 The Regents of the University  of California.   * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. *  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * *//** * Manages Deluge metadata. * * @author Jonathan Hui <jwhui@cs.berkeley.edu> */module DelugeMetadataM {  provides {    interface StdControl;    interface DelugeMetadata as Metadata;  }  uses {    interface BitVecUtils;    interface DelugeMetadataStableStore as StableStore;#ifdef PLATFORM_TELOS    interface SplitControl as RadioControl;#endif  }}implementation {  // metadata for img on flash  DelugeMetadata metadata;  // state of metadata component  uint8_t state;  uint8_t nextIncompleteImage;  pgnum_t nextIncompletePage;  enum {    S_NOT_READY,    S_IDLE,    S_LOADING,    S_FLUSHING,    S_FLUSHING_UPDATE,  };  result_t signalDone(result_t result) {    uint8_t tmpState = state;    state = S_IDLE;    switch(tmpState) {    case S_LOADING:      return signal Metadata.ready(result);    case S_FLUSHING:      return SUCCESS;    case S_FLUSHING_UPDATE:      return signal Metadata.applyPageDiffDone(result);    }    return FAIL;  }    command result_t StdControl.init() {    // init state    state = S_NOT_READY;    return SUCCESS;  }  command result_t StdControl.start() {#ifndef PLATFORM_TELOS    if (state == S_NOT_READY) {      state = S_LOADING;      return call StableStore.getMetadata(&metadata);    }#endif    return SUCCESS;  }#ifdef PLATFORM_TELOS  event result_t RadioControl.initDone() { return SUCCESS; }  event result_t RadioControl.startDone() {    if (state == S_NOT_READY) {      state = S_LOADING;      return call StableStore.getMetadata(&metadata);    }    return SUCCESS;  }  event result_t RadioControl.stopDone() { return SUCCESS; }#endif  command result_t StdControl.stop() {    return SUCCESS;  }  command bool Metadata.isReady() {    return ((state != S_NOT_READY) && (state != S_LOADING));  }  command imgvnum_t Metadata.getVNum(uint8_t imgNum) {    return metadata.imgSummary[imgNum].vNum;  }  command pgnum_t Metadata.getNumPgs(uint8_t imgNum) {     return metadata.imgSummary[imgNum].numPgs;  }  command pgnum_t Metadata.getNumPgsComplete(uint8_t imgNum) {     return metadata.imgSummary[imgNum].numPgsComplete;  }  command uint8_t Metadata.getNextIncompleteImage() {     return nextIncompleteImage;   }  command pgnum_t Metadata.getNextIncompletePage() {     return nextIncompletePage;   }  command bool Metadata.isValidTOSApp(uint8_t imgNum) {    if ((state == S_IDLE	&& imgNum < DELUGE_NUM_IMGS	&& metadata.imgSummary[imgNum].numPgsComplete != 0	&& (metadata.imgSummary[imgNum].numPgs ==	    metadata.imgSummary[imgNum].numPgsComplete))	|| imgNum == DELUGE_GOLDEN_IMAGE_NUM)      return TRUE;    return FALSE;  }  void setNextIncompletePage() {    uint8_t i;    // look for first incomplete image    for ( i = 0; i < DELUGE_NUM_IMGS; i++ ) {      if (metadata.imgSummary[i].numPgs != metadata.imgSummary[i].numPgsComplete) {	nextIncompleteImage = i;	nextIncompletePage = metadata.imgSummary[i].numPgsComplete;	return;      }    }    nextIncompleteImage = DELUGE_INVALID_IMGNUM;    nextIncompletePage = DELUGE_NO_PAGE;  }  task void writeMetadata() {    uint8_t* tmp = (uint8_t*)&metadata;    uint8_t  i;    for ( i = sizeof(metadata.crc), metadata.crc = 0; i < sizeof(DelugeMetadata); i++ )      metadata.crc = crcByte(metadata.crc, tmp[i]);    if (call StableStore.writeMetadata(&metadata) == FAIL)      post writeMetadata();  }  // every time you flush page this is called  command result_t Metadata.pgFlushed() {    if (state != S_IDLE || nextIncompleteImage == DELUGE_INVALID_IMGNUM)      return FAIL;        metadata.imgSummary[nextIncompleteImage].numPgsComplete++;    setNextIncompletePage();    if (nextIncompleteImage == DELUGE_INVALID_IMGNUM	|| (metadata.imgSummary[nextIncompleteImage].numPgsComplete % DELUGE_METADATA_FLUSH_INTERVAL) == 0) {      // entire image is complete, flush metadata      // flush metadata once in a while to limit rollback on a reboot            state = S_FLUSHING;      post writeMetadata();    }        return SUCCESS;      }  command bool Metadata.isImgSummariesEqual(DelugeImgSummary* summaries) {    uint8_t i;    for ( i = 0; i < DELUGE_NUM_IMGS; i++ ) {      if (summaries[i].vNum != metadata.imgSummary[i].vNum	  || summaries[i].numPgsComplete != metadata.imgSummary[i].numPgsComplete)	return FALSE;    }    return TRUE;  }  command result_t Metadata.getImgSummaries(DelugeImgSummary* summaries) {    memcpy(summaries, metadata.imgSummary, DELUGE_NUM_IMGS * sizeof(DelugeImgSummary));    return SUCCESS;  }  command result_t Metadata.setImgSummaries(DelugeImgSummary* summaries) {    uint8_t i;    bool changed = FALSE;    if (state != S_IDLE)      return FAIL;    for ( i = 0; i < DELUGE_NUM_IMGS; i++ ) {      if (summaries[i].vNum != DELUGE_INVALID_VNUM	  && (summaries[i].vNum - metadata.imgSummary[i].vNum) > 0	  && (((uint32_t)summaries[i].numPgs*(uint32_t)DELUGE_BYTES_PER_PAGE) 	      <= ((uint32_t)DELUGE_MAX_IMAGE_SIZE) * (uint32_t)1024)) {	// new summary to apply	metadata.imgSummary[i] = summaries[i];	metadata.imgSummary[i].numPgsComplete = 0;	changed = TRUE;      }    }        if (changed) {      state = S_FLUSHING_UPDATE;      setNextIncompletePage();      post writeMetadata();      return SUCCESS;    }        return FAIL;  }  command bool Metadata.canProvideData(DelugeImgSummary* summaries) {    uint8_t i;        for ( i = 0; i < DELUGE_NUM_IMGS; i++ ) {      if (summaries[i].vNum == metadata.imgSummary[i].vNum	  && summaries[i].numPgsComplete > metadata.imgSummary[i].numPgsComplete)	return TRUE;      else if (summaries[i].vNum < metadata.imgSummary[i].vNum	       || metadata.imgSummary[i].numPgsComplete < metadata.imgSummary[i].numPgs)	return FALSE;    }        return FALSE;  }  event result_t StableStore.getMetadataDone(result_t result) {    uint8_t* tmp = (uint8_t*)&metadata;    uint16_t crc = 0;    uint8_t  i;    for ( i = sizeof(metadata.crc); i < sizeof(DelugeMetadata); i++ )      crc = crcByte(crc, tmp[i]);    if (metadata.crc != crc) {      // metadata is somehow invalid, reset everything      for ( i = 0; i < DELUGE_NUM_IMGS; i++ ) {	metadata.imgSummary[i].vNum = DELUGE_INVALID_VNUM;	metadata.imgSummary[i].numPgs = metadata.imgSummary[i].numPgsComplete = 0;      }      for ( i = sizeof(metadata.crc), metadata.crc = 0; i < sizeof(DelugeMetadata); i++ )	metadata.crc = crcByte(metadata.crc, tmp[i]);    }    setNextIncompletePage();    return signalDone(result);  }  event result_t StableStore.writeMetadataDone(result_t result) {    return signalDone(result);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?