Be able to return index or can bus name.
[apps/agl-service-can-low-level.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                 : config_content_{}
27         {
28                 config_content_.read_file(conf_file);
29         }
30
31         /// @brief read the conf_file_ and parse it into an INIReader object
32         /// to search into later.
33         bool config_parser_t::check_conf()
34         {
35                 if (config_content_.size() <= 0)
36                 {
37                         ERROR(binder_interface, "%s: Can't load the INI config file.", __FUNCTION__);
38                         return false;
39                 }
40                         DEBUG(binder_interface, "%s: Configuration file parsed", __FUNCTION__);
41                         return true;
42         }
43
44         /// @brief Public method to access devices_name_ vector. If vector size equal 0
45         /// then it will parses the configuration file content to fill it. It could be empty even
46         /// after parsing if content file just don't have a correct "canbus" directive so you
47         /// have to test the returned value.
48         ///
49         /// @return A const vector with string of linux CAN devices.
50         const std::vector<std::pair<std::string, std::string> > config_parser_t::get_devices_name()
51         {
52                 std::vector<std::pair<std::string, std::string> > devices_name;
53
54                 std::map<std::string, std::string> bus_mapping = config_content_.get_keys("CANbus-mapping");
55                 for(const auto& busIt : bus_mapping )
56                 {
57                         devices_name.push_back(std::make_pair(busIt.first, busIt.second));
58                 }
59
60                 return devices_name;
61         }
62 }