First draft about lock/wait thread management.
[apps/agl-service-can-low-level.git] / src / can-utils.cpp
index 09f4947..5a845b3 100644 (file)
  * limitations under the License.
  */
 
-#pragma once
+#include "can-utils.hpp"
+
+#include <map>
+#include <vector>
+#include <cstdio>
+#include <string>
+#include <fcntl.h>
+#include <unistd.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <json-c/json.h>
+#include <linux/can/raw.h>
+#include <systemd/sd-event.h>
+
+extern "C"
+{
+       #include <afb/afb-binding.h>
+}
 
 /********************************************************************************
 *
-*              CanBus method implementation
+*              CanMessage method implementation
 *
 *********************************************************************************/
 
-CanBus_c::CanBus_c(afb_binding_interface *itf, const std:string& dev_name)
+can_message_t::can_message_t()
+       : id_{0}, length_{0}, format_{CanMessageFormat::ERROR}, data_{0,0,0,0,0,0,0,0}
+{}
+
+uint32_t can_message_t::get_id() const
 {
-       interface = itf;
-       deviceName = dev_name;
+       return id_;
 }
 
-int CanBus_c::open()
+int can_message_t::get_format() const
 {
-       const int canfd_on = 1;
-       struct ifreq ifr;
-       struct timeval timeout = {1, 0};
+       if (format_ != CanMessageFormat::STANDARD || format_ != CanMessageFormat::EXTENDED)
+               return CanMessageFormat::ERROR;
+       return format_;
+}
 
-       DEBUG(interface, "open_can_dev: CAN Handler socket : %d", socket);
-       if (socket >= 0)
-               close(socket);
+const uint8_t* can_message_t::get_data() const
+{
+       return data_;
+}
+uint8_t can_message_t::get_length() const
+{
+       return length_;
+}
 
-       socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
-       if (socket < 0)
+bool can_message_t::is_correct_to_send()
+{
+       if (id_ != 0 && length_ != 0 && format_ != CanMessageFormat::ERROR)
        {
-               ERROR(interface, "open_can_dev: socket could not be created");
+               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_)
+       {
+               case CanMessageFormat::STANDARD:
+                       id_ = new_id & CAN_SFF_MASK;
+                       break;
+               case CanMessageFormat::EXTENDED:
+                       id_ = new_id & CAN_EFF_MASK;
+                       break;
+               default:
+                       ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
+                       break;
+       }
+}
+
+void can_message_t::set_format(const CanMessageFormat new_format)
+{
+       if(new_format == CanMessageFormat::STANDARD || new_format == CanMessageFormat::EXTENDED)
+               format_ = new_format;
+       else
+               ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
+}
+
+void can_message_t::set_data(const uint8_t new_data)
+{
+       if ((sizeof(new_data) / sizeof(uint8_t) > CAN_MESSAGE_SIZE))
+               ERROR(binder_interface, "Can set data, your data array is too big");
        else
        {
-               /* Set timeout for read */
-               setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
-               /* try to switch the socket into CAN_FD mode */
-               if (setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
-               {
-                       NOTICE(interface, "open_can_dev: Can not switch into CAN Extended frame format.");
-                       is_fdmode_on = false;
-               } else {
-                       is_fdmode_on = true;
-               }
+               ::memcpy(&data_, &new_data, sizeof(new_data));
+               length_ = sizeof(new_data);
+       }
+}
 
-               /* Attempts to open a socket to CAN bus */
-               strcpy(ifr.ifr_name, device);
-               if(ioctl(socket, SIOCGIFINDEX, &ifr) < 0)
-                       ERROR(interface, "open_can_dev: ioctl failed");
-               else
-               {
-                       txAddress.can_family = AF_CAN;
-                       txAddress.can_ifindex = ifr.ifr_ifindex;
+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;
 
-                       /* And bind it to txAddress */
-                       if (bind(socket, (struct sockaddr *)&txAddress, sizeof(txAddress)) < 0)
-                       {
-                               ERROR(interface, "open_can_dev: bind failed");
-                       }
-                       else
-                       {
-                               fcntl(socket, F_SETFL, O_NONBLOCK);
-                               return 0;
-                       }
-               }
-               close(socket);
-               socket = -1;
+       if (frame.can_id & CAN_ERR_FLAG)
+       {
+               id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+               format_ = CanMessageFormat::ERROR;
        }
-       return -1;
+       else if (frame.can_id & CAN_EFF_FLAG)
+       {
+               id_ = frame.can_id & CAN_EFF_MASK;
+               format_ = CanMessageFormat::EXTENDED;
+       }
+       else
+       {
+               id_ = frame.can_id & CAN_SFF_MASK;
+               format_ = CanMessageFormat::STANDARD;
+       }
+
+       if (sizeof(frame.data) <= sizeof(data_))
+               ::memcpy(&data_, frame.data, length_);
+       else if (sizeof(frame.data) >= CAN_MAX_DLEN)
+               ERROR(binder_interface, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
 }
 
-int CanBus_c::close()
+canfd_frame can_message_t::convert_to_canfd_frame()
 {
-       close(socket);
-       socket = -1;
+       canfd_frame frame;
+
+       if(is_correct_to_send())
+       {
+               frame.can_id = get_id();
+               frame.len = get_length();
+               ::memcpy(frame.data, get_data(), length_);
+       }
+       else
+               ERROR(binder_interface, "can_message_t not correctly initialized to be sent");
+       
+       return frame;
 }
 
-void CanBus_c::start_threads()
+/********************************************************************************
+*
+*              can_bus_t method implementation
+*
+*********************************************************************************/
+
+can_bus_t::can_bus_t(int& conf_file)
+       :  conf_file_{conf_file}
 {
-       std::queue <CanMessage_c> can_message_q;
+}
 
-       th_reading = std::thread(can_reader, interface, socket, can_message_q);
-       th_decoding = std::thread(can_decoder, interface, can_message_q, can_message_q);
-       th_pushing = std::thread(can_event_push, interface, can_message_q);
+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));
 }
 
