15 PyObject *pyoptions = NULL, *temp;
19 if (!PyArg_ParseTuple(args,
"ss|iO", &serverURI, &clientId,
20 &persistence_option, &pyoptions))
26 PyErr_SetString(PyExc_TypeError,
"persistence must be DEFAULT or NONE");
33 if (!PyDict_Check(pyoptions))
35 PyErr_SetString(PyExc_TypeError,
36 "Create options parameter must be a dictionary");
40 if ((temp = PyDict_GetItemString(pyoptions,
"sendWhileDisconnected"))
43 if (PyInt_Check(temp))
47 PyErr_SetString(PyExc_TypeError,
"sendWhileDisconnected value must be int");
52 if ((temp = PyDict_GetItemString(pyoptions,
"maxBufferedMessages"))
55 if (PyInt_Check(temp))
59 PyErr_SetString(PyExc_TypeError,
"maxBufferedMessages value must be int");
69 return Py_BuildValue(
"ik", rc, c);
85 PyObject *cl, *
ma, *dc;
117 PyGILState_STATE gstate;
119 gstate = PyGILState_Ensure();
120 arglist = Py_BuildValue(
"Os", e->
context, cause);
121 result = PyEval_CallObject(e->
co, arglist);
123 PyGILState_Release(gstate);
133 PyGILState_STATE gstate;
135 gstate = PyGILState_Ensure();
136 arglist = Py_BuildValue(
"Os", e->
context, cause);
137 result = PyEval_CallObject(e->
cl, arglist);
139 PyGILState_Release(gstate);
149 PyGILState_STATE gstate;
151 gstate = PyGILState_Ensure();
153 result = PyObject_CallFunction(e->
ma,
"Os{ss#sisisisi}", e->
context,
155 "qos", message->
qos,
"retained", message->
retained,
"dup",
156 message->
dup,
"msgid", message->
msgid);
158 result = PyObject_CallFunction(e->
ma,
"Os#{ss#sisisisi}", e->
context,
159 topicName, topicLen,
"payload", message->
payload,
165 if (PyInt_Check(result))
166 rc = (int) PyInt_AsLong(result);
169 PyGILState_Release(gstate);
181 PyGILState_STATE gstate;
183 gstate = PyGILState_Ensure();
184 arglist = Py_BuildValue(
"Oi", e->
context, dt);
185 result = PyEval_CallObject(e->
dc, arglist);
187 PyGILState_Release(gstate);
199 if (!PyArg_ParseTuple(args,
"kOOOO", &c, (PyObject**) &e->
context, &e->
cl,
204 if ((e->
cl != Py_None && !PyCallable_Check(e->
cl))
205 || (e->
ma != Py_None && !PyCallable_Check(e->
ma))
206 || (e->
dc != Py_None && !PyCallable_Check(e->
dc)))
208 PyErr_SetString(PyExc_TypeError,
209 "3rd, 4th and 5th parameters must be callable or None");
213 Py_BEGIN_ALLOW_THREADS
232 return Py_BuildValue(
"i", rc);
244 if (!PyArg_ParseTuple(args,
"kOO", &c, (PyObject**) &e->
context, &e->
co))
248 if (e->
co != Py_None && !PyCallable_Check(e->
co))
250 PyErr_SetString(PyExc_TypeError,
251 "3rd parameter must be callable or None");
255 Py_BEGIN_ALLOW_THREADS
267 ListAppend(connected_callbacks, e,
sizeof(e));
272 return Py_BuildValue(
"i", rc);
289 PyGILState_STATE gstate;
291 gstate = PyGILState_Ensure();
296 "MQTTVersion", response->
alt.
connect.MQTTVersion,
297 "sessionPresent", response->
alt.
connect.sessionPresent,
298 "serverURI", response->
alt.
connect.serverURI);
303 "token", response->
token,
304 "destinationName", response->
alt.
pub.destinationName,
306 "payload", response->
alt.
pub.message.payload,
307 response->
alt.
pub.message.payloadlen,
308 "qos", response->
alt.
pub.message.qos,
309 "retained", response->
alt.
pub.message.retained);
314 "token", response->
token,
315 "qos", response->
alt.
qos);
320 "token", response->
token,
326 "token", response->
token);
332 printf(
"decrementing reference count for result\n");
334 PyGILState_Release(gstate);
342 PyObject *arglist = NULL;
344 PyGILState_STATE gstate;
346 gstate = PyGILState_Ensure();
350 arglist = Py_BuildValue(
"OO", e->
context, response);
352 arglist = Py_BuildValue(
"OO", Py_None, response);
354 result = PyEval_CallObject(e->
onFailure, arglist);
356 PyGILState_Release(gstate);
365 PyObject *temp = NULL;
370 if (!PyDict_Check(pyoptions))
372 PyErr_SetString(PyExc_TypeError,
"Response options must be a dictionary");
376 if ((temp = PyDict_GetItemString(pyoptions,
"onSuccess")) != NULL)
378 if (PyCallable_Check(temp))
382 PyErr_SetString(PyExc_TypeError,
383 "onSuccess value must be callable");
388 if ((temp = PyDict_GetItemString(pyoptions,
"onFailure")) != NULL)
390 if (PyCallable_Check(temp))
394 PyErr_SetString(PyExc_TypeError,
395 "onFailure value must be callable");
400 responseOptions->
context = PyDict_GetItemString(pyoptions,
"context");
422 PyObject *pyoptions = NULL, *temp;
427 if (!PyArg_ParseTuple(args,
"k|O", &c, &pyoptions))
433 if (!PyDict_Check(pyoptions))
435 PyErr_SetString(PyExc_TypeError,
"2nd parameter must be a dictionary");
439 if ((temp = PyDict_GetItemString(pyoptions,
"onSuccess")) != NULL)
441 if (PyCallable_Check(temp))
445 PyErr_SetString(PyExc_TypeError,
446 "onSuccess value must be callable");
451 if ((temp = PyDict_GetItemString(pyoptions,
"onFailure")) != NULL)
453 if (PyCallable_Check(temp))
457 PyErr_SetString(PyExc_TypeError,
458 "onFailure value must be callable");
463 connectOptions.
context = PyDict_GetItemString(pyoptions,
"context");
478 if ((temp = PyDict_GetItemString(pyoptions,
"keepAliveInterval")) != NULL)
480 if (PyInt_Check(temp))
484 PyErr_SetString(PyExc_TypeError,
485 "keepAliveLiveInterval value must be int");
490 if ((temp = PyDict_GetItemString(pyoptions,
"cleansession")) != NULL)
492 if (PyInt_Check(temp))
496 PyErr_SetString(PyExc_TypeError,
"cleansession value must be int");
501 if ((temp = PyDict_GetItemString(pyoptions,
"will")) != NULL)
503 if (PyDict_Check(temp))
505 PyObject *wtemp = NULL;
506 if ((wtemp = PyDict_GetItemString(temp,
"topicName")) == NULL)
508 PyErr_SetString(PyExc_TypeError,
509 "will topicName value must be set");
514 if (PyString_Check(wtemp))
515 willOptions.
topicName = PyString_AsString(wtemp);
518 PyErr_SetString(PyExc_TypeError,
519 "will topicName value must be string");
523 if ((wtemp = PyDict_GetItemString(temp,
"message")) == NULL)
525 PyErr_SetString(PyExc_TypeError,
526 "will message value must be set");
531 if (PyString_Check(wtemp))
532 willOptions.
message = PyString_AsString(wtemp);
535 PyErr_SetString(PyExc_TypeError,
536 "will message value must be string");
540 if ((wtemp = PyDict_GetItemString(temp,
"retained")) != NULL)
542 if (PyInt_Check(wtemp))
543 willOptions.
retained = (int) PyInt_AsLong(wtemp);
546 PyErr_SetString(PyExc_TypeError,
547 "will retained value must be int");
551 if ((wtemp = PyDict_GetItemString(temp,
"qos")) != NULL)
553 if (PyInt_Check(wtemp))
554 willOptions.
qos = (int) PyInt_AsLong(wtemp);
557 PyErr_SetString(PyExc_TypeError,
558 "will qos value must be int");
562 connectOptions.
will = &willOptions;
566 PyErr_SetString(PyExc_TypeError,
"will value must be dictionary");
571 if ((temp = PyDict_GetItemString(pyoptions,
"username")) != NULL)
573 if (PyString_Check(temp))
574 connectOptions.
username = PyString_AsString(temp);
577 PyErr_SetString(PyExc_TypeError,
"username value must be string");
582 if ((temp = PyDict_GetItemString(pyoptions,
"password")) != NULL)
584 if (PyString_Check(temp))
585 connectOptions.
username = PyString_AsString(temp);
588 PyErr_SetString(PyExc_TypeError,
"password value must be string");
593 if ((temp = PyDict_GetItemString(pyoptions,
"automaticReconnect")) != NULL)
595 if (PyInt_Check(temp))
599 PyErr_SetString(PyExc_TypeError,
"automatic reconnect value must be int");
604 if ((temp = PyDict_GetItemString(pyoptions,
"minRetryInterval")) != NULL)
606 if (PyInt_Check(temp))
610 PyErr_SetString(PyExc_TypeError,
"minRetryInterval value must be int");
615 if ((temp = PyDict_GetItemString(pyoptions,
"maxRetryInterval")) != NULL)
617 if (PyInt_Check(temp))
621 PyErr_SetString(PyExc_TypeError,
"maxRetryInterval value must be int");
627 Py_BEGIN_ALLOW_THREADS
630 return Py_BuildValue(
"i", rc);
640 if (!PyArg_ParseTuple(args,
"k|i", &c, &options.
timeout))
643 Py_BEGIN_ALLOW_THREADS
646 return Py_BuildValue(
"i", rc);
655 if (!PyArg_ParseTuple(args,
"k", &c))
657 Py_BEGIN_ALLOW_THREADS
660 return Py_BuildValue(
"i", rc);
668 PyObject *pyoptions = NULL;
673 if (!PyArg_ParseTuple(args,
"ks|iO", &c, &topic, &qos, &pyoptions))
679 Py_BEGIN_ALLOW_THREADS;
681 Py_END_ALLOW_THREADS;
682 return Py_BuildValue(
"i", rc);
692 PyObject *pyoptions = NULL;
700 if (!PyArg_ParseTuple(args,
"kOO|O", &c, &topicList, &qosList, &pyoptions))
706 if (!PySequence_Check(topicList) || !PySequence_Check(qosList))
708 PyErr_SetString(PyExc_TypeError,
709 "3rd and 4th parameters must be sequences");
713 if ((count = PySequence_Length(topicList)) != PySequence_Length(qosList))
715 PyErr_SetString(PyExc_TypeError,
716 "3rd and 4th parameters must be sequences of the same length");
720 topics =
malloc(count *
sizeof(
char*));
721 for (i = 0; i <
count; ++i)
722 topics[i] = PyString_AsString(PySequence_GetItem(topicList, i));
724 qoss =
malloc(count *
sizeof(
int));
725 for (i = 0; i <
count; ++i)
726 qoss[i] = (
int) PyInt_AsLong(PySequence_GetItem(qosList, i));
728 Py_BEGIN_ALLOW_THREADS
732 for (i = 0; i <
count; ++i)
733 PySequence_SetItem(qosList, i, PyInt_FromLong((
long) qoss[i]));
739 return Py_BuildValue(
"iO", rc, qosList);
741 return Py_BuildValue(
"i", rc);
749 PyObject *pyoptions = NULL;
753 if (!PyArg_ParseTuple(args,
"ks|O", &c, &topic, &pyoptions))
759 Py_BEGIN_ALLOW_THREADS
762 return Py_BuildValue(
"i", rc);
771 PyObject *pyoptions = NULL;
778 if (!PyArg_ParseTuple(args,
"kO|O", &c, &topicList, &pyoptions))
784 if (!PySequence_Check(topicList))
786 PyErr_SetString(PyExc_TypeError,
"3rd parameter must be sequences");
790 count = PySequence_Length(topicList);
791 topics =
malloc(count *
sizeof(
char*));
792 for (i = 0; i <
count; ++i)
793 topics[i] = PyString_AsString(PySequence_GetItem(topicList, i));
795 Py_BEGIN_ALLOW_THREADS
801 return Py_BuildValue(
"i", rc);
808 char* destinationName;
814 PyObject *pyoptions = NULL;
817 if (!PyArg_ParseTuple(args,
"kss#|iiO", &c, &destinationName, &payload,
818 &payloadlen, &qos, &retained, &pyoptions))
824 Py_BEGIN_ALLOW_THREADS
825 rc =
MQTTAsync_send(c, destinationName, payloadlen, payload, qos, retained, &response);
829 return Py_BuildValue(
"ii", rc, response);
831 return Py_BuildValue(
"i", rc);
838 char* destinationName;
839 PyObject *message, *temp;
842 PyObject *pyoptions = NULL;
845 if (!PyArg_ParseTuple(args,
"ksO|O", &c, &destinationName, &message, &pyoptions))
851 if (!PyDict_Check(message))
853 PyErr_SetString(PyExc_TypeError,
"3rd parameter must be a dictionary");
857 if ((temp = PyDict_GetItemString(message,
"payload")) == NULL)
859 PyErr_SetString(PyExc_TypeError,
"dictionary must have payload key");
863 if (PyString_Check(temp))
864 PyString_AsStringAndSize(temp, (
char**) &msg.
payload,
868 PyErr_SetString(PyExc_TypeError,
"payload value must be string");
872 if ((temp = PyDict_GetItemString(message,
"qos")) == NULL)
873 msg.
qos = (int) PyInt_AsLong(temp);
875 if ((temp = PyDict_GetItemString(message,
"retained")) == NULL)
876 msg.
retained = (
int) PyInt_AsLong(temp);
878 Py_BEGIN_ALLOW_THREADS
883 return Py_BuildValue(
"ii", rc, response);
885 return Py_BuildValue(
"i", rc);
892 unsigned long timeout = 1000L;
896 if (!PyArg_ParseTuple(args,
"ki|i", &c, &dt, &timeout))
899 Py_BEGIN_ALLOW_THREADS
903 return Py_BuildValue(
"i", rc);
913 if (!PyArg_ParseTuple(args,
"k", &c))
916 Py_BEGIN_ALLOW_THREADS
923 PyObject* dts = PyList_New(0);
925 while (tokens[i] != -1)
926 PyList_Append(dts, PyInt_FromLong((
long) tokens[i]));
928 return Py_BuildValue(
"iO", rc, dts);
931 return Py_BuildValue(
"i", rc);
940 if (!PyArg_ParseTuple(args,
"k", &c))
964 {
"create",
mqttv3_create, METH_VARARGS,
"Create an MQTTv3 client." },
966 "Sets the callback functions for a particular client." },
968 "Sets the connected callback function for a particular client." },
970 "Connects to a server using the specified options." },
972 "Disconnects from a server." },
974 "Determines if this client is currently connected to the server." },
976 "Subscribe to the given topic." },
978 "Subscribe to the given topics." },
980 "Unsubscribe from the given topic." },
982 "Unsubscribe from the given topics." },
984 "Publish a message to the given topic." },
986 "Publish a message to the given topic." },
988 "Waits for the completion of the delivery of the message represented by a delivery token." },
990 "Returns the tokens pending of completion." },
992 "Free memory allocated to a MQTT client. It is the opposite to create." },
993 { NULL, NULL, 0, NULL }
1001 PyEval_InitThreads();
1010 MqttV3Error = PyErr_NewException(
"paho_mqtt3a.error", NULL, NULL);
1024 PyModule_AddIntConstant(m,
"PERSISTENCE_ERROR",
MQTTAsync_onFailure * onFailure
void onSuccess(void *context, MQTTAsync_successData *response)
#define MQTTASYNC_NULL_PARAMETER
static PyObject * mqttv3_setcallbacks(PyObject *self, PyObject *args)
int MQTTAsync_createWithOptions(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context, MQTTAsync_createOptions *options)
static List * connected_callbacks
union MQTTAsync_successData::@46 alt
int sendWhileDisconnected
#define MQTTCLIENT_PERSISTENCE_ERROR
#define MQTTAsync_responseOptions_initializer
#define MQTTASYNC_FAILURE
static PyObject * mqttv3_unsubscribeMany(PyObject *self, PyObject *args)
void connected(void *context, char *cause)
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
#define MQTTASYNC_BAD_UTF8_STRING
#define MQTTCLIENT_PERSISTENCE_USER
void MQTTAsync_free(void *memory)
void MQTTAsync_freeMessage(MQTTAsync_message **message)
#define MQTTASYNC_DISCONNECTED
static PyObject * mqttv3_subscribe(PyObject *self, PyObject *args)
static PyObject * mqttv3_destroy(PyObject *self, PyObject *args)
int MQTTAsync_setConnected(MQTTAsync handle, void *context, MQTTAsync_connected *connected)
int MQTTAsync_unsubscribe(MQTTAsync handle, const char *topic, MQTTAsync_responseOptions *response)
static PyObject * mqttv3_connect(PyObject *self, PyObject *args)
struct MQTTAsync_successData::@46::@48 connect
int ListDetach(List *aList, void *content)
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
int MQTTAsync_subscribe(MQTTAsync handle, const char *topic, int qos, MQTTAsync_responseOptions *response)
void deliveryComplete(void *context, MQTTAsync_token dt)
static PyObject * mqttv3_disconnect(PyObject *self, PyObject *args)
#define MQTTAsync_willOptions_initializer
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
int MQTTAsync_getPendingTokens(MQTTAsync handle, MQTTAsync_token **tokens)
static PyObject * mqttv3_getPendingTokens(PyObject *self, PyObject *args)
#define MQTTAsync_disconnectOptions_initializer
MQTTAsync_onFailure * onFailure
int MQTTAsync_unsubscribeMany(MQTTAsync handle, int count, char *const *topic, MQTTAsync_responseOptions *response)
static PyObject * mqttv3_send(PyObject *self, PyObject *args)
#define MQTTAsync_createOptions_initializer
int MQTTAsync_isConnected(MQTTAsync handle)
static PyObject * mqttv3_create(PyObject *self, PyObject *args)
int MQTTAsync_subscribeMany(MQTTAsync handle, int count, char *const *topic, int *qos, MQTTAsync_responseOptions *response)
int getResponseOptions(MQTTAsync c, PyObject *pyoptions, MQTTAsync_responseOptions *responseOptions, enum msgTypes msgType)
ListElement * ListAppend(List *aList, void *content, size_t size)
#define MQTTAsync_connectOptions_initializer
MQTTAsync_onSuccess * onSuccess
int connectedCompare(void *a, void *b)
MQTTAsync_willOptions * will
static PyObject * mqttv3_subscribeMany(PyObject *self, PyObject *args)
#define MQTTCLIENT_PERSISTENCE_DEFAULT
int MQTTAsync_send(MQTTAsync handle, const char *destinationName, int payloadlen, const void *payload, int qos, int retained, MQTTAsync_responseOptions *response)
void MQTTAsync_onSuccess(void *context, MQTTAsync_successData *response)
void MQTTAsync_destroy(MQTTAsync *handle)
static PyObject * mqttv3_sendMessage(PyObject *self, PyObject *args)
#define MQTTASYNC_SUCCESS
static PyMethodDef MqttV3Methods[]
int MQTTAsync_sendMessage(MQTTAsync handle, const char *destinationName, const MQTTAsync_message *message, MQTTAsync_responseOptions *response)
static PyObject * mqttv3_isConnected(PyObject *self, PyObject *args)
struct MQTTAsync_successData::@46::@47 pub
#define MQTTCLIENT_PERSISTENCE_NONE
void connectionLost(void *context, char *cause)
static PyObject * mqttv3_waitForCompletion(PyObject *self, PyObject *args)
void MQTTAsync_onFailure(void *context, MQTTAsync_failureData *response)
List * ListInitialize(void)
PyMODINIT_FUNC initpaho_mqtt3a(void)
ListElement * ListFindItem(List *aList, void *content, int(*callback)(void *, void *))
#define MQTTASYNC_TOPICNAME_TRUNCATED
#define MQTTAsync_message_initializer
static PyObject * mqttv3_unsubscribe(PyObject *self, PyObject *args)
MQTTAsync_onSuccess * onSuccess
int clientCompare(void *a, void *b)
#define MQTTASYNC_MAX_MESSAGES_INFLIGHT
void onFailure(void *context, MQTTAsync_failureData *response)
int messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
static PyObject * mqttv3_setconnected(PyObject *self, PyObject *args)
int MQTTAsync_waitForCompletion(MQTTAsync handle, MQTTAsync_token dt, unsigned long timeout)
static PyObject * MqttV3Error