summaryrefslogtreecommitdiffstats
path: root/CAN-binder/low-can-binding/utils
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-04-18 20:23:14 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-04-18 20:23:14 +0200
commit44d7237fde80af222939445055a94a0e50e82935 (patch)
treea65cd2e82fc70ddeed4fbf3b2cd05c4b025c8f90 /CAN-binder/low-can-binding/utils
parent3642f8f2545f22f2e574bbd820e67229516e9149 (diff)
Use a system INI configuration file to get devices mapping
Instead of specifying a JSON configuration file with CAN devices name, it uses a mapping configuration file that map a high level device names to a real low level names. File path is to be specified into the generated source code which is /etc/dev-mapping.conf for now. Configuration file uses INI file format and is parsed using inih library cpp wrapper. Change-Id: Ibde104e76cd78a6cc86f6eec4f66c274b7567d43 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'CAN-binder/low-can-binding/utils')
-rw-r--r--CAN-binder/low-can-binding/utils/config-parser.cpp87
-rw-r--r--CAN-binder/low-can-binding/utils/config-parser.hpp14
2 files changed, 48 insertions, 53 deletions
diff --git a/CAN-binder/low-can-binding/utils/config-parser.cpp b/CAN-binder/low-can-binding/utils/config-parser.cpp
index e73b0f94..cb344be6 100644
--- a/CAN-binder/low-can-binding/utils/config-parser.cpp
+++ b/CAN-binder/low-can-binding/utils/config-parser.cpp
@@ -15,57 +15,37 @@
* limitations under the License.
*/
+#include "config-parser.hpp"
+
+#include "../low-can-binding.hpp"
+
namespace utils
{
+ /// @brief constructor using a POSIX file handle as input.
+ ///
+ /// @param conf_file - a POSIX file handle to the INI configuration file
config_parser_t::config_parser_t(int conf_file)
- : conf_file_{conf_file}, devices_name{}
+ : config_content_(fdopen(conf_file, "r"))
+ {
+ ::close(conf_file);
+ }
+
+ /// @brief constructor using path to file
+ config_parser_t::config_parser_t(std::string conf_file)
+ : config_content_{INIReader(conf_file)}
{}
- /// @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()
+ /// @brief read the conf_file_ and parse it into an INIReader object
+ /// to search into later.
+ bool config_parser_t::check_conf()
{
- FILE *fd = fdopen(conf_file_, "r");
- if (fd)
+ if (config_content_.ParseError() < 0)
{
- 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, "read_conf: Can't load the INI config file.");
+ return false;
}
- 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)));
- }
+ DEBUG(binder_interface, "read_conf: Configuration file parsed");
+ return true;
}
/// @brief Public method to access devices_name_ vector. If vector size equal 0
@@ -74,11 +54,24 @@ namespace utils
/// have to test the returned value.
///
/// @return A const vector with string of linux CAN devices.
- const std::vector<std::string>& get_devices_name()
+ const std::vector<std::string> config_parser_t::get_devices_name()
{
- if(devices_name_.empty())
- parse_devices_name();
+ std::vector<std::string> devices_name;
+
+ std::set<std::string> sections = config_content_.GetSections();
+ for(const auto& sectionsIt : sections)
+ {
+ if(sectionsIt == "CANbus-mapping")
+ {
+ std::set<std::string> fields = config_content_.GetFields(sectionsIt);
+ for(const auto& fieldsIt : fields)
+ {
+ std::string val = config_content_.Get(sectionsIt, fieldsIt, "INVALID");
+ devices_name.push_back(val);
+ }
+ }
+ }
- return devices_name_;
+ return devices_name;
}
} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/config-parser.hpp b/CAN-binder/low-can-binding/utils/config-parser.hpp
index e6bd9d22..2d504857 100644
--- a/CAN-binder/low-can-binding/utils/config-parser.hpp
+++ b/CAN-binder/low-can-binding/utils/config-parser.hpp
@@ -23,21 +23,23 @@
#include <json-c/json.h>
#include <string>
+#include <vector>
+#include "INIReader.h"
namespace utils
{
+ /// @brief A configuration file parser that handle INI configuration
+ /// file format.
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 */
+ INIReader config_content_; /*!< config_content_ - Parsed content of INI file.*/
- void parse_devices_name();
public:
config_parser_t(int conf_file);
+ config_parser_t(std::string conf_file);
- void read_conf();
- std::vector<std::string> get_devices_name();
+ bool check_conf();
+ const std::vector<std::string> get_devices_name();
};
}