Skip to content

fix WiFiDrv::fileOperation function padding - #331

Open
cultivate-track wants to merge 2 commits into
arduino-libraries:masterfrom
cultivate-track:fileOperation-padding-fix
Open

fix WiFiDrv::fileOperation function padding#331
cultivate-track wants to merge 2 commits into
arduino-libraries:masterfrom
cultivate-track:fileOperation-padding-fix

Conversation

@cultivate-track

Copy link
Copy Markdown

WiFiDrv::fileOperation function pads the data to be transmitted so that its length is a multiple of 4 bytes.
However, in some cases, the result of this padding is not a multiple of 4 bytes, which causes data loss.
Therefore, the code was fixed so that the length is correctly a multiple of 4 bytes.

Additionally, comments were added to clarify the meaning of the magic number.

fix WiFiDrv::fileOperation function to pad to multiple of 4 bytes.
@CLAassistant

CLAassistant commented Aug 4, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

Memory usage change @ 5efee6c

Board flash % RAM for global variables %
arduino:mbed_nano:nanorp2040connect 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table
Board examples/PreferencesCounter
flash
% examples/PreferencesCounter
RAM for global variables
% examples/PreferencesValidation
flash
% examples/PreferencesValidation
RAM for global variables
%
arduino:mbed_nano:nanorp2040connect 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrvidor4000 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwifi1010 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:nano_33_iot 0 0.0 0 0.0 0 0.0 0 0.0
Click for full report CSV
Board,examples/PreferencesCounter<br>flash,%,examples/PreferencesCounter<br>RAM for global variables,%,examples/PreferencesValidation<br>flash,%,examples/PreferencesValidation<br>RAM for global variables,%
arduino:mbed_nano:nanorp2040connect,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrvidor4000,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwifi1010,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:nano_33_iot,0,0.0,0,0.0,0,0.0,0,0.0

Comment thread src/utility/wifi_drv.cpp Outdated
SpiDrv::sendParamNoLen((uint8_t*)buffer, len, LAST_PARAM);
SpiDrv::sendParamNoLen((uint8_t*)buffer, len, LAST_PARAM); //send len + 1 bytes (*)
}
// (*)send extra 1 byte if last param

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// (*)send extra 1 byte if last param
// (*)send extra 1 byte if operation is WRITE_FILE

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have edited my comment because the intended meaning may not have been clear. Please review it again.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I find it clear now

@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels Aug 5, 2026
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Memory usage change @ 5edcb6d

Board flash % RAM for global variables %
arduino:mbed_nano:nanorp2040connect 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table
Board examples/PreferencesCounter
flash
% examples/PreferencesCounter
RAM for global variables
% examples/PreferencesValidation
flash
% examples/PreferencesValidation
RAM for global variables
%
arduino:mbed_nano:nanorp2040connect 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrvidor4000 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwifi1010 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:nano_33_iot 0 0.0 0 0.0 0 0.0 0 0.0
Click for full report CSV
Board,examples/PreferencesCounter<br>flash,%,examples/PreferencesCounter<br>RAM for global variables,%,examples/PreferencesValidation<br>flash,%,examples/PreferencesValidation<br>RAM for global variables,%
arduino:mbed_nano:nanorp2040connect,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrvidor4000,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwifi1010,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:nano_33_iot,0,0.0,0,0.0,0,0.0,0,0.0

@andreagilardoni

Copy link
Copy Markdown
Contributor

I need to test this PR, as soon i complete testing I will merge this PR

@andreagilardoni

Copy link
Copy Markdown
Contributor

@cultivate-track which board are you using? Do you have an example that reproduces the issue you are facing? I have tried to create one, but I didn't manage to reproduce the issue.

@cultivate-track

Copy link
Copy Markdown
Author

@andreagilardoni I'm using an Arduino MKR WiFi 1010 board. Here is the example code that reproduces the issue.

#include <SNU.h>
#include <ArduinoHttpClient.h> //ArduinoHttpClient library 0.6.1 by Arduino
#include <WiFiNINA.h>

#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;      // your network SSID (name)
char pass[] = SECRET_PASS;   // your network password

char urlHost[] = "downloads.arduino.cc";
char urlPath[] = "/misc/WiFi1010_blinkRBG.bin";

int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(9600);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
  }

  // wait a bit
  delay(1000);


  //start download file
  WiFiSSLClient wifiClient;
  HttpClient client = HttpClient(wifiClient, urlHost, 443);

  client.beginRequest();
  client.get(urlPath);
  client.endRequest();

  //receive header
  const int responseStatus = client.responseStatusCode();
  const int bodyLength = client.contentLength();
  client.skipResponseHeaders();

  //receive body and write to WiFiStorage
  const char tmpFileName[] = "/fs/UPDATE.BIN.TMP";

  if(WiFiStorage.exists(tmpFileName)){
    WiFiStorage.remove(tmpFileName);
  }

  WiFiStorageFile file = WiFiStorage.open(tmpFileName);
  if(responseStatus == 200 && bodyLength > 0){
    uint8_t buf[512];
    file.seek(0);
    while(!client.endOfBodyReached()){
      while(client.available() > 0){
        int readLen = client.read(buf, sizeof(buf));
        file.write(buf, readLen);
        Serial.print("Wrote "); Serial.print(readLen); Serial.println("Bytes.");
      }
    }

    client.stop();
    Serial.println("download complete.");
    file.close();


    //verify downloaded file
    client.beginRequest();
    client.get(urlPath);
    client.endRequest();

    //receive header
    const int responseStatus1 = client.responseStatusCode();
    const int bodyLength1 = client.contentLength();
    client.skipResponseHeaders();

    //receive body and verify downloaded file in WiFiStorage
    file = WiFiStorage.open(tmpFileName);
    uint8_t buf1[128], buf2[128];
    int verifyFail = 0;
    file.seek(0);
    int printPos = bodyLength1 / 50;
    while(!client.endOfBodyReached()){
      while(client.available() > 0){
        int readLen = client.read(buf1, sizeof(buf1));
        file.read(buf2, readLen);
        for(int i = 0; i < readLen; i++){
          if( buf1[i] != buf2[i] ){
            verifyFail++;
            Serial.print("Verify failed at position "); Serial.println(file.position() - readLen + i);
          }
        }
      }
    }
    file.close();

    if(verifyFail == 0){
      Serial.println("Verify OK");
    }else{
      file.erase();
    }


  }else{
    Serial.print("Status code: "); Serial.println(responseStatus);
    while(bodyLength > 0 && !client.endOfBodyReached()){
      if(client.available() > 0){
        Serial.write(client.read());
      }else{
        delay(100);
      }
    }
    client.stop();
  }

}

void loop() {
  // put your main code here, to run repeatedly:

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants