// mqttclient.h : // // https://mosquitto.org/man/mosquitto-8.html #if !defined(AGD_MQTTCLIENT_H__36D28DC5_D30B_43A6_B6EE_84BF95E02490__INCLUDED_) #define AGD_MQTTCLIENT_H__36D28DC5_D30B_43A6_B6EE_84BF95E02490__INCLUDED_ #include #ifndef _LIBBUILD #include #include #else // _LIBBUILD #include "mqttmsg.h" #include "processclock.h" #endif // _LIBBUILD #ifdef __cplusplus ///////////////////////////////////////////////////////////////////////////// // mqttclient.h - Declarations: ///////////////////////////////////////////////////////////////////////////// typedef enum _MQTT_NOTIFICATION_EVENTS { NEVT_Connect = 0x0001, NEVT_Connect_With_Flags = 0x0002, NEVT_Disconnect = 0x0004, NEVT_Subscribe = 0x0008, NEVT_Unsubscribe = 0x0010, NEVT_Publish = 0x0020, NEVT_Message = 0x0040, NEVT_Log = 0x0080, NEVT_Error = 0x0100 }MQTT_NOTIFICATION_EVENTS, *LPMQTT_NOTIFICATION_EVENTS; typedef const MQTT_NOTIFICATION_EVENTS *LPCMQTT_NOTIFICATION_EVENTS; ///////////////////////////////////////////////////////////////////////////// typedef struct _MQTT_EVENT_CONNECT { int rc; int flags; }MQTT_EVENT_CONNECT, *LPMQTT_EVENT_CONNECT; typedef const MQTT_EVENT_CONNECT *LPCMQTT_EVENT_CONNECT; ///////////////////////////////////////////////////////////////////////////// typedef struct _MQTT_EVENT_SUBSCRIBE { int mid; int qos_count; const int *granted_qos; }MQTT_EVENT_SUBSCRIBE, *LPMQTT_EVENT_SUBSCRIBE; typedef const MQTT_EVENT_SUBSCRIBE *LPCMQTT_EVENT_SUBSCRIBE; ///////////////////////////////////////////////////////////////////////////// typedef struct _MQTT_EVENT_PUBLISH { int mid; }MQTT_EVENT_PUBLISH, *LPMQTT_EVENT_PUBLISH; typedef const MQTT_EVENT_PUBLISH *LPCMQTT_EVENT_PUBLISH; ///////////////////////////////////////////////////////////////////////////// typedef struct _MQTT_EVENT_MESSAGE { const CMqttMessage *msg; }MQTT_EVENT_MESSAGE, *LPMQTT_EVENT_MESSAGE; typedef const MQTT_EVENT_MESSAGE *LPCMQTT_EVENT_MESSAGE; ///////////////////////////////////////////////////////////////////////////// typedef struct _MQTT_EVENT_LOG { int level; const char *str; }MQTT_EVENT_LOG, *LPMQTT_EVENT_LOG; typedef const MQTT_EVENT_LOG *LPCMQTT_EVENT_LOG; ///////////////////////////////////////////////////////////////////////////// typedef struct _MQTT_GENERIC_NOTIFICATION { _MQTT_GENERIC_NOTIFICATION(void) { #pragma GCC diagnostic push #if __GNUC__ >= 8 #pragma GCC diagnostic ignored "-Wclass-memaccess" #endif memset(this, 0, sizeof(*this)); #pragma GCC diagnostic pop } MQTT_NOTIFICATION_EVENTS evt; union { MQTT_EVENT_CONNECT con; MQTT_EVENT_SUBSCRIBE sub; MQTT_EVENT_PUBLISH pub; MQTT_EVENT_MESSAGE msg; MQTT_EVENT_LOG log; }; }MQTT_GENERIC_NOTIFICATION, *LPMQTT_GENERIC_NOTIFICATION; typedef const MQTT_GENERIC_NOTIFICATION *LPCMQTT_GENERIC_NOTIFICATION; ///////////////////////////////////////////////////////////////////////////// typedef void (*PFN_MQTT_EVENTS_CALLBACK)(LPCMQTT_GENERIC_NOTIFICATION, void*); ///////////////////////////////////////////////////////////////////////////// class CMqttClient : public mosqpp::mosquittopp { public: CMqttClient(const char *id); virtual ~CMqttClient(void); ///////////////////////////////////////////////////////////////////////// // static int Init(void); static int Cleanup(void); int publish(CMqttMessage *pMsg); CMqttMessage* PopRcvMsg(void); void PushRcvMsg(CMqttMessage *pMsg); CMqttMessage* PopSndMsg(void); void PushSndMsg(CMqttMessage *pMsg); CMqttMessageQueue& GetMsgQueueRcv(void) { return m_rcvMsg;} CMqttMessageQueue& GetMsgQueueSnd(void) { return m_sndMsg;} void SetClientEventCallback(PFN_MQTT_EVENTS_CALLBACK pfnEvtCallback, uint32_t evtMask, void *pUserParam) { m_pEvtCallback = pfnEvtCallback; m_evtMask = evtMask; m_pUserParam = pUserParam; } int timed_loop(pc_time64_t nLoopTime); bool TimedLoop(pc_time64_t nLoopTime, int &rnErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr); static bool EvalError(int nErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr); ///////////////////////////////////////////////////////////////////////// // virtual void on_connect(int rc); virtual void on_connect_with_flags(int rc, int flags); virtual void on_disconnect(int rc); virtual void on_subscribe(int mid, int qos_count, const int *granted_qos); virtual void on_unsubscribe(int mid); virtual void on_publish(int mid); virtual void on_message(const struct mosquitto_message *message); virtual void on_log(int level, const char *str); virtual void on_error(void); private: CProcessClock m_pc; CMqttMessageQueue m_rcvMsg; CMqttMessageQueue m_sndMsg; PFN_MQTT_EVENTS_CALLBACK m_pEvtCallback; uint32_t m_evtMask; void *m_pUserParam; }; ///////////////////////////////////////////////////////////////////////////// #endif // __cplusplus #endif // !defined(AGD_MQTTCLIENT_H__36D28DC5_D30B_43A6_B6EE_84BF95E02490__INCLUDED_)