cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
Protocol.h
1//
2// Copyright 2014 - 2024 (C). Alex Robenko. All rights reserved.
3//
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19#pragma once
20
21#include <memory>
22#include <cstdint>
23#include <cstddef>
24#include <list>
25
26#include <QtCore/QMetaType>
27#include <QtCore/QString>
28
29#include "Api.h"
30#include "Message.h"
31#include "ErrorStatus.h"
32#include "DataInfo.h"
33
34namespace cc_tools_qt
35{
36
41
42class CC_API Protocol
43{
44public:
46 using MessagesList = std::list<MessagePtr>;
47
49 using DataInfosList = std::list<DataInfoPtr>;
50
53
55 enum class UpdateStatus
56 {
57 NoChange,
58 Changed
59 };
60
62 virtual ~Protocol() noexcept;
63
66 const QString& name() const;
67
73 MessagesList read(const DataInfo& dataInfo, bool final = false);
74
80 DataInfoPtr write(Message& msg);
81
84 MessagesList createAllMessages();
85
91 MessagePtr createMessage(const QString& idAsString, unsigned idx = 0);
92
96 UpdateStatus updateMessage(Message& msg);
97
101 MessagePtr cloneMessage(const Message& msg);
102
105 MessagePtr createInvalidMessage(const MsgDataSeq& data);
106
110 void socketConnectionReport(bool connected);
111
115 void messageReceivedReport(MessagePtr msg);
116
120 void messageSentReport(MessagePtr msg);
121
127 void applyInterPluginConfig(const QVariantMap& props);
128
131 void setDebugOutputLevel(unsigned level = 0U);
132
134 using ErrorReportCallback = std::function<void (const QString& msg)>;
135
138 template <typename TFunc>
139 void setErrorReportCallback(TFunc&& func)
140 {
141 m_errorReportCallback = std::forward<TFunc>(func);
142 }
143
145 using SendMessageRequestCallback = std::function<void (MessagePtr)>;
146
149 template <typename TFunc>
151 {
152 m_sendMessageRequestCallback = std::forward<TFunc>(func);
153 }
154
156 using InterPluginConfigReportCallback = std::function <void (const QVariantMap&)>;
157
160 template <typename TFunc>
162 {
163 m_interPluginConfigReportCallback = std::forward<TFunc>(func);
164 }
165
166protected:
169 virtual const QString& nameImpl() const = 0;
170
173 virtual MessagesList readImpl(const DataInfo& dataInfo, bool final) = 0;
174
177 virtual DataInfoPtr writeImpl(Message& msg) = 0;
178
182
185 virtual MessagePtr createMessageImpl(const QString& idAsString, unsigned idx) = 0;
186
190
193 virtual MessagePtr cloneMessageImpl(const Message& msg) = 0;
194
198
201
204
208 virtual void socketConnectionReportImpl(bool connected);
209
214
219
223 virtual void applyInterPluginConfigImpl(const QVariantMap& props);
224
228
234 void reportError(const QString& str);
235
242
249 void reportInterPluginConfig(const QVariantMap& props);
250
252 unsigned getDebugOutputLevel() const;
253
256 static void setTransportToMessageProperties(MessagePtr transportMsg, Message& msg);
257
260 static void setRawDataToMessageProperties(MessagePtr rawDataMsg, Message& msg);
261
265
269
271 static QVariantMap getExtraInfoFromMessageProperties(const Message& msg);
272
274 static void setExtraInfoToMessageProperties(const QVariantMap& extraInfo, Message& msg);
275
277 static void mergeExtraInfoToMessageProperties(const QVariantMap& extraInfo, Message& msg);
278
281
284
285private:
286 ErrorReportCallback m_errorReportCallback;
287 SendMessageRequestCallback m_sendMessageRequestCallback;
288 InterPluginConfigReportCallback m_interPluginConfigReportCallback;
289 unsigned m_debugLevel = 0U;
290};
291
293using ProtocolPtr = std::shared_ptr<Protocol>;
294
295} // namespace cc_tools_qt
296
297Q_DECLARE_METATYPE(cc_tools_qt::ProtocolPtr);
Main interface class used by CommsChampion Tools to display and manipulate messages.
Definition Message.h:41
std::vector< std::uint8_t > DataSeq
Type for sequence of raw bytes.
Definition Message.h:45
Main polymorphic interface class for protocols.
Definition Protocol.h:43
virtual UpdateStatus updateMessageImpl(Message &msg)=0
Polymorphic message update (refresh) functionality.
void reportError(const QString &str)
Report operation error.
virtual void messageReceivedReportImpl(MessagePtr msg)
Polymorphic processing of the message reception report.
UpdateStatus
Status of message "update" operation.
Definition Protocol.h:56
static void setForceExtraInfoExistenceToMessageProperties(Message &msg)
Helper function to force "extra info" existence.
void sendMessageRequest(MessagePtr msg)
Request a protocol message to be sent out.
void setNameToMessageProperties(Message &msg)
Helper function to assign protocol name to message properties.
virtual MessagePtr createRawDataMessageImpl()=0
Polymorphic creation of message object representing raw data.
virtual void socketConnectionReportImpl(bool connected)
Polymorphic processing of the socket connection report.
virtual MessagePtr cloneMessageImpl(const Message &msg)=0
Polymorphic message cloning functionality.
std::list< MessagePtr > MessagesList
List of messages.
Definition Protocol.h:46
virtual MessagePtr createMessageImpl(const QString &idAsString, unsigned idx)=0
Polymorphic message creation functionality.
virtual DataInfoPtr writeImpl(Message &msg)=0
Polymorphic write functionality.
static void setExtraInfoMsgToMessageProperties(MessagePtr extraInfoMsg, Message &msg)
Helper function to assign "extra info message" object as a property of application message object.
virtual MessagePtr createExtraInfoMessageImpl()=0
Polymorphic creation of message object representing extra info.
static void setTransportToMessageProperties(MessagePtr transportMsg, Message &msg)
Helper function to assign "tranport message" object as a property of application message object.
virtual ~Protocol() noexcept
Destructor.
std::function< void(MessagePtr)> SendMessageRequestCallback
Type of callback to request message being sent initiated by the plugin itself.
Definition Protocol.h:145
virtual MessagesList readImpl(const DataInfo &dataInfo, bool final)=0
Polymorphic read functionality.
static void setExtraInfoToMessageProperties(const QVariantMap &extraInfo, Message &msg)
Helper function to set "extra info" to message properties.
static MessagePtr getExtraInfoMsgToMessageProperties(const Message &msg)
Helper function to retrieve "extra info message" object from properties of the application message ob...
void setSendMessageRequestCallback(TFunc &&func)
Set the callback to allow request of extra messages to be sent out.
Definition Protocol.h:150
virtual MessagePtr createInvalidMessageImpl()=0
Polymorphic creation of invalid message representation.
static void setRawDataToMessageProperties(MessagePtr rawDataMsg, Message &msg)
Helper function to assign "raw data message" object as a property of application message object.
virtual MessagesList createAllMessagesImpl()=0
Polymorphic creation of all messages protocol supports.
std::function< void(const QString &msg)> ErrorReportCallback
Type of callback to report errors.
Definition Protocol.h:134
Message::DataSeq MsgDataSeq
Type used to contain raw bytes seqence.
Definition Protocol.h:52
void setInterPluginConfigReportCallback(TFunc &&func)
Set callback to report inter-plugin configuration.
Definition Protocol.h:161
static QVariantMap getExtraInfoFromMessageProperties(const Message &msg)
Helper function to retrieve "extra info" from message properties.
virtual void messageSentReportImpl(MessagePtr msg)
Make the protocol aware that the message has been sent out to the remote end.
static bool getForceExtraInfoExistenceFromMessageProperties(const Message &msg)
Helper function to check whether "extra info" existence is force.
std::list< DataInfoPtr > DataInfosList
List of raw data buffers.
Definition Protocol.h:49
std::function< void(const QVariantMap &)> InterPluginConfigReportCallback
Callback to report inter-plugin configuration updates.
Definition Protocol.h:156
unsigned getDebugOutputLevel() const
Get current debug output level.
static void mergeExtraInfoToMessageProperties(const QVariantMap &extraInfo, Message &msg)
Helper function to merge existing "extra info" and provided one.
void reportInterPluginConfig(const QVariantMap &props)
Report inter-plugin configuration.
virtual void applyInterPluginConfigImpl(const QVariantMap &props)
Polymorphic inter-plugin configuration application.
virtual const QString & nameImpl() const =0
Polymorphic protocol name retrieval.
Main namespace for all classes / functions of the shared library.
std::shared_ptr< Protocol > ProtocolPtr
Pointer to Protocol object.
Definition Protocol.h:293
std::shared_ptr< Message > MessagePtr
Smart pointer to Message.
Definition Message.h:157
std::shared_ptr< DataInfo > DataInfoPtr
Pointer to DataInfo.
Definition DataInfo.h:57
Information about incomming or outdoing data.
Definition DataInfo.h:37