DbcLib 1.0
CAN DBC C++ library.
Loading...
Searching...
No Matches
message.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 <vector>
11#include <map>
12#include <string>
13
14#include "dbc/attribute.h"
15#include "dbc/signal.h"
16
17namespace dbc {
19using SignalList = std::map<std::string, Signal>;
20
22class Message {
23 public:
24 void Ident(uint64_t ident) {
25 ident_ = ident;
26 }
27 [[nodiscard]] uint64_t Ident() const {
28 return ident_;
29 }
30
31 [[nodiscard]] bool IsExtended() const;
32 [[nodiscard]] bool IsJ1939() const;
33 [[nodiscard]] uint32_t CanId() const;
34 [[nodiscard]] uint8_t Priority() const;
35 [[nodiscard]] uint32_t Pgn() const;
36 [[nodiscard]] uint8_t Source() const;
37 [[nodiscard]] bool ExtendedDataPage() const;
38 [[nodiscard]] bool DataPage() const;
39 [[nodiscard]] uint8_t PduFormat() const;
40 [[nodiscard]] uint8_t PduSpecific() const;
41 [[nodiscard]] bool IsPdu1() const;
42 [[nodiscard]] bool IsPdu2() const;
43
45 void Name(const std::string& name) { name_ = name; }
47 [[nodiscard]] const std::string& Name() const { return name_; }
48
50 void Comment(const std::string& comment) { comment_ = comment; }
52 [[nodiscard]] const std::string& Comment() const { return comment_; }
53
54 void NofBytes(size_t bytes);
55 [[nodiscard]] size_t NofBytes() const;
56
58 void Node(const std::string& node) {
59 if (node_.empty()) {
60 node_ = node;
61 }
62 sender_list_.push_back(node);
63 }
65 [[nodiscard]] std::string NodeName() const {
66 return node_;
67 }
68
70 [[nodiscard]] Signal* GetSignal(const std::string& name);
72 [[nodiscard]] const Signal* GetSignal(const std::string& name) const;
74 [[nodiscard]] Signal* GetMultiplexor();
75
77 [[nodiscard]] const Attribute* GetAttribute(const std::string& name) const;
78
80 [[nodiscard]] const SignalList& Signals() const {
81 return signal_list_;
82 }
83
85 [[nodiscard]] const std::vector<std::string>& Senders() const {
86 return sender_list_;
87 }
88
90 [[nodiscard]] const std::vector<Attribute>& Attributes() const {
91 return attribute_list_;
92 }
93
95 [[nodiscard]] bool IsNodeSender(const std::string& node_name) const;
96
98 [[nodiscard]] const std::vector<uint8_t>& Data() const {return data_;}
99
111 bool UpdateData(const std::vector<uint8_t>& message,
112 size_t offset = 0, size_t data_index = 0);
114 void ParseMessage(uint64_t ns1970, uint32_t can_id);
116 void ResetSequenceNumber() {sequence_number_ = 0;}
118 uint8_t NextSequenceNumber() const {return sequence_number_;}
119
120 void ResetSampleCounter() const;
122 void StepSampleCounter() const {++sample_counter_;}
124 size_t SampleCounter() const {return sample_counter_;}
125
128
130 Signal& CreateSignal(const std::string& name);
131
132 private:
133 uint64_t ident_;
134 std::string name_;
135 std::string comment_;
136 std::string node_;
137 std::vector<std::string> sender_list_;
138
139 std::vector<Attribute> attribute_list_;
140
141 SignalList signal_list_;
142
143 mutable size_t sample_counter_ = 0;
144 std::vector<uint8_t> data_;
145 uint8_t sequence_number_ = 0;
146};
147
148} // namespace dbc
All DBC network objects may have attributes attached to them.
Support class for handling attributes of network objects.
Definition: attribute.h:49
DBC message configuration object.
Definition: message.h:22
uint32_t Pgn() const
J1939 PGN.
const std::string & Comment() const
Returns the descriptive text.
Definition: message.h:52
bool IsExtended() const
True if 29-bit ID.
const std::vector< Attribute > & Attributes() const
Returns a list of attributes.
Definition: message.h:90
void ResetSampleCounter() const
Reset the sample counters.
void Name(const std::string &name)
Sets the message name.
Definition: message.h:45
void StepSampleCounter() const
Increments the internal sample counters.
Definition: message.h:122
void Node(const std::string &node)
Sets a node name.
Definition: message.h:58
Attribute & CreateAttribute(const Attribute &definition)
Creates an attributes by using its definition.
uint8_t NextSequenceNumber() const
Returns the next sequence number.
Definition: message.h:118
uint32_t CanId() const
Returns CAN ID.
bool UpdateData(const std::vector< uint8_t > &message, size_t offset=0, size_t data_index=0)
Update the internal data buffer.
bool IsNodeSender(const std::string &node_name) const
Returns true if the node is a sender.
void ParseMessage(uint64_t ns1970, uint32_t can_id)
Parses the message. Internal usage.
void NofBytes(size_t bytes)
Sets the number of bytes.
uint64_t Ident() const
Definition: message.h:27
void Ident(uint64_t ident)
Definition: message.h:24
bool IsPdu1() const
True if PDU1.
uint8_t Source() const
J1939 Source.
std::string NodeName() const
Returns the node name.
Definition: message.h:65
const SignalList & Signals() const
Returns the signal list.
Definition: message.h:80
bool ExtendedDataPage() const
J1938 Data Page Flag.
uint8_t Priority() const
J1939 Priority.
size_t SampleCounter() const
Returns number of samples.
Definition: message.h:124
const Signal * GetSignal(const std::string &name) const
Returns a signal by its name.
uint8_t PduSpecific() const
J1939 PS.
Signal * GetMultiplexor()
Returns the multiplexer signal object.
Signal * GetSignal(const std::string &name)
Returns a signal by its name.
bool IsJ1939() const
True if it is a J1939 message.
void Comment(const std::string &comment)
Sets the descriptive text.
Definition: message.h:50
const std::vector< std::string > & Senders() const
Returns a list of sender names.
Definition: message.h:85
const std::string & Name() const
Returns the message name.
Definition: message.h:47
bool IsPdu2() const
True if PDU2.
bool DataPage() const
J1938 Data Page Flag.
size_t NofBytes() const
Returns the number of bytes.
const Attribute * GetAttribute(const std::string &name) const
Returns an attribute by its name.
uint8_t PduFormat() const
J1939 PF.
const std::vector< uint8_t > & Data() const
Returns the last message data buffer.
Definition: message.h:98
void ResetSequenceNumber()
Reset the internal sequence counter.
Definition: message.h:116
Signal & CreateSignal(const std::string &name)
Create a new signal by its name.
Interface against a DBC signal configuration.
Definition: signal.h:68
Main namespace for the DBC library.
Definition: attribute.h:13
std::map< std::string, Signal > SignalList
Sorted list of signal objects.
Definition: message.h:19
Interface against a DBC signal configuration.