MQTT Functions
Microvisor Public Beta
Microvisor is in a pre-release phase and the information contained in this document is subject to change. Some features referenced below may not be fully available until Microvisor’s General Availability (GA) release.
Microvisor system calls currently include the following functions to manage MQTT communications over the Internet.
mvMqttGetNextReadableDataType()
mvMqttRequestConnect()
mvMqttReadConnectResponse()
mvMqttRequestSubscribe()
mvMqttReadSubscribeResponse()
mvMqttRequestUnsubscribe()
mvMqttReadUnsubscribeResponse()
mvMqttRequestPublish()
mvMqttReadPublishResponse()
mvMqttReceiveMessage()
mvMqttReceiveLostMessageInfo()
mvMqttAcknowledgeMessage()
mvMqttRequestDisconnect()
mvMqttReadDisconnectResponse()
MQTT
This document assumes you are familiar with the MQTT specification and how the protocol operates.
Request-response correlation
A number of the following system calls consume or return ‘correlation IDs’. These are 32-bit unsigned integer values you supply to help your code match responses to source requests. This is helpful because requests may be fulfilled in a non-deterministic order. Format values in a way that’s appropriate to your application.
Return values and errors
All of the functions described below return a 32-bit integer that is one of the values from the standard Microvisor enumeration MvStatus
. All possible error values for a given system call are provided with each function’s description.
Success is always signaled by a return value of zero (MV_STATUS_OKAY
).
The MQTT functions for managing requests and the responses they generate operate through Microvisor network channels. To learn how to establish network connections, and open channels through them, please see Microvisor network functions.
mvMqttGetNextReadableDataType()
Determine what type of MQTT data a channel last received
Declaration
extern enum MvStatus mvMqttGetNextReadableDataType(MvChannelHandle handle,
enum MvMqttReadableDataType *type);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to obtain the data type. This must be a channel of type MV_CHANNELTYPE_MQTT |
type |
A pointer to memory into which Microvisor will write the data type value |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
type does not reference memory accessible to the application |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
Description
Issued MQTT requests (listed below) will eventually cause a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
to be posted to the host channel’s notification center. On receipt of this notification, call mvMqttGetNextReadableDataType()
to determine the type of data received and use this information to select the appropriate system call to retrieve the data.
The integer written to the memory referenced by type
will be one of the following values:
Constant | Description |
---|---|
MV_MQTTREADABLEDATATYPE_NONE |
No unconsumed data is available at this time |
MV_MQTTREADABLEDATATYPE_MESSAGERECEIVED |
A message is ready for consumption |
MV_MQTTREADABLEDATATYPE_MESSAGELOST |
A message was lost; details are available |
MV_MQTTREADABLEDATATYPE_CONNECTRESPONSE |
A response to a connect request is available |
MV_MQTTREADABLEDATATYPE_SUBSCRIBERESPONSE |
A response to a subscribe request is available |
MV_MQTTREADABLEDATATYPE_UNSUBSCRIBERESPONSE |
A response to an unsubscribe request is available |
MV_MQTTREADABLEDATATYPE_PUBLISHRESPONSE |
A response to a publish request is available |
MV_MQTTREADABLEDATATYPE_DISCONNECTRESPONSE |
A response to a disconnect request is available |
Example
This function, taken from our MQTT demo code, shows how you might use mvMqttGetNextReadableDataType()
to check for the type of data received and to branch to the correct data-access function.
void mqtt_handle_readable_event() {
enum MvMqttReadableDataType readableDataType;
if (mvMqttGetNextReadableDataType(mqtt_channel, &readableDataType) != MV_STATUS_OKAY) return;
server_log("MQTT Event: data received of type %02X", readableDataType);
switch (readableDataType) {
case MV_MQTTREADABLEDATATYPE_CONNECTRESPONSE:
// A response to a connect request is available.
pushMessage(OnMQTTEventConnectResponse);
break;
case MV_MQTTREADABLEDATATYPE_MESSAGERECEIVED:
// A message is ready for consumption.
pushMessage(OnMQTTEventMessageReceived);
break;
case MV_MQTTREADABLEDATATYPE_MESSAGELOST:
// A message was lost, details are available.
pushMessage(OnMQTTEventMessageLost);
break;
case MV_MQTTREADABLEDATATYPE_SUBSCRIBERESPONSE:
// A response to a subscribe request is available.
pushMessage(OnMQTTEventSubscribeResponse);
break;
case MV_MQTTREADABLEDATATYPE_UNSUBSCRIBERESPONSE:
// A response to an unsubscribe request is available.
pushMessage(OnMQTTEventUnsubscribeResponse);
break;
case MV_MQTTREADABLEDATATYPE_PUBLISHRESPONSE:
// A response to a publish request is available.
pushMessage(OnMQTTEventPublishResponse);
break;
case MV_MQTTREADABLEDATATYPE_DISCONNECTRESPONSE:
// A response to a disconnect operation is available.
pushMessage(OnMQTTEventDisconnectResponse);
break;
default:
break;
}
}
mvMqttRequestConnect()
Initiate a connection to an MQTT broker
Declaration
extern enum MvStatus mvMqttRequestConnect(MvChannelHandle handle,
struct MvMqttConnectRequest *request);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
request |
A pointer to an MvMqttConnectRequest structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
request does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
One or more of the pointers within request are illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDCERTIFICATION |
Multiple credential types were supplied |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_REQUESTALREADYSENT |
The request has already been sent, either successfully or not |
MV_STATUS_TOOMANYELEMENTS |
More than eight certificates were included in either the CA or device certificate chains |
MV_STATUS_REQUIREDELEMENTMISSING |
A mandatory request configuration element has not been included |
Notifications
This call, if successful, may subsequently yield any of the following notifications.
Notification Event Type | Description |
---|---|
MV_EVENTTYPE_CHANNELDATAREADABLE |
Issued to the host channel’s notification center when data has been received through the channel. Call mvMqttReadConnectResponse() to the ultimate outcome of the request |
Description
Only one connect is permitted per channel life cycle. To connect again, create a new channel.
For each channel, only one request can be issued. If Microvisor returns MV_STATUS_PARAMETERFAULT
, MV_STATUS_INVALIDHANDLE
, MV_STATUS_INVALIDBUFFERSIZE
, MV_STATUS_INVALIDCERTIFICATION
, MV_STATUS_TOOMANYELEMENTS
, MV_STATUS_REQUIREDELEMENTMISSING
or MV_STATUS_CHANNELCLOSED
, then the request will not have been issued, and you can reconfigure the request and try again. Any other value indicates that the request has been sent, and any attempt to issue it again, or to use the channel for a fresh request, will result in MV_STATUS_REQUESTALREADYSENT
being returned. Instead, send any subsequent MQTT request through a new channel. You can re-use the channel definition structure and buffers if you wish. Be sure to close the channel used for a given MQTT request once you have received the response or a failure notification.
The call’s request
parameter takes a pointer to an MvMqttConnectRequest
structure:
struct MvMqttConnectRequest {
enum MvMqttProtocolVersion protocol_version,
struct mvSizedString host,
uint16_t port,
struct mvSizedString clientid,
struct MvMqttAuthentication *authentication,
struct MvTlsCredentials *tls_credentials,
uint32_t keepalive,
uint8_t clean_start,
struct MvMqttWill *will
}
This structure’s properties are:
protocol_version
— The MQTT protocol version used by the broker. Supported versions are 3.1.1 and 5, chosen using the constantsMV_MQTTPROTOCOLVERSION_V3_1_1
andMV_MQTTPROTOCOLVERSION_V5
respectively.host
— A data structure comprising the broker’s URL as bytes, which need not benul
-terminated, and the number of bytes.port
— The broker’s port number.clientid
— A data structure comprising a client ID as bytes, which need not benul
-terminated, and the number of bytes. The client ID must be unique to the broker. MQTT 3.1.1 allows you to send a zero-byte client ID, if you don’t need a state to be held by the broker. In this case, theclean_start
flag must be set to a non-zero value, or the broker will reject the connection.authentication
— A pointer to anMvMqttAuthentication
structure, see below.tls_credentials
— A pointer to anMvMqttTlsCredentials
structure, see below.keepalive
— The keepalive interval in seconds.clean_start
— A flag which can be set to inform the broker you do not wish to establish a persistent session, i.e., the broker should completely forget about the client when the connection closes. Pass zero for a persistent session: the broker will retain any subscriptions for the client plus all missed messages for the client that were subscribed with an MQTT quality of service (QoS) level 1 or 2.will
— An optional MQTT will for the connection, see below.
Authentication
The connection request’s authentication
property takes a pointer to an MvMqttAuthentication
structure:
struct MvMqttAuthentication {
enum MvMqttAuthenticationMethod method,
struct MvMqttUsernamePassword *username_password
}
The value of method
is MV_MQTTAUTHENTICATIONMETHOD_NONE
or MV_MQTTAUTHENTICATIONMETHOD_USERNAMEPASSWORD
. If you are using the former, there is no need to include the username_password
property.
To add a username and password to the MvMqttAuthentication
structure, complete and pass in the following data:
struct MvMqttUsernamePassword {
struct mvSizedString username,
struct mvSizedString password
}
Provide each credential as a data structure that comprises the name of the requested item and its length.
TLS credentials
To provide TLS credentials to allow your application’s access to a named broker to be authenticated, include a pointer to the following structure as the value of your MvMqttConnectRequest
’s tls_credentials
property:
struct MvTlsCredentials {
struct MvTlsCertificateChain cacert,
struct MvOwnTlsCertificateChain clientcert
}
This structure’s properties are themselves structures:
struct MvTlsCertificateChain {
uint32_t num_certs,
struct MvSizedString *certs
}
struct MvOwnTlsCertificateChain {
struct TlsCertificateChain chain,
struct MvSizedString key
}
Each MvTlsCertificateChain
structure comprises an array of certificates in binary form and the number of certificates in the array. Each MvOwnTlsCertificateChain
contains both a chain of certificates and an encryption key (RSA only).
Provide the certificate as a data structure incorporating a pointer to the bytes and the number of bytes.
Certificates and keys must be provided as DER (Distinguished Encoding Rules) binary data. Keys must be provided in PKCS#8 format. Convert to these format from commonplace .pem
files as follows:
openssl x509 -in cert.pem -inform pem -out cert.der -outform der
For keys:
openssl pkcs8 -topk8 -in key.pem -inform pem -out key.der -outform der -nocrypt
Wills
An MQTT client may specify a will message when it connects to a broker. A will is a normal MQTT message which the broker stores. If the broker subsequently detects that the client has disconnected unexpectedly, it sends the will to all other clients that have subscribed to its will messages topic (see your broker’s documentation for this topic’s name). If the client disconnects gracefully, the broker discards the will.
You specify your will when you configure your connection to the broker. Include it in the MvMqttConnectRequest
structure as its will
property. The data is provided as an MvMqttWill
structure:
struct MvMqttWill {
struct MvSizedString topic,
struct MvSizedString payload,
uint32_t qos,
uint8_t retain
}
This structure’s properties are:
topic
— A data structure comprising the topic name as bytes, which need not benul
-terminated, and the number of bytes.payload
— A data structure comprising the message bytes, which need not benul
-terminated, and the number of bytes.qos
— The required MQTT Quality of Service setting: 0 (at most once) or 1 (at least once). Microvisor does not support QoS level 2.retain
— The message retention flag. Set to a non-zero value to instruct the broker to set the message as its topic’s retained message. Each topic can have a single retained message, which will be automatically sent to every new subscriber.
mvMqttReadConnectResponse()
Read the MQTT connection attempt response
Declaration
extern enum MvStatus mvMqttReadConnectResponse(MvChannelHandle handle,
struct MvMqttConnectResponse *response);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
response |
A pointer to memory into which Microvisor will write an MvMqttConnectResponse structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
response does not reference memory accessible to the application |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
Description
Your application should call mvMqttGetNextReadableDataType()
when the MQTT channel receives a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
. The returned data type will MV_MQTTREADABLEDATATYPE_CONNECTRESPONSE
if the data is the response to an attempt to connect to an MQTT broker. In this case, call mvMqttReadConnectResponse()
. Microvisor will write an MvMqttConnectResponse
record which your code can then parse:
struct MvMqttConnectResponse {
enum MvMqttConnectStatus status,
uint32_t reason_code
}
Request states
The 32-bit integer in status
will be one of the following values:
Constant | Connection Only | Description |
---|---|---|
MV_MQTTREQUESTSTATE_REQUESTCOMPLETED |
No | The request was completed successfully |
MV_MQTTREQUESTSTATE_INVALIDPARAMETERS |
No | Invalid connection parameters were specified |
MV_MQTTREQUESTSTATE_ALREADYCONNECTED |
No | An MQTT connection has already been attempted on this channel |
MV_MQTTREQUESTSTATE_NOTCONNECTED |
No | The MQTT connection was not established or failed at the point the request was made |
MV_MQTTREQUESTSTATE_NXDOMAIN |
Yes | DNS resolution of the broker URL failed |
MV_MQTTREQUESTSTATE_UNKNOWNCA |
Yes | An unknown certificate authority was specified |
MV_MQTTREQUESTSTATE_CERTIFICATEEXPIRED |
Yes | A certificate is out of date |
MV_MQTTREQUESTSTATE_SOCKETERROR |
Yes | A socket error was encountered on connect |
MV_MQTTREQUESTSTATE_CONNECTIONCIRCUITBREAKER |
No | The Twilio Cloud dropped the connection because incoming messages were rate limited |
The value of reason_code
is supplied by the broker and will be a standard MQTT value. Zero indicates no error.
Example
void mqtt_handle_connect_response_event() {
struct MvMqttConnectResponse response = {};
enum MvStatus status = mvMqttReadConnectResponse(mqtt_channel, &response);
if (status != MV_STATUS_OKAY) {
server_error("mvMqttReadConnectResponse returned 0x%02x\n", (int) status);
pushMessage(OnBrokerConnectFailed);
return;
}
server_log("Connect response status is 0x%02X", response.status);
if (response.status != MV_MQTTCONNECTSTATUS_CONNACKRECEIVED) {
// Not the status we expect
pushMessage(OnBrokerConnectFailed);
return;
}
server_log("Connect response reason_code is 0x%02X", (int)response.reason_code);
if (response.reason_code != 0x00) {
// Not the reason code we expect
pushMessage(OnBrokerConnectFailed);
return;
}
server_log("MQTT broker connection established");
pushMessage(OnBrokerConnected);
}
mvMqttRequestSubscribe()
Subscribe to one or more MQTT topics
Declaration
extern enum MvStatus mvMqttRequestSubscribe(MvChannelHandle handle,
struct MvMqttSubscribeRequest *request);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
subscriptions |
A pointer to an MvMqttSubscribeRequest structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
request does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
A pointer within request is illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_RATELIMITED |
There are too many MQTT requests currently in flight |
MV_STATUS_TOOMANYELEMENTS |
There are too many (>8) topics specified in the request |
Notifications
This call, if successful, may subsequently yield any of the following notifications.
Notification Event Type | Description |
---|---|
MV_EVENTTYPE_CHANNELDATAREADABLE |
Issued to the host channel’s notification center when data has been received through the channel. Call mvMqttReadSubscribeResponse() to the ultimate outcome of the request |
Description
Only one subscription request will be serviced at a time. Subscribe to multiple topics with a single subscription request. However, you can current subscribe to no more than eight topics with one call.
struct MvMqttSubscribeRequest {
uint32_t correlation_id,
struct MvMqttSubscription *subscriptions,
uint32_t num_subscriptions
}
The value of correlation_id
is an application-defined ID that will be included in the response to enable you to match the response to the source request. Requests may be fulfilled in a non-deterministic order.
The subscriptions
property takes a pointer to an array of MvMqttSubscription
structures. Set the number of array elements as the num_subscriptions
property. Each element is a structure:
struct MvMqttSubscription {
struct MvSizedString topic,
uint32_t desired_qos,
uint32_t nl,
uint32_t rap,
uint32_t rh
}
This structure’s properties are:
topic
— A data structure comprising the topic name as bytes, which need not benul
-terminated, and the number of bytes.desired_qos
— The MQTT quality of service (QoS) setting (0 or 1; Microvisor does not support QoS level 2) you’d like to be applied.nl
— Your preferred MQTT no-local flag to request. MQTT v5 only.rap
— Your preferred MQTT retain-as-published flag. MQTT v5 only.rh
— Your preferred MQTT retain setting. MQTT v5 only.
mvMqttReadSubscribeResponse()
Read the response to an MQTT subscription request
Declaration
extern enum MvStatus mvMqttReadSubscribeResponse(MvChannelHandle handle,
struct MvMqttSubscribeResponse *response);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
response |
A pointer to an MvMqttSubscribeResponse structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
response does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
One or more of the pointers within response are illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_INVALIDBUFFERSIZE |
Response data doesn't fit into the buffer provided for response codes |
Description
Your application should call mvMqttGetNextReadableDataType()
when the MQTT channel receives a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
. If the returned data type is MV_MQTTREADABLEDATATYPE_SUBSCRIBERESPONSE
, call mvMqttReadSubscribeResponse()
. Pass in an MvMqttSubscribeResponse
record which Microvisor will use to write back response data:
struct MvMqttSubscribeResponse {
enum MvMqttRequestState *request_state,
uint32_t *correlation_id,
uint32_t *reason_codes,
uint32_t reason_codes_size,
uint32_t *reason_codes_len
}
The integer written to the memory referenced by request_state
will be one of the values listed above.
The value of correlation_id
is a pointer to the ID specified in the subscription request, allowing you to map responses to source requests. Requests may be fulfilled in a non-deterministic order.
The value is reason_codes
is a pointer to an array of 32-bit values. The maximum size of the array is placed in reason_codes_size
, and must match the maximum specified in the subscription request (as num_subscriptions
). This will be the same as or larger than the number of codes written, which Microvisor will is write into the memory referenced by reason_codes_len
.
mvMqttRequestUnsubscribe()
Unsubscribe from one or more MQTT topics
Declaration
extern enum MvStatus mvMqttRequestUnsubscribe(MvChannelHandle handle,
struct MqttUnsubscribeRequest *request);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
request |
A pointer to an MqttUnsubscribeRequest structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
request does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
One or more of the pointers within request are illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_RATELIMITED |
There are too many MQTT requests currently in flight |
MV_STATUS_TOOMANYELEMENTS |
There are too many (>8) topics specified in the request |
Notifications
This call, if successful, may subsequently yield any of the following notifications.
Notification Event Type | Description |
---|---|
MV_EVENTTYPE_CHANNELDATAREADABLE |
Issued to the host channel’s notification center when data has been received through the channel. Call mvMqttReadUnsubscribeResponse() to the ultimate outcome of the request |
Description
Only one unsubscribe request will be serviced at a time. Unsubscribe to multiple topics with a single unsubscribe request. However, you can current subscribe to no more than eight topics with one call.
struct MvMqttUnsubscribeRequest {
uint32_t correlation_id,
struct MvSizedString *topics,
uint32_t num_topics
}
The value of correlation_id
is an application-defined ID that will be included in the response to enable you to match the response to the source request. Requests may be fulfilled in a non-deterministic order.
The topics
property takes a pointer to an array of data structures. Each element comprises the name of a topic as bytes and the number of bytes. The number of elements in the array should be placed in num_topics
.
mvMqttReadUnsubscribeResponse()
Read the response to an MQTT unsubscribe request
Declaration
extern enum MvStatus mvMqttReadUnsubscribeResponse(MvChannelHandle handle,
struct MvMqttUnsubscriveResponse *response);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
response |
A pointer to an MvMqttUnsubscribeResponse structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
response does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
One or more of the pointers within response are illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
Description
Your application should call mvMqttGetNextReadableDataType()
when the MQTT channel receives a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
. If the returned data type is MV_MQTTREADABLEDATATYPE_UNSUBSCRIBERESPONSE
, call mvMqttReadUnsubscribeResponse()
. Pass in an MvMqttUnsubscribeResponse
record which Microvisor will use to write back the outcome(s) of the operation:
struct MvMqttUnsubscribeResponse {
enum MQTTRequestState *request_state,
uint32_t *correlation_id,
uint32_t *reason_codes,
uint32_t *reason_codes_len,
uint32_t reason_codes_size
}
The integer written to the memory referenced by request_state
will be one of the values listed above.
The value of correlation_id
is a pointer to the ID specified in the subscription request, allowing you to map responses to source requests. Requests may be fulfilled in a non-deterministic order.
The value of reason_codes
is a pointer to a buffer into which Microvisor will write a 32-bit MQTT reason code for each of the topics specified in the original unsubscribe request. Specify the size of the buffer in bytes as reason_codes_size
; it must match the maximum specified in the unsubscribe request (as num_topics
). This will be the same as or larger than the number of codes written, which Microvisor will write to the memory referenced by reason_codes_len
.
mvMqttRequestPublish()
Post a message to an MQTT topic
Declaration
extern enum MvStatus mvMqttRequestPublish(MvChannelHandle handle,
struct MvMqttPublishRequest *request);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
request |
A pointer to an MvMqttPublishRequest structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
request does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
One or more of the pointers within request are illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_RATELIMITED |
There are too many MQTT requests currently in flight |
Notifications
This call, if successful, may subsequently yield any of the following notifications.
Notification Event Type | Description |
---|---|
MV_EVENTTYPE_CHANNELDATAREADABLE |
Issued to the host channel’s notification center when data has been received through the channel. Call mvMqttReadPublishResponse() to the ultimate outcome of the request |
Description
The call’s request
parameter takes a pointer to an MvMqttPublishRequest
structure:
struct MvMqttPublishRequest {
uint32_t correlation_id,
struct MvSizedString topic,
struct MvSizedString payload,
uint32_t desired_qos,
uint32_t retain
}
This structure’s properties are:
correlation_id
— A 32-bit integer you can use to match a subsequent response to this request. Requests may be fulfilled in a non-deterministic order.topic
— A data structure comprising the topic name as bytes, which need not benul
-terminated, and the number of bytes.payload
— A data structure comprising the message bytes, which need not benul
-terminated, and the number of bytes.desired_qos
— The MQTT quality of service setting (0, 1 or 2) you’d like to be applied.retain
— The MQTT retention behavior you would like the broker to apply to the message. Set to a non-zero value to instruct the broker to set the message as its topic’s retained message. Each topic can have only one retained message, which will be automatically sent to every new subscriber.
mvMqttReadPublishResponse()
Read an MQTT publish-to-topic response
Declaration
extern enum MvStatus mvMqttReadPublishResponse(MvChannelHandle handle,
const struct MvMqttPublishResponse *response);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
response |
A pointer to an MvMqttPublishResponse structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
response does not reference memory accessible to the application |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
Description
Your application should call mvMqttGetNextReadableDataType()
when the MQTT channel receives a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
. If the returned data type is MV_MQTTREADABLEDATATYPE_PUBLISHRESPONSE
, call mvMqttReadPublishResponse()
. Microvisor will use the MvMqttPublishResponse
record which your code provides to write out publication outcome information:
struct MvMqttPublishResponse {
enum MQTTRequestState request_state,
uint32_t correlation_id,
uint32_t reason_code
}
The integer written to request_state
will be one of the values listed above.
The value of correlation_id
is the ID specified in the subscription request, allowing you to map responses to source requests. Requests may be fulfilled in a non-deterministic order.
The value written to reason_code
is an MQTT status value.
mvMqttReceiveMessage()
Read a received MQTT message
Declaration
extern enum MvStatus mvMqttReceiveMessage(MvChannelHandle handle,
struct MvMqttMessage *message);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
message |
A pointer to an MvMqttMessage structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
message does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
One or more of the pointers within message are illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_INVALIDBUFFERSIZE |
Some response elements don’t fit into the buffer provided |
Description
Your application should call mvMqttGetNextReadableDataType()
when the MQTT channel receives a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
. If the returned data type is MV_MQTTREADABLEDATATYPE_MESSAGERECEIVED
, the device has received a message from the broker. Call mvMqttReceiveMessage()
. Microvisor will use the MvMqttMessage
record which your code supplies to write back message data:
struct MvMqttMessage {
uint32_t *correlation_id,
struct MvSizedStringBuffer topic,
struct MvSizedStringBuffer payload,
uint32_t *qos,
uint8_t *retain
}
This structure’s properties are:
correlation_id
— A pointer to memory where Microvisor will write a 32-bit unsigned integer which will match the ID specified in the subscription request, allowing you to map responses to source requests. Requests may be fulfilled in a non-deterministic order.topic
— A pointer to a buffer structure which Microvisor will use to write back the topic name.payload
— A pointer to a buffer structure which Microvisor will use to write back the message payload.qos
— A pointer to the MQTT quality of service setting applied. You should acknowledge receipt of messages with QoS values 1 or 2 by callingmvMqttAcknowledgeMessage()
.retain
— A pointer to an MQTT flag indicating whether the message has been retained, not deleted by the broker.
mvMqttReceiveLostMessageInfo()
Read information about a lost MQTT message
Declaration
extern enum MvStatus mvMqttReceiveLostMessageInfo(MvChannelHandle handle,
struct MvMqttLostMessageInfo *info);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
info |
A pointer to an MvMqttLostMessageInfo structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
info does not reference memory accessible to the application |
MV_STATUS_LATEFAULT |
One or more of the pointers within info are illegal. The channel is no longer usable and should be closed |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_INVALIDBUFFERSIZE |
Some response elements don’t fit into the buffer provided |
Description
Your application should call mvMqttGetNextReadableDataType()
when the MQTT channel receives a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
. If the returned data type is MV_MQTTREADABLEDATATYPE_MESSAGELOST
, call mvMqttReceiveLostMessageInfo()
. Microvisor will use the MvMqttLostMessageInfo
record which your code provides to write back information about the lost message:
struct MvMqttMessage {
enum MvMqttLostMessageReason *reason,
uint32_t *correlation_id,
struct MvSizedStringBuffer topic,
uint32_t *message_len
}
The integer placed into memory referenced by reason
will beMV_MQTTLOSTMESSAGEREASON_DEVICERECEIVEBUFFERTOOSMALL
if the buffer allocated by the application was insufficient to receive the message. Consider increasing the size of the channel’s RX buffer.
This structure’s other properties are:
correlation_id
— A pointer to memory where Microvisor will write a 32-bit unsigned integer which will match the ID specified in the subscription request, allowing you to map responses to source requests. Requests may be fulfilled in a non-deterministic order.topic
— A pointer to a buffer structure which Microvisor will use to write back the topic name.message_len
— A pointer to the lost message’s size in bytes.
mvMqttAcknowledgeMessage()
Acknowledge the receipt of an MQTT message that arrived with QoS 1 or 2.
Declaration
extern enum MvStatus mvMqttAcknowledgeMessage(MvChannelHandle handle,
uint32_t correlation_id);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
correlation_id |
The ID of the message whose receipt is to be acknowledged |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_RATELIMITED |
There are too many MQTT requests currently in flight |
Description
The receipt of messages delivered with an MQTT quality of service (QoS) value of 1 or 2 should be manually acknowledged, and you should call mvMqttAcknowledgeMessage()
to do so.
mvMqttRequestDisconnect()
Disconnect from the channel’s current MQTT broker
Declaration
extern enum MvStatus mvMqttRequestDisconnect(MvChannelHandle handle);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
MV_STATUS_RATELIMITED |
There are too many MQTT requests currently in flight |
Notifications
This call, if successful, may subsequently yield any of the following notifications.
Notification Event Type | Description |
---|---|
MV_EVENTTYPE_CHANNELDATAREADABLE |
Issued to the host channel’s notification center when data has been received through the channel. Call mvMqttReadDisconnectResponse() to the ultimate outcome of the request |
Description
Call mvMqttRequestDisconnect()
to request disconnection from the broker you’re currently connected to through the channel. This will result in the asynchronous posting of a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
to your channel notification center. On receipt of this notification, you should call mvMqttGetNextReadableDataType()
and check the returned data type for MV_MQTTREADABLEDATATYPE_DISCONNECTRESPONSE
. If the values match, call mvMqttReadDisconnectResponse()
.
mvMqttReadDisconnectResponse()
Read an MQTT disconnect response
Declaration
extern enum MvStatus mvMqttReadDisconnectResponse(MvChannelHandle handle,
struct MvMqttDisconnectResponse *response);
Parameters
Parameter | Description |
---|---|
handle |
The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
response |
A pointer to an MvMqttDisconnectResponse structure |
Possible errors
Error Value | Description |
---|---|
MV_STATUS_PARAMETERFAULT |
response does not reference memory accessible to the application |
MV_STATUS_INVALIDHANDLE |
handle does not reference a valid channel of type MV_CHANNELTYPE_MQTT |
MV_STATUS_CHANNELCLOSED |
The specified channel has already been closed |
Description
Your application should call mvMqttGetNextReadableDataType()
when the MQTT channel receives a notification of type MV_EVENTTYPE_CHANNELDATAREADABLE
. If the returned data type is MV_MQTTREADABLEDATATYPE_DISCONNECTRESPONSE
, call mvMqttReadDisconnectResponse()
. Pass in an MvMqttDisconnectResponse
record which Microvisor will use to write back response data:
struct MvMqttDisconnectResponse {
enum MQTTRequestState request_state,
uint32_t disconnect_code
}
The integer written to request_state
will be one of the values listed above.
As the name of the second property indicates, the response you read may not have been initiated by a call to mvMqttRequestDisconnect()
. This status will also be generated if the connection to the broker was lost unexpectedly. The value of disconnect_code
is a standard MQTT disconnection status value.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.