2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "can-utils.hpp"
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <json-c/json.h>
30 #include <linux/can/raw.h>
34 #include <afb/afb-binding.h>
37 /********************************************************************************
39 * CanMessage method implementation
41 *********************************************************************************/
43 can_message_t::can_message_t()
44 : id_{0}, length_{0}, format_{CanMessageFormat::ERROR}, data_{0,0,0,0,0,0,0,0}
47 uint32_t can_message_t::get_id() const
52 int can_message_t::get_format() const
54 if (format_ != CanMessageFormat::STANDARD || format_ != CanMessageFormat::EXTENDED)
55 return CanMessageFormat::ERROR;
59 const uint8_t* can_message_t::get_data() const
63 uint8_t can_message_t::get_length() const
68 bool can_message_t::is_correct_to_send()
70 if (id_ != 0 && length_ != 0 && format_ != CanMessageFormat::ERROR)
73 for(i=0;i<CAN_MESSAGE_SIZE;i++)
80 void can_message_t::set_id(const uint32_t new_id)
84 case CanMessageFormat::STANDARD:
85 id_ = new_id & CAN_SFF_MASK;
87 case CanMessageFormat::EXTENDED:
88 id_ = new_id & CAN_EFF_MASK;
91 ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
96 void can_message_t::set_format(const CanMessageFormat new_format)
98 if(new_format == CanMessageFormat::STANDARD || new_format == CanMessageFormat::EXTENDED)
101 ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
104 void can_message_t::set_data(const uint8_t new_data)
106 if ((sizeof(new_data) / sizeof(uint8_t) > CAN_MESSAGE_SIZE))
107 ERROR(binder_interface, "Can set data, your data array is too big");
110 ::memcpy(&data_, &new_data, sizeof(new_data));
111 length_ = sizeof(new_data);
115 void can_message_t::convert_from_canfd_frame(const canfd_frame& frame)
117 length_ = (frame.len > CAN_MAX_DLEN) ? (uint8_t)CAN_MAX_DLEN : frame.len;
118 length_ = (frame.len > CANFD_MAX_DLEN) ? (uint8_t)CANFD_MAX_DLEN : frame.len;
120 if (frame.can_id & CAN_ERR_FLAG)
122 id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
123 format_ = CanMessageFormat::ERROR;
125 else if (frame.can_id & CAN_EFF_FLAG)
127 id_ = frame.can_id & CAN_EFF_MASK;
128 format_ = CanMessageFormat::EXTENDED;
132 id_ = frame.can_id & CAN_SFF_MASK;
133 format_ = CanMessageFormat::STANDARD;
136 if (sizeof(frame.data) <= sizeof(data_))
137 ::memcpy(&data_, frame.data, length_);
138 else if (sizeof(frame.data) >= CAN_MAX_DLEN)
139 ERROR(binder_interface, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
142 canfd_frame can_message_t::convert_to_canfd_frame()
146 if(is_correct_to_send())
148 frame.can_id = get_id();
149 frame.len = get_length();
150 ::memcpy(frame.data, get_data(), length_);
153 ERROR(binder_interface, "can_message_t not correctly initialized to be sent");
158 /********************************************************************************
160 * can_bus_t method implementation
162 *********************************************************************************/
164 can_bus_t::can_bus_t(int& conf_file)
165 : conf_file_{conf_file}
169 void can_bus_t::start_threads()
171 th_decoding_ = std::thread(can_decode_message, std::ref(*this));
173 th_pushing_ = std::thread(can_event_push, std::ref(*this));
177 void can_bus_t::stop_threads()
179 is_decoding_ = false;
183 bool can_bus_t::is_decoding()
188 bool can_bus_t::is_pushing()
193 int can_bus_t::init_can_dev()
195 std::vector<std::string> devices_name;
199 devices_name = read_conf();
201 if (! devices_name.empty())
203 t = devices_name.size();
206 for(const auto& device : devices_name)
208 can_bus_dev_t can_bus_device_handler(device);
209 if (can_bus_device_handler.open())
212 ERROR(binder_interface, "Can't open device %s", device);
213 can_bus_device_handler.start_reading(std::ref(*this));
216 NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
219 ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read from configuration file. Did you specify canbus JSON object ?");
223 std::vector<std::string> can_bus_t::read_conf()
225 std::vector<std::string> ret;
226 json_object *jo, *canbus;
229 FILE *fd = fdopen(conf_file_, "r");
232 std::string fd_conf_content;
233 std::fseek(fd, 0, SEEK_END);
234 fd_conf_content.resize(std::ftell(fd));
236 std::fread(&fd_conf_content[0], 1, fd_conf_content.size(), fd);
239 jo = json_tokener_parse(fd_conf_content.c_str());
241 if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
243 ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
246 else if (json_object_get_type(canbus) != json_type_array)
247 ret.push_back(json_object_get_string(canbus));
250 n = json_object_array_length(canbus);
251 for (i = 0 ; i < n ; i++)
252 ret.push_back(json_object_get_string(json_object_array_get_idx(canbus, i)));
256 ERROR(binder_interface, "Problem at reading the conf file");
261 std::condition_variable& can_bus_t::get_new_can_message()
263 return new_can_message_;
266 std::mutex& can_bus_t::get_can_message_mutex()
268 return can_message_mutex_;
271 std::condition_variable& can_bus_t::get_new_decoded_can_message()
273 return new_decoded_can_message_;
276 std::mutex& can_bus_t::get_decoded_can_message_mutex()
278 return decoded_can_message_mutex_;
281 can_message_t can_bus_t::next_can_message()
283 can_message_t can_msg;
285 if(!can_message_q_.empty())
287 can_msg = can_message_q_.front();
288 can_message_q_.pop();
289 DEBUG(binder_interface, "next_can_message: Here is the next can message : id %d, length %d", can_msg.get_id(), can_msg.get_length());
293 NOTICE(binder_interface, "next_can_message: End of can message queue");
294 has_can_message_ = false;
298 void can_bus_t::push_new_can_message(const can_message_t& can_msg)
300 can_message_q_.push(can_msg);
303 bool can_bus_t::has_can_message() const
305 return has_can_message_;
308 openxc_VehicleMessage can_bus_t::next_vehicle_message()
310 openxc_VehicleMessage v_msg;
312 if(! vehicle_message_q_.empty())
314 v_msg = vehicle_message_q_.front();
315 vehicle_message_q_.pop();
316 DEBUG(binder_interface, "next_vehicle_message: next vehicle message poped");
320 NOTICE(binder_interface, "next_vehicle_message: End of vehicle message queue");
321 has_vehicle_message_ = false;
325 void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
327 vehicle_message_q_.push(v_msg);
328 has_vehicle_message_ = true;
331 bool can_bus_t::has_vehicle_message() const
333 return has_vehicle_message_;
336 /********************************************************************************
338 * can_bus_dev_t method implementation
340 *********************************************************************************/
342 can_bus_dev_t::can_bus_dev_t(const std::string &dev_name)
343 : device_name_{dev_name}
347 int can_bus_dev_t::open()
349 const int canfd_on = 1;
351 struct timeval timeout = {1, 0};
353 DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_);
354 if (can_socket_ >= 0)
357 can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
360 ERROR(binder_interface, "socket could not be created");
364 /* Set timeout for read */
365 ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
366 /* try to switch the socket into CAN_FD mode */
367 if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
369 NOTICE(binder_interface, "Can not switch into CAN Extended frame format.");
370 is_fdmode_on_ = false;
372 is_fdmode_on_ = true;
375 /* Attempts to open a socket to CAN bus */
376 ::strcpy(ifr.ifr_name, device_name_.c_str());
377 if(::ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
378 ERROR(binder_interface, "ioctl failed");
381 txAddress_.can_family = AF_CAN;
382 txAddress_.can_ifindex = ifr.ifr_ifindex;
384 /* And bind it to txAddress */
385 if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
386 ERROR(binder_interface, "Bind failed");
395 int can_bus_dev_t::close()
397 ::close(can_socket_);
402 canfd_frame can_bus_dev_t::read()
406 canfd_frame canfd_frame;
408 /* Test that socket is really opened */
411 ERROR(binder_interface, "read_can: Socket unavailable. Closing thread.");
415 nbytes = ::read(can_socket_, &canfd_frame, CANFD_MTU);
420 DEBUG(binder_interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
421 //maxdlen = CANFD_MAX_DLEN;
424 DEBUG(binder_interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
425 //maxdlen = CAN_MAX_DLEN;
428 if (errno == ENETDOWN)
429 ERROR(binder_interface, "read_can: %s binder_interface down", device_name_);
430 ERROR(binder_interface, "read_can: Error reading CAN bus");
431 ::memset(&canfd_frame, 0, sizeof(canfd_frame));
439 void can_bus_dev_t::start_reading(can_bus_t& can_bus)
441 th_reading_ = std::thread(can_reader, std::ref(*this), std::ref(can_bus));
446 * Return is_running_ bool
448 bool can_bus_dev_t::is_running()
453 int can_bus_dev_t::send_can_message(can_message_t& can_msg)
458 f = can_msg.convert_to_canfd_frame();
462 nbytes = ::sendto(can_socket_, &f, sizeof(struct canfd_frame), 0,
463 (struct sockaddr*)&txAddress_, sizeof(txAddress_));
466 ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
473 ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");