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: args->ka = MQTT_KEEPALIVE;
90: strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
91: while ((ch = getopt(argc, argv, "T:U:P:p:t:s:q:drc:W:M:fDvh")) != -1)
92: switch (ch) {
93: case 'T':
94: args->ka = (u_short) strtol(optarg, NULL, 0);
95: break;
96: case 'M':
97: AIT_FREE_VAL(&args->Will.Msg);
98: AIT_SET_STR(&args->Will.Msg, optarg);
99: break;
100: case 'W':
101: AIT_FREE_VAL(&args->Will.Topic);
102: AIT_SET_STR(&args->Will.Topic, optarg);
103: break;
104: case 'U':
105: AIT_FREE_VAL(&args->User);
106: AIT_SET_STR(&args->User, optarg);
107: break;
108: case 'P':
109: AIT_FREE_VAL(&args->Pass);
110: AIT_SET_STR(&args->Pass, optarg);
111: break;
112: case 'p':
113: port = (u_short) strtol(optarg, NULL, 0);
114: break;
115: case 't':
116: AIT_FREE_VAL(&args->Publish);
117: AIT_SET_STR(&args->Publish, optarg);
118: break;
119: case 's':
120: v = io_allocVar();
121: if (!v) {
122: printf("Error:: not enough memory #%d - %s\n", errno, strerror(errno));
123: args->free(args);
124: free(args);
125: return 1;
126: } else
127: AIT_SET_STR(v, optarg);
128: io_arrayElem(args->Subscribes, io_arraySize(args->Subscribes), v);
129: break;
130: case 'q':
131: args->QoS = (char) strtol(optarg, NULL, 0);
132: if (args->QoS > MQTT_QOS_EXACTLY) {
133: printf("Error:: invalid QoS level %d\n", args->QoS);
134: args->free(args);
135: free(args);
136: return 1;
137: }
138: break;
139: case 'd':
140: args->Dup++;
141: break;
142: case 'r':
143: args->Retain++;
144: break;
145: case 'C':
146: args->notClear++;
147: break;
148: case 'f':
149: args->isFile++;
150: break;
151: case 'c':
152: strlcpy(szCfgName, optarg, sizeof szCfgName);
153: break;
154: case 'D':
155: batch = 0;
156: break;
157: case 'v':
158: Verbose++;
159: break;
160: case 'h':
161: default:
162: args->free(args);
163: free(args);
164: Usage();
165: return 1;
166: }
167: argc -= optind;
168: argv += optind;
169: if (argc < 2) {
170: printf("Error:: host for connect not found or connection id not supplied!\n");
171: args->free(args);
172: free(args);
173: Usage();
174: return 1;
175: } else {
176: AIT_FREE_VAL(&args->ConnID);
177: AIT_SET_STR(&args->ConnID, argv[1]);
178: }
179: if (argc > 2)
180: AIT_SET_STR(&args->Value, argv[2]);
181: if (!io_gethostbyname(*argv, port, &args->addr)) {
182: printf("Error:: host not connect #%d - %s\n", io_GetErrno(), io_GetError());
183: args->free(args);
184: free(args);
185: Usage();
186: return 1;
187: }
188: VERB(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
189:
190: if (LoadConfig(szCfgName, &cfg)) {
191: printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
192: args->free(args);
193: free(args);
194: return 1;
195: }
196:
197: if ((sock = InitClient()) == -1) {
198: UnloadConfig(&cfg);
199: args->free(args);
200: free(args);
201: return 2;
202: }
203:
204: printf("Connected ... ");
205: switch (try2Connect(sock)) {
206: case -1:
207: printf("FAILED!\n");
208: close(sock);
209: UnloadConfig(&cfg);
210: args->free(args);
211: free(args);
212: return 3;
213: case MQTT_RETCODE_ACCEPTED:
214: printf("OK\n");
215: break;
216: case MQTT_RETCODE_REFUSE_VER:
217: printf("Incorrect version\n");
218: break;
219: case MQTT_RETCODE_REFUSE_ID:
220: printf("Incorrect connectID\n");
221: break;
222: case MQTT_RETCODE_REFUSE_UNAVAIL:
223: printf("Service unavailable\n");
224: break;
225: case MQTT_RETCODE_REFUSE_USERPASS:
226: printf("Refuse user/pass\n");
227: break;
228: case MQTT_RETCODE_DENIED:
229: printf("DENIED.\n");
230: break;
231: }
232:
233: shutdown(sock, SHUT_RDWR);
234: close(sock);
235:
236: UnloadConfig(&cfg);
237: args->free(args);
238: free(args);
239: return 0;
240: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>