libptpmgmt 2.0
libptpmgmt library that provides the functionality of linuxptp pmc
Loading...
Searching...
No Matches
msg.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-3.0-or-later
2 SPDX-FileCopyrightText: Copyright © 2021 Erez Geva <ErezGeva2@gmail.com> */
3
14#ifndef __PTPMGMT_MSG_H
15#define __PTPMGMT_MSG_H
16
17#ifdef __cplusplus
18#include <functional>
19#include "cfg.h"
20#include "buf.h"
21#include "mngTlvs.h"
22#include "sigTlvs.h"
23
24__PTPMGMT_NAMESPACE_BEGIN
25
26struct MsgProc;
27class Message;
28class MessageSigTlvs;
29struct HMAC_Key;
30
36{
37 public:
38 MessageSigTlvCallback() = default;
39 virtual ~MessageSigTlvCallback() = default;
48 virtual bool callback(const Message &msg, tlvType_e tlvType,
49 const BaseSigTlv *tlv) = 0;
50};
57{
58 private:
59 std::unique_ptr<BaseSigTlv> m_tlv;
60 tlvType_e m_tlvType;
61 protected:
63 friend class MessageSigTlvs;
66 public:
72 #ifndef SWIG
78 #endif /* SWIG */
88 const BaseSigTlv *tlv() const;
89};
94{
95 private:
96 /* Parent class */
97 Message &m_msg;
98 /* hold signalling TLVs */
99 std::vector<MessageSigTlv> m_tlvs;
100 protected:
102 friend class Message;
103 bool m_lastSig = false; /* indicate last parse was signaling */
104 MessageSigTlvs(Message &m) : m_msg(m) {};
105 void clearToUse() { m_tlvs.clear(); m_lastSig = true; }
106 const MANAGEMENT_t *getMng(size_t position) const;
107 void push(tlvType_e tlvType, BaseSigTlv *tlv);
108
109 public:
110 #ifndef SWIG
111 class iterator
112 {
113 private:
114 std::vector<MessageSigTlv>::const_iterator it;
115 protected:
116 iterator(const std::vector<MessageSigTlv>::const_iterator &it);
117 friend class MessageSigTlvs;
118 public:
119 /* iterator need to support these following operators */
120 iterator &operator++(); /* prefix increment */
121 iterator &operator++(int); /* postfix increment */
122 const MessageSigTlv &operator*();
123 bool operator!=(iterator &o);
124 };
125 #endif /* SWIG */
138 bool traverse(const std::function<bool (const Message &msg,
139 tlvType_e tlvType, const BaseSigTlv *tlv)> &callback) const;
147 bool traverse(MessageSigTlvCallback &callback) const;
152 size_t size() const;
158 const BaseSigTlv *get(size_t position) const;
164 tlvType_e getType(size_t position) const;
172 mng_vals_e getTlvId(size_t position) const;
180 const BaseMngTlv *getMngTlv(size_t position) const;
186 const MessageSigTlv &getTlv(size_t position) const;
187 #ifndef SWIG
198 iterator begin() const;
204 iterator end() const;
205 #endif /* SWIG */
206};
207
215{
216 private:
217
218 /* build parameters */
219 actionField_e m_sendAction = GET;
220 size_t m_msgLen = 0;
221 const BaseMngTlv *m_dataSend = nullptr;
222 mng_vals_e m_tlv_id = NULL_PTP_MANAGEMENT; /* managementId */
223
224 /* parsing parameters */
225 uint16_t m_sequence = 0;
226 bool m_isUnicast = true;
227 uint8_t m_PTPProfileSpecific = 0;
228 actionField_e m_replyAction = RESPONSE;
229 mng_vals_e m_replayTlv_id = NULL_PTP_MANAGEMENT; /* managementId */
230 uint32_t m_sdoId = 0; /* parsed message sdoId (transportSpecific) */
231 msgType_e m_type = Management; /* parsed message type */
232 tlvType_e m_mngType = MANAGEMENT; /* parsed management message type */
233 uint8_t m_domainNumber = 0; /* parsed message domainNumber*/
234 uint8_t m_versionPTP = 2; /* parsed message ptp version */
235 uint8_t m_minorVersionPTP = 1; /* parsed message ptp version */
236 /* hold signalling TLVs */
237 MessageSigTlvs m_sigTlvs;
238 /* parsed management TLV */
239 std::unique_ptr<BaseMngTlv> m_dataGet;
240
241 /* Generic */
242 MsgParams m_prms;
243
244 /* Authentication TLV */
245 uint32_t m_keyID = 0;
246 uint8_t m_sppID = -1;
247 SaFile m_sa;
248 bool m_haveAuth = false;
249 std::unique_ptr<HMAC_Key> m_hmac;
251 /* parsing parameters */
252 PortIdentity_t m_peer; /* parsed message peer port id */
253 PortIdentity_t m_target; /* parsed message target port id */
254
255 /* For error messages */
257 PTPText_t m_errorDisplay;
258
259 bool allowedAction(mng_vals_e id, actionField_e action);
260 /* val in network order */
261 static bool findTlvId(uint16_t val, mng_vals_e &rid, implementSpecific_e spec);
262 bool checkReplyAction(uint8_t actionField);
263 /* parse signalling message */
264 MNG_PARSE_ERROR_e parseSig(const void *buf, MsgProc *);
265 /* parse authentication message */
266 MNG_PARSE_ERROR_e parseAuth(const void *buf, const void *auth, ssize_t left,
267 bool check = false);
268 /*
269 * dataFieldSize() for sending SET/COMMAND
270 * Get dataField of current build management ID
271 * For id with non fixed size
272 * @param[in] tlv to size
273 * The size is determined by the m_dataSend content
274 */
275 ssize_t dataFieldSize(const BaseMngTlv *tlv) const;
283 static bool verifyTlv(mng_vals_e tlv_id, const BaseMngTlv *tlv);
284
285 public:
286 Message();
287 ~Message();
293 Message(const MsgParams &prms);
298 const MsgParams &getParams() const;
304 bool updateParams(const MsgParams &prms);
312 bool useAuth(const ConfigFile &cfg, const std::string &section = "");
320 bool useAuth(const SaFile &sa, uint8_t spp, uint32_t key);
327 bool changeAuth(uint8_t spp, uint32_t key);
333 bool changeAuth(uint32_t key);
343 int usedAuthSppID() const;
348 uint32_t usedAuthKeyID() const;
353 const SaFile &getSa() const;
358 bool haveAuth() const;
377 bool isAllClocks() const;
385 bool useConfig(const ConfigFile &cfg, const std::string &section = "");
391 static const char *err2str_c(MNG_PARSE_ERROR_e err);
392
398 static const char *type2str_c(msgType_e type);
404 static const char *tlv2str_c(tlvType_e type);
410 static const char *act2str_c(actionField_e action);
416 static const char *mng2str_c(mng_vals_e id);
424 static const bool findMngID(const std::string &str, mng_vals_e &id,
425 bool exact = true);
431 static const char *errId2str_c(managementErrorId_e err);
437 static const char *clkType2str_c(clockType_e type);
443 static const char *netProt2str_c(networkProtocol_e protocol);
449 static const char *clockAcc2str_c(clockAccuracy_e value);
455 static const char *faultRec2str_c(faultRecord_e code);
461 static const char *timeSrc2str_c(timeSource_e type);
469 static const bool findTimeSrc(const std::string &str, timeSource_e &type,
470 bool exact = true);
476 static const char *portState2str_c(portState_e state);
484 static const bool findPortState(const std::string &str, portState_e &state,
485 bool caseSens = true);
491 static const char *delayMech2str_c(delayMechanism_e type);
499 static const bool findDelayMech(const std::string &str, delayMechanism_e &type,
500 bool exact = true);
512 static const char *ts2str_c(linuxptpTimeStamp_e type);
524 static const char *us2str_c(linuxptpUnicastState_e state);
530 static bool is_LI_61(uint8_t flags);
536 static bool is_LI_59(uint8_t flags);
542 static bool is_UTCV(uint8_t flags);
548 static bool is_PTP(uint8_t flags);
554 static bool is_TTRA(uint8_t flags);
560 static bool is_FTRA(uint8_t flags);
566 static bool isEmpty(mng_vals_e id);
573 bool isValidId(mng_vals_e id) const;
586 bool setAction(actionField_e actionField, mng_vals_e tlv_id,
587 const BaseMngTlv *dataSend = nullptr);
591 void clearData();
605 MNG_PARSE_ERROR_e build(void *buf, size_t bufSize, uint16_t sequence);
618 MNG_PARSE_ERROR_e build(Buf &buf, uint16_t sequence);
628 size_t getMsgLen() const;
637 ssize_t getMsgPlanedLen() const;
638 /* Parsed message functions */
645 MNG_PARSE_ERROR_e parse(const void *buf, ssize_t msgSize);
652 MNG_PARSE_ERROR_e parse(const Buf &buf, ssize_t msgSize);
663 bool isUnicast() const;
669 uint8_t getPTPProfileSpecific() const;
674 uint16_t getSequence() const;
679 const PortIdentity_t &getPeer() const;
684 const PortIdentity_t &getTarget() const;
690 uint32_t getSdoId() const;
695 uint8_t getDomainNumber() const;
700 uint8_t getVersionPTP() const;
705 uint8_t getMinorVersionPTP() const;
713 const BaseMngTlv *getData() const;
722 const BaseMngTlv *getSendData() const;
734 const std::string &getErrDisplay() const;
740 const char *getErrDisplay_c() const;
745 bool isLastMsgSig() const;
750 bool isLastMsgSMPTE() const;
768 bool traversSigTlvs(const std::function<bool (const Message &msg,
769 tlvType_e tlvType, const BaseSigTlv *tlv)> callback) const;
782 size_t getSigTlvsCount() const;
788 const BaseSigTlv *getSigTlv(size_t position) const;
794 tlvType_e getSigTlvType(size_t position) const;
802 mng_vals_e getSigMngTlvType(size_t position) const;
810 const BaseMngTlv *getSigMngTlv(size_t position) const;
816};
817
818__PTPMGMT_NAMESPACE_END
819#else /* __cplusplus */
820#include "c/msg.h"
821#endif /* __cplusplus */
822
823#endif /* __PTPMGMT_MSG_H */
Buffer for send, receive, build, and parse.
Create and parse PTP management messages.
Read ptp4l Configuration file.
Definition buf.h:26
Hold configuration parameters.
Definition cfg.h:34
Definition msg.h:36
virtual bool callback(const Message &msg, tlvType_e tlvType, const BaseSigTlv *tlv)=0
Definition msg.h:57
const BaseSigTlv * tlv() const
MessageSigTlv(MessageSigTlv &&other)
tlvType_e tlvType() const
MessageSigTlv(const MessageSigTlv &other)
Definition msg.h:94
tlvType_e getType(size_t position) const
MessageSigTlvs(const MessageSigTlvs &other)
size_t size() const
const BaseSigTlv * get(size_t position) const
bool traverse(const std::function< bool(const Message &msg, tlvType_e tlvType, const BaseSigTlv *tlv)> &callback) const
bool traverse(MessageSigTlvCallback &callback) const
const MessageSigTlv & getTlv(size_t position) const
MessageSigTlvs(MessageSigTlvs &&other)
iterator end() const
const BaseMngTlv * getMngTlv(size_t position) const
iterator begin() const
mng_vals_e getTlvId(size_t position) const
Handle PTP management message.
Definition msg.h:215
const PortIdentity_t & getTarget() const
ssize_t getMsgPlanedLen() const
bool isLastMsgSMPTE() const
static const char * timeSrc2str_c(timeSource_e type)
const SaFile & getSa() const
bool useAuth(const SaFile &sa, uint8_t spp, uint32_t key)
static const char * type2str_c(msgType_e type)
bool isValidId(mng_vals_e id) const
actionField_e getReplyAction() const
static bool is_FTRA(uint8_t flags)
static const char * smpteLck2str_c(SMPTEmasterLockingStatus_e state)
static bool isEmpty(mng_vals_e id)
static const bool findPortState(const std::string &str, portState_e &state, bool caseSens=true)
uint8_t getPTPProfileSpecific() const
static bool is_TTRA(uint8_t flags)
MNG_PARSE_ERROR_e build(Buf &buf, uint16_t sequence)
const BaseSigTlv * getSigTlv(size_t position) const
bool useAuth(const ConfigFile &cfg, const std::string &section="")
void setAllClocks()
const std::string & getErrDisplay() const
tlvType_e getMngType() const
static const char * ts2str_c(linuxptpTimeStamp_e type)
bool traversSigTlvs(const std::function< bool(const Message &msg, tlvType_e tlvType, const BaseSigTlv *tlv)> callback) const
mng_vals_e getTlvId() const
size_t getSigTlvsCount() const
MNG_PARSE_ERROR_e parse(const void *buf, ssize_t msgSize)
static const char * err2str_c(MNG_PARSE_ERROR_e err)
uint8_t getDomainNumber() const
actionField_e getSendAction() const
MNG_PARSE_ERROR_e parse(const Buf &buf, ssize_t msgSize)
bool updateParams(const MsgParams &prms)
static const char * act2str_c(actionField_e action)
static bool is_LI_59(uint8_t flags)
uint8_t getMinorVersionPTP() const
static const bool findTimeSrc(const std::string &str, timeSource_e &type, bool exact=true)
uint8_t getVersionPTP() const
tlvType_e getSigTlvType(size_t position) const
static const char * clockAcc2str_c(clockAccuracy_e value)
static const char * pwr2str_c(linuxptpPowerProfileVersion_e ver)
bool changeAuth(uint8_t spp, uint32_t key)
static const char * tlv2str_c(tlvType_e type)
uint32_t usedAuthKeyID() const
static const char * delayMech2str_c(delayMechanism_e type)
const BaseMngTlv * getSigMngTlv(size_t position) const
static const char * netProt2str_c(networkProtocol_e protocol)
managementErrorId_e getErrId() const
mng_vals_e getBuildTlvId() const
MNG_PARSE_ERROR_e build(void *buf, size_t bufSize, uint16_t sequence)
bool isLastMsgSig() const
const MessageSigTlvs & getSigTlvs() const
bool traversSigTlvsCl(MessageSigTlvCallback &callback) const
bool useConfig(const ConfigFile &cfg, const std::string &section="")
const BaseMngTlv * getSendData() const
mng_vals_e getSigMngTlvType(size_t position) const
static bool is_UTCV(uint8_t flags)
static const bool findDelayMech(const std::string &str, delayMechanism_e &type, bool exact=true)
static const char * portState2str_c(portState_e state)
const char * getErrDisplay_c() const
Message(const MsgParams &prms)
static const char * errId2str_c(managementErrorId_e err)
static bool is_PTP(uint8_t flags)
static const char * mng2str_c(mng_vals_e id)
static const char * clkType2str_c(clockType_e type)
bool isAllClocks() const
bool haveAuth() const
static bool is_LI_61(uint8_t flags)
size_t getMsgLen() const
const PortIdentity_t & getPeer() const
bool changeAuth(uint32_t key)
const MsgParams & getParams() const
static const char * faultRec2str_c(faultRecord_e code)
static const char * us2str_c(linuxptpUnicastState_e state)
bool isUnicast() const
uint16_t getSequence() const
int usedAuthSppID() const
msgType_e getType() const
bool setAction(actionField_e actionField, mng_vals_e tlv_id, const BaseMngTlv *dataSend=nullptr)
void clearData()
const BaseMngTlv * getData() const
static const bool findMngID(const std::string &str, mng_vals_e &id, bool exact=true)
uint32_t getSdoId() const
bool disableAuth()
authentication security association file
Definition cfg.h:308
mng_vals_e
Management TLVs ID.
Definition mngIds.h:29
PTP management TLV structures.
PTP signalling TLV structures.
Definition types.h:915
Definition types.h:919
Definition sigTlvs.h:27
Definition types.h:861
Definition types.h:801
Definition types.h:701
clockAccuracy_e
Definition types.h:229
managementErrorId_e
Definition types.h:199
linuxptpTimeStamp_e
Definition types.h:347
msgType_e
Definition types.h:114
@ Management
Definition types.h:136
implementSpecific_e
Definition types.h:322
delayMechanism_e
Definition types.h:299
tlvType_e
Definition types.h:142
@ MANAGEMENT
Definition types.h:144
portState_e
Definition types.h:284
SMPTEmasterLockingStatus_e
Definition types.h:327
linuxptpUnicastState_e
Definition types.h:372
actionField_e
Definition types.h:191
@ GET
Definition types.h:192
@ RESPONSE
Definition types.h:194
linuxptpPowerProfileVersion_e
Definition types.h:363
networkProtocol_e
Definition types.h:220
MNG_PARSE_ERROR_e
Definition types.h:71
faultRecord_e
Definition types.h:260
clockType_e
Definition types.h:212
timeSource_e
Definition types.h:271