Esempio n. 1
0
void RTCMemStore::updateData() {
  uint32_t cnew = calculateCRC32(((uint8_t*) rtcData) + 4, RTCData::rtcDataSize() - 4);
  if (cnew != rtcData->crc32) {
    rtcData->crc32 = cnew;
    ESP.rtcUserMemoryWrite(0, (uint32_t*) rtcData, RTCData::rtcDataSize());
  }
  delete rtcData;
}
Esempio n. 2
0
bool RTCMemStore::readData() {
  rtcData = new RTCData();
  //RTCData2 r2;
  //Serial << "sizeof rtcdat: " <<RTCData::rtcDataSize() << endl;
  if (ESP.rtcUserMemoryRead(0, (uint32_t*) rtcData, RTCData::rtcDataSize())) {
    uint32_t crcOfData = calculateCRC32(((uint8_t*) rtcData) + 4, RTCData::rtcDataSize() - 4);
    if (crcOfData != rtcData->crc32) {
      //Serial.println(F("CRC32 in RTC memory doesn't match CRC32 of data. Data is probably invalid!"));
      delete rtcData;
      rtcData = new RTCData();
      return false;
    } else {
      //Serial.println(F("CRC32 check ok, data is probably valid."));
      return true;
    }
  }
  return false;
}
Esempio n. 3
0
/* Construct an flb by reading it from a file
 */
DataStore::FreelistBucket::FreelistBucket(File::FilePtr& f, off_t offset)
{
    /* Format of bucket:
       <size><key><nelements><offset 1>...<offset n><crc>
    */

    size_t bufsize;
    size_t bucketsize;

    f->readAll(&bucketsize, sizeof(size_t), offset);
    bufsize = bucketsize + sizeof(size_t);
    offset += sizeof(size_t);

    _buf.reset(new char[bufsize]);

    f->readAll(_buf.get() + sizeof(size_t), bucketsize, offset);

    char* pos = _buf.get();

    _size = (size_t*) pos;
    *_size = bucketsize;
    pos += sizeof(size_t);
    _key = (size_t*) pos;
    pos += sizeof(size_t);
    _nelements = (size_t*) pos;
    pos += sizeof(size_t);
    _offsets = (off_t*) pos;
    pos += (*_nelements * sizeof(off_t));
    _crc = (uint32_t*) pos;

    if (*_crc != calculateCRC32(_key, *_size - sizeof(uint32_t)))
    {
        throw SYSTEM_EXCEPTION(SCIDB_SE_STORAGE, SCIDB_LE_DATASTORE_CORRUPT_FREELIST)
            << f->getPath();
    }
}
dsStatus ds_read_memory (unsigned char memory_type,
                         DALLAS_CONTEXT *dc,
                         unsigned int address,
                         unsigned char *data,
                         size_t bufsiz)
{
  const int repeatCount = 10;    // Should perhaps be user selectable
  bufdata_t result[repeatCount];
  int found;
  unsigned int maxcount, crc, i, j, maxidx;
  dsStatus stat;

  memset(result, 0, sizeof(result));

  for (i = 0; i < repeatCount; i++) {
    // Read memory and calculate checksum
    if (memory_type == DS2431_CODE) {
      ds2431_read_memory(dc, address, data, bufsiz);
    } else {
      ds2430_read_memory(dc, address, data, bufsiz);
    }
    crc = calculateCRC32(data, bufsiz);

    // Find same checksum in table, or first unused entry
    found = 0;
    for (j = 0; j < repeatCount; j++) {
      if (result[j].count == 0) {
        break;
      } else if (result[j].crc == crc) {
        found = 1;
        result[j].count++;
        break;
      }
    }

    if (!found) {
      // First time this crc was found; store.
      result[j].crc   = crc;
      result[j].count = 1;
    }
    // If we are repeating, sleep for a millisecond or two.
    if (repeatCount > 1) {
      os_if_set_task_uninterruptible ();
      os_if_wait_for_event_timeout_simple(2);
    }
  }

  maxidx = 0;
  maxcount = 0;
  for (i = 0; i < repeatCount; i++) {
    if (result[i].count > maxcount) {
      maxcount = result[i].count;
      // DbgPrint("%s dallas[%d].count=%d\n", hwif.hw_name, i, maxcount);
      maxidx = i;
    }
  }

  // Try to get the most common data again
  for (i = 0; i < repeatCount; i++) {
    // Read memory and calculate checksum
    if (memory_type == DS2431_CODE) {
      ds2431_read_memory(dc, address, data, bufsiz);
    } else {
      ds2430_read_memory(dc, address, data, bufsiz);
    }
    crc = calculateCRC32(data, bufsiz);
    if (crc == result[maxidx].crc) {
      break;
    }
  }

  if (i == repeatCount) {
    // Unable to find selected result again...
    stat = dsError_NotProgrammed;   // For lack of anything better
  } else if (repeatCount > 1) {
    if (maxcount < (repeatCount / 2)) {
      // Majority readings are < 50%, so fail
      stat = dsError_NotProgrammed; // Not entirely correct code, but...
    } else {
      stat = dsError_None;
    }
  } else {
    // We read the contents just once so we don't know if we failed or not.
    stat = dsError_None;
  }


  return stat;
}