int MQTTClient_subscribe(MQTTClient handle, char* topic, int qos) { int rc = 0; FUNC_ENTRY; rc = MQTTClient_subscribeMany(handle, 1, &topic, &qos); FUNC_EXIT_RC(rc); return rc; }
int MQTTClient_subscribe(MQTTClient handle, const char* topic, int qos) { int rc = 0; char *const topics[] = {(char*)topic}; FUNC_ENTRY; rc = MQTTClient_subscribeMany(handle, 1, topics, &qos); if (qos == MQTT_BAD_SUBSCRIBE) /* addition for MQTT 3.1.1 - error code from subscribe */ rc = MQTT_BAD_SUBSCRIBE; FUNC_EXIT_RC(rc); return rc; }
static PyObject* mqttv3_subscribeMany(PyObject* self, PyObject *args) { MQTTClient c; PyObject* topicList; PyObject* qosList; int count; char** topics; int* qoss; int i, rc = 0; if (!PyArg_ParseTuple(args, "kOO", &c, &topicList, &qosList)) return NULL; if (!PySequence_Check(topicList) || !PySequence_Check(qosList)) { PyErr_SetString(PyExc_TypeError, "3rd and 4th parameters must be sequences"); return NULL; } if ((count = PySequence_Length(topicList)) != PySequence_Length(qosList)) { PyErr_SetString(PyExc_TypeError, "3rd and 4th parameters must be sequences of the same length"); return NULL; } topics = malloc(count * sizeof(char*)); for (i = 0; i < count; ++i) topics[i] = PyString_AsString(PySequence_GetItem(topicList, i)); qoss = malloc(count * sizeof(int)); for (i = 0; i < count; ++i) qoss[i] = (int) PyInt_AsLong(PySequence_GetItem(qosList, i)); Py_BEGIN_ALLOW_THREADS rc = MQTTClient_subscribeMany(c, count, topics, qoss); Py_END_ALLOW_THREADS for (i = 0; i < count; ++i) PySequence_SetItem(qosList, i, PyInt_FromLong((long) qoss[i])); free(topics); free(qoss); if (rc == MQTTCLIENT_SUCCESS) return Py_BuildValue("iO", rc, qosList); else return Py_BuildValue("i", rc); }
int test2(struct Options options) { char* testname = "test2"; int subsqos = 2; MQTTClient c; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; int rc = 0; char* test_topic = "C client test2"; char* topics[2] = {"test_topic", "nosubscribe"}; int qoss[2] = {2, 2}; fprintf(xml, "<testcase classname=\"test1\" name=\"bad return code from subscribe\""); MyLog(LOGA_INFO, "Starting test 2 - bad return code from subscribe"); global_start_time = start_clock(); failures = 0; MQTTClient_create(&c, options.connection, "multi_threaded_sample", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); opts.keepAliveInterval = 20; opts.cleansession = 1; opts.MQTTVersion = 4; if (options.haconnections != NULL) { opts.serverURIs = options.haconnections; opts.serverURIcount = options.hacount; } rc = MQTTClient_setCallbacks(c, NULL, NULL, test2_messageArrived, test2_deliveryComplete); assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTClient_connect(c, &opts); assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); if (rc != MQTTCLIENT_SUCCESS) goto exit; assert("Correct serverURI returned", strcmp(opts.returned.serverURI, options.connection) == 0, "serverURI was %s", opts.returned.serverURI); assert("Correct MQTTVersion returned", opts.returned.MQTTVersion == 4, "MQTTVersion was %d", opts.returned.MQTTVersion); assert("Correct sessionPresent returned", opts.returned.sessionPresent == 0, "sessionPresent was %d", opts.returned.sessionPresent); rc = MQTTClient_subscribe(c, test_topic, subsqos); assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); rc = MQTTClient_subscribe(c, "nosubscribe", 2); assert("0x80 from subscribe", rc == 0x80, "rc was %d", rc); rc = MQTTClient_subscribeMany(c, 2, topics, qoss); assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); assert("Correct returned qos from subscribe", qoss[0] == 2, "qos 0 was %d", qoss[0]); assert("Correct returned qos from subscribe", qoss[1] == 0x80, "qos 0 was %d", qoss[0]); rc = MQTTClient_unsubscribe(c, test_topic); assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); rc = MQTTClient_disconnect(c, 0); assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); MQTTClient_destroy(&c); exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }