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