mqttcfg.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // mqttcfg.h :
  2. //
  3. #if !defined(AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_)
  4. #define AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_
  5. #include <string>
  6. #include <sys/socket.h>
  7. #include <arpa/inet.h>
  8. #include <linux/if_arp.h>
  9. #include <netinet/in.h>
  10. #include <ifaddrs.h>
  11. #include <sys/ioctl.h>
  12. #ifndef _LIBBUILD
  13. #include <gfa/svc/common/logfile.h>
  14. #include <gfa/svc/mqttcl/mqttjson.h>
  15. #else // _LIBBUILD
  16. #include "common/logfile.h"
  17. #include "mqttjson.h"
  18. #endif // _LIBBUILD
  19. #ifdef __cplusplus
  20. /////////////////////////////////////////////////////////////////////////////
  21. // mqttcfg.h - Declarations:
  22. #define MQTTCL_TLS_MODE_OFF 0 // no TLS
  23. #define MQTTCL_TLS_MODE_CRT 1 // TLS using certificates
  24. #define MQTTCL_TLS_MODE_PSK 2 // TLS using preshared key
  25. #define MQTTCL_MIN_QOS 0
  26. #define MQTTCL_MAX_QOS 2
  27. #define MQTTCL_DEFAULT_QOS MQTTCL_MAX_QOS
  28. #define MQTTCL_DEFAULT_RETAIN false
  29. #define MQTTCL_DEVICE_PREFIX "GfA"
  30. #define MQTTCL_DEFAULT_LAST_WILL_CONNECT_TOPIC "LAST_WILL_CONNECT"
  31. #define MQTTCL_DEFAULT_MAX_KEEP_ALIVE_TIME 60
  32. /////////////////////////////////////////////////////////////////////////////
  33. class CMqttClConfig
  34. {
  35. public:
  36. CMqttClConfig(const char *pszShmUuid);
  37. virtual ~CMqttClConfig(void);
  38. bool LoadCfg(const char *pszCfgFilePath, CLogfile &rlf);
  39. inline const char* GetBrokerAddr(void) const {
  40. return m_strBrokerAddr.empty() ? NULL : m_strBrokerAddr.c_str();
  41. }
  42. inline unsigned short GetBrokerPort(void) const {
  43. return (unsigned short)m_nBrokerPort;
  44. }
  45. inline int GetDefaultQOS(void) const {
  46. return m_nDefaultQOS;
  47. }
  48. inline int GetTLSMode(void) const {
  49. return m_nTlsMode;
  50. }
  51. inline bool GetDefaultRetain(void) const {
  52. return m_bDefaultRetain;
  53. }
  54. inline const char* GetDevicePrefix(void) const {
  55. return m_strDevicePrefix.empty() ? NULL : m_strDevicePrefix.c_str();
  56. }
  57. inline const char* GetTlsCaCrtFile(void) const {
  58. return m_strTlsCaCrtFile.empty() ? NULL : m_strTlsCaCrtFile.c_str();
  59. }
  60. inline const char* GetTlsClCrtFile(void) const {
  61. return m_strTlsClCrtFile.empty() ? NULL : m_strTlsClCrtFile.c_str();
  62. }
  63. inline const char* GetTlsClKeyFile(void) const {
  64. return m_strTlsClKeyFile.empty() ? NULL : m_strTlsClKeyFile.c_str();
  65. }
  66. inline const char* GetTlsPSK(void) const {
  67. return m_strTlsPSK.empty() ? NULL : m_strTlsPSK.c_str();
  68. }
  69. inline const char* GetDeviceID(void) const {
  70. return m_strDeviceID.empty() ? NULL : m_strDeviceID.c_str();
  71. }
  72. inline const char* GetShmID(void) const {
  73. return m_strShmID.empty() ? NULL : m_strShmID.c_str();
  74. }
  75. inline const char* GetPrefix(void) const {
  76. return m_strPrefix.empty() ? NULL : m_strPrefix.c_str();
  77. }
  78. inline const char* GetLastWillTopic(void) const {
  79. return m_strLastWillTopic.empty() ? NULL : m_strLastWillTopic.c_str();
  80. }
  81. inline const char* GetLastWillMessage(void) const {
  82. return m_strLastWillMessage.empty() ? NULL : m_strLastWillMessage.c_str();
  83. }
  84. inline int GetLastWillMessageLength(void) const {
  85. return (int)m_strLastWillMessage.length();
  86. }
  87. inline int GetLastWillQOS(void) const {
  88. return m_nLastWillQos;
  89. }
  90. inline bool GetLastWillRetain(void) const {
  91. return m_bLastWillRetain;
  92. }
  93. inline bool HasLastWill(void) const {
  94. return m_bHasLastWill;
  95. }
  96. inline bool HasLastWillOnExit(void) const {
  97. return m_bLastWillOnExit;
  98. }
  99. inline const char* GetLastWillOnExitMessage(void) const {
  100. return m_strLastWillOnExitMsg.empty() ? NULL : m_strLastWillOnExitMsg.c_str();
  101. }
  102. inline int GetLastWillOnExitMessageLength(void) const {
  103. return (int)m_strLastWillOnExitMsg.length();
  104. }
  105. inline const char* GetConnectTopic(void) const {
  106. return m_strConnectTopic.empty() ? NULL : m_strConnectTopic.c_str();
  107. }
  108. inline const char* GetConnectMessage(void) const {
  109. return m_strConnectMessage.empty() ? NULL : m_strConnectMessage.c_str();
  110. }
  111. inline int GetConnectMessageLength(void) const {
  112. return (int)m_strConnectMessage.length();
  113. }
  114. inline int GetConnectQOS(void) const {
  115. return m_nConnectQos;
  116. }
  117. inline bool GetConnectRetain(void) const {
  118. return m_bConnectRetain;
  119. }
  120. inline bool HasConnectMsg(void) const {
  121. return m_bHasConnect;
  122. }
  123. inline bool HasPrefix(void) const {
  124. return m_bHasPrefix;
  125. }
  126. inline int GetKeepAliveTime(void) const {
  127. return m_nMaxKeepAlive;
  128. }
  129. static sa_family_t GetDevIdInterfaceName(char *pszItfName, size_t nCChItfName, const char *pszRequested);
  130. static const char* GetMacAddress(std::string &s);
  131. static std::string CreateDeviceID(const char *pszDevicePrefix);
  132. private:
  133. bool GetValue(CJson_t &rjtParent, const char *pszKey, CJson_t &rjtVal, std::string &strErr);
  134. bool GetBoolValue(CJson_t &rjtParent, const char *pszKey, bool &rbVal, std::string &strErr);
  135. bool GetIntValue(CJson_t &rjtParent, const char *pszKey, int &rnVal, std::string &strErr);
  136. bool GetStringValue(CJson_t &rjtParent, const char *pszKey, std::string &rstrVal, std::string &strErr);
  137. private:
  138. std::string m_strBrokerAddr;
  139. std::string m_strDevicePrefix;
  140. std::string m_strTlsCaCrtFile;
  141. std::string m_strTlsClCrtFile;
  142. std::string m_strTlsClKeyFile;
  143. std::string m_strTlsPSK;
  144. std::string m_strDeviceID;
  145. std::string m_strShmID;
  146. std::string m_strPrefix;
  147. int m_nBrokerPort;
  148. int m_nDefaultQOS;
  149. bool m_bDefaultRetain;
  150. std::string m_strLastWillTopic;
  151. std::string m_strLastWillMessage;
  152. int m_nLastWillQos;
  153. bool m_bLastWillRetain;
  154. bool m_bHasLastWill;
  155. bool m_bLastWillOnExit;
  156. std::string m_strLastWillOnExitMsg;
  157. std::string m_strConnectTopic;
  158. std::string m_strConnectMessage;
  159. int m_nConnectQos;
  160. bool m_bConnectRetain;
  161. bool m_bHasConnect;
  162. bool m_bHasPrefix;
  163. int m_nTlsMode;
  164. int m_nMaxKeepAlive;
  165. };
  166. /////////////////////////////////////////////////////////////////////////////
  167. #endif // __cplusplus
  168. #endif // !defined(AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_)