[Tinyos-beta-commits]
CVS: tinyos-1.x/beta/STM25P/STM25P LogRead.nc, 1.2,
1.3 LogStorage.h, 1.2, 1.3 LogStorageM.nc, 1.2,
1.3 LogWrite.nc, 1.2, 1.3
Jonathan Hui
jwhui at users.sourceforge.net
Fri May 20 14:23:43 PDT 2005
Update of /cvsroot/tinyos/tinyos-1.x/beta/STM25P/STM25P
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1766
Modified Files:
LogRead.nc LogStorage.h LogStorageM.nc LogWrite.nc
Log Message:
Nearly working logging implementation for STM25P. erase, append,
currentOffset, and read all seem to work. seek has not been tested.
Index: LogRead.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/STM25P/STM25P/LogRead.nc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** LogRead.nc 15 Mar 2005 06:23:21 -0000 1.2
--- LogRead.nc 20 May 2005 21:23:41 -0000 1.3
***************
*** 30,35 ****
interface LogRead {
! command result_t read(uint8_t* data, log_len_t numBytes);
! event void readDone(storage_result_t result, uint8_t* data, log_len_t numBytes);
command result_t seek(log_cookie_t cookie);
--- 30,35 ----
interface LogRead {
! command result_t read(void* data, log_len_t numBytes);
! event void readDone(storage_result_t result, void* data, log_len_t numBytes);
command result_t seek(log_cookie_t cookie);
Index: LogStorage.h
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/STM25P/STM25P/LogStorage.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** LogStorage.h 15 Mar 2005 06:23:21 -0000 1.2
--- LogStorage.h 20 May 2005 21:23:41 -0000 1.3
***************
*** 52,55 ****
--- 52,56 ----
LOG_BLOCK_MAX_LENGTH = 1 << 8,
LOG_BLOCK_LENGTH_MASK = (1 << 12) - 1,
+ LOG_BLOCK_FLAGS_MASK = 0xf,
LOG_MAX_COOKIE = 0xffffffff,
};
Index: LogStorageM.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/STM25P/STM25P/LogStorageM.nc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** LogStorageM.nc 15 Mar 2005 06:23:21 -0000 1.2
--- LogStorageM.nc 20 May 2005 21:23:41 -0000 1.3
***************
*** 59,63 ****
log_len_t rwLen, curLen, lastLen;
! uint8_t* rwData;
uint8_t state;
--- 59,63 ----
log_len_t rwLen, curLen, lastLen;
! void* rwData;
uint8_t state;
***************
*** 66,70 ****
log_cookie_t curReadCookie, curWriteCookie;
! log_block_addr_t curWriteBlockPos, curReadBlockLen;
void signalDone(storage_result_t result) {
--- 66,70 ----
log_cookie_t curReadCookie, curWriteCookie;
! log_block_addr_t curReadBlockLen, curWriteBlockPos;
void signalDone(storage_result_t result) {
***************
*** 96,99 ****
--- 96,140 ----
}
+ result_t advanceCookie(log_cookie_t *curCookie) {
+
+ log_cookie_t cookie = *curCookie;
+ bool advancingWriteCookie = cookie == curWriteCookie;
+ uint8_t newSector;
+
+ while ( advancingWriteCookie || cookie < curWriteCookie ) {
+
+ // if at beginning of sector, advance read cookie
+ if (!(cookie % STM25P_SECTOR_SIZE))
+ cookie += sizeof(LogSectorHeader);
+
+ // read block header
+ if (call SectorStorage.read[client](cookie, &blockHeader, sizeof(blockHeader))
+ == FAIL)
+ return FAIL;
+
+ // take block if:
+ // 1) not allocated
+ // 2) block is valid
+ // 3) block current being written
+ if ( !(~blockHeader.flags & LOG_BLOCK_ALLOCATED)
+ || (~blockHeader.flags & LOG_BLOCK_VALID)
+ || (!advancingWriteCookie && cookie >= curWriteCookie - curWriteBlockPos) ) {
+ break;
+ }
+
+ // advance to next log block
+ newSector = (cookie >> STM25P_SECTOR_SIZE_LOG2) + 1;
+ cookie += LOG_BLOCK_MAX_LENGTH;
+ if (newSector == cookie >> STM25P_SECTOR_SIZE_LOG2)
+ cookie = (stm25p_addr_t)newSector << STM25P_SECTOR_SIZE_LOG2;
+
+ }
+
+ *curCookie = cookie;
+
+ return SUCCESS;
+
+ }
+
command result_t Mount.mount[logstorage_t logId](volume_id_t id) {
if (admitRequest(logId) == FAIL)
***************
*** 106,126 ****
stm25p_addr_t volumeSize = call StorageManager.getVolumeSize[client]();
! uint8_t numSectors = volumeSize / STORAGE_BLOCK_SIZE;
! uint8_t i;
volumeId = id;
- curWriteCookie = curReadCookie = 0;
curWriteBlockPos = curReadBlockLen = 0;
// find sector with smallest/largest sector header cookie
! for ( i = 0, curReadCookie = LOG_MAX_COOKIE, curWriteCookie = 0; i < numSectors; i++ ) {
! if (call SectorStorage.read[client](i*STORAGE_BLOCK_SIZE, (uint8_t*)§orHeader,
! sizeof(sectorHeader)) == FAIL) {
signalDone(STORAGE_FAIL);
return;
}
if (!~sectorHeader.cookie)
continue;
if (sectorHeader.cookie < curReadCookie)
curReadCookie = sectorHeader.cookie;
--- 147,172 ----
stm25p_addr_t volumeSize = call StorageManager.getVolumeSize[client]();
! stm25p_addr_t curAddr;
volumeId = id;
curWriteBlockPos = curReadBlockLen = 0;
// find sector with smallest/largest sector header cookie
! curWriteCookie = 0;
! curReadCookie = LOG_MAX_COOKIE;
! for ( curAddr = 0; curAddr < volumeSize; curAddr += STM25P_SECTOR_SIZE ) {
!
! if (call SectorStorage.read[client](curAddr, §orHeader, sizeof(sectorHeader))
! == FAIL) {
signalDone(STORAGE_FAIL);
return;
}
+
+ // skip if sector header is all ones
if (!~sectorHeader.cookie)
continue;
+
+ // remember smallest/largest sector header cookie
if (sectorHeader.cookie < curReadCookie)
curReadCookie = sectorHeader.cookie;
***************
*** 129,192 ****
}
- call Leds.yellowOn();
-
- curWriteCookie += sizeof(LogSectorHeader);
-
// advance curWriteCookie to last log block
blockHeader.length = 0;
do {
curWriteCookie += blockHeader.length;
! if (call SectorStorage.read[client](curWriteCookie, (uint8_t*)&blockHeader,
! sizeof(blockHeader)) == FAIL) {
! signalDone(STORAGE_FAIL);
return;
}
! } while ( ((~blockHeader.flags) & LOG_BLOCK_ALLOCATED)
! && (~blockHeader.length)
! && curWriteCookie < volumeSize );
- call Leds.greenOn();
-
signalDone(STORAGE_OK);
!
}
! command result_t LogRead.read[logstorage_t logId](uint8_t* data, log_len_t numBytes) {
log_len_t lastBytes;
! if ( admitRequest(logId) == FAIL
! || curReadCookie >= curWriteCookie )
return FAIL;
!
while ( numBytes > 0 ) {
! // read sector header
! if ( !(curReadCookie % STORAGE_BLOCK_SIZE) )
! curReadCookie += sizeof(LogSectorHeader);
!
! // read block header
if ( curReadBlockLen == 0 ) {
! blockHeader.length = 0;
! do {
! curReadCookie += blockHeader.length;
! if (call SectorStorage.read[logId](curReadCookie, (uint8_t*)&blockHeader,
! sizeof(blockHeader)) == FAIL)
! return FAIL;
! } while ( (~blockHeader.flags) & LOG_BLOCK_ALLOCATED
! && curReadCookie + blockHeader.length < curWriteCookie
! && !( (~blockHeader.flags) & LOG_BLOCK_VALID ) );
! curReadBlockLen = blockHeader.length - sizeof(LogBlockHeader);
! curReadCookie += sizeof(LogBlockHeader);
}
! // check for end of log block
lastBytes = numBytes;
if ( curReadBlockLen < lastBytes )
lastBytes = curReadBlockLen;
if (call SectorStorage.read[logId](curReadCookie, data, lastBytes) == FAIL)
return FAIL;
!
curReadCookie += lastBytes;
data += lastBytes;
--- 175,235 ----
}
// advance curWriteCookie to last log block
blockHeader.length = 0;
do {
curWriteCookie += blockHeader.length;
! if (advanceCookie(&curWriteCookie) == FAIL) {
! signalDone(STORAGE_FAIL);
return;
}
! } while ( ~blockHeader.flags & LOG_BLOCK_ALLOCATED );
signalDone(STORAGE_OK);
!
}
! command result_t LogRead.read[logstorage_t logId](void* data, log_len_t numBytes) {
log_len_t lastBytes;
! if ( admitRequest(logId) == FAIL )
return FAIL;
!
while ( numBytes > 0 ) {
! // if at beginning of block, read block header
if ( curReadBlockLen == 0 ) {
!
! if (advanceCookie(&curReadCookie) == FAIL)
! return FAIL;
!
! // if block header is valid
! if ( ~blockHeader.flags & LOG_BLOCK_VALID ) {
! curReadBlockLen = blockHeader.length - sizeof(LogBlockHeader);
! curReadCookie += sizeof(LogBlockHeader);
! }
! // if block header is for block that is currently being written
! else if ( curReadCookie >= curWriteCookie - curWriteBlockPos ) {
! curReadBlockLen = curWriteBlockPos - sizeof(LogBlockHeader);
! curReadCookie += sizeof(LogBlockHeader);
! }
!
}
! // make sure we're not reading off the end of the log
! if (curReadCookie + numBytes > curWriteCookie)
! return FAIL;
!
lastBytes = numBytes;
+
+ // check for end of log block
if ( curReadBlockLen < lastBytes )
lastBytes = curReadBlockLen;
+ // read data
if (call SectorStorage.read[logId](curReadCookie, data, lastBytes) == FAIL)
return FAIL;
!
! // advance pointers
curReadCookie += lastBytes;
data += lastBytes;
***************
*** 209,252 ****
log_cookie_t newReadCookie;
log_len_t newReadBlockLen;
! uint8_t sector;
if (admitRequest(logId) == FAIL)
return FAIL;
! sector = cookie / STORAGE_BLOCK_SIZE;
!
! // read sector header
! if (call SectorStorage.read[logId](sector, (uint8_t*)§orHeader,
! sizeof(sectorHeader)) == FAIL)
! return FAIL;
! if (!~sectorHeader.cookie)
return FAIL;
- newReadCookie = sectorHeader.cookie;
-
// scan through
- newReadBlockLen = 0;
while ( newReadCookie < cookie ) {
! // read block header
! blockHeader.length = 0;
! do {
! newReadCookie += blockHeader.length;
! if (call SectorStorage.read[logId](newReadCookie, (uint8_t*)&blockHeader,
! sizeof(blockHeader)) == FAIL)
! return FAIL;
! } while ( (~blockHeader.flags) & LOG_BLOCK_ALLOCATED
! && newReadCookie + blockHeader.length < cookie
! && !( (~blockHeader.flags) & LOG_BLOCK_VALID ) );
! newReadBlockLen = blockHeader.length - sizeof(LogBlockHeader);
! newReadCookie += sizeof(LogBlockHeader);
!
! // advance read cookie
if ( newReadCookie + newReadBlockLen > cookie ) {
newReadCookie = cookie;
- newReadBlockLen -= cookie - newReadCookie;
}
else {
! newReadCookie += newReadBlockLen;
}
--- 252,293 ----
log_cookie_t newReadCookie;
log_len_t newReadBlockLen;
! stm25p_addr_t volumeSize = call StorageManager.getVolumeSize[logId]();
! stm25p_addr_t sectorCookie = cookie & ~(STM25P_SECTOR_SIZE-1);
if (admitRequest(logId) == FAIL)
return FAIL;
! // look for the sector we want
! for ( newReadCookie = 0;
! newReadCookie < volumeSize && newReadCookie != sectorCookie;
! newReadCookie += STM25P_SECTOR_SIZE ) {
! if (call SectorStorage.read[logId](newReadCookie, §orHeader,
! sizeof(sectorHeader)) == FAIL)
! return FAIL;
! }
! // couldn't find the sector we want
! if ( newReadCookie >= volumeSize )
return FAIL;
// scan through
while ( newReadCookie < cookie ) {
! if (advanceCookie(&newReadCookie) == FAIL)
! return FAIL;
!
! // if block header is valid
! if ( ~blockHeader.flags & LOG_BLOCK_VALID )
! newReadBlockLen = blockHeader.length;
! // if block header is for block that is currently being written
! else if ( curReadCookie >= curWriteCookie - curWriteBlockPos )
! newReadBlockLen = curWriteBlockPos;
!
! // advance pointers
if ( newReadCookie + newReadBlockLen > cookie ) {
+ newReadBlockLen = blockHeader.length - newReadCookie;
newReadCookie = cookie;
}
else {
! newReadCookie += blockHeader.length;
}
***************
*** 255,260 ****
curReadCookie = newReadCookie;
curReadBlockLen = newReadBlockLen;
! return SUCCESS;
}
--- 296,306 ----
curReadCookie = newReadCookie;
curReadBlockLen = newReadBlockLen;
+
+ if (post signalDoneTask() == SUCCESS) {
+ state = S_SEEK;
+ return SUCCESS;
+ }
! return FAIL;
}
***************
*** 262,265 ****
--- 308,313 ----
command result_t LogWrite.erase[logstorage_t logId]() {
+ stm25p_addr_t len;
+
if (admitRequest(logId) == FAIL)
return FAIL;
***************
*** 267,273 ****
state = S_ERASE;
! curWriteCookie = call StorageManager.getVolumeSize[logId]() - STORAGE_BLOCK_SIZE;
!
! if (call SectorStorage.erase[logId](curWriteCookie) == FAIL) {
state = S_IDLE;
return FAIL;
--- 315,320 ----
state = S_ERASE;
! len = call StorageManager.getVolumeSize[logId]();
! if (call SectorStorage.erase[logId](curWriteCookie, len) == FAIL) {
state = S_IDLE;
return FAIL;
***************
*** 278,364 ****
}
- bool newEntryFits() {
-
- log_len_t remainingBytes = rwLen;
- log_len_t totalBytes;
- log_len_t tmp;
-
- // fill current log block
- totalBytes = LOG_BLOCK_MAX_LENGTH - curWriteBlockPos;
- remainingBytes -= totalBytes;
-
- // fill x log blocks
- tmp = (remainingBytes / LOG_BLOCK_MAX_LENGTH)*(sizeof(LogBlockHeader) + LOG_BLOCK_MAX_LENGTH);
- totalBytes += tmp;
- remainingBytes -= tmp;
-
- // complete final log block
- if (remainingBytes)
- totalBytes += remainingBytes + sizeof(LogBlockHeader);
-
- tmp = call StorageManager.getVolumeSize[client]() - curWriteCookie;
-
- // compensate for log sector header
- tmp -= (tmp / STORAGE_BLOCK_SIZE) * sizeof(LogSectorHeader);
-
- return (totalBytes < tmp);
-
- }
-
result_t appendData() {
log_len_t tmp;
- if ( (curLen == 0) && !newEntryFits() )
- return FAIL;
-
// commit log block header if at: (1) max block len or (2) end of sector
if ( curWriteBlockPos >= LOG_BLOCK_MAX_LENGTH
! || ( curWriteBlockPos && !(curWriteCookie % STORAGE_BLOCK_SIZE) ) ) {
call Leds.yellowToggle();
blockHeader.length = curWriteBlockPos;
blockHeader.flags = ~(LOG_BLOCK_VALID + LOG_BLOCK_ALLOCATED);
- lastLen = sizeof(blockHeader);
state = S_COMMIT_BLOCK_HEADER;
! return call SectorStorage.write[client](curWriteCookie-curWriteBlockPos,
! (uint8_t*)&blockHeader, lastLen);
}
// write cookie if at start of sector
! if ( !(curWriteCookie % STORAGE_BLOCK_SIZE) ) {
sectorHeader.cookie = curWriteCookie;
- lastLen = sizeof(sectorHeader);
state = S_WRITE_SECTOR_HEADER;
! return call SectorStorage.write[client](curWriteCookie, (uint8_t*)§orHeader, lastLen);
}
// begin log block header
! if ( !curWriteBlockPos ) {
blockHeader.length = LOG_BLOCK_LENGTH_MASK;
blockHeader.flags = ~(LOG_BLOCK_ALLOCATED);
- lastLen = sizeof(blockHeader);
state = S_INIT_BLOCK_HEADER;
! return call SectorStorage.write[client](curWriteCookie, (uint8_t*)&blockHeader, lastLen);
}
// write data
! // check for sector boundary
! lastLen = rwLen;
! tmp = STORAGE_BLOCK_SIZE - (curWriteCookie % STORAGE_BLOCK_SIZE);
! if ( tmp < lastLen )
! lastLen = tmp;
!
! // check for log block boundary
! tmp = LOG_BLOCK_MAX_LENGTH - curWriteBlockPos;
! if ( tmp < lastLen )
! lastLen = tmp;
!
! state = S_APPEND;
! return call SectorStorage.write[client](curWriteCookie, rwData + curLen, lastLen);
}
! command result_t LogWrite.append[logstorage_t logId](uint8_t* data, log_len_t numBytes) {
if (admitRequest(logId) == FAIL)
--- 325,390 ----
}
result_t appendData() {
+ stm25p_addr_t addr;
+ void* buf;
log_len_t tmp;
// commit log block header if at: (1) max block len or (2) end of sector
if ( curWriteBlockPos >= LOG_BLOCK_MAX_LENGTH
! || ( curWriteBlockPos && !(curWriteCookie % STM25P_SECTOR_SIZE) ) ) {
call Leds.yellowToggle();
blockHeader.length = curWriteBlockPos;
blockHeader.flags = ~(LOG_BLOCK_VALID + LOG_BLOCK_ALLOCATED);
state = S_COMMIT_BLOCK_HEADER;
! addr = curWriteCookie - curWriteBlockPos;
! buf = &blockHeader;
! lastLen = sizeof(blockHeader);
}
// write cookie if at start of sector
! else if ( !(curWriteCookie % STM25P_SECTOR_SIZE) ) {
sectorHeader.cookie = curWriteCookie;
state = S_WRITE_SECTOR_HEADER;
! addr = curWriteCookie;
! buf = §orHeader;
! lastLen = sizeof(sectorHeader);
}
// begin log block header
! else if ( !curWriteBlockPos ) {
blockHeader.length = LOG_BLOCK_LENGTH_MASK;
blockHeader.flags = ~(LOG_BLOCK_ALLOCATED);
state = S_INIT_BLOCK_HEADER;
! addr = curWriteCookie;
! buf = &blockHeader;
! lastLen = sizeof(blockHeader);
}
// write data
+ else {
+
+ // check for sector boundary
+ lastLen = rwLen;
+ tmp = STM25P_SECTOR_SIZE - (curWriteCookie % STM25P_SECTOR_SIZE);
+ if ( tmp < lastLen )
+ lastLen = tmp;
+
+ // check for log block boundary
+ tmp = LOG_BLOCK_MAX_LENGTH - curWriteBlockPos;
+ if ( tmp < lastLen )
+ lastLen = tmp;
+
+ state = S_APPEND;
+ addr = curWriteCookie;
+ buf = rwData + curLen;
! }
!
! return call SectorStorage.write[client](addr, buf, lastLen);
}
! command result_t LogWrite.append[logstorage_t logId](void* data, log_len_t numBytes) {
if (admitRequest(logId) == FAIL)
***************
*** 393,397 ****
if (call SectorStorage.write[client](curWriteCookie-curWriteBlockPos,
! (uint8_t*)&blockHeader, lastLen) == FAIL) {
state = S_IDLE;
return FAIL;
--- 419,423 ----
if (call SectorStorage.write[client](curWriteCookie-curWriteBlockPos,
! &blockHeader, lastLen) == FAIL) {
state = S_IDLE;
return FAIL;
***************
*** 408,427 ****
event void SectorStorage.eraseDone[logstorage_t logId](storage_result_t result) {
! if (result != STORAGE_OK) {
! signalDone(result);
! return;
! }
!
! // check for more sectors to erase
! if ( curWriteCookie > 0 ) {
! curWriteCookie -= STORAGE_BLOCK_SIZE;
! if (call SectorStorage.erase[logId](curWriteCookie) == FAIL)
! signalDone(STORAGE_FAIL);
! return;
! }
!
curWriteBlockPos = 0;
! signalDone(STORAGE_OK);
}
--- 434,441 ----
event void SectorStorage.eraseDone[logstorage_t logId](storage_result_t result) {
! curWriteCookie = 0;
curWriteBlockPos = 0;
! signalDone(result);
}
***************
*** 466,476 ****
}
! default event void LogRead.readDone[logstorage_t logId](storage_result_t result, uint8_t* data, log_len_t numBytes) { ; }
! default event void LogRead.seekDone[logstorage_t logId](storage_result_t result, log_len_t cookie) { ; }
! default event void LogWrite.eraseDone[logstorage_t logId](storage_result_t result) { ; }
! default event void LogWrite.appendDone[logstorage_t logId](storage_result_t result, uint8_t* data, log_len_t numBytes) { ; }
! default event void LogWrite.syncDone[logstorage_t logId](storage_result_t result) { ; }
! default event void Mount.mountDone[logstorage_t logId](storage_result_t result, volume_id_t id) { ; }
}
--- 480,490 ----
}
! default event void LogRead.readDone[logstorage_t logId](storage_result_t result, void* data, log_len_t numBytes) {}
! default event void LogRead.seekDone[logstorage_t logId](storage_result_t result, log_len_t cookie) {}
! default event void LogWrite.eraseDone[logstorage_t logId](storage_result_t result) {}
! default event void LogWrite.appendDone[logstorage_t logId](storage_result_t result, void* data, log_len_t numBytes) {}
! default event void LogWrite.syncDone[logstorage_t logId](storage_result_t result) {}
! default event void Mount.mountDone[logstorage_t logId](storage_result_t result, volume_id_t id) {}
}
Index: LogWrite.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/STM25P/STM25P/LogWrite.nc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** LogWrite.nc 15 Mar 2005 06:23:21 -0000 1.2
--- LogWrite.nc 20 May 2005 21:23:41 -0000 1.3
***************
*** 33,38 ****
event void eraseDone(storage_result_t result);
! command result_t append(uint8_t* data, log_len_t numBytes);
! event void appendDone(storage_result_t result, uint8_t* data, log_len_t numBytes);
command result_t sync();
--- 33,38 ----
event void eraseDone(storage_result_t result);
! command result_t append(void* data, log_len_t numBytes);
! event void appendDone(storage_result_t result, void* data, log_len_t numBytes);
command result_t sync();
More information about the Tinyos-beta-commits
mailing list