forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFU.cpp
214 lines (183 loc) · 6.94 KB
/
SFU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
SFU.h
Copyright (c) 2023 Arduino SA. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SFU.h"
#include <BlockDevice.h>
#include <MBRBlockDevice.h>
#include "FATFileSystem.h"
#include <Arduino_DebugUtils.h>
#include <WiFiC3.h>
#include <WiFiSSLClient.h>
#define AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms (5*1000UL);
#define AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms (5*60*1000UL);
const unsigned char SFU[0x20000] __attribute__ ((section(".second_stage_ota"), used)) = {
#include "c33.h"
};
BlockDevice* block_device = BlockDevice::get_default_instance();
MBRBlockDevice mbr(block_device, 1);
FATFileSystem fs("ota");
/* Original code: http://stackoverflow.com/questions/2616011/easy-way-to-parse-a-url-in-c-cross-platform */
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
struct URI {
public:
URI(const std::string& url_s) {
this->parse(url_s);
}
std::string protocol_, host_, path_, query_;
private:
void parse(const std::string& url_s);
};
using namespace std;
// ctors, copy, equality, ...
// TODO: change me into something embedded friendly (this function adds ~100KB to flash)
void URI::parse(const string& url_s)
{
const string prot_end("://");
string::const_iterator prot_i = search(url_s.begin(), url_s.end(),
prot_end.begin(), prot_end.end());
protocol_.reserve(distance(url_s.begin(), prot_i));
transform(url_s.begin(), prot_i,
back_inserter(protocol_),
ptr_fun<int,int>(tolower)); // protocol is icase
if( prot_i == url_s.end() )
return;
advance(prot_i, prot_end.length());
string::const_iterator path_i = find(prot_i, url_s.end(), '/');
host_.reserve(distance(prot_i, path_i));
transform(prot_i, path_i,
back_inserter(host_),
ptr_fun<int,int>(tolower)); // host is icase
string::const_iterator query_i = find(path_i, url_s.end(), '?');
path_.assign(path_i, query_i);
if( query_i != url_s.end() )
++query_i;
query_.assign(query_i, url_s.end());
}
int SFU::download(const char* ota_url) {
int err = -1;
if ((err = fs.reformat(&mbr)) != 0)
{
DEBUG_ERROR("%s: fs.reformat() failed with %d", __FUNCTION__, err);
return static_cast<int>(OTAError::PORTENTA_C33_ErrorReformat);
}
FILE * file = fopen("/ota/UPDATE.BIN.OTA", "wb");
if (!file)
{
DEBUG_ERROR("%s: fopen() failed", __FUNCTION__);
fclose(file);
return static_cast<int>(OTAError::PORTENTA_C33_ErrorOpenUpdateFile);
}
URI url(ota_url);
Client * client = nullptr;
int port = 0;
if (url.protocol_ == "http") {
client = new WiFiClient();
port = 80;
} else if (url.protocol_ == "https") {
client = new WiFiSSLClient();
port = 443;
} else {
DEBUG_ERROR("%s: Failed to parse OTA URL %s", __FUNCTION__, url.host_.c_str());
fclose(file);
return static_cast<int>(OTAError::PORTENTA_C33_UrlParseError);
}
if (!client->connect(url.host_.c_str(), port))
{
DEBUG_ERROR("%s: Connection failure with OTA storage server %s", __FUNCTION__, url.host_.c_str());
fclose(file);
return static_cast<int>(OTAError::PORTENTA_C33_ServerConnectError);
}
client->println(String("GET ") + url.path_.c_str() + " HTTP/1.1");
client->println(String("Host: ") + url.host_.c_str());
client->println("Connection: close");
client->println();
/* Receive HTTP header. */
String http_header;
bool is_header_complete = false,
is_http_header_timeout = false;
for (unsigned long const start = millis(); !is_header_complete;)
{
is_http_header_timeout = (millis() - start) > AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms;
if (is_http_header_timeout) break;
if (client->available())
{
char const c = client->read();
http_header += c;
if (http_header.endsWith("\r\n\r\n"))
is_header_complete = true;
}
}
if (!is_header_complete)
{
DEBUG_ERROR("%s: Error receiving HTTP header %s", __FUNCTION__, is_http_header_timeout ? "(timeout)":"");
fclose(file);
return static_cast<int>(OTAError::PORTENTA_C33_HttpHeaderError);
}
/* Extract concent length from HTTP header. A typical entry looks like
* "Content-Length: 123456"
*/
char const * content_length_ptr = strstr(http_header.c_str(), "Content-Length");
if (!content_length_ptr)
{
DEBUG_ERROR("%s: Failure to extract content length from http header", __FUNCTION__);
fclose(file);
return static_cast<int>(OTAError::PORTENTA_C33_ErrorParseHttpHeader);
}
/* Find start of numerical value. */
char * ptr = const_cast<char *>(content_length_ptr);
for (; (*ptr != '\0') && !isDigit(*ptr); ptr++) { }
/* Extract numerical value. */
String content_length_str;
for (; isDigit(*ptr); ptr++) content_length_str += *ptr;
int const content_length_val = atoi(content_length_str.c_str());
DEBUG_VERBOSE("%s: Length of OTA binary according to HTTP header = %d bytes", __FUNCTION__, content_length_val);
/* Receive as many bytes as are indicated by the HTTP header - or die trying. */
int bytes_received = 0;
bool is_http_data_timeout = false;
for(unsigned long const start = millis(); bytes_received < content_length_val;)
{
is_http_data_timeout = (millis() - start) > AIOT_CONFIG_PORTENTA_C33_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms;
if (is_http_data_timeout) break;
if (client->available())
{
char const c = client->read();
if (fwrite(&c, 1, sizeof(c), file) != sizeof(c))
{
DEBUG_ERROR("%s: Writing of firmware image to flash failed", __FUNCTION__);
fclose(file);
return static_cast<int>(OTAError::PORTENTA_C33_ErrorWriteUpdateFile);
}
bytes_received++;
}
}
if (bytes_received != content_length_val) {
DEBUG_ERROR("%s: Error receiving HTTP data %s (%d bytes received, %d expected)", __FUNCTION__, is_http_data_timeout ? "(timeout)":"", bytes_received, content_length_val);
fclose(file);
return static_cast<int>(OTAError::PORTENTA_C33_HttpDataError);
}
DEBUG_INFO("%s: %d bytes received", __FUNCTION__, ftell(file));
fclose(file);
/* Unmount the filesystem. */
if ((err = fs.unmount()) != 0)
{
DEBUG_ERROR("%s: fs.unmount() failed with %d", __FUNCTION__, err);
return static_cast<int>(OTAError::PORTENTA_C33_ErrorUnmount);
}
return static_cast<int>(OTAError::None);
}