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