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