mqtt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2013 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 - change delimiter option from char to string
  16. * Guilherme Maciel Ferreira - add keep alive option
  17. *******************************************************************************/
  18. /*
  19. stdout subscriber
  20. compulsory parameters:
  21. --topic topic to subscribe to
  22. defaulted parameters:
  23. --host localhost
  24. --port 1883
  25. --qos 2
  26. --delimiter \n
  27. --clientid stdout-subscriber
  28. --showtopics off
  29. --keepalive 10
  30. --userid none
  31. --password none
  32. */
  33. #include "MQTTClient.h"
  34. #include "MQTTClientPersistence.h"
  35. #include <stdio.h>
  36. #include <signal.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include "home_easy.h"
  40. #if defined(WIN32)
  41. #define sleep Sleep
  42. #else
  43. #include <sys/time.h>
  44. #endif
  45. unsigned int id = 0x01379F0E;
  46. volatile int toStop = 0;
  47. typedef struct Item {
  48. char * topic;
  49. unsigned int id;
  50. char section;
  51. char channel;
  52. } Item;
  53. struct opts_struct
  54. {
  55. char* clientid;
  56. int nodelimiter;
  57. char* delimiter;
  58. int qos;
  59. char* username;
  60. char* password;
  61. char* host;
  62. char* port;
  63. int showtopics;
  64. int keepalive;
  65. } opts =
  66. {
  67. "stdout-subscriber", 0, "\n", 2, NULL, NULL, "localhost", "1883", 0, 10
  68. };
  69. void usage(void)
  70. {
  71. printf("MQTT stdout subscriber\n");
  72. printf("Usage: stdoutsub topicname <options>, where options are:\n");
  73. printf(" --host <hostname> (default is %s)\n", opts.host);
  74. printf(" --port <port> (default is %s)\n", opts.port);
  75. printf(" --qos <qos> (default is %d)\n", opts.qos);
  76. printf(" --delimiter <delim> (default is \\n)\n");
  77. printf(" --clientid <clientid> (default is %s)\n", opts.clientid);
  78. printf(" --username none\n");
  79. printf(" --password none\n");
  80. printf(" --showtopics <on or off> (default is on if the topic has a wildcard, else off)\n");
  81. printf(" --keepalive <seconds> (default is %d seconds)\n", opts.keepalive);
  82. exit(EXIT_FAILURE);
  83. }
  84. void myconnect(MQTTClient* client, MQTTClient_connectOptions* opts)
  85. {
  86. int rc = 0;
  87. if ((rc = MQTTClient_connect(*client, opts)) != 0)
  88. {
  89. printf("Failed to connect, return code %d\n", rc);
  90. exit(EXIT_FAILURE);
  91. }
  92. }
  93. void cfinish(int sig)
  94. {
  95. signal(SIGINT, NULL);
  96. toStop = 1;
  97. }
  98. void getopts(int argc, char** argv);
  99. int main(int argc, char** argv)
  100. {
  101. MQTTClient client;
  102. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  103. Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1};
  104. Item videoP = { "chacon/maison/salon/videoproj", 0x01379F0E, 'A', 2};
  105. Item tardis = { "chacon/maison/salon/tardis", 0x01379F0E, 'A', 3};
  106. Item lampC = { "chacon/maison/chambre/lampe", 0x00DE29E6, 'A', 1};
  107. //Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1};
  108. Item * listItem[10];
  109. listItem[0] = &lampH;
  110. listItem[1] = &videoP;
  111. listItem[2] = &tardis;
  112. listItem[3] = &lampC;
  113. char* topic = "chacon/#";
  114. int rc = 0;
  115. char url[100];
  116. opts.showtopics = 1;
  117. if (opts.showtopics)
  118. printf("topic is %s\n", topic);
  119. getopts(argc, argv);
  120. sprintf(url, "%s:%s", opts.host, opts.port);
  121. rc = MQTTClient_create(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  122. signal(SIGINT, cfinish);
  123. signal(SIGTERM, cfinish);
  124. conn_opts.keepAliveInterval = opts.keepalive;
  125. conn_opts.reliable = 0;
  126. conn_opts.cleansession = 1;
  127. // conn_opts.username = opts.username;
  128. // conn_opts.password = opts.password;
  129. myconnect(&client, &conn_opts);
  130. rc = MQTTClient_subscribe(client, topic, opts.qos);
  131. while (!toStop)
  132. {
  133. char* topicName = NULL;
  134. int topicLen;
  135. MQTTClient_message* message = NULL;
  136. rc = MQTTClient_receive(client, &topicName, &topicLen, &message, 1000);
  137. if (message)
  138. {
  139. if (opts.showtopics)
  140. printf("%s\t", topicName);
  141. for(int i=0;i < 4; i++){
  142. if(strcmp(listItem[i]->topic, topicName) == 0){
  143. initIO();
  144. char onoff = strcmp(message->payload,"1") ? 0 : 1;
  145. sendHomeEasyCommand(listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff,10);
  146. }
  147. }
  148. if (opts.nodelimiter)
  149. printf("%.*s", message->payloadlen, (char*)message->payload);
  150. else
  151. printf("%.*s%s", message->payloadlen, (char*)message->payload, opts.delimiter);
  152. fflush(stdout);
  153. MQTTClient_freeMessage(&message);
  154. MQTTClient_free(topicName);
  155. }
  156. if (rc != 0)
  157. myconnect(&client, &conn_opts);
  158. }
  159. printf("Stopping\n");
  160. MQTTClient_disconnect(client, 0);
  161. MQTTClient_destroy(&client);
  162. return EXIT_SUCCESS;
  163. }
  164. void getopts(int argc, char** argv)
  165. {
  166. int count = 2;
  167. while (count < argc)
  168. {
  169. if (strcmp(argv[count], "--qos") == 0)
  170. {
  171. if (++count < argc)
  172. {
  173. if (strcmp(argv[count], "0") == 0)
  174. opts.qos = 0;
  175. else if (strcmp(argv[count], "1") == 0)
  176. opts.qos = 1;
  177. else if (strcmp(argv[count], "2") == 0)
  178. opts.qos = 2;
  179. else
  180. usage();
  181. }
  182. else
  183. usage();
  184. }
  185. else if (strcmp(argv[count], "--host") == 0)
  186. {
  187. if (++count < argc)
  188. opts.host = argv[count];
  189. else
  190. usage();
  191. }
  192. else if (strcmp(argv[count], "--port") == 0)
  193. {
  194. if (++count < argc)
  195. opts.port = argv[count];
  196. else
  197. usage();
  198. }
  199. else if (strcmp(argv[count], "--clientid") == 0)
  200. {
  201. if (++count < argc)
  202. opts.clientid = argv[count];
  203. else
  204. usage();
  205. }
  206. else if (strcmp(argv[count], "--username") == 0)
  207. {
  208. if (++count < argc)
  209. opts.username = argv[count];
  210. else
  211. usage();
  212. }
  213. else if (strcmp(argv[count], "--password") == 0)
  214. {
  215. if (++count < argc)
  216. opts.password = argv[count];
  217. else
  218. usage();
  219. }
  220. else if (strcmp(argv[count], "--delimiter") == 0)
  221. {
  222. if (++count < argc)
  223. opts.delimiter = argv[count];
  224. else
  225. opts.nodelimiter = 1;
  226. }
  227. else if (strcmp(argv[count], "--showtopics") == 0)
  228. {
  229. if (++count < argc)
  230. {
  231. if (strcmp(argv[count], "on") == 0)
  232. opts.showtopics = 1;
  233. else if (strcmp(argv[count], "off") == 0)
  234. opts.showtopics = 0;
  235. else
  236. usage();
  237. }
  238. else
  239. usage();
  240. }
  241. else if (strcmp(argv[count], "--keepalive") == 0)
  242. {
  243. if (++count < argc)
  244. opts.keepalive = atoi(argv[count]);
  245. else
  246. usage();
  247. }
  248. count++;
  249. }
  250. }