-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcmds_ota.h
194 lines (174 loc) · 6.99 KB
/
cmds_ota.h
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
#ifndef CMDS_OTA_H
#define CMDS_OTA_H
#include "at_handler.h"
#include "OTA.h"
#include "BossaUnoR4WiFi.h"
Arduino_UNOWIFIR4_OTA OTA;
void CAtHandler::add_cmds_ota() {
/* ....................................................................... */
command_table[_OTA_SETCAROOT] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
switch (parser.cmd_mode) {
case chAT::CommandMode::Write: {
if (parser.args.size() != 1) {
return chAT::CommandStatus::ERROR;
}
int ca_root_size = 0;
auto &ca_root_size_str = parser.args[0];
if (ca_root_size_str.empty()) {
return chAT::CommandStatus::ERROR;
}
ca_root_size = atoi(ca_root_size_str.c_str());
if(!ca_root_size) {
return chAT::CommandStatus::ERROR;
}
ota_cert_buf = srv.inhibit_read(ca_root_size);
size_t offset = ota_cert_buf.size();
if(offset < ca_root_size) {
ota_cert_buf.resize(ca_root_size);
do {
offset += serial->read(ota_cert_buf.data() + offset, ca_root_size - offset);
} while (offset < ca_root_size);
}
OTA.setCACert((const char *)ota_cert_buf.data());
srv.continue_read();
return chAT::CommandStatus::OK;
}
default:
return chAT::CommandStatus::ERROR;
}
};
/* ....................................................................... */
command_table[_OTA_BEGIN] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
switch (parser.cmd_mode) {
case chAT::CommandMode::Run: {
Arduino_ESP32_OTA::Error ota_error = OTA.begin();
String error = String((int)ota_error) + "\r\n";
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;
}
case chAT::CommandMode::Write: {
if (parser.args.size() != 1) {
return chAT::CommandStatus::ERROR;
}
auto &path = parser.args[0];
if (path.empty()) {
return chAT::CommandStatus::ERROR;
}
Arduino_ESP32_OTA::Error ota_error = OTA.begin(path.c_str());
String error = String((int)ota_error) + "\r\n";
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;
}
default:
return chAT::CommandStatus::ERROR;
}
};
/* ....................................................................... */
command_table[_OTA_DOWNLOAD] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
switch (parser.cmd_mode) {
case chAT::CommandMode::Write: {
if (parser.args.size() == 1) {
auto &url = parser.args[0];
if (url.empty()) {
return chAT::CommandStatus::ERROR;
}
int ota_error = OTA.download(url.c_str());
String error = String((int)ota_error) + "\r\n";
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;
} else if(parser.args.size() == 2) {
auto &url = parser.args[0];
if (url.empty()) {
return chAT::CommandStatus::ERROR;
}
auto &path = parser.args[1];
if (path.empty()) {
return chAT::CommandStatus::ERROR;
}
int ota_error = OTA.download(url.c_str(), path.c_str());
String error = String((int)ota_error) + "\r\n";
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;
} else {
return chAT::CommandStatus::ERROR;
}
}
default:
return chAT::CommandStatus::ERROR;
}
};
/* ....................................................................... */
command_table[_OTA_VERIFY] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
switch (parser.cmd_mode) {
case chAT::CommandMode::Run: {
Arduino_ESP32_OTA::Error ota_error = OTA.verify();
String error = String((int)ota_error) + "\r\n";
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;
}
default:
return chAT::CommandStatus::ERROR;
}
};
/* ....................................................................... */
command_table[_OTA_UPDATE] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
switch (parser.cmd_mode) {
case chAT::CommandMode::Run: {
Arduino_ESP32_OTA::Error ota_error = OTA.update();
String error = String((int)ota_error) + "\r\n";
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;
}
case chAT::CommandMode::Write: {
if (parser.args.size() != 1) {
return chAT::CommandStatus::ERROR;
}
auto &path = parser.args[0];
if (path.empty()) {
return chAT::CommandStatus::ERROR;
}
int flash_error = BOSSA.program(path.c_str(), Serial, GPIO_BOOT, GPIO_RST);
String error = String(flash_error) + "\r\n";
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;
}
default:
return chAT::CommandStatus::ERROR;
}
};
/* ....................................................................... */
command_table[_OTA_RESET] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
switch (parser.cmd_mode) {
case chAT::CommandMode::Run: {
OTA.reset();
srv.write_response_prompt();
srv.write_str("0");
srv.write_line_end();
return chAT::CommandStatus::OK;
}
default:
return chAT::CommandStatus::ERROR;
}
};
}
#endif