wip: generation of a configuration for can_signaling.
[apps/agl-service-can-low-level.git] / src / main.cpp
1 #include <exception>\r
2 #include <fstream>\r
3 #include <iostream>\r
4 #include <string>\r
5 #include <numeric>\r
6 #include <iterator>\r
7 #include <boost/program_options.hpp>\r
8 #include <boost/filesystem.hpp>\r
9 #include <json.hpp>\r
10 #include "openxc/message_set.hpp"\r
11 \r
12 #define EXIT_SUCCESS                            0\r
13 #define EXIT_UNKNOWN_ERROR                      1\r
14 #define EXIT_COMMAND_LINE_ERROR         2\r
15 #define EXIT_PROGRAM_ERROR                      3\r
16 \r
17 template <typename T>\r
18 struct generator\r
19 {\r
20         T v_;\r
21         std::string line_prefix_;\r
22         generator(T v, std::string line_prefix = "") : v_{v}, line_prefix_{line_prefix} {}\r
23 };\r
24 \r
25 template <>\r
26 struct generator<openxc::signal>\r
27 {\r
28         const openxc::signal& v_;\r
29         std::uint32_t index_;\r
30         std::string line_prefix_;\r
31         generator(const openxc::signal& v, std::uint32_t index, std::string line_prefix = "")\r
32                 : v_{v}, index_{index}, line_prefix_{line_prefix}\r
33         {\r
34         }\r
35 };\r
36 \r
37 template <typename T>\r
38 generator<T> gen(const T& v, std::string line_prefix = "") { return generator<T>(v, line_prefix); }\r
39 \r
40 generator<openxc::signal> gen(const openxc::signal& v, std::uint32_t index, std::string line_prefix = "")\r
41 {\r
42         return generator<openxc::signal>(v, index, line_prefix);\r
43 }\r
44 \r
45 template <typename T>\r
46 std::ostream& operator<<(std::ostream& o, const generator<T>& v)\r
47 {\r
48         o << v.line_prefix_ << v.v_;\r
49         return o;\r
50 }\r
51 \r
52 template <>\r
53 std::ostream& operator<<(std::ostream& o, const generator<bool>& v)\r
54 {\r
55         o << v.line_prefix_ << (v.v_ ? "true" : "false");\r
56         return o;\r
57 }\r
58 \r
59 template <>\r
60 std::ostream& operator<<(std::ostream& o, const generator<float>& v)\r
61 {\r
62         o << v.line_prefix_ << std::showpoint << v.v_ << "f";\r
63         return o;\r
64 }\r
65 \r
66 template <>\r
67 std::ostream& operator<<(std::ostream& o, const generator<std::string>& v)\r
68 {\r
69         o << v.line_prefix_ << '\"' << v.v_ << '\"';\r
70         return o;\r
71 }\r
72 \r
73 template <typename T>\r
74 std::ostream& operator<<(std::ostream& o, const generator<std::vector<T>>& v)\r
75 {\r
76         o << v.line_prefix_ << "{\n";\r
77         auto sz = v.v_.size();\r
78         for(const T& i : v.v_)\r
79         {\r
80                 o << gen(i, v.line_prefix_ + '\t');\r
81                 if (sz > 1) o << ",";\r
82                 --sz;\r
83                 o << '\n';\r
84         }\r
85         o << v.line_prefix_ << '}';\r
86         return o;\r
87 }\r
88 \r
89 template <>\r
90 std::ostream& operator<<(std::ostream& o, const generator<openxc::message_set>& v)\r
91 {\r
92         o       << v.line_prefix_\r
93                 << '{'\r
94                 << "0, "\r
95                 << gen(v.v_.name()) << ", "\r
96                 << v.v_.buses().size() << ", "\r
97                 << v.v_.messages().size() << ", "\r
98                 << std::accumulate(\r
99                         std::begin(v.v_.messages()),\r
100                         std::end(v.v_.messages()),\r
101                         0,\r
102                         [](int sum, const openxc::can_message& p) { return sum + p.signals().size(); }\r
103                         ) << ", "\r
104                 << v.v_.commands().size() << ", "\r
105                 << v.v_.diagnostic_messages().size() << "}";\r
106         return o;\r
107 }\r
108 \r
109 template <>\r
110 std::ostream& operator<<(std::ostream& o, const generator<openxc::can_message>& v)\r
111 {\r
112         o       << v.line_prefix_\r
113                 << "can_message_definition_t("\r
114                 << "0, "\r
115                 << gen(v.v_.bus()) << ", "\r
116                 << v.v_.id() << ", "\r
117                 << "can_message_format_t::STANDARD, "\r
118                 << "frequency_clock_t(" << gen(v.v_.max_frequency()) << "), "\r
119                 << gen(v.v_.force_send_changed())\r
120                 << ')';\r
121         return o;\r
122 }\r
123 \r
124 template <>\r
125 std::ostream& operator<<(std::ostream& o, const generator<std::map<std::string, std::vector<std::uint32_t>>>& v)\r
126 {\r
127         o << v.line_prefix_ << "{\n";\r
128         std::uint32_t c1 = v.v_.size();\r
129         for(const auto& state : v.v_)\r
130         {\r
131                 std::uint32_t c2 = state.second.size();\r
132                 for(const auto& i : state.second)\r
133                 {\r
134                         o << v.line_prefix_ << "\t" << "{" << i << ", " << gen(state.first) << "}";\r
135                         if (c1 > 1 || c2 > 1) o << ',';\r
136                         o << '\n';\r
137                         --c2;\r
138                 }\r
139                 --c1;\r
140         }\r
141         o << v.line_prefix_ << "}";\r
142         return o;\r
143 }\r
144 \r
145 template <>\r
146 std::ostream& operator<<(std::ostream& o, const generator<openxc::signal>& v)\r
147 {\r
148         o       << v.line_prefix_ << "{\n"\r
149                 << v.line_prefix_ << "\t0,\n"\r
150                 << v.line_prefix_ << "\t" << v.index_ << ",\n"\r
151                 << v.line_prefix_ << "\t" << gen(v.v_.generic_name()) << ",\n"\r
152                 << v.line_prefix_ << "\t" << v.v_.bit_position() << ",\n"\r
153                 << v.line_prefix_ << "\t" << v.v_.bit_size() << ",\n"\r
154                 << v.line_prefix_ << "\t" << gen(v.v_.factor()) << ", \n"\r
155                 << v.line_prefix_ << "\t" << v.v_.offset() << ", \n"\r
156                 << v.line_prefix_ << "\t" << "0,\n"\r
157                 << v.line_prefix_ << "\t" << "0,\n"\r
158                 << v.line_prefix_ << "\tfrequency_clock_t(" << gen(v.v_.max_frequency()) << "),\n"\r
159                 << v.line_prefix_ << "\t" << gen(v.v_.send_same()) << ",\n"\r
160                 << v.line_prefix_ << "\t" << gen(v.v_.force_send_changed()) << ",\n"\r
161                 << gen(v.v_.states(), v.line_prefix_ + '\t') << ",\n"\r
162                 << v.line_prefix_ << '\t' << gen(v.v_.writable()) << ",\n"\r
163                 << v.line_prefix_ << '\t' << (v.v_.decoder().size() ? v.v_.decoder() : "nullptr") << ",\n"\r
164                 << v.line_prefix_ << '\t' << (v.v_.encoder().size() ? v.v_.encoder() : "nullptr") << ",\n"\r
165                 << v.line_prefix_ << '\t' << "false\n"\r
166                 << v.line_prefix_ << "}";\r
167         return o;\r
168 }\r
169 \r
170 \r
171 /// @brief Generate the configuration code.\r
172 /// @param[in] header Content to be inserted as a header.\r
173 /// @param[in] footer Content to be inserted as a footer.\r
174 /// @param[in] message_set Configuration read from the json file.\r
175 /// @param[in] out Stream to write on.\r
176 void generate(const std::string& header, const std::string& footer, const openxc::message_set& message_set, std::ostream& out)\r
177 {\r
178         out << "#include \"configuration.hpp\"\n"\r
179                 << "#include \"can/can-decoder.hpp\"\n\n";\r
180 \r
181         if (header.size()) out << header << "\n";\r
182 \r
183         out     << "configuration_t::configuration_t()\n"\r
184                 << "    : can_message_set_" << gen(message_set) << '\n'\r
185                 << "    , can_message_definition_\n"\r
186                 << "    {\n"\r
187                                 << gen(message_set.messages(), "\t\t") << '\n'\r
188                 << "    }\n"\r
189                 << "    , can_signals_\n"\r
190                 << "    {\n";\r
191                 std::uint32_t message_count = message_set.messages().size();\r
192                 std::uint32_t index = 0;\r
193                 for(const openxc::can_message& m : message_set.messages())\r
194                 {\r
195                         out << "                {\n";\r
196                         std::uint32_t signal_count = m.signals().size();\r
197                         for(const openxc::signal& s : m.signals())\r
198                         {\r
199                                 out << gen(s, index, "                  ");\r
200                                 if (signal_count > 1) out << ',';\r
201                                 --signal_count;\r
202                                 out << '\n';\r
203                         }\r
204                         out << "                }";\r
205                         if (index + 1 < message_count) out << ',';\r
206                         ++index;\r
207                         out << '\n';\r
208                 }\r
209                 out << "        }\n"\r
210                         << "    //, obd2_signals_{" << "/*...*/" << "}\n"\r
211                         << "{\n"\r
212                         << "}\n\n";\r
213         out << footer << std::endl;\r
214 }\r
215 \r
216 /// @brief Read whole file content to a string.\r
217 /// @param[in] file Path to the file.\r
218 /// @return A std::string which contains the file content. If @c file is an empty string, the return value is also empty.\r
219 /// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.\r
220 std::string read_file(const std::string& file)\r
221 {\r
222         if(file.size() == 0) return std::string();\r
223         \r
224         std::string content;\r
225         std::ifstream stream(file);\r
226         if (stream)\r
227         {\r
228                 stream.seekg(0, std::ios::end);\r
229                 content.reserve(stream.tellg());\r
230                 stream.seekg(0, std::ios::beg);\r
231                 content.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());\r
232                 return content;\r
233         }\r
234         std::stringstream ss;\r
235         ss << "The specified file (" << file << ") is not found!";\r
236         throw std::runtime_error(ss.str());\r
237 }\r
238 \r
239 /// @brief Read whole file content as a json document.\r
240 /// @param[in] file Path to the file.\r
241 /// @return A @c nlohmann::json object.\r
242 /// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.\r
243 nlohmann::json read_json(const std::string& file)\r
244 {\r
245         std::ifstream stream(file);\r
246         if (stream)\r
247         {\r
248                 nlohmann::json result;\r
249                 stream >> result;\r
250                 return result;\r
251         }\r
252         std::stringstream ss;\r
253         ss << "The specified file (" << file << ") is not found!";\r
254         throw std::runtime_error(ss.str());\r
255 }\r
256 \r
257 /// @brief Entry point.\r
258 /// @param[in] argc Argument's count.\r
259 /// @param[in] argv Argument's array.\r
260 /// @return Exit code, zero if success.\r
261 int main(int argc, char** argv)\r
262 {\r
263         //std::ios::sync_with_stdio(false);\r
264         \r
265         try\r
266         {\r
267                 std::string appName = boost::filesystem::basename(argv[0]);\r
268                 std::string message_set_file;\r
269                 std::string output_file;\r
270                 std::string header_file;\r
271                 std::string footer_file;\r
272 \r
273                 namespace bpo = boost::program_options;\r
274                 bpo::options_description desc("Options");\r
275                 desc.add_options()\r
276                         ("help,h", "Display this help.")\r
277                         ("message-set,m", bpo::value<std::string>(&message_set_file)->required(), "The message set definition file.")\r
278                         ("output,o", bpo::value<std::string>(&output_file), "An output file, if not specified stdout is used.")\r
279                         ("header,h", bpo::value<std::string>(&header_file), "A file to copy at the top of the generated output.")\r
280                         ("footer,f", bpo::value<std::string>(&footer_file), "A file to copy at the end of the generated output.");\r
281 \r
282                 bpo::variables_map vm;\r
283                 try\r
284                 {\r
285                         bpo::store(bpo::parse_command_line(argc, argv, desc), vm);\r
286 \r
287                         if (vm.count("help"))\r
288                         {\r
289                                 std::cout << desc << std::endl;\r
290                                 return EXIT_SUCCESS;\r
291                         }\r
292 \r
293                         bpo::notify(vm);\r
294 \r
295                         std::stringstream header;\r
296                         header << read_file(header_file);\r
297                                                 \r
298                         std::string footer = read_file(footer_file);\r
299                         openxc::message_set message_set;\r
300                         message_set.from_json(read_json(message_set_file));\r
301 \r
302                         boost::filesystem::path message_set_path(message_set_file);\r
303                         message_set_path.remove_filename();\r
304                         for(const auto& s : message_set.extra_sources())\r
305                         {\r
306                                 boost::filesystem::path extra_source(s);\r
307                                 if (!extra_source.is_complete()) extra_source = message_set_path / extra_source;\r
308                                 header << "\n// >>>>> " << s << " >>>>>\n" << read_file(extra_source.string()) << "\n// <<<<< " << s << " <<<<<\n";\r
309                         }\r
310 \r
311                         std::ofstream out;\r
312                         if (output_file.size())\r
313                         {\r
314                                 out = std::ofstream(output_file);\r
315                                 if(!out)\r
316                                 {\r
317                                         std::stringstream ss;\r
318                                         ss << "Can't open the ouput file (" << output_file << ") for writing!";\r
319                                         throw std::runtime_error(ss.str());\r
320                                 }\r
321                         }\r
322                         \r
323                         generate(header.str(), footer, message_set, output_file.size() ? out : std::cout); \r
324 \r
325                 }\r
326                 catch (bpo::required_option& e)\r
327                 {\r
328                         std::cerr << "ERROR: Argument required - " << e.what() << std::endl;\r
329                         std::cout << desc << std::endl;\r
330                         return EXIT_COMMAND_LINE_ERROR;\r
331                 }\r
332                 catch (bpo::error& e)\r
333                 {\r
334                         std::cerr << "ERROR: Command line error - " << e.what() << std::endl;\r
335                         std::cout << desc << std::endl;\r
336                         return EXIT_COMMAND_LINE_ERROR;\r
337                 }\r
338                 catch(std::exception& e)\r
339                 {\r
340                         std::cerr << "ERROR: " << e.what() << std::endl;\r
341                         return EXIT_PROGRAM_ERROR;\r
342                 }\r
343         }\r
344         catch (std::exception& e)\r
345         {\r
346                 std::cerr << "ERROR: Unhandled exception - " << e.what() << std::endl;\r
347                 return EXIT_UNKNOWN_ERROR;\r
348         }\r
349 \r
350         return EXIT_SUCCESS;\r
351 }\r