summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-03-28 17:52:17 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-04-11 12:41:41 +0200
commit8f996bf611ed029bd99ae404fbef97e54eb16764 (patch)
tree27abcff0be679a60a1521856c8451dd172b0e635
parent2cf6a1685f3ad53db6714c01d600492fe4bfc11b (diff)
Port configuration parsing to a separate object.
Change-Id: If5da63d2fdd498dcb6dde10469acaa91b00e60df Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--src/utils/config-parser.cpp84
-rw-r--r--src/utils/config-parser.hpp43
2 files changed, 127 insertions, 0 deletions
diff --git a/src/utils/config-parser.cpp b/src/utils/config-parser.cpp
new file mode 100644
index 0000000..e73b0f9
--- /dev/null
+++ b/src/utils/config-parser.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace utils
+{
+ config_parser_t::config_parser_t(int conf_file)
+ : conf_file_{conf_file}, devices_name{}
+ {}
+
+ /// @brief read the conf_file_ and will parse json objects
+ /// in it searching for canbus objects devices name.
+ ///
+ /// @return Vector of can bus device name string.
+ void can_bus_t::read_conf()
+ {
+ FILE *fd = fdopen(conf_file_, "r");
+ if (fd)
+ {
+ std::fseek(fd, 0, SEEK_END);
+ config_content_.resize(std::ftell(fd));
+ std::rewind(fd);
+ std::fread(&config_content_[0], 1, config_content_.size(), fd);
+ std::fclose(fd);
+
+ DEBUG(binder_interface, "Configuration file content : %s", config_content_.c_str());
+ }
+ ERROR(binder_interface, "Problem at reading the conf file");
+ }
+
+ void parse_devices_name()
+ {
+ json_object *jo, *canbus;
+ const char* taxi;
+
+ jo = json_tokener_parse(config_content_.c_str());
+
+ if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
+ {
+ ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
+ devices_name_.clear();
+ }
+ else if (json_object_get_type(canbus) != json_type_array)
+ {
+ taxi = json_object_get_string(canbus);
+ DEBUG(binder_interface, "Can bus found: %s", taxi);
+ devices_name_.push_back(std::string(taxi));
+ }
+ else
+ {
+ int n, i;
+ n = json_object_array_length(canbus);
+ for (i = 0 ; i < n ; i++)
+ devices_name_.push_back(json_object_get_string(json_object_array_get_idx(canbus, i)));
+ }
+ }
+
+ /// @brief Public method to access devices_name_ vector. If vector size equal 0
+ /// then it will parses the configuration file content to fill it. It could be empty even
+ /// after parsing if content file just don't have a correct "canbus" directive so you
+ /// have to test the returned value.
+ ///
+ /// @return A const vector with string of linux CAN devices.
+ const std::vector<std::string>& get_devices_name()
+ {
+ if(devices_name_.empty())
+ parse_devices_name();
+
+ return devices_name_;
+ }
+} \ No newline at end of file
diff --git a/src/utils/config-parser.hpp b/src/utils/config-parser.hpp
new file mode 100644
index 0000000..e6bd9d2
--- /dev/null
+++ b/src/utils/config-parser.hpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <json-c/json.h>
+
+#include <string>
+
+namespace utils
+{
+ class config_parser_t
+ {
+ private:
+ int conf_file_; /*!< conf_file_ - file that handle the binding configuration file */
+ std::string config_content_; /*!< config_content_ - String that contains the content of config file */
+ std::vector<std::string> devices_name_; /*!< devices_name - Found devices name after reading configuration file */
+
+ void parse_devices_name();
+ public:
+ config_parser_t(int conf_file);
+
+ void read_conf();
+ std::vector<std::string> get_devices_name();
+ };
+}