linuxcan.cpp
Go to the documentation of this file.
1 /*
2 * Unpublished Copyright (c) 2009-2017 AutonomouStuff, LLC, All Rights Reserved.
3 *
4 * This file is part of the Kvaser ROS 1.0 driver which is released under the MIT license.
5 * See file LICENSE included with this software or go to https://opensource.org/licenses/MIT for full license details.
6 */
7 
8 #include <kvaser_interface.h>
9 #include <canlib.h>
10 
11 using namespace std;
12 using namespace AS::CAN;
13 
14 //Default constructor.
15 KvaserCan::KvaserCan() :
16  handle(NULL)
17 {
18  handle = malloc(sizeof(canHandle));
19 }
20 
21 //Default destructor.
23 {
24  if (handle != NULL)
25  {
26  close();
27  }
28 
29  free(handle);
30 }
31 
33  const int& circuit_id,
34  const int& bitrate,
35  const bool& echo_on)
36 {
37  if (handle == NULL)
38  {
39  return INIT_FAILED;
40  }
41 
42  if (!on_bus)
43  {
44  canHandle *h = (canHandle *) handle;
45 
46  int numChan;
47  if (canGetNumberOfChannels(&numChan) != canOK)
48  {
49  return INIT_FAILED;
50  }
51 
52  unsigned int serial[2];
53  unsigned int channel_number;
54  int channel = -1;
55 
56  for (int idx = 0; idx < numChan; idx++)
57  {
58  if (canGetChannelData(idx, canCHANNELDATA_CARD_SERIAL_NO, &serial, sizeof(serial)) == canOK)
59  {
60  if (serial[0] == (unsigned int) hardware_id)
61  {
62  if (canGetChannelData(idx, canCHANNELDATA_CHAN_NO_ON_CARD, &channel_number, sizeof(channel_number)) == canOK)
63  {
64  if (channel_number == (unsigned int) circuit_id)
65  {
66  channel = idx;
67  }
68  }
69  }
70  }
71  }
72 
73  if (channel == -1)
74  {
75  return BAD_PARAM;
76  }
77 
78  // Open channel
79  *h = canOpenChannel(channel, canOPEN_ACCEPT_VIRTUAL);
80  if (*h < 0)
81  {
82  return INIT_FAILED;
83  }
84 
85  // Set bit rate and other parameters
86  long freq;
87  switch (bitrate)
88  {
89  case 125000: freq = canBITRATE_125K; break;
90  case 250000: freq = canBITRATE_250K; break;
91  case 500000: freq = canBITRATE_500K; break;
92  case 1000000: freq = canBITRATE_1M; break;
93  default:
94  {
95  return BAD_PARAM;
96  }
97  }
98 
99  if (canSetBusParams(*h, freq, 0, 0, 0, 0, 0) < 0)
100  {
101  return BAD_PARAM;
102  }
103 
104  // Linuxcan defaults to echo on, so if you've opened the same can channel
105  // from multiple interfaces they will receive the messages that each other
106  // send. Turn it off here if desired.
107  if (!echo_on)
108  {
109  unsigned char off = 0;
110  canIoCtl(*h, canIOCTL_SET_LOCAL_TXECHO, &off, 1);
111  }
112 
113  // Set output control
114  canSetBusOutputControl(*h, canDRIVER_NORMAL);
115  canBusOn(*h);
116  on_bus = true;
117  }
118 
119  return OK;
120 }
121 
123 {
124  if (handle == NULL)
125  {
126  return false;
127  }
128  else
129  {
130  if (on_bus)
131  {
132  return true;
133  }
134  else
135  {
136  return false;
137  }
138  }
139 }
140 
142 {
143  if (handle == NULL)
144  {
145  return INIT_FAILED;
146  }
147 
148  canHandle *h = (canHandle *) handle;
149 
150  // Close the channel
151  if (canClose(*h) != canOK)
152  {
153  return CLOSE_FAILED;
154  }
155 
156  on_bus = false;
157 
158  return OK;
159 }
160 
162  unsigned char *msg,
163  unsigned int *size,
164  bool *extended,
165  unsigned long *time)
166 {
167  if (handle == NULL)
168  {
169  return INIT_FAILED;
170  }
171 
172  canHandle *h = (canHandle *) handle;
173 
174  bool done = false;
175  return_statuses ret_val = INIT_FAILED;
176  unsigned int flag = 0;
177 
178  while (!done)
179  {
180  canStatus ret = canRead(*h, id, msg, size, &flag, time);
181 
182  if (ret == canERR_NOTINITIALIZED)
183  {
184  ret_val = CHANNEL_CLOSED;
185  on_bus = false;
186  done = true;
187  }
188  else if (ret == canERR_NOMSG)
189  {
190  ret_val = NO_MESSAGES_RECEIVED;
191  done = true;
192  }
193  else if (ret != canOK)
194  {
195  ret_val = READ_FAILED;
196  done = true;
197  }
198  else if (!(flag & 0xF9))
199  {
200  // Was a received message with actual data
201  ret_val = OK;
202  done = true;
203  }
204  // Else a protocol message, such as a TX ACK, was received
205  // Keep looping until one of the other conditions above is met
206  }
207 
208  if (ret_val == OK)
209  {
210  *extended = ((flag & canMSG_EXT) > 0);
211  }
212 
213  return ret_val;
214 }
215 
217  unsigned char *msg,
218  const unsigned int& size,
219  const bool& extended)
220 {
221  if (handle == NULL)
222  {
223  return INIT_FAILED;
224  }
225 
226  canHandle *h = (canHandle *) handle;
227 
228  unsigned int flag;
229 
230  if (extended)
231  {
232  flag = canMSG_EXT;
233  }
234  else
235  {
236  flag = canMSG_STD;
237  }
238 
239  canStatus ret = canWrite(*h, id, msg, size, flag);
240 
241  return (ret == canOK) ? OK : WRITE_FAILED;
242 }
return_statuses close()
Definition: linuxcan.cpp:141
int hardware_id
return_statuses open(const int &hardware_id, const int &circuit_id, const int &bitrate, const bool &echo_on=true)
Definition: linuxcan.cpp:32
int circuit_id
return_statuses read(long *id, unsigned char *msg, unsigned int *size, bool *extended, unsigned long *time)
Definition: linuxcan.cpp:161
return_statuses write(const long &id, unsigned char *msg, const unsigned int &size, const bool &extended)
Definition: linuxcan.cpp:216


kvaser_interface
Author(s): Joshua Whitley , Daniel Stanek
autogenerated on Wed Jun 19 2019 19:36:17