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
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
10 * http://www.apache.org/licenses/LICENSE-2.0
\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
22 #include <exception>
\r
29 #include "openxc/message_set.hpp"
\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
36 template <typename T>
\r
40 std::string line_prefix_;
\r
41 generator(T v, std::string line_prefix = "") : v_{v}, line_prefix_{line_prefix} {}
\r
45 struct generator<openxc::signal>
\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
56 template <typename T>
\r
57 generator<T> gen(const T& v, std::string line_prefix = "") { return generator<T>(v, line_prefix); }
\r
59 generator<openxc::signal> gen(const openxc::signal& v, std::uint32_t index, std::string line_prefix = "")
\r
61 return generator<openxc::signal>(v, index, line_prefix);
\r
64 template <typename T>
\r
65 std::ostream& operator<<(std::ostream& o, const generator<T>& v)
\r
67 o << v.line_prefix_ << v.v_;
\r
72 std::ostream& operator<<(std::ostream& o, const generator<bool>& v)
\r
74 o << v.line_prefix_ << (v.v_ ? "true" : "false");
\r
79 std::ostream& operator<<(std::ostream& o, const generator<float>& v)
\r
81 o << v.line_prefix_ << std::showpoint << v.v_ << "f";
\r
86 std::ostream& operator<<(std::ostream& o, const generator<std::string>& v)
\r
88 o << v.line_prefix_ << '\"' << v.v_ << '\"';
\r
92 template <typename T>
\r
93 std::ostream& operator<<(std::ostream& o, const generator<std::vector<T>>& v)
\r
95 // o << v.line_prefix_;
\r
96 auto sz = v.v_.size();
\r
97 for(const T& i : v.v_)
\r
99 o << gen(i, v.line_prefix_ + '\t');
\r
100 if (sz > 1) o << ",";
\r
104 // o << v.line_prefix_;
\r
109 std::ostream& operator<<(std::ostream& o, const generator<openxc::message_set>& v)
\r
111 o << v.line_prefix_
\r
112 << "{std::make_shared<message_set_t>(message_set_t{"
\r
114 << gen(v.v_.name()) << ",\n"
\r
115 << "\t\t\t{ // beginning message_definition_ vector\n"
\r
116 << gen(v.v_.messages(), "\t\t\t")
\r
117 << "\n\t\t}, // end 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 message_set entry\n";
\r
126 std::ostream& operator<<(std::ostream& o, const generator<std::map<std::string, std::vector<std::uint32_t>>>& v)
\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
132 std::uint32_t c2 = (uint32_t)state.second.size();
\r
133 for(const auto& i : state.second)
\r
135 o << v.line_prefix_ << "\t" << "{" << i << "," << gen(state.first) << "}";
\r
136 if (c1 > 1 || c2 > 1) o << ',';
\r
142 o << v.line_prefix_ << "}";
\r
147 std::ostream& operator<<(std::ostream& o, const generator<openxc::signal>& v)
\r
149 o << v.line_prefix_ << "{std::make_shared<signal_t> (signal_t{\n"
\r
150 << v.line_prefix_ << "\t" << gen(v.v_.generic_name()) << ",// generic_name\n"
\r
151 << v.line_prefix_ << "\t" << v.v_.bit_position() << ",// bit_position\n"
\r
152 << v.line_prefix_ << "\t" << v.v_.bit_size() << ",// bit_size\n"
\r
153 << v.line_prefix_ << "\t" << gen(v.v_.factor()) << ",// factor\n"
\r
154 << v.line_prefix_ << "\t" << v.v_.offset() << ",// offset\n"
\r
155 << v.line_prefix_ << "\t" << "0,// min_value\n"
\r
156 << v.line_prefix_ << "\t" << "0,// max_value\n"
\r
157 << v.line_prefix_ << "\tfrequency_clock_t(" << gen(v.v_.max_frequency()) << "),// frequency\n"
\r
158 << v.line_prefix_ << "\t" << gen(v.v_.send_same()) << ",// send_same\n"
\r
159 << v.line_prefix_ << "\t" << gen(v.v_.force_send_changed()) << ",// force_send_changed\n"
\r
160 << gen(v.v_.states(), v.line_prefix_ + '\t') << ",// states\n"
\r
161 << v.line_prefix_ << '\t' << gen(v.v_.writable()) << ",// writable\n"
\r
162 << v.line_prefix_ << '\t' << (v.v_.decoder().size() ? v.v_.decoder() : v.v_.states().size() ? "decoder_t::decode_state" : "nullptr") << ",// decoder\n"
\r
163 << v.line_prefix_ << '\t' << (v.v_.encoder().size() ? v.v_.encoder() : "nullptr") << ",// encoder\n"
\r
164 << v.line_prefix_ << '\t' << "false,// received\n";
\r
165 std::string multi_first = "";
\r
166 if(v.v_.multiplex().first){
\r
167 multi_first = "true";
\r
169 multi_first = "false";
\r
171 std::string multi = "std::make_pair<bool, int>(" + multi_first + "," + std::to_string(v.v_.multiplex().second) + ")";
\r
172 o << v.line_prefix_ << '\t' << multi << ",// multiplex\n"
\r
173 << v.line_prefix_ << '\t' << v.v_.is_big_endian() << ",// is_big_endian\n"
\r
174 << v.line_prefix_ << '\t' << v.v_.is_signed() << ",// is_signed\n"
\r
175 << v.line_prefix_ << "\t" << gen(v.v_.unit()) << ",// unit\n"
\r
176 << v.line_prefix_ << "})}";
\r
181 std::ostream& operator<<(std::ostream& o, const generator<openxc::can_message>& v)
\r
183 o << v.line_prefix_
\r
184 << "{std::make_shared<message_definition_t>(message_definition_t{"
\r
185 << gen(v.v_.bus()) << ","
\r
186 << v.v_.id() << ","
\r
187 << "\"" << v.v_.name() << "\","
\r
188 << v.v_.length() << ","
\r
189 << gen(v.v_.is_fd()) << ",";
\r
190 if(v.v_.is_j1939()){
\r
191 o << "message_format_t::J1939,";
\r
193 else if(v.v_.is_extended())
\r
195 o << "message_format_t::EXTENDED,";
\r
198 o << "message_format_t::STANDARD,";
\r
200 o << "frequency_clock_t(" << gen(v.v_.max_frequency()) << "),"
\r
201 << gen(v.v_.force_send_changed()) << ",";
\r
202 std::uint32_t index = 0;
\r
203 o << "\t\t\t\t\t{ // beginning signals vector\n";
\r
204 std::uint32_t signal_count = (uint32_t)v.v_.signals().size();
\r
205 for(const openxc::signal& s : v.v_.signals())
\r
207 o << gen(s, index,"\t\t\t\t\t\t");
\r
208 if (signal_count > 1) o << ',';
\r
212 o << "\t\t\t\t\t} // end signals vector\n"
\r
213 << "\t\t\t\t})} // end message_definition entry\n";
\r
218 std::ostream& operator<<(std::ostream& o, const generator<openxc::diagnostic_message>& v)
\r
220 o << v.line_prefix_ << "{std::make_shared<diagnostic_message_t>(diagnostic_message_t{\n"
\r
221 << v.line_prefix_ << "\t" << v.v_.pid() << ",\n"
\r
222 << v.line_prefix_ << "\t" << gen(v.v_.name()) << ",\n"
\r
223 << v.line_prefix_ << "\t" << 0 << ",\n"
\r
224 << v.line_prefix_ << "\t" << 0 << ",\n"
\r
225 << v.line_prefix_ << "\t" << "UNIT::INVALID" << ",\n"
\r
226 << v.line_prefix_ << "\t" << gen(v.v_.frequency()) << ",\n"
\r
227 << v.line_prefix_ << "\t" << (v.v_.decoder().size() ? v.v_.decoder() : "nullptr") << ",\n"
\r
228 << v.line_prefix_ << "\t" << (v.v_.callback().size() ? v.v_.callback() : "nullptr") << ",\n"
\r
229 << v.line_prefix_ << "\t" << "true" << ",\n"
\r
230 << v.line_prefix_ << "\t" << "false" << "\n"
\r
231 << v.line_prefix_ << "})}\n";
\r
235 /// @brief Generate the application code.
\r
236 /// @param[in] header Content to be inserted as a header.
\r
237 /// @param[in] footer Content to be inserted as a footer.
\r
238 /// @param[in] message_set application read from the json file.
\r
239 /// @param[in] out Stream to write on.
\r
240 void generate(const std::string& header, const std::string& footer, const openxc::message_set& message_set, std::ostream& out)
\r
242 out << "#include \"application.hpp\"\n"
\r
243 << "#include \"../can/can-decoder.hpp\"\n"
\r
244 << "#include \"../can/can-encoder.hpp\"\n\n";
\r
246 if (header.size()) out << header << "\n";
\r
248 out << "application_t::application_t()\n"
\r
249 << " : can_bus_manager_{utils::config_parser_t{\"/etc/dev-mapping.conf\"}}\n"
\r
250 << " , message_set_{\n"
\r
251 << gen(message_set, "\t\t")
\r
252 << "\t} // end message_set vector\n"
\r
254 << " for(std::shared_ptr<message_set_t> cms: message_set_)\n"
\r
256 << " std::vector<std::shared_ptr<message_definition_t>> messages_definition = cms->get_messages_definition();\n"
\r
257 << " for(std::shared_ptr<message_definition_t> cmd : messages_definition)\n"
\r
259 << " cmd->set_parent(cms);\n"
\r
260 << " std::vector<std::shared_ptr<signal_t>> signals = cmd->get_signals();\n"
\r
261 << " for(std::shared_ptr<signal_t> sig: signals)\n"
\r
263 << " sig->set_parent(cmd);\n"
\r
266 << " std::vector<std::shared_ptr<diagnostic_message_t>> diagnostic_messages = cms->get_diagnostic_messages();\n"
\r
267 << " for(std::shared_ptr<diagnostic_message_t> dm : diagnostic_messages)\n"
\r
269 << " dm->set_parent(cms);\n"
\r
273 << "const std::string application_t::get_diagnostic_bus() const\n"
\r
276 std::string active_bus = "";
\r
277 for (const auto& d : message_set.diagnostic_messages())
\r
279 if (d.bus().size() == 0) std::cerr << "ERROR: The bus name should be set for each diagnostic message." << std::endl;
\r
280 if (active_bus.size() == 0) active_bus = d.bus();
\r
281 if (active_bus != d.bus()) std::cerr << "ERROR: The bus name should be the same for each diagnostic message." << std::endl;
\r
284 out << "\treturn " << gen(active_bus) << ";\n"
\r
286 out << footer << std::endl;
\r
289 /// @brief Read whole file content to a string.
\r
290 /// @param[in] file Path to the file.
\r
291 /// @return A std::string which contains the file content. If @c file is an empty string, the return value is also empty.
\r
292 /// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.
\r
293 std::string read_file(const std::string& file)
\r
295 if(file.size() == 0) return std::string();
\r
297 std::string content;
\r
298 std::ifstream stream(file);
\r
301 stream.seekg(0, std::ios::end);
\r
302 content.reserve(stream.tellg());
\r
303 stream.seekg(0, std::ios::beg);
\r
304 content.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
\r
307 std::stringstream ss;
\r
308 ss << "The specified file (" << file << ") is not found!";
\r
309 throw std::runtime_error(ss.str());
\r
312 /// @brief Read whole file content as a json document.
\r
313 /// @param[in] file Path to the file.
\r
314 /// @return A @c nlohmann::json object.
\r
315 /// @exception std::runtime_error Throw this exception if the specified file is not found or cannot be opened.
\r
316 nlohmann::json read_json(const std::string& file)
\r
318 std::ifstream stream(file);
\r
321 nlohmann::json result;
\r
325 std::stringstream ss;
\r
326 ss << "The specified file (" << file << ") is not found!";
\r
327 throw std::runtime_error(ss.str());
\r
330 // function that show the help information
\r
331 void showhelpinfo(char *s)
\r
333 std::cout<<"Usage: "<<s<<" <-m inpout.json> [-o application-generated.cpp]"<< std::endl;
\r
334 std::cout<<"option: "<<"-m input.json : JSON file describing CAN messages and signals"<< std::endl;
\r
335 std::cout<<" "<<"-h header.cpp : header source file insert at the beginning of generated file"<< std::endl;
\r
336 std::cout<<" "<<"-f footer.cpp : footer source file append to generated file."<< std::endl;
\r
337 std::cout<<" "<<"-o application-generated.cpp : output source file. Name has to be application-generated.cpp"<< std::endl;
\r
340 /// @brief Entry point.
\r
341 /// @param[in] argc Argument's count.
\r
342 /// @param[in] argv Argument's array.
\r
343 /// @return Exit code, zero if success.
\r
344 int main(int argc, char *argv[])
\r
348 std::string appName = argv[0];
\r
349 std::string message_set_file;
\r
350 std::string output_file;
\r
351 std::string header_file;
\r
352 std::string footer_file;
\r
355 /*if the program is ran witout options ,it will show the usgage and exit*/
\r
358 showhelpinfo(argv[0]);
\r
361 /*use function getopt to get the arguments with option."hu:p:s:v" indicate
\r
362 that option h,v are the options without arguments while u,p,s are the
\r
363 options with arguments*/
\r
364 while((tmp=(char)getopt(argc,argv,"m:h:f:o:"))!=-1)
\r
369 header_file = optarg;
\r
372 footer_file = optarg;
\r
375 message_set_file = optarg;
\r
378 output_file = optarg;
\r
381 showhelpinfo(argv[0]);
\r
386 std::stringstream header;
\r
387 header << read_file(header_file);
\r
389 std::string footer = read_file(footer_file);
\r
390 openxc::message_set message_set;
\r
391 message_set.from_json(read_json(message_set_file));
\r
393 std::string message_set_path = dirname(strdup(message_set_file.c_str()));
\r
394 for(const auto& s : message_set.extra_sources())
\r
396 std::string extra_source = s;
\r
397 extra_source = message_set_path + "/" + extra_source;
\r
398 header << "\n// >>>>> " << s << " >>>>>\n" << read_file(extra_source) << "\n// <<<<< " << s << " <<<<<\n";
\r
402 if (output_file.size())
\r
404 out.open(output_file);
\r
407 std::stringstream ss;
\r
408 ss << "Can't open the ouput file (" << output_file << ") for writing!";
\r
409 throw std::runtime_error(ss.str());
\r
412 generate(header.str(), footer, message_set, output_file.size() ? out : std::cout);
\r
414 catch (std::exception& e)
\r
416 std::cerr << "ERROR: Unhandled exception - " << e.what() << std::endl;
\r
417 return EXIT_UNKNOWN_ERROR;
\r
419 return EXIT_SUCCESS;
\r