tCanDeviceT.hpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef _icl_hardware_can_tCanDeviceT_hpp_
24 #define _icl_hardware_can_tCanDeviceT_hpp_
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 
30 #ifndef _SYSTEM_WIN32_
31 # include <unistd.h>
32 #endif
33 
34 #include <icl_core/os_lxrt.h>
35 
37 
38 #undef ICL_CORE_LOCAL_LOGGING
39 //#define ICL_CORE_LOCAL_LOGGING
41 
42 namespace icl_hardware {
43 namespace can {
44 
45 template <typename TCanDescriptor>
46 tCanDeviceT<TCanDescriptor>::tCanDeviceT(const char *device_name, int flags,
47  unsigned char acceptance_code, unsigned char acceptance_mask, unsigned int baud_rate,
48  unsigned send_fifo_size, unsigned receive_fifo_size)
49  : m_can_user(-1)
50 {
52  bool success = false;
53 #ifdef _SYSTEM_LXRT_
54  if (m_canlxrt_available && icl_core::os::IsThisLxrtTask() && CanDriverLxrtSupport())
55  {
56  char *check_pointer;
57  const char *convert_string = device_name + strlen(device_name) - 1;
58  m_can_device = (TCanDescriptor)strtol(convert_string, &check_pointer, 10);
59  if (check_pointer == convert_string)
60  {
61  LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Error converting given device name '" << device_name << "' to LXRT-device id" << endl);
62  }
63  else
64  {
65  LOGGING_INFO_CO(CAN, tCanDevice, CanDriverName(), "Using LXRT extension for accessing can device " << (int)m_can_device << endl);
66  m_can_user = CreateCanFifoUser(m_can_device, acceptance_code, acceptance_mask, baud_rate, send_fifo_size, receive_fifo_size);
67  if (m_can_user >= 0)
68  {
69  success = true;
70  LOGGING_INFO_CO(CAN, tCanDevice, CanDriverName(), "Can User: " << m_can_user << endl);
71  }
72  else
73  {
74  LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Error creating can fifo of LXRT-CAN-device " << (int)m_can_device << endl);
75  }
76  }
77  }
78  else
79 #endif
80  {
81  // Achtung, falls wir nicht lxrt-task sind oder kein lxrt can
82  // interface zur Verf¸gung steht, wird der folgende code ausgef¸hrt:
83  m_can_device = CanDeviceOpen(device_name, flags, acceptance_code, acceptance_mask, baud_rate, send_fifo_size, receive_fifo_size);
85  {
86  success = true;
87  LOGGING_INFO_CO(CAN, tCanDevice, CanDriverName(), "Opened device " << device_name << " -> " << (uint64_t)m_can_device << endl);
88  }
89  else
90  {
91  LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Error open CAN-device '" << device_name << "' (errno=" << strerror(errno) << ")" << endl);
92  }
93  }
94 
95  if (!success)
97 }
98 
99 
100 template <typename TCanDescriptor>
102 {
103  if (IsInitialized())
104  {
105 
106 #ifdef _SYSTEM_LXRT_
107  if (m_can_user >= 0)
108  {
110  return;
111  }
112 #endif
113 
115  }
116 }
117 
118 template <typename TCanDescriptor>
120 {
121 #ifdef _SYSTEM_LXRT_
122  if (m_can_user >= 0)
123  {
124  if (!icl_core::os::IsThisLxrtTask())
125  {
126  LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Send Using an LXRT CanDevice from a non LXRT task is not allowed" << endl);
127  }
128  else
129  {
130  return CanFifoSend(m_can_device, &msg);
131  }
132  }
133 #endif
134 
135  return CanDeviceSend(m_can_device, msg);
136 }
137 
138 template <typename TCanDescriptor>
140 {
141 #ifdef _SYSTEM_LXRT_
142  if (m_can_user >= 0)
143  {
144  if (!icl_core::os::IsThisLxrtTask())
145  {
146  LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Receive Using an LXRT CanDevice from a non LXRT& task is not allowed" << endl);
147  }
148  else
149  {
150  return CanFifoReceive(m_can_device, m_can_user, &msg);
151  }
152  }
153 #endif
154 
155  return CanDeviceReceive(m_can_device, msg);
156 }
157 
158 template <typename TCanDescriptor>
160 {
161 #ifdef _SYSTEM_LXRT_
162  if (m_can_user >= 0)
163  {
164  if (!icl_core::os::IsThisLxrtTask()) {
165  LOGGING_ERROR_CO(CAN, tCanDevice, CanDriverName(), "Reset Using an LXRT CAN device from a non LXRT task is not allowed" << endl);
166  }
167  else
168  {
170  }
171  return;
172  }
173 #endif
174 
176 }
177 
178 template <typename TCanDescriptor>
180 {
182 }
183 
184 }
185 }
186 
187 #endif
int CanDeviceClose(tCanDescriptor)
Definition: UseDummyCan.h:62
int CanFifoReceive(int device_id, int user, tCanMessage *msg)
Receive a message via CAN-FIFO.
Definition: UseCanNoLxrt.h:64
Implements a struct representing a can message.
Definition: tCanMessage.h:43
int DestroyCanFifoUser(int device_id, int user_id)
Destroys a can FIFO in kernel space.
Definition: UseCanNoLxrt.h:50
int CreateCanFifoUser(int device_id, unsigned acceptance_code, unsigned acceptance_mask, unsigned int baud_rate, unsigned send_fifo_size, unsigned receive_fifo_size)
Creates a can FIFO in kernel space.
Definition: UseCanNoLxrt.h:41
int CanFifoSend(int device_id, const tCanMessage *msg)
Send a message via CAN-FIFO.
Definition: UseCanNoLxrt.h:57
tCanDeviceT(const char *device_name, int flags, unsigned char acceptance_code, unsigned char acceptance_mask, unsigned int baud_rate, unsigned send_fifo_size, unsigned receive_fifo_size)
Definition: tCanDeviceT.hpp:46
bool CanDescriptorValid(tCanDescriptor can_device)
Definition: UseDummyCan.h:38
Contains tCanDevice.
#define LOGGING_ERROR_CO(stream, classname, objectname, arg)
int CanFifoReset(int device_id)
Reset a CAN-device.
Definition: UseCanNoLxrt.h:72
int CanDeviceReset(tCanDescriptor)
Definition: UseDummyCan.h:77
unsigned __int64 uint64_t
bool CanDriverLxrtSupport()
no support for LXRT available
Definition: UseCanNoLxrt.h:35
const char * CanDriverName()
Definition: UseDummyCan.h:48
ThreadStream & endl(ThreadStream &stream)
tCanDescriptor InvalidCanDescriptor()
Definition: UseDummyCan.h:43
virtual int Send(const tCanMessage &msg)
#define LOGGING_INFO_CO(stream, classname, objectname, arg)
tCanDescriptor CanDeviceOpen(const char *, int, unsigned char, unsigned char, unsigned int, unsigned, unsigned)
Open a can device and set parameters.
Definition: UseDummyCan.h:53
int CanDeviceSend(tCanDescriptor, const tCanMessage &)
Definition: UseDummyCan.h:67
virtual int Receive(tCanMessage &msg)
int CanDeviceReceive(tCanDescriptor, tCanMessage &)
Definition: UseDummyCan.h:72


fzi_icl_can
Author(s):
autogenerated on Mon Jun 10 2019 13:17:02