-/// @brief Will initialize can_bus_dev_t objects after reading
-/// the configuration file passed in the constructor. All CAN buses
-/// Initialized here will be added to a vector holding them for
-/// inventory and later access.
-///
-/// That will initialize CAN socket reading too using a new thread.
-///
-/// @return 0 if ok, other if not.
-int can_bus_t::init_can_dev()
-{
- std::vector<std::string> devices_name;
- int i = 0;
- size_t t;
-
- devices_name = read_conf();
-
- if (! devices_name.empty())
- {
- t = devices_name.size();
-
- for(const auto& device : devices_name)
- {
- can_bus_t::can_devices_[device] = std::make_shared<can_bus_dev_t>(device, i);
- if (can_bus_t::can_devices_[device]->open() == 0)
- {
- DEBUG(binder_interface, "Start reading thread");
- NOTICE(binder_interface, "%s device opened and reading", device.c_str());
- can_bus_t::can_devices_[device]->start_reading(*this);
- i++;
- }
- else
- {
- ERROR(binder_interface, "Can't open device %s", device.c_str());
- return 1;
- }
- }
-
- NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, (int)t);
- return 0;
- }
- ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read from configuration file. Did you specify canbus JSON object ?");
- return 1;
-}
-
-/// @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.
-std::vector<std::string> can_bus_t::read_conf()
-{
- std::vector<std::string> ret;
- json_object *jo, *canbus;
- int n, i;
- const char* taxi;
-
- FILE *fd = fdopen(conf_file_, "r");
- if (fd)
- {
- std::string fd_conf_content;
- std::fseek(fd, 0, SEEK_END);
- fd_conf_content.resize(std::ftell(fd));
- std::rewind(fd);
- std::fread(&fd_conf_content[0], 1, fd_conf_content.size(), fd);
- std::fclose(fd);
-
- DEBUG(binder_interface, "Configuration file content : %s", fd_conf_content.c_str());
- jo = json_tokener_parse(fd_conf_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.");
- ret.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);
- ret.push_back(std::string(taxi));
- }
- else
- {
- n = json_object_array_length(canbus);
- for (i = 0 ; i < n ; i++)
- ret.push_back(json_object_get_string(json_object_array_get_idx(canbus, i)));
- }
- return ret;
- }
- ERROR(binder_interface, "Problem at reading the conf file");
- ret.clear();
- return ret;
-}
-