7880da2ed1ec8abd0bf4b7288dec1141dabc7e36
[src/low-level-can-generator.git] / src / main.cpp
1 /*\r
2  * Copyright (C) 2015, 2016 "IoT.bzh"\r
3  * Author "Loïc Collignon" <loic.collignon@iot.bzh>\r
4  * Author "Romain Forlot" <romain.forlot@iot.bzh>\r
5  *\r
6  * Licensed under the Apache License, Version 2.0 (the "License");\r
7  * you may not use this file except in compliance with the License.\r
8  * You may obtain a copy of the License at\r
9  *\r
10  *       http://www.apache.org/licenses/LICENSE-2.0\r
11  *\r
12  * Unless required by applicable law or agreed to in writing, software\r
13  * distributed under the License is distributed on an "AS IS" BASIS,\r
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15  * See the License for the specific language governing permissions and\r
16  * limitations under the License.\r
17  */\r
18 \r
19 #include <unistd.h>\r
20 #include <stdlib.h>\r
21 #include <libgen.h>\r
22 #include <exception>\r
23 #include <fstream>\r
24 #include <iostream>\r
25 #include <string>\r
26 #include <numeric>\r
27 #include <iterator>\r
28 #include <json.hpp>\r
29 #include "openxc/message_set.hpp"\r
30 \r
31 #define EXIT_SUCCESS                            0\r
32 #define EXIT_UNKNOWN_ERROR                      1\r
33 #define EXIT_COMMAND_LINE_ERROR         2\r
34 #define EXIT_PROGRAM_ERROR                      3\r
35 \r
36 template <typename T>\r
37 struct generator\r
38 {\r
39         T v_;\r
40         std::string line_prefix_;\r
41         generator(T v, std::string line_prefix = "") : v_{v}, line_prefix_{line_prefix} {}\r
42 };\r
43 \r
44 template <>\r
45 struct generator<openxc::signal>\r
46 {\r
47         const openxc::signal& v_;\r
48         std::uint32_t index_;\r
49         std::string line_prefix_;\r
50         generator(const openxc::signal& v, std::uint32_t index, std::string line_prefix = "")\r
51                 : v_{v}, index_{index}, line_prefix_{line_prefix}\r
52         {\r
53         }\r
54 };\r
55 \r
56 template <typename T>\r
57 generator<T> gen(const T& v, std::string line_prefix = "") { return generator<T>(v, line_prefix); }\r
58 \r
59 generator<openxc::signal> gen(const openxc::signal& v, std::uint32_t index, std::string line_prefix = "")\r
60 {\r
61         return generator<openxc::signal>(v, index, line_prefix);\r
62 }\r
63 \r
64 template <typename T>\r
65 std::ostream& operator<<(std::ostream& o, const generator<T>& v)\r
66 {\r
67         o << v.line_prefix_ << v.v_;\r
68         return o;\r
69 }\r
70 \r
71 template <>\r
72 std::ostream& operator<<(std::ostream& o, const generator<bool>& v)\r
73 {\r
74         o << v.line_prefix_ << (v.v_ ? "true" : "false");\r
75         return o;\r
76 }\r
77 \r
78 template <>\r
79 std::ostream& operator<<(std::ostream& o, const generator<float>& v)\r
80 {\r
81         o << v.line_prefix_ << std::showpoint << v.v_ << "f";\r
82         return o;\r
83 }\r
84 \r
85 template <>\r
86 std::ostream& operator<<(std::ostream& o, const generator<std::string>& v)\r
87 {\r
88         o << v.line_prefix_ << '\"' << v.v_ << '\"';\r
89         return o;\r
90 }\r
91 \r
92 template <typename T>\r
93 std::ostream& operator<<(std::ostream& o, const generator<std::vector<T>>& v)\r
94 {\r
95 //      o << v.line_prefix_;\r
96         auto sz = v.v_.size();\r
97         for(const T& i : v.v_)\r
98         {\r
99                 o << gen(i, v.line_prefix_ + '\t');\r
100                 if (sz > 1) o << ",";\r
101                 --sz;\r
102 //              o << '\n';\r
103         }\r
104 //      o << v.line_prefix_;\r
105         return o;\r
106 }\r
107 \r
108 template <>\r
109 std::ostream& operator<<(std::ostream& o, const generator<openxc::message_set>& v)\r
110 {\r
111         o       << v.line_prefix_\r
112                 << "{std::make_shared<can_message_set_t>(can_message_set_t{"\r
113                 << "0,"\r
114                 << gen(v.v_.name()) << ",\n"\r
115                 << "\t\t\t{ // beginning can_message_definition_ vector\n"\r
116                 << gen(v.v_.messages(), "\t\t\t")\r
117                 << "\n\t\t}, // end can_message_definition vector\n"\r
118                 << "\t\t\t{ // beginning diagnostic_messages_ vector\n"\r
119                 << gen(v.v_.diagnostic_messages(),"\t\t\t") << "\n"\r
120                 << "\t\t\t} // end diagnostic_messages_ vector\n"\r
121                 << "\t\t})} // end can_message_set entry\n";\r
122         return o;\r
123 }\r
124 \r
125 template <>\r
126 std::ostream& operator<<(std::ostream& o, const generator<std::map<std::string, std::vector<std::uint32_t>>>& v)\r
127 {\r
128         o << v.line_prefix_ << "{\n";\r
129         std::uint32_t c1 = (uint32_t)v.v_.size();\r
130         for(const auto& state : v.v_)\r
131         {\r
132                 std::uint32_t c2 = (uint32_t)state.second.size();\r
133                 for(const auto& i : state.second)\r
134                 {\r
135                         o << v.line_prefix_ << "\t" << "{" << i << "," << gen(state.first) << "}";\r
136                         if (c1 > 1 || c2 > 1) o << ',';\r
137                         o << '\n';\r
138                         --c2;\r
139                 }\r
140                 --c1;\r
141         }\r
142         o << v.line_prefix_ << "}";\r
143         return o;\r
144 }\r
145 \r
146 template <>\r
147 std::ostream& operator<<(std::ostream& o, const generator<openxc::signal>& v)\r
148 {\r
149         o       << v.line_prefix_ << "{std::make_shared<can_signal_t> (can_signal_t{\n"\r
150                 << v.line_prefix_ << "\t" << gen(v.v_.generic_name()) << ",\n"\r
151                 << v.line_prefix_ << "\t" << v.v_.bit_position() << ",\n"\r
152                 << v.line_prefix_ << "\t" << v.v_.bit_size() << ",\n"\r
153                 << v.line_prefix_ << "\t" << gen(v.v_.factor()) << ",\n"\r
154                 << v.line_prefix_ << "\t" << v.v_.offset() << ",\n"\r
155                 << v.line_prefix_ << "\t" << "0,\n"\r
156                 << v.line_prefix_ << "\t" << "0,\n"\r
157                 << v.line_prefix_ << "\tfrequency_clock_t(" << gen(v.v_.max_frequency()) << "),\n"\r
158                 << v.line_prefix_ << "\t" << gen(v.v_.send_same()) << ",\n"\r
159                 << v.line_prefix_ << "\t" << gen(v.v_.force_send_changed()) << ",\n"\r
160                 << gen(v.v_.states(), v.line_prefix_ + '\t') << ",\n"\r
161                 << v.line_prefix_ << '\t' << gen(v.v_.writable()) << ",\n"\r
162                 << v.line_prefix_ << '\t' << (v.v_.decoder().size() ? v.v_.decoder() : v.v_.states().size() ? "decoder_t::decode_state" : "nullptr") << ",\n"\r
163                 << v.line_prefix_ << '\t' << (v.v_.encoder().size() ? v.v_.encoder() : "nullptr") << ",\n"\r
164                 << v.line_prefix_ << '\t' << "false\n"\r
165                 << v.line_prefix_ << "})}";\r
166         return o;\r
167 }\r
168 \r
169 template <>\r
170 std::ostream& operator<<(std::ostream& o, const generator<openxc::can_message>& v)\r
171 {\r
172         o       << v.line_prefix_\r
173                 << "{std::make_shared<can_message_definition_t>(can_message_definition_t{"\r
174                 << gen(v.v_.bus()) << ","\r
175                 << v.v_.id() << ","\r
176                 << v.v_.is_fd() << ","\r
177                 << "can_message_format_t::STANDARD,"\r
178                 << "frequency_clock_t(" << gen(v.v_.max_frequency()) << "),"\r
179                 << gen(v.v_.force_send_changed()) << ",\n";\r
180                 std::uint32_t index = 0;\r
181         o       << "\t\t\t\t\t{ // beginning can_signals vector\n";\r
182                         std::uint32_t signal_count = (uint32_t)v.v_.signals().size();\r
183                         for(const openxc::signal& s : v.v_.signals())\r
184                         {\r
185         o                       << gen(s, index,"\t\t\t\t\t\t");\r
186                                 if (signal_count > 1) o << ',';\r
187                                 --signal_count;\r
188         o                       << '\n';\r
189                         }\r
190         o       << "\t\t\t\t\t} // end can_signals vector\n"\r
191                 << "\t\t\t\t})} // end can_message_definition entry\n";\r
192         return o;\r
193 }\r
194 \r
195 template <>\r
196 std::ostream& operator<<(std::ostream& o, const generator<openxc::diagnostic_message>& v)\r
197 {\r
198         o       << v.line_prefix_ << "{std::make_shared<diagnostic_message_t>(diagnostic_message_t{\n"\r
199                 << v.line_prefix_ << "\t" << v.v_.pid() << ",\n"\r
200                 << v.line_prefix_ << "\t" << gen(v.v_.name()) << ",\n"\r
201                 << v.line_prefix_ << "\t" << 0 << ",\n"\r
202                 << v.line_prefix_ << "\t" << 0 << ",\n"\r
203                 << v.line_prefix_ << "\t" << "UNIT::INVALID" << ",\n"\r
204                 << v.line_prefix_ << "\t" << gen(v.v_.frequency()) << ",\n"\r
205                 << v.line_prefix_ << "\t" << (v.v_.decoder().size() ? v.v_.decoder() : "nullptr") << ",\n"\r
206                 << v.line_prefix_ << "\t" << (v.v_.callback().size() ? v.v_.callback() : "nullptr") << ",\n"\r
207                 << v.line_prefix_ << "\t" << "true" << ",\n"\r
208                 << v.line_prefix_ << "\t" << "false" << "\n"\r
209                 << v.line_prefix_ << "})}\n";\r
210         return o;\r
211 }\r
212 \r
213 /// @brief Generate the application code.\r
214 /// @param[in] header Content to be inserted as a header.\r
215 /// @param[in] footer Content to be inserted as a footer.\r
216 /// @param[in] message_set application read from the json file.\r
217 /// @param[in] out Stream to write on.\r
218 void generate(const std::string& header, const std::string& footer, const openxc::message_set& message_set, std::ostream& out)\r
219 {\r
220         out << "#include \"application.hpp\"\n"\r
221                 << "#include \"../can/can-decoder.hpp\"\n"\r
222                 << "#include \"../can/can-encoder.hpp\"\n\n";\r
223 \r
224         if (header.size()) out << header << "\n";\r
225 \r
226         out     << "application_t::application_t()\n"\r
227                 << "    : can_bus_manager_{utils::config_parser_t{\"/etc/dev-mapping.conf\"}}\n"\r
228                 << "    , can_message_set_{\n"\r
229                 << gen(message_set, "\t\t")\r
230                 << "\t} // end can_message_set vector\n"\r
231                 << "{\n"\r
232                 << "    for(auto& cms: can_message_set_)\n"\r
233                 << "    {\n"\r
234                 << "            std::vector<std::shared_ptr<can_message_definition_t> >& can_messages_definition = cms->get_can_message_definition();\n"\r
235                 << "            for(auto& cmd : can_messages_definition)\n"\r
236                 << "            {\n"\r
237                 << "                    cmd->set_parent(cms.get());\n"\r
238                 << "                    std::vector<std::shared_ptr<can_signal_t> >& can_signals = cmd->get_can_signals();\n"\r
239                 << "                    for(auto& sig: can_signals)\n"\r
240                 << "                    {\n"\r
241                 << "                            sig->set_parent(cmd.get());\n"\r
242                 << "                    }\n"\r
243                 << "            }\n\n"\r
244                 << "            std::vector<std::shared_ptr<diagnostic_message_t> >& diagnostic_messages = cms->get_diagnostic_messages();\n"\r
245                 << "            for(auto& dm : diagnostic_messages)\n"\r
246                 << "            {\n"\r
247                 << "                    dm->set_parent(cms.get());\n"\r
248                 << "            }\n"\r
249                 << "    }\n"\r
250                 << "            }\n\n"\r
251                 << "const std::string application_t::get_diagnostic_bus() const\n"\r
252                 << "{\n";\r
253 \r
254                 std::string active_bus = "";\r
255                 for (const auto& d : message_set.diagnostic_messages())\r
256                 {\r
257                         if (d.bus().size() == 0) std::cerr << "ERROR: The bus name should be set for each diagnostic message." << std::endl;\r
258                         if (active_bus.size() == 0) active_bus = d.bus();\r
259                         if (active_bus != d.bus()) std::cerr << "ERROR: The bus name should be the same for each diagnostic message." << std::endl;\r
260                 }\r
261 \r
262         out     << "\treturn " << gen(active_bus) << ";\n"\r
263                 << "}\n\n";\r
264         out     << footer << std::endl;\r
265 }\r
266 \r
267 /// @brief Read whole file content to a string.\r
268 /// @param[in] file Path to the file.\r
269 /// @return A std::string which contains the file content. If @c file is an empty string, the return value is also empty.\r
270 /// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.\r
271 std::string read_file(const std::string& file)\r
272 {\r
273         if(file.size() == 0) return std::string();\r
274 \r
275         std::string content;\r
276         std::ifstream stream(file);\r
277         if (stream)\r
278         {\r
279                 stream.seekg(0, std::ios::end);\r
280                 content.reserve(stream.tellg());\r
281                 stream.seekg(0, std::ios::beg);\r
282                 content.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());\r
283                 return content;\r
284         }\r
285         std::stringstream ss;\r
286         ss << "The specified file (" << file << ") is not found!";\r
287         throw std::runtime_error(ss.str());\r
288 }\r
289 \r
290 /// @brief Read whole file content as a json document.\r
291 /// @param[in] file Path to the file.\r
292 /// @return A @c nlohmann::json object.\r
293 /// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.\r
294 nlohmann::json read_json(const std::string& file)\r
295 {\r
296         std::ifstream stream(file);\r
297         if (stream)\r
298         {\r
299                 nlohmann::json result;\r
300                 stream >> result;\r
301                 return result;\r
302         }\r
303         std::stringstream ss;\r
304         ss << "The specified file (" << file << ") is not found!";\r
305         throw std::runtime_error(ss.str());\r
306 }\r
307 \r
308 // function that show the help information\r
309 void showhelpinfo(char *s)\r
310 {\r
311 std::cout<<"Usage:   "<<s<<" <-m inpout.json> [-o application-generated.cpp]"<< std::endl;\r
312 std::cout<<"option:  "<<"-m  input.json : JSON file describing CAN messages and signals"<< std::endl;\r
313 std::cout<<"         "<<"-h header.cpp : header source file insert at the beginning of generated file"<< std::endl;\r
314 std::cout<<"         "<<"-f footer.cpp : footer source file append to generated file."<< std::endl;\r
315 std::cout<<"         "<<"-o application-generated.cpp : output source file. Name has to be application-generated.cpp"<< std::endl;\r
316 }\r
317 \r
318 /// @brief Entry point.\r
319 /// @param[in] argc Argument's count.\r
320 /// @param[in] argv Argument's array.\r
321 /// @return Exit code, zero if success.\r
322 int main(int argc, char *argv[])\r
323 {\r
324         try\r
325         {\r
326                 std::string appName = argv[0];\r
327                 std::string message_set_file;\r
328                 std::string output_file;\r
329                 std::string header_file;\r
330                 std::string footer_file;\r
331 \r
332                 char tmp;\r
333                 /*if the program is ran witout options ,it will show the usgage and exit*/\r
334                 if(argc == 1)\r
335                 {\r
336                         showhelpinfo(argv[0]);\r
337                         exit(1);\r
338                 }\r
339                 /*use function getopt to get the arguments with option."hu:p:s:v" indicate\r
340                 that option h,v are the options without arguments while u,p,s are the\r
341                 options with arguments*/\r
342                 while((tmp=(char)getopt(argc,argv,"m:h:f:o:"))!=-1)\r
343                 {\r
344                         switch(tmp)\r
345                         {\r
346                         case 'h':\r
347                                 header_file = optarg;\r
348                                 break;\r
349                         case 'f':\r
350                                 footer_file = optarg;\r
351                                 break;\r
352                         case 'm':\r
353                                 message_set_file = optarg;\r
354                                 break;\r
355                         case 'o':\r
356                                 output_file = optarg;\r
357                                 break;\r
358                         default:\r
359                                 showhelpinfo(argv[0]);\r
360                         break;\r
361                         }\r
362                 }\r
363 \r
364                 std::stringstream header;\r
365                 header << read_file(header_file);\r
366 \r
367                 std::string footer = read_file(footer_file);\r
368                 openxc::message_set message_set;\r
369                 message_set.from_json(read_json(message_set_file));\r
370 \r
371                 std::string message_set_path = dirname(strdup(message_set_file.c_str()));\r
372                 for(const auto& s : message_set.extra_sources())\r
373                 {\r
374                         std::string extra_source = s;\r
375                         extra_source = message_set_path + "/" + extra_source;\r
376                         header << "\n// >>>>> " << s << " >>>>>\n" << read_file(extra_source) << "\n// <<<<< " << s << " <<<<<\n";\r
377                 }\r
378 \r
379                 std::ofstream out;\r
380                 if (output_file.size())\r
381                 {\r
382                         out.open(output_file);\r
383                         if(!out)\r
384                         {\r
385                                 std::stringstream ss;\r
386                                 ss << "Can't open the ouput file (" << output_file << ") for writing!";\r
387                                 throw std::runtime_error(ss.str());\r
388                         }\r
389                 }\r
390                 generate(header.str(), footer, message_set, output_file.size() ? out : std::cout);\r
391         }\r
392         catch (std::exception& e)\r
393         {\r
394                 std::cerr << "ERROR: Unhandled exception - " << e.what() << std::endl;\r
395                 return EXIT_UNKNOWN_ERROR;\r
396         }\r
397         return EXIT_SUCCESS;\r
398 }\r