Skip to content

Commit 6f2f6e4

Browse files
committed
SFU: download: take client as a funciotn parameter
1 parent 7a31968 commit 6f2f6e4

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

libraries/SFU/examples/OTAUpdate/OTAUpdate.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ BlockDevice* block_device = BlockDevice::get_default_instance();
4545
MBRBlockDevice mbr(block_device, 1);
4646
FATFileSystem fs("ota");
4747

48+
WiFiClient client;
49+
4850
/******************************************************************************
4951
* SETUP/LOOP
5052
******************************************************************************/
@@ -85,7 +87,7 @@ void setup()
8587

8688
SFU::begin();
8789

88-
SFU::download("/ota/UPDATE.BIN.OTA", OTA_FILE_LOCATION);
90+
SFU::download(client, "/ota/UPDATE.BIN.OTA", OTA_FILE_LOCATION);
8991

9092
/* Unmount the filesystem. */
9193
if ((err = fs.unmount()) != 0)

libraries/SFU/src/SFU.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <Arduino_DebugUtils.h>
2222
#include <WiFiC3.h>
2323
#include <WiFiSSLClient.h>
24+
#include <Client.h>
2425

2526
#define AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms (5*1000UL);
2627
#define AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms (5*60*1000UL);
@@ -74,7 +75,7 @@ void URI::parse(const string& url_s)
7475
query_.assign(query_i, url_s.end());
7576
}
7677

77-
int SFU::download(const char* ota_path, const char* ota_url) {
78+
int SFU::download(Client& client, const char* ota_path, const char* ota_url) {
7879
int err = -1;
7980

8081
FILE * file = fopen(ota_path, "wb");
@@ -86,32 +87,29 @@ int SFU::download(const char* ota_path, const char* ota_url) {
8687
}
8788

8889
URI url(ota_url);
89-
Client * client = nullptr;
9090
int port = 0;
9191

9292
if (url.protocol_ == "http") {
93-
client = new WiFiClient();
9493
port = 80;
9594
} else if (url.protocol_ == "https") {
96-
client = new WiFiSSLClient();
9795
port = 443;
9896
} else {
9997
DEBUG_ERROR("%s: Failed to parse OTA URL %s", __FUNCTION__, url.host_.c_str());
10098
fclose(file);
10199
return static_cast<int>(OTAError::PORTENTA_C33_UrlParseError);
102100
}
103101

104-
if (!client->connect(url.host_.c_str(), port))
102+
if (!client.connect(url.host_.c_str(), port))
105103
{
106104
DEBUG_ERROR("%s: Connection failure with OTA storage server %s", __FUNCTION__, url.host_.c_str());
107105
fclose(file);
108106
return static_cast<int>(OTAError::PORTENTA_C33_ServerConnectError);
109107
}
110108

111-
client->println(String("GET ") + url.path_.c_str() + " HTTP/1.1");
112-
client->println(String("Host: ") + url.host_.c_str());
113-
client->println("Connection: close");
114-
client->println();
109+
client.println(String("GET ") + url.path_.c_str() + " HTTP/1.1");
110+
client.println(String("Host: ") + url.host_.c_str());
111+
client.println("Connection: close");
112+
client.println();
115113

116114
/* Receive HTTP header. */
117115
String http_header;
@@ -122,9 +120,9 @@ int SFU::download(const char* ota_path, const char* ota_url) {
122120
is_http_header_timeout = (millis() - start) > AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms;
123121
if (is_http_header_timeout) break;
124122

125-
if (client->available())
123+
if (client.available())
126124
{
127-
char const c = client->read();
125+
char const c = client.read();
128126

129127
http_header += c;
130128
if (http_header.endsWith("\r\n\r\n"))
@@ -166,9 +164,9 @@ int SFU::download(const char* ota_path, const char* ota_url) {
166164
is_http_data_timeout = (millis() - start) > AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms;
167165
if (is_http_data_timeout) break;
168166

169-
if (client->available())
167+
if (client.available())
170168
{
171-
char const c = client->read();
169+
char const c = client.read();
172170

173171
if (fwrite(&c, 1, sizeof(c), file) != sizeof(c))
174172
{

libraries/SFU/src/SFU.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define _SFU_H_
2222

2323
#include "Arduino.h"
24+
#include "Client.h"
2425

2526
#define PORTENTA_C33_OTA_ERROR_BASE (-300)
2627

@@ -42,7 +43,7 @@ enum class OTAError : int
4243
class SFU {
4344
public:
4445
static int begin() {};
45-
static int download(const char* ota_path, const char* ota_url);
46+
static int download(Client& client, const char* ota_path, const char* ota_url);
4647
static int apply() { NVIC_SystemReset(); };
4748
};
4849

0 commit comments

Comments
 (0)