mqttclient.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // mqttclient.h :
  2. //
  3. // https://mosquitto.org/man/mosquitto-8.html
  4. #if !defined(AGD_MQTTCLIENT_H__36D28DC5_D30B_43A6_B6EE_84BF95E02490__INCLUDED_)
  5. #define AGD_MQTTCLIENT_H__36D28DC5_D30B_43A6_B6EE_84BF95E02490__INCLUDED_
  6. #include <string.h>
  7. #ifndef _LIBBUILD
  8. #include <gfa/svc/mqttcl/mqttmsg.h>
  9. #include <gfa/svc/common/processclock.h>
  10. #else // _LIBBUILD
  11. #include "mqttmsg.h"
  12. #include "processclock.h"
  13. #endif // _LIBBUILD
  14. #ifdef __cplusplus
  15. /////////////////////////////////////////////////////////////////////////////
  16. // mqttclient.h - Declarations:
  17. /////////////////////////////////////////////////////////////////////////////
  18. typedef enum _MQTT_NOTIFICATION_EVENTS
  19. {
  20. NEVT_Connect = 0x0001,
  21. NEVT_Connect_With_Flags = 0x0002,
  22. NEVT_Disconnect = 0x0004,
  23. NEVT_Subscribe = 0x0008,
  24. NEVT_Unsubscribe = 0x0010,
  25. NEVT_Publish = 0x0020,
  26. NEVT_Message = 0x0040,
  27. NEVT_Log = 0x0080,
  28. NEVT_Error = 0x0100
  29. }MQTT_NOTIFICATION_EVENTS, *LPMQTT_NOTIFICATION_EVENTS;
  30. typedef const MQTT_NOTIFICATION_EVENTS *LPCMQTT_NOTIFICATION_EVENTS;
  31. /////////////////////////////////////////////////////////////////////////////
  32. typedef struct _MQTT_EVENT_CONNECT
  33. {
  34. int rc;
  35. int flags;
  36. }MQTT_EVENT_CONNECT, *LPMQTT_EVENT_CONNECT;
  37. typedef const MQTT_EVENT_CONNECT *LPCMQTT_EVENT_CONNECT;
  38. /////////////////////////////////////////////////////////////////////////////
  39. typedef struct _MQTT_EVENT_SUBSCRIBE
  40. {
  41. int mid;
  42. int qos_count;
  43. const int *granted_qos;
  44. }MQTT_EVENT_SUBSCRIBE, *LPMQTT_EVENT_SUBSCRIBE;
  45. typedef const MQTT_EVENT_SUBSCRIBE *LPCMQTT_EVENT_SUBSCRIBE;
  46. /////////////////////////////////////////////////////////////////////////////
  47. typedef struct _MQTT_EVENT_PUBLISH
  48. {
  49. int mid;
  50. }MQTT_EVENT_PUBLISH, *LPMQTT_EVENT_PUBLISH;
  51. typedef const MQTT_EVENT_PUBLISH *LPCMQTT_EVENT_PUBLISH;
  52. /////////////////////////////////////////////////////////////////////////////
  53. typedef struct _MQTT_EVENT_MESSAGE
  54. {
  55. const CMqttMessage *msg;
  56. }MQTT_EVENT_MESSAGE, *LPMQTT_EVENT_MESSAGE;
  57. typedef const MQTT_EVENT_MESSAGE *LPCMQTT_EVENT_MESSAGE;
  58. /////////////////////////////////////////////////////////////////////////////
  59. typedef struct _MQTT_EVENT_LOG
  60. {
  61. int level;
  62. const char *str;
  63. }MQTT_EVENT_LOG, *LPMQTT_EVENT_LOG;
  64. typedef const MQTT_EVENT_LOG *LPCMQTT_EVENT_LOG;
  65. /////////////////////////////////////////////////////////////////////////////
  66. typedef struct _MQTT_GENERIC_NOTIFICATION
  67. {
  68. _MQTT_GENERIC_NOTIFICATION(void)
  69. {
  70. #pragma GCC diagnostic push
  71. #if __GNUC__ >= 8
  72. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  73. #endif
  74. memset(this, 0, sizeof(*this));
  75. #pragma GCC diagnostic pop
  76. }
  77. MQTT_NOTIFICATION_EVENTS evt;
  78. union
  79. {
  80. MQTT_EVENT_CONNECT con;
  81. MQTT_EVENT_SUBSCRIBE sub;
  82. MQTT_EVENT_PUBLISH pub;
  83. MQTT_EVENT_MESSAGE msg;
  84. MQTT_EVENT_LOG log;
  85. };
  86. }MQTT_GENERIC_NOTIFICATION, *LPMQTT_GENERIC_NOTIFICATION;
  87. typedef const MQTT_GENERIC_NOTIFICATION *LPCMQTT_GENERIC_NOTIFICATION;
  88. /////////////////////////////////////////////////////////////////////////////
  89. typedef void (*PFN_MQTT_EVENTS_CALLBACK)(LPCMQTT_GENERIC_NOTIFICATION, void*);
  90. /////////////////////////////////////////////////////////////////////////////
  91. class CMqttClient : public mosqpp::mosquittopp
  92. {
  93. public:
  94. CMqttClient(const char *id);
  95. virtual ~CMqttClient(void);
  96. /////////////////////////////////////////////////////////////////////////
  97. //
  98. static int Init(void);
  99. static int Cleanup(void);
  100. int publish(CMqttMessage *pMsg);
  101. CMqttMessage* PopRcvMsg(void);
  102. void PushRcvMsg(CMqttMessage *pMsg);
  103. CMqttMessage* PopSndMsg(void);
  104. void PushSndMsg(CMqttMessage *pMsg);
  105. CMqttMessageQueue& GetMsgQueueRcv(void) {
  106. return m_rcvMsg;}
  107. CMqttMessageQueue& GetMsgQueueSnd(void) {
  108. return m_sndMsg;}
  109. void SetClientEventCallback(PFN_MQTT_EVENTS_CALLBACK pfnEvtCallback, uint32_t evtMask, void *pUserParam)
  110. {
  111. m_pEvtCallback = pfnEvtCallback;
  112. m_evtMask = evtMask;
  113. m_pUserParam = pUserParam;
  114. }
  115. int timed_loop(pc_time64_t nLoopTime);
  116. bool TimedLoop(pc_time64_t nLoopTime, int &rnErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr);
  117. static bool EvalError(int nErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr);
  118. /////////////////////////////////////////////////////////////////////////
  119. //
  120. virtual void on_connect(int rc);
  121. virtual void on_connect_with_flags(int rc, int flags);
  122. virtual void on_disconnect(int rc);
  123. virtual void on_subscribe(int mid, int qos_count, const int *granted_qos);
  124. virtual void on_unsubscribe(int mid);
  125. virtual void on_publish(int mid);
  126. virtual void on_message(const struct mosquitto_message *message);
  127. virtual void on_log(int level, const char *str);
  128. virtual void on_error(void);
  129. private:
  130. CProcessClock m_pc;
  131. CMqttMessageQueue m_rcvMsg;
  132. CMqttMessageQueue m_sndMsg;
  133. PFN_MQTT_EVENTS_CALLBACK m_pEvtCallback;
  134. uint32_t m_evtMask;
  135. void *m_pUserParam;
  136. };
  137. /////////////////////////////////////////////////////////////////////////////
  138. #endif // __cplusplus
  139. #endif // !defined(AGD_MQTTCLIENT_H__36D28DC5_D30B_43A6_B6EE_84BF95E02490__INCLUDED_)