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.
20 /********************************************************************************
22 * CanBus method implementation
24 *********************************************************************************/
26 can_bus_dev_t::can_bus_dev_t(afb_binding_interface *itf, const std:string &dev_name)
27 : interface{itf}, deviceName{dev_name}
31 int can_bus_dev_t::open()
33 const int canfd_on = 1;
35 struct timeval timeout = {1, 0};
37 DEBUG(interface_, "open_can_dev: CAN Handler socket : %d", can_socket_);
41 can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
44 ERROR(interface_, "open_can_dev: socket could not be created");
48 /* Set timeout for read */
49 ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
50 /* try to switch the socket into CAN_FD mode */
51 if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
53 NOTICE(interface_, "open_can_dev: Can not switch into CAN Extended frame format.");
54 is_fdmode_on_ = false;
59 /* Attempts to open a socket to CAN bus */
60 ::strcpy(ifr.ifr_name, device);
61 if(ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
62 ERROR(interface_, "open_can_dev: ioctl failed");
65 txAddress.can_family = AF_CAN;
66 txAddress.can_ifindex = ifr.ifr_ifindex;
68 /* And bind it to txAddress */
69 if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
71 ERROR(interface_, "open_can_dev: bind failed");
75 ::fcntl(can_socket_, F_SETFL, O_NONBLOCK);
84 int can_bus_dev_t::close()
91 canfd_frame can_bus_dev_t::read()
95 canfd_frame canfd_frame;
97 /* Test that socket is really opened */
100 ERROR(interface_, "read_can: Socket unavailable. Closing thread.");
104 nbytes = ::read(can_socket_, &canfd_frame, CANFD_MTU);
109 DEBUG(interface_, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
110 //maxdlen = CANFD_MAX_DLEN;
113 DEBUG(interface_, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
114 //maxdlen = CAN_MAX_DLEN;
117 if (errno == ENETDOWN)
118 ERROR(interface_, "read_can: %s interface down", device);
119 ERROR(interface_, "read_can: Error reading CAN bus");
120 ::memset(&canfd_frame, 0, sizeof(canfd_frame));
128 * Return is_running_ bool
130 bool can_bus_dev_t::is_running()
135 can_bus_t::can_bus_t(afb_binding_interface *itf, const std:string &dev_name)
140 void can_bus_t::start_threads()
142 th_reading_ = std::thread(can_reader, interface, socket, can_message_q_);
145 th_decoding_ = std::thread(can_decoder, interface, can_message_q, can_message_q_);
146 th_pushing_ = std::thread(can_event_push, interface, can_message_q_);
150 * Get a CanMessage from can_message_q and return it
151 * then point to the next CanMessage in queue.
153 * Return the next queue element or NULL if queue is empty.
155 can_message_t can_bus_dev_t::next_can_message()
157 if(! can_message_q_.empty())
159 can_message_t can_msg = can_message_q_.front();
163 has_can_message_ = false;
167 * @return has_can_message_ bool
169 bool can_bus_dev_t::has_can_message() const
171 return has_can_message_;
174 void can_bus_dev_t::push_new_can_message(const can_message_t& can_msg)
176 can_message_q_.push(can_msg);
180 * Send a can message from a can_message_t object.
182 int can_bus_t::send_can_message(can_message_t &can_msg)
187 f = can_msg.convert_to_canfd_frame();
191 nbytes = ::sendto(can_socket_, &f, sizeof(struct canfd_frame), 0,
192 (struct sockaddr*)&txAddress, sizeof(txAddress));
196 ERROR(interface_, "send_can_message: Sending CAN frame failed.");
203 ERROR(interface_, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
210 * Get a VehicleMessage from vehicle_message_q and return it
211 * then point to the next VehicleMessage in queue.
213 * Return the next queue element or NULL if queue is empty.
215 openxc_VehicleMessage* can_bus_t::next_vehicle_message()
217 if(! vehicle_message_q_.empty())
219 openxc_VehicleMessage v_msg = vehicle_message_q_.front();
220 vehicle_message_q_.pop();
224 has_vehicle_message_ = false;
227 void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
229 vehicle_message_q_.push(v_msg);
230 has_vehicle_message_ = true;
234 * Return has_can_message_ bool
236 bool can_bus_t::has_vehicle_message() const
238 return has_vehicle_message_;
241 /********************************************************************************
243 * CanMessage method implementation
245 *********************************************************************************/
247 can_message_t::can_message_t(afb_binding_interface *itf)
251 uint32_t can_message_t::get_id() const
253 (id_ != 0) ? return id_ : return 0;
256 int can_message_t::get_format() const
258 (format_ != CanMessageFormat::SIMPLE || format_ != CanMessageFormat::EXTENDED) return -1 : return format_;
261 uint8_t can_message_t::get_data() const
265 uint8_t can_message_t::get_lenght() const
270 void can_message_t::set_id(uint32_t &new_id)
273 case CanMessageFormat::SIMPLE:
274 id = new_id & CAN_SFF_MASK;
275 case CanMessageFormat::EXTENDED:
276 id = new_id & CAN_EFF_MASK;
278 ERROR(interface_, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
281 void can_message_t::set_format(CanMessageFormat &new_format)
283 if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::EXTENDED)
286 ERROR(interface_, "ERROR: Can set format, wrong format chosen");
289 void can_message_t::set_data(uint8_t &new_data)
291 ::memcpy(data_, new_data, new_data.size());
292 lenght_ = new_data(size);
296 * This is the preferred way to initialize a CanMessage object
297 * from a read canfd_frame message.
299 * params: canfd_frame pointer
301 void can_message_t::convert_from_canfd_frame(canfd_frame &frame)
303 lenght_ = (frame.len > CAN_MAX_DLEN) ? CAN_MAX_DLEN : frame.len;
304 lenght_ = (frame.len > CANFD_MAX_DLEN) ? CANFD_MAX_DLEN : frame.len;
306 switch (frame.can_id):
307 case (frame.can_id & CAN_ERR_FLAG):
308 id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
310 case (frame.can_id & CAN_EFF_FLAG):
311 id_ = frame.can_id & CAN_EFF_MASK;
312 format_ = CanMessageFormat::EXTENDED;
315 format_ = CanMessageFormat::STANDARD;
316 id_ = frame.can_id & CAN_SFF_MASK;
319 if (sizeof(frame.data) <= data_.size())
321 ::memcpy(data_, canfd_frame.data, lenght_);
323 } else if (sizeof(frame.data) >= CAN_MAX_DLEN)
324 ERROR(interface_, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
327 canfd_frame convert_to_canfd_frame()
331 frame.can_id = get_id();
332 frame.len = get_lenght();
333 ::memcpy(frame.data, get_data(), lenght_);