Make low-can use afb-genskel by default
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-socket.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loic Collignon" <loic.collignon@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "low-can-subscription.hpp"
20 #include "application.hpp"
21 #include "canutil/write.h"
22
23 low_can_socket_t::low_can_socket_t()
24         : index_{-1},
25         event_filter_{},
26         socket_{}
27 {}
28
29 low_can_socket_t::low_can_socket_t(struct event_filter_t event_filter)
30         : event_filter_{event_filter}
31  {}
32
33 low_can_socket_t::low_can_socket_t( low_can_socket_t&& s)
34         : index_{s.index_},
35         event_filter_{s.event_filter_},
36         socket_{std::move(s.socket_)}
37 {}
38
39 low_can_socket_t& low_can_socket_t::operator=(const low_can_socket_t& s)
40 {
41         socket_ = std::move(s.socket_);
42         return *this;
43 }
44
45 low_can_socket_t::~low_can_socket_t()
46 {
47         socket_.close();
48 }
49
50 low_can_socket_t::operator bool() const
51 {
52         return ((can_signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
53 }
54
55 int low_can_socket_t::get_index() const
56 {
57         return index_;
58 }
59
60 const std::shared_ptr<can_signal_t> low_can_socket_t::get_can_signal() const
61 {
62         return can_signal_;
63 }
64
65 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_socket_t::get_diagnostic_message() const
66 {
67         return diagnostic_message_;
68 }
69
70 /// @brief Retrieve a diagnostic_message subscribed from a pid
71 ///
72 /// @param[in] pid - Diagnostic messages PID to search for
73 ///
74 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
75 const std::shared_ptr<diagnostic_message_t> low_can_socket_t::get_diagnostic_message(uint32_t pid) const
76 {
77         for(const auto& diag: diagnostic_message_)
78         {
79                 if(diag->get_pid() == pid)
80                 {
81                         return diag;
82                 }
83         }
84         return nullptr;
85 }
86
87 /// @brief Retrieve a diagnostic message search from its name
88 ///
89 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
90 const std::shared_ptr<diagnostic_message_t> low_can_socket_t::get_diagnostic_message(const std::string& name) const
91 {
92         for(const auto& diag: diagnostic_message_)
93         {
94                 if(diag->get_name() == name)
95                 {
96                         return diag;
97                 }
98         }
99         return nullptr;
100 }
101
102 /// @brief Return the CAN signal name and empty string if not found
103 /// or no CAN signal subscribed
104 const std::string low_can_socket_t::get_name() const
105 {
106         if (can_signal_ != nullptr)
107                 return can_signal_->get_name();
108         else if (!diagnostic_message_.empty())
109                 return "diagnostic_messages";
110
111         AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
112         return "";
113 }
114
115 /// @brief Return name from a diagnostic message from a PID
116 ///
117 /// @param[in] pid - Diagnostic message PID
118 const std::string low_can_socket_t::get_name(uint32_t pid) const
119 {
120         if (!diagnostic_message_.empty())
121                 return get_diagnostic_message(pid)->get_name() ;
122
123         AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
124         return "";
125 }
126
127 float low_can_socket_t::get_frequency() const
128 {
129         return event_filter_.frequency;
130 }
131
132 float low_can_socket_t::get_min() const
133 {
134         return event_filter_.min;
135 }
136
137 float low_can_socket_t::get_max() const
138 {
139         return event_filter_.max;
140 }
141
142 utils::socketcan_bcm_t& low_can_socket_t::get_socket()
143 {
144         return socket_;
145 }
146
147 void low_can_socket_t::set_frequency(float freq)
148 {
149         event_filter_.frequency = freq;
150 }
151
152 void low_can_socket_t::set_min(float min)
153 {
154         event_filter_.min = min;
155 }
156
157 void low_can_socket_t::set_max(float max)
158 {
159         event_filter_.max = max;
160 }
161 /// @brief Based upon which object is subscribed CAN signal or diagnostic message
162 /// this will open the socket with the required CAN bus device name.
163 ///
164 /// @return INVALID_SOCKET on failure else positive integer
165 int low_can_socket_t::open_socket(const std::string& bus_name)
166 {
167         int ret = 0;
168         if(! socket_)
169         {
170                 if( can_signal_ != nullptr)
171                         {ret = socket_.open(can_signal_->get_message()->get_bus_device_name());}
172                 else if (! diagnostic_message_ .empty())
173                         {ret = socket_.open(application_t::instance().get_diagnostic_manager().get_bus_device_name());}
174                 else if ( ! bus_name.empty())
175                         { ret = socket_.open(bus_name);}
176                 index_ = (int)socket_.socket();
177         }
178         return ret;
179 }
180
181 /// @brief Build a BCM message head but don't set can_frame.
182 ///
183 /// @return a simple_bcm_msg with the msg_head parts set and can_frame
184 /// zeroed.
185 struct utils::simple_bcm_msg low_can_socket_t::make_bcm_head(uint32_t opcode, uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning) const
186 {
187         struct utils::simple_bcm_msg bcm_msg;
188         ::memset(&bcm_msg, 0, sizeof(bcm_msg));
189
190         bcm_msg.msg_head.opcode  = opcode;
191         bcm_msg.msg_head.can_id  = can_id;
192         bcm_msg.msg_head.flags = flags;
193         bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
194         bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
195         bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
196         bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
197
198         return bcm_msg;
199 }
200
201 /// @brief Take an existing simple_bcm_msg struct and add a can_frame to it.
202 /// Only possible for now to add 1 uniq can_frame, it isn't possible to build
203 /// a multiplexed message with several can_frame.
204 void low_can_socket_t::add_bcm_frame(const struct can_frame& cf, struct utils::simple_bcm_msg& bcm_msg) const
205 {
206         for(int i=0; i < CAN_MAX_DLEN; i++)
207         {
208                 if(cf.data[i] != 0)
209                 {
210                         bcm_msg.msg_head.nframes = 1;
211                         bcm_msg.frames = cf;
212                         return;
213                 }
214         }
215 }
216
217 /// @brief Create a RX_SETUP receive job used by the BCM socket for a CAN signal
218 /// subscription
219 ///
220 /// @return 0 if ok else -1
221 int low_can_socket_t::create_rx_filter(std::shared_ptr<can_signal_t> sig)
222 {
223         can_signal_= sig;
224
225         struct can_frame cfd;
226         memset(&cfd, 0, sizeof(cfd));
227
228         float val = (float)(1 << can_signal_->get_bit_size()) - 1;
229         bitfield_encode_float(val,
230                                                         can_signal_->get_bit_position(),
231                                                         can_signal_->get_bit_size(),
232                                                         can_signal_->get_factor(),
233                                                         can_signal_->get_offset(),
234                                                         cfd.data,
235                                                         CAN_MAX_DLEN);
236
237         struct timeval freq, timeout = {0, 0};
238         frequency_clock_t f = std::isnan(event_filter_.frequency) ? can_signal_->get_frequency() : frequency_clock_t(event_filter_.frequency);
239         freq = f.get_timeval_from_period();
240
241         utils::simple_bcm_msg bcm_msg = make_bcm_head(RX_SETUP, can_signal_->get_message()->get_id(), SETTIMER|RX_NO_AUTOTIMER, timeout, freq);
242         add_bcm_frame(cfd, bcm_msg);
243
244         return create_rx_filter(bcm_msg);
245 }
246
247 /// @brief Create a RX_SETUP receive job used by the BCM socket for a
248 /// diagnostic message subscription.
249 ///
250 /// @return 0 if ok else -1
251 int low_can_socket_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
252 {
253         diagnostic_message_.push_back(sig);
254
255         struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
256         //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
257         struct timeval timeout = {0,0};
258
259         utils::simple_bcm_msg bcm_msg =  make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
260         return create_rx_filter(bcm_msg);
261 }
262
263 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
264 /// a simple_bcm_msg. You will not use this method directly but rather use the
265 /// two previous method with can_signal_t or diagnostic_message_t object.
266 ///
267 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
268 /// to the 8 classics OBD2 functional response ID
269 ///
270 /// @return 0 if ok else -1
271 int low_can_socket_t::create_rx_filter(utils::simple_bcm_msg& bcm_msg)
272 {
273         // Make sure that socket has been opened.
274         if(open_socket() < 0)
275                 {return -1;}
276
277         // If it isn't an OBD2 CAN ID then just add a simple RX_SETUP job
278         // else monitor all standard 8 CAN OBD2 ID response.
279         if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
280         {
281                 socket_ << bcm_msg;
282                         if(! socket_)
283                                 return -1;
284         }
285         else
286         {
287                 for(uint8_t i = 0; i < 8; i++)
288                 {
289                         bcm_msg.msg_head.can_id  =  OBD2_FUNCTIONAL_RESPONSE_START + i;
290
291                         socket_ << bcm_msg;
292                         if(! socket_)
293                                 return -1;
294                 }
295         }
296
297         return 0;
298 }
299
300 /// @brief Create a TX_SEND job used by the BCM socket to
301 /// simply send message
302 ///
303 /// @return 0 if ok else -1
304 int low_can_socket_t::tx_send(const struct can_frame& cf, std::shared_ptr<can_signal_t> sig)
305 {
306         can_signal_ = sig;
307
308         utils::simple_bcm_msg bcm_msg =  make_bcm_head(TX_SEND);
309         add_bcm_frame(cf, bcm_msg);
310
311         if(open_socket() < 0)
312                 {return -1;}
313
314         socket_ << bcm_msg;
315         if(! socket_)
316                 return -1;
317
318         return 0;
319 }
320
321 /// @brief Create a TX_SEND job used by the BCM socket to
322 /// simply send message
323 ///
324 /// @return 0 if ok else -1
325 int low_can_socket_t::tx_send(const struct can_frame& cf, const std::string& bus_name)
326 {
327         can_signal_ = nullptr;
328
329         utils::simple_bcm_msg bcm_msg =  make_bcm_head(TX_SEND);
330         add_bcm_frame(cf, bcm_msg);
331
332         if(open_socket(bus_name) < 0)
333                 {return -1;}
334
335         socket_ << bcm_msg;
336         if(! socket_)
337                 return -1;
338
339         return 0;
340 }