3.1.7.2 MQTT Service

The MQTT service provides an Application Programming Interface (API) to manage MQTT functionalities. These functionalities include, configuring the MQTT settings, connecting, disconnecting and reconnecting to the MQTT broker, publishing, subscribing and setting callbacks.

The MQTT service API example is as follows:
RNWF_RESULT_t RNWF_MQTT_SrvCtrl( RNWF_MQTT_SERVICE_t request, void *input)
It handles following services and reports the result to application over the return code or through the registered callback:
Table 3-4. MQTT Services
ServiceInputDescription
RNWF_MQTT_CONFIGBroker URL, Port, Client ID, Username, TLS configurationConfigures the MQTT server details along with the corresponding TLS configurations
RNWF_MQTT_CONNECTNoneInitiates the MQTT connection to the configured MQTT broker
RNWF_MQTT_RECONNECTNoneTriggers the re-connection to the configured MQTT broker
RNWF_MQTT_DISCONNECTNoneDisconnects from the connected MQTT broker
RNWF_MQTT_SUBSCRIBE_QOS0Subscribe topic (String)Subscribes to the given subscribe topic with QoS0
RNWF_MQTT_SUBSCRIBE_QOS1Subscribe topic (String)Subscribes to the given subscribe topic with QoS1
RNWF_MQTT_SUBSCRIBE_QOS2Subscribe topic (String)Subscribes to the given subscribe topic with QoS2
RNWF_MQTT_PUBLISHNew, QOS, Retain, topic, messagePublish the message on given publish topic and configuration
RNWF_MQTT_SET_CALLBACKCallback Function HandlerRegisters the MQTT callback to report the status to user application
The following list captures the MQTT Callback event codes and their arguments
Table 3-5. Callback Event codes
EventResponse ComponentsComments
RNWF_MQTT_CONNECTEDNoneReported once connected to MQTT broker
RNWF_MQTT_DISCONNECTEDNoneEvent to report the MQTT broker disconnection
RNWF_MQTT_SUBCRIBE_MSGdup, QoS, retain, topic, payloadReports the received payload for the subscribed topic
RNWF_MQTT_SUBCRIBE_ACKInteger string Subscribe ack return code

MQTT Publish

User application can publish to the MQTT broker by creating the MQTT frame and then sending the frame using the RNWF_MQTT_SrvCtrl(RNWF_MQTT_PUBLISH, (void *)&mqtt_pub) API. The sequence chart is shown below.

MQTT Subscribe

The sequence for subscribing to a topic from the MQTT Broker is shown below. The user application needs to use the RNWF_MQTT_SrvCtrl(RNWF_MQTT_SUBSCRIBE_QOS0, buffer) API to subscribe to the topic with the appropriate QoS value.