DbcLib 1.0
CAN DBC C++ library.
Loading...
Searching...
No Matches
signal.h
Go to the documentation of this file.
1/*
2* Copyright 2022 Ingemar Hedvall
3* SPDX-License-Identifier: MIT
4 */
8#pragma once
9#include <cstdint>
10#include <string>
11#include <vector>
12#include <map>
13#include "dbc/attribute.h"
14#include "dbc/isampleobserver.h"
15
16namespace dbc {
19 bool valid = false;
20 int64_t signed_value = 0;
21 uint64_t unsigned_value = 0;
22 double float_value = 0;
23 std::vector<uint8_t> array_value;
25 void Clear() {
26 valid = false;
27 signed_value = 0;
29 float_value = 0;
30 array_value.clear();
31 }
32};
33
35enum class SignalDataType : int {
38 FloatData,
40};
41
48enum class MuxType : int {
53};
54
56using RangePair = std::pair<size_t, size_t>;
57
60 std::string multiplexor;
61 std::vector<RangePair> range_list;
62 [[nodiscard]] bool InRange(size_t value) const;
63};
64
68class Signal {
69 public:
70 virtual ~Signal();
71
73 void Name(const std::string& name) { name_ = name; }
75 [[nodiscard]] const std::string& Name() const { return name_; }
76
78 void Unit(const std::string& unit) { unit_ = unit; }
80 [[nodiscard]] const std::string& Unit() const { return unit_; }
81
83 void Comment(const std::string& comment) { comment_ = comment; }
85 [[nodiscard]] const std::string& Comment() const { return comment_; }
86
88 void DataType(SignalDataType type) { data_type_ = type; }
90 [[nodiscard]] SignalDataType DataType() const { return data_type_; }
92 [[nodiscard]] std::string DataTypeAsString() const;
93
95 void Mux(MuxType type) { mux_type_ = type; }
97 [[nodiscard]] MuxType Mux() const { return mux_type_; }
99 [[nodiscard]] std::string MuxAsString() const;
101 void MuxValue(int value) { mux_value_ = value; }
103 [[nodiscard]] int MuxValue() const { return mux_value_; }
104
106 void BitStart(size_t start) { bit_start_ = start; }
108 [[nodiscard]] size_t BitStart() const { return bit_start_; }
109
111 void BitLength(size_t length) { bit_length_ = length; }
113 [[nodiscard]] size_t BitLength() const { return bit_length_; }
114
116 void LittleEndian(bool endian) { little_endian_ = endian; }
118 [[nodiscard]] bool LittleEndian() const { return little_endian_; }
119
120 void Scale(double scale) { scale_ = scale; }
121 [[nodiscard]] double Scale() const { return scale_; }
122
123 void Offset(double offset) { offset_ = offset; }
124 [[nodiscard]] double Offset() const { return offset_; }
125
126 void Min(double min) { min_ = min; }
127 [[nodiscard]] double Min() const { return min_; }
128
129 void Max(double max) { max_ = max; }
130 [[nodiscard]] double Max() const { return max_; }
131
133 void EnumList(const std::map<int64_t, std::string>& enum_list);
135 [[nodiscard]] const std::map<int64_t, std::string>& EnumList() const;
136
138 void Receivers(const std::vector<std::string>& receiver_list);
140 [[nodiscard]] const std::vector<std::string>& Receivers() const;
141
143 [[nodiscard]] const std::vector<Attribute>& Attributes() const {
144 return attribute_list_;
145 }
146
148 void MessageId(uint64_t message_id) { message_id_ = message_id;}
150 [[nodiscard]] uint64_t MessageId() const { return message_id_; }
151
152 [[nodiscard]] bool IsMultiplexed() const;
154 [[nodiscard]] Attribute& CreateAttribute(const Attribute& definition);
156 [[nodiscard]] ExtendedMux& GetExtendedMux();
157
159 [[nodiscard]] std::string GetEnumString(int64_t index) const;
160
162 void ParseMessage(const std::vector<uint8_t>& message, uint64_t ns1970,
163 uint32_t can_id);
165 void ResetSampleCounter() const {sample_counter_ = 0;}
167 void StepSampleCounter() const {++sample_counter_;}
169 size_t SampleCounter() const {return sample_counter_;}
170
172 void SampleTime(uint64_t ns1970) {sample_time_ = ns1970;}
174 [[nodiscard]] uint64_t SampleTime() const {return sample_time_;}
175
177 void SampleCanId(uint32_t can_id) {sample_can_id_ = can_id;}
179 [[nodiscard]] uint64_t SampleCanId() const {return sample_can_id_;}
180
181 void Valid(bool valid) {valid_ = valid;}
182 [[nodiscard]] bool Valid() const {return valid_;}
183
185 template <typename T>
186 bool ChannelValue( T& value ) const;
187
189 template <typename T>
190 bool EngValue( T& value ) const;
191
193 void AttachObserver(ISampleObserver* observer) const;
195 void DetachObserver(const ISampleObserver* observer) const;
196 private:
197 std::string name_;
198 std::string comment_;
199 std::string unit_;
200
201 std::vector<std::string> receiver_list_;
202
203 MuxType mux_type_ = MuxType::NotMultiplexed;
204 int mux_value_ = 0;
205 size_t bit_start_ = 0;
206 size_t bit_length_ = 0;
207 bool little_endian_ = true;
209 double scale_ = 1.0;
210 double offset_ = 0.0;
211 double min_ = 0.0;
212 double max_ = 0.0;
213 ExtendedMux extended_mux_;
214
215 std::vector<Attribute> attribute_list_;
216 std::map<int64_t, std::string> enum_list_;
217
218 SignalValue channel_value_;
219
220 uint64_t message_id_ = 0;
221 mutable size_t sample_counter_ = 0;
222 bool valid_ = true;
223 uint64_t sample_time_ = 0;
224 uint32_t sample_can_id_ = 0;
225
226 mutable std::vector<ISampleObserver*> observer_list_;
227 void FireOnSample();
228
229};
230
231template <typename T>
232bool Signal::ChannelValue(T& value) const {
233 bool valid = channel_value_.valid && Valid();
234 value = {};
235
236 switch (data_type_) {
238 try {
239 const auto temp = channel_value_.signed_value;
240 value = static_cast<T>(temp);
241 } catch (const std::exception&) {
242 valid = false;
243 }
244 break;
245 }
246
248 size_t bytes = bit_length_ / 8;
249 if ((bit_length_ % 8) != 0) {
250 ++bytes;
251 }
252 if (bytes > 8) {
253 valid = false;
254 } else {
255 try {
256 const auto temp = channel_value_.unsigned_value;
257 value = static_cast<T>(temp);
258 } catch (const std::exception&) {
259 valid = false;
260 }
261 }
262 break;
263 }
264
267 try {
268 const auto temp = channel_value_.float_value;
269 value = static_cast<T>(temp);
270 } catch (const std::exception&) {
271 valid = false;
272 }
273 break;
274 }
275
276 default:
277 valid = false;
278 break;
279 }
280 return valid;
281}
282
284template <>
285bool Signal::ChannelValue(std::string& value) const;
286
288template <>
290
291
292template <typename T>
293bool Signal::EngValue(T& value) const {
294 bool valid = false;
295 value = {};
296 bool need_to_convert = true;
297 // If it is an enumerate. We could convert the enum string but that's weird.
298 if (!enum_list_.empty()) {
299 need_to_convert = false;
300 }
301 // Avoid truncation
302 if (scale_ == 1.0 && offset_ == 0) {
303 need_to_convert = false;
304 }
305
306 switch (data_type_) {
308 int64_t channel = 0;
309 valid = ChannelValue(channel);
310
311 if (need_to_convert) {
312 auto temp = static_cast<double>(channel);
313 temp *= scale_;
314 temp += offset_;
315 value = static_cast<T>(temp);
316 } else {
317 value = static_cast<T>(channel);
318 }
319 break;
320 }
321
323 uint64_t channel = 0;
324 valid = ChannelValue(channel);
325 if (need_to_convert) {
326 auto temp = static_cast<double>(channel);
327 temp *= scale_;
328 temp += offset_;
329 value = static_cast<T>(temp);
330 } else {
331 value = static_cast<T>(channel);
332 }
333 break;
334 }
335
337 float channel = 0;
338 valid = ChannelValue(channel);
339 if (need_to_convert) {
340 auto temp = static_cast<double>(channel);
341 temp *= scale_;
342 temp += offset_;
343 value = static_cast<T>(temp);
344 } else {
345 value = static_cast<T>(channel);
346 }
347 break;
348 }
349
351 double channel = 0;
352 valid = ChannelValue(channel);
353 if (need_to_convert) {
354 auto temp = channel;
355 temp *= scale_;
356 temp += offset_;
357 value = static_cast<T>(temp);
358 } else {
359 value = static_cast<T>(channel);
360 }
361 break;
362 }
363
364 default:
365 break;
366 }
367 return valid;
368}
369
371template <>
372bool Signal::EngValue(std::string& value) const;
373
374} // namespace dbc
All DBC network objects may have attributes attached to them.
Support class for handling attributes of network objects.
Definition: attribute.h:49
Interface that handles samples. Internal usage.
Definition: isampleobserver.h:13
Interface against a DBC signal configuration.
Definition: signal.h:68
void BitLength(size_t length)
Sets the bit length.
Definition: signal.h:111
int MuxValue() const
Returns the multiplexor value.
Definition: signal.h:103
size_t BitLength() const
Returns the bit length.
Definition: signal.h:113
bool EngValue(T &value) const
Returns the scaled engineering value.
Definition: signal.h:293
std::string DataTypeAsString() const
Returns the data type as text.
double Offset() const
Return offset.
Definition: signal.h:124
void Offset(double offset)
Sets the offset.
Definition: signal.h:123
void Mux(MuxType type)
Sets the multiplexer type.
Definition: signal.h:95
void Max(double max)
Sets the max range.
Definition: signal.h:129
void SampleCanId(uint32_t can_id)
Sets the CAN ID for the sample.
Definition: signal.h:177
void SampleTime(uint64_t ns1970)
Sets the sample time.
Definition: signal.h:172
Attribute & CreateAttribute(const Attribute &definition)
Creates an attribute.
uint64_t SampleCanId() const
Returns the CAN ID for latest sample.
Definition: signal.h:179
bool IsMultiplexed() const
True if multiplexed signal.
uint64_t MessageId() const
Returns the message ID that the signal belongs to.
Definition: signal.h:150
const std::vector< Attribute > & Attributes() const
Returns the attribute list.
Definition: signal.h:143
size_t SampleCounter() const
Returns the sample counter.
Definition: signal.h:169
std::string GetEnumString(int64_t index) const
Returns the enumerate text for an index.
bool Valid() const
Trie if value is valid.
Definition: signal.h:182
void MuxValue(int value)
Sets the multiplexor value.
Definition: signal.h:101
void Unit(const std::string &unit)
Sets the unit of measure.
Definition: signal.h:78
void AttachObserver(ISampleObserver *observer) const
Attach a sample observer.
const std::vector< std::string > & Receivers() const
Return the receiver list.
bool ChannelValue(T &value) const
Returns the channel value.
Definition: signal.h:232
virtual ~Signal()
Destructor.
ExtendedMux & GetExtendedMux()
Creates an extended multiplexor struct.
void Scale(double scale)
Sets the scaling constant.
Definition: signal.h:120
void ResetSampleCounter() const
Resets the sample counter.
Definition: signal.h:165
size_t BitStart() const
Returns the start bit.
Definition: signal.h:108
uint64_t SampleTime() const
Returns the sample time.
Definition: signal.h:174
const std::string & Unit() const
Returns the unit of measure.
Definition: signal.h:80
void DetachObserver(const ISampleObserver *observer) const
Detach a sample observer.
MuxType Mux() const
Returns the multiplexer type.
Definition: signal.h:97
void Name(const std::string &name)
Sets the signal name.
Definition: signal.h:73
void BitStart(size_t start)
Sets the start bit.
Definition: signal.h:106
void EnumList(const std::map< int64_t, std::string > &enum_list)
Sets the enumeration.
double Scale() const
Scaling constant.
Definition: signal.h:121
SignalDataType DataType() const
Returns the data type.
Definition: signal.h:90
void Receivers(const std::vector< std::string > &receiver_list)
Sets the receiver list.
void Min(double min)
Sets min range.
Definition: signal.h:126
void Valid(bool valid)
Set to true if valid value.
Definition: signal.h:181
const std::string & Comment() const
Returns the descriptive text.
Definition: signal.h:85
const std::map< int64_t, std::string > & EnumList() const
Returns the enumeration.
void DataType(SignalDataType type)
Sets the data type.
Definition: signal.h:88
std::string MuxAsString() const
Returns the multiplexer type as text.
bool LittleEndian() const
Return true if little endian byte order.
Definition: signal.h:118
void StepSampleCounter() const
Steps the sample counter.
Definition: signal.h:167
const std::string & Name() const
Returns the signal name.
Definition: signal.h:75
double Max() const
Max range.
Definition: signal.h:130
double Min() const
Min range.
Definition: signal.h:127
void Comment(const std::string &comment)
Sets the descriptive text.
Definition: signal.h:83
void MessageId(uint64_t message_id)
Sets the signals message ID.
Definition: signal.h:148
void ParseMessage(const std::vector< uint8_t > &message, uint64_t ns1970, uint32_t can_id)
Parse out the signal value from a message data buffer.
void LittleEndian(bool endian)
Set true if little endian byte order.
Definition: signal.h:116
Internal object that define an observer interface that handle samples.
Main namespace for the DBC library.
Definition: attribute.h:13
SignalDataType
Signal data type.
Definition: signal.h:35
@ SignedData
Signed integer.
@ DoubleData
Double value.
@ UnsignedData
Unsigned integer.
@ FloatData
Float value.
MuxType
Multiplexer type.
Definition: signal.h:48
@ Multiplexor
Multiplexor signal.
@ Multiplexed
Multiplexed signal.
@ NotMultiplexed
Normal signal.
@ ExtendedMultiplexor
Extended multiplexor signal.
std::pair< size_t, size_t > RangePair
Min and Max range definition.
Definition: signal.h:56
Support function for the extended multiplexor functionality.
Definition: signal.h:59
bool InRange(size_t value) const
True if value is active.
std::vector< RangePair > range_list
Active range for the multiplexor.
Definition: signal.h:61
std::string multiplexor
Signal name of the multiplexor.
Definition: signal.h:60
Support function that holds the channel value for a signal.
Definition: signal.h:18
bool valid
True if the value is valid.
Definition: signal.h:19
void Clear()
Resets the value.
Definition: signal.h:25
std::vector< uint8_t > array_value
Array or string value.
Definition: signal.h:23
int64_t signed_value
Integer value.
Definition: signal.h:20
uint64_t unsigned_value
Unsigned value.
Definition: signal.h:21
double float_value
Float value.
Definition: signal.h:22