Rename some of the classes removing can- prefix
[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 "message/can-message.hpp"
26 #include "can-bus.hpp"
27 #include "../diagnostic/diagnostic-message.hpp"
28 #include "canutil/write.h"
29
30 std::string signal_t::prefix_ = "messages";
31
32 signal_t::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         signal_decoder decoder,
46         signal_encoder encoder,
47         bool received,
48         std::pair<bool,int> multiplex,
49         bool is_big_endian,
50         bool is_signed,
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         , is_signed_{is_signed}
72         , unit_{unit}
73 {}
74
75 signal_t::signal_t(
76         std::string generic_name,
77         uint8_t bit_position,
78         uint8_t bit_size,
79         float factor,
80         float offset,
81         float min_value,
82         float max_value,
83         frequency_clock_t frequency,
84         bool send_same,
85         bool force_send_changed,
86         std::map<uint8_t, std::string> states,
87         bool writable,
88         signal_decoder decoder,
89         signal_encoder encoder,
90         bool received)
91         : generic_name_{ generic_name }
92         , bit_position_{ bit_position }
93         , bit_size_{ bit_size }
94         , factor_{ factor }
95         , offset_{ offset }
96         , min_value_{min_value}
97         , max_value_{max_value}
98         , frequency_{frequency}
99         , send_same_{send_same}
100         , force_send_changed_{force_send_changed}
101         , states_{states}
102         , writable_{writable}
103         , decoder_{decoder}
104         , encoder_{encoder}
105         , received_{received}
106 {}
107
108 std::shared_ptr<message_definition_t> signal_t::get_message() const
109 {
110         return parent_;
111 }
112
113 const std::string signal_t::get_generic_name() const
114 {
115         return generic_name_;
116 }
117
118 const std::string signal_t::get_name() const
119 {
120         return prefix_ + "." + generic_name_;
121 }
122
123 uint8_t signal_t::get_bit_position() const
124 {
125         return bit_position_;
126 }
127
128 uint8_t signal_t::get_bit_size() const
129 {
130         return bit_size_;
131 }
132
133 float signal_t::get_factor() const
134 {
135         return factor_;
136 }
137
138 float signal_t::get_offset() const
139 {
140         return offset_;
141 }
142
143 frequency_clock_t& signal_t::get_frequency()
144 {
145         return frequency_;
146 }
147
148 bool signal_t::get_send_same() const
149 {
150         return send_same_;
151 }
152
153 const std::string signal_t::get_states(uint8_t value)
154 {
155         if ( states_.count(value) > 0 )
156                 return states_[value];
157         return std::string();
158 }
159
160 uint64_t signal_t::get_states(const std::string& value) const
161 {
162         uint64_t ret = -1;
163         for( const auto& state: states_)
164         {
165                 if(state.second == value)
166                 {
167                         ret = (uint64_t)state.first;
168                         break;
169                 }
170         }
171         return ret;
172 }
173
174 bool signal_t::get_writable() const
175 {
176         return writable_;
177 }
178
179 signal_decoder& signal_t::get_decoder()
180 {
181         return decoder_;
182 }
183
184 signal_encoder& signal_t::get_encoder()
185 {
186         return encoder_;
187 }
188
189 bool signal_t::get_received() const
190 {
191         return received_;
192 }
193
194 float signal_t::get_last_value() const
195 {
196         return last_value_;
197 }
198
199 std::pair<float, uint64_t> signal_t::get_last_value_with_timestamp() const
200 {
201         return std::make_pair(last_value_, frequency_.get_last_tick());
202 }
203
204 void signal_t::set_parent(std::shared_ptr<message_definition_t> parent)
205 {
206         parent_ = parent;
207 }
208
209 void signal_t::set_received(bool r)
210 {
211         received_ = r;
212 }
213
214 void signal_t::set_last_value(float val)
215 {
216         last_value_ = val;
217 }
218
219 void signal_t::set_timestamp(uint64_t timestamp)
220 {
221         frequency_.tick(timestamp);
222 }
223
224 std::pair<bool,int> signal_t::get_multiplex() const
225 {
226         return multiplex_;
227 }
228
229 bool signal_t::get_is_big_endian() const
230 {
231         return is_big_endian_;
232 }
233
234 bool signal_t::get_is_signed() const
235 {
236         return is_signed_;
237 }
238
239 const std::string signal_t::get_unit() const
240 {
241         return unit_;
242 }