Cleaning, improve comments
[apps/agl-service-can-low-level.git] / src / can-utils.cpp
index 55daf44..de9e8ce 100644 (file)
 *********************************************************************************/
 
 can_message_t::can_message_t(const struct afb_binding_interface* interface)
-       : interface_{interface}
+       : interface_{interface}, id_{0}, length_{0}, format_{CanMessageFormat::ERROR}, data_{0,0,0,0,0,0,0,0}
 {}
 
 uint32_t can_message_t::get_id() const
 {
-       if (id_ != 0)
-               return id_;
-       return 0;
+       return id_;
 }
 
 int can_message_t::get_format() const
 {
        if (format_ != CanMessageFormat::STANDARD || format_ != CanMessageFormat::EXTENDED)
-               return -1;
+               return CanMessageFormat::ERROR;
        return format_;
 }
 
 const uint8_t* can_message_t::get_data() const
 {
-       return &data_;
+       return data_;
 }
 uint8_t can_message_t::get_length() const
 {
        return length_;
 }
 
+bool can_message_t::is_correct_to_send()
+{
+       if (id_ != 0 && length_ != 0 && format_ != CanMessageFormat::ERROR)
+       {
+               int i;
+               for(i=0;i<CAN_MESSAGE_SIZE;i++)
+                       if(data_[i] != 0)
+                               return true;
+       }
+       return false;
+}
+
 void can_message_t::set_id(const uint32_t new_id)
 {
        switch(format_)
@@ -76,23 +86,25 @@ void can_message_t::set_format(const CanMessageFormat new_format)
 
 void can_message_t::set_data(const uint8_t new_data)
 {
-       ::memcpy(&data_, &new_data, sizeof(new_data));
-       length_ = sizeof(new_data);
+       if ((sizeof(new_data) / sizeof(uint8_t) > CAN_MESSAGE_SIZE))
+               ERROR(interface_, "Can set data, your data array is too big");
+       else
+       {
+               ::memcpy(&data_, &new_data, sizeof(new_data));
+               length_ = sizeof(new_data);
+       }
 }
 
-/*
- * @brief This is the preferred way to initialize a CanMessage object 
- * from a read canfd_frame message.
- * 
- * @param: canfd_frame pointer
- */
 void can_message_t::convert_from_canfd_frame(const canfd_frame& frame)
 {
        length_ = (frame.len > CAN_MAX_DLEN) ? (uint8_t)CAN_MAX_DLEN : frame.len;
        length_ = (frame.len > CANFD_MAX_DLEN) ? (uint8_t)CANFD_MAX_DLEN : frame.len;
 
        if (frame.can_id & CAN_ERR_FLAG)
+       {
                id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+               format_ = CanMessageFormat::ERROR;
+       }
        else if (frame.can_id & CAN_EFF_FLAG)
        {
                id_ = frame.can_id & CAN_EFF_MASK;
@@ -114,12 +126,18 @@ canfd_frame can_message_t::convert_to_canfd_frame()
 {
        canfd_frame frame;
 
-       frame.can_id = get_id();
-       frame.len = get_length();
-       ::memcpy(frame.data, get_data(), length_);
-
+       if(is_correct_to_send())
+       {
+               frame.can_id = get_id();
+               frame.len = get_length();
+               ::memcpy(frame.data, get_data(), length_);
+       }
+       else
+               ERROR(interface_, "can_message_t not correctly initialized to be sent");
+       
        return frame;
 }
+
 /********************************************************************************
 *
 *              can_bus_dev_t method implementation
@@ -228,13 +246,9 @@ canfd_frame can_bus_dev_t::read(const struct afb_binding_interface* interface)
        return canfd_frame;
 }
 
-/**
- * @brief start reading threads and set flag is_running_
- * 
- */
-void can_bus_dev_t::start_reading()
+void can_bus_dev_t::start_reading(can_bus_t& can_bus)
 {
-       th_reading_ = std::thread(can_reader, *this);
+       th_reading_ = std::thread(can_reader, std::ref(*this), std::ref(can_bus));
        is_running_ = true;
 }
 
@@ -246,57 +260,6 @@ bool can_bus_dev_t::is_running()
        return is_running_;
 }
 
-/**
- * @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_dev_t::next_can_message(const struct afb_binding_interface* interface)
-{
-       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_dev_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_dev_t::has_can_message() const
-{
-       return has_can_message_;
-}
-
-/**
- * @brief Send a can message from a can_message_t object.
- * 
- * params[const can_message_t& can_msg] the can message object to send
- * 
- */
 int can_bus_dev_t::send_can_message(can_message_t& can_msg, const struct afb_binding_interface* interface)
 {
        ssize_t nbytes;
@@ -328,27 +291,18 @@ int can_bus_dev_t::send_can_message(can_message_t& can_msg, const struct afb_bin
 *
 *********************************************************************************/
 
-can_bus_t::can_bus_t(const afb_binding_interface *itf, int& conf_file)
-       : interface_{itf}, conf_file_{conf_file}
+can_bus_t::can_bus_t(const struct afb_binding_interface *interface, int& conf_file)
+       :  conf_file_{conf_file}, interface_{interface}
 {
 }
 
-/**
- * @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, *this);
-       th_pushing_ = std::thread(can_event_push, *this);
+       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;
@@ -366,7 +320,7 @@ int can_bus_t::init_can_dev()
                {
                        can_bus_dev_t can_bus_device_handler(device);
                        can_bus_device_handler.open(interface_);
-                       can_bus_device_handler.start_reading();
+                       can_bus_device_handler.start_reading(std::ref(*this));
                        i++;
                }
 
@@ -377,11 +331,6 @@ int can_bus_t::init_can_dev()
        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;
@@ -420,12 +369,33 @@ std::vector<std::string> can_bus_t::read_conf()
        return ret;
 }
 
-/**
- * @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.
- */
+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;
+}
+
+void can_bus_t::push_new_can_message(const can_message_t& can_msg)
+{
+       can_message_q_.push(can_msg);
+}
+
+bool can_bus_t::has_can_message() const
+{
+       return has_can_message_;
+}
+
 openxc_VehicleMessage can_bus_t::next_vehicle_message()
 {
        openxc_VehicleMessage v_msg;
@@ -443,24 +413,12 @@ openxc_VehicleMessage can_bus_t::next_vehicle_message()
        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_;