File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqtt_pub.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Sun Oct 8 22:49:25 2017 UTC (6 years, 8 months ago) by misho
Branches: MAIN
CVS tags: mqtt2_1, MQTT2_0, HEAD
version 2.0

    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: mqtt_pub.c,v 1.5 2017/10/08 22:49:25 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, 2013
   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: */
   46: #include "global.h"
   47: #include "rtlm.h"
   48: #include "mqtt.h"
   49: #include "client.h"
   50: 
   51: 
   52: extern char compiled[], compiledby[], compilehost[];
   53: 
   54: struct tagArgs *args;
   55: 
   56: 
   57: static void
   58: Usage(void)
   59: {
   60: 	printf(	" -= MQTT Publisher Client =- Publisher from ELWIX\n"
   61: 		"=== %s@%s === Compiled: %s ===\n\n"
   62: 		" Syntax: mqtt_pub [options] <connect_to_broker[:port]> <ConnectID> <topic> <value_for_publish>\n\n"
   63: 		"\t-f\t\t\t'value_for_publish' is file name instead text\n"
   64: 		"\t-q <QoS>\t\tQoS level (0-at most 1, 1-at least 1, 2-exactly 1)\n"
   65: 		"\t-d\t\t\tSend duplicate message\n"
   66: 		"\t-r\t\t\tRetain message from broker\n\n"
   67: 		"\t-C\t\t\tNot clear before connect!!!\n"
   68: 		"\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
   69: 		"\t-T <timeout>\t\tKeep alive timeout in seconds (default: 10sec)\n"
   70: 		"\t-U <username>\t\tUsername\n"
   71: 		"\t-P <password>\t\tPassword\n"
   72: 		"\t-W <topic>\t\tWill Topic\n"
   73: 		"\t-M <message>\t\tWill Message\n"
   74: 		"\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
   75: 		"\t-h\t\t\tHelp! This screen\n\n", 
   76: 		compiledby, compilehost, compiled);
   77: }
   78: 
   79: static void
   80: cleanArgs(struct tagArgs * __restrict args)
   81: {
   82: 	mqtt_msgFree(&args->msg, 42);
   83: 	AIT_FREE_VAL(&args->Will.Msg);
   84: 	AIT_FREE_VAL(&args->Will.Topic);
   85: 	AIT_FREE_VAL(&args->User);
   86: 	AIT_FREE_VAL(&args->Pass);
   87: 	AIT_FREE_VAL(&args->Publish);
   88: 	AIT_FREE_VAL(&args->Value);
   89: 	AIT_FREE_VAL(&args->ConnID);
   90: }
   91: 
   92: static int
   93: Publish(int sock)
   94: {
   95: 	int siz = 0;
   96: 	u_short mid = 0;
   97: 
   98: #ifdef __NetBSD__
   99: 	srandom(getpid() ^ time(NULL));
  100: #else
  101: 	srandomdev();
  102: #endif
  103: 	mid = random() % USHRT_MAX;
  104: 
  105: 	printf(" > Execute PUBLISH request #%d ... ", mid);
  106: 	siz = mqtt_cli_Publish(args->cli, mid, args->Dup, args->QoS, args->Retain, 
  107: 			AIT_GET_STR(&args->Publish), AIT_ADDR(&args->Value), AIT_LEN(&args->Value));
  108: 	if (siz == -1) {
  109: 		printf("Error:: Publish #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
  110: 		return -1;
  111: 	} else
  112: 		printf("Sended %d bytes \n", siz);
  113: 
  114: 	return siz;
  115: }
  116: 
  117: 
  118: int
  119: main(int argc, char **argv)
  120: {
  121: 	char ch;
  122: 	ait_val_t val;
  123: 	u_short port = atoi(MQTT_PORT);
  124: 	int ret = 0;
  125: 
  126: 	if (!(args = e_malloc(sizeof(struct tagArgs)))) {
  127: 		printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
  128: 		return 1;
  129: 	} else
  130: 		memset(args, 0, sizeof(struct tagArgs));
  131: 	args->free = cleanArgs;
  132: 
  133: 	if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
  134: 		printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
  135: 		args->free(args);
  136: 		e_free(args);
  137: 		return 1;
  138: 	}
  139: 
  140: 	AIT_SET_STR(&args->ConnID, "");
  141: 	AIT_SET_STR(&args->User, "");
  142: 	AIT_SET_STR(&args->Pass, "");
  143: 
  144: 	args->ka = MQTT_KEEPALIVE;
  145: 	while ((ch = getopt(argc, argv, "T:U:P:p:q:drCW:M:fvh")) != -1)
  146: 		switch (ch) {
  147: 			case 'T':
  148: 				args->ka = (u_short) strtol(optarg, NULL, 0);
  149: 				break;
  150: 			case 'M':
  151: 				AIT_FREE_VAL(&args->Will.Msg);
  152: 				AIT_SET_STR(&args->Will.Msg, optarg);
  153: 				break;
  154: 			case 'W':
  155: 				AIT_FREE_VAL(&args->Will.Topic);
  156: 				AIT_SET_STR(&args->Will.Topic, optarg);
  157: 				break;
  158: 			case 'U':
  159: 				AIT_FREE_VAL(&args->User);
  160: 				AIT_SET_STR(&args->User, optarg);
  161: 				break;
  162: 			case 'P':
  163: 				AIT_FREE_VAL(&args->Pass);
  164: 				AIT_SET_STR(&args->Pass, optarg);
  165: 				break;
  166: 			case 'p':
  167: 				port = (u_short) strtol(optarg, NULL, 0);
  168: 				break;
  169: 			case 'q':
  170: 				args->QoS = (char) strtol(optarg, NULL, 0);
  171: 				if (args->QoS > MQTT_QOS_EXACTLY) {
  172: 					printf("Error:: invalid QoS level %d\n", args->QoS);
  173: 					args->free(args);
  174: 					e_free(args);
  175: 					return 1;
  176: 				}
  177: 				break;
  178: 			case 'd':
  179: 				args->Dup++;
  180: 				break;
  181: 			case 'r':
  182: 				args->Retain++;
  183: 				break;
  184: 			case 'C':
  185: 				args->notClear++;
  186: 				break;
  187: 			case 'f':
  188: 				args->isFile++;
  189: 				break;
  190: 			case 'v':
  191: 				e_incVerbose;
  192: 				break;
  193: 			case 'h':
  194: 			default:
  195: 				args->free(args);
  196: 				e_free(args);
  197: 				Usage();
  198: 				return 1;
  199: 		}
  200: 	argc -= optind;
  201: 	argv += optind;
  202: 	if (argc < 4) {
  203: 		printf("Error:: host for connect not found, connection id, topic or value not supplied!\n\n");
  204: 		args->free(args);
  205: 		e_free(args);
  206: 		Usage();
  207: 		return 1;
  208: 	} else {
  209: 		AIT_FREE_VAL(&args->ConnID);
  210: 		AIT_SET_STR(&args->ConnID, argv[1]);
  211: 		AIT_FREE_VAL(&args->Publish);
  212: 		AIT_SET_STR(&args->Publish, argv[2]);
  213: 		AIT_FREE_VAL(&args->Value);
  214: 		AIT_SET_STR(&args->Value, argv[3]);
  215: 	}
  216: 	if (!e_gethostbyname(*argv, port, &args->addr)) {
  217: 		printf("Error:: host not valid #%d - %s\n", elwix_GetErrno(), elwix_GetError());
  218: 		args->free(args);
  219: 		e_free(args);
  220: 		Usage();
  221: 		return 1;
  222: 	}
  223: 	printf("Connecting to %s:%d ... ", e_n2addr(&args->addr, &val), e_n2port(&args->addr));
  224: 	AIT_FREE_VAL(&val);
  225: 
  226: 	if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
  227: 		args->free(args);
  228: 		e_free(args);
  229: 		return 2;
  230: 	}
  231: 
  232: 	if (args->isFile && !OpenFile()) {
  233: 		mqtt_cli_Close(&args->cli);
  234: 		args->free(args);
  235: 		e_free(args);
  236: 		return 3;
  237: 	}
  238: 
  239: 	switch ((ret = ConnectClient(args->cli->sock))) {
  240: 		case -1:
  241: 			printf(">> FAILED!\n");
  242: 			break;
  243: 		case MQTT_RETCODE_ACCEPTED:
  244: 			printf(">> OK\n");
  245: 			break;
  246: 		case MQTT_RETCODE_REFUSE_VER:
  247: 			printf(">> Incorrect version\n");
  248: 			break;
  249: 		case MQTT_RETCODE_REFUSE_ID:
  250: 			printf(">> Incorrect connectID\n");
  251: 			break;
  252: 		case MQTT_RETCODE_REFUSE_UNAVAIL:
  253: 			printf(">> Service unavailable\n");
  254: 			break;
  255: 		case MQTT_RETCODE_REFUSE_USERPASS:
  256: 			printf(">> Refuse user/pass\n");
  257: 			break;
  258: 		case MQTT_RETCODE_DENIED:
  259: 			printf(">> DENIED.\n");
  260: 			break;
  261: 	}
  262: 
  263: 	if (ret == MQTT_RETCODE_ACCEPTED) {
  264: 		ret = (Publish(args->cli->sock) == -1);
  265: 	} else
  266: 		ret = 4;
  267: 
  268: 	mqtt_cli_Close(&args->cli);
  269: 
  270: 	CloseFile();
  271: 	args->free(args);
  272: 	e_free(args);
  273: 	return ret;
  274: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>