Use initialisation list with CanBus_c constructor
[apps/agl-service-can-low-level.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 CanBus_c::CanBus_c(afb_binding_interface *itf, const std:string& dev_name)
27     : interface{itf}, deviceName{dev_name}
28 {
29 }
30
31 int CanBus_c::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", 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 CanBus_c::close()
85 {
86         ::close(can_socket_);
87         can_socket_ = -1;
88 }
89
90 void CanBus_c::start_threads()
91 {
92         th_reading_ = std::thread(can_reader, interface, socket, can_message_q_);
93         th_decoding_ = std::thread(can_decoder, interface, can_message_q, can_message_q_);
94         th_pushing_ = std::thread(can_event_push, interface, can_message_q_);
95 }
96
97 /*
98  * Send a can message from a CanMessage_c object.
99  */
100 int CanBus_c::send_can_message(CanMessage_c can_msg)
101 {
102         int nbytes;
103         canfd_frame *f;
104
105         f = can_msg.convert_to_canfd_frame();
106
107         if(can_socket_ >= 0)
108         {
109                 nbytes = ::sendto(can_socket_, &f, sizeof(struct canfd_frame), 0,
110                                 (struct sockaddr*)&txAddress, sizeof(txAddress));
111                                 
112                 if (nbytes == -1)
113                 {
114                         ERROR(interface_, "send_can_message: Sending CAN frame failed.");
115                         return -1;
116                 }
117                 return nbytes;
118         }
119         else
120         {
121                 ERROR(interface_, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
122                 open();
123         }
124         return 0;
125 }
126
127 /*
128  * Get a CanMessage from can_message_q and return it
129  * then point to the next CanMessage in queue.
130  * 
131  * Return the next queue element or NULL if queue is empty.
132  */
133 CanMessage_c* CanBus_c::next_can_message()
134 {
135         if(! can_message_q_.empty())
136         {
137                 CanMessage_c can_msg = can_message_q_.front();
138                 can_message_q_.pop()
139                 return &can_msg;
140         }
141
142         return nullptr;
143 }
144
145 void CanBus_c::insert_new_can_message(CanMessage_c *can_msg)
146 {
147         can_message_q_.push(can_msg);
148 }
149
150 /*
151  * Get a VehicleMessage from vehicle_message_q and return it
152  * then point to the next VehicleMessage in queue.
153  * 
154  * Return the next queue element or NULL if queue is empty.
155  */
156 openxc_VehicleMessage* CanBus_c::next_vehicle_message()
157 {
158         if(! vehicle_message_q_.empty())
159         {
160                 openxc_VehicleMessage v_msg = vehicle_message_q_.front();
161                 vehicle_message_q_.pop();
162                 return &v_msg;
163         }
164
165         return nullptr;
166 }
167
168 void CanBus_c::insert_new_vehicle_message(openxc_VehicleMessage *v_msg)
169 {
170         vehicle_message_q_.push(v_msg);
171 }
172 /********************************************************************************
173 *
174 *               CanMessage method implementation
175 *
176 *********************************************************************************/
177
178 uint32_t CanMessage_c::get_id() const
179 {
180         return id;
181 }
182
183 int CanMessage_c::get_format() const
184 {
185         return format;
186 }
187
188 uint8_t CanMessage_c::get_data() const
189 {
190         return data;
191 }
192 uint8_t CanMessage_c::get_lenght() const
193 {
194         return lenght;
195 }
196
197 void CanMessage_c::set_id(uint32_t new_id)
198 {
199         switch(format):
200                 case CanMessageFormat::SIMPLE:
201                         id = new_id & CAN_SFF_MASK;
202                 case CanMessageFormat::EXTENDED:
203                         id = new_id & CAN_EFF_MASK;
204                 default:
205                         ERROR(interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
206 }
207
208 void CanMessage_c::set_format(CanMessageFormat new_format)
209 {
210         if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::EXTENDED)
211                 format = new_format;
212         else
213                 ERROR(interface, "ERROR: Can set format, wrong format chosen");
214 }
215
216 void CanMessage_c::set_data(uint8_t new_data)
217 {
218         data = new_data;
219 }
220
221 void CanMessage_c::set_lenght(uint8_t new_length)
222 {
223         lenght = new_lenght;
224 }
225
226 /*
227  * This is the preferred way to initialize a CanMessage object 
228  * from a read canfd_frame message.
229  * 
230  * params: canfd_frame pointer
231  */
232 void CanMessage_c::convert_from_canfd_frame(canfd_frame *frame)
233 {
234         
235         lenght = (canfd_frame->len > maxdlen) ? maxdlen : canfd_frame->len;
236
237         switch (canfd_frame->can_id): 
238                 case (canfd_frame->can_id & CAN_ERR_FLAG):
239                         id = canfd_frame->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
240                         break;
241                 case (canfd_frame->can_id & CAN_EFF_FLAG):
242                         id = canfd_frame->can_id & CAN_EFF_MASK;
243                         format = CanMessageFormat::EXTENDED;
244                         break;
245                 default:
246                         format = CanMessageFormat::STANDARD;
247                         id = canfd_frame->can_id & CAN_SFF_MASK;
248                         break;
249
250         if (sizeof(canfd_frame->data) <= sizeof(data))
251         {
252                 memcpy(data, canfd_frame->data, lenght);
253                 return 0;
254         } else if (sizeof(canfd_frame->data) >= CAN_MAX_DLEN)
255                 ERROR(interface, "CanMessage_c: canfd_frame data too long to be stored into CanMessage object");
256 }
257
258 canfd_frame* convert_to_canfd_frame()
259 {
260         canfd_frame frame;
261
262         frame.id = can_msg.get_id();
263         frame.len = can_msg.get_lenght();
264         frame.data = can_msg.get_data();
265
266         return &frame;
267 }