Get diag manager's socket into subscription obj
[apps/agl-service-can-low-level.git] / CAN-binder / low-can-binding / binding / low-can-cb.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 struct event_filter_t
29 {
30         float frequency;
31         float min;
32         float max;
33         event_filter_t() : frequency{NAN}, min{NAN}, max{NAN} {}
34 };
35
36 class low_can_subscription_t
37 {
38 private:
39         int index_;
40         struct afb_event event_;
41
42         /// Signal part
43         std::shared_ptr<can_signal_t> can_signal_;
44         std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_message_;
45
46         /// Filtering part
47         struct event_filter_t event_filter_;
48
49         utils::socketcan_bcm_t socket_;
50 public:
51         low_can_subscription_t();
52         low_can_subscription_t(struct event_filter_t event_filter);
53         low_can_subscription_t(const low_can_subscription_t& s) = delete;
54         low_can_subscription_t(low_can_subscription_t&& s);
55         ~low_can_subscription_t();
56
57         low_can_subscription_t& operator=(const low_can_subscription_t& s);
58         explicit operator bool() const;
59
60         int get_index() const;
61         struct afb_event& get_event();
62         const std::shared_ptr<can_signal_t> get_can_signal() const;
63         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(uint32_t pid) const;
64         const std::vector<std::shared_ptr<diagnostic_message_t> > get_diagnostic_message() const;
65         const std::shared_ptr<diagnostic_message_t> get_diagnostic_message(const std::string& name) const;
66         const std::string get_name() const;
67         const std::string get_name(uint32_t pid) const;
68         float get_frequency() const;
69         float get_min() const;
70         float get_max() const;
71         utils::socketcan_bcm_t& get_socket();
72
73         void set_event(struct afb_event event);
74         void set_frequency(float freq);
75         void set_min(float min);
76         void set_max(float max);
77
78         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;
79         void add_bcm_frame(const struct can_frame& cfd, struct utils::simple_bcm_msg& bcm_msg) const;
80         int open_socket();
81         int create_rx_filter();
82         int create_rx_filter(std::shared_ptr<can_signal_t> sig);
83         int create_rx_filter(std::shared_ptr<diagnostic_message_t> sig);
84         int create_rx_filter(utils::simple_bcm_msg& bcm_msg);
85 };