#include "terminal_api_driver.h" #include #include #include #include #include #include #include #include #include "common/nlohmann/json.hpp" #include "api-driver/daemon.h" typedef boost::property_tree::ptree::path_type json_path; api_driver::ApiDriver::ApiDriver() = default; void api_driver::ApiDriver::startDaemon() { if (daemon == nullptr) { daemon = std::make_unique(); BOOST_LOG_TRIVIAL(info) << "api_driver::ApiDriver::startDaemon(): API daemon succes started!"; } } std::string api_driver::buildEscapedString(const std::string& source) { std::string str(source); size_t start_pos = 0; while((start_pos = str.find('\"', start_pos)) != std::string::npos) { str.replace(start_pos, 1, "\\\""); start_pos += 2; } for (start_pos = 0; start_pos < str.size() && (str[start_pos] == ' ' || str[start_pos] == '\n' || str[start_pos] == '\t'); start_pos++) {} size_t end_pos = str.size() - 1; for (; end_pos > start_pos && end_pos != 0 && (str[end_pos] == ' ' || str[end_pos] == '\n' || str[end_pos] == '\t'); end_pos--) {} return "\"" + str.substr(start_pos, end_pos - start_pos + 1) + "\""; } std::tuple translateCoordinates(double abs) { auto deg = static_cast(abs); double min_double = (abs - deg) * 60; auto min = static_cast(min_double); return std::make_tuple(deg, min); } nlohmann::json api_driver::ApiDriver::loadTerminalState() const { if (daemon == nullptr) { return R"({"error": "api daemon not started!"})"; } obj::TerminalState state; daemon->getState(state); obj::TerminalDeviceState devState; daemon->getDeviceState(devState); auto res = state.asJson(); res["device"] = devState.asJson(); return res; } void api_driver::ApiDriver::resetPacketStatistics() const { this->daemon->resetPacketStatistics(); } nlohmann::json api_driver::ApiDriver::loadSettings() const { if (daemon == nullptr) { return R"({"error": "api daemon not started!"})"; } nlohmann::json res = daemon->getSettingsRxTx().asJson(); #ifdef API_OBJECT_QOS_SETTINGS_ENABLE res["qos"] = (daemon->getQosSettings().asJson()); #endif #ifdef API_OBJECT_NETWORK_SETTINGS_ENABLE res["network"] = (daemon->getNetworkSettings().asJson()); #endif return res; } nlohmann::json api_driver::ApiDriver::loadFirmwareVersion() const { if (daemon == nullptr) { return R"({"error": "api daemon not started!"})"; } return daemon->getFirmware().asJson(); } void api_driver::ApiDriver::setRxTxSettings(const nlohmann::json& data) { auto rxtx = daemon->getSettingsRxTx(); rxtx.updateMainSettings(data); std::lock_guard _lapi(this->daemon->cpApiMutex); this->daemon->cp.setDmaDebug("begin_save_config", ""); rxtx.storeMainSettings(this->daemon->cp); this->daemon->cp.setDmaDebug("save_config", ""); rxtx.updateCallback(this->daemon->cp); { daemon->setSettingsRxTx(rxtx); } } #ifdef API_OBJECT_DPDI_SETTINGS_ENABLE void api_driver::ApiDriver::setDpdiSettings(const nlohmann::json& data) { auto rxtx = daemon->getSettingsRxTx(); rxtx.updateDpdiSettings(data); std::lock_guard _lapi(this->daemon->cpApiMutex); this->daemon->cp.setDmaDebug("begin_save_config", ""); rxtx.storeMainSettings(this->daemon->cp); this->daemon->cp.setDmaDebug("save_config", ""); rxtx.updateCallback(this->daemon->cp); { daemon->setSettingsRxTx(rxtx); } } #endif #ifdef API_OBJECT_BUCLNB_SETTINGS_ENABLE void api_driver::ApiDriver::setBucLnbSettings(const nlohmann::json& data) { auto rxtx = daemon->getSettingsRxTx(); rxtx.updateBuclnbSettings(data); std::lock_guard _lapi(this->daemon->cpApiMutex); this->daemon->cp.setDmaDebug("begin_save_config", ""); rxtx.storeBuclnbSettings(this->daemon->cp); this->daemon->cp.setDmaDebug("save_config", ""); rxtx.updateCallback(this->daemon->cp); { daemon->setSettingsRxTx(rxtx); } } #endif #ifdef API_OBJECT_QOS_SETTINGS_ENABLE void api_driver::ApiDriver::setQosSettings(const nlohmann::json& data) { auto qos = daemon->getQosSettings(); qos.updateFromJson(data); std::lock_guard _lapi(this->daemon->cpApiMutex); this->daemon->cp.setDmaDebug("begin_save_config", ""); qos.store(this->daemon->cp); this->daemon->cp.setDmaDebug("save_config", ""); qos.updateCallback(this->daemon->cp); { daemon->setQosSettings(qos); } } #endif #ifdef API_OBJECT_NETWORK_SETTINGS_ENABLE void api_driver::ApiDriver::setNetworkSettings(const nlohmann::json& data) { auto net = daemon->getNetworkSettings(); net.updateFromJson(data); std::lock_guard _lapi(this->daemon->cpApiMutex); this->daemon->cp.setDmaDebug("begin_save_config", ""); net.store(this->daemon->cp); this->daemon->cp.setDmaDebug("save_config", ""); net.updateCallback(this->daemon->cp); { daemon->setNetworkSettings(net); } } #endif void api_driver::ApiDriver::resetDefaultSettings() { daemon->resetDefaultSettings(); } void api_driver::ApiDriver::executeInApi(const std::function& callback) { try { std::lock_guard lock(this->daemon->cpApiMutex); callback(this->daemon->cp); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "ApiDriver::executeInApi(): failed to exec with error: " << e.what(); } } #ifdef MODEM_IS_TDMA std::string api_driver::ApiDriver::getOtaFileLocation() const { obj::TerminalDeviceState ds; daemon->getDeviceState(ds); return ds.fUpgradeImage; } #endif #ifdef MODEM_IS_SCPC nlohmann::json api_driver::ApiDriver::getLoggingStatisticsSettings() { return this->daemon->statsLogs.getSettings(); } void api_driver::ApiDriver::setLoggingStatisticsSettings(const nlohmann::json& data) { this->daemon->statsLogs.setSettings(data); } #endif api_driver::ApiDriver::~ApiDriver() = default;