From ccc7766e888050088dc1f1d012bbf716e299ffc2 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Thu, 14 Nov 2024 16:42:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BA=D1=83=D1=87=D0=BA=D0=B0=20=D0=BC=D0=B5?= =?UTF-8?q?=D0=BB=D0=BA=D0=B8=D1=85=20=D1=84=D0=B8=D0=BA=D1=81=D0=BE=D0=B2?= =?UTF-8?q?=20+=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=BA=D0=B8=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BC=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D1=81=D0=B1=D1=80=D0=BE=D1=81=D0=B0=20=D0=BD=D0=B0?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- src/main.cpp | 27 +++++++++++++++++++++++++++ src/terminal_api_driver.cpp | 18 ++++++++++++++++++ src/terminal_api_driver.h | 2 ++ static/main.html | 20 ++++++++++---------- 5 files changed, 58 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index e74b518..cd6780b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ cmake-build-* cert.pem key.pem dh.pem -/do-terminal-update.sh +/web-action diff --git a/src/main.cpp b/src/main.cpp index 066ff9e..9debdc7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,9 @@ namespace ssl = boost::asio::ssl; // from +constexpr const char* REBOOT_COMMAND = "web-action reboot"; +constexpr const char* UPGRADE_COMMAND = "web-action upgrade"; + static std::vector loadFile(const std::string& path) { std::ifstream is(path, std::ios::in | std::ios::binary); @@ -407,6 +410,30 @@ public: } })); + s.resources.emplace_back(std::make_unique("/api/reboot", this->auth, 0, [this](const auto& req, auto& rep) { + if (req.method != "POST") { + http::server::stockReply(http::server::bad_request, rep); + } + rep.status = http::server::ok; + rep.headers.clear(); + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + const std::string result = R"({"status":"ok"})"; + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + system(REBOOT_COMMAND); + })); + s.resources.emplace_back(std::make_unique("/api/resetSettings", this->auth, http::auth::User::SUPERUSER, [this](const auto& req, auto& rep) { + if (req.method != "POST") { + http::server::stockReply(http::server::bad_request, rep); + } + rep.status = http::server::ok; + rep.headers.clear(); + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + const std::string result = R"({"status":"ok"})"; + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + api->resetDefaultSettings(); + system(REBOOT_COMMAND); + })); + s.resources.emplace_back(std::make_unique("/api/firmwareUpdate", this->auth, http::auth::User::UPDATE_FIRMWARE, [this](const auto& req, auto& rep) { if (req.method != "PUT") { http::server::stockReply(http::server::bad_request, rep); diff --git a/src/terminal_api_driver.cpp b/src/terminal_api_driver.cpp index 3742d39..e68c727 100644 --- a/src/terminal_api_driver.cpp +++ b/src/terminal_api_driver.cpp @@ -58,6 +58,12 @@ std::pair splitIpAndMask(const std::string& input) { return std::make_pair(ip, mask_str); } +static inline void rtrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), s.end()); +} + class TerminalNetworkSettings { public: std::string managementIp, managementGateway, mode, dataIp; @@ -103,6 +109,7 @@ private: CP_GetNetwork(sid, "version", &version); CP_GetNetwork(sid, "chip_id", &chip_id); + rtrim(chip_id); CP_GetNetwork(sid, "serial", &sn); CP_GetNetwork(sid, "mac_eth0", &mac0); CP_GetNetwork(sid, "mac_eth1", &mac1); @@ -468,6 +475,13 @@ public: CP_GetDmaDebug(sid, "reset_cnt_rx", &tmp); } + void resetDefaultSettings() { + std::lock_guard lock(this->cpApiMutex); + CP_SetDmaDebug(sid, "begin_save_config", " "); + CP_SetDmaDebug(sid, "default_params", ""); + CP_SetDmaDebug(sid, "save_config", " "); + } + ~TerminalApiDaemon() { try { daemon.interrupt(); @@ -873,6 +887,10 @@ void api_driver::ApiDriver::setDebugSendSettings(boost::property_tree::ptree &pt boost::ignore_unused(pt); } +void api_driver::ApiDriver::resetDefaultSettings() { + daemon->resetDefaultSettings(); +} + bool api_driver::ApiDriver::getIsCinC() const { modulator_settings s{}; daemon->getSettings(&s, nullptr, nullptr, nullptr, nullptr); diff --git a/src/terminal_api_driver.h b/src/terminal_api_driver.h index 4b2c819..60db10a 100644 --- a/src/terminal_api_driver.h +++ b/src/terminal_api_driver.h @@ -65,6 +65,8 @@ namespace api_driver { void setDebugSendSettings(boost::property_tree::ptree & pt); + void resetDefaultSettings(); + ~ApiDriver(); private: diff --git a/static/main.html b/static/main.html index 865e54f..3485f4a 100644 --- a/static/main.html +++ b/static/main.html @@ -167,7 +167,7 @@

Параметры передачи