fix WiFiDrv::fileOperation function padding - #331
Open
cultivate-track wants to merge 2 commits into
Open
Conversation
fix WiFiDrv::fileOperation function to pad to multiple of 4 bytes.
|
Memory usage change @ 5efee6c
Click for full report table
Click for full report CSV |
| 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 |
Contributor
There was a problem hiding this comment.
Suggested change
| // (*)send extra 1 byte if last param | |
| // (*)send extra 1 byte if operation is WRITE_FILE |
Author
There was a problem hiding this comment.
I have edited my comment because the intended meaning may not have been clear. Please review it again.
Contributor
There was a problem hiding this comment.
Thanks, I find it clear now
|
Memory usage change @ 5edcb6d
Click for full report table
Click for full report CSV |
Contributor
|
I need to test this PR, as soon i complete testing I will merge this PR |
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. |
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:
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WiFiDrv::fileOperationfunction 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.