// mqttcfg.h : // #if !defined(AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_) #define AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_ #include #include #include #include #include #include #include #ifndef _LIBBUILD #include #include #else // _LIBBUILD #include "common/logfile.h" #include "mqttjson.h" #endif // _LIBBUILD #ifdef __cplusplus ///////////////////////////////////////////////////////////////////////////// // mqttcfg.h - Declarations: #define MQTTCL_TLS_MODE_OFF 0 // no TLS #define MQTTCL_TLS_MODE_CRT 1 // TLS using certificates #define MQTTCL_TLS_MODE_PSK 2 // TLS using preshared key #define MQTTCL_MIN_QOS 0 #define MQTTCL_MAX_QOS 2 #define MQTTCL_DEFAULT_QOS MQTTCL_MAX_QOS #define MQTTCL_DEFAULT_RETAIN false #define MQTTCL_DEFAULT_LAST_WILL_CONNECT_TOPIC "LWC" #define MQTTCL_MIN_MAX_KEEP_ALIVE_TIME 10 #define MQTTCL_DEFAULT_MAX_KEEP_ALIVE_TIME 60 ///////////////////////////////////////////////////////////////////////////// class CMqttClConfig { public: CMqttClConfig(const char *pszShmUuid); virtual ~CMqttClConfig(void); bool LoadCfg(const char *pszCfgFilePath, CLogfile &rlf); inline const char* GetBrokerAddr(void) const { return m_strBrokerAddr.empty() ? NULL : m_strBrokerAddr.c_str(); } inline unsigned short GetBrokerPort(void) const { return (unsigned short)m_nBrokerPort; } inline int GetDefaultQOS(void) const { return m_nDefaultQOS; } inline int GetTLSMode(void) const { return m_nTlsMode; } inline bool GetDefaultRetain(void) const { return m_bDefaultRetain; } inline const char* GetDevicePrefix(void) const { return m_strDevicePrefix.empty() ? NULL : m_strDevicePrefix.c_str(); } inline const char* GetTlsCaCrtFile(void) const { return m_strTlsCaCrtFile.empty() ? NULL : m_strTlsCaCrtFile.c_str(); } inline const char* GetTlsClCrtFile(void) const { return m_strTlsClCrtFile.empty() ? NULL : m_strTlsClCrtFile.c_str(); } inline const char* GetTlsClKeyFile(void) const { return m_strTlsClKeyFile.empty() ? NULL : m_strTlsClKeyFile.c_str(); } inline const char* GetTlsPSK(void) const { return m_strTlsPSK.empty() ? NULL : m_strTlsPSK.c_str(); } inline const char* GetDeviceID(void) const { return m_strDeviceID.empty() ? NULL : m_strDeviceID.c_str(); } inline const char* GetShmID(void) const { return m_strShmID.empty() ? NULL : m_strShmID.c_str(); } inline const char* GetTopicPrefix(void) const { return m_strTopicPrefix.empty() ? NULL : m_strTopicPrefix.c_str(); } inline const char* GetLastWillTopic(void) const { return m_strLastWillTopic.empty() ? NULL : m_strLastWillTopic.c_str(); } inline const void* GetLastWillMessage(void) const { if(!m_bHasLastWill) return nullptr; return m_strLastWillMessage.empty() ? (const void*)&m_nLastWillMessage : (const void*)m_strLastWillMessage.c_str(); } inline int GetLastWillMessageLength(void) const { if(!m_bHasLastWill) return 0; return (int)(m_strLastWillMessage.empty() ? sizeof(m_nLastWillMessage) : m_strLastWillMessage.length()); } inline int GetLastWillQOS(void) const { return m_nLastWillQos; } inline bool GetLastWillRetain(void) const { return m_bLastWillRetain; } inline bool HasLastWill(void) const { return m_bHasLastWill; } inline bool HasLastWillOnExit(void) const { return m_bLastWillOnExit; } inline const void* GetLastWillOnExitMessage(void) const { return m_strLastWillOnExitMsg.empty() ? (const void*)&m_nLastWillOnExitMsg : (const void*)m_strLastWillOnExitMsg.c_str(); } inline int GetLastWillOnExitMessageLength(void) const { return (int)(m_strLastWillOnExitMsg.empty() ? sizeof(m_nLastWillOnExitMsg) : m_strLastWillOnExitMsg.length()); } inline const char* GetConnectTopic(void) const { return m_strConnectTopic.empty() ? NULL : m_strConnectTopic.c_str(); } inline const void* GetConnectMessage(void) const { if(!m_bHasConnect) return nullptr; return m_strConnectMessage.empty() ? (const void*)&m_nConnectMessage : (const void*)m_strConnectMessage.c_str(); } inline int GetConnectMessageLength(void) const { if(!m_bHasConnect) return 0; return (int)(m_strConnectMessage.empty() ? sizeof(m_nConnectMessage) : m_strConnectMessage.length()); } inline int GetConnectQOS(void) const { return m_nConnectQos; } inline bool GetConnectRetain(void) const { return m_bConnectRetain; } inline bool HasConnectMsg(void) const { return m_bHasConnect; } inline bool TopicPrefixDisabled(void) const { return m_bDisableTopicPrefix; } inline bool HasPrefix(void) const { return m_bHasPrefix; } inline int GetKeepAliveTime(void) const { return m_nMaxKeepAlive; } static sa_family_t GetDevIdInterfaceName(char *pszItfName, size_t nCChItfName, const char *pszRequested); static const char* GetMacAddress(std::string &s); static bool GetPreferredMacAddress(const char *pszRequested, std::string &s); static std::string CreateDeviceID(const char *pszDevicePrefix); private: bool GetValue(CJson_t &rjtParent, const char *pszKey, CJson_t &rjtVal, std::string &strErr); bool GetBoolValue(CJson_t &rjtParent, const char *pszKey, bool &rbVal, std::string &strErr); bool GetIntValue(CJson_t &rjtParent, const char *pszKey, int &rnVal, std::string &strErr); bool GetInt64Value(CJson_t &rjtParent, const char *pszKey, long long &rnVal, std::string &strErr); bool GetStringValue(CJson_t &rjtParent, const char *pszKey, std::string &rstrVal, std::string &strErr); private: std::string m_strBrokerAddr; std::string m_strDevicePrefix; std::string m_strTlsCaCrtFile; std::string m_strTlsClCrtFile; std::string m_strTlsClKeyFile; std::string m_strTlsPSK; std::string m_strDeviceID; std::string m_strShmID; std::string m_strTopicPrefix; int m_nBrokerPort; int m_nDefaultQOS; bool m_bDefaultRetain; std::string m_strLastWillTopic; std::string m_strLastWillMessage; long long m_nLastWillMessage; int m_nLastWillQos; bool m_bLastWillRetain; bool m_bHasLastWill; bool m_bLastWillOnExit; std::string m_strLastWillOnExitMsg; long long m_nLastWillOnExitMsg; std::string m_strConnectTopic; std::string m_strConnectMessage; long long m_nConnectMessage; int m_nConnectQos; bool m_bConnectRetain; bool m_bHasConnect; bool m_bDisableTopicPrefix; bool m_bHasPrefix; int m_nTlsMode; int m_nMaxKeepAlive; }; ///////////////////////////////////////////////////////////////////////////// #endif // __cplusplus #endif // !defined(AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_)