File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqtt_pub.c
Revision 1.2.2.3: download - view: text, annotated - select for diffs - revision graph
Mon Apr 16 00:09:57 2012 UTC (12 years, 2 months ago) by misho
Branches: mqtt1_1
fix reply

    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-m <message_id>\t\tMessage ID for publish message\n"
   23: 		"\t-d\t\t\tSend duplicate message\n"
   24: 		"\t-r\t\t\tRetain message from broker\n\n"
   25: 		"\t-C\t\t\tNot clear before connect!!!\n"
   26: 		"\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
   27: 		"\t-T <timeout>\t\tKeep alive timeout in seconds (default: 10sec)\n"
   28: 		"\t-U <username>\t\tUsername\n"
   29: 		"\t-P <password>\t\tPassword\n"
   30: 		"\t-W <topic>\t\tWill Topic\n"
   31: 		"\t-M <message>\t\tWill Message\n"
   32: 		"\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
   33: 		"\t-h\t\t\tHelp! This screen\n\n", 
   34: 		compiledby, compilehost, compiled);
   35: }
   36: 
   37: static void
   38: cleanArgs(struct tagArgs * __restrict args)
   39: {
   40: 	mqtt_msgFree(&args->msg, 42);
   41: 	AIT_FREE_VAL(&args->Will.Msg);
   42: 	AIT_FREE_VAL(&args->Will.Topic);
   43: 	AIT_FREE_VAL(&args->User);
   44: 	AIT_FREE_VAL(&args->Pass);
   45: 	AIT_FREE_VAL(&args->Publish);
   46: 	AIT_FREE_VAL(&args->Value);
   47: 	AIT_FREE_VAL(&args->ConnID);
   48: }
   49: 
   50: static int
   51: Publish(int sock)
   52: {
   53: 	int siz = 0;
   54: 
   55: 	siz = mqtt_msgPUBLISH(args->msg, AIT_GET_STR(&args->Publish), args->MsgID, args->Dup, 
   56: 			args->QoS, args->Retain, AIT_ADDR(&args->Value), AIT_LEN(&args->Value));
   57: 	if (siz == -1) {
   58: 		printf("Error:: msgPUBLISH #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   59: 		return -1;
   60: 	}
   61: 	if (SendTo(sock, siz) == -1)
   62: 		return -1;
   63: 	/* QoS == MQTT_QOS_ONCE, no wait for reply */
   64: 	if (args->QoS == MQTT_QOS_ONCE)
   65: 		return 0;
   66: 
   67: 	if ((siz = RecvFrom(sock)) == -1 || !siz)
   68: 		return -1;
   69: 	/* QoS == MQTT_QOS_ACK, wait for PUBACK */
   70: 	if (args->QoS == MQTT_QOS_ACK) {
   71: 		siz = mqtt_readPUBACK(args->msg);
   72: 		if (siz == args->MsgID)
   73: 			return siz;
   74: 		if (!args->Dup) {
   75: 			args->Dup++;
   76: 			return Publish(sock);
   77: 		}
   78: 		goto end;
   79: 	}
   80: 
   81: 
   82: 	/* QoS == MQTT_QOS_EXACTLY */
   83: 	siz = mqtt_readPUBREC(args->msg);
   84: 	if (siz != args->MsgID) {
   85: 		if (!args->Dup) {
   86: 			args->Dup++;
   87: 			return Publish(sock);
   88: 		}
   89: 		goto end;
   90: 	}
   91: 
   92: 	do {
   93: 		siz = mqtt_msgPUBREL(args->msg, args->MsgID);
   94: 		if (siz == -1) {
   95: 			printf("Error:: msgPUBREL #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   96: 			return -1;
   97: 		}
   98: 		if (SendTo(sock, siz) == -1)
   99: 			return -1;
  100: 
  101: 		if ((siz = RecvFrom(sock)) == -1 || !siz)
  102: 			return -1;
  103: 
  104: 		siz = mqtt_readPUBCOMP(args->msg);
  105: 		if (siz == args->MsgID)
  106: 			return siz;
  107: 		if (!args->Dup) {
  108: 			args->Dup++;
  109: 			continue;
  110: 		}
  111: 	} while (0);
  112: 
  113: end:
  114: 	printf("Error:: Message not delivered\n");
  115: 	return -1;
  116: }
  117: 
  118: 
  119: int
  120: main(int argc, char **argv)
  121: {
  122: 	char ch;
  123: 	ait_val_t val;
  124: 	u_short port = atoi(MQTT_PORT);
  125: 	int sock, ret = 0;
  126: 
  127: 	if (!(args = malloc(sizeof(struct tagArgs)))) {
  128: 		printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
  129: 		return 1;
  130: 	} else
  131: 		memset(args, 0, sizeof(struct tagArgs));
  132: 	args->free = cleanArgs;
  133: 
  134: 	if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
  135: 		printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
  136: 		args->free(args);
  137: 		free(args);
  138: 		return 1;
  139: 	}
  140: 
  141: 	AIT_SET_STR(&args->ConnID, "");
  142: 	AIT_SET_STR(&args->User, "");
  143: 	AIT_SET_STR(&args->Pass, "");
  144: 
  145: 	args->ka = MQTT_KEEPALIVE;
  146: 	while ((ch = getopt(argc, argv, "T:U:P:p:q:drCW:M:m:fvh")) != -1)
  147: 		switch (ch) {
  148: 			case 'T':
  149: 				args->ka = (u_short) strtol(optarg, NULL, 0);
  150: 				break;
  151: 			case 'M':
  152: 				AIT_FREE_VAL(&args->Will.Msg);
  153: 				AIT_SET_STR(&args->Will.Msg, optarg);
  154: 				break;
  155: 			case 'W':
  156: 				AIT_FREE_VAL(&args->Will.Topic);
  157: 				AIT_SET_STR(&args->Will.Topic, optarg);
  158: 				break;
  159: 			case 'U':
  160: 				AIT_FREE_VAL(&args->User);
  161: 				AIT_SET_STR(&args->User, optarg);
  162: 				break;
  163: 			case 'P':
  164: 				AIT_FREE_VAL(&args->Pass);
  165: 				AIT_SET_STR(&args->Pass, optarg);
  166: 				break;
  167: 			case 'm':
  168: 				args->MsgID = (u_short) strtol(optarg, NULL, 0);
  169: 				break;
  170: 			case 'p':
  171: 				port = (u_short) strtol(optarg, NULL, 0);
  172: 				break;
  173: 			case 'q':
  174: 				args->QoS = (char) strtol(optarg, NULL, 0);
  175: 				if (args->QoS > MQTT_QOS_EXACTLY) {
  176: 					printf("Error:: invalid QoS level %d\n", args->QoS);
  177: 					args->free(args);
  178: 					free(args);
  179: 					return 1;
  180: 				}
  181: 				break;
  182: 			case 'd':
  183: 				args->Dup++;
  184: 				break;
  185: 			case 'r':
  186: 				args->Retain++;
  187: 				break;
  188: 			case 'C':
  189: 				args->notClear++;
  190: 				break;
  191: 			case 'f':
  192: 				args->isFile++;
  193: 				break;
  194: 			case 'v':
  195: 				io_incDebug;
  196: 				break;
  197: 			case 'h':
  198: 			default:
  199: 				args->free(args);
  200: 				free(args);
  201: 				Usage();
  202: 				return 1;
  203: 		}
  204: 	argc -= optind;
  205: 	argv += optind;
  206: 	if (argc < 4) {
  207: 		printf("Error:: host for connect not found, connection id, topic or value not supplied!\n\n");
  208: 		args->free(args);
  209: 		free(args);
  210: 		Usage();
  211: 		return 1;
  212: 	} else {
  213: 		AIT_FREE_VAL(&args->ConnID);
  214: 		AIT_SET_STR(&args->ConnID, argv[1]);
  215: 		AIT_FREE_VAL(&args->Publish);
  216: 		AIT_SET_STR(&args->Publish, argv[2]);
  217: 		AIT_FREE_VAL(&args->Value);
  218: 		AIT_SET_STR(&args->Value, argv[3]);
  219: 	}
  220: 	if (!io_gethostbyname(*argv, port, &args->addr)) {
  221: 		printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
  222: 		args->free(args);
  223: 		free(args);
  224: 		Usage();
  225: 		return 1;
  226: 	}
  227: 	if (args->QoS && !args->MsgID)
  228: 		args->MsgID = MQTT_DEFAULT_MSGID;
  229: 	printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
  230: 	AIT_FREE_VAL(&val);
  231: 
  232: 	if ((sock = InitClient()) == -1) {
  233: 		args->free(args);
  234: 		free(args);
  235: 		return 2;
  236: 	}
  237: 
  238: 	if (args->isFile && !OpenFile()) {
  239: 		args->free(args);
  240: 		free(args);
  241: 		return 3;
  242: 	}
  243: 
  244: 	switch ((ret = ConnectClient(sock))) {
  245: 		case -1:
  246: 			printf(">> FAILED!\n");
  247: 			break;
  248: 		case MQTT_RETCODE_ACCEPTED:
  249: 			printf(">> OK\n");
  250: 			break;
  251: 		case MQTT_RETCODE_REFUSE_VER:
  252: 			printf(">> Incorrect version\n");
  253: 			break;
  254: 		case MQTT_RETCODE_REFUSE_ID:
  255: 			printf(">> Incorrect connectID\n");
  256: 			break;
  257: 		case MQTT_RETCODE_REFUSE_UNAVAIL:
  258: 			printf(">> Service unavailable\n");
  259: 			break;
  260: 		case MQTT_RETCODE_REFUSE_USERPASS:
  261: 			printf(">> Refuse user/pass\n");
  262: 			break;
  263: 		case MQTT_RETCODE_DENIED:
  264: 			printf(">> DENIED.\n");
  265: 			break;
  266: 	}
  267: 
  268: 	if (ret == MQTT_RETCODE_ACCEPTED) {
  269: 		ret = !(Publish(sock) == args->MsgID);
  270: 		CloseClient(sock);
  271: 	} else {
  272: 		close(sock);
  273: 		ret = 4;
  274: 	}
  275: 
  276: 	CloseFile();
  277: 	args->free(args);
  278: 	free(args);
  279: 	return ret;
  280: }

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