-}
-/********************************************************************************
-*
-* can_bus_t method implementation
-*
-*********************************************************************************/
-
-can_bus_t::can_bus_t(const struct afb_binding_interface *interface, int& conf_file)
- : interface_{interface}, conf_file_{conf_file}
-{
-}
-
-/**
- * @brief start threads relative to the can bus: decoding and pushing
- * as the reading is handled by can_bus_dev_t object
- */
-void can_bus_t::start_threads()
-{
- th_decoding_ = std::thread(can_decode_message, std::ref(*this));
- th_pushing_ = std::thread(can_event_push, std::ref(*this));
-}
-
-/**
- * @brief Initialize as many as can_bus_dev_t objects with their respective reading thread
- *
- * params[std::ifstream& conf_file] conf_file ifstream to the JSON configuration
- * file located at the rootdir of the binding
- */
-int can_bus_t::init_can_dev()
-{
- std::vector<std::string> devices_name;
- int i;
- size_t t;
-
- devices_name = read_conf();
-
- if (! devices_name.empty())
- {
- t = devices_name.size();
- i=0;
-
- for(const auto& device : devices_name)
- {
- can_bus_dev_t can_bus_device_handler(device);
- can_bus_device_handler.open(interface_);
- can_bus_device_handler.start_reading(std::ref(*this));
- i++;
- }
-
- NOTICE(interface_, "Initialized %d/%d can bus device(s)", i, t);
- return 0;
- }
- ERROR(interface_, "init_can_dev: Error at CAN device initialization.");
- return 1;
-}
-
-/**
- * @brief Read the conf file and extract device name
- *
- * @return[std:vector<std::string>] return a vector of device name
- */
-std::vector<std::string> can_bus_t::read_conf()
-{
- std::vector<std::string> ret;
- json_object *jo, *canbus;
- int n, i;
-
- 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);
-
- jo = json_tokener_parse(fd_conf_content.c_str());
-
- if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
- {
- ERROR(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)
- ret.push_back(json_object_get_string(canbus));
- 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(interface_, "Problem at reading the conf file");
- ret.clear();
- return ret;
-}
-
-/**
- * @brief: Get a can_message_t from can_message_q and return it
- * then point to the next can_message_t in queue.
- *
- * @return the next queue element or NULL if queue is empty.
- */
-can_message_t can_bus_t::next_can_message()
-{
- can_message_t can_msg(interface_);
-
- if(!can_message_q_.empty())
- {
- can_msg = can_message_q_.front();
- can_message_q_.pop();
- DEBUG(interface_, "next_can_message: Here is the next can message : id %d, length %d", can_msg.get_id(), can_msg.get_length());
- return can_msg;
- }
-
- NOTICE(interface_, "next_can_message: End of can message queue");
- has_can_message_ = false;
- return can_msg;
-}
-
-/**
- * @brief Append a new element to the can message queue and set
- * has_can_message_ boolean to true
- *
- * @params[const can_message_t& can_msg] the can_message_t to append
- *
- */
-void can_bus_t::push_new_can_message(const can_message_t& can_msg)
-{
- can_message_q_.push(can_msg);
-}
-
-/**
- * @brief Flag that let you know when can message queue is exhausted
- *
- * @return[bool] has_can_message_ bool
- */
-bool can_bus_t::has_can_message() const
-{
- return has_can_message_;
-}
-
-/**
- * @brief: Get a VehicleMessage from vehicle_message_q and return it
- * then point to the next VehicleMessage in queue.
- *
- * @return the next queue element or NULL if queue is empty.
- */
-openxc_VehicleMessage can_bus_t::next_vehicle_message()
-{
- openxc_VehicleMessage v_msg;
-
- if(! vehicle_message_q_.empty())
- {
- v_msg = vehicle_message_q_.front();
- vehicle_message_q_.pop();
- DEBUG(interface_, "next_vehicle_message: next vehicle message poped");
- return v_msg;
- }
-
- NOTICE(interface_, "next_vehicle_message: End of vehicle message queue");
- has_vehicle_message_ = false;
- return v_msg;
-}
-
-/**
- * @brief Append a new element to the vehicle message queue and set
- * has_vehicle_message_ boolean to true
- *
- * @params[const openxc_VehicleMessage& v_msg] the openxc_VehicleMessage to append
- *
- */
-void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
-{
- vehicle_message_q_.push(v_msg);
- has_vehicle_message_ = true;
-}
-
-/**
- * @brief Flag that let you know when vehicle message queue is exhausted
- *
- * @return[bool] has_vehicle_message_ bool
- */
-bool can_bus_t::has_vehicle_message() const
-{
- return has_vehicle_message_;