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 --- CAN-binder/low-can-binding/can/can-bus.cpp | 100 ++++++++--------------------- CAN-binder/low-can-binding/can/can-bus.hpp | 5 +- 2 files changed, 30 insertions(+), 75 deletions(-) (limited to 'CAN-binder/low-can-binding/can') diff --git a/CAN-binder/low-can-binding/can/can-bus.cpp b/CAN-binder/low-can-binding/can/can-bus.cpp index 33f58fd..8befec6 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 eb47476..5715c74 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(); -- cgit 1.2.3-korg