mqttcfg.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_DEFAULT_LAST_WILL_CONNECT_TOPIC "LWC"
  30. #define MQTTCL_MIN_MAX_KEEP_ALIVE_TIME 10
  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* GetTopicPrefix(void) const {
  76. return m_strTopicPrefix.empty() ? NULL : m_strTopicPrefix.c_str();
  77. }
  78. inline const char* GetLastWillTopic(void) const {
  79. return m_strLastWillTopic.empty() ? NULL : m_strLastWillTopic.c_str();
  80. }
  81. inline const void* GetLastWillMessage(void) const {
  82. if(!m_bHasLastWill)
  83. return nullptr;
  84. return m_strLastWillMessage.empty() ? (const void*)&m_nLastWillMessage : (const void*)m_strLastWillMessage.c_str();
  85. }
  86. inline int GetLastWillMessageLength(void) const {
  87. if(!m_bHasLastWill)
  88. return 0;
  89. return (int)(m_strLastWillMessage.empty() ? sizeof(m_nLastWillMessage) : m_strLastWillMessage.length());
  90. }
  91. inline int GetLastWillQOS(void) const {
  92. return m_nLastWillQos;
  93. }
  94. inline bool GetLastWillRetain(void) const {
  95. return m_bLastWillRetain;
  96. }
  97. inline bool HasLastWill(void) const {
  98. return m_bHasLastWill;
  99. }
  100. inline bool HasLastWillOnExit(void) const {
  101. return m_bLastWillOnExit;
  102. }
  103. inline const void* GetLastWillOnExitMessage(void) const {
  104. return m_strLastWillOnExitMsg.empty() ? (const void*)&m_nLastWillOnExitMsg : (const void*)m_strLastWillOnExitMsg.c_str();
  105. }
  106. inline int GetLastWillOnExitMessageLength(void) const {
  107. return (int)(m_strLastWillOnExitMsg.empty() ? sizeof(m_nLastWillOnExitMsg) : m_strLastWillOnExitMsg.length());
  108. }
  109. inline const char* GetConnectTopic(void) const {
  110. return m_strConnectTopic.empty() ? NULL : m_strConnectTopic.c_str();
  111. }
  112. inline const void* GetConnectMessage(void) const {
  113. if(!m_bHasConnect)
  114. return nullptr;
  115. return m_strConnectMessage.empty() ? (const void*)&m_nConnectMessage : (const void*)m_strConnectMessage.c_str();
  116. }
  117. inline int GetConnectMessageLength(void) const {
  118. if(!m_bHasConnect)
  119. return 0;
  120. return (int)(m_strConnectMessage.empty() ? sizeof(m_nConnectMessage) : m_strConnectMessage.length());
  121. }
  122. inline int GetConnectQOS(void) const {
  123. return m_nConnectQos;
  124. }
  125. inline bool GetConnectRetain(void) const {
  126. return m_bConnectRetain;
  127. }
  128. inline bool HasConnectMsg(void) const {
  129. return m_bHasConnect;
  130. }
  131. inline bool TopicPrefixDisabled(void) const {
  132. return m_bDisableTopicPrefix;
  133. }
  134. inline bool HasPrefix(void) const {
  135. return m_bHasPrefix;
  136. }
  137. inline int GetKeepAliveTime(void) const {
  138. return m_nMaxKeepAlive;
  139. }
  140. static sa_family_t GetDevIdInterfaceName(char *pszItfName, size_t nCChItfName, const char *pszRequested);
  141. static const char* GetMacAddress(std::string &s);
  142. static std::string CreateDeviceID(const char *pszDevicePrefix);
  143. private:
  144. bool GetValue(CJson_t &rjtParent, const char *pszKey, CJson_t &rjtVal, std::string &strErr);
  145. bool GetBoolValue(CJson_t &rjtParent, const char *pszKey, bool &rbVal, std::string &strErr);
  146. bool GetIntValue(CJson_t &rjtParent, const char *pszKey, int &rnVal, std::string &strErr);
  147. bool GetInt64Value(CJson_t &rjtParent, const char *pszKey, long long &rnVal, std::string &strErr);
  148. bool GetStringValue(CJson_t &rjtParent, const char *pszKey, std::string &rstrVal, std::string &strErr);
  149. private:
  150. std::string m_strBrokerAddr;
  151. std::string m_strDevicePrefix;
  152. std::string m_strTlsCaCrtFile;
  153. std::string m_strTlsClCrtFile;
  154. std::string m_strTlsClKeyFile;
  155. std::string m_strTlsPSK;
  156. std::string m_strDeviceID;
  157. std::string m_strShmID;
  158. std::string m_strTopicPrefix;
  159. int m_nBrokerPort;
  160. int m_nDefaultQOS;
  161. bool m_bDefaultRetain;
  162. std::string m_strLastWillTopic;
  163. std::string m_strLastWillMessage;
  164. long long m_nLastWillMessage;
  165. int m_nLastWillQos;
  166. bool m_bLastWillRetain;
  167. bool m_bHasLastWill;
  168. bool m_bLastWillOnExit;
  169. std::string m_strLastWillOnExitMsg;
  170. long long m_nLastWillOnExitMsg;
  171. std::string m_strConnectTopic;
  172. std::string m_strConnectMessage;
  173. long long m_nConnectMessage;
  174. int m_nConnectQos;
  175. bool m_bConnectRetain;
  176. bool m_bHasConnect;
  177. bool m_bDisableTopicPrefix;
  178. bool m_bHasPrefix;
  179. int m_nTlsMode;
  180. int m_nMaxKeepAlive;
  181. };
  182. /////////////////////////////////////////////////////////////////////////////
  183. #endif // __cplusplus
  184. #endif // !defined(AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_)