From 44d7237fde80af222939445055a94a0e50e82935 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Tue, 18 Apr 2017 20:23:14 +0200 Subject: Use a system INI configuration file to get devices mapping Instead of specifying a JSON configuration file with CAN devices name, it uses a mapping configuration file that map a high level device names to a real low level names. File path is to be specified into the generated source code which is /etc/dev-mapping.conf for now. Configuration file uses INI file format and is parsed using inih library cpp wrapper. Change-Id: Ibde104e76cd78a6cc86f6eec4f66c274b7567d43 Signed-off-by: Romain Forlot --- .gitmodules | 3 + CAN-binder/libs/inih | 1 + CAN-binder/low-can-binding/CMakeLists.txt | 5 +- CAN-binder/low-can-binding/can/can-bus.cpp | 100 ++++++--------------- CAN-binder/low-can-binding/can/can-bus.hpp | 5 +- .../low-can-binding/configuration-generated.cpp | 5 +- CAN-binder/low-can-binding/configuration.hpp | 5 +- CAN-binder/low-can-binding/utils/config-parser.cpp | 87 +++++++++--------- CAN-binder/low-can-binding/utils/config-parser.hpp | 14 +-- 9 files changed, 88 insertions(+), 137 deletions(-) create mode 160000 CAN-binder/libs/inih diff --git a/.gitmodules b/.gitmodules index 8441eeac..6984efe8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "low-can-binding/libs/isotp-c"] path = CAN-binder/libs/isotp-c url = https://github.com/openxc/isotp-c +[submodule "CAN-binder/libs/inih"] + path = CAN-binder/libs/inih + url = git@github.com:claneys/inih.git diff --git a/CAN-binder/libs/inih b/CAN-binder/libs/inih new file mode 160000 index 00000000..3706abf1 --- /dev/null +++ b/CAN-binder/libs/inih @@ -0,0 +1 @@ +Subproject commit 3706abf10c66599695a3f08e460774ffa7f40ef9 diff --git a/CAN-binder/low-can-binding/CMakeLists.txt b/CAN-binder/low-can-binding/CMakeLists.txt index a1768abb..7359ac7f 100644 --- a/CAN-binder/low-can-binding/CMakeLists.txt +++ b/CAN-binder/low-can-binding/CMakeLists.txt @@ -24,7 +24,7 @@ PROJECT_TARGET_ADD(low-can-binding) add_library(${TARGET_NAME} MODULE ${TARGET_NAME}.cpp configuration.cpp configuration-generated.cpp can/can-bus.cpp can/can-bus-dev.cpp can/can-message-set.cpp can/can-message-definition.cpp can/can-message.cpp can/can-signals.cpp can/can-decoder.cpp diagnostic/diagnostic-message.cpp diagnostic/diagnostic-manager.cpp diagnostic/active-diagnostic-request.cpp - utils/signals.cpp utils/openxc-utils.cpp utils/timer.cpp utils/socket.cpp) + utils/signals.cpp utils/openxc-utils.cpp utils/timer.cpp utils/socket.cpp utils/config-parser.cpp) # Binder exposes a unique public entry point SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES @@ -40,6 +40,7 @@ PROJECT_TARGET_ADD(low-can-binding) uds-c isotp-c bitfield-c + inih ${link_libraries} ) @@ -47,4 +48,4 @@ PROJECT_TARGET_ADD(low-can-binding) INSTALL(TARGETS ${TARGET_NAME} LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) - build_widget("BINDING") + build_widget("BINDING") \ No newline at end of file diff --git a/CAN-binder/low-can-binding/can/can-bus.cpp b/CAN-binder/low-can-binding/can/can-bus.cpp index 33f58fd9..8befec6a 100644 --- a/CAN-binder/low-can-binding/can/can-bus.cpp +++ b/CAN-binder/low-can-binding/can/can-bus.cpp @@ -43,10 +43,9 @@ extern "C" /// @brief Class constructor /// /// @param[in] conf_file - handle to the json configuration file. -can_bus_t::can_bus_t(int conf_file) +can_bus_t::can_bus_t(utils::config_parser_t conf_file) : conf_file_{conf_file} -{ -} +{} std::map> can_bus_t::can_devices_; @@ -231,82 +230,37 @@ int can_bus_t::init_can_dev() int i = 0; size_t t; - devices_name = read_conf(); - - if (! devices_name.empty()) + if(conf_file_.check_conf()) { - t = devices_name.size(); - - for(const auto& device : devices_name) + devices_name = conf_file_.get_devices_name(); + if (! devices_name.empty()) { - can_bus_t::can_devices_[device] = std::make_shared(device, i); - if (can_bus_t::can_devices_[device]->open() == 0) - { - DEBUG(binder_interface, "Start reading thread"); - NOTICE(binder_interface, "%s device opened and reading", device.c_str()); - can_bus_t::can_devices_[device]->start_reading(*this); - i++; - } - else + t = devices_name.size(); + + for(const auto& device : devices_name) { - ERROR(binder_interface, "Can't open device %s", device.c_str()); - return 1; + can_bus_t::can_devices_[device] = std::make_shared(device, i); + if (can_bus_t::can_devices_[device]->open() == 0) + { + DEBUG(binder_interface, "Start reading thread"); + NOTICE(binder_interface, "%s device opened and reading", device.c_str()); + can_bus_t::can_devices_[device]->start_reading(*this); + i++; + } + else + { + ERROR(binder_interface, "Can't open device %s", device.c_str()); + return 1; + } } + NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, (int)t); + return 0; } - - NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, (int)t); - return 0; - } - ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read from configuration file. Did you specify canbus JSON object ?"); - return 1; -} - -/// @brief read the conf_file_ and will parse json objects -/// in it searching for canbus objects devices name. -/// -/// @return Vector of can bus device name string. -std::vector can_bus_t::read_conf() -{ - std::vector ret; - json_object *jo, *canbus; - int n, i; - const char* taxi; - - FILE *fd = fdopen(conf_file_, "r"); - if (fd) - { - std::string fd_conf_content; - std::fseek(fd, 0, SEEK_END); - fd_conf_content.resize(std::ftell(fd)); - std::rewind(fd); - std::fread(&fd_conf_content[0], 1, fd_conf_content.size(), fd); - std::fclose(fd); - - DEBUG(binder_interface, "Configuration file content : %s", fd_conf_content.c_str()); - jo = json_tokener_parse(fd_conf_content.c_str()); - - if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus)) - { - ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it."); - ret.clear(); - } - else if (json_object_get_type(canbus) != json_type_array) - { - taxi = json_object_get_string(canbus); - DEBUG(binder_interface, "Can bus found: %s", taxi); - ret.push_back(std::string(taxi)); - } - else - { - n = json_object_array_length(canbus); - for (i = 0 ; i < n ; i++) - ret.push_back(json_object_get_string(json_object_array_get_idx(canbus, i))); - } - return ret; + ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read from configuration file"); + return 1; } - ERROR(binder_interface, "Problem at reading the conf file"); - ret.clear(); - return ret; + ERROR(binder_interface, "init_can_dev: Can't read INI configuration file"); + return 2; } /// @brief return new_can_message_cv_ member diff --git a/CAN-binder/low-can-binding/can/can-bus.hpp b/CAN-binder/low-can-binding/can/can-bus.hpp index eb474769..5715c748 100644 --- a/CAN-binder/low-can-binding/can/can-bus.hpp +++ b/CAN-binder/low-can-binding/can/can-bus.hpp @@ -27,6 +27,7 @@ #include "openxc.pb.h" #include "can-message.hpp" #include "can-bus-dev.hpp" +#include "../utils/config-parser.hpp" #include "../diagnostic/active-diagnostic-request.hpp" #include "../low-can-binding.hpp" @@ -48,7 +49,7 @@ class can_bus_t { private: - int conf_file_; ///< configuration file handle used to initialize can_bus_dev_t objects. + utils::config_parser_t& conf_file_; ///< configuration file handle used to initialize can_bus_dev_t objects. void can_decode_message(); std::thread th_decoding_; ///< thread that'll handle decoding a can frame @@ -69,7 +70,7 @@ private: static std::map> can_devices_; ///< Can device map containing all can_bus_dev_t objects initialized during init_can_dev function public: - can_bus_t(int conf_file); + can_bus_t(utils::config_parser_t conf_file); can_bus_t(can_bus_t&&); int init_can_dev(); diff --git a/CAN-binder/low-can-binding/configuration-generated.cpp b/CAN-binder/low-can-binding/configuration-generated.cpp index 2e965703..98227cb5 100644 --- a/CAN-binder/low-can-binding/configuration-generated.cpp +++ b/CAN-binder/low-can-binding/configuration-generated.cpp @@ -2,7 +2,8 @@ #include "can/can-decoder.hpp" configuration_t::configuration_t() - : can_message_set_{{0, "example", 0, 1, 5, 0, 20}} + : can_bus_manager_(utils::config_parser_t("/etc/dev-mapping.conf")) + , can_message_set_{{0, "example", 0, 1, 5, 0, 20}} , can_message_definition_ { { @@ -346,5 +347,3 @@ const std::string configuration_t::get_diagnostic_bus() const { return "can0"; } - - diff --git a/CAN-binder/low-can-binding/configuration.hpp b/CAN-binder/low-can-binding/configuration.hpp index 017b148d..535ab8de 100644 --- a/CAN-binder/low-can-binding/configuration.hpp +++ b/CAN-binder/low-can-binding/configuration.hpp @@ -43,10 +43,7 @@ class configuration_t { private: - can_bus_t can_bus_manager_ = can_bus_t( - afb_daemon_rootdir_open_locale( - binder_interface->daemon, "etc/can_buses.json", O_RDONLY, NULL)); ///< instanciate the CAN bus - ///< This will read the configuration file and initialize all CAN devices specified in it. + can_bus_t can_bus_manager_; ///< instanciate the CAN bus manager. It's the one in charge of initialize the CAN bus devices. diagnostic_manager_t diagnostic_manager_; ///< Diagnostic manager use to manage diagnostic message communication. uint8_t active_message_set_ = 0; ///< Which is the active message set ? Default to 0. diff --git a/CAN-binder/low-can-binding/utils/config-parser.cpp b/CAN-binder/low-can-binding/utils/config-parser.cpp index e73b0f94..cb344be6 100644 --- a/CAN-binder/low-can-binding/utils/config-parser.cpp +++ b/CAN-binder/low-can-binding/utils/config-parser.cpp @@ -15,57 +15,37 @@ * limitations under the License. */ +#include "config-parser.hpp" + +#include "../low-can-binding.hpp" + namespace utils { + /// @brief constructor using a POSIX file handle as input. + /// + /// @param conf_file - a POSIX file handle to the INI configuration file config_parser_t::config_parser_t(int conf_file) - : conf_file_{conf_file}, devices_name{} + : config_content_(fdopen(conf_file, "r")) + { + ::close(conf_file); + } + + /// @brief constructor using path to file + config_parser_t::config_parser_t(std::string conf_file) + : config_content_{INIReader(conf_file)} {} - /// @brief read the conf_file_ and will parse json objects - /// in it searching for canbus objects devices name. - /// - /// @return Vector of can bus device name string. - void can_bus_t::read_conf() + /// @brief read the conf_file_ and parse it into an INIReader object + /// to search into later. + bool config_parser_t::check_conf() { - FILE *fd = fdopen(conf_file_, "r"); - if (fd) + if (config_content_.ParseError() < 0) { - std::fseek(fd, 0, SEEK_END); - config_content_.resize(std::ftell(fd)); - std::rewind(fd); - std::fread(&config_content_[0], 1, config_content_.size(), fd); - std::fclose(fd); - - DEBUG(binder_interface, "Configuration file content : %s", config_content_.c_str()); + ERROR(binder_interface, "read_conf: Can't load the INI config file."); + return false; } - ERROR(binder_interface, "Problem at reading the conf file"); - } - - void parse_devices_name() - { - json_object *jo, *canbus; - const char* taxi; - - jo = json_tokener_parse(config_content_.c_str()); - - if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus)) - { - ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it."); - devices_name_.clear(); - } - else if (json_object_get_type(canbus) != json_type_array) - { - taxi = json_object_get_string(canbus); - DEBUG(binder_interface, "Can bus found: %s", taxi); - devices_name_.push_back(std::string(taxi)); - } - else - { - int n, i; - n = json_object_array_length(canbus); - for (i = 0 ; i < n ; i++) - devices_name_.push_back(json_object_get_string(json_object_array_get_idx(canbus, i))); - } + DEBUG(binder_interface, "read_conf: Configuration file parsed"); + return true; } /// @brief Public method to access devices_name_ vector. If vector size equal 0 @@ -74,11 +54,24 @@ namespace utils /// have to test the returned value. /// /// @return A const vector with string of linux CAN devices. - const std::vector& get_devices_name() + const std::vector config_parser_t::get_devices_name() { - if(devices_name_.empty()) - parse_devices_name(); + std::vector devices_name; + + std::set sections = config_content_.GetSections(); + for(const auto& sectionsIt : sections) + { + if(sectionsIt == "CANbus-mapping") + { + std::set fields = config_content_.GetFields(sectionsIt); + for(const auto& fieldsIt : fields) + { + std::string val = config_content_.Get(sectionsIt, fieldsIt, "INVALID"); + devices_name.push_back(val); + } + } + } - return devices_name_; + return devices_name; } } \ No newline at end of file diff --git a/CAN-binder/low-can-binding/utils/config-parser.hpp b/CAN-binder/low-can-binding/utils/config-parser.hpp index e6bd9d22..2d504857 100644 --- a/CAN-binder/low-can-binding/utils/config-parser.hpp +++ b/CAN-binder/low-can-binding/utils/config-parser.hpp @@ -23,21 +23,23 @@ #include #include +#include +#include "INIReader.h" namespace utils { + /// @brief A configuration file parser that handle INI configuration + /// file format. class config_parser_t { private: - int conf_file_; /*!< conf_file_ - file that handle the binding configuration file */ - std::string config_content_; /*!< config_content_ - String that contains the content of config file */ - std::vector devices_name_; /*!< devices_name - Found devices name after reading configuration file */ + INIReader config_content_; /*!< config_content_ - Parsed content of INI file.*/ - void parse_devices_name(); public: config_parser_t(int conf_file); + config_parser_t(std::string conf_file); - void read_conf(); - std::vector get_devices_name(); + bool check_conf(); + const std::vector get_devices_name(); }; } -- cgit 1.2.3-korg