Change way signaling end of queue for can messages
[apps/low-level-can-service.git] / src / can-utils.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #pragma once
19
20 /********************************************************************************
21 *
22 *               CanBus method implementation
23 *
24 *********************************************************************************/
25
26 can_bus_t::can_bus_t(afb_binding_interface *itf, const std:string &dev_name)
27     : interface{itf}, deviceName{dev_name}
28 {
29 }
30
31 int can_bus_t::open()
32 {
33         const int canfd_on = 1;
34         struct ifreq ifr;
35         struct timeval timeout = {1, 0};
36
37         DEBUG(interface_, "open_can_dev: CAN Handler socket : %d", can_socket_);
38         if (can_socket_ >= 0)
39                 return 0;
40
41         can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
42         if (socket < 0)
43         {
44                 ERROR(interface_, "open_can_dev: socket could not be created");
45         }
46         else
47         {
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)
52                 {
53                         NOTICE(interface_, "open_can_dev: Can not switch into CAN Extended frame format.");
54                         is_fdmode_on_ = false;
55                 } else {
56                         is_fdmode_on_ = true;
57                 }
58
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");
63                 else
64                 {
65                         txAddress.can_family = AF_CAN;
66                         txAddress.can_ifindex = ifr.ifr_ifindex;
67
68                         /* And bind it to txAddress */
69                         if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
70                         {
71                                 ERROR(interface_, "open_can_dev: bind failed");
72                         }
73                         else
74                         {
75                                 ::fcntl(can_socket_, F_SETFL, O_NONBLOCK);
76                                 return 0;
77                         }
78                 }
79                 close();
80         }
81         return -1;
82 }
83
84 int can_bus_t::close()
85 {
86         ::close(can_socket_);
87         can_socket_ = -1;
88 }
89
90
91 canfd_frame can_bus_t::can_read()
92 {
93         ssize_t nbytes;
94         //int maxdlen;
95         canfd_frame canfd_frame;
96
97         /* Test that socket is really opened */
98         if (can_socket_ < 0)
99         {
100                 ERROR(interface_, "read_can: Socket unavailable. Closing thread.");
101                 is_running_ = false;
102         }
103
104         nbytes = ::read(can_socket_, &canfd_frame, CANFD_MTU);
105
106         switch(nbytes)
107         {
108                 case CANFD_MTU:
109                         DEBUG(interface_, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
110                         //maxdlen = CANFD_MAX_DLEN;
111                         break;
112                 case CAN_MTU:
113                         DEBUG(interface_, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
114                         //maxdlen = CAN_MAX_DLEN;
115                         break;
116                 default:
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));
121                         break;
122         }
123         
124         return canfd_frame;
125 }
126
127 void can_bus_t::start_threads()
128 {
129         th_reading_ = std::thread(can_reader, interface, socket, can_message_q_);
130         is_running_ = true;
131
132         th_decoding_ = std::thread(can_decoder, interface, can_message_q, can_message_q_);
133         th_pushing_ = std::thread(can_event_push, interface, can_message_q_);
134 }
135
136 /*
137  * Return is_running_ bool
138  */
139 bool can_bus_t::is_running()
140 {
141         return is_running_;
142 }
143
144 /*
145  * Send a can message from a can_message_t object.
146  */
147 int can_bus_t::send_can_message(can_message_t &can_msg)
148 {
149         int nbytes;
150         canfd_frame *f;
151
152         f = can_msg.convert_to_canfd_frame();
153
154         if(can_socket_ >= 0)
155         {
156                 nbytes = ::sendto(can_socket_, &f, sizeof(struct canfd_frame), 0,
157                                 (struct sockaddr*)&txAddress, sizeof(txAddress));
158                                 
159                 if (nbytes == -1)
160                 {
161                         ERROR(interface_, "send_can_message: Sending CAN frame failed.");
162                         return -1;
163                 }
164                 return nbytes;
165         }
166         else
167         {
168                 ERROR(interface_, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
169                 open();
170         }
171         return 0;
172 }
173
174 /*
175  * Get a CanMessage from can_message_q and return it
176  * then point to the next CanMessage in queue.
177  * 
178  * Return the next queue element or NULL if queue is empty.
179  */
180 can_message_t* can_bus_t::next_can_message()
181 {
182         if(! can_message_q_.empty())
183         {
184                 can_message_t can_msg = can_message_q_.front();
185                 can_message_q_.pop()
186                 return &can_msg;
187         }
188         has_can_message_ = false;
189 }
190
191 /**
192  * @return has_can_message_ bool
193  */
194 bool can_bus_t::has_can_message() const
195 {
196         return has_can_message_;
197 }
198
199 void can_bus_t::insert_new_can_message(can_message_t &can_msg)
200 {
201         can_message_q_.push(can_msg);
202 }
203
204 /*
205  * Get a VehicleMessage from vehicle_message_q and return it
206  * then point to the next VehicleMessage in queue.
207  * 
208  * Return the next queue element or NULL if queue is empty.
209  */
210 openxc_VehicleMessage* can_bus_t::next_vehicle_message()
211 {
212         if(! vehicle_message_q_.empty())
213         {
214                 openxc_VehicleMessage v_msg = vehicle_message_q_.front();
215                 vehicle_message_q_.pop();
216                 return &v_msg;
217         }
218
219         has_vehicle_message_ = false;
220 }
221
222 void can_bus_t::insert_new_vehicle_message(openxc_VehicleMessage *v_msg)
223 {
224         vehicle_message_q_.push(v_msg);
225         has_vehicle_message_ = true;
226 }
227
228 /*
229  * Return has_can_message_ bool
230  */
231 bool can_bus_t::has_vehicle_message() const
232 {
233         return has_vehicle_message_;
234 }
235
236 /********************************************************************************
237 *
238 *               CanMessage method implementation
239 *
240 *********************************************************************************/
241
242 can_message_t::can_message_t(afb_binding_interface *itf)
243         : interface_{itf}
244 {}
245
246 uint32_t can_message_t::get_id() const
247 {
248         (id_ != 0) ? return id_ : return 0;
249 }
250
251 int can_message_t::get_format() const
252 {
253         (format_ != CanMessageFormat::SIMPLE || format_ != CanMessageFormat::EXTENDED) return -1 : return format_;
254 }
255
256 uint8_t can_message_t::get_data() const
257 {
258         return data_;
259 }
260 uint8_t can_message_t::get_lenght() const
261 {
262         return lenght_;
263 }
264
265 void can_message_t::set_id(uint32_t &new_id)
266 {
267         switch(format):
268                 case CanMessageFormat::SIMPLE:
269                         id = new_id & CAN_SFF_MASK;
270                 case CanMessageFormat::EXTENDED:
271                         id = new_id & CAN_EFF_MASK;
272                 default:
273                         ERROR(interface_, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
274 }
275
276 void can_message_t::set_format(CanMessageFormat &new_format)
277 {
278         if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::EXTENDED)
279                 format = new_format;
280         else
281                 ERROR(interface_, "ERROR: Can set format, wrong format chosen");
282 }
283
284 void can_message_t::set_data(uint8_t &new_data)
285 {
286         ::memcpy(data_, new_data, new_data.size());
287         lenght_ = new_data(size);
288 }
289
290 /*
291  * This is the preferred way to initialize a CanMessage object 
292  * from a read canfd_frame message.
293  * 
294  * params: canfd_frame pointer
295  */
296 void can_message_t::convert_from_canfd_frame(canfd_frame &frame)
297 {
298         lenght_ = (frame.len > CAN_MAX_DLEN) ? CAN_MAX_DLEN : frame.len;
299         lenght_ = (frame.len > CANFD_MAX_DLEN) ? CANFD_MAX_DLEN : frame.len;
300
301         switch (frame.can_id): 
302                 case (frame.can_id & CAN_ERR_FLAG):
303                         id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
304                         break;
305                 case (frame.can_id & CAN_EFF_FLAG):
306                         id_ = frame.can_id & CAN_EFF_MASK;
307                         format_ = CanMessageFormat::EXTENDED;
308                         break;
309                 default:
310                         format_ = CanMessageFormat::STANDARD;
311                         id_ = frame.can_id & CAN_SFF_MASK;
312                         break;
313
314         if (sizeof(frame.data) <= data_.size())
315         {
316                 ::memcpy(data_, canfd_frame.data, lenght_);
317                 return 0;
318         } else if (sizeof(frame.data) >= CAN_MAX_DLEN)
319                 ERROR(interface_, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
320 }
321
322 canfd_frame convert_to_canfd_frame()
323 {
324         canfd_frame frame;
325
326         frame.can_id = get_id();
327         frame.len = get_lenght();
328         ::memcpy(frame.data, get_data(), lenght_);
329
330         return frame;
331 }