DbcLib 1.0
CAN DBC C++ library.
Loading...
Searching...
No Matches
network.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 <vector>
10#include <map>
11#include "dbc/envvar.h"
12#include "dbc/attribute.h"
13#include "dbc/node.h"
14#include "dbc/message.h"
15#include "dbc/signal.h"
16#include "dbc/signalgroup.h"
17
18namespace dbc {
20using EnumMap = std::map<int64_t, std::string>;
21
23enum class ProtocolType : int {
24 StandardCAN = 0,
25 J1939 = 1,
26 NMEA2000 = 2,
27 OBD2 = 3
28};
29
31enum class BusType : int {
32 CAN = 0
33};
34
36using MessageList = std::map<uint64_t, Message>;
37
39class Network {
40 public:
41 [[nodiscard]] std::string Name() const;
42
44 void Filename(const std::string& filename) { filename_ = filename;}
46 [[nodiscard]] std::string Filename() const {return filename_;};
47
54 [[nodiscard]] ProtocolType Protocol() const;
56 [[nodiscard]] std::string ProtocolAsString() const;
57
59 void Bus(BusType type);
61 [[nodiscard]] BusType Bus() const;
63 [[nodiscard]] std::string BusAsString() const;
64
66 void Version(const std::string& version) { version_ = version; }
68 [[nodiscard]] const std::string& Version() const {
69 return version_;
70 }
72 void Comment(const std::string& comment) {
73 comment_ = comment;
74 }
76 [[nodiscard]] const std::string& Comment() const { return comment_; }
77
79 [[nodiscard]] Node* GetNode(const std::string& name);
81 [[nodiscard]] const Node* GetNode(const std::string& name) const;
83 [[nodiscard]] const Node* GetNodeBySource(uint8_t source) const;
84
86 void J1939(bool j1939) {j1939_ = j1939;}
88 [[nodiscard]] bool J1939() const {return j1939_;}
89
91 [[nodiscard]] Message* GetMessage(uint64_t message_id);
93 [[nodiscard]] const Message* GetMessage(uint64_t message_id) const;
95 [[nodiscard]] Message* GetMessageByCanId(uint64_t can_id);
97 [[nodiscard]] const Message* GetMessageByCanId(uint64_t can_id) const;
99 [[nodiscard]] const Message* GetMessageByName(const std::string &name) const;
101 [[nodiscard]] Message* GetMessageByPgn(uint32_t pgn);
103 [[nodiscard]] Message* GetMessageByPgnAndSource(uint32_t pgn, uint8_t source);
104
106 [[nodiscard]] Signal* GetSignal(uint64_t message_id,
107 const std::string& signal_name);
109 [[nodiscard]] const Signal* GetSignal(uint64_t message_id,
110 const std::string& signal_name) const;
112 [[nodiscard]] const Signal* GetSignalByCanId(uint64_t can_id,
113 const std::string& signal_name) const;
115 [[nodiscard]] const Signal* GetSignalByName(
116 const std::string& signal_name) const;
117
119 [[nodiscard]] const SignalGroup* GetSignalGroup(uint64_t message_id,
120 const std::string& name) const;
122 [[nodiscard]] const SignalGroup* GetSignalGroupByName(
123 const std::string& name) const;
124
126 [[nodiscard]] const std::map<std::string, EnvVar>& EnvVars() const {
127 return env_var_list_;
128 }
129
131 [[nodiscard]] const MessageList& Messages() const {
132 return message_list_;
133 }
134
136 [[nodiscard]] const std::map<std::string, Node>& Nodes() const {
137 return node_list_;
138 }
139
141 [[nodiscard]] const std::vector<SignalGroup>& SignalGroups() const {
142 return signal_group_list_;
143 }
144
146 [[nodiscard]] const std::map<std::string, EnumMap>& Enums() const {
147 return value_table_list_;
148 }
149
151 [[nodiscard]] const std::vector<Attribute>& Attributes() const {
152 return attribute_list_;
153 }
155 [[nodiscard]] Attribute* GetAttribute(const std::string& name);
157 [[nodiscard]] const Attribute* GetAttribute(const std::string& name) const;
158
160 void AddValueTable(const std::string& name, const EnumMap& list);
161
163 void AddSignalGroup(const SignalGroup& group);
164
165 // Parser only functions
167 [[nodiscard]] EnvVar& GetEnvVar(const std::string& name);
169 [[nodiscard]] Node& CreateNode(const std::string& name);
171 [[nodiscard]] Attribute& CreateDefinition(const std::string& name);
173 [[nodiscard]] Attribute& CreateAttribute(const Attribute& definition);
175 [[nodiscard]] Message& CreateMessage(uint64_t message_id);
177 [[nodiscard]] Message* LastMessage();
178 private:
179 std::string filename_;
180 std::string version_;
181 std::string comment_;
182 std::map<std::string, EnvVar> env_var_list_;
183 uint64_t last_message_id_ = 0;
184 bool j1939_ = false;
185
186 // Only define the name and type of value
187 std::map<std::string, Attribute> definition_list_;
188 std::vector<Attribute> attribute_list_;
189
190 using NodeList = std::map<std::string, Node>;
191 NodeList node_list_;
192 MessageList message_list_;
193
194 std::map<std::string, EnumMap> value_table_list_;
195 std::vector<SignalGroup> signal_group_list_;
196};
197
198} // namespace dbc
All DBC network objects may have attributes attached to them.
Support class for handling attributes of network objects.
Definition: attribute.h:49
Wrapper around an environment DBC variable.
Definition: envvar.h:34
DBC message configuration object.
Definition: message.h:22
Interface against a DBC network configuration.
Definition: network.h:39
std::string ProtocolAsString() const
Returns the network protocol as text.
const Attribute * GetAttribute(const std::string &name) const
Returns an attribute by its name.
const Node * GetNode(const std::string &name) const
Returns the node by its name.
Attribute & CreateDefinition(const std::string &name)
Parser function that creates a definition.
const SignalGroup * GetSignalGroupByName(const std::string &name) const
Returns a signal group by its group name.
ProtocolType Protocol() const
Returns what type of protocol the bus is using.
const Signal * GetSignalByName(const std::string &signal_name) const
Returns a signal by its name.
void Protocol(ProtocolType type)
Sets the protocol type. Should be included in the file but sometimes is missing.
const std::map< std::string, EnvVar > & EnvVars() const
Returns the list of environment variables.
Definition: network.h:126
const MessageList & Messages() const
Returns the list of messages.
Definition: network.h:131
const Message * GetMessageByName(const std::string &name) const
Returns a message object by its name.
bool J1939() const
Returns true if this is a J1939 bus.
Definition: network.h:88
Attribute & CreateAttribute(const Attribute &definition)
Parser function that creates an attribute.
Message * GetMessageByCanId(uint64_t can_id)
Returns a message object by its CAN ID.
Node * GetNode(const std::string &name)
Returns the node by its name.
void Bus(BusType type)
Sets the type of bus.
Message * GetMessageByPgn(uint32_t pgn)
Returns a message object by its PGN.
EnvVar & GetEnvVar(const std::string &name)
Parser function that gets/creates an environment variable.
Message & CreateMessage(uint64_t message_id)
Parser function that creates a message.
void Filename(const std::string &filename)
Sets the file name.
Definition: network.h:44
const Signal * GetSignalByCanId(uint64_t can_id, const std::string &signal_name) const
Returns a signal by its CAN ID and signal name.
Message * GetMessage(uint64_t message_id)
Returns a message object by its message ID.
Signal * GetSignal(uint64_t message_id, const std::string &signal_name)
Returns a signal by its message ID and signal name.
const std::string & Version() const
Returns the DBC file version.
Definition: network.h:68
const std::vector< SignalGroup > & SignalGroups() const
Returns the list of signal groups.
Definition: network.h:141
void Comment(const std::string &comment)
Sets the descriptive text for the DBC file.
Definition: network.h:72
const std::string & Comment() const
Returns the desciptive text.
Definition: network.h:76
void AddSignalGroup(const SignalGroup &group)
Adds a signal group.
BusType Bus() const
Returns the type of bus.
const Signal * GetSignal(uint64_t message_id, const std::string &signal_name) const
Returns a signal by its message ID and signal name.
const Message * GetMessage(uint64_t message_id) const
Returns a message object by its message ID.
std::string BusAsString() const
Returns the type of bus as text.
const std::map< std::string, Node > & Nodes() const
Returns the list of Nodes.
Definition: network.h:136
Message * LastMessage()
Returns the last message.
void AddValueTable(const std::string &name, const EnumMap &list)
Adds a enumerate.
const Node * GetNodeBySource(uint8_t source) const
Returns the node by its source number.
std::string Name() const
Network name.
std::string Filename() const
Returns the file name.
Definition: network.h:46
const SignalGroup * GetSignalGroup(uint64_t message_id, const std::string &name) const
Returns a signal group by its message ID and group name.
const std::vector< Attribute > & Attributes() const
Returns the list of attributes.
Definition: network.h:151
void Version(const std::string &version)
Sets the DBC (file) version.
Definition: network.h:66
void J1939(bool j1939)
Set to true if this is a J1939 bus.
Definition: network.h:86
const Message * GetMessageByCanId(uint64_t can_id) const
Returns a message object by its CAN ID.
Attribute * GetAttribute(const std::string &name)
Returns an attribute by its name.
Node & CreateNode(const std::string &name)
Parser function that creates a node.
const std::map< std::string, EnumMap > & Enums() const
Returns the list of enumerates.
Definition: network.h:146
Message * GetMessageByPgnAndSource(uint32_t pgn, uint8_t source)
Returns a message object by its PGN and source number.
Interface against a DBC node which normally is an ECU.
Definition: node.h:16
Interface against a signal group.
Definition: signalgroup.h:15
Interface against a DBC signal configuration.
Definition: signal.h:68
Wrapper wrong environment variables.
DBC message configuration object.
Main namespace for the DBC library.
Definition: attribute.h:13
BusType
Type of bus.
Definition: network.h:31
@ CAN
CAN bus.
std::map< int64_t, std::string > EnumMap
Enumerate list.
Definition: network.h:20
std::map< uint64_t, Message > MessageList
Sorted message of messages. Message ID is the key.
Definition: network.h:36
ProtocolType
Type of CAN protocol.
Definition: network.h:23
@ StandardCAN
Standard CAN.
@ OBD2
OBD2 protocol.
@ NMEA2000
NMEA protocol.
@ J1939
J1939 protocol.
Interface against a DBC node (Device(ECU).
Interface against a DBC signal configuration.
Defines a signal group.