-/*
- * Send a can message from a CanMessage_c object.
- */
-int CanBus_c::send_can_message(CanMessage_c can_msg)
+
+int can_bus_t::init_can_dev()
 {
-       int nbytes;
-       canfd_frame *f;
+       std::vector<std::string> devices_name;
+       int i;
+       size_t t;
 
-       f = can_msg.convert_to_canfd_frame();
+       devices_name = read_conf();
 
-       if(socket >= 0)
+       if (! devices_name.empty())
        {
-               nbytes = sendto(socket, &f, sizeof(struct canfd_frame), 0,
-                               (struct sockaddr*)&txAddress, sizeof(txAddress));
-                               
-               if (nbytes == -1)
+               t = devices_name.size();
+               i=0;
+
+               for(const auto& device : devices_name)
                {
-                       ERROR(interface, "send_can_message: Sending CAN frame failed.");
-                       return -1;
+                       can_bus_dev_t can_bus_device_handler(device);
+                       can_bus_device_handler.open();
+                       can_bus_device_handler.start_reading(std::ref(*this));
+                       i++;
                }
-               return nbytes;
+
+               NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
+               return 0;
        }
-       else
+       ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read into configuration file. Did you specify canbus JSON object ?");
+       return 1;
+}
+
+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)
        {
-               ERROR(interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
-               open_can_dev();
+               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(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)
+                       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;
        }
-       return 0;
+       ERROR(binder_interface, "Problem at reading the conf file");
+       ret.clear();
+       return ret;
 }
 
