manage unset values with NAN
[apps/low-level-can-service.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 #include <string>
19 #include <cmath>
20 #include "../utils/socketcan-bcm.hpp"
21
22 struct event_filter_t
23 {
24         float frequency;
25         float min;
26         float max;
27         event_filter_t() : frequency{NAN}, min{NAN}, max{NAN} {}
28 };
29
30 class low_can_subscription_t
31 {
32 private:
33         int index_;
34
35         /// Signal part
36         std::string sig_name_;
37         std::string bus_name_;
38         uint32_t can_id_;
39         uint8_t bit_position_; /*!< bitPosition_ - The starting bit of the signal in its CAN message (assuming
40                                                                         *       non-inverted bit numbering, i.e. the most significant bit of
41                                                                         *       each byte is 0) */
42         uint8_t bit_size_; /*!< bit_size_ - The width of the bit field in the CAN message. */
43         float factor_; /*!< factor_ - The final value will be multiplied by this factor. Use 1 if you
44                                                         *       don't need a factor. */
45         float offset_; /*!< offset_ - The final value will be added to this offset. Use 0 if you
46                                                         *       don't need an offset. */
47
48         /// Filtering part
49         struct event_filter_t event_filter_;
50
51         utils::socketcan_bcm_t socket_;
52 public:
53         low_can_subscription_t();
54         low_can_subscription_t(struct event_filter_t event_filter);
55         low_can_subscription_t(const low_can_subscription_t& s) = delete;
56         low_can_subscription_t(low_can_subscription_t&& s);
57
58         low_can_subscription_t& operator=(const low_can_subscription_t& s);
59         explicit operator bool() const;
60
61         int get_index() const;
62         const std::string get_sig_name() const;
63         float get_frequency() const;
64         float get_min() const;
65         float get_max() const;
66         utils::socketcan_bcm_t& get_socket();
67
68         void set_frequency(float freq);
69         void set_min(float min);
70         void set_max(float max);
71
72         int create_rx_filter();
73         int create_rx_filter(const std::string& bus_name, const std::string& sig_name, uint32_t can_id, uint8_t bit_position, uint8_t bit_size, float factor, float offset);
74 };