![]() |
Paho Asynchronous MQTT C Client Library
|
Go to the source code of this file.
#define MQTTASYNC_SUCCESS 0 |
Return code: No error. Indicates successful completion of an MQTT client operation.
#define MQTTASYNC_FAILURE -1 |
Return code: A generic error code indicating the failure of an MQTT client operation.
#define MQTTASYNC_PERSISTENCE_ERROR -2 |
#define MQTTASYNC_DISCONNECTED -3 |
Return code: The client is disconnected.
#define MQTTASYNC_MAX_MESSAGES_INFLIGHT -4 |
Return code: The maximum number of messages allowed to be simultaneously in-flight has been reached.
#define MQTTASYNC_BAD_UTF8_STRING -5 |
Return code: An invalid UTF-8 string has been detected.
#define MQTTASYNC_NULL_PARAMETER -6 |
Return code: A NULL parameter has been supplied when this is invalid.
#define MQTTASYNC_TOPICNAME_TRUNCATED -7 |
Return code: The topic has been truncated (the topic string includes embedded NULL characters). String functions will not access the full topic. Use the topic length value to access the full topic.
#define MQTTASYNC_BAD_STRUCTURE -8 |
Return code: A structure parameter does not have the correct eyecatcher and version number.
#define MQTTASYNC_BAD_QOS -9 |
Return code: A qos parameter is not 0, 1 or 2
#define MQTTASYNC_NO_MORE_MSGIDS -10 |
Return code: All 65535 MQTT msgids are being used
#define MQTTAsync_message_initializer { {'M', 'Q', 'T', 'M'}, 0, 0, NULL, 0, 0, 0, 0 } |
#define MQTTAsync_responseOptions_initializer { {'M', 'Q', 'T', 'R'}, 0, NULL, NULL, 0, 0 } |
#define MQTTAsync_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, NULL, NULL, 0, 0 } |
#define MQTTAsync_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 0, NULL, NULL, NULL, NULL, NULL, 1 } |
#define MQTTAsync_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 2, 60, 1, 10, NULL, NULL, NULL, 30, 20, NULL, NULL, 0, NULL} |
#define MQTTAsync_disconnectOptions_initializer { {'M', 'Q', 'T', 'D'}, 0, 0, NULL, NULL, NULL } |
typedef void* MQTTAsync |
A handle representing an MQTT client. A valid client handle is available following a successful call to MQTTAsync_create().
typedef int MQTTAsync_token |
A value representing an MQTT message. A token is returned to the client application when a message is published. The token can then be used to check that the message was successfully delivered to its destination (see MQTTAsync_publish(), MQTTAsync_publishMessage(), MQTTAsync_deliveryComplete(), and MQTTAsync_getPendingTokens()).
typedef int MQTTAsync_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message) |
This is a callback function. The client application must provide an implementation of this function to enable asynchronous receipt of messages. The function is registered with the client library by passing it as an argument to MQTTAsync_setCallbacks(). It is called by the client library when a new message that matches a client subscription has been received from the server. This function is executed on a separate thread to the one on which the client application is running.
context | A pointer to the context value originally passed to MQTTAsync_setCallbacks(), which contains any application-specific context. |
topicName | The topic associated with the received message. |
topicLen | The length of the topic if there are one more NULL characters embedded in topicName, otherwise topicLen is 0. If topicLen is 0, the value returned by strlen(topicName) can be trusted. If topicLen is greater than 0, the full topic name can be retrieved by accessing topicName as a byte array of length topicLen. |
message | The MQTTAsync_message structure for the received message. This structure contains the message payload and attributes. |
typedef void MQTTAsync_deliveryComplete(void *context, MQTTAsync_token token) |
This is a callback function. The client application must provide an implementation of this function to enable asynchronous notification of delivery of messages to the server. The function is registered with the client library by passing it as an argument to MQTTAsync_setCallbacks(). It is called by the client library after the client application has published a message to the server. It indicates that the necessary handshaking and acknowledgements for the requested quality of service (see MQTTAsync_message.qos) have been completed. This function is executed on a separate thread to the one on which the client application is running.
context | A pointer to the context value originally passed to MQTTAsync_setCallbacks(), which contains any application-specific context. |
token | The MQTTAsync_token associated with the published message. Applications can check that all messages have been correctly published by matching the tokens returned from calls to MQTTAsync_send() and MQTTAsync_sendMessage() with the tokens passed to this callback. |
typedef void MQTTAsync_connectionLost(void *context, char *cause) |
This is a callback function. The client application must provide an implementation of this function to enable asynchronous notification of the loss of connection to the server. The function is registered with the client library by passing it as an argument to MQTTAsync_setCallbacks(). It is called by the client library if the client loses its connection to the server. The client application must take appropriate action, such as trying to reconnect or reporting the problem. This function is executed on a separate thread to the one on which the client application is running.
context | A pointer to the context value originally passed to MQTTAsync_setCallbacks(), which contains any application-specific context. |
cause | The reason for the disconnection. Currently, cause is always set to NULL. |
typedef void MQTTAsync_onSuccess(void *context, MQTTAsync_successData *response) |
This is a callback function. The client application must provide an implementation of this function to enable asynchronous notification of the successful completion of an API call. The function is registered with the client library by passing it as an argument in MQTTAsync_responseOptions.
context | A pointer to the context value originally passed to MQTTAsync_responseOptions, which contains any application-specific context. |
response | Any success data associated with the API completion. |
typedef void MQTTAsync_onFailure(void *context, MQTTAsync_failureData *response) |
This is a callback function. The client application must provide an implementation of this function to enable asynchronous notification of the unsuccessful completion of an API call. The function is registered with the client library by passing it as an argument in MQTTAsync_responseOptions.
context | A pointer to the context value originally passed to MQTTAsync_responseOptions, which contains any application-specific context. |
response | Any failure data associated with the API completion. |
typedef void MQTTAsync_traceCallback(enum MQTTASYNC_TRACE_LEVELS level, char *message) |
This is a callback function prototype which must be implemented if you want to receive trace information.
level | the trace level of the message returned |
meesage | the trace message. This is a pointer to a static buffer which will be overwritten on each call. You must copy the data if you want to keep it for later. |
int MQTTAsync_setCallbacks | ( | MQTTAsync | handle, |
void * | context, | ||
MQTTAsync_connectionLost * | cl, | ||
MQTTAsync_messageArrived * | ma, | ||
MQTTAsync_deliveryComplete * | dc | ||
) |
This function sets the global callback functions for a specific client. If your client application doesn't use a particular callback, set the relevant parameter to NULL. Any necessary message acknowledgements and status communications are handled in the background without any intervention from the client application. If you do not set a messageArrived callback function, you will not be notified of the receipt of any messages as a result of a subscription.
Note: The MQTT client must be disconnected when this function is called.
handle | A valid client handle from a successful call to MQTTAsync_create(). |
context | A pointer to any application-specific context. The the context pointer is passed to each of the callback functions to provide access to the context information in the callback. |
cl | A pointer to an MQTTAsync_connectionLost() callback function. You can set this to NULL if your application doesn't handle disconnections. |
ma | A pointer to an MQTTAsync_messageArrived() callback function. You can set this to NULL if your application doesn't handle receipt of messages. |
dc | A pointer to an MQTTAsync_deliveryComplete() callback function. You can set this to NULL if you do not want to check for successful delivery. |
int MQTTAsync_create | ( | MQTTAsync * | handle, |
char * | serverURI, | ||
char * | clientId, | ||
int | persistence_type, | ||
void * | persistence_context | ||
) |
This function creates an MQTT client ready for connection to the specified server and using the specified persistent storage (see MQTTAsync_persistence). See also MQTTAsync_destroy().
handle | A pointer to an MQTTAsync handle. The handle is populated with a valid client reference following a successful return from this function. |
serverURI | A null-terminated string specifying the server to which the client will connect. It takes the form protocol://host:port. protocol must be tcp or ssl. For host, you can specify either an IP address or a domain name. For instance, to connect to a server running on the local machines with the default MQTT port, specify tcp://localhost:1883. |
clientId | The client identifier passed to the server when the client connects to it. It is a null-terminated UTF-8 encoded string. ClientIDs must be no longer than 23 characters according to the MQTT specification. |
persistence_type | The type of persistence to be used by the client: MQTTCLIENT_PERSISTENCE_NONE: Use in-memory persistence. If the device or system on which the client is running fails or is switched off, the current state of any in-flight messages is lost and some messages may not be delivered even at QoS1 and QoS2. MQTTCLIENT_PERSISTENCE_DEFAULT: Use the default (file system-based) persistence mechanism. Status about in-flight messages is held in persistent storage and provides some protection against message loss in the case of unexpected failure. MQTTCLIENT_PERSISTENCE_USER: Use an application-specific persistence implementation. Using this type of persistence gives control of the persistence mechanism to the application. The application has to implement the MQTTClient_persistence interface. |
persistence_context | If the application uses MQTTCLIENT_PERSISTENCE_NONE persistence, this argument is unused and should be set to NULL. For MQTTCLIENT_PERSISTENCE_DEFAULT persistence, it should be set to the location of the persistence directory (if set to NULL, the persistence directory used is the working directory). Applications that use MQTTCLIENT_PERSISTENCE_USER persistence set this argument to point to a valid MQTTClient_persistence structure. |
int MQTTAsync_connect | ( | MQTTAsync | handle, |
MQTTAsync_connectOptions * | options | ||
) |
This function attempts to connect a previously-created client (see MQTTAsync_create()) to an MQTT server using the specified options. If you want to enable asynchronous message and status notifications, you must call MQTTAsync_setCallbacks() prior to MQTTAsync_connect().
handle | A valid client handle from a successful call to MQTTAsync_create(). |
options | A pointer to a valid MQTTAsync_connectOptions structure. |
int MQTTAsync_disconnect | ( | MQTTAsync | handle, |
MQTTAsync_disconnectOptions * | options | ||
) |
This function attempts to disconnect the client from the MQTT server. In order to allow the client time to complete handling of messages that are in-flight when this function is called, a timeout period is specified. When the timeout period has expired, the client disconnects even if there are still outstanding message acknowledgements. The next time the client connects to the same server, any QoS 1 or 2 messages which have not completed will be retried depending on the cleansession settings for both the previous and the new connection (see MQTTAsync_connectOptions.cleansession and MQTTAsync_connect()).
handle | A valid client handle from a successful call to MQTTAsync_create(). |
options | The client delays disconnection for up to this time (in milliseconds) in order to allow in-flight message transfers to complete. |
int MQTTAsync_isConnected | ( | MQTTAsync | handle | ) |
This function allows the client application to test whether or not a client is currently connected to the MQTT server.
handle | A valid client handle from a successful call to MQTTAsync_create(). |
int MQTTAsync_subscribe | ( | MQTTAsync | handle, |
char * | topic, | ||
int | qos, | ||
MQTTAsync_responseOptions * | response | ||
) |
This function attempts to subscribe a client to a single topic, which may contain wildcards (see Subscription wildcards). This call also specifies the Quality of service requested for the subscription (see also MQTTAsync_subscribeMany()).
handle | A valid client handle from a successful call to MQTTAsync_create(). |
topic | The subscription topic, which may include wildcards. |
qos | The requested quality of service for the subscription. |
response | A pointer to a response options structure. Used to set callback functions. |
int MQTTAsync_subscribeMany | ( | MQTTAsync | handle, |
int | count, | ||
char ** | topic, | ||
int * | qos, | ||
MQTTAsync_responseOptions * | response | ||
) |
This function attempts to subscribe a client to a list of topics, which may contain wildcards (see Subscription wildcards). This call also specifies the Quality of service requested for each topic (see also MQTTAsync_subscribe()).
handle | A valid client handle from a successful call to MQTTAsync_create(). |
count | The number of topics for which the client is requesting subscriptions. |
topic | An array (of length count) of pointers to topics, each of which may include wildcards. |
qos | An array (of length count) of Quality of service values. qos[n] is the requested QoS for topic[n]. |
response | A pointer to a response options structure. Used to set callback functions. |
int MQTTAsync_unsubscribe | ( | MQTTAsync | handle, |
char * | topic, | ||
MQTTAsync_responseOptions * | response | ||
) |
This function attempts to remove an existing subscription made by the specified client.
handle | A valid client handle from a successful call to MQTTAsync_create(). |
topic | The topic for the subscription to be removed, which may include wildcards (see Subscription wildcards). |
response | A pointer to a response options structure. Used to set callback functions. |
int MQTTAsync_unsubscribeMany | ( | MQTTAsync | handle, |
int | count, | ||
char ** | topic, | ||
MQTTAsync_responseOptions * | response | ||
) |
This function attempts to remove existing subscriptions to a list of topics made by the specified client.
handle | A valid client handle from a successful call to MQTTAsync_create(). |
count | The number subscriptions to be removed. |
topic | An array (of length count) of pointers to the topics of the subscriptions to be removed, each of which may include wildcards. |
response | A pointer to a response options structure. Used to set callback functions. |
int MQTTAsync_send | ( | MQTTAsync | handle, |
char * | destinationName, | ||
int | payloadlen, | ||
void * | payload, | ||
int | qos, | ||
int | retained, | ||
MQTTAsync_responseOptions * | response | ||
) |
This function attempts to publish a message to a given topic (see also MQTTAsync_sendMessage()). An MQTTAsync_token is issued when this function returns successfully. If the client application needs to test for successful delivery of messages, a callback should be set (see MQTTAsync_onSuccess() and MQTTAsync_deliveryComplete()).
handle | A valid client handle from a successful call to MQTTAsync_create(). |
destinationName | The topic associated with this message. |
payloadlen | The length of the payload in bytes. |
payload | A pointer to the byte array payload of the message. |
qos | The Quality of service of the message. |
retained | The retained flag for the message. |
response | A pointer to an MQTTAsync_responseOptions structure. Used to set callback functions. This is optional and can be set to NULL. |
int MQTTAsync_sendMessage | ( | MQTTAsync | handle, |
char * | destinationName, | ||
MQTTAsync_message * | msg, | ||
MQTTAsync_responseOptions * | response | ||
) |
This function attempts to publish a message to a given topic (see also MQTTAsync_publish()). An MQTTAsync_token is issued when this function returns successfully. If the client application needs to test for successful delivery of messages, a callback should be set (see MQTTAsync_onSuccess() and MQTTAsync_deliveryComplete()).
handle | A valid client handle from a successful call to MQTTAsync_create(). |
destinationName | The topic associated with this message. |
msg | A pointer to a valid MQTTAsync_message structure containing the payload and attributes of the message to be published. |
response | A pointer to an MQTTAsync_responseOptions structure. Used to set callback functions. |
int MQTTAsync_getPendingTokens | ( | MQTTAsync | handle, |
MQTTAsync_token ** | tokens | ||
) |
This function sets a pointer to an array of tokens for messages that are currently in-flight (pending completion).
Important note: The memory used to hold the array of tokens is malloc()'d in this function. The client application is responsible for freeing this memory when it is no longer required.
handle | A valid client handle from a successful call to MQTTAsync_create(). |
tokens | The address of a pointer to an MQTTAsync_token. When the function returns successfully, the pointer is set to point to an array of tokens representing messages pending completion. The last member of the array is set to -1 to indicate there are no more tokens. If no tokens are pending, the pointer is set to NULL. |
void MQTTAsync_freeMessage | ( | MQTTAsync_message ** | msg | ) |
This function frees memory allocated to an MQTT message, including the additional memory allocated to the message payload. The client application calls this function when the message has been fully processed. Important note: This function does not free the memory allocated to a message topic string. It is the responsibility of the client application to free this memory using the MQTTAsync_free() library function.
msg | The address of a pointer to the MQTTAsync_message structure to be freed. |
void MQTTAsync_free | ( | void * | ptr | ) |
This function frees memory allocated by the MQTT C client library, especially the topic name. This is needed on Windows when the client libary and application program have been compiled with different versions of the C compiler. It is thus good policy to always use this function when freeing any MQTT C client- allocated memory.
ptr | The pointer to the client library storage to be freed. |
void MQTTAsync_destroy | ( | MQTTAsync * | handle | ) |
This function frees the memory allocated to an MQTT client (see MQTTAsync_create()). It should be called when the client is no longer required.
handle | A pointer to the handle referring to the MQTTAsync structure to be freed. |
void MQTTAsync_setTraceLevel | ( | enum MQTTASYNC_TRACE_LEVELS | level | ) |
This function sets the level of trace information which will be returned in the trace callback.
level | the trace level required |
void MQTTAsync_setTraceCallback | ( | MQTTAsync_traceCallback * | callback | ) |
This function sets the trace callback if needed. If set to NULL, no trace information will be returned. The default trace level is MQTTASYNC_TRACE_MINIMUM.
callback | a pointer to the function which will handle the trace information |
This function returns version information about the library. no trace information will be returned. The default trace level is MQTTASYNC_TRACE_MINIMUM