Return to mqtt_subs.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / mqtt / src |
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.8 misho 22: "\t-u\t\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.10 misho 59: #ifdef __NetBSD__
60: srandom(getpid() ^ time(NULL));
61: #else
1.2.2.4 misho 62: srandomdev();
1.2.2.10 misho 63: #endif
1.2.2.6 misho 64: mid = random() % USHRT_MAX;
1.2.2.4 misho 65:
1.2.2.6 misho 66: printf(" > Execute SUBSCRIBE request #%d ... ", mid);
67: qoses = mqtt_cli_Subscribe(args->cli, args->subscr, mid, args->Dup, MQTT_QOS_ACK);
68: if (!qoses) {
69: printf("Error:: Subscribe #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2.2.3 misho 70: return -1;
1.2.2.6 misho 71: } else
72: printf("OK\n");
1.2.2.3 misho 73:
1.2.2.6 misho 74: for (sub = args->subscr, qos = qoses; sub->sub_topic.msg_base; sub++, qos++)
75: printf(" + Topic %s with QoS %d subscribed %s\n", (char*)
76: sub->sub_topic.msg_base, sub->sub_ret, *qos ? "done" : "failed");
77:
78: free(qoses);
79: return 0;
80: }
81:
82: static int
83: Unsubscribe(int sock)
84: {
85: u_short mid;
86:
1.2.2.10 misho 87: #ifdef __NetBSD__
88: srandom(getpid() ^ time(NULL));
89: #else
1.2.2.6 misho 90: srandomdev();
1.2.2.10 misho 91: #endif
1.2.2.6 misho 92: mid = random() % USHRT_MAX;
93:
1.2.2.7 misho 94: printf(" > Execute UNSUBSCRIBE request #%d ... ", mid);
1.2.2.6 misho 95: if (mqtt_cli_Unsubscribe(args->cli, args->subscr, mid, args->Dup, MQTT_QOS_ACK)) {
96: printf("Error:: Unsubscribe #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2.2.3 misho 97: return -1;
1.2.2.7 misho 98: } else
99: printf("OK\n");
1.2.2.3 misho 100:
1.2 misho 101: return 0;
102: }
103:
104:
105: int
106: main(int argc, char **argv)
107: {
1.2.2.6 misho 108: char ch, un = 0, idx = 0, batch = 1;
1.2.2.3 misho 109: ait_val_t val;
1.2 misho 110: u_short port = atoi(MQTT_PORT);
1.2.2.3 misho 111: mqtt_subscr_t *sub;
1.2.2.5 misho 112: int ret = 0;
1.2.2.3 misho 113: char *str, szStr[STRSIZ], szLogName[MAXPATHLEN] = { 0 };
1.2 misho 114: FILE *lf;
115:
1.2.2.9 misho 116: if (!(args = io_malloc(sizeof(struct tagArgs)))) {
1.2 misho 117: printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
118: return 1;
119: } else
120: memset(args, 0, sizeof(struct tagArgs));
1.2.2.3 misho 121: if (!(args->subscr = mqtt_subAlloc(idx))) {
122: printf("Error:: in subscribes array #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2.2.9 misho 123: io_free(args);
1.2 misho 124: return 1;
125: } else
126: args->free = cleanArgs;
127:
128: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
129: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
130: args->free(args);
1.2.2.9 misho 131: io_free(args);
1.2 misho 132: return 1;
133: }
134:
1.2.2.4 misho 135: AIT_SET_STR(&args->ConnID, "");
1.2 misho 136: AIT_SET_STR(&args->User, "");
137: AIT_SET_STR(&args->Pass, "");
138:
139: args->ka = MQTT_KEEPALIVE;
1.2.2.6 misho 140: while ((ch = getopt(argc, argv, "T:U:P:p:s:q:dl:W:M:CDvuh")) != -1)
1.2 misho 141: switch (ch) {
142: case 'T':
143: args->ka = (u_short) strtol(optarg, NULL, 0);
144: break;
145: case 'M':
146: AIT_FREE_VAL(&args->Will.Msg);
147: AIT_SET_STR(&args->Will.Msg, optarg);
148: break;
149: case 'W':
150: AIT_FREE_VAL(&args->Will.Topic);
151: AIT_SET_STR(&args->Will.Topic, optarg);
152: break;
153: case 'U':
154: AIT_FREE_VAL(&args->User);
155: AIT_SET_STR(&args->User, optarg);
156: break;
157: case 'P':
158: AIT_FREE_VAL(&args->Pass);
159: AIT_SET_STR(&args->Pass, optarg);
160: break;
161: case 'p':
162: port = (u_short) strtol(optarg, NULL, 0);
163: break;
164: case 's':
1.2.2.3 misho 165: sub = mqtt_subRealloc(&args->subscr, idx + 1);
166: if (!sub) {
167: printf("Error:: #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2 misho 168: args->free(args);
1.2.2.9 misho 169: io_free(args);
1.2 misho 170: return 1;
171: } else
1.2.2.3 misho 172: sub += idx++;
173:
174: strlcpy(szStr, optarg, sizeof szStr);
175: if ((str = strchr(szStr, '|'))) {
176: *str++ = 0;
177: *str -= 0x30;
178: if (*str < 0 || *str > MQTT_QOS_RESERVED)
179: sub->sub_ret = (u_char) args->QoS;
180: else
181: sub->sub_ret = (u_char) *str;
182: } else
183: sub->sub_ret = (u_char) args->QoS;
1.2.2.11! misho 184: sub->sub_topic.msg_base = strdup(szStr);
1.2.2.3 misho 185: sub->sub_topic.msg_len = strlen(szStr);
1.2 misho 186: break;
187: case 'q':
188: args->QoS = (char) strtol(optarg, NULL, 0);
189: if (args->QoS > MQTT_QOS_EXACTLY) {
190: printf("Error:: invalid QoS level %d\n", args->QoS);
191: args->free(args);
1.2.2.9 misho 192: io_free(args);
1.2 misho 193: return 1;
194: }
195: break;
196: case 'd':
197: args->Dup++;
198: break;
199: case 'C':
200: args->notClear++;
201: break;
202: case 'l':
203: strlcpy(szLogName, optarg, sizeof szLogName);
204: break;
205: case 'D':
206: batch = 0;
207: break;
208: case 'v':
209: io_incDebug;
210: break;
1.2.2.6 misho 211: case 'u':
212: un = 1;
213: break;
1.2 misho 214: case 'h':
215: default:
216: args->free(args);
1.2.2.9 misho 217: io_free(args);
1.2 misho 218: Usage();
219: return 1;
220: }
221: argc -= optind;
222: argv += optind;
1.2.2.3 misho 223: if (argc < 2) {
1.2 misho 224: printf("Error:: host for connect not found, connection id or topic not supplied!\n\n");
225: args->free(args);
1.2.2.9 misho 226: io_free(args);
1.2 misho 227: Usage();
228: return 1;
1.2.2.4 misho 229: } else {
230: AIT_FREE_VAL(&args->ConnID);
231: AIT_SET_STR(&args->ConnID, argv[1]);
232: }
1.2.2.3 misho 233: if (argc > 2) {
1.2 misho 234: AIT_FREE_VAL(&args->Value);
1.2.2.3 misho 235: AIT_SET_STR(&args->Value, argv[2]);
1.2 misho 236: }
237: if (!io_gethostbyname(*argv, port, &args->addr)) {
238: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
239: args->free(args);
1.2.2.9 misho 240: io_free(args);
1.2 misho 241: Usage();
242: return 1;
243: }
1.2.2.1 misho 244: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
245: AIT_FREE_VAL(&val);
1.2 misho 246:
1.2.2.6 misho 247: if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
1.2 misho 248: args->free(args);
1.2.2.9 misho 249: io_free(args);
1.2 misho 250: return 2;
251: }
252:
1.2.2.5 misho 253: switch ((ret = ConnectClient(args->cli->sock))) {
1.2 misho 254: case -1:
255: printf(">> FAILED!\n");
256: break;
257: case MQTT_RETCODE_ACCEPTED:
258: printf(">> OK\n");
259: break;
260: case MQTT_RETCODE_REFUSE_VER:
261: printf(">> Incorrect version\n");
262: break;
263: case MQTT_RETCODE_REFUSE_ID:
264: printf(">> Incorrect connectID\n");
265: break;
266: case MQTT_RETCODE_REFUSE_UNAVAIL:
267: printf(">> Service unavailable\n");
268: break;
269: case MQTT_RETCODE_REFUSE_USERPASS:
270: printf(">> Refuse user/pass\n");
271: break;
272: case MQTT_RETCODE_DENIED:
273: printf(">> DENIED.\n");
274: break;
275: }
276:
277: if (ret == MQTT_RETCODE_ACCEPTED) {
278: if (*szLogName)
279: lf = fopen(szLogName, "w");
280: else
281: lf = stdout;
282: if (lf) {
1.2.2.7 misho 283: ret = Subscribe(args->cli->sock, lf);
284: if (un)
285: Unsubscribe(args->cli->sock);
1.2 misho 286: fclose(lf);
287: } else
288: printf("Error:: in subscribe file #%d - %s\n", errno, strerror(errno));
1.2.2.5 misho 289: } else
1.2 misho 290: ret = 3;
1.2.2.5 misho 291:
292: mqtt_cli_Close(&args->cli);
1.2 misho 293:
294: args->free(args);
1.2.2.9 misho 295: io_free(args);
1.2 misho 296: return ret;
297: }