Skip to content

Commit 036094b

Browse files
committed
Use separate OTA commands to perform update
1 parent 58341d5 commit 036094b

File tree

3 files changed

+161
-46
lines changed

3 files changed

+161
-46
lines changed

libraries/OTAUpdate/examples/OTA/OTA.ino

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
22
OTA
33
4-
This sketch demonstrates how to make an OTA Update on the UNO R4 WiFi
4+
This sketch demonstrates how to make an OTA Update on the UNO R4 WiFi.
5+
Upload the sketch and wait for the invasion!
56
67
*/
78

@@ -17,6 +18,7 @@ char pass[] = SECRET_PASS; // your network password (use for WPA, or use as k
1718
int status = WL_IDLE_STATUS;
1819

1920
OTAUpdate ota;
21+
static char const OTA_FILE_LOCATION[] = "https://downloads.arduino.cc/ota/UNOR4WIFI_Animation.ota";
2022

2123
/* -------------------------------------------------------------------------- */
2224
void setup() {
@@ -50,18 +52,42 @@ void setup() {
5052
delay(1000);
5153
}
5254

53-
printWifiStatus();
55+
OTAUpdate::Error ret = OTAUpdate::Error::None;
56+
ret = ota.begin("/update.bin");
57+
if(ret != OTAUpdate::Error::None) {
58+
Serial.println("ota.begin() error: ");
59+
Serial.println((int)ret);
60+
return;
61+
}
62+
ret = ota.setCACert(root_ca);
63+
if(ret != OTAUpdate::Error::None) {
64+
Serial.println("ota.setCACert() error: ");
65+
Serial.println((int)ret);
66+
return;
67+
}
68+
if(ota.download(OTA_FILE_LOCATION, "/update.bin") <= 0) {
69+
Serial.println("ota.download() error: ");
70+
Serial.println((int)ret);
71+
return;
72+
}
73+
ret = ota.verify();
74+
if(ret != OTAUpdate::Error::None) {
75+
Serial.println("ota.verify() error: ");
76+
Serial.println((int)ret);
77+
return;
78+
}
5479

55-
if(!ota.isRunning()) {
56-
ota.setCACert(root_ca);
57-
ota.update("http://downloads.arduino.cc/ota/UNOR4WIFI_Animation.ota");
80+
ret = ota.update("/update.bin");
81+
if(ret != OTAUpdate::Error::None) {
82+
Serial.println("ota.update() error: ");
83+
Serial.println((int)ret);
84+
return;
5885
}
5986
}
6087

6188
/* -------------------------------------------------------------------------- */
6289
void loop() {
6390
/* -------------------------------------------------------------------------- */
64-
Serial.println(ota.getLastError());
6591
delay(1000);
6692
}
6793

libraries/OTAUpdate/src/OTAUpdate.cpp

Lines changed: 98 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,102 @@ using namespace std;
2222

2323
OTAUpdate::OTAUpdate() {}
2424

25-
int OTAUpdate::update(const char* url) {
26-
string res = "";
27-
if(modem.write(string(PROMPT(_OTA_RUN)), res, "%s%s\r\n" , CMD_WRITE(_OTA_RUN), url)) {
28-
return 1;
29-
}
30-
return 0;
31-
}
32-
33-
int OTAUpdate::setCACert(const char* root_ca) {
34-
string res = "";
35-
if(strlen(root_ca) > 0) {
36-
modem.write_nowait(string(PROMPT(_OTA_SETCAROOT)),res, "%s%d\r\n" , CMD_WRITE(_OTA_SETCAROOT), strlen(root_ca));
37-
if(modem.passthrough((uint8_t *)root_ca, strlen(root_ca))) {
38-
return 1;
39-
}
40-
}
41-
return 0;
42-
}
43-
44-
bool OTAUpdate::isRunning() {
45-
string res = "";
46-
if(modem.write(string(PROMPT(_OTA_RUN)), res, CMD_READ(_OTA_RUN))) {
47-
return atoi(res.c_str());
48-
}
49-
return 0;
50-
}
51-
52-
int OTAUpdate::getLastError() {
53-
string res = "";
54-
if(modem.write(string(PROMPT(_OTA_ERROR)), res, CMD_READ(_OTA_ERROR))) {
55-
return atoi(res.c_str());
56-
}
57-
return 0;
25+
OTAUpdate::Error OTAUpdate::setCACert(const char* root_ca) {
26+
string res = "";
27+
if ( root_ca != nullptr && strlen(root_ca) > 0) {
28+
modem.write_nowait(string(PROMPT(_OTA_SETCAROOT)), res, "%s%d\r\n", CMD_WRITE(_OTA_SETCAROOT), strlen(root_ca));
29+
if(modem.passthrough((uint8_t *)root_ca, strlen(root_ca))) {
30+
return Error::None;
31+
}
32+
return Error::Modem;
33+
}
34+
return Error::Library;
35+
}
36+
37+
OTAUpdate::Error OTAUpdate::begin() {
38+
string res = "";
39+
if (modem.write(string(PROMPT(_OTA_BEGIN)), res, "%s", CMD(_OTA_BEGIN))) {
40+
return static_cast<OTAUpdate::Error>(atoi(res.c_str()));
41+
}
42+
return Error::Modem;
43+
}
44+
45+
OTAUpdate::Error OTAUpdate::begin(const char* file_path) {
46+
string res = "";
47+
if ( file_path != nullptr && strlen(file_path) > 0) {
48+
if (modem.write(string(PROMPT(_OTA_BEGIN)), res, "%s%s\r\n", CMD_WRITE(_OTA_BEGIN), file_path)) {
49+
return static_cast<OTAUpdate::Error>(atoi(res.c_str()));
50+
}
51+
return Error::Modem;
52+
}
53+
return Error::Library;
54+
}
55+
56+
int OTAUpdate::download(const char* url) {
57+
string res = "";
58+
int ret = -1;
59+
if ( url != nullptr && strlen(url) > 0) {
60+
modem.timeout(EXTENDED_MODEM_TIMEOUT);
61+
if(modem.write(string(PROMPT(_OTA_DOWNLOAD)), res, "%s%s\r\n", CMD_WRITE(_OTA_DOWNLOAD), url)) {
62+
ret = atoi(res.c_str());
63+
} else {
64+
ret = static_cast<int>(Error::Modem);
65+
}
66+
} else {
67+
ret = static_cast<int>(Error::Library);
68+
}
69+
modem.timeout(MODEM_TIMEOUT);
70+
return ret;
71+
}
72+
73+
int OTAUpdate::download(const char* url, const char* file_path) {
74+
string res = "";
75+
int ret = -1;
76+
if ( url != nullptr && strlen(url) > 0 && file_path != nullptr && strlen(file_path) >0) {
77+
modem.timeout(EXTENDED_MODEM_TIMEOUT);
78+
if(modem.write(string(PROMPT(_OTA_DOWNLOAD)), res, "%s%s,%s\r\n", CMD_WRITE(_OTA_DOWNLOAD), url, file_path)) {
79+
ret = atoi(res.c_str());
80+
} else {
81+
ret = static_cast<int>(Error::Modem);
82+
}
83+
} else {
84+
ret = static_cast<int>(Error::Library);
85+
}
86+
modem.timeout(MODEM_TIMEOUT);
87+
return ret;
88+
}
89+
90+
OTAUpdate::Error OTAUpdate::verify() {
91+
string res = "";
92+
if (modem.write(string(PROMPT(_OTA_VERIFY)), res, "%s", CMD(_OTA_VERIFY))) {
93+
return static_cast<OTAUpdate::Error>(atoi(res.c_str()));
94+
}
95+
return Error::Modem;
96+
}
97+
98+
OTAUpdate::Error OTAUpdate::update() {
99+
string res = "";
100+
if (modem.write(string(PROMPT(_OTA_UPDATE)), res, "%s", CMD(_OTA_UPDATE))) {
101+
return static_cast<OTAUpdate::Error>(atoi(res.c_str()));
102+
}
103+
return Error::Modem;
104+
}
105+
106+
OTAUpdate::Error OTAUpdate::update(const char* file_path) {
107+
string res = "";
108+
if ( file_path != nullptr && strlen(file_path) > 0) {
109+
if (modem.write(string(PROMPT(_OTA_UPDATE)), res, "%s%s\r\n", CMD_WRITE(_OTA_UPDATE), file_path)) {
110+
return Error::None;
111+
}
112+
return Error::Modem;
113+
}
114+
return Error::Library;
115+
}
116+
117+
OTAUpdate::Error OTAUpdate::reset() {
118+
string res = "";
119+
if (modem.write(string(PROMPT(_OTA_RESET)), res, "%s", CMD(_OTA_RESET))) {
120+
return Error::None;
121+
}
122+
return Error::Modem;
58123
}

libraries/OTAUpdate/src/OTAUpdate.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,38 @@
2626

2727
class OTAUpdate {
2828

29-
//Add error definitions
30-
3129
public:
32-
OTAUpdate();
33-
int setCACert(const char* root_ca);
34-
int update(const char* url);
35-
bool isRunning();
36-
int getLastError();
30+
31+
enum class Error: int {
32+
None = 0,
33+
StorageConfig = -1,
34+
NoOtaStorage = -2,
35+
OtaStorageInit = -3,
36+
OtaStorageEnd = -4,
37+
UrlParseError = -5,
38+
ServerConnectError = -6,
39+
HttpHeaderError = -7,
40+
ParseHttpHeader = -8,
41+
OtaHeaderLength = -9,
42+
OtaHeaderCrc = -10,
43+
OtaHeaderMagicNumber = -11,
44+
OtaDownload = -12,
45+
OtaFlash = -13,
46+
Library = -14,
47+
Modem = -15,
48+
};
49+
50+
OTAUpdate();
51+
OTAUpdate::Error setCACert(const char* root_ca);
52+
OTAUpdate::Error begin();
53+
OTAUpdate::Error begin(const char* file_path);
54+
int download(const char* url);
55+
int download(const char* url, const char* file_path);
56+
OTAUpdate::Error verify();
57+
OTAUpdate::Error update();
58+
OTAUpdate::Error update(const char* file_path);
59+
OTAUpdate::Error reset();
60+
3761
};
3862

3963
#endif /* OTA_UPDATE_H */

0 commit comments

Comments
 (0)