paho_cs_sub.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2018 IBM Corp., and others
  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. * Ian Craggs - change delimiter option from char to string
  16. * Guilherme Maciel Ferreira - add keep alive option
  17. * Ian Craggs - add full capability
  18. *******************************************************************************/
  19. #include "MQTTClient.h"
  20. #include "MQTTClientPersistence.h"
  21. #include "pubsub_opts.h"
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #if defined(WIN32)
  27. #define sleep Sleep
  28. #else
  29. #include <sys/time.h>
  30. #endif
  31. volatile int toStop = 0;
  32. struct pubsub_opts opts =
  33. {
  34. 0, 0, 0, 0, "\n", 100, /* debug/app options */
  35. NULL, NULL, 1, 0, 0, /* message options */
  36. MQTTVERSION_DEFAULT, NULL, "paho-cs-sub", 0, 0, NULL, NULL, "localhost", "1883", NULL, 10, /* MQTT options */
  37. NULL, NULL, 0, 0, /* will options */
  38. 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* TLS options */
  39. 0, {NULL, NULL}, /* MQTT V5 options */
  40. };
  41. int myconnect(MQTTClient* client)
  42. {
  43. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  44. MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
  45. MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;
  46. int rc = 0;
  47. if (opts.verbose)
  48. printf("Connecting\n");
  49. if (opts.MQTTVersion == MQTTVERSION_5)
  50. {
  51. MQTTClient_connectOptions conn_opts5 = MQTTClient_connectOptions_initializer5;
  52. conn_opts = conn_opts5;
  53. }
  54. conn_opts.keepAliveInterval = opts.keepalive;
  55. conn_opts.username = opts.username;
  56. conn_opts.password = opts.password;
  57. conn_opts.MQTTVersion = opts.MQTTVersion;
  58. if (opts.will_topic) /* will options */
  59. {
  60. will_opts.message = opts.will_payload;
  61. will_opts.topicName = opts.will_topic;
  62. will_opts.qos = opts.will_qos;
  63. will_opts.retained = opts.will_retain;
  64. conn_opts.will = &will_opts;
  65. }
  66. if (opts.connection && (strncmp(opts.connection, "ssl://", 6) == 0 ||
  67. strncmp(opts.connection, "wss://", 6) == 0))
  68. {
  69. if (opts.insecure)
  70. ssl_opts.verify = 0;
  71. else
  72. ssl_opts.verify = 1;
  73. ssl_opts.CApath = opts.capath;
  74. ssl_opts.keyStore = opts.cert;
  75. ssl_opts.trustStore = opts.cafile;
  76. ssl_opts.privateKey = opts.key;
  77. ssl_opts.privateKeyPassword = opts.keypass;
  78. ssl_opts.enabledCipherSuites = opts.ciphers;
  79. conn_opts.ssl = &ssl_opts;
  80. }
  81. if (opts.MQTTVersion == MQTTVERSION_5)
  82. {
  83. MQTTProperties props = MQTTProperties_initializer;
  84. MQTTProperties willProps = MQTTProperties_initializer;
  85. MQTTResponse response = MQTTResponse_initializer;
  86. conn_opts.cleanstart = 1;
  87. response = MQTTClient_connect5(client, &conn_opts, &props, &willProps);
  88. rc = response.reasonCode;
  89. MQTTResponse_free(response);
  90. }
  91. else
  92. {
  93. conn_opts.cleansession = 1;
  94. rc = MQTTClient_connect(client, &conn_opts);
  95. }
  96. if (opts.verbose && rc == MQTTCLIENT_SUCCESS)
  97. fprintf(stderr, "Connected\n");
  98. else if (rc != MQTTCLIENT_SUCCESS && !opts.quiet)
  99. fprintf(stderr, "Connect failed return code: %s\n", MQTTClient_strerror(rc));
  100. return rc;
  101. }
  102. void cfinish(int sig)
  103. {
  104. signal(SIGINT, NULL);
  105. toStop = 1;
  106. }
  107. void trace_callback(enum MQTTCLIENT_TRACE_LEVELS level, char* message)
  108. {
  109. fprintf(stderr, "Trace : %d, %s\n", level, message);
  110. }
  111. int main(int argc, char** argv)
  112. {
  113. MQTTClient client;
  114. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  115. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  116. int rc = 0;
  117. char* url;
  118. const char* version = NULL;
  119. #if !defined(WIN32)
  120. struct sigaction sa;
  121. #endif
  122. const char* program_name = "paho_cs_sub";
  123. MQTTClient_nameValue* infos = MQTTClient_getVersionInfo();
  124. if (argc < 2)
  125. usage(&opts, (pubsub_opts_nameValue*)infos, program_name);
  126. if (getopts(argc, argv, &opts) != 0)
  127. usage(&opts, (pubsub_opts_nameValue*)infos, program_name);
  128. if (strchr(opts.topic, '#') || strchr(opts.topic, '+'))
  129. opts.verbose = 1;
  130. if (opts.connection)
  131. url = opts.connection;
  132. else
  133. {
  134. url = malloc(100);
  135. sprintf(url, "%s:%s", opts.host, opts.port);
  136. }
  137. if (opts.verbose)
  138. printf("URL is %s\n", url);
  139. if (opts.tracelevel > 0)
  140. {
  141. MQTTClient_setTraceCallback(trace_callback);
  142. MQTTClient_setTraceLevel(opts.tracelevel);
  143. }
  144. if (opts.MQTTVersion >= MQTTVERSION_5)
  145. createOpts.MQTTVersion = MQTTVERSION_5;
  146. rc = MQTTClient_createWithOptions(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE,
  147. NULL, &createOpts);
  148. if (rc != MQTTCLIENT_SUCCESS)
  149. {
  150. if (!opts.quiet)
  151. fprintf(stderr, "Failed to create client, return code: %s\n", MQTTClient_strerror(rc));
  152. exit(EXIT_FAILURE);
  153. }
  154. #if defined(WIN32)
  155. signal(SIGINT, cfinish);
  156. signal(SIGTERM, cfinish);
  157. #else
  158. memset(&sa, 0, sizeof(struct sigaction));
  159. sa.sa_handler = cfinish;
  160. sa.sa_flags = 0;
  161. sigaction(SIGINT, &sa, NULL);
  162. sigaction(SIGTERM, &sa, NULL);
  163. #endif
  164. if (myconnect(client) != MQTTCLIENT_SUCCESS)
  165. goto exit;
  166. if (opts.MQTTVersion >= MQTTVERSION_5)
  167. {
  168. MQTTResponse response = MQTTClient_subscribe5(client, opts.topic, opts.qos, NULL, NULL);
  169. rc = response.reasonCode;
  170. MQTTResponse_free(response);
  171. }
  172. else
  173. rc = MQTTClient_subscribe(client, opts.topic, opts.qos);
  174. if (rc != MQTTCLIENT_SUCCESS && rc != opts.qos)
  175. {
  176. if (!opts.quiet)
  177. fprintf(stderr, "Error %d subscribing to topic %s\n", rc, opts.topic);
  178. goto exit;
  179. }
  180. while (!toStop)
  181. {
  182. char* topicName = NULL;
  183. int topicLen;
  184. MQTTClient_message* message = NULL;
  185. rc = MQTTClient_receive(client, &topicName, &topicLen, &message, 1000);
  186. if (message)
  187. {
  188. size_t delimlen = 0;
  189. if (opts.verbose)
  190. printf("%s\t", topicName);
  191. if (opts.delimiter)
  192. delimlen = strlen(opts.delimiter);
  193. if (opts.delimiter == NULL || (message->payloadlen > delimlen &&
  194. strncmp(opts.delimiter, &((char*)message->payload)[message->payloadlen - delimlen], delimlen) == 0))
  195. printf("%.*s", message->payloadlen, (char*)message->payload);
  196. else
  197. printf("%.*s%s", message->payloadlen, (char*)message->payload, opts.delimiter);
  198. if (message->struct_version == 1 && opts.verbose)
  199. logProperties(&message->properties);
  200. fflush(stdout);
  201. MQTTClient_freeMessage(&message);
  202. MQTTClient_free(topicName);
  203. }
  204. if (rc != 0)
  205. myconnect(&client);
  206. }
  207. exit:
  208. MQTTClient_disconnect(client, 0);
  209. MQTTClient_destroy(&client);
  210. return EXIT_SUCCESS;
  211. }