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()
This document assumes you are familiar with the MQTT specification and how the protocol operates.
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.
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.
Determine what type of MQTT data a channel last received
1extern enum MvStatus mvMqttGetNextReadableDataType(MvChannelHandle handle,2enum MvMqttReadableDataType *type);
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 |
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 |
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 |
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.
1void mqtt_handle_readable_event() {23enum MvMqttReadableDataType readableDataType;4if (mvMqttGetNextReadableDataType(mqtt_channel, &readableDataType) != MV_STATUS_OKAY) return;5server_log("MQTT Event: data received of type %02X", readableDataType);67switch (readableDataType) {8case MV_MQTTREADABLEDATATYPE_CONNECTRESPONSE:9// A response to a connect request is available.10pushMessage(OnMQTTEventConnectResponse);11break;12case MV_MQTTREADABLEDATATYPE_MESSAGERECEIVED:13// A message is ready for consumption.14pushMessage(OnMQTTEventMessageReceived);15break;16case MV_MQTTREADABLEDATATYPE_MESSAGELOST:17// A message was lost, details are available.18pushMessage(OnMQTTEventMessageLost);19break;20case MV_MQTTREADABLEDATATYPE_SUBSCRIBERESPONSE:21// A response to a subscribe request is available.22pushMessage(OnMQTTEventSubscribeResponse);23break;24case MV_MQTTREADABLEDATATYPE_UNSUBSCRIBERESPONSE:25// A response to an unsubscribe request is available.26pushMessage(OnMQTTEventUnsubscribeResponse);27break;28case MV_MQTTREADABLEDATATYPE_PUBLISHRESPONSE:29// A response to a publish request is available.30pushMessage(OnMQTTEventPublishResponse);31break;32case MV_MQTTREADABLEDATATYPE_DISCONNECTRESPONSE:33// A response to a disconnect operation is available.34pushMessage(OnMQTTEventDisconnectResponse);35break;36default:37break;38}39}
Initiate a connection to an MQTT broker
1extern enum MvStatus mvMqttRequestConnect(MvChannelHandle handle,2struct MvMqttConnectRequest *request);
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 |
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 |
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 |
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:
1struct MvMqttConnectRequest {2enum MvMqttProtocolVersion protocol_version,3struct mvSizedString host,4uint16_t port,5struct mvSizedString clientid,6struct MvMqttAuthentication *authentication,7struct MvTlsCredentials *tls_credentials,8uint32_t keepalive,9uint8_t clean_start,10struct MvMqttWill *will11}
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 constants MV_MQTTPROTOCOLVERSION_V3_1_1
and MV_MQTTPROTOCOLVERSION_V5
respectively.host
— A data structure comprising the broker's URL as bytes, which need not be nul
-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 be nul
-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, the clean_start
flag must be set to a non-zero value, or the broker will reject the connection.authentication
— A pointer to an MvMqttAuthentication
structure, see below.tls_credentials
— A pointer to an MvMqttTlsCredentials
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.The connection request's authentication
property takes a pointer to an MvMqttAuthentication
structure:
1struct MvMqttAuthentication {2enum MvMqttAuthenticationMethod method,3struct MvMqttUsernamePassword *username_password4}
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:
1struct MvMqttUsernamePassword {2struct mvSizedString username,3struct mvSizedString password4}
Provide each credential as a data structure that comprises the name of the requested item and its length.
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:
1struct MvTlsCredentials {2struct MvTlsCertificateChain cacert,3struct MvOwnTlsCertificateChain clientcert4}
This structure's properties are themselves structures:
1struct MvTlsCertificateChain {2uint32_t num_certs,3struct MvSizedString *certs4}
1struct MvOwnTlsCertificateChain {2struct TlsCertificateChain chain,3struct MvSizedString key4}
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
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:
1struct MvMqttWill {2struct MvSizedString topic,3struct MvSizedString payload,4uint32_t qos,5uint8_t retain6}
This structure's properties are:
topic
— A data structure comprising the topic name as bytes, which need not be nul
-terminated, and the number of bytes.payload
— A data structure comprising the message bytes, which need not be nul
-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.Read the MQTT connection attempt response
1extern enum MvStatus mvMqttReadConnectResponse(MvChannelHandle handle,2struct MvMqttConnectResponse *response);
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 |
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 |
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:
1struct MvMqttConnectResponse {2enum MvMqttConnectStatus status,3uint32_t reason_code4}
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 Microvisor 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.
1void mqtt_handle_connect_response_event() {23struct MvMqttConnectResponse response = {};45enum MvStatus status = mvMqttReadConnectResponse(mqtt_channel, &response);6if (status != MV_STATUS_OKAY) {7server_error("mvMqttReadConnectResponse returned 0x%02x\n", (int) status);8pushMessage(OnBrokerConnectFailed);9return;10}1112server_log("Connect response status is 0x%02X", response.status);13if (response.status != MV_MQTTCONNECTSTATUS_CONNACKRECEIVED) {14// Not the status we expect15pushMessage(OnBrokerConnectFailed);16return;17}1819server_log("Connect response reason_code is 0x%02X", (int)response.reason_code);20if (response.reason_code != 0x00) {21// Not the reason code we expect22pushMessage(OnBrokerConnectFailed);23return;24}2526server_log("MQTT broker connection established");27pushMessage(OnBrokerConnected);28}
Subscribe to one or more MQTT topics
1extern enum MvStatus mvMqttRequestSubscribe(MvChannelHandle handle,2struct MvMqttSubscribeRequest *request);
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 |
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 |
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 |
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.
1struct MvMqttSubscribeRequest {2uint32_t correlation_id,3struct MvMqttSubscription *subscriptions,4uint32_t num_subscriptions5}
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:
1struct MvMqttSubscription {2struct MvSizedString topic,3uint32_t desired_qos,4uint32_t nl,5uint32_t rap,6uint32_t rh7}
This structure's properties are:
topic
— A data structure comprising the topic name as bytes, which need not be nul
-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.Read the response to an MQTT subscription request
1extern enum MvStatus mvMqttReadSubscribeResponse(MvChannelHandle handle,2struct MvMqttSubscribeResponse *response);
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 |
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 |
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:
1struct MvMqttSubscribeResponse {2enum MvMqttRequestState *request_state,3uint32_t *correlation_id,4uint32_t *reason_codes,5uint32_t reason_codes_size,6uint32_t *reason_codes_len7}
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
.
Unsubscribe from one or more MQTT topics
1extern enum MvStatus mvMqttRequestUnsubscribe(MvChannelHandle handle,2struct MqttUnsubscribeRequest *request);
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 |
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 |
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 |
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.
1struct MvMqttUnsubscribeRequest {2uint32_t correlation_id,3struct MvSizedString *topics,4uint32_t num_topics5}
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
.
Read the response to an MQTT unsubscribe request
1extern enum MvStatus mvMqttReadUnsubscribeResponse(MvChannelHandle handle,2struct MvMqttUnsubscribeResponse *response);
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 |
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 |
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:
1struct MvMqttUnsubscribeResponse {2enum MQTTRequestState *request_state,3uint32_t *correlation_id,4uint32_t *reason_codes,5uint32_t *reason_codes_len,6uint32_t reason_codes_size7}
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
.
Post a message to an MQTT topic
1extern enum MvStatus mvMqttRequestPublish(MvChannelHandle handle,2struct MvMqttPublishRequest *request);
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 |
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 |
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 |
The call's request
parameter takes a pointer to an MvMqttPublishRequest
structure:
1struct MvMqttPublishRequest {2uint32_t correlation_id,3struct MvSizedString topic,4struct MvSizedString payload,5uint32_t desired_qos,6uint32_t retain7}
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 be nul
-terminated, and the number of bytes.payload
— A data structure comprising the message bytes, which need not be nul
-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.Read an MQTT publish-to-topic response
1extern enum MvStatus mvMqttReadPublishResponse(MvChannelHandle handle,2const struct MvMqttPublishResponse *response);
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 |
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 |
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:
1struct MvMqttPublishResponse {2enum MQTTRequestState request_state,3uint32_t correlation_id,4uint32_t reason_code5}
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.
Read a received MQTT message
1extern enum MvStatus mvMqttReceiveMessage(MvChannelHandle handle,2struct MvMqttMessage *message);
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 |
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 |
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:
1struct MvMqttMessage {2uint32_t *correlation_id,3struct MvSizedStringBuffer topic,4struct MvSizedStringBuffer payload,5uint32_t *qos,6uint8_t *retain7}
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 calling mvMqttAcknowledgeMessage()
.retain
— A pointer to an MQTT flag indicating whether the message has been retained, not deleted by the broker.Read information about a lost MQTT message
1extern enum MvStatus mvMqttReceiveLostMessageInfo(MvChannelHandle handle,2struct MvMqttLostMessageInfo *info);
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 |
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 |
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:
1struct MvMqttMessage {2enum MvMqttLostMessageReason *reason,3uint32_t *correlation_id,4struct MvSizedStringBuffer topic,5uint32_t *message_len6}
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.Acknowledge the receipt of an MQTT message that arrived with QoS 1 or 2.
1extern enum MvStatus mvMqttAcknowledgeMessage(MvChannelHandle handle,2uint32_t correlation_id);
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 |
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 |
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.
Disconnect from the channel's current MQTT broker
extern enum MvStatus mvMqttRequestDisconnect(MvChannelHandle handle);
Parameter | Description |
---|---|
handle | The handle of the channel over which to establish the connection. This must be a channel of type MV_CHANNELTYPE_MQTT |
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 |
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 |
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()
.
Read an MQTT disconnect response
1extern enum MvStatus mvMqttReadDisconnectResponse(MvChannelHandle handle,2struct MvMqttDisconnectResponse *response);
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 |
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 |
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:
1struct MvMqttDisconnectResponse {2enum MQTTRequestState request_state,3uint32_t disconnect_code4}
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.