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()
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
_10extern enum MvStatus mvMqttGetNextReadableDataType(MvChannelHandle handle,_10 enum 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.
_39void mqtt_handle_readable_event() {_39_39 enum MvMqttReadableDataType readableDataType;_39 if (mvMqttGetNextReadableDataType(mqtt_channel, &readableDataType) != MV_STATUS_OKAY) return;_39 server_log("MQTT Event: data received of type %02X", readableDataType);_39_39 switch (readableDataType) {_39 case MV_MQTTREADABLEDATATYPE_CONNECTRESPONSE: _39 // A response to a connect request is available._39 pushMessage(OnMQTTEventConnectResponse);_39 break;_39 case MV_MQTTREADABLEDATATYPE_MESSAGERECEIVED:_39 // A message is ready for consumption._39 pushMessage(OnMQTTEventMessageReceived);_39 break;_39 case MV_MQTTREADABLEDATATYPE_MESSAGELOST:_39 // A message was lost, details are available._39 pushMessage(OnMQTTEventMessageLost);_39 break;_39 case MV_MQTTREADABLEDATATYPE_SUBSCRIBERESPONSE:_39 // A response to a subscribe request is available._39 pushMessage(OnMQTTEventSubscribeResponse);_39 break;_39 case MV_MQTTREADABLEDATATYPE_UNSUBSCRIBERESPONSE:_39 // A response to an unsubscribe request is available._39 pushMessage(OnMQTTEventUnsubscribeResponse);_39 break;_39 case MV_MQTTREADABLEDATATYPE_PUBLISHRESPONSE:_39 // A response to a publish request is available._39 pushMessage(OnMQTTEventPublishResponse);_39 break;_39 case MV_MQTTREADABLEDATATYPE_DISCONNECTRESPONSE: _39 // A response to a disconnect operation is available._39 pushMessage(OnMQTTEventDisconnectResponse);_39 break;_39 default:_39 break;_39 }_39}
Initiate a connection to an MQTT broker
_10extern enum MvStatus mvMqttRequestConnect(MvChannelHandle handle,_10 struct 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:
_11struct MvMqttConnectRequest {_11 enum MvMqttProtocolVersion protocol_version,_11 struct mvSizedString host,_11 uint16_t port,_11 struct mvSizedString clientid,_11 struct MvMqttAuthentication *authentication,_11 struct MvTlsCredentials *tls_credentials,_11 uint32_t keepalive,_11 uint8_t clean_start,_11 struct MvMqttWill *will_11}
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:
_10struct MvMqttAuthentication {_10 enum MvMqttAuthenticationMethod method,_10 struct MvMqttUsernamePassword *username_password_10}
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:
_10struct MvMqttUsernamePassword {_10 struct mvSizedString username,_10 struct mvSizedString password_10}
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:
_10struct MvTlsCredentials {_10 struct MvTlsCertificateChain cacert,_10 struct MvOwnTlsCertificateChain clientcert_10}
This structure's properties are themselves structures:
_10struct MvTlsCertificateChain {_10 uint32_t num_certs,_10 struct MvSizedString *certs_10}
_10struct MvOwnTlsCertificateChain {_10 struct TlsCertificateChain chain,_10 struct MvSizedString key_10}
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:
_10openssl x509 -in cert.pem -inform pem -out cert.der -outform der
For keys:
_10openssl 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:
_10struct MvMqttWill {_10 struct MvSizedString topic,_10 struct MvSizedString payload,_10 uint32_t qos,_10 uint8_t retain_10}
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
_10extern enum MvStatus mvMqttReadConnectResponse(MvChannelHandle handle,_10 struct 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:
_10struct MvMqttConnectResponse {_10 enum MvMqttConnectStatus status,_10 uint32_t reason_code_10}
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.
_28void mqtt_handle_connect_response_event() {_28_28 struct MvMqttConnectResponse response = {};_28_28 enum MvStatus status = mvMqttReadConnectResponse(mqtt_channel, &response);_28 if (status != MV_STATUS_OKAY) {_28 server_error("mvMqttReadConnectResponse returned 0x%02x\n", (int) status);_28 pushMessage(OnBrokerConnectFailed);_28 return;_28 }_28_28 server_log("Connect response status is 0x%02X", response.status);_28 if (response.status != MV_MQTTCONNECTSTATUS_CONNACKRECEIVED) {_28 // Not the status we expect_28 pushMessage(OnBrokerConnectFailed);_28 return;_28 }_28_28 server_log("Connect response reason_code is 0x%02X", (int)response.reason_code);_28 if (response.reason_code != 0x00) {_28 // Not the reason code we expect_28 pushMessage(OnBrokerConnectFailed);_28 return;_28 }_28_28 server_log("MQTT broker connection established");_28 pushMessage(OnBrokerConnected);_28}
Subscribe to one or more MQTT topics
_10extern enum MvStatus mvMqttRequestSubscribe(MvChannelHandle handle,_10 struct 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.
_10struct MvMqttSubscribeRequest {_10 uint32_t correlation_id,_10 struct MvMqttSubscription *subscriptions,_10 uint32_t num_subscriptions_10}
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:
_10struct MvMqttSubscription {_10 struct MvSizedString topic,_10 uint32_t desired_qos,_10 uint32_t nl,_10 uint32_t rap,_10 uint32_t rh_10}
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
_10extern enum MvStatus mvMqttReadSubscribeResponse(MvChannelHandle handle,_10 struct 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:
_10struct MvMqttSubscribeResponse {_10 enum MvMqttRequestState *request_state,_10 uint32_t *correlation_id,_10 uint32_t *reason_codes,_10 uint32_t reason_codes_size,_10 uint32_t *reason_codes_len_10}
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
_10extern enum MvStatus mvMqttRequestUnsubscribe(MvChannelHandle handle,_10 struct 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.
_10struct MvMqttUnsubscribeRequest {_10 uint32_t correlation_id,_10 struct MvSizedString *topics,_10 uint32_t num_topics_10}
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
_10extern enum MvStatus mvMqttReadUnsubscribeResponse(MvChannelHandle handle,_10 struct 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:
_10struct MvMqttUnsubscribeResponse {_10 enum MQTTRequestState *request_state,_10 uint32_t *correlation_id,_10 uint32_t *reason_codes,_10 uint32_t *reason_codes_len,_10 uint32_t reason_codes_size_10}
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
_10extern enum MvStatus mvMqttRequestPublish(MvChannelHandle handle,_10 struct 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:
_10struct MvMqttPublishRequest {_10 uint32_t correlation_id,_10 struct MvSizedString topic,_10 struct MvSizedString payload,_10 uint32_t desired_qos,_10 uint32_t retain_10}
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
_10extern enum MvStatus mvMqttReadPublishResponse(MvChannelHandle handle,_10 const 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:
_10struct MvMqttPublishResponse {_10 enum MQTTRequestState request_state,_10 uint32_t correlation_id,_10 uint32_t reason_code_10}
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
_10extern enum MvStatus mvMqttReceiveMessage(MvChannelHandle handle,_10 struct 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:
_10struct MvMqttMessage {_10 uint32_t *correlation_id,_10 struct MvSizedStringBuffer topic,_10 struct MvSizedStringBuffer payload,_10 uint32_t *qos,_10 uint8_t *retain_10}
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
_10extern enum MvStatus mvMqttReceiveLostMessageInfo(MvChannelHandle handle,_10 struct 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:
_10struct MvMqttMessage {_10 enum MvMqttLostMessageReason *reason,_10 uint32_t *correlation_id,_10 struct MvSizedStringBuffer topic,_10 uint32_t *message_len_10}
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.
_10extern enum MvStatus mvMqttAcknowledgeMessage(MvChannelHandle handle,_10 uint32_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
_10extern 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
_10extern enum MvStatus mvMqttReadDisconnectResponse(MvChannelHandle handle,_10 struct 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:
_10struct MvMqttDisconnectResponse {_10 enum MQTTRequestState request_state,_10 uint32_t disconnect_code_10}
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.