d46f1f8cb44be721f598cb02596f0dad9b86abc8
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-socket.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 #define OBDII_MAX_SIMULTANEOUS_RESPONSES 8
29
30 /// @brief Filtering values. Theses values have to be tested in
31 /// can_bus_t::apply_filter method.
32 struct event_filter_t
33 {
34         float frequency; ///< frequency - Maximum frequency which will be received and pushed to a subscribed event.
35         float min; ///< min - Minimum value that the signal doesn't have to go below to be pushed.
36         float max; ///< max - Maximum value that the signal doesn't have to go above to be pushed.
37         event_filter_t() : frequency{0}, min{-__FLT_MAX__}, max{__FLT_MAX__} {};
38         bool operator==(const event_filter_t& ext) const {
39                 return frequency == ext.frequency && min == ext.min && max == ext.max;
40         }
41 };
42
43 /// @brief The object stores socket to CAN to be used to write on it.
44 /// This is a simple access to a CAN bus device without any subscriptions attached
45 class low_can_socket_t
46 {
47 protected:
48         int index_; ///< index_ - index number is the socket (int) casted
49         struct event_filter_t event_filter_;
50
51         /// Signal part
52         std::shared_ptr<can_signal_t> can_signal_; ///< can_signal_ - the CAN signal subscribed
53         std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_message_; ///< diagnostic_message_ - diagnostic messages meant to receive OBD2 responses.
54                                 /// normal diagnostic request and response are not tested for now.
55
56         utils::socketcan_bcm_t socket_; ///< socket_ - socket_ that receives CAN messages.
57
58 public:
59         low_can_socket_t();
60         low_can_socket_t(struct event_filter_t event_filter);
61         low_can_socket_t(const low_can_socket_t& s) = delete;
62         low_can_socket_t(low_can_socket_t&& s);
63         virtual ~low_can_socket_t();
64
65         low_can_socket_t& operator=(const low_can_socket_t& s);
66         explicit operator bool() const;
67
68         int get_index() const;
69         const std::shared_ptr<can_signal_t> get_can_signal() const;
70         bool is_signal_subscription_corresponding(const std::shared_ptr<can_signal_t>, const struct event_filter_t& event_filter) const;
71         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(uint32_t pid) const;
72         const std::vector<std::shared_ptr<diagnostic_message_t> > get_diagnostic_message() const;
73         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(const std::string& name) const;
74         const std::string get_name() const;
75         const std::string get_name(uint32_t pid) const;
76         float get_frequency() const;
77         float get_min() const;
78         float get_max() const;
79         utils::socketcan_bcm_t& get_socket();
80
81         void set_event(afb_event_t event);
82         void set_frequency(float freq);
83         void set_min(float min);
84         void set_max(float max);
85
86         struct utils::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}) const;
87         void add_one_bcm_frame(struct canfd_frame& cfd, struct utils::bcm_msg& bcm_msg) const;
88
89         int open_socket(const std::string& bus_name = "");
90
91         int create_rx_filter(std::shared_ptr<can_signal_t> sig);
92         int create_rx_filter(std::shared_ptr<diagnostic_message_t> sig);
93         int create_rx_filter(utils::bcm_msg& bcm_msg);
94
95         int tx_send(struct canfd_frame& cfd, const std::string& bus_name);
96 };