|
| 1 | +#ifndef CMDS_OTA_H |
| 2 | +#define CMDS_OTA_H |
| 3 | + |
| 4 | +#include "at_handler.h" |
| 5 | +#include "Arduino_ESP32_OTA.h" |
| 6 | +#include <BossaArduino.h> |
| 7 | + |
| 8 | +Arduino_ESP32_OTA OTA; |
| 9 | + |
| 10 | +void CAtHandler::add_cmds_ota() { |
| 11 | + /* ....................................................................... */ |
| 12 | + command_table[_OTA_SETCAROOT] = [this](auto & srv, auto & parser) { |
| 13 | + /* ....................................................................... */ |
| 14 | + switch (parser.cmd_mode) { |
| 15 | + case chAT::CommandMode::Write: { |
| 16 | + if (parser.args.size() != 1) { |
| 17 | + return chAT::CommandStatus::ERROR; |
| 18 | + } |
| 19 | + |
| 20 | + int ca_root_size = 0; |
| 21 | + auto &ca_root_size_str = parser.args[0]; |
| 22 | + if (ca_root_size_str.empty()) { |
| 23 | + return chAT::CommandStatus::ERROR; |
| 24 | + } |
| 25 | + ca_root_size = atoi(ca_root_size_str.c_str()); |
| 26 | + if(!ca_root_size) { |
| 27 | + return chAT::CommandStatus::ERROR; |
| 28 | + } |
| 29 | + |
| 30 | + cert_buf = srv.inhibit_read(ca_root_size); |
| 31 | + size_t offset = cert_buf.size(); |
| 32 | + if(offset < ca_root_size) { |
| 33 | + cert_buf.resize(ca_root_size); |
| 34 | + do { |
| 35 | + offset += serial->read(cert_buf.data() + offset, ca_root_size - offset); |
| 36 | + } while (offset < ca_root_size); |
| 37 | + } |
| 38 | + OTA.setCACert((const char *)cert_buf.data()); |
| 39 | + srv.continue_read(); |
| 40 | + return chAT::CommandStatus::OK; |
| 41 | + } |
| 42 | + default: |
| 43 | + return chAT::CommandStatus::ERROR; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + /* ....................................................................... */ |
| 48 | + command_table[_OTA_BEGIN] = [this](auto & srv, auto & parser) { |
| 49 | + /* ....................................................................... */ |
| 50 | + switch (parser.cmd_mode) { |
| 51 | + case chAT::CommandMode::Run: { |
| 52 | + Arduino_ESP32_OTA::Error ota_error = OTA.begin(); |
| 53 | + String error = String((int)ota_error) + "\r\n"; |
| 54 | + srv.write_response_prompt(); |
| 55 | + srv.write_str((const char *)(error.c_str())); |
| 56 | + srv.write_line_end(); |
| 57 | + return chAT::CommandStatus::OK; |
| 58 | + } |
| 59 | + case chAT::CommandMode::Write: { |
| 60 | + if (parser.args.size() != 1) { |
| 61 | + return chAT::CommandStatus::ERROR; |
| 62 | + } |
| 63 | + |
| 64 | + auto &path = parser.args[0]; |
| 65 | + if (path.empty()) { |
| 66 | + return chAT::CommandStatus::ERROR; |
| 67 | + } |
| 68 | + |
| 69 | + Arduino_ESP32_OTA::Error ota_error = OTA.begin(path.c_str()); |
| 70 | + String error = String((int)ota_error) + "\r\n"; |
| 71 | + srv.write_response_prompt(); |
| 72 | + srv.write_str((const char *)(error.c_str())); |
| 73 | + srv.write_line_end(); |
| 74 | + return chAT::CommandStatus::OK; |
| 75 | + |
| 76 | + } |
| 77 | + default: |
| 78 | + return chAT::CommandStatus::ERROR; |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + /* ....................................................................... */ |
| 83 | + command_table[_OTA_DOWNLOAD] = [this](auto & srv, auto & parser) { |
| 84 | + /* ....................................................................... */ |
| 85 | + switch (parser.cmd_mode) { |
| 86 | + case chAT::CommandMode::Write: { |
| 87 | + if (parser.args.size() == 1) { |
| 88 | + auto &url = parser.args[0]; |
| 89 | + if (url.empty()) { |
| 90 | + return chAT::CommandStatus::ERROR; |
| 91 | + } |
| 92 | + |
| 93 | + int ota_error = OTA.download(url.c_str()); |
| 94 | + String error = String((int)ota_error) + "\r\n"; |
| 95 | + srv.write_response_prompt(); |
| 96 | + srv.write_str((const char *)(error.c_str())); |
| 97 | + srv.write_line_end(); |
| 98 | + return chAT::CommandStatus::OK; |
| 99 | + } else if(parser.args.size() == 2) { |
| 100 | + auto &url = parser.args[0]; |
| 101 | + if (url.empty()) { |
| 102 | + return chAT::CommandStatus::ERROR; |
| 103 | + } |
| 104 | + |
| 105 | + auto &path = parser.args[1]; |
| 106 | + if (path.empty()) { |
| 107 | + return chAT::CommandStatus::ERROR; |
| 108 | + } |
| 109 | + |
| 110 | + int ota_error = OTA.download(url.c_str(), path.c_str()); |
| 111 | + String error = String((int)ota_error) + "\r\n"; |
| 112 | + srv.write_response_prompt(); |
| 113 | + srv.write_str((const char *)(error.c_str())); |
| 114 | + srv.write_line_end(); |
| 115 | + return chAT::CommandStatus::OK; |
| 116 | + } else { |
| 117 | + return chAT::CommandStatus::ERROR; |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + default: |
| 122 | + return chAT::CommandStatus::ERROR; |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + /* ....................................................................... */ |
| 127 | + command_table[_OTA_VERIFY] = [this](auto & srv, auto & parser) { |
| 128 | + /* ....................................................................... */ |
| 129 | + switch (parser.cmd_mode) { |
| 130 | + case chAT::CommandMode::Run: { |
| 131 | + Arduino_ESP32_OTA::Error ota_error = OTA.verify(); |
| 132 | + String error = String((int)ota_error) + "\r\n"; |
| 133 | + srv.write_response_prompt(); |
| 134 | + srv.write_str((const char *)(error.c_str())); |
| 135 | + srv.write_line_end(); |
| 136 | + return chAT::CommandStatus::OK; |
| 137 | + } |
| 138 | + default: |
| 139 | + return chAT::CommandStatus::ERROR; |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + /* ....................................................................... */ |
| 144 | + command_table[_OTA_UPDATE] = [this](auto & srv, auto & parser) { |
| 145 | + /* ....................................................................... */ |
| 146 | + switch (parser.cmd_mode) { |
| 147 | + case chAT::CommandMode::Run: { |
| 148 | + Arduino_ESP32_OTA::Error ota_error = OTA.update(); |
| 149 | + String error = String((int)ota_error) + "\r\n"; |
| 150 | + srv.write_response_prompt(); |
| 151 | + srv.write_str((const char *)(error.c_str())); |
| 152 | + srv.write_line_end(); |
| 153 | + return chAT::CommandStatus::OK; |
| 154 | + } |
| 155 | + case chAT::CommandMode::Write: { |
| 156 | + if (parser.args.size() != 1) { |
| 157 | + return chAT::CommandStatus::ERROR; |
| 158 | + } |
| 159 | + |
| 160 | + auto &path = parser.args[0]; |
| 161 | + if (path.empty()) { |
| 162 | + return chAT::CommandStatus::ERROR; |
| 163 | + } |
| 164 | + |
| 165 | + int flash_error = BOSSA::flashUnoR4WiFi(path.c_str(), Serial, GPIO_BOOT, GPIO_RST); |
| 166 | + String error = String(flash_error) + "\r\n"; |
| 167 | + srv.write_response_prompt(); |
| 168 | + srv.write_str((const char *)(error.c_str())); |
| 169 | + srv.write_line_end(); |
| 170 | + return chAT::CommandStatus::OK; |
| 171 | + } |
| 172 | + default: |
| 173 | + return chAT::CommandStatus::ERROR; |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + /* ....................................................................... */ |
| 178 | + command_table[_OTA_RESET] = [this](auto & srv, auto & parser) { |
| 179 | + /* ....................................................................... */ |
| 180 | + switch (parser.cmd_mode) { |
| 181 | + case chAT::CommandMode::Run: { |
| 182 | + OTA.reset(); |
| 183 | + srv.write_response_prompt(); |
| 184 | + srv.write_str("0"); |
| 185 | + srv.write_line_end(); |
| 186 | + return chAT::CommandStatus::OK; |
| 187 | + } |
| 188 | + default: |
| 189 | + return chAT::CommandStatus::ERROR; |
| 190 | + } |
| 191 | + }; |
| 192 | +} |
| 193 | + |
| 194 | +#endif |
0 commit comments