Finish config parser retrieving device mapping.
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / utils / config-parser.cpp
1 /*
2  * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "config-parser.hpp"
19
20 #include "../binding/low-can-hat.hpp"
21
22 namespace utils
23 {
24         /// @brief constructor using path to file
25         config_parser_t::config_parser_t(std::string conf_file)
26                 : filepath_{conf_file}, config_content_{}
27         {
28                 config_content_.read_file(conf_file);
29         }
30
31         const std::string& config_parser_t::filepath() const
32         {
33                 return filepath_;
34         }
35
36         /// @brief read the conf_file_ and parse it into an INIReader object
37         /// to search into later.
38         bool config_parser_t::check_conf()
39         {
40                 if (config_content_.size() <= 0)
41                 {
42                         ERROR(binder_interface, "%s: Can't load the INI config file.", __FUNCTION__);
43                         return false;
44                 }
45                         DEBUG(binder_interface, "%s: Configuration file parsed", __FUNCTION__);
46                         return true;
47         }
48
49         /// @brief Public method to access devices_name_ vector. If vector size equal 0
50         /// then it will parses the configuration file content to fill it. It could be empty even
51         /// after parsing if content file just don't have a correct "canbus" directive so you
52         /// have to test the returned value.
53         ///
54         /// @return A const vector with string of linux CAN devices.
55         const std::vector<std::pair<std::string, std::string> > config_parser_t::get_devices_name()
56         {
57                 std::vector<std::pair<std::string, std::string> > devices_name;
58
59                 std::map<std::string, std::string> bus_mapping = config_content_.get_keys("CANbus-mapping");
60                 for(const auto& busIt : bus_mapping )
61                 {
62                         devices_name.push_back(std::make_pair(busIt.first, busIt.second));
63                 }
64
65                 return devices_name;
66         }
67 }