X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.cpp;h=cfefed84331bcee5ca58bf81692df4520f3c47da;hb=b7591d16c2686214d5d8dcc0739a233f15aee5db;hp=3de298a5a75106138265d82d7008e4cc53d2dfe7;hpb=fbd0fb6d5f38f7a12f3bebb4a7fb14bd3e6bfb7c;p=apps%2Fagl-service-can-low-level.git diff --git a/src/main.cpp b/src/main.cpp old mode 100755 new mode 100644 index 3de298a5..cfefed84 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,30 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Loïc Collignon" + * Author "Romain Forlot" + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include #include #include #include #include #include #include -#include -#include #include #include "openxc/message_set.hpp" @@ -167,6 +186,22 @@ std::ostream& operator<<(std::ostream& o, const generator& v) return o; } +template <> +std::ostream& operator<<(std::ostream& o, const generator& v) +{ + o << v.line_prefix_ << "{\n" + << v.line_prefix_ << "\t" << v.v_.pid() << ",\n" + << v.line_prefix_ << "\t" << gen(v.v_.name()) << ",\n" + << v.line_prefix_ << "\t" << 0 << ",\n" + << v.line_prefix_ << "\t" << 0 << ",\n" + << v.line_prefix_ << "\t" << "UNIT::INVALID" << ",\n" + << v.line_prefix_ << "\t" << gen(v.v_.frequency()) << ",\n" + << v.line_prefix_ << "\t" << (v.v_.decoder().size() ? v.v_.decoder() : "nullptr") << ",\n" + << v.line_prefix_ << "\t" << (v.v_.callback().size() ? v.v_.callback() : "nullptr") << ",\n" + << v.line_prefix_ << "\t" << "true" << "\n" + << v.line_prefix_ << "}"; + return o; +} /// @brief Generate the configuration code. /// @param[in] header Content to be inserted as a header. @@ -181,7 +216,7 @@ void generate(const std::string& header, const std::string& footer, const openxc if (header.size()) out << header << "\n"; out << "configuration_t::configuration_t()\n" - << " : can_message_set_" << gen(message_set) << '\n' + << " : can_message_set_{" << gen(message_set) << "}\n" << " , can_message_definition_\n" << " {\n" << gen(message_set.messages(), "\t\t") << '\n' @@ -207,8 +242,24 @@ void generate(const std::string& header, const std::string& footer, const openxc out << '\n'; } out << " }\n" - << " //, obd2_signals_{" << "/*...*/" << "}\n" + << " , diagnostic_messages_\n" + << " {\n" + << gen(message_set.diagnostic_messages(), " ") << "\n" + << " }\n" << "{\n" + << "}\n\n" + << "const std::string configuration_t::get_diagnostic_bus() const\n" + << "{\n"; + + std::string active_bus = ""; + for (const auto& d : message_set.diagnostic_messages()) + { + if (d.bus().size() == 0) std::cerr << "ERROR: The bus name should be set for each diagnostic message." << std::endl; + if (active_bus.size() == 0) active_bus = d.bus(); + if (active_bus != d.bus()) std::cerr << "ERROR: The bus name should be the same for each diagnostic message." << std::endl; + } + + out << " return " << gen(active_bus) << ";\n" << "}\n\n"; out << footer << std::endl; } @@ -254,98 +305,94 @@ nlohmann::json read_json(const std::string& file) throw std::runtime_error(ss.str()); } +// function that show the help information +void showhelpinfo(char *s) +{ +std::cout<<"Usage: "< [-o configuration-generated.cpp]"<< std::endl; +std::cout<<"option: "<<"-m input.json : JSON file describing CAN messages and signals"<< std::endl; +std::cout<<" "<<"-h header.cpp : header source file insert at the beginning of generated file"<< std::endl; +std::cout<<" "<<"-f footer.cpp : footer source file append to generated file."<< std::endl; +std::cout<<" "<<"-o configuration-generated.cpp : output source file. Name has to be configuration-generated.cpp"<< std::endl; +} + /// @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) +int main(int argc, char *argv[]) { - //std::ios::sync_with_stdio(false); - try { - std::string appName = boost::filesystem::basename(argv[0]); + std::string appName = 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(&message_set_file)->required(), "The message set definition file.") - ("output,o", bpo::value(&output_file), "An output file, if not specified stdout is used.") - ("header,h", bpo::value(&header_file), "A file to copy at the top of the generated output.") - ("footer,f", bpo::value(&footer_file), "A file to copy at the end of the generated output."); - - bpo::variables_map vm; - try + char tmp; + /*if the program is ran witout options ,it will show the usgage and exit*/ + if(argc == 1) { - bpo::store(bpo::parse_command_line(argc, argv, desc), vm); - - if (vm.count("help")) + showhelpinfo(argv[0]); + exit(1); + } + /*use function getopt to get the arguments with option."hu:p:s:v" indicate + that option h,v are the options without arguments while u,p,s are the + options with arguments*/ + while((tmp=getopt(argc,argv,"m:h:f:o:"))!=-1) + { + switch(tmp) { - std::cout << desc << std::endl; - return EXIT_SUCCESS; + case 'h': + header_file = optarg; + break; + case 'f': + footer_file = optarg; + break; + case 'm': + message_set_file = optarg; + break; + case 'o': + output_file = optarg; + break; + default: + showhelpinfo(argv[0]); + break; } + } - bpo::notify(vm); - - std::stringstream header; - 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)); - - boost::filesystem::path message_set_path(message_set_file); - message_set_path.remove_filename(); - for(const auto& s : message_set.extra_sources()) - { - boost::filesystem::path extra_source(s); - if (!extra_source.is_complete()) extra_source = message_set_path / extra_source; - header << "\n// >>>>> " << s << " >>>>>\n" << read_file(extra_source.string()) << "\n// <<<<< " << s << " <<<<<\n"; - } + std::stringstream header; + header << read_file(header_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.str(), footer, message_set, output_file.size() ? out : std::cout); + std::string footer = read_file(footer_file); + openxc::message_set message_set; + message_set.from_json(read_json(message_set_file)); - } - 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::string message_set_path = dirname(strdup(message_set_file.c_str())); + for(const auto& s : message_set.extra_sources()) { - std::cerr << "ERROR: Command line error - " << e.what() << std::endl; - std::cout << desc << std::endl; - return EXIT_COMMAND_LINE_ERROR; + std::string extra_source = s; + extra_source = message_set_path + "/" + extra_source; + header << "\n// >>>>> " << s << " >>>>>\n" << read_file(extra_source) << "\n// <<<<< " << s << " <<<<<\n"; } - catch(std::exception& e) + + std::ofstream out; + if (output_file.size()) { - std::cerr << "ERROR: " << e.what() << std::endl; - return EXIT_PROGRAM_ERROR; + out.open(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.str(), footer, message_set, output_file.size() ? out : std::cout); } catch (std::exception& e) { std::cerr << "ERROR: Unhandled exception - " << e.what() << std::endl; return EXIT_UNKNOWN_ERROR; } - return EXIT_SUCCESS; }