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