-/*
- * Get a CanMessage from can_message_q and return it
- * then point to the next CanMessage in queue.
- * 
- * Return the next queue element or NULL if queue is empty.
- */
-CanMessage_c* CanBus_c::next_can_message()
+can_message_t can_bus_t::next_can_message()
 {
-       if(! can_message_q.empty())
+       can_message_t can_msg;
+
+       if(!can_message_q_.empty())
        {
-               CanMessage_c can_msg = can_message_q.front();
-               can_message_q.pop()
-               return &can_msg;
+               can_msg = can_message_q_.front();
+               can_message_q_.pop();
+               DEBUG(binder_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;
        }
-
-       return NULL;
+       
+       NOTICE(binder_interface, "next_can_message: End of can message queue");
+       has_can_message_ = false;
+       return can_msg;
 }
 
-void CanBus_c::insert_new_can_message(CanMessage_c *can_msg)
+void can_bus_t::push_new_can_message(const can_message_t& can_msg)
 {
-       can_message_q.push(can_msg);
+       can_message_q_.push(can_msg);
 }
 
-/********************************************************************************
-*
-*              CanMessage method implementation
-*
-*********************************************************************************/
-
-uint32_t CanMessage_c::get_id()
+bool can_bus_t::has_can_message() const
 {
-       return id;
+       return has_can_message_;
 }
 
-int CanMessage_c::get_format()
+openxc_VehicleMessage can_bus_t::next_vehicle_message()
 {
-       return format;
+       openxc_VehicleMessage v_msg;
+
+       if(! vehicle_message_q_.empty())
+       {
+               v_msg = vehicle_message_q_.front();
+               vehicle_message_q_.pop();
+               DEBUG(binder_interface, "next_vehicle_message: next vehicle message poped");
+               return v_msg;
+       }
+       
+       NOTICE(binder_interface, "next_vehicle_message: End of vehicle message queue");
+       has_vehicle_message_ = false;
+       return v_msg;
 }
 
-uint8_t CanMessage_c::get_data()
+void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
 {
-       return data;
+       vehicle_message_q_.push(v_msg);
+       has_vehicle_message_ = true;
 }
-uint8_t CanMessage_c::get_lenght()
+
+bool can_bus_t::has_vehicle_message() const
 {
-       return lenght;
+       return has_vehicle_message_;
 }
 
-void CanMessage_c::set_id(uint32_t new_id)
+/********************************************************************************
+*
+*              This is the sd_event_add_io callback function declaration. 
+*              Its implementation can be found into low-can-binding.cpp.
+*
+*********************************************************************************/
+
+int can_frame_received(sd_event_source *s, int fd, uint32_t revents, void *userdata);
+
+/********************************************************************************
+*
+*              can_bus_dev_t method implementation
+*
+*********************************************************************************/
+
+can_bus_dev_t::can_bus_dev_t(const std::string &dev_name)
+       : device_name_{dev_name}
 {
-       switch(format):
-               case CanMessageFormat::SIMPLE:
-                       id = new_id & CAN_SFF_MASK;
-               case CanMessageFormat::EXTENDED:
-                       id = new_id & CAN_EFF_MASK;
-               default:
-                       ERROR(interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
 }
 
-void CanMessage_c::set_format(CanMessageFormat new_format)
+int can_bus_dev_t::event_loop_connection()
 {
-       if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::EXTENDED)
-               format = new_format;
-       else
-               ERROR(interface, "ERROR: Can set format, wrong format chosen");
+       sd_event_source *source;
+       int rc;
+
+       /* adds to the event loop */
+       rc = sd_event_add_io(afb_daemon_get_event_loop(binder_interface->daemon), &source, can_socket_, EPOLLIN, can_frame_received, this);
+       if (rc < 0) {
+               close();
+               ERROR(binder_interface, "Can't coonect CAN device %s to the event loop", device_name_);
+       } else {
+               NOTICE(binder_interface, "Connected to %s", device_name_);
+       }
+       return rc;
 }
 
-void CanMessage_c::set_data(uint8_t new_data)
+int can_bus_dev_t::open()
 {
-       data = new_data;
+       const int canfd_on = 1;
+       struct ifreq ifr;
+       struct timeval timeout = {1, 0};
+
+       DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_);
+       if (can_socket_ >= 0)
+               return 0;
+
+       can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
+       if (can_socket_ < 0)
+       {
+               ERROR(binder_interface, "socket could not be created");
+       }
+       else
+       {
+               /* Set timeout for read */
+               ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
+               /* try to switch the socket into CAN_FD mode */
+               if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
+               {
+                       NOTICE(binder_interface, "Can not switch into CAN Extended frame format.");
+                       is_fdmode_on_ = false;
+               } else {
+                       is_fdmode_on_ = true;
+               }
+
+               /* Attempts to open a socket to CAN bus */
+               ::strcpy(ifr.ifr_name, device_name_.c_str());
+               if(::ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
+                       ERROR(binder_interface, "ioctl failed");
+               else
+               {
+                       txAddress_.can_family = AF_CAN;
+                       txAddress_.can_ifindex = ifr.ifr_ifindex;
+
+                       /* And bind it to txAddress */
+                       if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
+                       {
+                               ERROR(binder_interface, "Bind failed");
+                       }
+                       else
+                       {
+                               ::fcntl(can_socket_, F_SETFL, O_NONBLOCK);
+                               return 0;
+                       }
+               }
+               close();
+       }
+       return -1;
 }
 
-void CanMessage_c::set_lenght(uint8_t new_length)
+int can_bus_dev_t::close()
 {
-       lenght = new_lenght;
+       ::close(can_socket_);
+       can_socket_ = -1;
+       return can_socket_;
 }
 
-/*
- * This is the preferred way to initialize a CanMessage object 
- * from a read canfd_frame message.
- * 
- * params: canfd_frame pointer
- */
-void CanMessage_c::convert_from_canfd_frame(canfd_frame *frame)
+canfd_frame can_bus_dev_t::read()
 {
-       
-       lenght = (canfd_frame->len > maxdlen) ? maxdlen : canfd_frame->len;
+       ssize_t nbytes;
+       //int maxdlen;
+       canfd_frame canfd_frame;
 
-       switch (canfd_frame->can_id): 
-               case (canfd_frame->can_id & CAN_ERR_FLAG):
-                       id = canfd_frame->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+       /* Test that socket is really opened */
+       if (can_socket_ < 0)
+       {
+               ERROR(binder_interface, "read_can: Socket unavailable. Closing thread.");
+               is_running_ = false;
+       }
+
+       nbytes = ::read(can_socket_, &canfd_frame, CANFD_MTU);
+
+       switch(nbytes)
+       {
+               case CANFD_MTU:
+                       DEBUG(binder_interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
+                       //maxdlen = CANFD_MAX_DLEN;
                        break;
-               case (canfd_frame->can_id & CAN_EFF_FLAG):
-                       id = canfd_frame->can_id & CAN_EFF_MASK;
-                       format = CanMessageFormat::EXTENDED;
+               case CAN_MTU:
+                       DEBUG(binder_interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
+                       //maxdlen = CAN_MAX_DLEN;
                        break;
                default:
-                       format = CanMessageFormat::STANDARD;
-                       id = canfd_frame->can_id & CAN_SFF_MASK;
+                       if (errno == ENETDOWN)
+                                       ERROR(binder_interface, "read_can: %s binder_interface down", device_name_);
+                       ERROR(binder_interface, "read_can: Error reading CAN bus");
+                       ::memset(&canfd_frame, 0, sizeof(canfd_frame));
+                       is_running_ = false;
                        break;
+       }
+       
+       return canfd_frame;
+}
 
-       if (sizeof(canfd_frame->data) <= sizeof(data))
-       {
-               memcpy(data, canfd_frame->data, lenght);
-               return 0;
-       } else if (sizeof(canfd_frame->data) >= CAN_MAX_DLEN)
-               ERROR(interface, "CanMessage_c: canfd_frame data too long to be stored into CanMessage object");
+void can_bus_dev_t::start_reading(can_bus_t& can_bus)
+{
+       th_reading_ = std::thread(can_reader, std::ref(*this), std::ref(can_bus));
+       is_running_ = true;
 }
 
-canfd_frame* convert_to_canfd_frame()
+/*
+ * Return is_running_ bool
+ */
+bool can_bus_dev_t::is_running()
 {
-       canfd_frame frame;
+       return is_running_;
+}
 
-       frame.id = can_msg.get_id();
-       frame.len = can_msg.get_lenght();
-       frame.data = can_msg.get_data();
+int can_bus_dev_t::send_can_message(can_message_t& can_msg)
+{
+       ssize_t nbytes;
+       canfd_frame f;
 
-       return &frame;
-}
+       f = can_msg.convert_to_canfd_frame();
+
+       if(can_socket_ >= 0)
+       {
+               nbytes = ::sendto(can_socket_, &f, sizeof(struct canfd_frame), 0,
+                               (struct sockaddr*)&txAddress_, sizeof(txAddress_));
+               if (nbytes == -1)
+               {
+                       ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
+                       return -1;
+               }
+               return (int)nbytes;
+       }
+       else
+       {
+               ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
+               open();
+       }
+       return 0;
+}
\ No newline at end of file