]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/at45db/LogStorageP.nc
update to new debug if
[tinyos-2.x.git] / tos / chips / at45db / LogStorageP.nc
index 4349e1eb870a5e1a900d394346be7cde2ba8f358..f228e3ffd4537c8b41be91c9f7e700b2d741a403 100644 (file)
@@ -1,4 +1,4 @@
-/*                                                                     tab:4
+/*
  * "Copyright (c) 2000-2004 The Regents of the University  of California.  
  * All rights reserved.
  *
@@ -38,7 +38,7 @@
  * @author: Jonathan Hui <jwhui@cs.berkeley.edu>
  */
 
-module LogStorageP {
+module LogStorageP @safe() {
   provides {
     interface LogRead[uint8_t logId];
     interface LogWrite[uint8_t logId];
@@ -200,13 +200,13 @@ implementation
   uint8_t metaState;
   bool recordsLost;
   at45page_t firstPage, lastPage;
-  storage_len_t len;
+  storage_len_t pos;
   nx_struct pageinfo metadata;
 
   struct {
     /* The latest request made for this client, and it's arguments */
     uint8_t request; 
-    uint8_t *buf;
+    uint8_t *COUNT_NOK(len) buf;
     storage_len_t len;
 
     /* Log r/w positions */
@@ -267,6 +267,9 @@ implementation
   void sync() {
     metadata.flags = F_SYNC | F_LASTVALID;
     metadata.lastRecordOffset = s[client].woffset;
+    /* rend is now no longer the end of the page */
+    if (s[client].rpage == s[client].wpage)
+      s[client].rend = s[client].woffset;
     wmetadataStart();
   }
 
@@ -303,8 +306,8 @@ implementation
   void endRequest(error_t ok) {
     uint8_t c = client;
     uint8_t request = s[c].request;
-    storage_len_t actualLen = s[c].len - len;
-    void *ptr = s[c].buf - actualLen;
+    storage_len_t actualLen = pos;
+    void *ptr = s[c].buf;
     
     client = NO_CLIENT;
     s[c].request = R_IDLE;
@@ -323,7 +326,7 @@ implementation
 
   /* Enqueue request and request the underlying flash */
   error_t newRequest(uint8_t newRequest, uint8_t id,
-                    uint8_t *buf, storage_len_t length) {
+                    uint8_t *COUNT_NOK(length) buf, storage_len_t length) {
     s[id >> 1].circular = id & 1;
     id >>= 1;
 
@@ -331,8 +334,9 @@ implementation
       return EBUSY;
 
     s[id].request = newRequest;
-    s[id].buf = buf;
+    s[id].buf = NULL;
     s[id].len = length;
+    s[id].buf = buf;
     call Resource.request[id]();
 
     return SUCCESS;
@@ -340,12 +344,12 @@ implementation
 
   event void Resource.granted[uint8_t id]() {
     client = id;
-    len = s[client].len;
+    pos = 0;
     startRequest();
   }
 
   command error_t LogWrite.append[uint8_t id](void* buf, storage_len_t length) {
-    if (len > call LogRead.getSize[id]() - PAGE_SIZE)
+    if (length > call LogRead.getSize[id]() - PAGE_SIZE)
       /* Writes greater than the volume size are invalid.
         Writes equal to the volume size could break the log volume
         invariant (see next comment).
@@ -379,7 +383,7 @@ implementation
   }
 
   command error_t LogRead.seek[uint8_t id](storage_cookie_t offset) {
-    return newRequest(R_SEEK, id, (void *)(offset >> 16), offset);
+    return newRequest(R_SEEK, id, TCAST(void *COUNT(offset), ((uint16_t)(offset >> 16))), offset);
   }
 
   command storage_len_t LogRead.getSize[uint8_t id]() {
@@ -568,8 +572,9 @@ implementation
   /* ------------------------------------------------------------------ */
 
   void appendContinue() {
-    uint8_t *buf = s[client].buf;
+    uint8_t *buf = s[client].buf + pos;
     at45pageoffset_t offset = s[client].woffset, count;
+    storage_len_t len = s[client].len - pos;
     
     if (len == 0)
       {
@@ -589,10 +594,9 @@ implementation
     else
       count = PAGE_SIZE - offset;
 
-    s[client].buf += count;
     s[client].wpos += count;
     s[client].woffset += count;
-    len -= count;
+    pos += count;
 
     /* We normally lose data at the point we make the first write to a
        page in a log that has circled. */
@@ -621,6 +625,7 @@ implementation
   }
 
   void appendStart() {
+    storage_len_t len = s[client].len - pos;
     storage_len_t vlen = (storage_len_t)npages() * PAGE_SIZE;
 
     recordsLost = FALSE;
@@ -717,9 +722,10 @@ implementation
   /* ------------------------------------------------------------------ */
 
   void readContinue() {
-    uint8_t *buf = s[client].buf;
+    uint8_t *buf = s[client].buf + pos;
     at45pageoffset_t offset = s[client].roffset, count;
     at45pageoffset_t end = s[client].rend;
+    storage_len_t len = s[client].len - pos;
     
     if (len == 0)
       {
@@ -761,8 +767,7 @@ implementation
     else
       count = end - offset;
 
-    s[client].buf += count;
-    len -= count;
+    pos += count;
     s[client].rpos += count;
     s[client].roffset = offset + count;