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