MQTTAsync_publish.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2023 IBM Corp., Ian Craggs
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  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://mqtt.eclipseprojects.io:1883"
  29. #define CLIENTID "ExampleClientPub"
  30. #define TOPIC "MQTT Examples"
  31. #define PAYLOAD "Hello World!"
  32. #define QOS 1
  33. #define TIMEOUT 10000L
  34. int finished = 0;
  35. void connlost(void *context, char *cause)
  36. {
  37. MQTTAsync client = (MQTTAsync)context;
  38. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  39. int rc;
  40. printf("\nConnection lost\n");
  41. if (cause)
  42. printf(" cause: %s\n", cause);
  43. printf("Reconnecting\n");
  44. conn_opts.keepAliveInterval = 20;
  45. conn_opts.cleansession = 1;
  46. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  47. {
  48. printf("Failed to start connect, return code %d\n", rc);
  49. finished = 1;
  50. }
  51. }
  52. void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
  53. {
  54. printf("Disconnect failed\n");
  55. finished = 1;
  56. }
  57. void onDisconnect(void* context, MQTTAsync_successData* response)
  58. {
  59. printf("Successful disconnection\n");
  60. finished = 1;
  61. }
  62. void onSendFailure(void* context, MQTTAsync_failureData* response)
  63. {
  64. MQTTAsync client = (MQTTAsync)context;
  65. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  66. int rc;
  67. printf("Message send failed token %d error code %d\n", response->token, response->code);
  68. opts.onSuccess = onDisconnect;
  69. opts.onFailure = onDisconnectFailure;
  70. opts.context = client;
  71. if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
  72. {
  73. printf("Failed to start disconnect, return code %d\n", rc);
  74. exit(EXIT_FAILURE);
  75. }
  76. }
  77. void onSend(void* context, MQTTAsync_successData* response)
  78. {
  79. MQTTAsync client = (MQTTAsync)context;
  80. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  81. int rc;
  82. printf("Message with token value %d delivery confirmed\n", response->token);
  83. opts.onSuccess = onDisconnect;
  84. opts.onFailure = onDisconnectFailure;
  85. opts.context = client;
  86. if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
  87. {
  88. printf("Failed to start disconnect, return code %d\n", rc);
  89. exit(EXIT_FAILURE);
  90. }
  91. }
  92. void onConnectFailure(void* context, MQTTAsync_failureData* response)
  93. {
  94. printf("Connect failed, rc %d\n", response ? response->code : 0);
  95. finished = 1;
  96. }
  97. void onConnect(void* context, MQTTAsync_successData* response)
  98. {
  99. MQTTAsync client = (MQTTAsync)context;
  100. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  101. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  102. int rc;
  103. printf("Successful connection\n");
  104. opts.onSuccess = onSend;
  105. opts.onFailure = onSendFailure;
  106. opts.context = client;
  107. pubmsg.payload = PAYLOAD;
  108. pubmsg.payloadlen = (int)strlen(PAYLOAD);
  109. pubmsg.qos = QOS;
  110. pubmsg.retained = 0;
  111. if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
  112. {
  113. printf("Failed to start sendMessage, return code %d\n", rc);
  114. exit(EXIT_FAILURE);
  115. }
  116. }
  117. int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
  118. {
  119. /* not expecting any messages */
  120. return 1;
  121. }
  122. int main(int argc, char* argv[])
  123. {
  124. MQTTAsync client;
  125. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  126. int rc;
  127. const char* uri = (argc > 1) ? argv[1] : ADDRESS;
  128. printf("Using server at %s\n", uri);
  129. if ((rc = MQTTAsync_create(&client, uri, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS)
  130. {
  131. printf("Failed to create client object, return code %d\n", rc);
  132. exit(EXIT_FAILURE);
  133. }
  134. if ((rc = MQTTAsync_setCallbacks(client, client, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
  135. {
  136. printf("Failed to set callback, return code %d\n", rc);
  137. exit(EXIT_FAILURE);
  138. }
  139. conn_opts.keepAliveInterval = 20;
  140. conn_opts.cleansession = 1;
  141. conn_opts.onSuccess = onConnect;
  142. conn_opts.onFailure = onConnectFailure;
  143. conn_opts.context = client;
  144. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  145. {
  146. printf("Failed to start connect, return code %d\n", rc);
  147. exit(EXIT_FAILURE);
  148. }
  149. printf("Waiting for publication of %s\n"
  150. "on topic %s for client with ClientID: %s\n",
  151. PAYLOAD, TOPIC, CLIENTID);
  152. while (!finished)
  153. #if defined(_WIN32)
  154. Sleep(100);
  155. #else
  156. usleep(10000L);
  157. #endif
  158. MQTTAsync_destroy(&client);
  159. return rc;
  160. }