Annotation of mqtt/src/mqtt_pub.c, revision 1.3.2.1
1.3.2.1 ! misho 1: /*************************************************************************
! 2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
! 3: * by Michael Pounov <misho@openbsd-bg.org>
! 4: *
! 5: * $Author: misho $
! 6: * $Id: global.h,v 1.4 2012/07/03 08:57:04 misho Exp $
! 7: *
! 8: **************************************************************************
! 9: The ELWIX and AITNET software is distributed under the following
! 10: terms:
! 11:
! 12: All of the documentation and software included in the ELWIX and AITNET
! 13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
! 14:
! 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
! 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
! 17:
! 18: Redistribution and use in source and binary forms, with or without
! 19: modification, are permitted provided that the following conditions
! 20: are met:
! 21: 1. Redistributions of source code must retain the above copyright
! 22: notice, this list of conditions and the following disclaimer.
! 23: 2. Redistributions in binary form must reproduce the above copyright
! 24: notice, this list of conditions and the following disclaimer in the
! 25: documentation and/or other materials provided with the distribution.
! 26: 3. All advertising materials mentioning features or use of this software
! 27: must display the following acknowledgement:
! 28: This product includes software developed by Michael Pounov <misho@elwix.org>
! 29: ELWIX - Embedded LightWeight unIX and its contributors.
! 30: 4. Neither the name of AITNET nor the names of its contributors
! 31: may be used to endorse or promote products derived from this software
! 32: without specific prior written permission.
! 33:
! 34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
! 35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 44: SUCH DAMAGE.
! 45: */
1.2 misho 46: #include "global.h"
47: #include "rtlm.h"
48: #include "mqtt.h"
49: #include "client.h"
50:
51:
52: io_enableDEBUG;
53:
54: extern char compiled[], compiledby[], compilehost[];
55:
56: struct tagArgs *args;
57:
58:
59: static void
60: Usage(void)
61: {
62: printf( " -= MQTT Publisher Client =- Publisher from ELWIX\n"
63: "=== %s@%s === Compiled: %s ===\n\n"
64: " Syntax: mqtt_pub [options] <connect_to_broker[:port]> <ConnectID> <topic> <value_for_publish>\n\n"
65: "\t-f\t\t\t'value_for_publish' is file name instead text\n"
66: "\t-q <QoS>\t\tQoS level (0-at most 1, 1-at least 1, 2-exactly 1)\n"
67: "\t-d\t\t\tSend duplicate message\n"
68: "\t-r\t\t\tRetain message from broker\n\n"
69: "\t-C\t\t\tNot clear before connect!!!\n"
70: "\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
71: "\t-T <timeout>\t\tKeep alive timeout in seconds (default: 10sec)\n"
72: "\t-U <username>\t\tUsername\n"
73: "\t-P <password>\t\tPassword\n"
74: "\t-W <topic>\t\tWill Topic\n"
75: "\t-M <message>\t\tWill Message\n"
76: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
77: "\t-h\t\t\tHelp! This screen\n\n",
78: compiledby, compilehost, compiled);
79: }
80:
81: static void
82: cleanArgs(struct tagArgs * __restrict args)
83: {
84: mqtt_msgFree(&args->msg, 42);
85: AIT_FREE_VAL(&args->Will.Msg);
86: AIT_FREE_VAL(&args->Will.Topic);
87: AIT_FREE_VAL(&args->User);
88: AIT_FREE_VAL(&args->Pass);
89: AIT_FREE_VAL(&args->Publish);
90: AIT_FREE_VAL(&args->Value);
91: AIT_FREE_VAL(&args->ConnID);
92: }
93:
94: static int
95: Publish(int sock)
96: {
97: int siz = 0;
1.3 misho 98: u_short mid = 0;
1.2 misho 99:
1.3 misho 100: #ifdef __NetBSD__
101: srandom(getpid() ^ time(NULL));
102: #else
103: srandomdev();
104: #endif
105: mid = random() % USHRT_MAX;
106:
107: printf(" > Execute PUBLISH request #%d ... ", mid);
108: siz = mqtt_cli_Publish(args->cli, mid, args->Dup, args->QoS, args->Retain,
109: AIT_GET_STR(&args->Publish), AIT_ADDR(&args->Value), AIT_LEN(&args->Value));
1.2 misho 110: if (siz == -1) {
1.3 misho 111: printf("Error:: Publish #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2 misho 112: return -1;
1.3 misho 113: } else
114: printf("Sended %d bytes \n", siz);
1.2 misho 115:
1.3 misho 116: return siz;
1.2 misho 117: }
118:
119:
120: int
121: main(int argc, char **argv)
122: {
123: char ch;
124: ait_val_t val;
125: u_short port = atoi(MQTT_PORT);
1.3 misho 126: int ret = 0;
1.2 misho 127:
1.3 misho 128: if (!(args = io_malloc(sizeof(struct tagArgs)))) {
1.2 misho 129: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
130: return 1;
131: } else
132: memset(args, 0, sizeof(struct tagArgs));
133: args->free = cleanArgs;
134:
135: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
136: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
137: args->free(args);
1.3 misho 138: io_free(args);
1.2 misho 139: return 1;
140: }
141:
142: AIT_SET_STR(&args->ConnID, "");
143: AIT_SET_STR(&args->User, "");
144: AIT_SET_STR(&args->Pass, "");
145:
146: args->ka = MQTT_KEEPALIVE;
1.3 misho 147: while ((ch = getopt(argc, argv, "T:U:P:p:q:drCW:M:fvh")) != -1)
1.2 misho 148: switch (ch) {
149: case 'T':
150: args->ka = (u_short) strtol(optarg, NULL, 0);
151: break;
152: case 'M':
153: AIT_FREE_VAL(&args->Will.Msg);
154: AIT_SET_STR(&args->Will.Msg, optarg);
155: break;
156: case 'W':
157: AIT_FREE_VAL(&args->Will.Topic);
158: AIT_SET_STR(&args->Will.Topic, optarg);
159: break;
160: case 'U':
161: AIT_FREE_VAL(&args->User);
162: AIT_SET_STR(&args->User, optarg);
163: break;
164: case 'P':
165: AIT_FREE_VAL(&args->Pass);
166: AIT_SET_STR(&args->Pass, optarg);
167: break;
168: case 'p':
169: port = (u_short) strtol(optarg, NULL, 0);
170: break;
171: case 'q':
172: args->QoS = (char) strtol(optarg, NULL, 0);
173: if (args->QoS > MQTT_QOS_EXACTLY) {
174: printf("Error:: invalid QoS level %d\n", args->QoS);
175: args->free(args);
1.3 misho 176: io_free(args);
1.2 misho 177: return 1;
178: }
179: break;
180: case 'd':
181: args->Dup++;
182: break;
183: case 'r':
184: args->Retain++;
185: break;
186: case 'C':
187: args->notClear++;
188: break;
189: case 'f':
190: args->isFile++;
191: break;
192: case 'v':
193: io_incDebug;
194: break;
195: case 'h':
196: default:
197: args->free(args);
1.3 misho 198: io_free(args);
1.2 misho 199: Usage();
200: return 1;
201: }
202: argc -= optind;
203: argv += optind;
204: if (argc < 4) {
205: printf("Error:: host for connect not found, connection id, topic or value not supplied!\n\n");
206: args->free(args);
1.3 misho 207: io_free(args);
1.2 misho 208: Usage();
209: return 1;
210: } else {
211: AIT_FREE_VAL(&args->ConnID);
212: AIT_SET_STR(&args->ConnID, argv[1]);
213: AIT_FREE_VAL(&args->Publish);
214: AIT_SET_STR(&args->Publish, argv[2]);
215: AIT_FREE_VAL(&args->Value);
216: AIT_SET_STR(&args->Value, argv[3]);
217: }
218: if (!io_gethostbyname(*argv, port, &args->addr)) {
219: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
220: args->free(args);
1.3 misho 221: io_free(args);
1.2 misho 222: Usage();
223: return 1;
224: }
1.3 misho 225: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
226: AIT_FREE_VAL(&val);
1.2 misho 227:
1.3 misho 228: if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
1.2 misho 229: args->free(args);
1.3 misho 230: io_free(args);
1.2 misho 231: return 2;
232: }
233:
234: if (args->isFile && !OpenFile()) {
1.3 misho 235: mqtt_cli_Close(&args->cli);
1.2 misho 236: args->free(args);
1.3 misho 237: io_free(args);
1.2 misho 238: return 3;
239: }
240:
1.3 misho 241: switch ((ret = ConnectClient(args->cli->sock))) {
1.2 misho 242: case -1:
243: printf(">> FAILED!\n");
244: break;
245: case MQTT_RETCODE_ACCEPTED:
246: printf(">> OK\n");
247: break;
248: case MQTT_RETCODE_REFUSE_VER:
249: printf(">> Incorrect version\n");
250: break;
251: case MQTT_RETCODE_REFUSE_ID:
252: printf(">> Incorrect connectID\n");
253: break;
254: case MQTT_RETCODE_REFUSE_UNAVAIL:
255: printf(">> Service unavailable\n");
256: break;
257: case MQTT_RETCODE_REFUSE_USERPASS:
258: printf(">> Refuse user/pass\n");
259: break;
260: case MQTT_RETCODE_DENIED:
261: printf(">> DENIED.\n");
262: break;
263: }
264:
265: if (ret == MQTT_RETCODE_ACCEPTED) {
1.3 misho 266: ret = (Publish(args->cli->sock) == -1);
267: } else
1.2 misho 268: ret = 4;
1.3 misho 269:
270: mqtt_cli_Close(&args->cli);
1.2 misho 271:
272: CloseFile();
273: args->free(args);
1.3 misho 274: io_free(args);
1.2 misho 275: return ret;
276: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>