diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Json.cpp | 102 | ||||
-rw-r--r-- | lib/Json.h | 66 | ||||
-rw-r--r-- | lib/RemoteControl.cpp | 94 | ||||
-rw-r--r-- | lib/RemoteControl.h | 8 |
4 files changed, 189 insertions, 81 deletions
diff --git a/lib/Json.cpp b/lib/Json.cpp new file mode 100644 index 0000000..9bda8c3 --- /dev/null +++ b/lib/Json.cpp @@ -0,0 +1,102 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2023 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + */ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +#include <list> +#include <string> +#include <iostream> +#include <sstream> +#include <iomanip> +#include <string> +#include <algorithm> + +#include "Json.h" + +namespace json { + static std::string escape_json(const std::string &s) { + std::ostringstream o; + for (auto c = s.cbegin(); c != s.cend(); c++) { + switch (*c) { + case '"': o << "\\\""; break; + case '\\': o << "\\\\"; break; + case '\b': o << "\\b"; break; + case '\f': o << "\\f"; break; + case '\n': o << "\\n"; break; + case '\r': o << "\\r"; break; + case '\t': o << "\\t"; break; + default: + if ('\x00' <= *c && *c <= '\x1f') { + o << "\\u" + << std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(*c); + } else { + o << *c; + } + } + } + return o.str(); + } + + std::string map_to_json(const map_t& values) { + std::ostringstream ss; + ss << "{ "; + size_t ix = 0; + for (const auto& element : values) { + if (ix > 0) { + ss << ","; + } + + ss << "\"" << escape_json(element.first) << "\": "; + + const auto& value = element.second.data; + if (std::holds_alternative<std::string>(value)) { + ss << "\"" << escape_json(std::get<std::string>(value)) << "\""; + } + else if (std::holds_alternative<double>(value)) { + ss << std::defaultfloat << std::get<double>(value); + } + else if (std::holds_alternative<ssize_t>(value)) { + ss << std::get<ssize_t>(value); + } + else if (std::holds_alternative<size_t>(value)) { + ss << std::get<size_t>(value); + } + else if (std::holds_alternative<bool>(value)) { + ss << (std::get<bool>(value) ? "true" : "false"); + } + else if (std::holds_alternative<std::nullopt_t>(value)) { + ss << "null"; + } + else if (std::holds_alternative<json::map_t>(value)) { + ss << map_to_json(std::get<json::map_t>(value)); + } + else { + throw std::logic_error("variant alternative not handled"); + } + + ix++; + } + ss << " }"; + + return ss.str(); + } +} diff --git a/lib/Json.h b/lib/Json.h new file mode 100644 index 0000000..26da9a8 --- /dev/null +++ b/lib/Json.h @@ -0,0 +1,66 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2023 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + + This module adds remote-control capability to some of the dabmux/dabmod modules. + */ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#pragma once + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include <list> +#include <memory> +#include <optional> +#include <stdexcept> +#include <string> +#include <unordered_map> +#include <variant> + +namespace json { + + using map_t = std::unordered_map<std::string, struct value_t>; + + struct value_t { + std::variant< + map_t, + std::string, + double, + size_t, + ssize_t, + bool, + std::nullopt_t> data; + + template<class T> + value_t operator=(const T& map) { + value_t v; + v.data = map; + return v; + } + + }; + + std::string map_to_json(const map_t& values); +} diff --git a/lib/RemoteControl.cpp b/lib/RemoteControl.cpp index fd0ea77..b544461 100644 --- a/lib/RemoteControl.cpp +++ b/lib/RemoteControl.cpp @@ -105,71 +105,14 @@ std::list< std::vector<std::string> > RemoteControllers::get_param_list_values(c } -static std::string escape_json(const std::string &s) { - std::ostringstream o; - for (auto c = s.cbegin(); c != s.cend(); c++) { - switch (*c) { - case '"': o << "\\\""; break; - case '\\': o << "\\\\"; break; - case '\b': o << "\\b"; break; - case '\f': o << "\\f"; break; - case '\n': o << "\\n"; break; - case '\r': o << "\\r"; break; - case '\t': o << "\\t"; break; - default: - if ('\x00' <= *c && *c <= '\x1f') { - o << "\\u" - << std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(*c); - } else { - o << *c; - } - } - } - return o.str(); -} - -std::string RemoteControllers::get_params_json(const std::string& name) { - RemoteControllable* controllable = get_controllable_(name); - const auto& values = controllable->get_all_values(); - - std::ostringstream ss; - ss << "{ "; - size_t ix = 0; - for (const auto& element : values) { - if (ix > 0) { - ss << ","; - } - - ss << "\"" << escape_json(element.first) << "\": "; - - const auto& value = element.second; - if (std::holds_alternative<string>(value)) { - ss << "\"" << escape_json(std::get<string>(value)) << "\""; - } - else if (std::holds_alternative<double>(value)) { - ss << std::defaultfloat << std::get<double>(value); - } - else if (std::holds_alternative<ssize_t>(value)) { - ss << std::get<ssize_t>(value); - } - else if (std::holds_alternative<size_t>(value)) { - ss << std::get<size_t>(value); - } - else if (std::holds_alternative<bool>(value)) { - ss << (std::get<bool>(value) ? "true" : "false"); - } - else if (std::holds_alternative<std::nullopt_t>(value)) { - ss << "null"; - } - else { - throw std::logic_error("variant alternative not handled"); - } - ix++; +std::string RemoteControllers::get_showjson() { + json::map_t root; + for (auto &controllable : rcs.controllables) { + root[controllable->get_rc_name()].data = controllable->get_all_values(); } - ss << " }"; - return ss.str(); + return json::map_to_json(root); } std::string RemoteControllers::get_param(const std::string& name, const std::string& param) { @@ -590,6 +533,19 @@ void RemoteControllerZmq::process() repSocket.send(zmsg, (--cohort_size > 0) ? zmq::send_flags::sndmore : zmq::send_flags::none); } } + else if (msg.size() == 1 && command == "showjson") { + try { + std::string json = rcs.get_showjson(); + + zmq::message_t zmsg(json.size()); + memcpy(zmsg.data(), json.data(), json.size()); + + repSocket.send(zmsg, zmq::send_flags::none); + } + catch (const ParameterError &err) { + send_fail_reply(repSocket, err.what()); + } + } else if (msg.size() == 2 && command == "show") { const std::string module((char*) msg[1].data(), msg[1].size()); try { @@ -608,20 +564,6 @@ void RemoteControllerZmq::process() send_fail_reply(repSocket, err.what()); } } - else if (msg.size() == 2 && command == "showjson") { - const std::string module((char*) msg[1].data(), msg[1].size()); - try { - std::string json = rcs.get_params_json(module); - - zmq::message_t zmsg(json.size()); - memcpy(zmsg.data(), json.data(), json.size()); - - repSocket.send(zmsg, zmq::send_flags::none); - } - catch (const ParameterError &err) { - send_fail_reply(repSocket, err.what()); - } - } else if (msg.size() == 3 && command == "get") { const std::string module((char*) msg[1].data(), msg[1].size()); const std::string parameter((char*) msg[2].data(), msg[2].size()); diff --git a/lib/RemoteControl.h b/lib/RemoteControl.h index 4bc3b68..26f30d9 100644 --- a/lib/RemoteControl.h +++ b/lib/RemoteControl.h @@ -48,6 +48,7 @@ #include "Log.h" #include "Socket.h" +#include "Json.h" #define RC_ADD_PARAMETER(p, desc) { \ std::vector<std::string> p; \ @@ -120,10 +121,7 @@ class RemoteControllable { /* Getting a parameter always returns a string. */ virtual const std::string get_parameter(const std::string& parameter) const = 0; - using value_t = std::variant<std::string, double, size_t, ssize_t, bool, std::nullopt_t>; - using map_t = std::unordered_map<std::string, value_t>; - - virtual const map_t get_all_values() const = 0; + virtual const json::map_t get_all_values() const = 0; protected: std::string m_rc_name; @@ -140,7 +138,7 @@ class RemoteControllers { void check_faults(); std::list< std::vector<std::string> > get_param_list_values(const std::string& name); std::string get_param(const std::string& name, const std::string& param); - std::string get_params_json(const std::string& name); + std::string get_showjson(); void set_param( const std::string& name, |