iCub-main
Cfw2Can.cpp
Go to the documentation of this file.
1 // -*- Mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4 * Copyright (C) 2010 Robotcub Consortium
5 * Author: Lorenzo Natale
6 * CopyPolicy: Released under the terms of the GNU GPL v2.0.
7 *
8 */
12 
13 #include "libcfw002.h"
14 #include <yarp/dev/CanBusInterface.h>
15 #include <yarp/os/Bottle.h>
16 #include <yarp/os/Value.h>
17 #include "Cfw2Can.h"
18 
19 using namespace yarp::dev;
20 using namespace yarp::os;
21 
23 {
24  handle = new CFWCAN_HANDLE;
25 }
26 
28 {
29  if (handle!=0)
30  delete handle;
31 }
32 
33 bool Cfw2Can::canSetBaudRate(unsigned int rate)
34 {
35  // printf("CfwCan::canSetBaudRate not yet implemented\n");
36  return true;
37 }
38 
39 bool Cfw2Can::canGetBaudRate(unsigned int *rate)
40 {
41  // printf("Cfw2Can::canSetBaudRate not yet implemented\n");
42  *rate=0;
43  return true;
44 }
45 
46 bool Cfw2Can::canIdAdd(unsigned int id)
47 {
48 
49 #ifdef CFW2_HAS_FILTERS
50  int res;
51  res = cfwCanSetIdFilter(*handle , id , 1);
52 #endif
53  return true;
54 }
55 
56 bool Cfw2Can::canIdDelete(unsigned int id)
57 {
58 
59 #ifdef CFW2_HAS_FILTERS
60  int res;
61  res = cfwCanSetIdFilter(*handle , id , 0);
62 #endif
63  return true;
64 }
65 
66 bool Cfw2Can::canRead(CanBuffer &msgs,
67  unsigned int size,
68  unsigned int *read,
69  bool wait)
70 {
71  int res;
72  *read=size;
73  CFWCAN_MSG *tmp=reinterpret_cast<CFWCAN_MSG *>(msgs[0].getPointer());
74  if (wait)
75  res=cfwCanRead(*handle, tmp, read, 1);
76  else
77  res=cfwCanRead(*handle, tmp, read, 0);
78 
79  if(res!=0)
80  return false;
81 
82  return true;
83 }
84 
85 bool Cfw2Can::canWrite(const CanBuffer &msgs,
86  unsigned int size,
87  unsigned int *sent,
88  bool wait)
89 {
90  int res;
91  *sent=size;
92 
93  CanBuffer &buffer=const_cast<CanBuffer &>(msgs);
94  const CFWCAN_MSG *tmp=reinterpret_cast<const CFWCAN_MSG *>(buffer[0].getPointer());
95 
96  if (wait)
97  res=cfwCanWrite(*handle, const_cast<CFWCAN_MSG *>(tmp), sent, 1);
98  else
99  res=cfwCanWrite(*handle, const_cast<CFWCAN_MSG *>(tmp), sent, 0);
100 
101  if (res!=0)
102  return false;
103 
104  return true;
105 }
106 
107 bool Cfw2Can::open(yarp::os::Searchable &par)
108 {
109  int txQueueSize=0;
110  int rxQueueSize=0;
111  int netId =-1;
112  int txTimeout=0;
113  int rxTimeout=0;
114 
115  netId=par.check("CanDeviceNum", Value(-1), "numeric identifier of the can device").asInt32();
116  if (netId == -1) netId=par.check("canDeviceNum", Value(-1), "numeric identifier of the can device").asInt32();
117 
118  txTimeout=par.check("CanTxTimeout", Value(0), "timeout on transmission [ms]").asInt32();
119  if (txTimeout == 0) txTimeout=par.check("canTxTimeout", Value(0), "timeout on transmission [ms]").asInt32();
120 
121  rxTimeout=par.check("CanRxTimeout", Value(0), "timeout on receive when calling blocking read [ms]").asInt32() ;
122  if (rxTimeout == 0) rxTimeout=par.check("canRxTimeout", Value(0), "timeout on receive when calling blocking read [ms]").asInt32() ;
123 
124  int res = cfwCanOpen (netId, txQueueSize, rxQueueSize, txTimeout, rxTimeout, handle);
125  if (res != 0)
126  {
127  fprintf(stderr, "Cfw2Can::open() returning error %d\n", res);
128  return false;
129  }
130 #ifdef CFW2_HAS_FILTERS
131  int rtxEnable = cfwCanRtxEnable(*handle);
132  if( rtxEnable != 0) {
133  fprintf(stderr, "Cfw2Can::RtxEnable() returning error %d\n", rtxEnable);
134  return false;
135  }
136 #endif
137 
138  return true;
139 }
140 
142 {
143  int res;
144  if (!handle)
145  return false;
146 #ifdef CFW2_HAS_FILTERS
147  int rtxDisable = cfwCanRtxDisable(*handle);
148  if(rtxDisable != 0) {
149  fprintf(stderr, "Cfw2Can::RtxDisabling() returning error %d\n", rtxDisable);
150  return false;
151  }
152 #endif
153  res=cfwCanClose (*handle);
154 
155  if (res!=0)
156  return false;
157 
158  delete handle;
159  handle=0;
160  return true;
161 }
162 
163 /*
164 bool Cfw2Can::canGetErrors(CanErrors &err)
165 {
166  return true;
167 }
168 */
169 
171 #include <string.h>
172 
173 CanMessage &Cfw2CanMessage::operator=(const CanMessage &l)
174 {
175  // reinterpret_cast is used here for performance reasons, and it works
176  // with the current version of the iCub software. However it can hide bugs
177  // if this operator is used in a reckless way (i.e. assigning
178  // a CanMessage of different type to a Cfw2CanMessage).
179  // For more information, check https://github.com/robotology/icub-main/pull/82
180  const Cfw2CanMessage &tmp=reinterpret_cast<const Cfw2CanMessage &>(l);
181  memcpy(msg, tmp.msg, sizeof(CFWCAN_MSG));
182  return *this;
183 }
virtual CanMessage & operator=(const CanMessage &l)
This operator is defined in a general way, but it will crash if you try to assign to a Cfw2CanMessage...
Definition: Cfw2Can.cpp:173
virtual bool canRead(CanBuffer &msgs, unsigned int size, unsigned int *read, bool wait=false)
Definition: Cfw2Can.cpp:66
virtual bool close()
Definition: Cfw2Can.cpp:141
virtual bool canWrite(const CanBuffer &msgs, unsigned int size, unsigned int *sent, bool wait=false)
Definition: Cfw2Can.cpp:85
virtual bool open(yarp::os::Searchable &par)
Definition: Cfw2Can.cpp:107
virtual bool canIdAdd(unsigned int id)
Definition: Cfw2Can.cpp:46
virtual bool canGetBaudRate(unsigned int *rate)
Definition: Cfw2Can.cpp:39
virtual bool canIdDelete(unsigned int id)
Definition: Cfw2Can.cpp:56
virtual bool canSetBaudRate(unsigned int rate)
Definition: Cfw2Can.cpp:33
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
Definition: ethParser.cpp:92
static pcap_t * handle
fprintf(fid,'\n')