MQTTAsync_subscribe.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2018 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial contribution
  15. *******************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "MQTTAsync.h"
  20. #if !defined(WIN32)
  21. #include <unistd.h>
  22. #else
  23. #include <windows.h>
  24. #endif
  25. #if defined(_WRS_KERNEL)
  26. #include <OsWrapper.h>
  27. #endif
  28. #define ADDRESS "tcp://localhost:1883"
  29. #define CLIENTID "ExampleClientSub"
  30. #define TOPIC "MQTT Examples"
  31. #define PAYLOAD "Hello World!"
  32. #define QOS 1
  33. #define TIMEOUT 10000L
  34. volatile MQTTAsync_token deliveredtoken;
  35. int disc_finished = 0;
  36. int subscribed = 0;
  37. int finished = 0;
  38. void connlost(void *context, char *cause)
  39. {
  40. MQTTAsync client = (MQTTAsync)context;
  41. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  42. int rc;
  43. printf("\nConnection lost\n");
  44. if (cause)
  45. printf(" cause: %s\n", cause);
  46. printf("Reconnecting\n");
  47. conn_opts.keepAliveInterval = 20;
  48. conn_opts.cleansession = 1;
  49. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  50. {
  51. printf("Failed to start connect, return code %d\n", rc);
  52. finished = 1;
  53. }
  54. }
  55. int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
  56. {
  57. int i;
  58. char* payloadptr;
  59. printf("Message arrived\n");
  60. printf(" topic: %s\n", topicName);
  61. printf(" message: ");
  62. payloadptr = message->payload;
  63. for(i=0; i<message->payloadlen; i++)
  64. {
  65. putchar(*payloadptr++);
  66. }
  67. putchar('\n');
  68. MQTTAsync_freeMessage(&message);
  69. MQTTAsync_free(topicName);
  70. return 1;
  71. }
  72. void onDisconnect(void* context, MQTTAsync_successData* response)
  73. {
  74. printf("Successful disconnection\n");
  75. disc_finished = 1;
  76. }
  77. void onSubscribe(void* context, MQTTAsync_successData* response)
  78. {
  79. printf("Subscribe succeeded\n");
  80. subscribed = 1;
  81. }
  82. void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
  83. {
  84. printf("Subscribe failed, rc %d\n", response ? response->code : 0);
  85. finished = 1;
  86. }
  87. void onConnectFailure(void* context, MQTTAsync_failureData* response)
  88. {
  89. printf("Connect failed, rc %d\n", response ? response->code : 0);
  90. finished = 1;
  91. }
  92. void onConnect(void* context, MQTTAsync_successData* response)
  93. {
  94. MQTTAsync client = (MQTTAsync)context;
  95. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  96. int rc;
  97. printf("Successful connection\n");
  98. printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
  99. "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
  100. opts.onSuccess = onSubscribe;
  101. opts.onFailure = onSubscribeFailure;
  102. opts.context = client;
  103. deliveredtoken = 0;
  104. if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
  105. {
  106. printf("Failed to start subscribe, return code %d\n", rc);
  107. exit(EXIT_FAILURE);
  108. }
  109. }
  110. int main(int argc, char* argv[])
  111. {
  112. MQTTAsync client;
  113. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  114. MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
  115. int rc;
  116. int ch;
  117. MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  118. MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL);
  119. conn_opts.keepAliveInterval = 20;
  120. conn_opts.cleansession = 1;
  121. conn_opts.onSuccess = onConnect;
  122. conn_opts.onFailure = onConnectFailure;
  123. conn_opts.context = client;
  124. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  125. {
  126. printf("Failed to start connect, return code %d\n", rc);
  127. exit(EXIT_FAILURE);
  128. }
  129. while (!subscribed)
  130. #if defined(WIN32)
  131. Sleep(100);
  132. #else
  133. usleep(10000L);
  134. #endif
  135. if (finished)
  136. goto exit;
  137. do
  138. {
  139. ch = getchar();
  140. } while (ch!='Q' && ch != 'q');
  141. disc_opts.onSuccess = onDisconnect;
  142. if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
  143. {
  144. printf("Failed to start disconnect, return code %d\n", rc);
  145. exit(EXIT_FAILURE);
  146. }
  147. while (!disc_finished)
  148. #if defined(WIN32)
  149. Sleep(100);
  150. #else
  151. usleep(10000L);
  152. #endif
  153. exit:
  154. MQTTAsync_destroy(&client);
  155. return rc;
  156. }