paho_cs_pub.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. * Ian Craggs - add full capability
  16. *******************************************************************************/
  17. #include "MQTTClient.h"
  18. #include "MQTTClientPersistence.h"
  19. #include "pubsub_opts.h"
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #if defined(WIN32)
  25. #define sleep Sleep
  26. #else
  27. #include <sys/time.h>
  28. #endif
  29. volatile int toStop = 0;
  30. void cfinish(int sig)
  31. {
  32. signal(SIGINT, NULL);
  33. toStop = 1;
  34. }
  35. struct pubsub_opts opts =
  36. {
  37. 1, 0, 0, 0, "\n", 100, /* debug/app options */
  38. NULL, NULL, 1, 0, 0, /* message options */
  39. MQTTVERSION_DEFAULT, NULL, "paho-cs-pub", 0, 0, NULL, NULL, "localhost", "1883", NULL, 10, /* MQTT options */
  40. NULL, NULL, 0, 0, /* will options */
  41. 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* TLS options */
  42. 0, {NULL, NULL}, /* MQTT V5 options */
  43. };
  44. int myconnect(MQTTClient* client)
  45. {
  46. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  47. MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
  48. MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;
  49. int rc = 0;
  50. if (opts.verbose)
  51. printf("Connecting\n");
  52. if (opts.MQTTVersion == MQTTVERSION_5)
  53. {
  54. MQTTClient_connectOptions conn_opts5 = MQTTClient_connectOptions_initializer5;
  55. conn_opts = conn_opts5;
  56. }
  57. conn_opts.keepAliveInterval = opts.keepalive;
  58. conn_opts.username = opts.username;
  59. conn_opts.password = opts.password;
  60. conn_opts.MQTTVersion = opts.MQTTVersion;
  61. if (opts.will_topic) /* will options */
  62. {
  63. will_opts.message = opts.will_payload;
  64. will_opts.topicName = opts.will_topic;
  65. will_opts.qos = opts.will_qos;
  66. will_opts.retained = opts.will_retain;
  67. conn_opts.will = &will_opts;
  68. }
  69. if (opts.connection && (strncmp(opts.connection, "ssl://", 6) == 0 ||
  70. strncmp(opts.connection, "wss://", 6) == 0))
  71. {
  72. if (opts.insecure)
  73. ssl_opts.verify = 0;
  74. else
  75. ssl_opts.verify = 1;
  76. ssl_opts.CApath = opts.capath;
  77. ssl_opts.keyStore = opts.cert;
  78. ssl_opts.trustStore = opts.cafile;
  79. ssl_opts.privateKey = opts.key;
  80. ssl_opts.privateKeyPassword = opts.keypass;
  81. ssl_opts.enabledCipherSuites = opts.ciphers;
  82. conn_opts.ssl = &ssl_opts;
  83. }
  84. if (opts.MQTTVersion == MQTTVERSION_5)
  85. {
  86. MQTTProperties props = MQTTProperties_initializer;
  87. MQTTProperties willProps = MQTTProperties_initializer;
  88. MQTTResponse response = MQTTResponse_initializer;
  89. conn_opts.cleanstart = 1;
  90. response = MQTTClient_connect5(client, &conn_opts, &props, &willProps);
  91. rc = response.reasonCode;
  92. }
  93. else
  94. {
  95. conn_opts.cleansession = 1;
  96. rc = MQTTClient_connect(client, &conn_opts);
  97. }
  98. if (opts.verbose && rc == MQTTCLIENT_SUCCESS)
  99. printf("Connected\n");
  100. else if (rc != MQTTCLIENT_SUCCESS && !opts.quiet)
  101. fprintf(stderr, "Connect failed return code: %s\n", MQTTClient_strerror(rc));
  102. return rc;
  103. }
  104. int messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  105. {
  106. /* not expecting any messages */
  107. return 1;
  108. }
  109. void trace_callback(enum MQTTCLIENT_TRACE_LEVELS level, char* message)
  110. {
  111. fprintf(stderr, "Trace : %d, %s\n", level, message);
  112. }
  113. int main(int argc, char** argv)
  114. {
  115. MQTTClient client;
  116. MQTTProperties pub_props = MQTTProperties_initializer;
  117. MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
  118. char* buffer = NULL;
  119. int rc = 0;
  120. char* url;
  121. const char* version = NULL;
  122. #if !defined(WIN32)
  123. struct sigaction sa;
  124. #endif
  125. const char* program_name = "paho_cs_pub";
  126. MQTTClient_nameValue* infos = MQTTClient_getVersionInfo();
  127. if (argc < 2)
  128. usage(&opts, (pubsub_opts_nameValue*)infos, program_name);
  129. if (getopts(argc, argv, &opts) != 0)
  130. usage(&opts, (pubsub_opts_nameValue*)infos, program_name);
  131. if (opts.connection)
  132. url = opts.connection;
  133. else
  134. {
  135. url = malloc(100);
  136. sprintf(url, "%s:%s", opts.host, opts.port);
  137. }
  138. if (opts.verbose)
  139. printf("URL is %s\n", url);
  140. if (opts.tracelevel > 0)
  141. {
  142. MQTTClient_setTraceCallback(trace_callback);
  143. MQTTClient_setTraceLevel(opts.tracelevel);
  144. }
  145. if (opts.MQTTVersion >= MQTTVERSION_5)
  146. createOpts.MQTTVersion = MQTTVERSION_5;
  147. rc = MQTTClient_createWithOptions(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE,
  148. NULL, &createOpts);
  149. if (rc != MQTTCLIENT_SUCCESS)
  150. {
  151. if (!opts.quiet)
  152. fprintf(stderr, "Failed to create client, return code: %s\n", MQTTClient_strerror(rc));
  153. exit(EXIT_FAILURE);
  154. }
  155. #if defined(WIN32)
  156. signal(SIGINT, cfinish);
  157. signal(SIGTERM, cfinish);
  158. #else
  159. memset(&sa, 0, sizeof(struct sigaction));
  160. sa.sa_handler = cfinish;
  161. sa.sa_flags = 0;
  162. sigaction(SIGINT, &sa, NULL);
  163. sigaction(SIGTERM, &sa, NULL);
  164. #endif
  165. rc = MQTTClient_setCallbacks(client, NULL, NULL, messageArrived, NULL);
  166. if (rc != MQTTCLIENT_SUCCESS)
  167. {
  168. if (!opts.quiet)
  169. fprintf(stderr, "Failed to set callbacks, return code: %s\n", MQTTClient_strerror(rc));
  170. exit(EXIT_FAILURE);
  171. }
  172. if (myconnect(client) != MQTTCLIENT_SUCCESS)
  173. goto exit;
  174. if (opts.MQTTVersion >= MQTTVERSION_5)
  175. {
  176. MQTTProperty property;
  177. if (opts.message_expiry > 0)
  178. {
  179. property.identifier = MQTTPROPERTY_CODE_MESSAGE_EXPIRY_INTERVAL;
  180. property.value.integer4 = opts.message_expiry;
  181. MQTTProperties_add(&pub_props, &property);
  182. }
  183. if (opts.user_property.name)
  184. {
  185. property.identifier = MQTTPROPERTY_CODE_USER_PROPERTY;
  186. property.value.data.data = opts.user_property.name;
  187. property.value.data.len = (int)strlen(opts.user_property.name);
  188. property.value.value.data = opts.user_property.value;
  189. property.value.value.len = (int)strlen(opts.user_property.value);
  190. MQTTProperties_add(&pub_props, &property);
  191. }
  192. }
  193. while (!toStop)
  194. {
  195. int data_len = 0;
  196. int delim_len = 0;
  197. if (opts.stdin_lines)
  198. {
  199. buffer = malloc(opts.maxdatalen);
  200. delim_len = (int)strlen(opts.delimiter);
  201. do
  202. {
  203. int c = getchar();
  204. if (c < 0)
  205. goto exit;
  206. buffer[data_len++] = c;
  207. if (data_len > delim_len)
  208. {
  209. if (strncmp(opts.delimiter, &buffer[data_len - delim_len], delim_len) == 0)
  210. break;
  211. }
  212. } while (data_len < opts.maxdatalen);
  213. }
  214. else if (opts.message)
  215. {
  216. buffer = opts.message;
  217. data_len = (int)strlen(opts.message);
  218. }
  219. else if (opts.filename)
  220. {
  221. buffer = readfile(&data_len, &opts);
  222. if (buffer == NULL)
  223. goto exit;
  224. }
  225. if (opts.verbose)
  226. fprintf(stderr, "Publishing data of length %d\n", data_len);
  227. if (opts.MQTTVersion == MQTTVERSION_5)
  228. {
  229. MQTTResponse response = MQTTResponse_initializer;
  230. response = MQTTClient_publish5(client, opts.topic, data_len, buffer, opts.qos, opts.retained, &pub_props, NULL);
  231. rc = response.reasonCode;
  232. }
  233. else
  234. rc = MQTTClient_publish(client, opts.topic, data_len, buffer, opts.qos, opts.retained, NULL);
  235. if (opts.stdin_lines == 0)
  236. break;
  237. if (rc != 0)
  238. {
  239. myconnect(client);
  240. if (opts.MQTTVersion == MQTTVERSION_5)
  241. {
  242. MQTTResponse response = MQTTResponse_initializer;
  243. response = MQTTClient_publish5(client, opts.topic, data_len, buffer, opts.qos, opts.retained, &pub_props, NULL);
  244. rc = response.reasonCode;
  245. }
  246. else
  247. rc = MQTTClient_publish(client, opts.topic, data_len, buffer, opts.qos, opts.retained, NULL);
  248. }
  249. if (opts.qos > 0)
  250. MQTTClient_yield();
  251. }
  252. exit:
  253. if (opts.filename || opts.stdin_lines)
  254. free(buffer);
  255. if (opts.MQTTVersion == MQTTVERSION_5)
  256. rc = MQTTClient_disconnect5(client, 0, MQTTREASONCODE_SUCCESS, NULL);
  257. else
  258. rc = MQTTClient_disconnect(client, 0);
  259. MQTTClient_destroy(&client);
  260. return EXIT_SUCCESS;
  261. }