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"
20 /********************************************************************************
22 * CanMessage method implementation
24 *********************************************************************************/
26 can_message_t::can_message_t()
27 : id_{0}, length_{0}, format_{CanMessageFormat::ERROR}, data_{0,0,0,0,0,0,0,0}
30 uint32_t can_message_t::get_id() const
35 int can_message_t::get_format() const
37 if (format_ != CanMessageFormat::STANDARD || format_ != CanMessageFormat::EXTENDED)
38 return CanMessageFormat::ERROR;
42 const uint8_t* can_message_t::get_data() const
46 uint8_t can_message_t::get_length() const
51 bool can_message_t::is_correct_to_send()
53 if (id_ != 0 && length_ != 0 && format_ != CanMessageFormat::ERROR)
56 for(i=0;i<CAN_MESSAGE_SIZE;i++)
63 void can_message_t::set_id(const uint32_t new_id)
67 case CanMessageFormat::STANDARD:
68 id_ = new_id & CAN_SFF_MASK;
70 case CanMessageFormat::EXTENDED:
71 id_ = new_id & CAN_EFF_MASK;
74 ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
79 void can_message_t::set_format(const CanMessageFormat new_format)
81 if(new_format == CanMessageFormat::STANDARD || new_format == CanMessageFormat::EXTENDED)
84 ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
87 void can_message_t::set_data(const uint8_t new_data)
89 if ((sizeof(new_data) / sizeof(uint8_t) > CAN_MESSAGE_SIZE))
90 ERROR(binder_interface, "Can set data, your data array is too big");
93 ::memcpy(&data_, &new_data, sizeof(new_data));
94 length_ = sizeof(new_data);
98 void can_message_t::convert_from_canfd_frame(const canfd_frame& frame)
100 length_ = (frame.len > CAN_MAX_DLEN) ? (uint8_t)CAN_MAX_DLEN : frame.len;
101 length_ = (frame.len > CANFD_MAX_DLEN) ? (uint8_t)CANFD_MAX_DLEN : frame.len;
103 if (frame.can_id & CAN_ERR_FLAG)
105 id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
106 format_ = CanMessageFormat::ERROR;
108 else if (frame.can_id & CAN_EFF_FLAG)
110 id_ = frame.can_id & CAN_EFF_MASK;
111 format_ = CanMessageFormat::EXTENDED;
115 id_ = frame.can_id & CAN_SFF_MASK;
116 format_ = CanMessageFormat::STANDARD;
119 if (sizeof(frame.data) <= sizeof(data_))
120 ::memcpy(&data_, frame.data, length_);
121 else if (sizeof(frame.data) >= CAN_MAX_DLEN)
122 ERROR(binder_interface, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
125 canfd_frame can_message_t::convert_to_canfd_frame()
129 if(is_correct_to_send())
131 frame.can_id = get_id();
132 frame.len = get_length();
133 ::memcpy(frame.data, get_data(), length_);
136 ERROR(binder_interface, "can_message_t not correctly initialized to be sent");
141 /********************************************************************************
143 * can_bus_dev_t method implementation
145 *********************************************************************************/
147 can_bus_dev_t::can_bus_dev_t(const std::string &dev_name)
148 : device_name_{dev_name}
152 int can_bus_dev_t::open()
154 const int canfd_on = 1;
156 struct timeval timeout = {1, 0};
158 DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_);
159 if (can_socket_ >= 0)
162 can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
165 ERROR(binder_interface, "socket could not be created");
169 /* Set timeout for read */
170 ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
171 /* try to switch the socket into CAN_FD mode */
172 if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
174 NOTICE(binder_interface, "Can not switch into CAN Extended frame format.");
175 is_fdmode_on_ = false;
177 is_fdmode_on_ = true;
180 /* Attempts to open a socket to CAN bus */
181 ::strcpy(ifr.ifr_name, device_name_.c_str());
182 if(::ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
183 ERROR(binder_interface, "ioctl failed");
186 txAddress_.can_family = AF_CAN;
187 txAddress_.can_ifindex = ifr.ifr_ifindex;
189 /* And bind it to txAddress */
190 if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
192 ERROR(binder_interface, "Bind failed");
196 ::fcntl(can_socket_, F_SETFL, O_NONBLOCK);
205 int can_bus_dev_t::close()
207 ::close(can_socket_);
212 canfd_frame can_bus_dev_t::read()
216 canfd_frame canfd_frame;
218 /* Test that socket is really opened */
221 ERROR(binder_interface, "read_can: Socket unavailable. Closing thread.");
225 nbytes = ::read(can_socket_, &canfd_frame, CANFD_MTU);
230 DEBUG(binder_interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
231 //maxdlen = CANFD_MAX_DLEN;
234 DEBUG(binder_interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
235 //maxdlen = CAN_MAX_DLEN;
238 if (errno == ENETDOWN)
239 ERROR(binder_interface, "read_can: %s binder_interface down", device_name_);
240 ERROR(binder_interface, "read_can: Error reading CAN bus");
241 ::memset(&canfd_frame, 0, sizeof(canfd_frame));
249 void can_bus_dev_t::start_reading(can_bus_t& can_bus)
251 th_reading_ = std::thread(can_reader, std::ref(*this), std::ref(can_bus));
257 * Return is_running_ bool
259 bool can_bus_dev_t::is_running()
264 int can_bus_dev_t::send_can_message(can_message_t& can_msg)
269 f = can_msg.convert_to_canfd_frame();
273 nbytes = ::sendto(can_socket_, &f, sizeof(struct canfd_frame), 0,
274 (struct sockaddr*)&txAddress_, sizeof(txAddress_));
277 ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
284 ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
289 /********************************************************************************
291 * can_bus_t method implementation
293 *********************************************************************************/
295 can_bus_t::can_bus_t(int& conf_file)
296 : conf_file_{conf_file}
300 void can_bus_t::start_threads()
302 th_decoding_ = std::thread(can_decode_message, std::ref(*this));
303 th_pushing_ = std::thread(can_event_push, std::ref(*this));
307 int can_bus_t::init_can_dev()
309 std::vector<std::string> devices_name;
313 devices_name = read_conf();
315 if (! devices_name.empty())
317 t = devices_name.size();
320 for(const auto& device : devices_name)
322 can_bus_dev_t can_bus_device_handler(device);
323 can_bus_device_handler.open();
324 can_bus_device_handler.start_reading(std::ref(*this));
328 NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
331 ERROR(binder_interface, "init_can_dev: Error at CAN device initialization.");
335 std::vector<std::string> can_bus_t::read_conf()
337 std::vector<std::string> ret;
338 json_object *jo, *canbus;
341 FILE *fd = fdopen(conf_file_, "r");
344 std::string fd_conf_content;
345 std::fseek(fd, 0, SEEK_END);
346 fd_conf_content.resize(std::ftell(fd));
348 std::fread(&fd_conf_content[0], 1, fd_conf_content.size(), fd);
351 jo = json_tokener_parse(fd_conf_content.c_str());
353 if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
355 ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
358 else if (json_object_get_type(canbus) != json_type_array)
359 ret.push_back(json_object_get_string(canbus));
362 n = json_object_array_length(canbus);
363 for (i = 0 ; i < n ; i++)
364 ret.push_back(json_object_get_string(json_object_array_get_idx(canbus, i)));
368 ERROR(binder_interface, "Problem at reading the conf file");
373 can_message_t can_bus_t::next_can_message()
375 can_message_t can_msg;
377 if(!can_message_q_.empty())
379 can_msg = can_message_q_.front();
380 can_message_q_.pop();
381 DEBUG(binder_interface, "next_can_message: Here is the next can message : id %d, length %d", can_msg.get_id(), can_msg.get_length());
385 NOTICE(binder_interface, "next_can_message: End of can message queue");
386 has_can_message_ = false;
390 void can_bus_t::push_new_can_message(const can_message_t& can_msg)
392 can_message_q_.push(can_msg);
395 bool can_bus_t::has_can_message() const
397 return has_can_message_;
400 openxc_VehicleMessage can_bus_t::next_vehicle_message()
402 openxc_VehicleMessage v_msg;
404 if(! vehicle_message_q_.empty())
406 v_msg = vehicle_message_q_.front();
407 vehicle_message_q_.pop();
408 DEBUG(binder_interface, "next_vehicle_message: next vehicle message poped");
412 NOTICE(binder_interface, "next_vehicle_message: End of vehicle message queue");
413 has_vehicle_message_ = false;
417 void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
419 vehicle_message_q_.push(v_msg);
420 has_vehicle_message_ = true;
423 bool can_bus_t::has_vehicle_message() const
425 return has_vehicle_message_;