|
3 | 3 |
|
4 | 4 | #include "at_handler.h"
|
5 | 5 | #include "OTA.h"
|
6 |
| -#include "BossaUnoR4WiFi.h" |
7 | 6 |
|
8 | 7 | Arduino_UNOWIFIR4_OTA OTA;
|
| 8 | +void otaDownloadProgress(CAtHandler* at_handler); |
9 | 9 |
|
10 | 10 | void CAtHandler::add_cmds_ota() {
|
11 | 11 | /* ....................................................................... */
|
@@ -123,6 +123,75 @@ void CAtHandler::add_cmds_ota() {
|
123 | 123 | }
|
124 | 124 | };
|
125 | 125 |
|
| 126 | + /* ....................................................................... */ |
| 127 | + command_table[_OTA_DOWNLOAD_START] = [this](auto & srv, auto & parser) { |
| 128 | + /* ....................................................................... */ |
| 129 | + |
| 130 | + switch (parser.cmd_mode) { |
| 131 | + case chAT::CommandMode::Write: { |
| 132 | + if (parser.args.size() == 1) { |
| 133 | + auto &url = parser.args[0]; |
| 134 | + if (url.empty()) { |
| 135 | + return chAT::CommandStatus::ERROR; |
| 136 | + } |
| 137 | + |
| 138 | + int ota_error = OTA.startDownload(url.c_str()); |
| 139 | + String error = String((int)ota_error) + _ENDL; |
| 140 | + srv.write_response_prompt(); |
| 141 | + srv.write_str((const char *)(error.c_str())); |
| 142 | + srv.write_line_end(); |
| 143 | + |
| 144 | + this->addTask(std::bind(&otaDownloadProgress, this)); |
| 145 | + |
| 146 | + return chAT::CommandStatus::OK; |
| 147 | + } else if(parser.args.size() == 2) { |
| 148 | + auto &url = parser.args[0]; |
| 149 | + if (url.empty()) { |
| 150 | + return chAT::CommandStatus::ERROR; |
| 151 | + } |
| 152 | + |
| 153 | + auto &path = parser.args[1]; |
| 154 | + if (path.empty()) { |
| 155 | + return chAT::CommandStatus::ERROR; |
| 156 | + } |
| 157 | + |
| 158 | + int ota_error = OTA.startDownload(url.c_str(), path.c_str()); |
| 159 | + |
| 160 | + this->addTask(std::bind(&otaDownloadProgress, this)); |
| 161 | + |
| 162 | + String error = String((int)ota_error) + _ENDL; |
| 163 | + srv.write_response_prompt(); |
| 164 | + srv.write_str((const char *)(error.c_str())); |
| 165 | + srv.write_line_end(); |
| 166 | + return chAT::CommandStatus::OK; |
| 167 | + } else { |
| 168 | + return chAT::CommandStatus::ERROR; |
| 169 | + } |
| 170 | + |
| 171 | + } |
| 172 | + default: |
| 173 | + return chAT::CommandStatus::ERROR; |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + /* ....................................................................... */ |
| 178 | + command_table[_OTA_DOWNLOAD_PROGRESS] = [this](auto & srv, auto & parser) { |
| 179 | + /* ....................................................................... */ |
| 180 | + switch (parser.cmd_mode) { |
| 181 | + case chAT::CommandMode::Run: { |
| 182 | + int progress = OTA.downloadProgress(); |
| 183 | + String pogress_str = String((int)progress) + _ENDL; |
| 184 | + srv.write_response_prompt(); |
| 185 | + srv.write_str((const char *)(pogress_str.c_str())); |
| 186 | + srv.write_line_end(); |
| 187 | + |
| 188 | + return chAT::CommandStatus::OK; |
| 189 | + } |
| 190 | + default: |
| 191 | + return chAT::CommandStatus::ERROR; |
| 192 | + } |
| 193 | + }; |
| 194 | + |
126 | 195 | /* ....................................................................... */
|
127 | 196 | command_table[_OTA_VERIFY] = [this](auto & srv, auto & parser) {
|
128 | 197 | /* ....................................................................... */
|
@@ -162,8 +231,8 @@ void CAtHandler::add_cmds_ota() {
|
162 | 231 | return chAT::CommandStatus::ERROR;
|
163 | 232 | }
|
164 | 233 |
|
165 |
| - int flash_error = BOSSA.program(path.c_str(), Serial, GPIO_BOOT, GPIO_RST); |
166 |
| - String error = String(flash_error) + "\r\n"; |
| 234 | + int flash_error = OTA.update(path.c_str()); |
| 235 | + String error = String(flash_error) + _ENDL; |
167 | 236 | srv.write_response_prompt();
|
168 | 237 | srv.write_str((const char *)(error.c_str()));
|
169 | 238 | srv.write_line_end();
|
@@ -191,4 +260,15 @@ void CAtHandler::add_cmds_ota() {
|
191 | 260 | };
|
192 | 261 | }
|
193 | 262 |
|
| 263 | +// in order to make the download progress downloadPoll must be called periodically |
| 264 | +// until the end, this function readds itself to the task list until completed |
| 265 | +void otaDownloadProgress(CAtHandler* at_handler) { |
| 266 | + auto res = OTA.downloadPoll(); |
| 267 | + |
| 268 | + // continue to progress the download if the state is not completed or not an error |
| 269 | + if(res == 0) { |
| 270 | + at_handler->addTask(std::bind(&otaDownloadProgress, at_handler)); |
| 271 | + } |
| 272 | +}; |
| 273 | + |
194 | 274 | #endif
|
0 commit comments