Annotation of mqtt/src/mqtt_subs.c, revision 1.2.2.7
1.2 misho 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[];
1.2.2.2 misho 10: volatile intptr_t Kill;
1.2 misho 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"
1.2.2.3 misho 20: " Syntax: mqtt_subs [options] <connect_to_broker[:port]> <ConnectID> [exec_script <value>]\n\n"
1.2 misho 21: "\t-l <value2file>\t\tSave received values to file\n"
1.2.2.6 misho 22: "\t-u\t\tUnsubscribe given topic(s)\n"
1.2 misho 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);
1.2.2.3 misho 42: mqtt_subFree(&args->subscr);
1.2 misho 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: {
1.2.2.6 misho 55: u_char *qoses, *qos;
56: u_short mid;
57: mqtt_subscr_t *sub;
1.2.2.3 misho 58:
1.2.2.4 misho 59: srandomdev();
1.2.2.6 misho 60: mid = random() % USHRT_MAX;
1.2.2.4 misho 61:
1.2.2.6 misho 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());
1.2.2.3 misho 66: return -1;
1.2.2.6 misho 67: } else
68: printf("OK\n");
1.2.2.3 misho 69:
1.2.2.6 misho 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:
1.2.2.7 ! misho 86: printf(" > Execute UNSUBSCRIBE request #%d ... ", mid);
1.2.2.6 misho 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());
1.2.2.3 misho 89: return -1;
1.2.2.7 ! misho 90: } else
! 91: printf("OK\n");
1.2.2.3 misho 92:
1.2 misho 93: return 0;
94: }
95:
96:
97: int
98: main(int argc, char **argv)
99: {
1.2.2.6 misho 100: char ch, un = 0, idx = 0, batch = 1;
1.2.2.3 misho 101: ait_val_t val;
1.2 misho 102: u_short port = atoi(MQTT_PORT);
1.2.2.3 misho 103: mqtt_subscr_t *sub;
1.2.2.5 misho 104: int ret = 0;
1.2.2.3 misho 105: char *str, szStr[STRSIZ], szLogName[MAXPATHLEN] = { 0 };
1.2 misho 106: FILE *lf;
107:
108: if (!(args = 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));
1.2.2.3 misho 113: if (!(args->subscr = mqtt_subAlloc(idx))) {
114: printf("Error:: in subscribes array #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2 misho 115: 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: free(args);
124: return 1;
125: }
126:
1.2.2.4 misho 127: AIT_SET_STR(&args->ConnID, "");
1.2 misho 128: AIT_SET_STR(&args->User, "");
129: AIT_SET_STR(&args->Pass, "");
130:
131: args->ka = MQTT_KEEPALIVE;
1.2.2.6 misho 132: while ((ch = getopt(argc, argv, "T:U:P:p:s:q:dl:W:M:CDvuh")) != -1)
1.2 misho 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':
1.2.2.3 misho 157: sub = mqtt_subRealloc(&args->subscr, idx + 1);
158: if (!sub) {
159: printf("Error:: #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2 misho 160: args->free(args);
161: free(args);
162: return 1;
163: } else
1.2.2.3 misho 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 = strdup(szStr);
177: sub->sub_topic.msg_len = strlen(szStr);
1.2 misho 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: 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;
1.2.2.6 misho 203: case 'u':
204: un = 1;
205: break;
1.2 misho 206: case 'h':
207: default:
208: args->free(args);
209: free(args);
210: Usage();
211: return 1;
212: }
213: argc -= optind;
214: argv += optind;
1.2.2.3 misho 215: if (argc < 2) {
1.2 misho 216: printf("Error:: host for connect not found, connection id or topic not supplied!\n\n");
217: args->free(args);
218: free(args);
219: Usage();
220: return 1;
1.2.2.4 misho 221: } else {
222: AIT_FREE_VAL(&args->ConnID);
223: AIT_SET_STR(&args->ConnID, argv[1]);
224: }
1.2.2.3 misho 225: if (argc > 2) {
1.2 misho 226: AIT_FREE_VAL(&args->Value);
1.2.2.3 misho 227: AIT_SET_STR(&args->Value, argv[2]);
1.2 misho 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: free(args);
233: Usage();
234: return 1;
235: }
1.2.2.1 misho 236: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
237: AIT_FREE_VAL(&val);
1.2 misho 238:
1.2.2.6 misho 239: if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
1.2 misho 240: args->free(args);
241: free(args);
242: return 2;
243: }
244:
1.2.2.5 misho 245: switch ((ret = ConnectClient(args->cli->sock))) {
1.2 misho 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) {
1.2.2.7 ! misho 275: ret = Subscribe(args->cli->sock, lf);
! 276: if (un)
! 277: Unsubscribe(args->cli->sock);
1.2 misho 278: fclose(lf);
279: } else
280: printf("Error:: in subscribe file #%d - %s\n", errno, strerror(errno));
1.2.2.5 misho 281: } else
1.2 misho 282: ret = 3;
1.2.2.5 misho 283:
284: mqtt_cli_Close(&args->cli);
1.2 misho 285:
286: args->free(args);
287: free(args);
288: return ret;
289: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>