Commit e798b9ce authored by fmp's avatar fmp

Added separate context pointers for each of the global callbacks

parent 7fb29737
...@@ -298,7 +298,9 @@ typedef struct MQTTAsync_struct ...@@ -298,7 +298,9 @@ typedef struct MQTTAsync_struct
MQTTAsync_connectionLost* cl; MQTTAsync_connectionLost* cl;
MQTTAsync_messageArrived* ma; MQTTAsync_messageArrived* ma;
MQTTAsync_deliveryComplete* dc; MQTTAsync_deliveryComplete* dc;
void* context; /* the context to be associated with the main callbacks*/ void* clContext; /* the context to be associated with the conn lost callback*/
void* maContext; /* the context to be associated with the msg arrived callback*/
void* dcContext; /* the context to be associated with the deliv complete callback*/
MQTTAsync_connected* connected; MQTTAsync_connected* connected;
void* connected_context; /* the context to be associated with the connected callback*/ void* connected_context; /* the context to be associated with the connected callback*/
...@@ -971,7 +973,7 @@ static void MQTTAsync_checkDisconnect(MQTTAsync handle, MQTTAsync_command* comma ...@@ -971,7 +973,7 @@ static void MQTTAsync_checkDisconnect(MQTTAsync handle, MQTTAsync_command* comma
if (m->cl && was_connected) if (m->cl && was_connected)
{ {
Log(TRACE_MIN, -1, "Calling connectionLost for client %s", m->c->clientID); Log(TRACE_MIN, -1, "Calling connectionLost for client %s", m->c->clientID);
(*(m->cl))(m->context, NULL); (*(m->cl))(m->clContext, NULL);
} }
MQTTAsync_startConnectRetry(m); MQTTAsync_startConnectRetry(m);
} }
...@@ -2034,7 +2036,7 @@ int MQTTAsync_setCallbacks(MQTTAsync handle, void* context, ...@@ -2034,7 +2036,7 @@ int MQTTAsync_setCallbacks(MQTTAsync handle, void* context,
rc = MQTTASYNC_FAILURE; rc = MQTTASYNC_FAILURE;
else else
{ {
m->context = context; m->clContext = m->maContext = m->dcContext = context;
m->cl = cl; m->cl = cl;
m->ma = ma; m->ma = ma;
m->dc = dc; m->dc = dc;
...@@ -2045,6 +2047,74 @@ int MQTTAsync_setCallbacks(MQTTAsync handle, void* context, ...@@ -2045,6 +2047,74 @@ int MQTTAsync_setCallbacks(MQTTAsync handle, void* context,
return rc; return rc;
} }
int MQTTAsync_setConnectionLostCallback(MQTTAsync handle, void* context,
MQTTAsync_connectionLost* cl)
{
int rc = MQTTASYNC_SUCCESS;
MQTTAsyncs* m = handle;
FUNC_ENTRY;
MQTTAsync_lock_mutex(mqttasync_mutex);
if (m == NULL || m->c->connect_state != 0)
rc = MQTTASYNC_FAILURE;
else
{
m->clContext = context;
m->cl = cl;
}
MQTTAsync_unlock_mutex(mqttasync_mutex);
FUNC_EXIT_RC(rc);
return rc;
}
int MQTTAsync_setMessageArrivedCallback(MQTTAsync handle, void* context,
MQTTAsync_messageArrived* ma)
{
int rc = MQTTASYNC_SUCCESS;
MQTTAsyncs* m = handle;
FUNC_ENTRY;
MQTTAsync_lock_mutex(mqttasync_mutex);
if (m == NULL || ma == NULL || m->c->connect_state != 0)
rc = MQTTASYNC_FAILURE;
else
{
m->maContext = context;
m->ma = ma;
}
MQTTAsync_unlock_mutex(mqttasync_mutex);
FUNC_EXIT_RC(rc);
return rc;
}
int MQTTAsync_setDeliveryCompleteCallback(MQTTAsync handle, void* context,
MQTTAsync_deliveryComplete* dc)
{
int rc = MQTTASYNC_SUCCESS;
MQTTAsyncs* m = handle;
FUNC_ENTRY;
MQTTAsync_lock_mutex(mqttasync_mutex);
if (m == NULL || m->c->connect_state != 0)
rc = MQTTASYNC_FAILURE;
else
{
m->dcContext = context;
m->dc = dc;
}
MQTTAsync_unlock_mutex(mqttasync_mutex);
FUNC_EXIT_RC(rc);
return rc;
}
int MQTTAsync_setConnected(MQTTAsync handle, void* context, MQTTAsync_connected* connected) int MQTTAsync_setConnected(MQTTAsync handle, void* context, MQTTAsync_connected* connected)
{ {
...@@ -2152,7 +2222,7 @@ static int MQTTAsync_deliverMessage(MQTTAsyncs* m, char* topicName, size_t topic ...@@ -2152,7 +2222,7 @@ static int MQTTAsync_deliverMessage(MQTTAsyncs* m, char* topicName, size_t topic
Log(TRACE_MIN, -1, "Calling messageArrived for client %s, queue depth %d", Log(TRACE_MIN, -1, "Calling messageArrived for client %s, queue depth %d",
m->c->clientID, m->c->messageQueue->count); m->c->clientID, m->c->messageQueue->count);
rc = (*(m->ma))(m->context, topicName, (int)topicLen, mm); rc = (*(m->ma))(m->maContext, topicName, (int)topicLen, mm);
/* if 0 (false) is returned by the callback then it failed, so we don't remove the message from /* if 0 (false) is returned by the callback then it failed, so we don't remove the message from
* the queue, and it will be retried later. If 1 is returned then the message data may have been freed, * the queue, and it will be retried later. If 1 is returned then the message data may have been freed,
* so we must be careful how we use it. * so we must be careful how we use it.
...@@ -3006,7 +3076,7 @@ static MQTTPacket* MQTTAsync_cycle(int* sock, unsigned long timeout, int* rc) ...@@ -3006,7 +3076,7 @@ static MQTTPacket* MQTTAsync_cycle(int* sock, unsigned long timeout, int* rc)
if (m->dc) if (m->dc)
{ {
Log(TRACE_MIN, -1, "Calling deliveryComplete for client %s, msgid %d", m->c->clientID, msgid); Log(TRACE_MIN, -1, "Calling deliveryComplete for client %s, msgid %d", m->c->clientID, msgid);
(*(m->dc))(m->context, msgid); (*(m->dc))(m->dcContext, msgid);
} }
/* use the msgid to find the callback to be called */ /* use the msgid to find the callback to be called */
while (ListNextElement(m->responses, &current)) while (ListNextElement(m->responses, &current))
......
...@@ -496,8 +496,76 @@ typedef struct ...@@ -496,8 +496,76 @@ typedef struct
* ::MQTTASYNC_FAILURE if an error occurred. * ::MQTTASYNC_FAILURE if an error occurred.
*/ */
DLLExport int MQTTAsync_setCallbacks(MQTTAsync handle, void* context, MQTTAsync_connectionLost* cl, DLLExport int MQTTAsync_setCallbacks(MQTTAsync handle, void* context, MQTTAsync_connectionLost* cl,
MQTTAsync_messageArrived* ma, MQTTAsync_deliveryComplete* dc); MQTTAsync_messageArrived* ma, MQTTAsync_deliveryComplete* dc);
/**
* This function sets the callback function for a connection lost event for
* a specific client. Any necessary message acknowledgements and status
* communications are handled in the background without any intervention
* from the client application.
*
* <b>Note:</b> The MQTT client must be disconnected when this function is
* called.
* @param handle A valid client handle from a successful call to
* MQTTAsync_create().
* @param context A pointer to any application-specific context. The
* the <i>context</i> pointer is passed the callback functions to provide
* access to the context information in the callback.
* @param cl A pointer to an MQTTAsync_connectionLost() callback
* function. You can set this to NULL if your application doesn't handle
* disconnections.
* @return ::MQTTASYNC_SUCCESS if the callbacks were correctly set,
* ::MQTTASYNC_FAILURE if an error occurred.
*/
DLLExport int MQTTAsync_setConnectionLostCallback(MQTTAsync handle, void* context,
MQTTAsync_connectionLost* cl);
/**
* This function sets the callback function for a message arrived event for
* a specific client. 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.
*
* <b>Note:</b> The MQTT client must be disconnected when this function is
* called.
* @param handle A valid client handle from a successful call to
* MQTTAsync_create().
* @param context A pointer to any application-specific context. The
* the <i>context</i> pointer is passed to the callback functions to provide
* access to the context information in the callback.
* @param ma A pointer to an MQTTAsync_messageArrived() callback
* function. You can set this to NULL if your application doesn't handle
* receipt of messages.
* @return ::MQTTASYNC_SUCCESS if the callbacks were correctly set,
* ::MQTTASYNC_FAILURE if an error occurred.
*/
DLLExport int MQTTAsync_setMessageArrivedCallback(MQTTAsync handle, void* context,
MQTTAsync_messageArrived* ma);
/**
* This function sets the callback function for a delivery complete event
* for a specific client. Any necessary message acknowledgements and status
* communications are handled in the background without any intervention
* from the client application.
*
* <b>Note:</b> The MQTT client must be disconnected when this function is
* called.
* @param handle A valid client handle from a successful call to
* MQTTAsync_create().
* @param context A pointer to any application-specific context. The
* the <i>context</i> pointer is passed to the callback functions to provide
* access to the context information in the callback.
* @param 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.
* @return ::MQTTASYNC_SUCCESS if the callbacks were correctly set,
* ::MQTTASYNC_FAILURE if an error occurred.
*/
DLLExport int MQTTAsync_setDeliveryCompleteCallback(MQTTAsync handle, void* context,
MQTTAsync_deliveryComplete* dc);
/** /**
* Sets the MQTTAsync_connected() callback function for a client. * Sets the MQTTAsync_connected() callback function for a client.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment