File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqtt_subs.c
Revision 1.2.2.9: download - view: text, annotated - select for diffs - revision graph
Sun May 27 10:04:05 2012 UTC (12 years, 1 month ago) by misho
Branches: mqtt1_1
start using new elwix memory management

    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: volatile intptr_t Kill;
   11: 
   12: struct tagArgs *args;
   13: 
   14: 
   15: static void
   16: Usage(void)
   17: {
   18: 	printf(	" -= MQTT Subscriber Client =- Subscriber from ELWIX\n"
   19: 		"=== %s@%s === Compiled: %s ===\n\n"
   20: 		" Syntax: mqtt_subs [options] <connect_to_broker[:port]> <ConnectID> [exec_script <value>]\n\n"
   21: 		"\t-l <value2file>\t\tSave received values to file\n"
   22: 		"\t-u\t\t\tUnsubscribe given topic(s)\n"
   23: 		"\t-s <topic[|QoS]>\tSubscribe for this topic, if wish add different |QoS to topic\n"
   24: 		"\t-d\t\t\tSend duplicate message\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\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\n"
   32: 		"\t-D\t\t\tDaemon mode\n"
   33: 		"\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
   34: 		"\t-h\t\t\tHelp! This screen\n\n", 
   35: 		compiledby, compilehost, compiled);
   36: }
   37: 
   38: static void
   39: cleanArgs(struct tagArgs * __restrict args)
   40: {
   41: 	mqtt_msgFree(&args->msg, 42);
   42: 	mqtt_subFree(&args->subscr);
   43: 	AIT_FREE_VAL(&args->Will.Msg);
   44: 	AIT_FREE_VAL(&args->Will.Topic);
   45: 	AIT_FREE_VAL(&args->User);
   46: 	AIT_FREE_VAL(&args->Pass);
   47: 	AIT_FREE_VAL(&args->Publish);
   48: 	AIT_FREE_VAL(&args->Value);
   49: 	AIT_FREE_VAL(&args->ConnID);
   50: }
   51: 
   52: static int
   53: Subscribe(int sock, FILE *lf)
   54: {
   55: 	u_char *qoses, *qos;
   56: 	u_short mid;
   57: 	mqtt_subscr_t *sub;
   58: 
   59: 	srandomdev();
   60: 	mid = random() % USHRT_MAX;
   61: 
   62: 	printf(" > Execute SUBSCRIBE request #%d ... ", mid);
   63: 	qoses = mqtt_cli_Subscribe(args->cli, args->subscr, mid, args->Dup, MQTT_QOS_ACK);
   64: 	if (!qoses) {
   65: 		printf("Error:: Subscribe #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   66: 		return -1;
   67: 	} else
   68: 		printf("OK\n");
   69: 
   70: 	for (sub = args->subscr, qos = qoses; sub->sub_topic.msg_base; sub++, qos++)
   71: 		printf("  + Topic %s with QoS %d subscribed %s\n", (char*)
   72: 				sub->sub_topic.msg_base, sub->sub_ret, *qos ? "done" : "failed");
   73: 
   74: 	free(qoses);
   75: 	return 0;
   76: }
   77: 
   78: static int
   79: Unsubscribe(int sock)
   80: {
   81: 	u_short mid;
   82: 
   83: 	srandomdev();
   84: 	mid = random() % USHRT_MAX;
   85: 
   86: 	printf(" > Execute UNSUBSCRIBE request #%d ... ", mid);
   87: 	if (mqtt_cli_Unsubscribe(args->cli, args->subscr, mid, args->Dup, MQTT_QOS_ACK)) {
   88: 		printf("Error:: Unsubscribe #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   89: 		return -1;
   90: 	} else
   91: 		printf("OK\n");
   92: 
   93: 	return 0;
   94: }
   95: 
   96: 
   97: int
   98: main(int argc, char **argv)
   99: {
  100: 	char ch, un = 0, idx = 0, batch = 1;
  101: 	ait_val_t val;
  102: 	u_short port = atoi(MQTT_PORT);
  103: 	mqtt_subscr_t *sub;
  104: 	int ret = 0;
  105: 	char *str, szStr[STRSIZ], szLogName[MAXPATHLEN] = { 0 };
  106: 	FILE *lf;
  107: 
  108: 	if (!(args = io_malloc(sizeof(struct tagArgs)))) {
  109: 		printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
  110: 		return 1;
  111: 	} else
  112: 		memset(args, 0, sizeof(struct tagArgs));
  113: 	if (!(args->subscr = mqtt_subAlloc(idx))) {
  114: 		printf("Error:: in subscribes array #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
  115: 		io_free(args);
  116: 		return 1;
  117: 	} else
  118: 		args->free = cleanArgs;
  119: 
  120: 	if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
  121: 		printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
  122: 		args->free(args);
  123: 		io_free(args);
  124: 		return 1;
  125: 	}
  126: 
  127: 	AIT_SET_STR(&args->ConnID, "");
  128: 	AIT_SET_STR(&args->User, "");
  129: 	AIT_SET_STR(&args->Pass, "");
  130: 
  131: 	args->ka = MQTT_KEEPALIVE;
  132: 	while ((ch = getopt(argc, argv, "T:U:P:p:s:q:dl:W:M:CDvuh")) != -1)
  133: 		switch (ch) {
  134: 			case 'T':
  135: 				args->ka = (u_short) strtol(optarg, NULL, 0);
  136: 				break;
  137: 			case 'M':
  138: 				AIT_FREE_VAL(&args->Will.Msg);
  139: 				AIT_SET_STR(&args->Will.Msg, optarg);
  140: 				break;
  141: 			case 'W':
  142: 				AIT_FREE_VAL(&args->Will.Topic);
  143: 				AIT_SET_STR(&args->Will.Topic, optarg);
  144: 				break;
  145: 			case 'U':
  146: 				AIT_FREE_VAL(&args->User);
  147: 				AIT_SET_STR(&args->User, optarg);
  148: 				break;
  149: 			case 'P':
  150: 				AIT_FREE_VAL(&args->Pass);
  151: 				AIT_SET_STR(&args->Pass, optarg);
  152: 				break;
  153: 			case 'p':
  154: 				port = (u_short) strtol(optarg, NULL, 0);
  155: 				break;
  156: 			case 's':
  157: 				sub = mqtt_subRealloc(&args->subscr, idx + 1);
  158: 				if (!sub) {
  159: 					printf("Error:: #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
  160: 					args->free(args);
  161: 					io_free(args);
  162: 					return 1;
  163: 				} else
  164: 					sub += idx++;
  165: 
  166: 				strlcpy(szStr, optarg, sizeof szStr);
  167: 				if ((str = strchr(szStr, '|'))) {
  168: 					*str++ = 0;
  169: 					*str -= 0x30;
  170: 					if (*str < 0 || *str > MQTT_QOS_RESERVED)
  171: 						sub->sub_ret = (u_char) args->QoS;
  172: 					else
  173: 						sub->sub_ret = (u_char) *str;
  174: 				} else
  175: 					sub->sub_ret = (u_char) args->QoS;
  176: 				sub->sub_topic.msg_base = io_strdup(szStr);
  177: 				sub->sub_topic.msg_len = strlen(szStr);
  178: 				break;
  179: 			case 'q':
  180: 				args->QoS = (char) strtol(optarg, NULL, 0);
  181: 				if (args->QoS > MQTT_QOS_EXACTLY) {
  182: 					printf("Error:: invalid QoS level %d\n", args->QoS);
  183: 					args->free(args);
  184: 					io_free(args);
  185: 					return 1;
  186: 				}
  187: 				break;
  188: 			case 'd':
  189: 				args->Dup++;
  190: 				break;
  191: 			case 'C':
  192: 				args->notClear++;
  193: 				break;
  194: 			case 'l':
  195: 				strlcpy(szLogName, optarg, sizeof szLogName);
  196: 				break;
  197: 			case 'D':
  198: 				batch = 0;
  199: 				break;
  200: 			case 'v':
  201: 				io_incDebug;
  202: 				break;
  203: 			case 'u':
  204: 				un = 1;
  205: 				break;
  206: 			case 'h':
  207: 			default:
  208: 				args->free(args);
  209: 				io_free(args);
  210: 				Usage();
  211: 				return 1;
  212: 		}
  213: 	argc -= optind;
  214: 	argv += optind;
  215: 	if (argc < 2) {
  216: 		printf("Error:: host for connect not found, connection id or topic not supplied!\n\n");
  217: 		args->free(args);
  218: 		io_free(args);
  219: 		Usage();
  220: 		return 1;
  221: 	} else {
  222: 		AIT_FREE_VAL(&args->ConnID);
  223: 		AIT_SET_STR(&args->ConnID, argv[1]);
  224: 	}
  225: 	if (argc > 2) {
  226: 		AIT_FREE_VAL(&args->Value);
  227: 		AIT_SET_STR(&args->Value, argv[2]);
  228: 	}
  229: 	if (!io_gethostbyname(*argv, port, &args->addr)) {
  230: 		printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
  231: 		args->free(args);
  232: 		io_free(args);
  233: 		Usage();
  234: 		return 1;
  235: 	}
  236: 	printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
  237: 	AIT_FREE_VAL(&val);
  238: 
  239: 	if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
  240: 		args->free(args);
  241: 		io_free(args);
  242: 		return 2;
  243: 	}
  244: 
  245: 	switch ((ret = ConnectClient(args->cli->sock))) {
  246: 		case -1:
  247: 			printf(">> FAILED!\n");
  248: 			break;
  249: 		case MQTT_RETCODE_ACCEPTED:
  250: 			printf(">> OK\n");
  251: 			break;
  252: 		case MQTT_RETCODE_REFUSE_VER:
  253: 			printf(">> Incorrect version\n");
  254: 			break;
  255: 		case MQTT_RETCODE_REFUSE_ID:
  256: 			printf(">> Incorrect connectID\n");
  257: 			break;
  258: 		case MQTT_RETCODE_REFUSE_UNAVAIL:
  259: 			printf(">> Service unavailable\n");
  260: 			break;
  261: 		case MQTT_RETCODE_REFUSE_USERPASS:
  262: 			printf(">> Refuse user/pass\n");
  263: 			break;
  264: 		case MQTT_RETCODE_DENIED:
  265: 			printf(">> DENIED.\n");
  266: 			break;
  267: 	}
  268: 
  269: 	if (ret == MQTT_RETCODE_ACCEPTED) {
  270: 		if (*szLogName)
  271: 			lf = fopen(szLogName, "w");
  272: 		else
  273: 			lf = stdout;
  274: 		if (lf) {
  275: 			ret = Subscribe(args->cli->sock, lf);
  276: 			if (un)
  277: 				Unsubscribe(args->cli->sock);
  278: 			fclose(lf);
  279: 		} else
  280: 			printf("Error:: in subscribe file #%d - %s\n", errno, strerror(errno));
  281: 	} else
  282: 		ret = 3;
  283: 
  284: 	mqtt_cli_Close(&args->cli);
  285: 
  286: 	args->free(args);
  287: 	io_free(args);
  288: 	return ret;
  289: }

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