28246a05bb99a51f998d2090fc2d06044affae11
[apps/agl-service-can-low-level.git] / low-can-binding / 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 "signals.hpp"
21
22 #include "../binding/application.hpp"
23 #include "../utils/signals.hpp"
24 #include "can-decoder.hpp"
25 #include "can-bus.hpp"
26 #include "../diagnostic/diagnostic-message.hpp"
27 #include "canutil/write.h"
28
29 std::string signal_t::prefix_ = "messages";
30
31 signal_t::signal_t(
32         std::string generic_name,
33         uint32_t bit_position,
34         uint32_t bit_size,
35         float factor,
36         float offset,
37         float min_value,
38         float max_value,
39         frequency_clock_t frequency,
40         bool send_same,
41         bool force_send_changed,
42         std::map<uint8_t, std::string> states,
43         bool writable,
44         signal_decoder decoder,
45         signal_encoder encoder,
46         bool received,
47         std::pair<bool, int> multiplex,
48         bool is_big_endian,
49         sign_t sign,
50         int32_t bit_sign_position,
51         std::string unit)
52         : parent_{nullptr},
53          generic_name_{ generic_name }
54         , bit_position_{ bit_position }
55         , bit_size_{ bit_size }
56         , factor_{ factor }
57         , offset_{ offset }
58         , min_value_{min_value}
59         , max_value_{max_value}
60         , frequency_{frequency}
61         , send_same_{send_same}
62         , force_send_changed_{force_send_changed}
63         , states_{states}
64         , writable_{writable}
65         , decoder_{decoder}
66         , encoder_{encoder}
67         , received_{received}
68         , last_value_{.0f}
69         , multiplex_{multiplex}
70         , is_big_endian_{is_big_endian}
71         , sign_{sign}
72         , bit_sign_position_{bit_sign_position}
73         , unit_{unit}
74         ,bit_position_is_swapped_{false}
75 {}
76
77 signal_t::signal_t(
78         std::string generic_name,
79         uint32_t bit_position,
80         uint32_t bit_size,
81         float factor,
82         float offset,
83         float min_value,
84         float max_value,
85         frequency_clock_t frequency,
86         bool send_same,
87         bool force_send_changed,
88         std::map<uint8_t, std::string> states,
89         bool writable,
90         signal_decoder decoder,
91         signal_encoder encoder,
92         bool received)
93         : generic_name_{ generic_name }
94         , bit_position_{ bit_position }
95         , bit_size_{ bit_size }
96         , factor_{ factor }
97         , offset_{ offset }
98         , min_value_{min_value}
99         , max_value_{max_value}
100         , frequency_{frequency}
101         , send_same_{send_same}
102         , force_send_changed_{force_send_changed}
103         , states_{states}
104         , writable_{writable}
105         , decoder_{decoder}
106         , encoder_{encoder}
107         , received_{received}
108         , bit_position_is_swapped_{false}
109 {}
110
111 std::shared_ptr<message_definition_t> signal_t::get_message() const
112 {
113         return parent_;
114 }
115
116 const std::string signal_t::get_generic_name() const
117 {
118         return generic_name_;
119 }
120
121 const std::string signal_t::get_name() const
122 {
123         return prefix_ + "." + generic_name_;
124 }
125
126 uint32_t signal_t::get_bit_position() const
127 {
128         return bit_position_;
129 }
130
131 uint32_t signal_t::get_bit_size() const
132 {
133         return bit_size_;
134 }
135
136 float signal_t::get_factor() const
137 {
138         return factor_;
139 }
140
141 float signal_t::get_offset() const
142 {
143         return offset_;
144 }
145
146 frequency_clock_t& signal_t::get_frequency()
147 {
148         return frequency_;
149 }
150
151 bool signal_t::get_send_same() const
152 {
153         return send_same_;
154 }
155
156 const std::string signal_t::get_states(uint8_t value)
157 {
158         if ( states_.count(value) > 0 )
159                 return states_[value];
160         return std::string();
161 }
162
163 uint64_t signal_t::get_states(const std::string& value) const
164 {
165         uint64_t ret = -1;
166         for( const auto& state: states_)
167         {
168                 if(caseInsCompare(state.second, value))
169                 {
170                         ret = (uint64_t)state.first;
171                         break;
172                 }
173         }
174         return ret;
175 }
176
177 bool signal_t::get_writable() const
178 {
179         return writable_;
180 }
181
182 signal_decoder& signal_t::get_decoder()
183 {
184         return decoder_;
185 }
186
187 signal_encoder& signal_t::get_encoder()
188 {
189         return encoder_;
190 }
191
192 bool signal_t::get_received() const
193 {
194         return received_;
195 }
196
197 float signal_t::get_last_value() const
198 {
199         return last_value_;
200 }
201
202 std::pair<float, uint64_t> signal_t::get_last_value_with_timestamp() const
203 {
204         return std::make_pair(last_value_, frequency_.get_last_tick());
205 }
206
207 void signal_t::set_parent(std::shared_ptr<message_definition_t> parent)
208 {
209         parent_ = parent;
210 }
211
212 void signal_t::set_received(bool r)
213 {
214         received_ = r;
215 }
216
217 void signal_t::set_last_value(float val)
218 {
219         last_value_ = val;
220 }
221
222 void signal_t::set_timestamp(uint64_t timestamp)
223 {
224         frequency_.tick(timestamp);
225 }
226
227 void signal_t::set_bit_position(uint32_t bit_position)
228 {
229         bit_position_=bit_position;
230 }
231
232 std::pair<bool,int> signal_t::get_multiplex() const
233 {
234         return multiplex_;
235 }
236
237 bool signal_t::get_is_big_endian() const
238 {
239         return is_big_endian_;
240 }
241
242 sign_t signal_t::get_sign() const
243 {
244         return sign_;
245 }
246
247 int32_t signal_t::get_bit_sign_position() const
248 {
249         return bit_sign_position_;
250 }
251
252 const std::string signal_t::get_unit() const
253 {
254         return unit_;
255 }
256
257 bool signal_t::bit_position_is_swapped() const
258 {
259         return bit_position_is_swapped_;
260 }
261
262 void signal_t::bit_position_is_swapped_reverse()
263 {
264         bit_position_is_swapped_ = !bit_position_is_swapped_;
265 }