libptpmgmt  1.4
libptpmgmt library that provides the functionality of linuxptp pmc
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 "proc.h"
22 #include "sig.h"
23 
24 __PTPMGMT_NAMESPACE_BEGIN
25 
26 struct MsgProc;
27 class Message;
28 struct HMAC_Key;
29 
35 {
36  public:
37  MessageSigTlvCallback() = default;
38  virtual ~MessageSigTlvCallback() = default;
47  virtual bool callback(const Message &msg, tlvType_e tlvType,
48  const BaseSigTlv *tlv) = 0;
49 };
50 
57 class Message
58 {
63  private:
64 
67  /* build parameters */
68  actionField_e m_sendAction = GET;
69  size_t m_msgLen = 0;
70  const BaseMngTlv *m_dataSend = nullptr;
71  mng_vals_e m_tlv_id = NULL_PTP_MANAGEMENT; /* managementId */
72 
73  /* parsing parameters */
74  uint16_t m_sequence = 0;
75  bool m_isUnicast = true;
76  uint8_t m_PTPProfileSpecific = 0;
77  actionField_e m_replyAction = RESPONSE;
78  mng_vals_e m_replayTlv_id = NULL_PTP_MANAGEMENT; /* managementId */
79  uint32_t m_sdoId = 0; /* parsed message sdoId (transportSpecific) */
80  msgType_e m_type = Management; /* parsed message type */
81  tlvType_e m_mngType = MANAGEMENT; /* parsed management message type */
82  uint8_t m_domainNumber = 0; /* parsed message domainNumber*/
83  uint8_t m_versionPTP = 2; /* parsed message ptp version */
84  uint8_t m_minorVersionPTP = 1; /* parsed message ptp version */
85  /* hold signalling TLVs */
86  std::vector<std::unique_ptr<BaseSigTlv>> m_sigTlvs;
87  /* hold signalling TLVs type */
88  std::vector<tlvType_e> m_sigTlvsType;
89  /* parsed management TLV */
90  std::unique_ptr<BaseMngTlv> m_dataGet;
91 
92  /* Generic */
93  MsgParams m_prms;
94 
95  /* Authentication TLV */
96  uint32_t m_keyID = 0;
97  uint8_t m_sppID = -1;
98  SaFile m_sa;
99  bool m_haveAuth = false;
100  HMAC_Key *m_hmac = nullptr;
102  /* parsing parameters */
103  PortIdentity_t m_peer; /* parsed message peer port id */
104  PortIdentity_t m_target; /* parsed message target port id */
105 
106  /* For error messages */
108  PTPText_t m_errorDisplay;
109 
110  /* Map to all management IDs */
111  static const ManagementId_t mng_all_vals[];
112 
113  bool allowedAction(mng_vals_e id, actionField_e action);
114  /* val in network order */
115  static bool findTlvId(uint16_t val, mng_vals_e &rid, implementSpecific_e spec);
116  bool checkReplyAction(uint8_t actionField);
117  /* parse signalling message */
118  MNG_PARSE_ERROR_e parseSig(const void *buf, MsgProc *);
119  /* parse authentication message */
120  MNG_PARSE_ERROR_e parseAuth(const void *buf, const void *auth, ssize_t left,
121  bool check = false);
122  /*
123  * dataFieldSize() for sending SET/COMMAND
124  * Get dataField of current build management ID
125  * For id with non fixed size
126  * The size is determined by the m_dataSend content
127  */
128  ssize_t dataFieldSize(const BaseMngTlv *data) const;
136  static bool verifyTlv(mng_vals_e tlv_id, const BaseMngTlv *tlv);
137 
138  public:
139  Message();
140  ~Message();
146  Message(const MsgParams &prms);
151  const MsgParams &getParams() const { return m_prms; }
157  bool updateParams(const MsgParams &prms);
165  bool useAuth(const ConfigFile &cfg, const std::string &section = "");
173  bool useAuth(const SaFile &sa, uint8_t spp, uint32_t key);
180  bool changeAuth(uint8_t spp, uint32_t key);
186  bool changeAuth(uint32_t key);
191  bool disableAuth() { m_haveAuth = false; return true; }
196  int usedAuthSppID() const { return m_haveAuth ? m_sppID : -1; }
201  uint32_t usedAuthKeyID() const { return m_haveAuth ? m_keyID : 0; }
206  const SaFile &getSa() const { return m_sa; }
211  bool haveAuth() const { return m_haveAuth; }
216  mng_vals_e getTlvId() const { return m_replayTlv_id; }
221  mng_vals_e getBuildTlvId() const { return m_tlv_id; }
225  void setAllClocks();
230  bool isAllClocks() const;
238  bool useConfig(const ConfigFile &cfg, const std::string &section = "");
244  static const char *err2str_c(MNG_PARSE_ERROR_e err);
245 
251  static const char *type2str_c(msgType_e type);
257  static const char *tlv2str_c(tlvType_e type);
263  static const char *act2str_c(actionField_e action);
269  static const char *mng2str_c(mng_vals_e id);
277  static const bool findMngID(const std::string &str, mng_vals_e &id,
278  bool exact = true);
284  static const char *errId2str_c(managementErrorId_e err);
290  static const char *clkType2str_c(clockType_e type);
296  static const char *netProt2str_c(networkProtocol_e protocol);
302  static const char *clockAcc2str_c(clockAccuracy_e value);
308  static const char *faultRec2str_c(faultRecord_e code);
314  static const char *timeSrc2str_c(timeSource_e type);
322  static const bool findTimeSrc(const std::string &str, timeSource_e &type,
323  bool exact = true);
329  static const char *portState2str_c(portState_e state);
337  static const bool findPortState(const std::string &str, portState_e &state,
338  bool caseSens = true);
344  static const char *delayMech2str_c(delayMechanism_e type);
352  static const bool findDelayMech(const std::string &str, delayMechanism_e &type,
353  bool exact = true);
359  static const char *smpteLck2str_c(SMPTEmasterLockingStatus_e state);
365  static const char *ts2str_c(linuxptpTimeStamp_e type);
371  static const char *pwr2str_c(linuxptpPowerProfileVersion_e ver);
377  static const char *us2str_c(linuxptpUnicastState_e state);
383  static bool is_LI_61(uint8_t flags) { return (flags & F_LI_61) != 0; }
389  static bool is_LI_59(uint8_t flags) { return (flags & F_LI_59) != 0; }
395  static bool is_UTCV(uint8_t flags) { return (flags & F_UTCV) != 0; }
401  static bool is_PTP(uint8_t flags) { return (flags & F_PTP) != 0; }
407  static bool is_TTRA(uint8_t flags) { return (flags & F_TTRA) != 0; }
413  static bool is_FTRA(uint8_t flags) { return (flags & F_FTRA) != 0; }
419  static bool isEmpty(mng_vals_e id);
439  bool setAction(actionField_e actionField, mng_vals_e tlv_id,
440  const BaseMngTlv *dataSend = nullptr);
444  void clearData();
458  MNG_PARSE_ERROR_e build(void *buf, size_t bufSize, uint16_t sequence);
471  MNG_PARSE_ERROR_e build(Buf &buf, uint16_t sequence)
472  { return build(buf.get(), buf.size(), sequence); }
477  actionField_e getSendAction() const { return m_sendAction; }
482  size_t getMsgLen() const { return m_msgLen; }
491  ssize_t getMsgPlanedLen() const;
492  /* Parsed message functions */
499  MNG_PARSE_ERROR_e parse(const void *buf, ssize_t msgSize);
506  MNG_PARSE_ERROR_e parse(const Buf &buf, ssize_t msgSize);
512  actionField_e getReplyAction() const { return m_replyAction; }
517  bool isUnicast() const { return m_isUnicast; }
523  uint8_t getPTPProfileSpecific() const { return m_PTPProfileSpecific; }
528  uint16_t getSequence() const { return m_sequence; }
533  const PortIdentity_t &getPeer() const { return m_peer; }
538  const PortIdentity_t &getTarget() const { return m_target; }
544  uint32_t getSdoId() const { return m_sdoId; }
549  uint8_t getDomainNumber() const { return m_domainNumber; }
554  uint8_t getVersionPTP() const { return m_versionPTP; }
559  uint8_t getMinorVersionPTP() const { return m_minorVersionPTP; }
567  const BaseMngTlv *getData() const { return m_dataGet.get(); }
576  const BaseMngTlv *getSendData() const { return m_dataSend; }
582  managementErrorId_e getErrId() const { return m_errorId; }
588  const std::string &getErrDisplay() const { return m_errorDisplay.textField; }
594  const char *getErrDisplay_c() const { return m_errorDisplay.string(); }
599  bool isLastMsgSig() const { return m_type == Signaling; }
604  bool isLastMsgSMPTE() const;
609  msgType_e getType() const { return m_type; }
615  tlvType_e getMngType() const { return m_mngType; }
622  bool traversSigTlvs(const std::function<bool (const Message &msg,
623  tlvType_e tlvType, const BaseSigTlv *tlv)> callback) const;
636  size_t getSigTlvsCount() const;
642  const BaseSigTlv *getSigTlv(size_t position) const;
648  tlvType_e getSigTlvType(size_t position) const;
656  mng_vals_e getSigMngTlvType(size_t position) const;
664  const BaseMngTlv *getSigMngTlv(size_t position) const;
665 };
666 
667 __PTPMGMT_NAMESPACE_END
668 #else /* __cplusplus */
669 #include "c/msg.h"
670 #endif /* __cplusplus */
671 
672 #endif /* __PTPMGMT_MSG_H */
Buffer for send, receive, build, and parse.
C interface to configuration class.
Create and parse PTP management messages.
PTP management TLV structures.
PTP signalling TLV structures.
Definition: buf.h:26
size_t size() const
Definition: buf.h:58
void * get() const
Definition: buf.h:48
Hold configuration parameters.
Definition: cfg.h:89
Abstract class used for callback for Signalling TLVs traverse.
Definition: msg.h:35
virtual bool callback(const Message &msg, tlvType_e tlvType, const BaseSigTlv *tlv)=0
Handle PTP management message.
Definition: msg.h:58
const PortIdentity_t & getTarget() const
Definition: msg.h:538
ssize_t getMsgPlanedLen() const
bool isValidId(mng_vals_e id)
bool isLastMsgSMPTE() const
const PortIdentity_t & getPeer() const
Definition: msg.h:533
bool useAuth(const SaFile &sa, uint8_t spp, uint32_t key)
static const char * portState2str_c(portState_e state)
actionField_e getReplyAction() const
Definition: msg.h:512
static bool is_FTRA(uint8_t flags)
Definition: msg.h:413
static const char * err2str_c(MNG_PARSE_ERROR_e err)
static bool isEmpty(mng_vals_e id)
static const char * ts2str_c(linuxptpTimeStamp_e type)
static const bool findPortState(const std::string &str, portState_e &state, bool caseSens=true)
uint8_t getPTPProfileSpecific() const
Definition: msg.h:523
static bool is_TTRA(uint8_t flags)
Definition: msg.h:407
MNG_PARSE_ERROR_e build(Buf &buf, uint16_t sequence)
Definition: msg.h:471
bool useAuth(const ConfigFile &cfg, const std::string &section="")
void setAllClocks()
tlvType_e getMngType() const
Definition: msg.h:615
const std::string & getErrDisplay() const
Definition: msg.h:588
bool traversSigTlvs(const std::function< bool(const Message &msg, tlvType_e tlvType, const BaseSigTlv *tlv)> callback) const
mng_vals_e getTlvId() const
Definition: msg.h:216
size_t getSigTlvsCount() const
MNG_PARSE_ERROR_e parse(const void *buf, ssize_t msgSize)
uint8_t getDomainNumber() const
Definition: msg.h:549
actionField_e getSendAction() const
Definition: msg.h:477
MNG_PARSE_ERROR_e parse(const Buf &buf, ssize_t msgSize)
static const char * us2str_c(linuxptpUnicastState_e state)
bool updateParams(const MsgParams &prms)
static const char * timeSrc2str_c(timeSource_e type)
static bool is_LI_59(uint8_t flags)
Definition: msg.h:389
uint8_t getMinorVersionPTP() const
Definition: msg.h:559
static const bool findTimeSrc(const std::string &str, timeSource_e &type, bool exact=true)
uint8_t getVersionPTP() const
Definition: msg.h:554
static const char * act2str_c(actionField_e action)
static const char * pwr2str_c(linuxptpPowerProfileVersion_e ver)
static const char * faultRec2str_c(faultRecord_e code)
tlvType_e getSigTlvType(size_t position) const
bool changeAuth(uint8_t spp, uint32_t key)
uint32_t usedAuthKeyID() const
Definition: msg.h:201
static const char * mng2str_c(mng_vals_e id)
managementErrorId_e getErrId() const
Definition: msg.h:582
mng_vals_e getBuildTlvId() const
Definition: msg.h:221
static const char * netProt2str_c(networkProtocol_e protocol)
const BaseMngTlv * getSigMngTlv(size_t position) const
static const char * errId2str_c(managementErrorId_e err)
MNG_PARSE_ERROR_e build(void *buf, size_t bufSize, uint16_t sequence)
const BaseSigTlv * getSigTlv(size_t position) const
bool isLastMsgSig() const
Definition: msg.h:599
static const char * smpteLck2str_c(SMPTEmasterLockingStatus_e state)
bool useConfig(const ConfigFile &cfg, const std::string &section="")
mng_vals_e getSigMngTlvType(size_t position) const
static bool is_UTCV(uint8_t flags)
Definition: msg.h:395
static const bool findDelayMech(const std::string &str, delayMechanism_e &type, bool exact=true)
const char * getErrDisplay_c() const
Definition: msg.h:594
const BaseMngTlv * getSendData() const
Definition: msg.h:576
Message(const MsgParams &prms)
static const char * clkType2str_c(clockType_e type)
static bool is_PTP(uint8_t flags)
Definition: msg.h:401
static const char * type2str_c(msgType_e type)
bool isAllClocks() const
bool haveAuth() const
Definition: msg.h:211
static bool is_LI_61(uint8_t flags)
Definition: msg.h:383
size_t getMsgLen() const
Definition: msg.h:482
bool changeAuth(uint32_t key)
const MsgParams & getParams() const
Definition: msg.h:151
static const char * clockAcc2str_c(clockAccuracy_e value)
bool isUnicast() const
Definition: msg.h:517
uint16_t getSequence() const
Definition: msg.h:528
int usedAuthSppID() const
Definition: msg.h:196
msgType_e getType() const
Definition: msg.h:609
bool traversSigTlvsCl(MessageSigTlvCallback &callback)
static const char * delayMech2str_c(delayMechanism_e type)
bool setAction(actionField_e actionField, mng_vals_e tlv_id, const BaseMngTlv *dataSend=nullptr)
void clearData()
static const bool findMngID(const std::string &str, mng_vals_e &id, bool exact=true)
const BaseMngTlv * getData() const
Definition: msg.h:567
uint32_t getSdoId() const
Definition: msg.h:544
const SaFile & getSa() const
Definition: msg.h:206
static const char * tlv2str_c(tlvType_e type)
bool disableAuth()
Definition: msg.h:191
authentication security association file
Definition: cfg.h:315
mng_vals_e
Management TLVs ID.
Definition: mngIds.h:29
Definition: types.h:962
Definition: types.h:966
Definition: types.h:878
Definition: types.h:908
Definition: types.h:828
const char * string() const
Definition: types.h:846
std::string textField
Definition: types.h:836
Definition: types.h:717
clockAccuracy_e
Definition: types.h:229
managementErrorId_e
Definition: types.h:199
@ F_TTRA
Definition: types.h:389
@ F_FTRA
Definition: types.h:392
@ F_UTCV
Definition: types.h:385
@ F_LI_61
Definition: types.h:381
@ F_PTP
Definition: types.h:387
@ F_LI_59
Definition: types.h:383
linuxptpTimeStamp_e
Definition: types.h:347
msgType_e
Definition: types.h:114
@ Signaling
Definition: types.h:134
@ 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