Annotation of mqtt/src/mqtt_pub.c, revision 1.1.2.3
1.1.2.1 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[];
10:
11: struct tagArgs *args;
12:
13:
14: static void
15: Usage(void)
16: {
17: printf( " -= MQTT Publisher Client =- Publisher from ELWIX\n"
18: "=== %s@%s === Compiled: %s ===\n\n"
19: " Syntax: mqtt_pub [options] <connect_to_broker[:port]> <ConnectID> <topic> <value_for_publish>\n\n"
20: "\t-f\t\t\t'value_for_publish' is file name instead text\n"
21: "\t-q <QoS>\t\tQoS level (0-at most 1, 1-at least 1, 2-exactly 1)\n"
22: "\t-d\t\t\tSend duplicate message\n"
23: "\t-r\t\t\tRetain message from broker\n\n"
24: "\t-C\t\t\tNot clear before connect!!!\n"
25: "\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
26: "\t-T <timeout>\t\tKeep alive timeout in seconds (default: 10sec)\n"
27: "\t-U <username>\t\tUsername\n"
28: "\t-P <password>\t\tPassword\n"
29: "\t-W <topic>\t\tWill Topic\n"
30: "\t-M <message>\t\tWill Message\n"
31: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
32: "\t-h\t\t\tHelp! This screen\n\n",
33: compiledby, compilehost, compiled);
34: }
35:
36: static void
37: cleanArgs(struct tagArgs * __restrict args)
38: {
39: mqtt_msgFree(&args->msg, 42);
40: AIT_FREE_VAL(&args->Will.Msg);
41: AIT_FREE_VAL(&args->Will.Topic);
42: AIT_FREE_VAL(&args->User);
43: AIT_FREE_VAL(&args->Pass);
44: AIT_FREE_VAL(&args->Publish);
45: AIT_FREE_VAL(&args->Value);
46: AIT_FREE_VAL(&args->ConnID);
47: }
48:
49: static int
50: Publish(int sock)
51: {
52: return 0;
53: }
54:
55:
56: int
57: main(int argc, char **argv)
58: {
59: char ch;
60: ait_val_t val;
61: u_short port = atoi(MQTT_PORT);
62: int sock, ret = 0;
63:
64: if (!(args = malloc(sizeof(struct tagArgs)))) {
65: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
66: return 1;
67: } else
68: memset(args, 0, sizeof(struct tagArgs));
69: args->free = cleanArgs;
70:
71: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
72: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
73: args->free(args);
74: free(args);
75: return 1;
76: }
77:
78: AIT_SET_STR(&args->ConnID, "");
79: AIT_SET_STR(&args->User, "");
80: AIT_SET_STR(&args->Pass, "");
81:
82: args->ka = MQTT_KEEPALIVE;
1.1.2.2 misho 83: while ((ch = getopt(argc, argv, "T:U:P:p:q:drCW:M:fvh")) != -1)
1.1.2.1 misho 84: switch (ch) {
85: case 'T':
86: args->ka = (u_short) strtol(optarg, NULL, 0);
87: break;
88: case 'M':
89: AIT_FREE_VAL(&args->Will.Msg);
90: AIT_SET_STR(&args->Will.Msg, optarg);
91: break;
92: case 'W':
93: AIT_FREE_VAL(&args->Will.Topic);
94: AIT_SET_STR(&args->Will.Topic, optarg);
95: break;
96: case 'U':
97: AIT_FREE_VAL(&args->User);
98: AIT_SET_STR(&args->User, optarg);
99: break;
100: case 'P':
101: AIT_FREE_VAL(&args->Pass);
102: AIT_SET_STR(&args->Pass, optarg);
103: break;
104: case 'p':
105: port = (u_short) strtol(optarg, NULL, 0);
106: break;
107: case 'q':
108: args->QoS = (char) strtol(optarg, NULL, 0);
109: if (args->QoS > MQTT_QOS_EXACTLY) {
110: printf("Error:: invalid QoS level %d\n", args->QoS);
111: args->free(args);
112: free(args);
113: return 1;
114: }
115: break;
116: case 'd':
117: args->Dup++;
118: break;
119: case 'r':
120: args->Retain++;
121: break;
122: case 'C':
123: args->notClear++;
124: break;
125: case 'f':
126: args->isFile++;
127: break;
128: case 'v':
129: io_incDebug;
130: break;
131: case 'h':
132: default:
133: args->free(args);
134: free(args);
135: Usage();
136: return 1;
137: }
138: argc -= optind;
139: argv += optind;
140: if (argc < 4) {
141: printf("Error:: host for connect not found, connection id, topic or value not supplied!\n\n");
142: args->free(args);
143: free(args);
144: Usage();
145: return 1;
146: } else {
147: AIT_FREE_VAL(&args->ConnID);
148: AIT_SET_STR(&args->ConnID, argv[1]);
149: AIT_FREE_VAL(&args->Publish);
150: AIT_SET_STR(&args->Publish, argv[2]);
151: AIT_FREE_VAL(&args->Value);
152: AIT_SET_STR(&args->Value, argv[3]);
153: }
154: if (!io_gethostbyname(*argv, port, &args->addr)) {
155: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
156: args->free(args);
157: free(args);
158: Usage();
159: return 1;
160: }
161: ioVERBOSE(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
162:
163: if ((sock = InitClient()) == -1) {
164: args->free(args);
165: free(args);
166: return 2;
167: }
168:
169: printf("Connected ... ");
1.1.2.3 ! misho 170: switch ((ret = ConnectClient(sock))) {
1.1.2.1 misho 171: case -1:
172: printf(">> FAILED!\n");
173: break;
174: case MQTT_RETCODE_ACCEPTED:
175: printf(">> OK\n");
176: break;
177: case MQTT_RETCODE_REFUSE_VER:
178: printf(">> Incorrect version\n");
179: break;
180: case MQTT_RETCODE_REFUSE_ID:
181: printf(">> Incorrect connectID\n");
182: break;
183: case MQTT_RETCODE_REFUSE_UNAVAIL:
184: printf(">> Service unavailable\n");
185: break;
186: case MQTT_RETCODE_REFUSE_USERPASS:
187: printf(">> Refuse user/pass\n");
188: break;
189: case MQTT_RETCODE_DENIED:
190: printf(">> DENIED.\n");
191: break;
192: }
193:
194: if (ret == MQTT_RETCODE_ACCEPTED) {
195: ret = Publish(sock);
196: shutdown(sock, SHUT_RDWR);
1.1.2.3 ! misho 197: CloseClient(sock);
! 198: } else {
! 199: close(sock);
1.1.2.1 misho 200: ret = 3;
1.1.2.3 ! misho 201: }
1.1.2.1 misho 202:
203: args->free(args);
204: free(args);
205: return ret;
206: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>