mqttcfg.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /////////////////////////////////////////////////////////////////////////////
  31. class CMqttClConfig
  32. {
  33. public:
  34. CMqttClConfig(const char *pszShmUuid);
  35. virtual ~CMqttClConfig(void);
  36. bool LoadCfg(const char *pszCfgFilePath, CLogfile &rlf);
  37. inline const char* GetBrokerAddr(void) const {
  38. return m_strBrokerAddr.empty() ? NULL : m_strBrokerAddr.c_str();
  39. }
  40. inline unsigned short GetBrokerPort(void) const {
  41. return (unsigned short)m_nBrokerPort;
  42. }
  43. inline int GetDefaultQOS(void) const {
  44. return m_nDefaultQOS;
  45. }
  46. inline int GetTLSMode(void) const {
  47. return m_nTlsMode;
  48. }
  49. inline bool GetDefaultRetain(void) const {
  50. return m_bDefaultRetain;
  51. }
  52. inline const char* GetDevicePrefix(void) const {
  53. return m_strDevicePrefix.empty() ? NULL : m_strDevicePrefix.c_str();
  54. }
  55. inline const char* GetTlsCaCrtFile(void) const {
  56. return m_strTlsCaCrtFile.empty() ? NULL : m_strTlsCaCrtFile.c_str();
  57. }
  58. inline const char* GetTlsClCrtFile(void) const {
  59. return m_strTlsClCrtFile.empty() ? NULL : m_strTlsClCrtFile.c_str();
  60. }
  61. inline const char* GetTlsClKeyFile(void) const {
  62. return m_strTlsClKeyFile.empty() ? NULL : m_strTlsClKeyFile.c_str();
  63. }
  64. inline const char* GetTlsPSK(void) const {
  65. return m_strTlsPSK.empty() ? NULL : m_strTlsPSK.c_str();
  66. }
  67. inline const char* GetDeviceID(void) const {
  68. return m_strDeviceID.empty() ? NULL : m_strDeviceID.c_str();
  69. }
  70. inline const char* GetShmID(void) const {
  71. return m_strShmID.empty() ? NULL : m_strShmID.c_str();
  72. }
  73. static sa_family_t GetDevIdInterfaceName(char *pszItfName, size_t nCChItfName, const char *pszRequested);
  74. static const char* GetMacAddress(std::string &s);
  75. static std::string CreateDeviceID(const char *pszDevicePrefix);
  76. private:
  77. bool GetValue(CJson_t &rjtParent, const char *pszKey, CJson_t &rjtVal, std::string &strErr);
  78. bool GetBoolValue(CJson_t &rjtParent, const char *pszKey, bool &rbVal, std::string &strErr);
  79. bool GetIntValue(CJson_t &rjtParent, const char *pszKey, int &rnVal, std::string &strErr);
  80. bool GetStringValue(CJson_t &rjtParent, const char *pszKey, std::string &rstrVal, std::string &strErr);
  81. private:
  82. std::string m_strBrokerAddr;
  83. std::string m_strDevicePrefix;
  84. std::string m_strTlsCaCrtFile;
  85. std::string m_strTlsClCrtFile;
  86. std::string m_strTlsClKeyFile;
  87. std::string m_strTlsPSK;
  88. std::string m_strDeviceID;
  89. std::string m_strShmID;
  90. int m_nBrokerPort;
  91. int m_nDefaultQOS;
  92. bool m_bDefaultRetain;
  93. int m_nTlsMode;
  94. };
  95. /////////////////////////////////////////////////////////////////////////////
  96. #endif // __cplusplus
  97. #endif // !defined(AGD_MQTTCFG_H__35CD3AFA_36BC_442B_83B7_D12E407E6B3A__INCLUDED_)