File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqtt_pub.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Tue Jul 3 09:02:50 2012 UTC (12 years ago) by misho
Branches: MAIN
CVS tags: mqtt1_2, MQTT1_1, HEAD
version 1.1

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

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