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