tCanDeviceT.hpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #ifndef _icl_hardware_can_tCanDeviceT_hpp_
00024 #define _icl_hardware_can_tCanDeviceT_hpp_
00025 
00026 #include <errno.h>
00027 #include <fcntl.h>
00028 #include <string.h>
00029 
00030 #ifndef _SYSTEM_WIN32_
00031 # include <unistd.h>
00032 #endif
00033 
00034 #include <icl_core/os_lxrt.h>
00035 
00036 #include "icl_hardware_can/tCanDeviceT.h"
00037 
00038 #undef ICL_CORE_LOCAL_LOGGING
00039 //#define ICL_CORE_LOCAL_LOGGING
00040 #include "icl_hardware_can/Logging.h"
00041 
00042 namespace icl_hardware {
00043 namespace can {
00044 
00045 template <typename TCanDescriptor>
00046 tCanDeviceT<TCanDescriptor>::tCanDeviceT(const char *device_name, int flags,
00047     unsigned char acceptance_code, unsigned char acceptance_mask, unsigned int baud_rate,
00048     unsigned send_fifo_size, unsigned receive_fifo_size)
00049     : m_can_user(-1)
00050 {
00051   m_can_device = InvalidCanDescriptor();
00052   bool success = false;
00053 #ifdef _SYSTEM_LXRT_
00054   if (m_canlxrt_available && icl_core::os::IsThisLxrtTask() && CanDriverLxrtSupport())
00055   {
00056     char *check_pointer;
00057     const char *convert_string = device_name + strlen(device_name) - 1;
00058     m_can_device = (TCanDescriptor)strtol(convert_string, &check_pointer, 10);
00059     if (check_pointer == convert_string)
00060     {
00061       LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Error converting given device name '" << device_name << "' to LXRT-device id" << endl);
00062     }
00063     else
00064     {
00065       LOGGING_INFO_CO(CAN, tCanDevice, CanDriverName(), "Using LXRT extension for accessing can device " << (int)m_can_device << endl);
00066       m_can_user = CreateCanFifoUser(m_can_device, acceptance_code, acceptance_mask, baud_rate, send_fifo_size, receive_fifo_size);
00067       if (m_can_user >= 0)
00068       {
00069         success = true;
00070         LOGGING_INFO_CO(CAN, tCanDevice, CanDriverName(), "Can User: " << m_can_user << endl);
00071       }
00072       else
00073       {
00074        LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Error creating can fifo of LXRT-CAN-device " << (int)m_can_device << endl);
00075       }
00076     }
00077   }
00078   else
00079 #endif
00080   {
00081     // Achtung, falls wir nicht lxrt-task sind oder kein lxrt can
00082     // interface zur Verf¸gung steht, wird der folgende code ausgef¸hrt:
00083     m_can_device = CanDeviceOpen(device_name, flags, acceptance_code, acceptance_mask, baud_rate, send_fifo_size, receive_fifo_size);
00084     if (CanDescriptorValid(m_can_device))
00085     {
00086       success = true;
00087       LOGGING_INFO_CO(CAN, tCanDevice, CanDriverName(), "Opened device " << device_name << " -> " << (uint64_t)m_can_device << endl);
00088     }
00089     else
00090     {
00091       LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Error open CAN-device '" << device_name << "' (errno=" << strerror(errno) << ")" << endl);
00092     }
00093   }
00094 
00095   if (!success)
00096     m_can_device = InvalidCanDescriptor();
00097 }
00098 
00099 
00100 template <typename TCanDescriptor>
00101 tCanDeviceT<TCanDescriptor>::~tCanDeviceT()
00102 {
00103   if (IsInitialized())
00104   {
00105 
00106 #ifdef _SYSTEM_LXRT_
00107     if (m_can_user >= 0)
00108     {
00109       DestroyCanFifoUser(m_can_device, m_can_user);
00110       return;
00111     }
00112 #endif
00113 
00114     CanDeviceClose(m_can_device);
00115   }
00116 }
00117 
00118 template <typename TCanDescriptor>
00119 int tCanDeviceT<TCanDescriptor>::Send(const tCanMessage &msg)
00120 {
00121 #ifdef _SYSTEM_LXRT_
00122   if (m_can_user >= 0)
00123   {
00124     if (!icl_core::os::IsThisLxrtTask())
00125     {
00126       LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Send Using an LXRT CanDevice from a non LXRT task is not allowed" << endl);
00127     }
00128     else
00129     {
00130       return CanFifoSend(m_can_device, &msg);
00131     }
00132   }
00133 #endif
00134 
00135   return CanDeviceSend(m_can_device, msg);
00136 }
00137 
00138 template <typename TCanDescriptor>
00139 int tCanDeviceT<TCanDescriptor>::Receive(tCanMessage &msg)
00140 {
00141 #ifdef _SYSTEM_LXRT_
00142   if (m_can_user >= 0)
00143   {
00144     if (!icl_core::os::IsThisLxrtTask())
00145     {
00146      LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Receive Using an LXRT CanDevice from a non LXRT& task is not allowed" << endl);
00147     }
00148     else
00149     {
00150       return CanFifoReceive(m_can_device, m_can_user, &msg);
00151     }
00152   }
00153 #endif
00154 
00155   return CanDeviceReceive(m_can_device, msg);
00156 }
00157 
00158 template <typename TCanDescriptor>
00159 void tCanDeviceT<TCanDescriptor>::Reset()
00160 {
00161 #ifdef _SYSTEM_LXRT_
00162   if (m_can_user >= 0)
00163   {
00164     if (!icl_core::os::IsThisLxrtTask()) {
00165       LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Reset Using an LXRT CAN device from a non LXRT task is not allowed" << endl);
00166     }
00167     else
00168     {
00169       CanFifoReset(m_can_device);
00170     }
00171     return;
00172   }
00173 #endif
00174 
00175   CanDeviceReset(m_can_device);
00176 }
00177 
00178 template <typename TCanDescriptor>
00179 bool tCanDeviceT<TCanDescriptor>::IsInitialized()
00180 {
00181   return CanDescriptorValid(m_can_device);
00182 }
00183 
00184 }
00185 }
00186 
00187 #endif


fzi_icl_can
Author(s):
autogenerated on Tue Aug 8 2017 03:07:51