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