low_can_subscription: Add msg_def and create_rx for message
[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/signals.hpp"
25 #include "../diagnostic/diagnostic-message.hpp"
26 #include "../utils/socketcan.hpp"
27
28
29 #define OBDII_MAX_SIMULTANEOUS_RESPONSES 8
30
31 /// @brief Filtering values. Theses values have to be tested in
32 /// can_bus_t::apply_filter method.
33 struct event_filter_t
34 {
35         float frequency; ///< frequency - Maximum frequency which will be received and pushed to a subscribed event.
36         float min; ///< min - Minimum value that the signal doesn't have to go below to be pushed.
37         float max; ///< max - Maximum value that the signal doesn't have to go above to be pushed.
38         bool promisc; //<promisc - Active mode promisc on socket that permit this feature (j1939)
39         canid_t rx_id; ///< rx_id - RX_ID for ISO_TP protocol
40         canid_t tx_id; ///< tx_id - TX_ID for ISO_TP protocol
41
42         event_filter_t() : frequency{0}, min{-__FLT_MAX__}, max{__FLT_MAX__}, promisc{false}, rx_id{NO_CAN_ID}, tx_id{NO_CAN_ID} {};
43         bool operator==(const event_filter_t& ext) const {
44                 return frequency == ext.frequency && min == ext.min && max == ext.max && promisc == ext.promisc && rx_id == ext.rx_id && tx_id == ext.tx_id;
45         }
46 };
47
48
49 /// @brief The object stores socket to CAN to be used to write on it.
50 /// This is a simple access to a CAN bus device without any subscriptions attached
51 class low_can_subscription_t
52 {
53 private:
54         int index_; ///< index_ - index number is the socket (int) casted
55         struct event_filter_t event_filter_;
56         afb_event_t event_; ///< event_ - application framework event used to push on client
57
58         /// Signal part
59         std::shared_ptr<message_definition_t> message_;
60         std::shared_ptr<signal_t> signal_; ///< signal_ - the CAN signal subscribed
61         vect_ptr_diag_msg_t diagnostic_message_; ///< diagnostic_message_ - diagnostic messages meant to receive OBD2
62                                                                                  /// responses. Normal diagnostic request and response are not tested for now.
63         std::shared_ptr<utils::socketcan_t> socket_; ///< socket_ - socket_ that receives CAN messages.
64
65         int set_event();
66
67 public:
68         low_can_subscription_t();
69         low_can_subscription_t(struct event_filter_t event_filter);
70         low_can_subscription_t(const low_can_subscription_t& s) = delete;
71         low_can_subscription_t(low_can_subscription_t&& s);
72         ~low_can_subscription_t();
73
74         low_can_subscription_t& operator=(const low_can_subscription_t& s);
75         explicit operator bool() const;
76
77         afb_event_t get_event();
78         int subscribe(afb_req_t request);
79         int unsubscribe(afb_req_t request);
80
81         int get_index() const;
82         const std::shared_ptr<signal_t> get_signal() const;
83         bool is_signal_subscription_corresponding(const std::shared_ptr<signal_t>, const struct event_filter_t& event_filter) const;
84         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(uint32_t pid) const;
85         const vect_ptr_diag_msg_t get_diagnostic_message() const;
86         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(const std::string& name) const;
87         const std::string get_name() const;
88         const std::string get_name(uint32_t pid) const;
89         float get_frequency() const;
90         float get_min() const;
91         float get_max() const;
92         bool get_promisc() const;
93         canid_t get_rx_id() const;
94         canid_t get_tx_id() const;
95         std::shared_ptr<utils::socketcan_t> get_socket();
96         std::shared_ptr<message_definition_t> get_message_definition();
97
98         void set_frequency(float freq);
99         void set_min(float min);
100         void set_max(float max);
101         void set_index(int index);
102         void set_promisc(bool promisc);
103         void set_rx_id(canid_t rx_id);
104         void set_tx_id(canid_t tx_id);
105         void set_signal(std::shared_ptr<signal_t> signal);
106         void set_message_definition(std::shared_ptr<message_definition_t> message);
107
108         static struct bcm_msg make_bcm_head(uint32_t opcode, uint32_t can_id = 0, uint32_t flags = 0, const struct timeval& timeout = {0, 0}, const struct timeval& frequency_thinning = {0, 0});
109         static void add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg);
110         static void remove_last_bcm_frame(struct bcm_msg& bcm_msg);
111
112         static int open_socket(low_can_subscription_t &subscription, const std::string& bus_name = "", uint32_t flags = INVALID_FLAG);
113
114         int create_rx_filter(std::shared_ptr<signal_t> sig);
115         int create_rx_filter(std::shared_ptr<message_definition_t> msg);
116         int create_rx_filter(std::shared_ptr<diagnostic_message_t> sig);
117         static int create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig);
118         static int create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig);
119         static int create_rx_filter_isotp(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig);
120         static int create_rx_filter_bcm(low_can_subscription_t &subscription, bcm_msg& bcm_msg);
121
122         static int tx_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name);
123         static int j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name);
124         static int isotp_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name);
125 };