16 if (!PyArg_ParseTuple(args,
"ss|i", &serverURI, &clientId,
23 PyErr_SetString(PyExc_TypeError,
"persistence must be DEFAULT or NONE");
29 printf(
"create MQTTClient pointer %p\n", c);
31 return Py_BuildValue(
"ik", rc, c);
40 PyObject *cl, *ma, *dc;
55 PyGILState_STATE gstate;
57 gstate = PyGILState_Ensure();
58 arglist = Py_BuildValue(
"Os", e->
context, cause);
59 result = PyEval_CallObject(e->
cl, arglist);
61 PyGILState_Release(gstate);
70 PyGILState_STATE gstate;
75 gstate = PyGILState_Ensure();
77 result = PyEval_CallFunction(e->
ma,
"Os{ss#sisisisi}", e->
context,
79 "qos", message->
qos,
"retained", message->
retained,
"dup",
80 message->
dup,
"msgid", message->
msgid);
82 result = PyEval_CallFunction(e->
ma,
"Os#{ss#sisisisi}", e->
context,
83 topicName, topicLen,
"payload", message->
payload,
89 if (PyInt_Check(result))
90 rc = (int) PyInt_AsLong(result);
93 PyGILState_Release(gstate);
104 PyGILState_STATE gstate;
106 gstate = PyGILState_Ensure();
107 arglist = Py_BuildValue(
"Oi", e->
context, dt);
108 result = PyEval_CallObject(e->
dc, arglist);
110 PyGILState_Release(gstate);
121 if (!PyArg_ParseTuple(args,
"kOOOO", &c, (PyObject**) &e->
context, &e->
cl,
126 printf(
"setCallbacks MQTTClient pointer %p\n", c);
128 if ((e->
cl != Py_None && !PyCallable_Check(e->
cl))
129 || (e->
ma != Py_None && !PyCallable_Check(e->
ma))
130 || (e->
dc != Py_None && !PyCallable_Check(e->
dc)))
132 PyErr_SetString(PyExc_TypeError,
133 "3rd, 4th and 5th parameters must be callable or None");
156 return Py_BuildValue(
"i", rc);
162 PyObject *pyoptions = NULL, *temp;
167 if (!PyArg_ParseTuple(args,
"k|O", &c, &pyoptions))
170 printf(
"connect MQTTClient pointer %p\n", c);
174 if (!PyDict_Check(pyoptions))
176 PyErr_SetString(PyExc_TypeError,
"2nd parameter must be a dictionary");
180 if ((temp = PyDict_GetItemString(pyoptions,
"keepAliveInterval")) != NULL)
182 if (PyInt_Check(temp))
186 PyErr_SetString(PyExc_TypeError,
187 "keepAliveLiveInterval value must be int");
192 if ((temp = PyDict_GetItemString(pyoptions,
"cleansession")) != NULL)
194 if (PyInt_Check(temp))
198 PyErr_SetString(PyExc_TypeError,
"cleansession value must be int");
203 if ((temp = PyDict_GetItemString(pyoptions,
"reliable")) != NULL)
205 if (PyInt_Check(temp))
206 options.
reliable = (int) PyInt_AsLong(temp);
209 PyErr_SetString(PyExc_TypeError,
"reliable value must be int");
214 if ((temp = PyDict_GetItemString(pyoptions,
"will")) != NULL)
216 if (PyDict_Check(temp))
218 PyObject *wtemp = NULL;
219 if ((wtemp = PyDict_GetItemString(temp,
"topicName")) == NULL)
221 PyErr_SetString(PyExc_TypeError,
222 "will topicName value must be set");
227 if (PyString_Check(wtemp))
228 woptions.
topicName = PyString_AsString(wtemp);
231 PyErr_SetString(PyExc_TypeError,
232 "will topicName value must be string");
236 if ((wtemp = PyDict_GetItemString(temp,
"message")) == NULL)
238 PyErr_SetString(PyExc_TypeError,
239 "will message value must be set");
244 if (PyString_Check(wtemp))
245 woptions.
message = PyString_AsString(wtemp);
248 PyErr_SetString(PyExc_TypeError,
249 "will message value must be string");
253 if ((wtemp = PyDict_GetItemString(temp,
"retained")) != NULL)
255 if (PyInt_Check(wtemp))
256 woptions.
retained = (int) PyInt_AsLong(wtemp);
259 PyErr_SetString(PyExc_TypeError,
260 "will retained value must be int");
264 if ((wtemp = PyDict_GetItemString(temp,
"qos")) != NULL)
266 if (PyInt_Check(wtemp))
267 woptions.
qos = (int) PyInt_AsLong(wtemp);
270 PyErr_SetString(PyExc_TypeError,
271 "will qos value must be int");
275 options.
will = &woptions;
279 PyErr_SetString(PyExc_TypeError,
"will value must be dictionary");
284 if ((temp = PyDict_GetItemString(pyoptions,
"username")) != NULL)
286 if (PyString_Check(temp))
287 options.
username = PyString_AsString(temp);
290 PyErr_SetString(PyExc_TypeError,
"username value must be string");
295 if ((temp = PyDict_GetItemString(pyoptions,
"password")) != NULL)
297 if (PyString_Check(temp))
298 options.
username = PyString_AsString(temp);
301 PyErr_SetString(PyExc_TypeError,
"password value must be string");
308 return Py_BuildValue(
"i", rc);
317 if (!PyArg_ParseTuple(args,
"k|i", &c, &timeout))
321 return Py_BuildValue(
"i", rc);
329 if (!PyArg_ParseTuple(args,
"k", &c))
333 return Py_BuildValue(
"i", rc);
343 if (!PyArg_ParseTuple(args,
"ks|i", &c, &topic, &qos))
347 return Py_BuildValue(
"i", rc);
362 if (!PyArg_ParseTuple(args,
"kOO", &c, &topicList, &qosList))
365 if (!PySequence_Check(topicList) || !PySequence_Check(qosList))
367 PyErr_SetString(PyExc_TypeError,
368 "3rd and 4th parameters must be sequences");
372 if ((count = PySequence_Length(topicList)) != PySequence_Length(qosList))
374 PyErr_SetString(PyExc_TypeError,
375 "3rd and 4th parameters must be sequences of the same length");
379 topics =
malloc(count *
sizeof(
char*));
380 for (i = 0; i <
count; ++i)
381 topics[i] = PyString_AsString(PySequence_GetItem(topicList, i));
383 qoss =
malloc(count *
sizeof(
int));
384 for (i = 0; i <
count; ++i)
385 qoss[i] = (
int) PyInt_AsLong(PySequence_GetItem(qosList, i));
391 for (i = 0; i <
count; ++i)
392 PySequence_SetItem(qosList, i, PyInt_FromLong((
long) qoss[i]));
398 return Py_BuildValue(
"iO", rc, qosList);
400 return Py_BuildValue(
"i", rc);
409 if (!PyArg_ParseTuple(args,
"ks", &c, &topic))
413 return Py_BuildValue(
"i", rc);
426 if (!PyArg_ParseTuple(args,
"kOO", &c, &topicList))
429 if (!PySequence_Check(topicList))
431 PyErr_SetString(PyExc_TypeError,
"3rd parameter must be sequences");
435 count = PySequence_Length(topicList);
436 topics =
malloc(count *
sizeof(
char*));
437 for (i = 0; i <
count; ++i)
438 topics[i] = PyString_AsString(PySequence_GetItem(topicList, i));
445 return Py_BuildValue(
"i", rc);
459 if (!PyArg_ParseTuple(args,
"kss#|ii", &c, &topicName, &payload,
460 &payloadlen, &qos, &retained))
464 payload, qos, retained, &dt);
468 return Py_BuildValue(
"ii", rc, dt);
470 return Py_BuildValue(
"i", rc);
477 PyObject *message, *temp;
482 if (!PyArg_ParseTuple(args,
"ksO", &c, &topicName, &message))
485 if (!PyDict_Check(message))
487 PyErr_SetString(PyExc_TypeError,
"3rd parameter must be a dictionary");
491 if ((temp = PyDict_GetItemString(message,
"payload")) == NULL)
493 PyErr_SetString(PyExc_TypeError,
"dictionary must have payload key");
497 if (PyString_Check(temp))
498 PyString_AsStringAndSize(temp, (
char**) &msg.
payload,
502 PyErr_SetString(PyExc_TypeError,
"payload value must be string");
506 if ((temp = PyDict_GetItemString(message,
"qos")) == NULL)
507 msg.
qos = (int) PyInt_AsLong(temp);
509 if ((temp = PyDict_GetItemString(message,
"retained")) == NULL)
510 msg.
retained = (
int) PyInt_AsLong(temp);
517 return Py_BuildValue(
"ii", rc, dt);
519 return Py_BuildValue(
"i", rc);
525 unsigned long timeout = 1000L;
529 if (!PyArg_ParseTuple(args,
"ki|i", &c, &dt, &timeout))
535 return Py_BuildValue(
"i", rc);
544 if (!PyArg_ParseTuple(args,
"k", &c))
553 PyObject* dts = PyList_New(0);
555 while (tokens[i] != -1)
556 PyList_Append(dts, PyInt_FromLong((
long) tokens[i]));
558 return Py_BuildValue(
"iO", rc, dts);
561 return Py_BuildValue(
"i", rc);
566 if (!PyArg_ParseTuple(args,
""))
569 Py_BEGIN_ALLOW_THREADS
580 unsigned long timeout = 1000L;
582 PyObject* temp = NULL;
588 if (!PyArg_ParseTuple(args,
"k|k", &c, &timeout))
595 temp = Py_BuildValue(
"is#{ss#sisisisi}", rc, topicName, topicLen,
597 message->
qos,
"retained", message->
retained,
"dup",
598 message->
dup,
"msgid", message->
msgid);
603 temp = Py_BuildValue(
"iz", rc, NULL);
613 if (!PyArg_ParseTuple(args,
"k", &c))
630 {
"create",
mqttv3_create, METH_VARARGS,
"Create an MQTTv3 client." },
632 "Sets the callback functions for a particular client." },
634 "Connects to a server using the specified options." },
636 "Disconnects from a server." },
638 "Determines if this client is currently connected to the server." },
640 "Subscribe to the given topic." },
642 "Subscribe to the given topics." },
644 "Unsubscribe from the given topic." },
646 "Unsubscribe from the given topics." },
648 "Publish a message to the given topic." },
650 "Publish a message to the given topic." },
652 "Waits for the completion of the delivery of the message represented by a delivery token." },
655 "Returns the delivery tokens pending of completion." },
657 "Single-thread keep alive but don't receive message." },
659 "Single-thread receive message if available." },
661 "Free memory allocated to a MQTT client. It is the opposite to create." },
662 { NULL, NULL, 0, NULL }
669 PyEval_InitThreads();
677 MqttV3Error = PyErr_NewException(
"paho_mqtt3c.error", NULL, NULL);
691 PyModule_AddIntConstant(m,
"PERSISTENCE_ERROR",
int clientCompare(void *a, void *b)
int MQTTClient_waitForCompletion(MQTTClient handle, MQTTClient_deliveryToken mdt, unsigned long timeout)
#define MQTTCLIENT_FAILURE
int MQTTClient_receive(MQTTClient handle, char **topicName, int *topicLen, MQTTClient_message **message, unsigned long timeout)
#define MQTTCLIENT_SUCCESS
int MQTTClient_isConnected(MQTTClient handle)
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
int MQTTClient_publish(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTClient_deliveryToken *deliveryToken)
#define MQTTCLIENT_PERSISTENCE_ERROR
int MQTTClient_disconnect(MQTTClient handle, int timeout)
static PyObject * mqttv3_publishMessage(PyObject *self, PyObject *args)
#define MQTTCLIENT_PERSISTENCE_USER
static PyObject * mqttv3_isConnected(PyObject *self, PyObject *args)
#define MQTTCLIENT_MAX_MESSAGES_INFLIGHT
int ListDetach(List *aList, void *content)
#define MQTTClient_message_initializer
#define MQTTCLIENT_BAD_UTF8_STRING
static PyObject * mqttv3_create(PyObject *self, PyObject *args)
#define MQTTClient_willOptions_initializer
static PyObject * mqttv3_unsubscribe(PyObject *self, PyObject *args)
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
static PyObject * mqttv3_disconnect(PyObject *self, PyObject *args)
int MQTTClient_publishMessage(MQTTClient handle, const char *topicName, MQTTClient_message *message, MQTTClient_deliveryToken *deliveryToken)
static PyObject * mqttv3_setcallbacks(PyObject *self, PyObject *args)
int MQTTClient_unsubscribe(MQTTClient handle, const char *topic)
static PyObject * mqttv3_yield(PyObject *self, PyObject *args)
static PyObject * MqttV3Error
#define MQTTCLIENT_NULL_PARAMETER
void MQTTClient_freeMessage(MQTTClient_message **message)
static PyObject * mqttv3_unsubscribeMany(PyObject *self, PyObject *args)
ListElement * ListAppend(List *aList, void *content, size_t size)
void MQTTClient_yield(void)
static PyObject * mqttv3_receive(PyObject *self, PyObject *args)
void MQTTClient_destroy(MQTTClient *handle)
void connectionLost(void *context, char *cause)
#define MQTTCLIENT_PERSISTENCE_DEFAULT
static PyObject * mqttv3_waitForCompletion(PyObject *self, PyObject *args)
static PyObject * mqttv3_publish(PyObject *self, PyObject *args)
int MQTTClient_subscribe(MQTTClient handle, const char *topic, int qos)
static PyObject * mqttv3_connect(PyObject *self, PyObject *args)
static PyObject * mqttv3_subscribeMany(PyObject *self, PyObject *args)
void deliveryComplete(void *context, MQTTClient_deliveryToken dt)
static PyMethodDef MqttV3Methods[]
void MQTTClient_free(void *memory)
static PyObject * mqttv3_destroy(PyObject *self, PyObject *args)
#define MQTTCLIENT_PERSISTENCE_NONE
#define MQTTCLIENT_DISCONNECTED
#define MQTTClient_connectOptions_initializer
int MQTTClient_subscribeMany(MQTTClient handle, int count, char *const *topic, int *qos)
int MQTTClient_deliveryToken
int MQTTClient_getPendingDeliveryTokens(MQTTClient handle, MQTTClient_deliveryToken **tokens)
List * ListInitialize(void)
static PyObject * mqttv3_getPendingDeliveryTokens(PyObject *self, PyObject *args)
int MQTTClient_unsubscribeMany(MQTTClient handle, int count, char *const *topic)
#define MQTTCLIENT_TOPICNAME_TRUNCATED
ListElement * ListFindItem(List *aList, void *content, int(*callback)(void *, void *))
PyMODINIT_FUNC initpaho_mqtt3c(void)
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
MQTTClient_willOptions * will
int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
static PyObject * mqttv3_subscribe(PyObject *self, PyObject *args)