diff --git a/MQTTClient-C/samples/linux/stdoutsub.c b/MQTTClient-C/samples/linux/stdoutsub.c index f57506e7..992cee4e 100644 --- a/MQTTClient-C/samples/linux/stdoutsub.c +++ b/MQTTClient-C/samples/linux/stdoutsub.c @@ -219,7 +219,7 @@ int main(int argc, char** argv) NetworkInit(&n); NetworkConnect(&n, opts.host, opts.port); MQTTClientInit(&c, &n, 1000, buf, 100, readbuf, 100); - + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; data.MQTTVersion = 3; @@ -230,11 +230,11 @@ int main(int argc, char** argv) data.keepAliveInterval = 10; data.cleansession = 1; printf("Connecting to %s %d\n", opts.host, opts.port); - + rc = MQTTConnect(&c, &data); printf("Connected %d\n", rc); - - printf("Subscribing to %s\n", topic); + + printf("Subscribing to %s\n", topic); rc = MQTTSubscribe(&c, topic, opts.qos, messageArrived); printf("Subscribed %d\n", rc); @@ -242,7 +242,7 @@ int main(int argc, char** argv) { MQTTYield(&c, 1000); } - + printf("Stopping\n"); MQTTDisconnect(&c); diff --git a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c index 220c7f35..b74e6483 100755 --- a/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c +++ b/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c @@ -169,7 +169,7 @@ int NetworkConnect(Network* n, char* addr, int port) if ((retVal = FreeRTOS_connect(n->my_socket, &sAddr, sizeof(sAddr))) < 0) { FreeRTOS_closesocket(n->my_socket); - goto exit; + goto exit; } exit: diff --git a/MQTTClient-C/src/MQTTClient.c b/MQTTClient-C/src/MQTTClient.c index bd24dff0..5ac3f9d0 100755 --- a/MQTTClient-C/src/MQTTClient.c +++ b/MQTTClient-C/src/MQTTClient.c @@ -71,11 +71,11 @@ void MQTTClientInit(MQTTClient* c, Network* network, unsigned int command_timeou c->cleansession = 0; c->ping_outstanding = 0; c->defaultMessageHandler = NULL; - c->next_packetid = 1; + c->next_packetid = 1; TimerInit(&c->last_sent); TimerInit(&c->last_received); #if defined(MQTT_TASK) - MutexInit(&c->mutex); + MutexInit(&c->mutex); #endif } @@ -346,21 +346,21 @@ int MQTTYield(MQTTClient* c, int timeout_ms) TimerInit(&timer); TimerCountdownMS(&timer, timeout_ms); - do + do { if (cycle(c, &timer) < 0) { rc = FAILURE; break; } - } while (!TimerIsExpired(&timer)); + } while (!TimerIsExpired(&timer)); return rc; } int MQTTIsConnected(MQTTClient* client) { - return client->isconnected; + return client->isconnected; } void MQTTRun(void* parm) @@ -418,10 +418,10 @@ int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTC int len = 0; #if defined(MQTT_TASK) - MutexLock(&c->mutex); + MutexLock(&c->mutex); #endif - if (c->isconnected) /* don't send connect packet again if we are already connected */ - goto exit; + if (c->isconnected) /* don't send connect packet again if we are already connected */ + goto exit; TimerInit(&connect_timer); TimerCountdownMS(&connect_timer, c->command_timeout_ms); @@ -458,7 +458,7 @@ int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTC } #if defined(MQTT_TASK) - MutexUnlock(&c->mutex); + MutexUnlock(&c->mutex); #endif return rc; @@ -524,10 +524,10 @@ int MQTTSubscribeWithResults(MQTTClient* c, const char* topicFilter, enum QoS qo topic.cstring = (char *)topicFilter; #if defined(MQTT_TASK) - MutexLock(&c->mutex); + MutexLock(&c->mutex); #endif - if (!c->isconnected) - goto exit; + if (!c->isconnected) + goto exit; TimerInit(&timer); TimerCountdownMS(&timer, c->command_timeout_ms); @@ -556,7 +556,7 @@ int MQTTSubscribeWithResults(MQTTClient* c, const char* topicFilter, enum QoS qo if (rc == FAILURE) MQTTCloseSession(c); #if defined(MQTT_TASK) - MutexUnlock(&c->mutex); + MutexUnlock(&c->mutex); #endif return rc; } @@ -579,10 +579,10 @@ int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter) int len = 0; #if defined(MQTT_TASK) - MutexLock(&c->mutex); + MutexLock(&c->mutex); #endif - if (!c->isconnected) - goto exit; + if (!c->isconnected) + goto exit; TimerInit(&timer); TimerCountdownMS(&timer, c->command_timeout_ms); @@ -608,7 +608,7 @@ int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter) if (rc == FAILURE) MQTTCloseSession(c); #if defined(MQTT_TASK) - MutexUnlock(&c->mutex); + MutexUnlock(&c->mutex); #endif return rc; } @@ -623,10 +623,10 @@ int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message) int len = 0; #if defined(MQTT_TASK) - MutexLock(&c->mutex); + MutexLock(&c->mutex); #endif - if (!c->isconnected) - goto exit; + if (!c->isconnected) + goto exit; TimerInit(&timer); TimerCountdownMS(&timer, c->command_timeout_ms); @@ -670,7 +670,7 @@ int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message) if (rc == FAILURE) MQTTCloseSession(c); #if defined(MQTT_TASK) - MutexUnlock(&c->mutex); + MutexUnlock(&c->mutex); #endif return rc; } @@ -688,13 +688,13 @@ int MQTTDisconnect(MQTTClient* c) TimerInit(&timer); TimerCountdownMS(&timer, c->command_timeout_ms); - len = MQTTSerialize_disconnect(c->buf, c->buf_size); + len = MQTTSerialize_disconnect(c->buf, c->buf_size); if (len > 0) rc = sendPacket(c, len, &timer); // send the disconnect packet MQTTCloseSession(c); #if defined(MQTT_TASK) - MutexUnlock(&c->mutex); + MutexUnlock(&c->mutex); #endif return rc; } diff --git a/MQTTClient-C/src/cc3200/MQTTCC3200.c b/MQTTClient-C/src/cc3200/MQTTCC3200.c index ef798cc5..ae07bc0b 100644 --- a/MQTTClient-C/src/cc3200/MQTTCC3200.c +++ b/MQTTClient-C/src/cc3200/MQTTCC3200.c @@ -185,7 +185,7 @@ int ConnectNetwork(Network* n, char* addr, int port) if( retVal < 0 ) { // error sl_Close(n->my_socket); - return retVal; + return retVal; } SysTickIntRegister(SysTickIntHandler); diff --git a/MQTTClient-C/src/linux/MQTTLinux.c b/MQTTClient-C/src/linux/MQTTLinux.c index 7cd00845..51633efd 100644 --- a/MQTTClient-C/src/linux/MQTTLinux.c +++ b/MQTTClient-C/src/linux/MQTTLinux.c @@ -77,7 +77,7 @@ int linux_read(Network* n, unsigned char* buffer, int len, int timeout_ms) if (rc == -1) { if (errno != EAGAIN && errno != EWOULDBLOCK) - bytes = -1; + bytes = -1; break; } else if (rc == 0) diff --git a/MQTTClient-C/test/test1.c b/MQTTClient-C/test/test1.c index 5a998992..84e78137 100644 --- a/MQTTClient-C/test/test1.c +++ b/MQTTClient-C/test/test1.c @@ -46,9 +46,9 @@ void usage(void) struct Options { char* host; /**< connection to system under test. */ - int port; + int port; char* proxy_host; - int proxy_port; + int proxy_port; int verbose; int test_no; int MQTTVersion; @@ -56,7 +56,7 @@ struct Options } options = { "localhost", - 1883, + 1883, "localhost", 1885, 0, //verbose @@ -88,36 +88,36 @@ void getopts(int argc, char** argv) else usage(); } - else if (strcmp(argv[count], "--port") == 0) - { - if (++count < argc) - { - options.port = atoi(argv[count]); - printf("\nSetting port to %d\n", options.port); - } - else - usage(); - } + else if (strcmp(argv[count], "--port") == 0) + { + if (++count < argc) + { + options.port = atoi(argv[count]); + printf("\nSetting port to %d\n", options.port); + } + else + usage(); + } else if (strcmp(argv[count], "--proxy_host") == 0) { if (++count < argc) - { + { options.proxy_host = argv[count]; - printf("\nSetting proxy_host to %s\n", options.proxy_host); - } + printf("\nSetting proxy_host to %s\n", options.proxy_host); + } + else + usage(); + } + else if (strcmp(argv[count], "--proxy_port") == 0) + { + if (++count < argc) + { + options.proxy_port = atoi(argv[count]); + printf("\nSetting proxy_port to %d\n", options.proxy_port); + } else usage(); } - else if (strcmp(argv[count], "--proxy_port") == 0) - { - if (++count < argc) - { - options.proxy_port = atoi(argv[count]); - printf("\nSetting proxy_port to %d\n", options.proxy_port); - } - else - usage(); - } else if (strcmp(argv[count], "--MQTTversion") == 0) { if (++count < argc) @@ -159,7 +159,7 @@ void MyLog(int LOGA_level, char* format, ...) struct tm *timeinfo; if (LOGA_level == LOGA_DEBUG && options.verbose == 0) - return; + return; ftime(&ts); timeinfo = localtime(&ts.time); @@ -285,15 +285,15 @@ static MQTTMessage pubmsg; void messageArrived(MessageData* md) { - test1_message_data = md; - MQTTMessage* m = md->message; + test1_message_data = md; + MQTTMessage *m = md->message; assert("Good message lengths", pubmsg.payloadlen == m->payloadlen, - "payloadlen was %d", m->payloadlen); + "payloadlen was %d", m->payloadlen); - if (pubmsg.payloadlen == m->payloadlen) - assert("Good message contents", memcmp(m->payload, pubmsg.payload, m->payloadlen) == 0, - "payload was %s", m->payload); + if (pubmsg.payloadlen == m->payloadlen) + assert("Good message contents", memcmp(m->payload, pubmsg.payload, m->payloadlen) == 0, + "payload was %s", m->payload); } @@ -309,15 +309,15 @@ void test1_sendAndReceive(MQTTClient* c, int qos, char* test_topic) int i = 0; int iterations = 50; int rc; - int wait_seconds; + int wait_seconds; MyLog(LOGA_DEBUG, "%d messages at QoS %d", iterations, qos); - memset(&pubmsg, '\0', sizeof(pubmsg)); + memset(&pubmsg, '\0', sizeof(pubmsg)); pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11"; pubmsg.payloadlen = 11; pubmsg.qos = qos; pubmsg.retained = 0; - pubmsg.dup = 0; + pubmsg.dup = 0; for (i = 0; i < iterations; ++i) { @@ -329,7 +329,7 @@ void test1_sendAndReceive(MQTTClient* c, int qos, char* test_topic) wait_seconds = 10; while ((test1_message_data == NULL) && (wait_seconds-- > 0)) { - MQTTYield(c, 100); + MQTTYield(c, 100); } assert("Message Arrived", wait_seconds > 0, "Time out waiting for message %d\n", i); @@ -338,44 +338,44 @@ void test1_sendAndReceive(MQTTClient* c, int qos, char* test_topic) } /* wait to receive any outstanding messages */ - wait_seconds = 2; - while (wait_seconds-- > 0) - { - MQTTYield(c, 1000); - } + wait_seconds = 2; + while (wait_seconds-- > 0) + { + MQTTYield(c, 1000); + } } int test1(struct Options options) { int subsqos = 2; - Network n; + Network n; MQTTClient c; int rc = 0; char* test_topic = "C client test1"; - MQTTPacket_willOptions wopts; - unsigned char buf[100]; - unsigned char readbuf[100]; + MQTTPacket_willOptions wopts; + unsigned char buf[100]; + unsigned char readbuf[100]; - printf("test1\n"); + printf("test1\n"); fprintf(xml, "message; + MQTTMessage *m = md->message; assert("Good message lengths", pubmsg.payloadlen == m->payloadlen, - "payloadlen was %d", m->payloadlen); + "payloadlen was %d", m->payloadlen); if (pubmsg.payloadlen == m->payloadlen) assert("Good message contents", memcmp(m->payload, pubmsg.payload, m->payloadlen) == 0, - "payload was %s", m->payload); + "payload was %s", m->payload); } @@ -583,185 +583,185 @@ int check_subs_exist(MQTTClient* c, const char* test_topic, int which) int test3(struct Options options) { - enum QoS subsqos = QOS2; - Network n; - MQTTClient c; - int rc; - const char* test_topic = "C client test3"; - int wait_seconds = 0; - unsigned char buf[100]; - unsigned char readbuf[100]; - MQTTConnackData connack; - MQTTSubackData suback; - - fprintf(xml, " 0)) - { - MQTTYield(&c, 1000); - } - assert("Disconnected", !MQTTIsConnected(&c), "isConnected was %d", - MQTTIsConnected(&c)); - NetworkDisconnect(&n); - - /* reconnect with cleansession false */ - data.cleansession = 0; - rc = NetworkConnect(&n, options.host, options.port); - assert("TCP connect successful", rc == SUCCESS, "rc was %d", rc); - rc = MQTTConnectWithResults(&c, &data, &connack); - assert("Connect successful", rc == SUCCESS, "rc was %d", rc); - - assert("Good rc in connack", connack.rc == 0, "rc was %d", connack.rc); - assert("Session present is 1", connack.sessionPresent == 1, - "sessionPresent was %d", connack.sessionPresent); + check_subs_exist(&c, test_topic, 1); - check_subs_exist(&c, test_topic, 1); + // cause a connection FAILURE + memset(&pubmsg, '\0', sizeof(pubmsg)); + pubmsg.payload = (void*)"TERMINATE"; + pubmsg.payloadlen = strlen((char*)pubmsg.payload); + pubmsg.qos = QOS0; + pubmsg.retained = 0; + pubmsg.dup = 0; + rc = MQTTPublish(&c, "MQTTSAS topic", &pubmsg); + assert("Good rc from publish", rc == SUCCESS, "rc was %d", rc); - rc = MQTTSubscribeWithResults(&c, test_topic, subsqos, messageArrived2, &suback); - assert("Good rc from subscribe", rc == SUCCESS, "rc was %d", rc); - assert("Granted QoS rc from subscribe", suback.grantedQoS == QOS2, - "rc was %d", suback.grantedQoS); + // wait for failure to be noticed by keepalive + wait_seconds = 20; + while (MQTTIsConnected(&c) && (wait_seconds-- > 0)) + { + MQTTYield(&c, 1000); + } + assert("Disconnected", !MQTTIsConnected(&c), "isConnected was %d", + MQTTIsConnected(&c)); + NetworkDisconnect(&n); + + /* reconnect with cleansession false */ + data.cleansession = 0; + rc = NetworkConnect(&n, options.host, options.port); + assert("TCP connect successful", rc == SUCCESS, "rc was %d", rc); + rc = MQTTConnectWithResults(&c, &data, &connack); + assert("Connect successful", rc == SUCCESS, "rc was %d", rc); + + assert("Good rc in connack", connack.rc == 0, "rc was %d", connack.rc); + assert("Session present is 1", connack.sessionPresent == 1, + "sessionPresent was %d", connack.sessionPresent); + + check_subs_exist(&c, test_topic, 1); + + rc = MQTTSubscribeWithResults(&c, test_topic, subsqos, messageArrived2, &suback); + assert("Good rc from subscribe", rc == SUCCESS, "rc was %d", rc); + assert("Granted QoS rc from subscribe", suback.grantedQoS == QOS2, + "rc was %d", suback.grantedQoS); - check_subs_exist(&c, test_topic, 2); + check_subs_exist(&c, test_topic, 2); - rc = MQTTDisconnect(&c); - assert("Disconnect successful", rc == SUCCESS, "rc was %d", rc); - NetworkDisconnect(&n); + rc = MQTTDisconnect(&c); + assert("Disconnect successful", rc == SUCCESS, "rc was %d", rc); + NetworkDisconnect(&n); - /* reconnect with cleansession true to clean up both server and client state */ - data.cleansession = 1; - rc = NetworkConnect(&n, options.host, options.port); - assert("TCP connect successful", rc == SUCCESS, "rc was %d", rc); - rc = MQTTConnectWithResults(&c, &data, &connack); - assert("Connect successful", rc == SUCCESS, "rc was %d", rc); + /* reconnect with cleansession true to clean up both server and client state */ + data.cleansession = 1; + rc = NetworkConnect(&n, options.host, options.port); + assert("TCP connect successful", rc == SUCCESS, "rc was %d", rc); + rc = MQTTConnectWithResults(&c, &data, &connack); + assert("Connect successful", rc == SUCCESS, "rc was %d", rc); - assert("Good rc in connack", connack.rc == 0, "rc was %d", connack.rc); - assert("Session present is 0", connack.sessionPresent == 0, - "sessionPresent was %d", connack.sessionPresent); + assert("Good rc in connack", connack.rc == 0, "rc was %d", connack.rc); + assert("Session present is 0", connack.sessionPresent == 0, + "sessionPresent was %d", connack.sessionPresent); - rc = MQTTSubscribeWithResults(&c, test_topic, subsqos, messageArrived2, &suback); - assert("Good rc from subscribe", rc == SUCCESS, "rc was %d", rc); - assert("Granted QoS rc from subscribe", suback.grantedQoS == QOS2, - "rc was %d", suback.grantedQoS); + rc = MQTTSubscribeWithResults(&c, test_topic, subsqos, messageArrived2, &suback); + assert("Good rc from subscribe", rc == SUCCESS, "rc was %d", rc); + assert("Granted QoS rc from subscribe", suback.grantedQoS == QOS2, + "rc was %d", suback.grantedQoS); - check_subs_exist(&c, test_topic, 2); + check_subs_exist(&c, test_topic, 2); - rc = MQTTDisconnect(&c); - assert("Disconnect successful", rc == SUCCESS, "rc was %d", rc); - NetworkDisconnect(&n); + rc = MQTTDisconnect(&c); + assert("Disconnect successful", rc == SUCCESS, "rc was %d", rc); + NetworkDisconnect(&n); exit: - MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.", - (failures == 0) ? "passed" : "failed", tests, failures); - write_test_result(); - return failures; + MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.", + (failures == 0) ? "passed" : "failed", tests, failures); + write_test_result(); + return failures; } #if 0 @@ -793,7 +793,7 @@ int test6_messageArrived(void* context, char* topicName, int topicLen, MQTTClien { MQTTClient c = (MQTTClient)context; printf("%s -> Callback: message received on topic '%s' is '%.*s'.\n", - (c == test6_c1) ? "Client-1" : "Client-2", topicName, m->payloadlen, (char*)(m->payload)); + (c == test6_c1) ? "Client-1" : "Client-2", topicName, m->payloadlen, (char*)(m->payload)); if (c == test6_c2 && strcmp(topicName, test6_will_topic) == 0 && memcmp(m->payload, test6_will_message, m->payloadlen) == 0) test6_will_message_arrived = 1; MQTTClient_free(topicName); @@ -884,7 +884,7 @@ int test6(struct Options options) assert("will message arrived", test6_will_message_arrived == 1, "will_message_arrived was %d\n", test6_will_message_arrived); assert("connection lost called", test6_connection_lost_called == 1, - "connection_lost_called %d\n", test6_connection_lost_called); + "connection_lost_called %d\n", test6_connection_lost_called); rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic); assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); @@ -992,7 +992,7 @@ int test6a(struct Options options) assert("will message arrived", test6_will_message_arrived == 1, "will_message_arrived was %d\n", test6_will_message_arrived); assert("connection lost called", test6_connection_lost_called == 1, - "connection_lost_called %d\n", test6_connection_lost_called); + "connection_lost_called %d\n", test6_connection_lost_called); rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic); assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); @@ -1020,7 +1020,7 @@ int test6a(struct Options options) int main(int argc, char** argv) { int rc = 0; - int (*tests[])() = {NULL, test1, test2, test3}; + int (*tests[])() = {NULL, test1, test2, test3}; int i; xml = fopen("TEST-test1.xml", "w"); @@ -1030,16 +1030,16 @@ int main(int argc, char** argv) for (i = 0; i < options.iterations; ++i) { - if (options.test_no == 0) + if (options.test_no == 0) { /* run all the tests */ - for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no) + for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no) rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */ } else - rc = tests[options.test_no](options); /* run just the selected test */ + rc = tests[options.test_no](options); /* run just the selected test */ } - if (rc == 0) + if (rc == 0) MyLog(LOGA_INFO, "verdict pass"); else MyLog(LOGA_INFO, "verdict fail");