Update struct to follow af-binder changes.
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-subscription.hpp
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 #include <string>
21 #include <cmath>
22 #include <utility>
23
24 #include "../can/can-signals.hpp"
25 #include "../diagnostic/diagnostic-message.hpp"
26 #include "../utils/socketcan-bcm.hpp"
27
28 /// @brief Filtering values. Theses values has to be tested into
29 /// can_bus_t::apply_filter method.
30 struct event_filter_t
31 {
32         float frequency; ///< frequency - Maximum frequency which will be received and pushed a subscribed event.
33         float min; ///< min - Minimum value that the signal don't have to go below to be pushed.
34         float max; ///< max - Maximum value that the signal don't have to go above to be pushed.
35         event_filter_t() : frequency{NAN}, min{NAN}, max{NAN} {}
36 };
37
38 /// @brief A subscription object used has a context that handle all needed values to describe a subscription
39 /// to the low-can binding. It can holds a CAN signal or diagnostic message. Diagnostic message for OBD2 is a kind
40 /// of special because there is only 1 listener to retrieve OBD2 requests. So it's needed that all diagnostic messages
41 /// subscriptions is to be in 1 object.
42 class low_can_subscription_t
43 {
44 private:
45         int index_; ///< index_ - index number is the socket (int) casted
46         struct afb_event event_; ///< event_ - application framework event used to push on client
47
48         /// Signal part
49         std::shared_ptr<can_signal_t> can_signal_; ///< can_signal_ - the CAN signal subscribed
50         std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_message_; ///< diagnostic_message_ - diagnostic messages meant to received OBD2 responses.
51                                 /// normal diagnostic request and response not tested for now.
52
53         /// Filtering part
54         struct event_filter_t event_filter_; ///< event_filter_ - filtering values applied to a subscription
55
56         utils::socketcan_bcm_t socket_; ///< socket_ - socket_ that receives CAN messages.
57 public:
58         low_can_subscription_t();
59         low_can_subscription_t(struct event_filter_t event_filter);
60         low_can_subscription_t(const low_can_subscription_t& s) = delete;
61         low_can_subscription_t(low_can_subscription_t&& s);
62         ~low_can_subscription_t();
63
64         low_can_subscription_t& operator=(const low_can_subscription_t& s);
65         explicit operator bool() const;
66
67         int get_index() const;
68         struct afb_event& get_event();
69         const std::shared_ptr<can_signal_t> get_can_signal() const;
70         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(uint32_t pid) const;
71         const std::vector<std::shared_ptr<diagnostic_message_t> > get_diagnostic_message() const;
72         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(const std::string& name) const;
73         const std::string get_name() const;
74         const std::string get_name(uint32_t pid) const;
75         float get_frequency() const;
76         float get_min() const;
77         float get_max() const;
78         utils::socketcan_bcm_t& get_socket();
79
80         void set_event(struct afb_event event);
81         void set_frequency(float freq);
82         void set_min(float min);
83         void set_max(float max);
84
85         struct utils::simple_bcm_msg make_bcm_head(uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning) const;
86         void add_bcm_frame(const struct can_frame& cfd, struct utils::simple_bcm_msg& bcm_msg) const;
87         int open_socket();
88         int create_rx_filter(std::shared_ptr<can_signal_t> sig);
89         int create_rx_filter(std::shared_ptr<diagnostic_message_t> sig);
90         int create_rx_filter(utils::simple_bcm_msg& bcm_msg);
91 };