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