SocketCANDevice.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Intelligent Industrial Robotics (IIROB) Group,
3  * Institute for Anthropomatics and Robotics (IAR) -
4  * Intelligent Process Control and Robotics (IPR),
5  * Karlsruhe Institute of Technology (KIT)
6  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #include "SocketCANDevice.h"
22 #include <errno.h>
23 
24 struct can_frame frame;
25 // ========================================================================== ;
26 // ;
27 // ---- private auxiliary functions ----------------------------------------- ;
28 // ;
29 // ========================================================================== ;
30 
31 // ========================================================================== ;
32 // ;
33 // ---- protected auxiliary functions --------------------------------------- ;
34 // ;
35 // ========================================================================== ;
36 
37 //possible incomplete
38 //
39 int SocketCANDevice::getDeviceError(int iErrorState) {
41 
42  if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_QRCVEMPTY) {
43  warning("receive queue is empty");
44  error = ERRID_DEV_WRITEERROR;
45  }
46  if (iErrorState & CAN_ERR_OVERRUN) {
47  warning("receive buffer overrun");
48  error = ERRID_DEV_READERROR;
49  }
50  if (iErrorState & CAN_ERR_XMTFULL) {
51  warning("transmit buffer full");
52  error = ERRID_DEV_WRITEERROR;
53  }
54  if (iErrorState & CAN_ERR_BUSOFF) {
55  warning("CAN_ERR_OFF_BUS");
56  error = ERRID_DEV_READERROR;
57  }
58  if (iErrorState & CAN_ERR_ILLPARAMTYPE) {
59  warning("CAN_ERR_ILLPARAMTYPE");
60  error = ERRID_DEV_READERROR;
61  }
62  if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_QXMTFULL) {
63  warning("transmit queue full");
64  error = ERRID_DEV_WRITEERROR;
65  }
66  if (iErrorState & CAN_ERR_BUSLIGHT) {
67  warning("bus error");
68  error = ERRID_DEV_WRITEERROR;
69  }
70  if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_BUSHEAVY) {
71  warning("bus error");
72  error = ERRID_DEV_WRITEERROR;
73  }
74  if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_RESOURCE) {
75  warning("can't create resource");
76  error = ERRID_DEV_WRITEERROR;
77  }
78 
79  return error;
80 }
81 
82 int SocketCANDevice::setBaudRate(unsigned char iBaudRate) {
83  return setBaudRate();
84 }
85 
87  return m_iErrorState;
88 }
89 
90 int SocketCANDevice::setMessageId(unsigned long uiMessageId) {
91  return m_iErrorState;
92 }
93 
95 
96  int iRetVal = 0;
97  can_frame frame;
98 
99  debug(1, "entering SocketCANDevice::clearReadQueue()...\n");
100  m_iErrorState = 0;
101  do {
102  debug(1, "Trying to read messages ...");
103  iRetVal = read(m_iDeviceId, &frame, sizeof(frame));
104  debug(0, " 0x%04x\n", iRetVal);
105 
106  } while (iRetVal != CAN_ERR_QRCVEMPTY);
107  return iRetVal;
108 }
109 
110 int SocketCANDevice::reinit(unsigned char ucBaudRateId) {
111 
112  return m_iErrorState;
113 }
114 
116 // set frame to zero
117  frame.data[0] = 0;
118  frame.data[1] = 0;
119  frame.data[2] = 0;
120  frame.data[3] = 0;
121  frame.data[4] = 0;
122  frame.data[5] = 0;
123  frame.data[6] = 0;
124  frame.data[7] = 0;
125  frame.can_dlc = 8; //rclProtocolMessage.m_ucMessageLength;
126  frame.can_id = 0; //rclProtocolMessage.m_uiMessageId;
127  int iCount = 0;
128  int bRet = 1, iRet = 0;
129  int nrRead = 0;
130 
131  debug(1, "Trying to read Device");
132 
133  iRet = read(m_iDeviceId, &frame, sizeof(frame));
134 
135  if (iRet >= 0) // no problem occured
136  {
137 
138  debug(1, "sizeof(frame)", sizeof(frame));
139  debug(1, "bytes read: %d", iRet);
140  debug(1, "id: %d", frame.can_id);
141  debug(1, "data: %c ", frame.data);
142  debug(1, "length: %d ", frame.can_dlc);
143 
144 // copy read data from frame to ProtocolMessage (byte-wise or string)
145  rclProtocolMessage.m_uiMessageId = frame.can_id;
146  rclProtocolMessage.m_ucMessageLength = frame.can_dlc;
147  memcpy(rclProtocolMessage.m_aucMessageData, frame.data,
148  rclProtocolMessage.m_ucMessageLength);
149  bRet = 0;
150  } else // reading problem occured
151  {
152  // no message, errror
153  if (iCount > 26)
154  printf("error in SocketCANDevice::readDevice()");
155  iCount++;
156  bRet = 1;
157  }
158 
159  debug(1, "bRet before return from SocketCANDevice::readDevice: %d ", bRet);
160  return bRet;
161 
162 }
163 
165 
166  debug(1, "SocketCANDevice::writeDevice ");
167 
168  int bytes_sent = -1;
169 
170  debug(1, "m_iDeviceId %d", m_iDeviceId);
171 
172  if (m_bInitialized == false)
173  return false;
174 
175  frame.can_dlc = (int) rclProtocolMessage.m_ucMessageLength;
176 
177  debug(1, "frame can_dlc: %d", frame.can_dlc);
178  debug(1, "clProtocolMessage.m_ucMessageLength: %d",
179  (unsigned int) rclProtocolMessage.m_ucMessageLength);
180 
181  frame.can_id = rclProtocolMessage.m_uiMessageId;
182 
183  debug(1, "frame can_id_%d ", frame.can_id);
184  debug(1, "rclProtocolMessage.m_uiMessageId: %d ",
185  rclProtocolMessage.m_uiMessageId);
186 
187 // copy data from ProtocolMessage to frame.data (byte-wise or string)
188  for (int i = 0; i < (unsigned int) rclProtocolMessage.m_ucMessageLength;
189  i++) {
190 
191  frame.data[i] = rclProtocolMessage.m_aucMessageData[i];
192  debug(1, "rclProtocolMessage.m_aucMessageData: %c ",
193  rclProtocolMessage.m_aucMessageData);
194  debug(1, "frame data[%d]: %c", i, frame.data);
195  }
196  debug(1, "sizeof frame : %d", sizeof(frame));
197 
198  int bRet = 1;
199 
200 //writing
201  bytes_sent = write(m_iDeviceId, &frame, sizeof(frame));
202  usleep(10000);
203  if (bytes_sent < 0) {
204  debug(1, "error in SocketCANDevice::writeDevice: ");
205  bRet = 1;
206  }
207 
208  if (bytes_sent > 0) {
209  bRet = 0;
210  debug(1, "bytes sent in SocketCANDevice::writeDevice: %d ", bytes_sent);
211  }
212 
213  return bRet;
214 
215 }
216 
217 // ========================================================================== ;
218 // ;
219 // ---- constructors / destructor ------------------------------------------- ;
220 // ;
221 // ========================================================================== ;
222 
224  m_bInitialized = false;
225 }
226 
228  error(-1, "Sorry constructor is not implemented");
229 }
230 
232  if (m_bInitialized) {
233  this->exit();
234  }
235 }
236 // ========================================================================== ;
237 // ;
238 // ---- operators ----------------------------------------------------------- ;
239 // ;
240 // ========================================================================== ;
241 
243  error(-1, "Sorry operator= is not implemented");
244  return *this;
245 }
246 
247 // ========================================================================== ;
248 // ;
249 // ---- query functions ----------------------------------------------------- ;
250 // ;
251 // ========================================================================== ;
252 
253 // ========================================================================== ;
254 // ;
255 // ---- modify functions ---------------------------------------------------- ;
256 // ;
257 // ========================================================================== ;
258 
259 void SocketCANDevice::setQueueSize(unsigned short uiQueueSize) {
260  m_uiQueueSize = uiQueueSize;
261 }
262 
263 void SocketCANDevice::setTimeOut(unsigned long uiTimeOut) {
264  m_uiTimeOut = uiTimeOut;
265 }
266 
267 // ========================================================================== ;
268 // ;
269 // ---- I/O functions ------------------------------------------------------- ;
270 // ;
271 // ========================================================================== ;
272 
273 // ========================================================================== ;
274 // ;
275 // ---- exec functions ------------------------------------------------------ ;
276 // ;
277 // ========================================================================== ;
278 
280  return init(0x014);
281 }
282 
283 int SocketCANDevice::init(unsigned long baudRate) {
284  return m_iErrorState;
285 }
286 
287 int SocketCANDevice::init(const char* acInitString) {
288 
289  printf("Trying to open CAN on can0 ...");
290  m_iErrorState = 0;
291  setTimeOut(100000);
292  m_iDeviceId = socket(PF_CAN, SOCK_RAW, CAN_RAW);
293 
294  struct timeval timeout;
295  timeout.tv_sec = 0;
296  timeout.tv_usec = m_uiTimeOut;
297 
298  if (setsockopt(m_iDeviceId, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout,
299  sizeof(struct timeval)) < 0)
300  error("setsockopt failed\n");
301 
302  struct ifreq ifr;
303  char* pcToken;
304  char acString[128];
305  strncpy(m_acInitString, acInitString, 128);
306  strncpy(acString, acInitString, 128);
307  pcToken = strtok(acString, ",");
308  std::string pcTokenStrInit(pcToken);
309  pcToken = strtok(pcToken, ":");
310  if (!pcToken) {
312  return m_iErrorState;
313  }
314  if (strcmp(pcToken, "SOCKETCAN") != 0) {
316  return m_iErrorState;
317  }
318  std::string pcTokenStr(pcToken);
319  std::string devName = pcTokenStrInit.substr(pcTokenStr.length() + 1, 4);
320  strcpy(ifr.ifr_name, devName.c_str());
321  m_DeviceName = ifr.ifr_name;
322  debug(1,"name: %x",*m_DeviceName);
323  int ret = ioctl(m_iDeviceId, SIOCGIFINDEX, &ifr);
324  struct sockaddr_can addr;
325  addr.can_family = AF_CAN;
326  addr.can_ifindex = ifr.ifr_ifindex;
327  int ret1 = bind(m_iDeviceId, (struct sockaddr*) &addr, sizeof(addr));
328 
329  m_bInitFlag = true;
330 
331  if (!m_iDeviceId) {
332  printf("Cannot open CAN on USB:\n");
333  } else {
334  printf("Open CAN on USB suceeded!\n");
335  m_bInitialized = true;
336  }
337 
339 
340  debug(1, "finished updateModuleIdMap");
341  debug(1,
342  "m_iErrorState before returning of SocketCANDevice::init(const char* acInitString): %d",
343  m_iErrorState);
344 
345  return m_iErrorState;
346 }
347 
349  int iRetVal = 0;
350  if (!m_bInitialized) {
351  warning("exit:device not initialized");
353  return m_iErrorState;
354  }
355  EnterCriticalSection(&m_csDevice);
356  iRetVal = CAN_ERR_OK;
357  if (iRetVal != CAN_ERR_OK) {
358  warning("can close failed Errorcode: %d", iRetVal);
359  getDeviceError(iRetVal);
361  }
362  m_bInitFlag = false;
363  LeaveCriticalSection(&m_csDevice);
364  DeleteCriticalSection(&m_csDevice);
365  return m_iErrorState;
366 }
367 
369  int iRetVal = 0;
370  TPCANRdMsg TPCMsg;
371  TPCMsg.Msg.LEN = 8;
372  TPCMsg.Msg.MSGTYPE = 0;
373  TPCMsg.Msg.ID = 0;
374  bool bRecieved = false;
375  can_frame frame;
376  m_iErrorState = 0;
377  iRetVal = read(m_iDeviceId, &frame, sizeof(frame));
378  m_iErrorState = 0;
379 
380  do {
381  iRetVal = read(m_iDeviceId, &frame, sizeof(frame));
382  if (iRetVal != CAN_ERR_OK) {
383  warning("can read failed Errorcode: 0x%04x", iRetVal);
384  m_iErrorState = getDeviceError(iRetVal);
385  return m_iErrorState;
386  }
387  bRecieved = true;
388  if (frame.can_id != MSGID_ALL) {
389  debug(1, "received CAN-ID %x, expected %x", TPCMsg.Msg.ID,
390  MSGID_ALL);
391  bRecieved = false;
392  }
393  if (frame.data[0] != CMDID_STARTMOVE) {
394  debug(1, "wrong command ID");
395  bRecieved = false;
396  }
397  } while (!bRecieved);
398  return m_iErrorState;
399 }
400 
#define ERRID_DEV_BADINITSTRING
Definition: m5apiw32.h:209
unsigned char m_aucMessageData[8]
int updateModuleIdMap()
Definition: Device.cpp:3779
int getDeviceError(int iErrorState)
#define PF_CAN
CRITICAL_SECTION m_csDevice
void error(const int iErrorCode, const char *pcErrorMessage,...) const
Definition: Message.cpp:204
#define ERRID_DEV_WRITEERROR
Definition: m5apiw32.h:212
void setTimeOut(unsigned long uiTimeOut)
unsigned short m_uiQueueSize
#define ERRID_DEV_NOTINITIALIZED
Definition: m5apiw32.h:211
unsigned char m_ucMessageLength
void setQueueSize(unsigned short uiQueueSize)
unsigned long m_uiMessageId
void warning(const char *pcWarningMessage,...) const
Definition: Message.cpp:257
int setMessageId(unsigned long uiMessageId)
int readDevice(CProtocolMessage &rclProtocolMessage)
bool m_bInitFlag
Definition: Device.h:40
#define MSGID_ALL
#define ERRID_DEV_EXITERROR
Definition: m5apiw32.h:219
void debug(const int iDebugLevel, const char *pcDebugMessage,...) const
Definition: Message.cpp:332
#define CMDID_STARTMOVE
int reinit(unsigned char ucBaudRateId)
SocketCANDevice & operator=(const SocketCANDevice &rclSocketCANDevice)
unsigned long m_uiTimeOut
#define AF_CAN
int m_iErrorState
Definition: Device.h:50
#define ERRID_DEV_READERROR
Definition: m5apiw32.h:213
struct can_frame frame
char m_acInitString[128]
Definition: Device.h:42
int writeDevice(CProtocolMessage &rclProtocolMessage)


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19