X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Futils%2Fconfig-parser.cpp;h=820c6d9b9faefe0e9ca953f08d828590e2f12405;hb=7eb266ac372a3ccfc0857a6a63d9f6c88afa1ffd;hp=e73b0f946aa80ddd3896a33f6d8d2b7d28dba439;hpb=a58d40b5ae336a54408201963b065ee049b43acd;p=apps%2Fagl-service-can-low-level.git diff --git a/low-can-binding/utils/config-parser.cpp b/low-can-binding/utils/config-parser.cpp index e73b0f94..820c6d9b 100644 --- a/low-can-binding/utils/config-parser.cpp +++ b/low-can-binding/utils/config-parser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015, 2016 ,2017 "IoT.bzh" + * Copyright (C) 2015, 2016 , 2017, 2018, 2019 "IoT\.bzh" * Author "Romain Forlot" * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,57 +15,35 @@ * limitations under the License. */ +#include "config-parser.hpp" + +#include "../binding/low-can-hat.hpp" + namespace utils { - config_parser_t::config_parser_t(int conf_file) - : conf_file_{conf_file}, devices_name{} - {} - - /// @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 constructor using path to file + config_parser_t::config_parser_t(std::string conf_file) + : filepath_{conf_file}, config_content_{} { - FILE *fd = fdopen(conf_file_, "r"); - if (fd) - { - 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, "Problem at reading the conf file"); + config_content_.read_file(conf_file); } - void parse_devices_name() + const std::string& config_parser_t::filepath() const { - json_object *jo, *canbus; - const char* taxi; - - jo = json_tokener_parse(config_content_.c_str()); + return filepath_; + } - 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))); - } + /// @brief read the conf_file_ and parse it into an INIReader object + /// to search into later. + bool config_parser_t::check_conf() + { + if (config_content_.size() <= 0) + { + AFB_ERROR("Can't load the INI config file: /etc/dev-mapping.conf."); + return false; + } + AFB_DEBUG("Configuration file parsed"); + return true; } /// @brief Public method to access devices_name_ vector. If vector size equal 0 @@ -74,11 +52,16 @@ 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::map bus_mapping = config_content_.get_keys("CANbus-mapping"); + for(const auto& busIt : bus_mapping ) + { + devices_name.push_back(std::make_pair(busIt.first, busIt.second)); + } - return devices_name_; + return devices_name; } -} \ No newline at end of file +}