2 * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 config_parser_t::config_parser_t(int conf_file)
21 : conf_file_{conf_file}, devices_name{}
24 /// @brief read the conf_file_ and will parse json objects
25 /// in it searching for canbus objects devices name.
27 /// @return Vector of can bus device name string.
28 void can_bus_t::read_conf()
30 FILE *fd = fdopen(conf_file_, "r");
33 std::fseek(fd, 0, SEEK_END);
34 config_content_.resize(std::ftell(fd));
36 std::fread(&config_content_[0], 1, config_content_.size(), fd);
39 DEBUG(binder_interface, "Configuration file content : %s", config_content_.c_str());
41 ERROR(binder_interface, "Problem at reading the conf file");
44 void parse_devices_name()
46 json_object *jo, *canbus;
49 jo = json_tokener_parse(config_content_.c_str());
51 if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
53 ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
54 devices_name_.clear();
56 else if (json_object_get_type(canbus) != json_type_array)
58 taxi = json_object_get_string(canbus);
59 DEBUG(binder_interface, "Can bus found: %s", taxi);
60 devices_name_.push_back(std::string(taxi));
65 n = json_object_array_length(canbus);
66 for (i = 0 ; i < n ; i++)
67 devices_name_.push_back(json_object_get_string(json_object_array_get_idx(canbus, i)));
71 /// @brief Public method to access devices_name_ vector. If vector size equal 0
72 /// then it will parses the configuration file content to fill it. It could be empty even
73 /// after parsing if content file just don't have a correct "canbus" directive so you
74 /// have to test the returned value.
76 /// @return A const vector with string of linux CAN devices.
77 const std::vector<std::string>& get_devices_name()
79 if(devices_name_.empty())