aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Collignon <loic.collignon@iot.bzh>2017-03-15 02:36:22 +0100
committerLoïc Collignon <loic.collignon@iot.bzh>2017-03-15 02:36:22 +0100
commitf2487f0b481fae4aa1561e82e8e4aa5b8d0654a0 (patch)
tree7bfc1b140af1e922fc14ba3a02c042b69f24c128 /src
parent00655ea12cc870b84291e462ff791461cc500d68 (diff)
initial commit.
Change-Id: I5e639061c99b7cc6da3da06ccf19bde683a69e9f Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
Diffstat (limited to 'src')
-rwxr-xr-xsrc/main.cpp197
-rwxr-xr-xsrc/openxc/can_bus.cpp89
-rwxr-xr-xsrc/openxc/can_bus.hpp43
-rwxr-xr-xsrc/openxc/can_message.cpp99
-rwxr-xr-xsrc/openxc/can_message.hpp46
-rwxr-xr-xsrc/openxc/command.cpp45
-rwxr-xr-xsrc/openxc/command.hpp25
-rwxr-xr-xsrc/openxc/diagnostic_message.cpp80
-rwxr-xr-xsrc/openxc/diagnostic_message.hpp35
-rwxr-xr-xsrc/openxc/mapping.cpp59
-rwxr-xr-xsrc/openxc/mapping.hpp29
-rwxr-xr-xsrc/openxc/message_set.cpp153
-rwxr-xr-xsrc/openxc/message_set.hpp60
-rwxr-xr-xsrc/openxc/signal.cpp130
-rwxr-xr-xsrc/openxc/signal.hpp50
15 files changed, 1140 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100755
index 00000000..042781c8
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,197 @@
+#include <exception>
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <numeric>
+#include <iterator>
+#include <boost/program_options.hpp>
+#include <boost/filesystem.hpp>
+#include <json.hpp>
+#include "openxc/message_set.hpp"
+
+#define EXIT_SUCCESS 0
+#define EXIT_UNKNOWN_ERROR 1
+#define EXIT_COMMAND_LINE_ERROR 2
+#define EXIT_PROGRAM_ERROR 3
+
+/// @brief Generate the configuration code.
+/// @param[in] header Content to be inserted as a header.
+/// @param[in] footer Content to be inserted as a footer.
+/// @param[in] message_set Configuration read from the json file.
+/// @param[in] out Stream to write on.
+void generate(const std::string& header, const std::string& footer, const openxc::message_set& message_set, std::ostream& out)
+{
+ if (header.size()) out << header << "\n";
+ else out << "#pragma once\n\n";
+
+ out << "namespace generated {\n"
+ << " class configuration_generated\n"
+ << " {\n"
+ << " private:\n"
+ << " can_message_set_t can_message_set_;\n"
+ << " std::vector<std::vector<can_message_definition_t>> can_message_definition_;\n"
+ << " std::vector<std::vector<can_signal_t>> can_signals_;\n"
+ << " std::vector<std::vector<obd2_signal_t>> obd2_signals_;\n"
+ << " public:\n"
+ << " configuration_generated()\n"
+ << " : message_set_{0, \""
+ << message_set.name() << "\", "
+ << message_set.buses().size() << ", "
+ << message_set.messages().size() << ", "
+ << std::accumulate(
+ std::begin(message_set.messages()),
+ std::end(message_set.messages()),
+ 0,
+ [](int sum, const std::map<std::string, openxc::can_message>::value_type& p) { return sum + p.second.signals().size(); }
+ ) << ", "
+ << message_set.commands().size() << ", "
+ << message_set.diagnostic_messages().size() << "}\n"
+ << " , can_message_definition_{\n";
+
+ std::uint32_t count = message_set.messages().size();
+ for(const std::map<std::string, openxc::can_message>::value_type& m : message_set.messages())
+ {
+ out << " {{\"" << m.second.bus() << "\", "
+ << m.first << ", "
+ << "can_message_format_t::STANDARD, frequency_clock_t(), false}}";
+ if (count > 1) out << ",";
+ out << "\n";
+ --count;
+ }
+ out << " }\n";
+
+
+ out << " , can_signals_" << "..." << "\n"
+ << " , obd2_signals_" << "..." << "\n"
+ << " {\n"
+ << " }\n"
+ << " };\n"
+ << "}\n\n";
+
+ out << footer << std::endl;
+}
+
+/// @brief Read whole file content to a string.
+/// @param[in] file Path to the file.
+/// @return A std::string which contains the file content. If @c file is an empty string, the return value is also empty.
+/// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.
+std::string read_file(const std::string& file)
+{
+ if(file.size() == 0) return std::string();
+
+ std::string content;
+ std::ifstream stream(file);
+ if (stream)
+ {
+ stream.seekg(0, std::ios::end);
+ content.reserve(stream.tellg());
+ stream.seekg(0, std::ios::beg);
+ content.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
+ return content;
+ }
+ std::stringstream ss;
+ ss << "The specified file (" << file << ") is not found!";
+ throw std::runtime_error(ss.str());
+}
+
+/// @brief Read whole file content as a json document.
+/// @param[in] file Path to the file.
+/// @return A @c nlohmann::json object.
+/// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.
+nlohmann::json read_json(const std::string& file)
+{
+ std::ifstream stream(file);
+ if (stream)
+ {
+ nlohmann::json result;
+ stream >> result;
+ return result;
+ }
+ std::stringstream ss;
+ ss << "The specified file (" << file << ") is not found!";
+ throw std::runtime_error(ss.str());
+}
+
+/// @brief Entry point.
+/// @param[in] argc Argument's count.
+/// @param[in] argv Argument's array.
+/// @return Exit code, zero if success.
+int main(int argc, char** argv)
+{
+ std::ios::sync_with_stdio(false);
+
+ try
+ {
+ std::string appName = boost::filesystem::basename(argv[0]);
+ std::string message_set_file;
+ std::string output_file;
+ std::string header_file;
+ std::string footer_file;
+
+ namespace bpo = boost::program_options;
+ bpo::options_description desc("Options");
+ desc.add_options()
+ ("help,h", "Display this help.")
+ ("message-set,m", bpo::value<std::string>(&message_set_file)->required(), "The message set definition file.")
+ ("output,o", bpo::value<std::string>(&output_file), "An output file, if not specified stdout is used.")
+ ("header,h", bpo::value<std::string>(&header_file), "A file to copy at the top of the generated output.")
+ ("footer,f", bpo::value<std::string>(&footer_file), "A file to copy at the end of the generated output.");
+
+ bpo::variables_map vm;
+ try
+ {
+ bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
+
+ if (vm.count("help"))
+ {
+ std::cout << desc << std::endl;
+ return EXIT_SUCCESS;
+ }
+
+ bpo::notify(vm);
+
+ std::string header = read_file(header_file);
+ std::string footer = read_file(footer_file);
+ openxc::message_set message_set;
+ message_set.from_json(read_json(message_set_file));
+ std::ofstream out;
+ if (output_file.size())
+ {
+ out = std::ofstream(output_file);
+ if(!out)
+ {
+ std::stringstream ss;
+ ss << "Can't open the ouput file (" << output_file << ") for writing!";
+ throw std::runtime_error(ss.str());
+ }
+ }
+
+ generate(header, footer, message_set, output_file.size() ? out : std::cout);
+
+ }
+ catch (bpo::required_option& e)
+ {
+ std::cerr << "ERROR: Argument required - " << e.what() << std::endl;
+ std::cout << desc << std::endl;
+ return EXIT_COMMAND_LINE_ERROR;
+ }
+ catch (bpo::error& e)
+ {
+ std::cerr << "ERROR: Command line error - " << e.what() << std::endl;
+ std::cout << desc << std::endl;
+ return EXIT_COMMAND_LINE_ERROR;
+ }
+ catch(std::exception& e)
+ {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ return EXIT_PROGRAM_ERROR;
+ }
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "ERROR: Unhandled exception - " << e.what() << std::endl;
+ return EXIT_UNKNOWN_ERROR;
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/src/openxc/can_bus.cpp b/src/openxc/can_bus.cpp
new file mode 100755
index 00000000..545a6c5c
--- /dev/null
+++ b/src/openxc/can_bus.cpp
@@ -0,0 +1,89 @@
+#include "can_bus.hpp"
+
+namespace openxc
+{
+ std::uint32_t can_bus::controller() const
+ {
+ return controller_;
+ }
+
+ std::uint32_t can_bus::speed() const
+ {
+ return speed_;
+ }
+
+ can_bus_mode can_bus::raw_can_mode() const
+ {
+ return raw_can_mode_;
+ }
+
+ bool can_bus::raw_writable() const
+ {
+ return raw_writable_;
+ }
+
+ std::uint32_t can_bus::max_message_frequency() const
+ {
+ return max_message_frequency_;
+ }
+
+ bool can_bus::force_send_changed() const
+ {
+ return force_send_changed_;
+ }
+
+ void can_bus::from_json(const nlohmann::json& j)
+ {
+ controller_ = j.count("controller") ? j["controller"].get<std::uint32_t>() : 1;
+ speed_ = j.count("speed") ? j["speed"].get<std::uint32_t>() : 0;
+ raw_can_mode_ = j.count("raw_can_mode") ? j["raw_can_mode"].get<can_bus_mode>() : can_bus_mode::off;
+ raw_writable_ = j.count("raw_writable") ? j["raw_writable"].get<bool>() : false;
+ max_message_frequency_ = j.count("max_message_frequency") ? j["max_message_frequency"].get<std::uint32_t>() : 0;
+ force_send_changed_ = j.count("force_send_changed") ? j["force_send_changed"].get<bool>() : false;
+ }
+
+ nlohmann::json can_bus::to_json() const
+ {
+ nlohmann::json j;
+ j["controller"] = controller_;
+ j["speed"] = speed_;
+ j["raw_can_mode"] = raw_can_mode_;
+ j["raw_writable"] = raw_writable_;
+ j["max_message_frequency"] = max_message_frequency_;
+ j["force_send_changed"] = force_send_changed_;
+ return j;
+ }
+
+ void to_json(nlohmann::json& j, const can_bus& p)
+ {
+ j = p.to_json();
+ }
+
+ void from_json(const nlohmann::json& j, can_bus& p)
+ {
+ p.from_json(j);
+ }
+
+ void to_json(nlohmann::json& j, const can_bus_mode& p)
+ {
+ switch (p)
+ {
+ case can_bus_mode::off:
+ j = std::string("off");
+ break;
+ case can_bus_mode::filtered:
+ j = std::string("filtered");
+ break;
+ case can_bus_mode::unfiltered:
+ j = std::string("unfiltered");
+ break;
+ }
+ }
+
+ void from_json(const nlohmann::json& j, can_bus_mode& p)
+ {
+ if (j.get<std::string>() == "off") p = can_bus_mode::off;
+ else if (j.get<std::string>() == "filtered") p = can_bus_mode::filtered;
+ else p = can_bus_mode::unfiltered;
+ }
+}
diff --git a/src/openxc/can_bus.hpp b/src/openxc/can_bus.hpp
new file mode 100755
index 00000000..db8e0250
--- /dev/null
+++ b/src/openxc/can_bus.hpp
@@ -0,0 +1,43 @@
+#pragma once
+
+#include <cstdint>
+#include <json.hpp>
+
+namespace openxc
+{
+ enum class can_bus_mode
+ {
+ off,
+ filtered,
+ unfiltered
+ };
+
+ class can_bus
+ {
+ private:
+ std::uint32_t controller_;
+ std::uint32_t speed_;
+ can_bus_mode raw_can_mode_;
+ bool raw_writable_;
+ std::uint32_t max_message_frequency_;
+ bool force_send_changed_;
+
+ public:
+
+ std::uint32_t controller() const;
+ std::uint32_t speed() const;
+ can_bus_mode raw_can_mode() const;
+ bool raw_writable() const;
+ std::uint32_t max_message_frequency() const;
+ bool force_send_changed() const;
+
+ void from_json(const nlohmann::json& j);
+ nlohmann::json to_json() const;
+ };
+
+ void to_json(nlohmann::json& j, const can_bus& p);
+ void from_json(const nlohmann::json& j, can_bus& p);
+
+ void to_json(nlohmann::json& j, const can_bus_mode& p);
+ void from_json(const nlohmann::json& j, can_bus_mode& p);
+}
diff --git a/src/openxc/can_message.cpp b/src/openxc/can_message.cpp
new file mode 100755
index 00000000..d42d1efa
--- /dev/null
+++ b/src/openxc/can_message.cpp
@@ -0,0 +1,99 @@
+#include "can_message.hpp"
+
+namespace openxc
+{
+ std::string can_message::bus() const
+ {
+ return bus_;
+ }
+
+ bool can_message::bit_numbering_inverted() const
+ {
+ return bit_numbering_inverted_;
+ }
+
+ const std::map<std::string, signal>& can_message::signals() const
+ {
+ return signals_;
+ }
+
+ std::string can_message::name() const
+ {
+ return name_;
+ }
+
+ std::vector<std::string> can_message::handlers() const
+ {
+ return handlers_;
+ }
+
+ bool can_message::enabled() const
+ {
+ return enabled_;
+ }
+
+ std::uint32_t can_message::max_frequency() const
+ {
+ return max_frequency_;
+ }
+
+ std::uint32_t can_message::max_signal_frequency() const
+ {
+ return max_signal_frequency_;
+ }
+
+ bool can_message::force_send_changed() const
+ {
+ return force_send_changed_;
+ }
+
+ bool can_message::force_send_changed_signals() const
+ {
+ return force_send_changed_;
+ }
+
+ void can_message::from_json(const nlohmann::json& j)
+ {
+ bus_ = j.count("bus") ? j["bus"].get<std::string>() : "";
+ bit_numbering_inverted_ = j.count("bit_numbering_inverted") ? j["bit_numbering_inverted"].get<bool>() : false;
+ signals_= j.count("signals") ? j["signals"].get<std::map<std::string, signal>>() : std::map<std::string, signal>();
+ name_ = j.count("name") ? j["name"].get<std::string>() : "";
+ handlers_ = j.count("handlers") ? j["handlers"].get<std::vector<std::string>>() : std::vector<std::string>();
+ enabled_ = j.count("enabled") ? j["enabled"].get<bool>() : true;
+ max_frequency_ = j.count("max_frequency") ? j["max_frequency"].get<std::uint32_t>() : 0;
+ max_signal_frequency_ = j.count("max_signal_frequency") ? j["max_signal_frequency"].get<std::uint32_t>() : 0;
+ force_send_changed_ = j.count("force_send_changed") ? j["force_send_changed"].get<bool>() : true;
+ force_send_changed_signals_ = j.count("force_send_changed_signals") ? j["force_send_changed_signals"].get<bool>() : false;
+ }
+
+ std::uint32_t can_message::get_signals_count() const
+ {
+ return signals_.size();
+ }
+
+ nlohmann::json can_message::to_json() const
+ {
+ nlohmann::json j;
+ j["bus"] = bus_;
+ j["bit_numbering_inverted"] = bit_numbering_inverted_;
+ j["signals"] = signals_;
+ j["name"] = name_;
+ j["handlers"] = handlers_;
+ j["enabled"] = enabled_;
+ j["max_frequency"] = max_frequency_;
+ j["max_signal_frequency"] = max_signal_frequency_;
+ j["force_send_changed"] = force_send_changed_;
+ j["force_send_changed_signals"] = force_send_changed_signals_;
+ return j;
+ }
+
+ void to_json(nlohmann::json& j, const can_message& p)
+ {
+ j = p.to_json();
+ }
+
+ void from_json(const nlohmann::json& j, can_message& p)
+ {
+ p.from_json(j);
+ }
+}
diff --git a/src/openxc/can_message.hpp b/src/openxc/can_message.hpp
new file mode 100755
index 00000000..57bd23fd
--- /dev/null
+++ b/src/openxc/can_message.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <string>
+#include <vector>
+#include <map>
+#include <json.hpp>
+
+#include "signal.hpp"
+
+namespace openxc
+{
+ class can_message
+ {
+ private:
+ std::string bus_;
+ bool bit_numbering_inverted_;
+ std::map<std::string, signal> signals_;
+ std::string name_;
+ std::vector<std::string> handlers_;
+ bool enabled_;
+ std::uint32_t max_frequency_;
+ std::uint32_t max_signal_frequency_;
+ bool force_send_changed_;
+ bool force_send_changed_signals_;
+
+ public:
+ std::string bus() const;
+ bool bit_numbering_inverted() const;
+ const std::map<std::string, signal>& signals() const;
+ std::string name() const;
+ std::vector<std::string> handlers() const;
+ bool enabled() const;
+ std::uint32_t max_frequency() const;
+ std::uint32_t max_signal_frequency() const;
+ bool force_send_changed() const;
+ bool force_send_changed_signals() const;
+
+ void from_json(const nlohmann::json& j);
+ nlohmann::json to_json() const;
+
+ std::uint32_t get_signals_count() const;
+ };
+
+ void to_json(nlohmann::json& j, const can_message& p);
+ void from_json(const nlohmann::json& j, can_message& p);
+}
diff --git a/src/openxc/command.cpp b/src/openxc/command.cpp
new file mode 100755
index 00000000..4cc0682f
--- /dev/null
+++ b/src/openxc/command.cpp
@@ -0,0 +1,45 @@
+#include "command.hpp"
+
+namespace openxc
+{
+ std::string command::name() const
+ {
+ return name_;
+ }
+
+ bool command::enabled() const
+ {
+ return enabled_;
+ }
+
+ std::string command::handler() const
+ {
+ return handler_;
+ }
+
+ void command::from_json(const nlohmann::json& j)
+ {
+ name_ = j.count("name") ? j["name"].get<std::string>() : "";
+ enabled_ = j.count("enabled") ? j["enabled"].get<bool>() : true;
+ handler_ = j.count("handler") ? j["handler"].get<std::string>() : "";
+ }
+
+ nlohmann::json command::to_json() const
+ {
+ nlohmann::json j;
+ j["name"] = name_;
+ j["enabled"] = enabled_;
+ j["handler"] = handler_;
+ return j;
+ }
+
+ void to_json(nlohmann::json& j, const command& p)
+ {
+ j = p.to_json();
+ }
+
+ void from_json(const nlohmann::json& j, command& p)
+ {
+ p.from_json(j);
+ }
+}
diff --git a/src/openxc/command.hpp b/src/openxc/command.hpp
new file mode 100755
index 00000000..2c052266
--- /dev/null
+++ b/src/openxc/command.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <string>
+#include <json.hpp>
+
+namespace openxc
+{
+ class command
+ {
+ private:
+ std::string name_;
+ bool enabled_;
+ std::string handler_;
+ public:
+ std::string name() const;
+ bool enabled() const;
+ std::string handler() const;
+
+ void from_json(const nlohmann::json& j);
+ nlohmann::json to_json() const;
+ };
+
+ void to_json(nlohmann::json& j, const command& p);
+ void from_json(const nlohmann::json& j, command& p);
+}
diff --git a/src/openxc/diagnostic_message.cpp b/src/openxc/diagnostic_message.cpp
new file mode 100755
index 00000000..44b21c64
--- /dev/null
+++ b/src/openxc/diagnostic_message.cpp
@@ -0,0 +1,80 @@
+#include "diagnostic_message.hpp"
+
+namespace openxc
+{
+ std::string diagnostic_message::bus() const
+ {
+ return bus_;
+ }
+
+ std::uint32_t diagnostic_message::id() const
+ {
+ return id_;
+ }
+
+ std::uint32_t diagnostic_message::mode() const
+ {
+ return mode_;
+ }
+
+ std::uint32_t diagnostic_message::frequency() const
+ {
+ return frequency_;
+ }
+
+ std::uint32_t diagnostic_message::pid() const
+ {
+ return pid_;
+ }
+
+ std::string diagnostic_message::name() const
+ {
+ return name_;
+ }
+
+ std::string diagnostic_message::decoder() const
+ {
+ return decoder_;
+ }
+
+ std::string diagnostic_message::callback() const
+ {
+ return callback_;
+ }
+
+ void diagnostic_message::from_json(const nlohmann::json& j)
+ {
+ bus_ = j.count("bus") ? j["bus"].get<std::string>() : "";
+ id_ = j.count("id") ? j["id"].get<std::uint32_t>() : 0;
+ mode_ = j.count("mode") ? j["mode"].get<std::uint32_t>() : 1;
+ frequency_ = j.count("frequency") ? j["frequency"].get<std::uint32_t>() : 0;
+ pid_ = j.count("pid") ? j["pid"].get<std::uint32_t>() : 0;
+ name_ = j.count("name") ? j["name"].get<std::string>() : "";
+ decoder_ = j.count("decoder") ? j["decoder"].get<std::string>() : "";
+ callback_ = j.count("callback") ? j["callback"].get<std::string>() : "";
+ }
+
+ nlohmann::json diagnostic_message::to_json() const
+ {
+ nlohmann::json j;
+ j["bus"] = bus_;
+ j["id"] = id_;
+ j["mode"] = mode_;
+ j["frequency"] = frequency_;
+ j["pid"] = pid_;
+ j["name"] = name_;
+ j["decoder"] = decoder_;
+ j["callback"] = callback_;
+ return j;
+ }
+
+ void to_json(nlohmann::json& j, const diagnostic_message& p)
+ {
+ j = p.to_json();
+ }
+
+ void from_json(const nlohmann::json& j, diagnostic_message& p)
+ {
+ p.from_json(j);
+ }
+}
diff --git a/src/openxc/diagnostic_message.hpp b/src/openxc/diagnostic_message.hpp
new file mode 100755
index 00000000..33b41e61
--- /dev/null
+++ b/src/openxc/diagnostic_message.hpp
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <string>
+#include <json.hpp>
+
+namespace openxc
+{
+ class diagnostic_message
+ {
+ private:
+ std::string bus_;
+ std::uint32_t id_;
+ std::uint32_t mode_;
+ std::uint32_t frequency_;
+ std::uint32_t pid_;
+ std::string name_;
+ std::string decoder_;
+ std::string callback_;
+ public:
+ std::string bus() const;
+ std::uint32_t id() const;
+ std::uint32_t mode() const;
+ std::uint32_t frequency() const;
+ std::uint32_t pid() const;
+ std::string name() const;
+ std::string decoder() const;
+ std::string callback() const;
+
+ void from_json(const nlohmann::json& j);
+ nlohmann::json to_json() const;
+ };
+
+ void to_json(nlohmann::json& j, const diagnostic_message& p);
+ void from_json(const nlohmann::json& j, diagnostic_message& p);
+}
diff --git a/src/openxc/mapping.cpp b/src/openxc/mapping.cpp
new file mode 100755
index 00000000..74d5c94e
--- /dev/null
+++ b/src/openxc/mapping.cpp
@@ -0,0 +1,59 @@
+#include "mapping.hpp"
+
+namespace openxc
+{
+ std::string mapping::mapping_name() const
+ {
+ return mapping_name_;
+ }
+
+ std::string mapping::bus() const
+ {
+ return bus_;
+ }
+
+ std::string mapping::database() const
+ {
+ return database_;
+ }
+
+ bool mapping::bit_numbering_inverted() const
+ {
+ return bit_numbering_inverted_;
+ }
+
+ bool mapping::enabled() const
+ {
+ return enabled_;
+ }
+
+ void mapping::from_json(const nlohmann::json& j)
+ {
+ mapping_name_ = j.count("mapping") ? j["mapping"].get<std::string>() : "";
+ bus_ = j.count("bus") ? j["bus"].get<std::string>() : "";
+ database_ = j.count("database") ? j["database"].get<std::string>() : "";
+ bit_numbering_inverted_ = j.count("bit_numbering_inverted") ? j["bit_numbering_inverted"].get<bool>() : false;
+ enabled_ = j.count("enabled") ? j["enabled"].get<bool>() : true;
+ }
+
+ nlohmann::json mapping::to_json() const
+ {
+ nlohmann::json j;
+ j["mapping"] = mapping_name_;
+ j["bus"] = bus_;
+ j["database"] = database_;
+ j["bit_numbering_inverted"] = bit_numbering_inverted_;
+ j["enabled"] = enabled_;
+ return j;
+ }
+
+ void to_json(nlohmann::json& j, const mapping& p)
+ {
+ j = p.to_json();
+ }
+
+ void from_json(const nlohmann::json& j, mapping& p)
+ {
+ p.from_json(j);
+ }
+}
diff --git a/src/openxc/mapping.hpp b/src/openxc/mapping.hpp
new file mode 100755
index 00000000..30895cba
--- /dev/null
+++ b/src/openxc/mapping.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <string>
+#include <json.hpp>
+
+namespace openxc
+{
+ class mapping
+ {
+ private:
+ std::string mapping_name_;
+ std::string bus_;
+ std::string database_;
+ bool bit_numbering_inverted_;
+ bool enabled_;
+ public:
+ std::string mapping_name() const;
+ std::string bus() const;
+ std::string database() const;
+ bool bit_numbering_inverted() const;
+ bool enabled() const;
+
+ void from_json(const nlohmann::json& j);
+ nlohmann::json to_json() const;
+ };
+
+ void to_json(nlohmann::json& j, const mapping& p);
+ void from_json(const nlohmann::json& j, mapping& p);
+}
diff --git a/src/openxc/message_set.cpp b/src/openxc/message_set.cpp
new file mode 100755
index 00000000..7f3d2fbb
--- /dev/null
+++ b/src/openxc/message_set.cpp
@@ -0,0 +1,153 @@
+#include <sstream>
+#include "message_set.hpp"
+
+namespace openxc
+{
+ message_set::message_set()
+ : name_{""}
+ , bit_numbering_inverted_{false}
+ , max_message_frequency_{0}
+ , raw_can_mode_{can_bus_mode::off}
+ , parents_{}
+ , initializers_{}
+ , loopers_{}
+ , buses_{}
+ , messages_{}
+ , diagnostic_messages_{}
+ , mappings_{}
+ , extra_sources_{}
+ , commands_{}
+ {
+ }
+
+ std::string message_set::name() const
+ {
+ return name_;
+ }
+
+ bool message_set::bit_numbering_inverted() const
+ {
+ return bit_numbering_inverted_;
+ }
+
+ std::uint32_t message_set::max_message_frequency() const
+ {
+ return max_message_frequency_;
+ }
+
+ can_bus_mode message_set::raw_can_mode() const
+ {
+ return raw_can_mode_;
+ }
+
+ const std::vector<std::string>& message_set::parents() const
+ {
+ return parents_;
+ }
+
+ const std::vector<std::string>& message_set::initializers() const
+ {
+ return initializers_;
+ }
+
+ const std::vector<std::string>& message_set::loopers() const
+ {
+ return loopers_;
+ }
+
+ const std::map<std::string, can_bus>& message_set::buses() const
+ {
+ return buses_;
+ }
+
+ const std::map<std::string, can_message>& message_set::messages() const
+ {
+ return messages_;
+ }
+
+ const std::vector<diagnostic_message>& message_set::diagnostic_messages() const
+ {
+ return diagnostic_messages_;
+ }
+
+ const std::vector<mapping>& message_set::mappings() const
+ {
+ return mappings_;
+ }
+
+ const std::vector<std::string>& message_set::extra_sources() const
+ {
+ return extra_sources_;
+ }
+
+ const std::vector<command>& message_set::commands() const
+ {
+ return commands_;
+ }
+
+ std::string message_set::to_initializer() const
+ {
+ std::uint32_t signal_count = 0;
+ for(const auto& m : messages_)
+ {
+ signal_count += m.second.get_signals_count();
+ }
+
+ std::stringstream ss;
+ ss << "{0, "
+ << "\"" << name_ << "\", "
+ << buses_.size() << ", "
+ << messages_.size() << ", "
+ << signal_count << ", "
+ << commands_.size() << ", "
+ << diagnostic_messages_.size() << "}";
+ return ss.str();
+ }
+
+ void message_set::from_json(const nlohmann::json& j)
+ {
+ name_ = j["name"].get<std::string>();
+ bit_numbering_inverted_ = j.count("bit_numbering_inverted") ? j["bit_numbering_inverted"].get<bool>() : false; // TODO: should be true by default if database-backed.
+ max_message_frequency_ = j.count("max_message_frequency") ? j["max_message_frequency"].get<std::uint32_t>() : 0;
+ raw_can_mode_ = j.count("raw_can_mode") ? j["raw_can_mode"].get<can_bus_mode>() : can_bus_mode::off;
+ parents_ = j.count("parents") ? j["parents"].get<std::vector<std::string>>() : std::vector<std::string>();
+ initializers_ = j.count("initializers") ? j["initializers"].get<std::vector<std::string>>() : std::vector<std::string>();
+ loopers_ = j.count("loopers") ? j["loopers"].get<std::vector<std::string>>() : std::vector<std::string>();
+ buses_ = j.count("buses") ? j["buses"].get<std::map<std::string, can_bus>>() : std::map<std::string, can_bus>();
+ messages_ = j.count("messages") ? j["messages"].get<std::map<std::string, can_message>>() : std::map<std::string, can_message>();
+ diagnostic_messages_ = j.count("diagnostic_messages") ? j["diagnostic_messages"].get<std::vector<diagnostic_message>>() : std::vector<diagnostic_message>();
+ mappings_ = j.count("mappings") ? j["mappings"].get<std::vector<mapping>>() : std::vector<mapping>();
+ extra_sources_ = j.count("extra_sources") ? j["extra_sources"].get<std::vector<std::string>>() : std::vector<std::string>();
+ commands_ = j.count("commands") ? j["commands"].get<std::vector<command>>() : std::vector<command>();
+ }
+
+
+ nlohmann::json message_set::to_json() const
+ {
+ nlohmann::json j;
+ j["name_"] = name_;
+ j["bit_numbering_inverted"] = bit_numbering_inverted_;
+ j["max_message_frequency"] = max_message_frequency_;
+ j["raw_can_mode"] = raw_can_mode_;
+ j["parents"] = parents_;
+ j["initializers"] = initializers_;
+ j["loopers"] = loopers_;
+ j["buses"] = buses_;
+ j["messages"] = messages_;
+ j["diagnostic_messages"] = diagnostic_messages_;
+ j["mappings"] = mappings_;
+ j["extra_sources"] = extra_sources_;
+ j["commands"] = commands_;
+ return j;
+ }
+
+ void to_json(nlohmann::json& j, const message_set& p)
+ {
+ j = p.to_json();
+ }
+
+ void from_json(const nlohmann::json& j, message_set& p)
+ {
+ p.from_json(j);
+ }
+}
diff --git a/src/openxc/message_set.hpp b/src/openxc/message_set.hpp
new file mode 100755
index 00000000..37b40fc4
--- /dev/null
+++ b/src/openxc/message_set.hpp
@@ -0,0 +1,60 @@
+#pragma once
+
+#include <string>
+#include <vector>
+#include <map>
+#include <json.hpp>
+
+#include "can_bus.hpp"
+#include "can_message.hpp"
+#include "diagnostic_message.hpp"
+#include "mapping.hpp"
+#include "command.hpp"
+
+namespace openxc
+{
+ class message_set
+ {
+ private:
+ std::string name_;
+ bool bit_numbering_inverted_;
+ std::uint32_t max_message_frequency_;
+ can_bus_mode raw_can_mode_;
+ std::vector<std::string> parents_;
+ std::vector<std::string> initializers_;
+ std::vector<std::string> loopers_;
+ std::map<std::string, can_bus> buses_;
+ std::map<std::string, can_message> messages_;
+ std::vector<diagnostic_message> diagnostic_messages_;
+ std::vector<mapping> mappings_;
+ std::vector<std::string> extra_sources_;
+ std::vector<command> commands_;
+
+ public:
+ message_set();
+ message_set(const message_set&) = default;
+ message_set(message_set&&) = default;
+
+ std::string name() const;
+ bool bit_numbering_inverted() const;
+ std::uint32_t max_message_frequency() const;
+ can_bus_mode raw_can_mode() const;
+ const std::vector<std::string>& parents() const;
+ const std::vector<std::string>& initializers() const;
+ const std::vector<std::string>& loopers() const;
+ const std::map<std::string, can_bus>& buses() const;
+ const std::map<std::string, can_message>& messages() const;
+ const std::vector<diagnostic_message>& diagnostic_messages() const;
+ const std::vector<mapping>& mappings() const;
+ const std::vector<std::string>& extra_sources() const;
+ const std::vector<command>& commands() const;
+
+ std::string to_initializer() const;
+
+ void from_json(const nlohmann::json& j);
+ nlohmann::json to_json() const;
+ };
+
+ void to_json(nlohmann::json& j, const message_set& p);
+ void from_json(const nlohmann::json& j, message_set& p);
+}
diff --git a/src/openxc/signal.cpp b/src/openxc/signal.cpp
new file mode 100755
index 00000000..9406112c
--- /dev/null
+++ b/src/openxc/signal.cpp
@@ -0,0 +1,130 @@
+#include "signal.hpp"
+
+namespace openxc
+{
+ std::string signal::generic_name() const
+ {
+ return generic_name_;
+ }
+
+ std::uint32_t signal::bit_position() const
+ {
+ return bit_position_;
+ }
+
+ std::uint32_t signal::bit_size() const
+ {
+ return bit_size_;
+ }
+
+ float signal::factor() const
+ {
+ return factor_;
+ }
+
+ std::uint32_t signal::offset() const
+ {
+ return offset_;
+ }
+
+ std::string signal::decoder() const
+ {
+ return decoder_;
+ }
+
+ bool signal::ignore() const
+ {
+ return ignore_;
+ }
+
+ bool signal::enabled() const
+ {
+ return enabled_;
+ }
+
+ const std::map<std::string, std::vector<std::uint32_t>>& signal::states() const
+ {
+ return states_;
+ }
+
+ std::uint32_t signal::max_frequency() const
+ {
+ return max_frequency_;
+ }
+
+ bool signal::send_same() const
+ {
+ return send_same_;
+ }
+
+ bool signal::force_send_changed() const
+ {
+ return force_send_changed_;
+ }
+
+ bool signal::writable() const
+ {
+ return writable_;
+ }
+
+ std::string signal::encoder() const
+ {
+ return encoder_;
+ }
+
+ void signal::from_json(const nlohmann::json& j)
+ {
+ generic_name_ = j.count("generic_name") ? j["generic_name"].get<std::string>() : "";
+ bit_position_ = j.count("bit_position") ? j["bit_position"].get<std::uint32_t>() : 0;
+ bit_size_ = j.count("bit_size") ? j["bit_size"].get<std::uint32_t>() : 0;
+ factor_ = j.count("factor") ? j["factor"].get<float>() : 1.0f;
+ offset_ = j.count("offset") ? j["offset"].get<std::uint32_t>() : 0;
+ decoder_ = j.count("decoder") ? j["decoder"].get<std::string>() : "";
+ ignore_ = j.count("ignore") ? j["ignore"].get<bool>() : false;
+ enabled_ = j.count("enabled") ? j["enabled"].get<bool>() : true;
+ max_frequency_ = j.count("max_frequency") ? j["max_frequency"].get<std::uint32_t>() : 0;
+ send_same_ = j.count("send_same") ? j["send_same"].get<bool>() : true;
+ force_send_changed_ = j.count("force_send_changed") ? j["force_send_changed"].get<bool>() : false;
+ writable_ = j.count("writable") ? j["writable"].get<bool>() : false;
+ encoder_ = j.count("encoder") ? j["encoder"].get<std::string>() : "";
+
+ if (j.count("states"))
+ {
+ std::map<std::string, nlohmann::json> items = j["states"];
+ for(const auto& i : items)
+ {
+ states_[i.first] = i.second.get<std::vector<std::uint32_t>>();
+ }
+ }
+ }
+
+ nlohmann::json signal::to_json() const
+ {
+ nlohmann::json j;
+ j["generic_name"] = generic_name_;
+ j["bit_position"] = bit_position_;
+ j["bit_size"] = bit_size_;
+ j["factor"] = factor_;
+ j["offset"] = offset_;
+ j["decoder"] = decoder_;
+ j["ignore"] = ignore_;
+ j["enabled"] = enabled_;
+ j["states"] = states_;
+ j["max_frequency"] = max_frequency_;
+ j["send_same"] = send_same_;
+ j["force_send_changed"] = force_send_changed_;
+ j["writable"] = writable_;
+ j["encoder"] = encoder_;
+ return j;
+ }
+
+ void to_json(nlohmann::json& j, const signal& p)
+ {
+ j = p.to_json();
+ }
+
+ void from_json(const nlohmann::json& j, signal& p)
+ {
+ p.from_json(j);
+ }
+}
diff --git a/src/openxc/signal.hpp b/src/openxc/signal.hpp
new file mode 100755
index 00000000..f4b6683b
--- /dev/null
+++ b/src/openxc/signal.hpp
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <string>
+#include <map>
+#include <vector>
+#include <cstdint>
+#include <json.hpp>
+
+namespace openxc
+{
+ class signal
+ {
+ private:
+ std::string generic_name_;
+ std::uint32_t bit_position_;
+ std::uint32_t bit_size_;
+ float factor_;
+ std::uint32_t offset_;
+ std::string decoder_;
+ bool ignore_;
+ bool enabled_;
+ std::map<std::string, std::vector<std::uint32_t>> states_;
+ std::uint32_t max_frequency_;
+ bool send_same_;
+ bool force_send_changed_;
+ bool writable_;
+ std::string encoder_;
+ public:
+ std::string generic_name() const;
+ std::uint32_t bit_position() const;
+ std::uint32_t bit_size() const;
+ float factor() const;
+ std::uint32_t offset() const;
+ std::string decoder() const;
+ bool ignore() const;
+ bool enabled() const;
+ const std::map<std::string, std::vector<std::uint32_t>>& states() const;
+ std::uint32_t max_frequency() const;
+ bool send_same() const;
+ bool force_send_changed() const;
+ bool writable() const;
+ std::string encoder() const;
+
+ void from_json(const nlohmann::json& j);
+ nlohmann::json to_json() const;
+ };
+
+ void to_json(nlohmann::json& j, const signal& p);
+ void from_json(const nlohmann::json& j, signal& p);
+}