Reworked subscription to integrate filtering.
[apps/agl-service-can-low-level.git] / CAN-binder / low-can-binding / can / can-signals.cpp
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 <fnmatch.h>
19
20 #include "can-signals.hpp"
21
22 #include "../binding/application.hpp"
23 #include "../utils/signals.hpp"
24 #include "can-decoder.hpp"
25 #include "can-message.hpp"
26 #include "can-bus.hpp"
27 #include "../diagnostic/diagnostic-message.hpp"
28 #include "canutil/write.h"
29
30 std::string can_signal_t::prefix_ = "messages";
31
32 can_signal_t::can_signal_t(
33         std::string generic_name,
34         uint8_t bit_position,
35         uint8_t bit_size,
36         float factor,
37         float offset,
38         float min_value,
39         float max_value,
40         frequency_clock_t frequency,
41         bool send_same,
42         bool force_send_changed,
43         std::map<uint8_t, std::string> states,
44         bool writable,
45         SignalDecoder decoder,
46         SignalEncoder encoder,
47         bool received)
48         : parent_{nullptr},
49          generic_name_{ generic_name }
50         , bit_position_{ bit_position }
51         , bit_size_{ bit_size }
52         , factor_{ factor }
53         , offset_{ offset }
54         , min_value_{min_value}
55         , max_value_{max_value}
56         , frequency_{frequency}
57         , send_same_{send_same}
58         , force_send_changed_{force_send_changed}
59         , states_{states}
60         , writable_{writable}
61         , decoder_{decoder}
62         , encoder_{encoder}
63         , received_{received}
64         , last_value_{.0f}
65 {}
66
67 /*can_signal_t::can_signal_t(const can_signal_t& b)
68         : parent_{b.parent_},
69          generic_name_{ b.generic_name_}
70         , bit_position_{ b.bit_position_}
71         , bit_size_{ b.bit_size_}
72         , factor_{ b.factor_ }
73         , offset_{ b.offset_}
74         , min_value_{b.min_value_}
75         , max_value_{b.max_value_}
76         , frequency_{b.frequency_}
77         , send_same_{b.send_same_}
78         , force_send_changed_{b.force_send_changed_}
79         , states_{b.states_}
80         , writable_{b.writable_}
81         , decoder_{b.decoder_}
82         , encoder_{b.encoder_}
83         , received_{b.received_}
84         , last_value_{b.last_value_}
85 {}*/
86 can_message_definition_t* can_signal_t::get_message() const
87 {
88         return parent_;
89 }
90
91 const std::string can_signal_t::get_generic_name() const
92 {
93         return generic_name_;
94 }
95
96 const std::string can_signal_t::get_name() const
97 {
98         return prefix_ + "." + generic_name_;
99 }
100
101 const std::string can_signal_t::get_prefix() const
102 {
103         return prefix_;
104 }
105
106 uint8_t can_signal_t::get_bit_position() const
107 {
108         return bit_position_;
109 }
110
111 uint8_t can_signal_t::get_bit_size() const
112 {
113         return bit_size_;
114 }
115
116 float can_signal_t::get_factor() const
117 {
118         return factor_;
119 }
120
121 float can_signal_t::get_offset() const
122 {
123         return offset_;
124 }
125
126 float can_signal_t::get_min_value() const
127 {
128         return min_value_;
129 }       
130
131 float can_signal_t::get_max_value() const
132 {
133         return max_value_;
134 }
135
136 frequency_clock_t& can_signal_t::get_frequency()
137 {
138         return frequency_;
139 }
140
141 bool can_signal_t::get_send_same() const
142 {
143         return send_same_;
144 }
145
146 bool can_signal_t::get_force_send_changed() const
147 {
148         return force_send_changed_;
149 }
150
151 const std::map<uint8_t, std::string>& can_signal_t::get_states() const
152 {
153         return states_;
154 }
155
156 const std::string can_signal_t::get_states(uint8_t value)
157 {
158         if (value < states_.size())
159                 return states_[value];
160         return std::string();
161 }
162
163 size_t can_signal_t::get_state_count() const
164 {
165         return states_.size();
166 }
167
168 bool can_signal_t::get_writable() const
169 {
170         return writable_;
171 }
172
173 SignalDecoder& can_signal_t::get_decoder()
174 {
175         return decoder_;
176 }
177
178 SignalEncoder& can_signal_t::get_encoder()
179 {
180         return encoder_;
181 }
182
183 bool can_signal_t::get_received() const
184 {
185         return received_;
186 }
187 float can_signal_t::get_last_value() const
188 {
189         return last_value_;
190 }
191
192 void can_signal_t::set_parent(can_message_definition_t* parent)
193 {
194         parent_ = parent;
195 }
196
197 void can_signal_t::set_prefix(std::string val)
198 {
199         prefix_ = val;
200 }
201
202 void can_signal_t::set_received(bool r)
203 {
204         received_ = r;
205 }
206
207 void can_signal_t::set_last_value(float val)
208 {
209         last_value_ = val;
210 }
211
212 void can_signal_t::set_timestamp(uint64_t timestamp)
213 {
214         frequency_.tick(timestamp);
215 }
216