Skip to content

Commit 7dddfd1

Browse files
committed
Extracting the code responsible for determining the length of the update file from the filesystem.
1 parent 65f238f commit 7dddfd1

File tree

5 files changed

+34
-44
lines changed

5 files changed

+34
-44
lines changed

src/Arduino_Portenta_OTA.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ void Arduino_Portenta_OTA::reset()
7171
NVIC_SystemReset();
7272
}
7373

74+
/******************************************************************************
75+
* PROTECTED MEMBER FUNCTIONS
76+
******************************************************************************/
77+
78+
bool Arduino_Portenta_OTA::findProgramLength(DIR * dir, uint32_t & program_length)
79+
{
80+
struct dirent * entry = NULL;
81+
while ((entry = readdir(dir)) != NULL)
82+
{
83+
if (String(entry->d_name) == "UPDATE.BIN")
84+
{
85+
struct stat stat_buf;
86+
stat("/fs/UPDATE.BIN", &stat_buf);
87+
program_length = stat_buf.st_size;
88+
return true;
89+
}
90+
}
91+
92+
return false;
93+
}
94+
7495
/******************************************************************************
7596
* PRIVATE MEMBER FUNCTIONS
7697
******************************************************************************/

src/Arduino_Portenta_OTA.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class Arduino_Portenta_OTA
103103
virtual bool init() = 0;
104104
virtual bool open() = 0;
105105

106+
static bool findProgramLength(DIR * dir, uint32_t & program_length);
106107

107108
private:
108109

src/Arduino_Portenta_OTA_InternalFlash.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,15 @@ bool Arduino_Portenta_OTA_InternalFlash::init()
7777
bool Arduino_Portenta_OTA_InternalFlash::open()
7878
{
7979
DIR * dir = NULL;
80-
struct dirent *entry = NULL;
8180

8281
if (_storage_type == INTERNAL_FLASH_FATFS)
8382
{
8483
if ((dir = opendir("/fs")) != NULL)
8584
{
86-
/* print all the files and directories within directory */
87-
while ((entry = readdir(dir)) != NULL)
85+
if (Arduino_Portenta_OTA::findProgramLength(dir, _program_length))
8886
{
89-
if (String(entry->d_name) == "UPDATE.BIN")
90-
{
91-
struct stat stat_buf;
92-
stat("/fs/UPDATE.BIN", &stat_buf);
93-
_program_length = stat_buf.st_size;
94-
closedir(dir);
95-
return true;
96-
}
87+
closedir(dir);
88+
return true;
9789
}
9890
closedir(dir);
9991
}
@@ -104,17 +96,10 @@ bool Arduino_Portenta_OTA_InternalFlash::open()
10496
{
10597
if ((dir = opendir("/little_fs")) != NULL)
10698
{
107-
/* print all the files and directories within directory */
108-
while ((entry = readdir(dir)) != NULL)
99+
if (Arduino_Portenta_OTA::findProgramLength(dir, _program_length))
109100
{
110-
if (String(entry->d_name) == "UPDATE.BIN")
111-
{
112-
struct stat stat_buf;
113-
stat("/little_fs/UPDATE.BIN", &stat_buf);
114-
_program_length = stat_buf.st_size;
115-
closedir(dir);
116-
return true;
117-
}
101+
closedir(dir);
102+
return true;
118103
}
119104
closedir(dir);
120105
}

src/Arduino_Portenta_OTA_QSPI.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,10 @@ bool Arduino_Portenta_OTA_QSPI::open()
8686
DIR * dir = NULL;
8787
if ((dir = opendir("/fs")) != NULL)
8888
{
89-
/* print all the files and directories within directory */
90-
struct dirent * entry = NULL;
91-
while ((entry = readdir(dir)) != NULL)
89+
if (Arduino_Portenta_OTA::findProgramLength(dir, _program_length))
9290
{
93-
if (String(entry->d_name) == "UPDATE.BIN")
94-
{
95-
struct stat stat_buf;
96-
stat("/fs/UPDATE.BIN", &stat_buf);
97-
_program_length = stat_buf.st_size;
98-
closedir(dir);
99-
return true;
100-
}
91+
closedir(dir);
92+
return true;
10193
}
10294
closedir(dir);
10395
}

src/Arduino_Portenta_OTA_SD.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,13 @@ bool Arduino_Portenta_OTA_SD::open()
9595
DIR * dir = NULL;
9696
if ((dir = opendir("/fs")) != NULL)
9797
{
98-
/* print all the files and directories within directory */
99-
struct dirent * entry = NULL;
100-
while ((entry = readdir(dir)) != NULL)
98+
if (Arduino_Portenta_OTA::findProgramLength(dir, _program_length))
10199
{
102-
if (String(entry->d_name) == "UPDATE.BIN")
103-
{
104-
struct stat stat_buf;
105-
stat("/fs/UPDATE.BIN", &stat_buf);
106-
_program_length = stat_buf.st_size;
107-
closedir(dir);
108-
return true;
109-
}
100+
closedir(dir);
101+
return true;
110102
}
111103
closedir(dir);
112104
}
113-
return false;
114105
}
115106

116107
return false;

0 commit comments

Comments
 (0)