PageRenderTime 57ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/client/sub_client.c

https://bitbucket.org/karlp/mosquitto
C | 361 lines | 315 code | 18 blank | 28 comment | 85 complexity | 0e1cc57804715b9c5e940c5b62dde847 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. Copyright (c) 2009-2011 Roger Light <roger@atchoo.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. Neither the name of mosquitto nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #ifndef WIN32
  30. #include <unistd.h>
  31. #else
  32. #include <process.h>
  33. #define snprintf sprintf_s
  34. #endif
  35. #include <mosquitto.h>
  36. static char **topics = NULL;
  37. static int topic_count = 0;
  38. static int topic_qos = 0;
  39. static char *username = NULL;
  40. static char *password = NULL;
  41. int verbose = 0;
  42. bool quiet = false;
  43. void my_message_callback(void *obj, const struct mosquitto_message *message)
  44. {
  45. if(verbose){
  46. if(message->payloadlen){
  47. printf("%s %s\n", message->topic, message->payload);
  48. }else{
  49. printf("%s (null)\n", message->topic);
  50. }
  51. fflush(stdout);
  52. }else{
  53. if(message->payloadlen){
  54. printf("%s\n", message->payload);
  55. fflush(stdout);
  56. }
  57. }
  58. }
  59. void my_connect_callback(void *obj, int result)
  60. {
  61. struct mosquitto *mosq = obj;
  62. int i;
  63. if(!result){
  64. for(i=0; i<topic_count; i++){
  65. mosquitto_subscribe(mosq, NULL, topics[i], topic_qos);
  66. }
  67. }else{
  68. switch(result){
  69. case 1:
  70. if(!quiet) fprintf(stderr, "Connection Refused: unacceptable protocol version\n");
  71. break;
  72. case 2:
  73. if(!quiet) fprintf(stderr, "Connection Refused: identifier rejected\n");
  74. break;
  75. case 3:
  76. if(!quiet) fprintf(stderr, "Connection Refused: broker unavailable\n");
  77. break;
  78. case 4:
  79. if(!quiet) fprintf(stderr, "Connection Refused: bad user name or password\n");
  80. break;
  81. case 5:
  82. if(!quiet) fprintf(stderr, "Connection Refused: not authorised\n");
  83. break;
  84. default:
  85. if(!quiet) fprintf(stderr, "Connection Refused: unknown reason\n");
  86. break;
  87. }
  88. }
  89. }
  90. void my_subscribe_callback(void *obj, uint16_t mid, int qos_count, const uint8_t *granted_qos)
  91. {
  92. int i;
  93. if(!quiet) printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
  94. for(i=1; i<qos_count; i++){
  95. if(!quiet) printf(", %d", granted_qos[i]);
  96. }
  97. if(!quiet) printf("\n");
  98. }
  99. void print_usage(void)
  100. {
  101. printf("mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.\n\n");
  102. printf("Usage: mosquitto_sub [-c] [-i id] [-k keepalive] [-p port] [-q qos] [-v] -t topic ...\n");
  103. printf(" [-d] [--quiet]\n");
  104. printf(" [-u username [--pw password]]\n");
  105. printf(" [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]\n\n");
  106. printf(" -c : disable 'clean session' (store subscription and pending messages when client disconnects).\n");
  107. printf(" -d : enable debug messages.\n");
  108. printf(" -h : mqtt host to connect to. Defaults to localhost.\n");
  109. printf(" -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.\n");
  110. printf(" -k : keep alive in seconds for this client. Defaults to 60.\n");
  111. printf(" -p : network port to connect to. Defaults to 1883.\n");
  112. printf(" -q : quality of service level to use for the subscription. Defaults to 0.\n");
  113. printf(" -t : mqtt topic to subscribe to. May be repeated multiple times.\n");
  114. printf(" -u : provide a username (requires MQTT 3.1 broker)\n");
  115. printf(" -v : print published messages verbosely.\n");
  116. printf(" --pw : provide a password (requires MQTT 3.1 broker)\n");
  117. printf(" --quiet : don't print error messages.\n");
  118. printf(" --will-payload : payload for the client Will, which is sent by the broker in case of\n");
  119. printf(" unexpected disconnection. If not given and will-topic is set, a zero\n");
  120. printf(" length message will be sent.\n");
  121. printf(" --will-qos : QoS level for the client Will.\n");
  122. printf(" --will-retain : if given, make the client Will retained.\n");
  123. printf(" --will-topic : the topic on which to publish the client Will.\n");
  124. printf("\nSee http://mosquitto.org/ for more information.\n\n");
  125. }
  126. int main(int argc, char *argv[])
  127. {
  128. char *id = NULL;
  129. int i;
  130. char *host = "localhost";
  131. int port = 1883;
  132. int keepalive = 60;
  133. bool clean_session = true;
  134. bool debug = false;
  135. struct mosquitto *mosq = NULL;
  136. int rc;
  137. uint8_t *will_payload = NULL;
  138. long will_payloadlen = 0;
  139. int will_qos = 0;
  140. bool will_retain = false;
  141. char *will_topic = NULL;
  142. for(i=1; i<argc; i++){
  143. if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){
  144. if(i==argc-1){
  145. fprintf(stderr, "Error: -p argument given but no port specified.\n\n");
  146. print_usage();
  147. return 1;
  148. }else{
  149. port = atoi(argv[i+1]);
  150. if(port<1 || port>65535){
  151. fprintf(stderr, "Error: Invalid port given: %d\n", port);
  152. print_usage();
  153. return 1;
  154. }
  155. }
  156. i++;
  157. }else if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--disable-clean-session")){
  158. clean_session = false;
  159. }else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){
  160. debug = true;
  161. }else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host")){
  162. if(i==argc-1){
  163. fprintf(stderr, "Error: -h argument given but no host specified.\n\n");
  164. print_usage();
  165. return 1;
  166. }else{
  167. host = argv[i+1];
  168. }
  169. i++;
  170. }else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--id")){
  171. if(i==argc-1){
  172. fprintf(stderr, "Error: -i argument given but no id specified.\n\n");
  173. print_usage();
  174. return 1;
  175. }else{
  176. id = argv[i+1];
  177. }
  178. i++;
  179. }else if(!strcmp(argv[i], "-k") || !strcmp(argv[i], "--keepalive")){
  180. if(i==argc-1){
  181. fprintf(stderr, "Error: -k argument given but no keepalive specified.\n\n");
  182. print_usage();
  183. return 1;
  184. }else{
  185. keepalive = atoi(argv[i+1]);
  186. if(keepalive>65535){
  187. fprintf(stderr, "Error: Invalid keepalive given: %d\n", keepalive);
  188. print_usage();
  189. return 1;
  190. }
  191. }
  192. i++;
  193. }else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--qos")){
  194. if(i==argc-1){
  195. fprintf(stderr, "Error: -q argument given but no QoS specified.\n\n");
  196. print_usage();
  197. return 1;
  198. }else{
  199. topic_qos = atoi(argv[i+1]);
  200. if(topic_qos<0 || topic_qos>2){
  201. fprintf(stderr, "Error: Invalid QoS given: %d\n", topic_qos);
  202. print_usage();
  203. return 1;
  204. }
  205. }
  206. i++;
  207. }else if(!strcmp(argv[i], "--quiet")){
  208. quiet = true;
  209. }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){
  210. if(i==argc-1){
  211. fprintf(stderr, "Error: -t argument given but no topic specified.\n\n");
  212. print_usage();
  213. return 1;
  214. }else{
  215. topic_count++;
  216. topics = realloc(topics, topic_count*sizeof(char *));
  217. topics[topic_count-1] = argv[i+1];
  218. }
  219. i++;
  220. }else if(!strcmp(argv[i], "-u") || !strcmp(argv[i], "--username")){
  221. if(i==argc-1){
  222. fprintf(stderr, "Error: -u argument given but no username specified.\n\n");
  223. print_usage();
  224. return 1;
  225. }else{
  226. username = argv[i+1];
  227. }
  228. i++;
  229. }else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")){
  230. verbose = 1;
  231. }else if(!strcmp(argv[i], "--pw")){
  232. if(i==argc-1){
  233. fprintf(stderr, "Error: --pw argument given but no password specified.\n\n");
  234. print_usage();
  235. return 1;
  236. }else{
  237. password = argv[i+1];
  238. }
  239. i++;
  240. }else if(!strcmp(argv[i], "--will-payload")){
  241. if(i==argc-1){
  242. fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n");
  243. print_usage();
  244. return 1;
  245. }else{
  246. will_payload = (uint8_t *)argv[i+1];
  247. will_payloadlen = strlen((char *)will_payload);
  248. }
  249. i++;
  250. }else if(!strcmp(argv[i], "--will-qos")){
  251. if(i==argc-1){
  252. fprintf(stderr, "Error: --will-qos argument given but no will QoS specified.\n\n");
  253. print_usage();
  254. return 1;
  255. }else{
  256. will_qos = atoi(argv[i+1]);
  257. if(will_qos < 0 || will_qos > 2){
  258. fprintf(stderr, "Error: Invalid will QoS %d.\n\n", will_qos);
  259. return 1;
  260. }
  261. }
  262. i++;
  263. }else if(!strcmp(argv[i], "--will-retain")){
  264. will_retain = true;
  265. }else if(!strcmp(argv[i], "--will-topic")){
  266. if(i==argc-1){
  267. fprintf(stderr, "Error: --will-topic argument given but no will topic specified.\n\n");
  268. print_usage();
  269. return 1;
  270. }else{
  271. will_topic = argv[i+1];
  272. }
  273. i++;
  274. }else{
  275. fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]);
  276. print_usage();
  277. return 1;
  278. }
  279. }
  280. if(!id){
  281. id = malloc(30);
  282. if(!id){
  283. if(!quiet) fprintf(stderr, "Error: Out of memory.\n");
  284. return 1;
  285. }
  286. snprintf(id, 30, "mosquitto_sub_%d", getpid());
  287. }
  288. if(topic_count == 0){
  289. fprintf(stderr, "Error: You must specify a topic to subscribe to.\n");
  290. print_usage();
  291. return 1;
  292. }
  293. if(will_payload && !will_topic){
  294. fprintf(stderr, "Error: Will payload given, but no will topic given.\n");
  295. print_usage();
  296. return 1;
  297. }
  298. if(will_retain && !will_topic){
  299. fprintf(stderr, "Error: Will retain given, but no will topic given.\n");
  300. print_usage();
  301. return 1;
  302. }
  303. if(password && !username){
  304. if(!quiet) fprintf(stderr, "Warning: Not using password since username not set.\n");
  305. }
  306. mosquitto_lib_init();
  307. mosq = mosquitto_new(id, NULL);
  308. if(!mosq){
  309. if(!quiet) fprintf(stderr, "Error: Out of memory.\n");
  310. return 1;
  311. }
  312. if(debug){
  313. mosquitto_log_init(mosq, MOSQ_LOG_DEBUG | MOSQ_LOG_ERR | MOSQ_LOG_WARNING
  314. | MOSQ_LOG_NOTICE | MOSQ_LOG_INFO, MOSQ_LOG_STDERR);
  315. }
  316. if(will_topic && mosquitto_will_set(mosq, true, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){
  317. if(!quiet) fprintf(stderr, "Error: Problem setting will.\n");
  318. return 1;
  319. }
  320. if(username && mosquitto_username_pw_set(mosq, username, password)){
  321. if(!quiet) fprintf(stderr, "Error: Problem setting username and password.\n");
  322. return 1;
  323. }
  324. mosquitto_connect_callback_set(mosq, my_connect_callback);
  325. mosquitto_message_callback_set(mosq, my_message_callback);
  326. if(debug){
  327. mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
  328. }
  329. rc = mosquitto_connect(mosq, host, port, keepalive, clean_session);
  330. if(rc){
  331. if(!quiet) fprintf(stderr, "Unable to connect (%d).\n", rc);
  332. return rc;
  333. }
  334. do{
  335. rc = mosquitto_loop(mosq, -1);
  336. }while(rc == MOSQ_ERR_SUCCESS);
  337. mosquitto_destroy(mosq);
  338. mosquitto_lib_cleanup();
  339. return rc;
  340. }