Change directory architecture to use 2 separated projects.
[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 namespace utils
19 {
20         config_parser_t::config_parser_t(int conf_file)
21                 : conf_file_{conf_file}, devices_name{}
22         {}
23
24         /// @brief read the conf_file_ and will parse json objects
25         /// in it searching for canbus objects devices name.
26         ///
27         /// @return Vector of can bus device name string.
28         void can_bus_t::read_conf()
29         {
30                 FILE *fd = fdopen(conf_file_, "r");
31                 if (fd)
32                 {
33                         std::fseek(fd, 0, SEEK_END);
34                         config_content_.resize(std::ftell(fd));
35                         std::rewind(fd);
36                         std::fread(&config_content_[0], 1, config_content_.size(), fd);
37                         std::fclose(fd);
38
39                         DEBUG(binder_interface, "Configuration file content : %s", config_content_.c_str());
40                 }
41                 ERROR(binder_interface, "Problem at reading the conf file");
42         }
43
44         void parse_devices_name()
45         {
46                 json_object *jo, *canbus;
47                 const char* taxi;
48
49                 jo = json_tokener_parse(config_content_.c_str());
50
51                         if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
52                         {
53                                 ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
54                                 devices_name_.clear();
55                         }
56                         else if (json_object_get_type(canbus) != json_type_array)
57                         {
58                                 taxi = json_object_get_string(canbus);
59                                 DEBUG(binder_interface, "Can bus found: %s", taxi);
60                                 devices_name_.push_back(std::string(taxi));
61                         }
62                         else
63                         {
64                                 int n, i;
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)));
68                         }
69         }
70
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.
75         ///
76         /// @return A const vector with string of linux CAN devices.
77         const std::vector<std::string>& get_devices_name()
78         {
79                 if(devices_name_.empty())
80                         parse_devices_name();
81
82                 return devices_name_;
83         }
84 }