1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Romain Forlot" <romain.forlot@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
/********************************************************************************
*
* CanBus method implementation
*
*********************************************************************************/
CanBus_c::CanBus_c(afb_binding_interface *itf, const std:string& dev_name)
{
interface = itf;
deviceName = dev_name;
}
int CanBus_c::open()
{
const int canfd_on = 1;
struct ifreq ifr;
struct timeval timeout = {1, 0};
DEBUG(interface, "open_can_dev: CAN Handler socket : %d", socket);
if (socket >= 0)
close(socket);
socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (socket < 0)
{
ERROR(interface, "open_can_dev: socket could not be created");
}
else
{
/* Set timeout for read */
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
/* try to switch the socket into CAN_FD mode */
if (setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
{
NOTICE(interface, "open_can_dev: Can not switch into CAN Extended frame format.");
is_fdmode_on = false;
} else {
is_fdmode_on = true;
}
/* Attempts to open a socket to CAN bus */
strcpy(ifr.ifr_name, device);
if(ioctl(socket, SIOCGIFINDEX, &ifr) < 0)
ERROR(interface, "open_can_dev: ioctl failed");
else
{
txAddress.can_family = AF_CAN;
txAddress.can_ifindex = ifr.ifr_ifindex;
/* And bind it to txAddress */
if (bind(socket, (struct sockaddr *)&txAddress, sizeof(txAddress)) < 0)
{
ERROR(interface, "open_can_dev: bind failed");
}
else
{
fcntl(socket, F_SETFL, O_NONBLOCK);
return 0;
}
}
close(socket);
socket = -1;
}
return -1;
}
int CanBus_c::close()
{
close(socket);
socket = -1;
}
void CanBus_c::start_threads()
{
std::queue <CanMessage_c> can_message_q;
th_reading = std::thread(can_reader, interface, socket, can_message_q);
th_decoding = std::thread(can_decoder, interface, can_message_q, can_message_q);
th_pushing = std::thread(can_event_push, interface, can_message_q);
}
/*
* Send a can message from a CanMessage_c object.
*/
int CanBus_c::send_can_message(CanMessage_c can_msg)
{
int nbytes;
canfd_frame *f;
f = can_msg.convert_to_canfd_frame();
if(socket >= 0)
{
nbytes = sendto(socket, &f, sizeof(struct canfd_frame), 0,
(struct sockaddr*)&txAddress, sizeof(txAddress));
if (nbytes == -1)
{
ERROR(interface, "send_can_message: Sending CAN frame failed.");
return -1;
}
return nbytes;
}
else
{
ERROR(interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
open_can_dev();
}
return 0;
}
/*
* Get a CanMessage from can_message_q and return it
* then point to the next CanMessage in queue.
*
* Return the next queue element or NULL if queue is empty.
*/
CanMessage_c* CanBus_c::next_can_message()
{
if(! can_message_q.empty())
{
CanMessage_c can_msg = can_message_q.front();
can_message_q.pop()
return &can_msg;
}
return NULL;
}
void CanBus_c::insert_new_can_message(CanMessage_c *can_msg)
{
can_message_q.push(can_msg);
}
/********************************************************************************
*
* CanMessage method implementation
*
*********************************************************************************/
uint32_t CanMessage_c::get_id()
{
return id;
}
int CanMessage_c::get_format()
{
return format;
}
uint8_t CanMessage_c::get_data()
{
return data;
}
uint8_t CanMessage_c::get_lenght()
{
return lenght;
}
void CanMessage_c::set_id(uint32_t new_id)
{
switch(format):
case CanMessageFormat::SIMPLE:
id = new_id & CAN_SFF_MASK;
case CanMessageFormat::EXTENDED:
id = new_id & CAN_EFF_MASK;
default:
ERROR(interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
}
void CanMessage_c::set_format(CanMessageFormat new_format)
{
if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::EXTENDED)
format = new_format;
else
ERROR(interface, "ERROR: Can set format, wrong format chosen");
}
void CanMessage_c::set_data(uint8_t new_data)
{
data = new_data;
}
void CanMessage_c::set_lenght(uint8_t new_length)
{
lenght = new_lenght;
}
/*
* This is the preferred way to initialize a CanMessage object
* from a read canfd_frame message.
*
* params: canfd_frame pointer
*/
void CanMessage_c::convert_from_canfd_frame(canfd_frame *frame)
{
lenght = (canfd_frame->len > maxdlen) ? maxdlen : canfd_frame->len;
switch (canfd_frame->can_id):
case (canfd_frame->can_id & CAN_ERR_FLAG):
id = canfd_frame->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
break;
case (canfd_frame->can_id & CAN_EFF_FLAG):
id = canfd_frame->can_id & CAN_EFF_MASK;
format = CanMessageFormat::EXTENDED;
break;
default:
format = CanMessageFormat::STANDARD;
id = canfd_frame->can_id & CAN_SFF_MASK;
break;
if (sizeof(canfd_frame->data) <= sizeof(data))
{
memcpy(data, canfd_frame->data, lenght);
return 0;
} else if (sizeof(canfd_frame->data) >= CAN_MAX_DLEN)
ERROR(interface, "CanMessage_c: canfd_frame data too long to be stored into CanMessage object");
}
canfd_frame* convert_to_canfd_frame()
{
canfd_frame frame;
frame.id = can_msg.get_id();
frame.len = can_msg.get_lenght();
frame.data = can_msg.get_data();
return &frame;
}
|