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 * can_bus_dev_t method implementation
24 *********************************************************************************/
27 can_bus_dev_t::can_bus_dev_t(afb_binding_interface *itf, const std:string &dev_name)
28 : device_name_{dev_name}
32 int can_bus_dev_t::open()
34 const int canfd_on = 1;
36 struct timeval timeout = {1, 0};
38 DEBUG(interface_, "open_can_dev: CAN Handler socket : %d", can_socket_);
42 can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
45 ERROR(interface_, "open_can_dev: socket could not be created");
49 /* Set timeout for read */
50 ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
51 /* try to switch the socket into CAN_FD mode */
52 if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
54 NOTICE(interface_, "open_can_dev: Can not switch into CAN Extended frame format.");
55 is_fdmode_on_ = false;
60 /* Attempts to open a socket to CAN bus */
61 ::strcpy(ifr.ifr_name, device);
62 if(ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
63 ERROR(interface_, "open_can_dev: ioctl failed");
66 txAddress.can_family = AF_CAN;
67 txAddress.can_ifindex = ifr.ifr_ifindex;
69 /* And bind it to txAddress */
70 if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
72 ERROR(interface_, "open_can_dev: bind failed");
76 ::fcntl(can_socket_, F_SETFL, O_NONBLOCK);
85 int can_bus_dev_t::close()
92 canfd_frame can_bus_dev_t::read()
96 canfd_frame canfd_frame;
98 /* Test that socket is really opened */
101 ERROR(interface_, "read_can: Socket unavailable. Closing thread.");
105 nbytes = ::read(can_socket_, &canfd_frame, CANFD_MTU);
110 DEBUG(interface_, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
111 //maxdlen = CANFD_MAX_DLEN;
114 DEBUG(interface_, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
115 //maxdlen = CAN_MAX_DLEN;
118 if (errno == ENETDOWN)
119 ERROR(interface_, "read_can: %s interface down", device);
120 ERROR(interface_, "read_can: Error reading CAN bus");
121 ::memset(&canfd_frame, 0, sizeof(canfd_frame));
129 * @brief start reading threads and set flag is_running_
132 void can_bus_dev_t::start_reading()
134 th_reading_ = std::thread(can_reader, this);
139 * Return is_running_ bool
141 bool can_bus_dev_t::is_running()
147 * @brief: Get a can_message_t from can_message_q and return it
148 * then point to the next can_message_t in queue.
150 * @return the next queue element or NULL if queue is empty.
152 can_message_t can_bus_dev_t::next_can_message()
154 if(! can_message_q_.empty())
156 can_message_t can_msg = can_message_q_.front();
161 has_can_message_ = false;
165 * @brief Append a new element to the can message queue and set
166 * has_can_message_ boolean to true
168 * @params[const can_message_t& can_msg] the can_message_t to append
171 void can_bus_dev_t::push_new_can_message(const can_message_t& can_msg)
173 can_message_q_.push(can_msg);
177 * @brief Flag that let you know when can message queue is exhausted
179 * @return[bool] has_can_message_ bool
181 bool can_bus_dev_t::has_can_message() const
183 return has_can_message_;
186 /********************************************************************************
188 * can_bus_t method implementation
190 *********************************************************************************/
192 can_bus_t::can_bus_t(afb_binding_interface *itf, std::ifstream& conf_file)
193 : interface{itf}, conf_file_{conf_file}
198 * @brief start threads relative to the can bus: decoding and pushing
199 * as the reading is handled by can_bus_dev_t object
202 void can_bus_t::start_threads()
204 th_decoding_ = std::thread(can_decoder, this);
205 th_pushing_ = std::thread(can_event_push, this);
210 * @brief Initialize as many as can_bus_dev_t objects with their respective reading thread
212 * params[std::ifstream& conf_file] conf_file ifstream to the JSON configuration
213 * file located at the rootdir of the binding
217 std::vector<std::string> devices_name;
220 devices_name = read_conf(conf_file_);
222 t = devices_name.size();
225 for(const auto& device : devices_name)
227 can_bus_dev_t(device);
231 NOTICE(interface_, "Initialized %d/%d can bus device(s)", i, t);
235 * @brief Read the conf file and extract device name
237 * @params[std::ifstream& conf_file] conf_file JSON configuration
238 * file located at the rootdir of the binding
240 * @return[std:vector<std::string>] return a vector of device name
242 std::vector<std::string> read_conf(std::ifstream& conf_file)
244 std::vector<std::string> ret;
245 std::string fd_conf_content;
246 json_object jo, canbus;
249 /* Open JSON conf file */
252 conf_file.seekg(0, std::ios::end);
253 conf_file.resize(conf_file.tellg());
254 conf_file.seekg(0, std::ios::beg);
255 conf_file.read(&fd_conf_content[0], fd_conf_content.size());
258 jo = json_tokener_parse(&fd_conf_content);
260 if (jo == NULL || !json_object_object_get_ex(&jo, "canbus", &&canbus))
261 ERROR(interface_, "Can't find canbus node in the configuration file. Please review it.");
262 else if (json_object_get_type(canbus) != json_type_array)
263 ret.push_back(json_object_get_string(a));
266 n = json_object_array_length(a);
268 for (i = 0 ; i < n ; i++)
269 ret.push_back(json_object_get_string(json_object_array_get_idx(a, i)));
275 ERROR(interface_, "Problem at reading the conf file");
281 * @brief Send a can message from a can_message_t object.
282 * TODO: specify which can_dev to use as we can use many
284 * params[const can_message_t& can_msg] the can message object to send
287 int can_bus_t::send_can_message(const can_message_t &can_msg)
292 f = can_msg.convert_to_canfd_frame();
296 nbytes = ::sendto(can_socket_, &f, sizeof(struct canfd_frame), 0,
297 (struct sockaddr*)&txAddress, sizeof(txAddress));
301 ERROR(interface_, "send_can_message: Sending CAN frame failed.");
308 ERROR(interface_, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
315 * @brief: Get a VehicleMessage from vehicle_message_q and return it
316 * then point to the next VehicleMessage in queue.
318 * @return the next queue element or NULL if queue is empty.
320 openxc_VehicleMessage* can_bus_t::next_vehicle_message()
322 if(! vehicle_message_q_.empty())
324 openxc_VehicleMessage v_msg = vehicle_message_q_.front();
325 vehicle_message_q_.pop();
329 has_vehicle_message_ = false;
333 * @brief Append a new element to the vehicle message queue and set
334 * has_vehicle_message_ boolean to true
336 * @params[const openxc_VehicleMessage& v_msg] the openxc_VehicleMessage to append
339 void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
341 vehicle_message_q_.push(v_msg);
342 has_vehicle_message_ = true;
346 * @brief Flag that let you know when vehicle message queue is exhausted
348 * @return[bool] has_vehicle_message_ bool
350 bool can_bus_t::has_vehicle_message() const
352 return has_vehicle_message_;
355 /********************************************************************************
357 * CanMessage method implementation
359 *********************************************************************************/
361 can_message_t::can_message_t(afb_binding_interface *itf)
365 uint32_t can_message_t::get_id() const
367 (id_ != 0) ? return id_ : return 0;
370 int can_message_t::get_format() const
372 (format_ != CanMessageFormat::SIMPLE || format_ != CanMessageFormat::EXTENDED) return -1 : return format_;
375 uint8_t can_message_t::get_data() const
379 uint8_t can_message_t::get_lenght() const
384 void can_message_t::set_id(uint32_t &new_id)
387 case CanMessageFormat::SIMPLE:
388 id = new_id & CAN_SFF_MASK;
389 case CanMessageFormat::EXTENDED:
390 id = new_id & CAN_EFF_MASK;
392 ERROR(interface_, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
395 void can_message_t::set_format(CanMessageFormat &new_format)
397 if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::EXTENDED)
400 ERROR(interface_, "ERROR: Can set format, wrong format chosen");
403 void can_message_t::set_data(uint8_t &new_data)
405 ::memcpy(data_, new_data, new_data.size());
406 lenght_ = new_data(size);
410 * This is the preferred way to initialize a CanMessage object
411 * from a read canfd_frame message.
413 * params: canfd_frame pointer
415 void can_message_t::convert_from_canfd_frame(canfd_frame &frame)
417 lenght_ = (frame.len > CAN_MAX_DLEN) ? CAN_MAX_DLEN : frame.len;
418 lenght_ = (frame.len > CANFD_MAX_DLEN) ? CANFD_MAX_DLEN : frame.len;
420 switch (frame.can_id):
421 case (frame.can_id & CAN_ERR_FLAG):
422 id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
424 case (frame.can_id & CAN_EFF_FLAG):
425 id_ = frame.can_id & CAN_EFF_MASK;
426 format_ = CanMessageFormat::EXTENDED;
429 format_ = CanMessageFormat::STANDARD;
430 id_ = frame.can_id & CAN_SFF_MASK;
433 if (sizeof(frame.data) <= data_.size())
435 ::memcpy(data_, canfd_frame.data, lenght_);
437 } else if (sizeof(frame.data) >= CAN_MAX_DLEN)
438 ERROR(interface_, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
441 canfd_frame convert_to_canfd_frame()
445 frame.can_id = get_id();
446 frame.len = get_lenght();
447 ::memcpy(frame.data, get_data(), lenght_);