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]> <ConnectID> [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\t\t\tNot clear before connect!\n"
29: "\t-c <config>\t\tService config\n"
30: "\t-f\t\t\t'value_for_publish' is file instead text\n"
31: "\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
32: "\t-T <timeout>\t\tKeep alive timeout in seconds\n"
33: "\t-U <username>\t\tUsername\n"
34: "\t-P <password>\t\tPassword\n"
35: "\t-W <topic>\t\tWill Topic\n"
36: "\t-M <message>\t\tWill Message\n"
37: "\t-D\t\t\tDaemon mode\n"
38: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
39: "\t-h\t\t\tHelp! This screen\n\n",
40: compiledby, compilehost, compiled);
41: }
42:
43: static void
44: cleanArgs(struct tagArgs * __restrict args)
45: {
46: mqtt_msgFree(&args->msg, 42);
47: AIT_FREE_VAL(&args->Will.Msg);
48: AIT_FREE_VAL(&args->Will.Topic);
49: AIT_FREE_VAL(&args->User);
50: AIT_FREE_VAL(&args->Pass);
51: AIT_FREE_VAL(&args->Publish);
52: AIT_FREE_VAL(&args->Value);
53: AIT_FREE_VAL(&args->ConnID);
54: io_freeVars(&args->Subscribes);
55: }
56:
57:
58: int
59: main(int argc, char **argv)
60: {
61: char ch, batch = 1;
62: ait_val_t *v, val;
63: u_short port = atoi(MQTT_PORT);
64: int sock;
65:
66: if (!(args = malloc(sizeof(struct tagArgs)))) {
67: printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
68: return 1;
69: } else
70: memset(args, 0, sizeof(struct tagArgs));
71: if (!(args->Subscribes = io_allocVars(1))) {
72: printf("Error:: in subscribes array #%d - %s\n", io_GetErrno(), io_GetError());
73: free(args);
74: return 1;
75: } else
76: args->free = cleanArgs;
77:
78: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
79: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
80: args->free(args);
81: free(args);
82: return 1;
83: }
84:
85: AIT_SET_STR(&args->ConnID, "");
86: AIT_SET_STR(&args->User, "");
87: AIT_SET_STR(&args->Pass, "");
88:
89: strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
90: while ((ch = getopt(argc, argv, "T:U:P:p:t:s:q:drc:W:M:fDvh")) != -1)
91: switch (ch) {
92: case 'T':
93: args->ka = (u_short) strtol(optarg, NULL, 0);
94: break;
95: case 'M':
96: AIT_FREE_VAL(&args->Will.Msg);
97: AIT_SET_STR(&args->Will.Msg, optarg);
98: break;
99: case 'W':
100: AIT_FREE_VAL(&args->Will.Topic);
101: AIT_SET_STR(&args->Will.Topic, optarg);
102: break;
103: case 'U':
104: AIT_FREE_VAL(&args->User);
105: AIT_SET_STR(&args->User, optarg);
106: break;
107: case 'P':
108: AIT_FREE_VAL(&args->Pass);
109: AIT_SET_STR(&args->Pass, optarg);
110: break;
111: case 'p':
112: port = (u_short) strtol(optarg, NULL, 0);
113: break;
114: case 't':
115: AIT_FREE_VAL(&args->Publish);
116: AIT_SET_STR(&args->Publish, optarg);
117: break;
118: case 's':
119: v = io_allocVar();
120: if (!v) {
121: printf("Error:: not enough memory #%d - %s\n", errno, strerror(errno));
122: args->free(args);
123: free(args);
124: return 1;
125: } else
126: AIT_SET_STR(v, optarg);
127: io_arrayElem(args->Subscribes, io_arraySize(args->Subscribes), v);
128: break;
129: case 'q':
130: args->QoS = (char) strtol(optarg, NULL, 0);
131: if (args->QoS > MQTT_QOS_EXACTLY) {
132: printf("Error:: invalid QoS level %d\n", args->QoS);
133: args->free(args);
134: free(args);
135: return 1;
136: }
137: break;
138: case 'd':
139: args->Dup++;
140: break;
141: case 'r':
142: args->Retain++;
143: break;
144: case 'C':
145: args->notClear++;
146: break;
147: case 'f':
148: args->isFile++;
149: break;
150: case 'c':
151: strlcpy(szCfgName, optarg, sizeof szCfgName);
152: break;
153: case 'D':
154: batch = 0;
155: break;
156: case 'v':
157: Verbose++;
158: break;
159: case 'h':
160: default:
161: args->free(args);
162: free(args);
163: Usage();
164: return 1;
165: }
166: argc -= optind;
167: argv += optind;
168: if (argc < 2) {
169: printf("Error:: host for connect not found or connection id not supplied!\n");
170: args->free(args);
171: free(args);
172: Usage();
173: return 1;
174: } else {
175: AIT_FREE_VAL(&args->ConnID);
176: AIT_SET_STR(&args->ConnID, argv[1]);
177: }
178: if (argc > 2)
179: AIT_SET_STR(&args->Value, argv[2]);
180: if (!io_gethostbyname(*argv, port, &args->addr)) {
181: printf("Error:: host not connect #%d - %s\n", io_GetErrno(), io_GetError());
182: args->free(args);
183: free(args);
184: Usage();
185: return 1;
186: }
187: VERB(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
188:
189: if (LoadConfig(szCfgName, &cfg)) {
190: printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
191: args->free(args);
192: free(args);
193: return 1;
194: }
195:
196: if ((sock = InitClient()) == -1) {
197: UnloadConfig(&cfg);
198: args->free(args);
199: free(args);
200: return 2;
201: }
202:
203: if (try2Connect(sock) == -1) {
204: close(sock);
205: UnloadConfig(&cfg);
206: args->free(args);
207: free(args);
208: return 3;
209: }
210:
211: shutdown(sock, SHUT_RDWR);
212: close(sock);
213:
214: UnloadConfig(&cfg);
215: args->free(args);
216: free(args);
217: return 0;
218: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>