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 CanBus_c::CanBus_c(afb_binding_interface *itf, const std:string& dev_name)
29 deviceName = dev_name;
34 const int canfd_on = 1;
36 struct timeval timeout = {1, 0};
38 DEBUG(interface, "open_can_dev: CAN Handler socket : %d", socket);
42 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(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
51 /* try to switch the socket into CAN_FD mode */
52 if (setsockopt(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.");
60 /* Attempts to open a socket to CAN bus */
61 strcpy(ifr.ifr_name, device);
62 if(ioctl(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(socket, (struct sockaddr *)&txAddress, sizeof(txAddress)) < 0)
72 ERROR(interface, "open_can_dev: bind failed");
76 fcntl(socket, F_SETFL, O_NONBLOCK);
92 void CanBus_c::start_threads()
94 std::queue <CanMessage_c> can_message_q;
96 th_reading = std::thread(can_reader, interface, socket, can_message_q);
97 th_decoding = std::thread(can_decoder, interface, can_message_q, can_message_q);
98 th_pushing = std::thread(can_event_push, interface, can_message_q);
102 * Send a can message from a CanMessage_c object.
104 int CanBus_c::send_can_message(CanMessage_c can_msg)
109 f = can_msg.convert_to_canfd_frame();
113 nbytes = sendto(socket, &f, sizeof(struct canfd_frame), 0,
114 (struct sockaddr*)&txAddress, sizeof(txAddress));
118 ERROR(interface, "send_can_message: Sending CAN frame failed.");
125 ERROR(interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
132 * Get a CanMessage from can_message_q and return it
133 * then point to the next CanMessage in queue.
135 * Return the next queue element or NULL if queue is empty.
137 CanMessage_c* CanBus_c::next_can_message()
139 if(! can_message_q.empty())
141 CanMessage_c can_msg = can_message_q.front();
149 void CanBus_c::insert_new_can_message(CanMessage_c *can_msg)
151 can_message_q.push(can_msg);
155 * Get a VehicleMessage from vehicle_message_q and return it
156 * then point to the next VehicleMessage in queue.
158 * Return the next queue element or NULL if queue is empty.
160 openxc_VehicleMessage* CanBus_c::next_vehicle_message()
162 if(! vehicle_message_q.empty())
164 openxc_VehicleMessage v_msg = vehicle_message_q.front();
165 vehicle_message_q.pop()
172 void CanBus_c::insert_new_vehicle_message(openxc_VehicleMessage *v_msg)
174 vehicle_message_q.push(v_msg);
176 /********************************************************************************
178 * CanMessage method implementation
180 *********************************************************************************/
182 uint32_t CanMessage_c::get_id()
187 int CanMessage_c::get_format()
192 uint8_t CanMessage_c::get_data()
196 uint8_t CanMessage_c::get_lenght()
201 void CanMessage_c::set_id(uint32_t new_id)
204 case CanMessageFormat::SIMPLE:
205 id = new_id & CAN_SFF_MASK;
206 case CanMessageFormat::EXTENDED:
207 id = new_id & CAN_EFF_MASK;
209 ERROR(interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
212 void CanMessage_c::set_format(CanMessageFormat new_format)
214 if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::EXTENDED)
217 ERROR(interface, "ERROR: Can set format, wrong format chosen");
220 void CanMessage_c::set_data(uint8_t new_data)
225 void CanMessage_c::set_lenght(uint8_t new_length)
231 * This is the preferred way to initialize a CanMessage object
232 * from a read canfd_frame message.
234 * params: canfd_frame pointer
236 void CanMessage_c::convert_from_canfd_frame(canfd_frame *frame)
239 lenght = (canfd_frame->len > maxdlen) ? maxdlen : canfd_frame->len;
241 switch (canfd_frame->can_id):
242 case (canfd_frame->can_id & CAN_ERR_FLAG):
243 id = canfd_frame->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
245 case (canfd_frame->can_id & CAN_EFF_FLAG):
246 id = canfd_frame->can_id & CAN_EFF_MASK;
247 format = CanMessageFormat::EXTENDED;
250 format = CanMessageFormat::STANDARD;
251 id = canfd_frame->can_id & CAN_SFF_MASK;
254 if (sizeof(canfd_frame->data) <= sizeof(data))
256 memcpy(data, canfd_frame->data, lenght);
258 } else if (sizeof(canfd_frame->data) >= CAN_MAX_DLEN)
259 ERROR(interface, "CanMessage_c: canfd_frame data too long to be stored into CanMessage object");
262 canfd_frame* convert_to_canfd_frame()
266 frame.id = can_msg.get_id();
267 frame.len = can_msg.get_lenght();
268 frame.data = can_msg.get_data();