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