File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / Attic / mqtt.c
Revision 1.1.1.1.2.25: download - view: text, annotated - select for diffs - revision graph
Tue Dec 13 10:12:36 2011 UTC (12 years, 6 months ago) by misho
Branches: mqtt1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
fix signess typecasts

    1: #include "global.h"
    2: #include "rtlm.h"
    3: #include "mqtt.h"
    4: #include "client.h"
    5: 
    6: 
    7: sl_config cfg;
    8: sqlite3 *acc, *pub;
    9: FILE *logg;
   10: extern char compiled[], compiledby[], compilehost[];
   11: static char szCfgName[MAXPATHLEN];
   12: int Verbose, Kill;
   13: 
   14: struct tagArgs *args;
   15: 
   16: 
   17: static void
   18: Usage(void)
   19: {
   20: 	printf(	" -= MQTT Client =- Publisher/Subscriber from ELWIX\n"
   21: 		"=== %s@%s === Compiled: %s ===\n\n"
   22: 		" Syntax: mqtt [options] <connect_to_broker[:port]> [value_for_publish]\n\n"
   23: 		"\t-t <topic>\t\tPublish topic\n"
   24: 		"\t-s <topic[|QoS]>\tSubscribe for this topic, if wish add different |QoS to topic\n"
   25: 		"\t-q <QoS>\t\tQoS level (0-at most 1, 1-at least 1, 2-exactly 1)\n"
   26: 		"\t-d\t\t\tSend duplicate message\n"
   27: 		"\t-r\t\t\tRetain message from broker\n"
   28: 		"\t-c <config>\t\tService config\n"
   29: 		"\t-f\t\t\t'value_for_publish' is file instead text\n"
   30: 		"\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
   31: 		"\t-U <username>\t\tUsername\n"
   32: 		"\t-P <password>\t\tPassword\n"
   33: 		"\t-W <topic>\t\tWill Topic\n"
   34: 		"\t-M <message>\t\tWill Message\n"
   35: 		"\t-D\t\t\tDaemon mode\n"
   36: 		"\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
   37: 		"\t-h\t\t\tHelp! This screen\n\n", 
   38: 		compiledby, compilehost, compiled);
   39: }
   40: 
   41: static void
   42: cleanArgs(struct tagArgs * __restrict args)
   43: {
   44: 	mqtt_msgFree(&args->msg, 42);
   45: 	AIT_FREE_VAL(&args->Will.Msg);
   46: 	AIT_FREE_VAL(&args->Will.Topic);
   47: 	AIT_FREE_VAL(&args->User);
   48: 	AIT_FREE_VAL(&args->Pass);
   49: 	AIT_FREE_VAL(&args->Publish);
   50: 	AIT_FREE_VAL(&args->Value);
   51: 	io_freeVars(&args->Subscribes);
   52: }
   53: 
   54: 
   55: int
   56: main(int argc, char **argv)
   57: {
   58: 	char ch, batch = 1, szStr[STRSIZ] = { 0 };
   59: 	ait_val_t *v, val;
   60: 	u_short port = atoi(MQTT_PORT);
   61: 	int sock;
   62: 
   63: 	if (!(args = malloc(sizeof(struct tagArgs)))) {
   64: 		printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
   65: 		return 1;
   66: 	} else
   67: 		memset(args, 0, sizeof(struct tagArgs));
   68: 	if (!(args->Subscribes = io_allocVars(1))) {
   69: 		printf("Error:: in subscribes array #%d - %s\n", io_GetErrno(), io_GetError());
   70: 		free(args);
   71: 		return 1;
   72: 	} else
   73: 		args->free = cleanArgs;
   74: 
   75: 	if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
   76: 		printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   77: 		args->free(args);
   78: 		free(args);
   79: 		return 1;
   80: 	}
   81: 
   82: 	strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
   83: 	while ((ch = getopt(argc, argv, "U:P:p:t:s:q:drc:W:M:fDvh")) != -1)
   84: 		switch (ch) {
   85: 			case 'M':
   86: 				AIT_FREE_VAL(&args->Will.Msg);
   87: 				AIT_SET_STR(&args->Will.Msg, optarg);
   88: 				break;
   89: 			case 'W':
   90: 				AIT_FREE_VAL(&args->Will.Topic);
   91: 				AIT_SET_STR(&args->Will.Topic, optarg);
   92: 				break;
   93: 			case 'U':
   94: 				AIT_FREE_VAL(&args->User);
   95: 				AIT_SET_STR(&args->User, optarg);
   96: 				break;
   97: 			case 'P':
   98: 				AIT_FREE_VAL(&args->Pass);
   99: 				AIT_SET_STR(&args->Pass, optarg);
  100: 				break;
  101: 			case 'p':
  102: 				port = (u_short) strtol(optarg, NULL, 0);
  103: 				break;
  104: 			case 't':
  105: 				AIT_FREE_VAL(&args->Publish);
  106: 				AIT_SET_STR(&args->Publish, optarg);
  107: 				break;
  108: 			case 's':
  109: 				v = malloc(sizeof(ait_val_t));
  110: 				if (!v) {
  111: 					printf("Error:: not enough memory #%d - %s\n", errno, strerror(errno));
  112: 					args->free(args);
  113: 					free(args);
  114: 					return 1;
  115: 				} else
  116: 					AIT_SET_STR(v, optarg);
  117: 				io_arrayElem(args->Subscribes, io_arraySize(args->Subscribes), v);
  118: 				break;
  119: 			case 'q':
  120: 				args->QoS = (char) strtol(optarg, NULL, 0);
  121: 				if (args->QoS < MQTT_QOS_ONCE || args->QoS > MQTT_QOS_EXACTLY) {
  122: 					printf("Error:: invalid QoS level %d\n", args->QoS);
  123: 					args->free(args);
  124: 					free(args);
  125: 					return 1;
  126: 				}
  127: 				break;
  128: 			case 'd':
  129: 				args->Dup++;
  130: 				break;
  131: 			case 'r':
  132: 				args->Retain++;
  133: 				break;
  134: 			case 'f':
  135: 				args->isFile++;
  136: 				break;
  137: 			case 'c':
  138: 				strlcpy(szCfgName, optarg, sizeof szCfgName);
  139: 				break;
  140: 			case 'D':
  141: 				batch = 0;
  142: 				break;
  143: 			case 'v':
  144: 				Verbose++;
  145: 				break;
  146: 			case 'h':
  147: 			default:
  148: 				args->free(args);
  149: 				free(args);
  150: 				Usage();
  151: 				return 1;
  152: 		}
  153: 	argc -= optind;
  154: 	argv += optind;
  155: 	if (!argc) {
  156: 		printf("Error:: host for connect not found!\n");
  157: 		args->free(args);
  158: 		free(args);
  159: 		Usage();
  160: 		return 1;
  161: 	}
  162: 	if (argc > 1)
  163: 		AIT_SET_STR(&args->Value, argv[1]);
  164: 	if (!io_gethostbyname(*argv, port, &args->addr)) {
  165: 		printf("Error:: host not connect #%d - %s\n", io_GetErrno(), io_GetError());
  166: 		args->free(args);
  167: 		free(args);
  168: 		Usage();
  169: 		return 1;
  170: 	}
  171: 	VERB(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
  172: 
  173: 	if (LoadConfig(szCfgName, &cfg)) {
  174: 		printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
  175: 		args->free(args);
  176: 		free(args);
  177: 		return 1;
  178: 	}
  179: 
  180: 	if ((sock = InitClient()) == -1) {
  181: 		UnloadConfig(&cfg);
  182: 		args->free(args);
  183: 		free(args);
  184: 		return 2;
  185: 	}
  186: 
  187: 	if (SendConnect(sock) == -1) {
  188: 		close(sock);
  189: 		UnloadConfig(&cfg);
  190: 		args->free(args);
  191: 		free(args);
  192: 		return 3;
  193: 	}
  194: 
  195: 	shutdown(sock, SHUT_RDWR);
  196: 	close(sock);
  197: 
  198: 	UnloadConfig(&cfg);
  199: 	args->free(args);
  200: 	free(args);
  201: 	return 0;
  202: }

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