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