arduino-libraries / WiFiNINA

136 stars 105 forks source link

`WiFiStorage`: writing/reading small files is not working #239

Open vshymanskyy opened 1 year ago

vshymanskyy commented 1 year ago

When working on the multi-platform Preferences library, I noticed that storing small chunks of data on the NINA module produces incorrect results.

Example Code

I'm using Arduino Nano 33 IoT. WiFiNINA v1.8.13. Firmware v1.4.8. This happens with small values, i.e. with the length of 1, 2, or 3 bytes.

#include "WiFiNINA.h"

void setup() {
  Serial.begin(115200);
  delay(2000);

  const char* fn = "/fs/test.bin";

  WiFiStorage.remove(fn);

  uint8_t data[3] = { 'A', 'B', 'C' };
  WiFiStorage.write(fn, 0, data, sizeof(data));

  uint32_t fsize = 0;
  if (WiFiStorage.exists(fn, &fsize)) {
    Serial.print("File exists. Size: "); Serial.println(fsize);
  }

  uint8_t result[16];
  WiFiStorage.read(fn, 0, result, fsize);

  for (int i=0; i<fsize; i++) {
    char out[4] = { 0, };
    uint8_t c = result[i];
    sprintf(out, "%02X ", c, c);
    Serial.print(out);
  }
  Serial.println();

  // Cleanup
  WiFiStorage.remove(fn);
}

void loop() {
  delay(100);
}

Actual Output

File exists. Size: 3
41 00 00 

Expected Output

File exists. Size: 3
41 42 43