Return to mqtt_pub.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[];
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-m <message_id>\t\tMessage ID for publish message\n"
23: "\t-d\t\t\tSend duplicate message\n"
24: "\t-r\t\t\tRetain message from broker\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 (default: 10sec)\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"
32: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
33: "\t-h\t\t\tHelp! This screen\n\n",
34: compiledby, compilehost, compiled);
35: }
36:
37: static void
38: cleanArgs(struct tagArgs * __restrict args)
39: {
40: mqtt_msgFree(&args->msg, 42);
41: AIT_FREE_VAL(&args->Will.Msg);
42: AIT_FREE_VAL(&args->Will.Topic);
43: AIT_FREE_VAL(&args->User);
44: AIT_FREE_VAL(&args->Pass);
45: AIT_FREE_VAL(&args->Publish);
46: AIT_FREE_VAL(&args->Value);
47: AIT_FREE_VAL(&args->ConnID);
48: }
49:
50: static int
51: Publish(int sock)
52: {
53: int siz = 0;
54:
55: siz = mqtt_msgPUBLISH(args->msg, AIT_GET_STR(&args->Publish), args->MsgID, args->Dup,
1.2.2.2 misho 56: args->QoS, args->Retain, AIT_ADDR(&args->Value), AIT_LEN(&args->Value));
1.2 misho 57: if (siz == -1) {
58: printf("Error:: msgPUBLISH #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
59: return -1;
60: }
61: if (SendTo(sock, siz) == -1)
62: return -1;
63: /* QoS == MQTT_QOS_ONCE, no wait for reply */
64: if (args->QoS == MQTT_QOS_ONCE)
65: return 0;
66:
67: if ((siz = RecvFrom(sock)) == -1 || !siz)
68: return -1;
69: /* QoS == MQTT_QOS_ACK, wait for PUBACK */
70: if (args->QoS == MQTT_QOS_ACK) {
71: siz = mqtt_readPUBACK(args->msg);
72: if (siz == args->MsgID)
73: return siz;
74: if (!args->Dup) {
75: args->Dup++;
76: return Publish(sock);
77: }
78: goto end;
79: }
80:
81:
82: /* QoS == MQTT_QOS_EXACTLY */
83: siz = mqtt_readPUBREC(args->msg);
84: if (siz != args->MsgID) {
85: if (!args->Dup) {
86: args->Dup++;
87: return Publish(sock);
88: }
89: goto end;
90: }
91:
1.2.2.3 misho 92: do {
93: siz = mqtt_msgPUBREL(args->msg, args->MsgID);
94: if (siz == -1) {
95: printf("Error:: msgPUBREL #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
96: return -1;
97: }
98: if (SendTo(sock, siz) == -1)
99: return -1;
100:
101: if ((siz = RecvFrom(sock)) == -1 || !siz)
102: return -1;
103:
104: siz = mqtt_readPUBCOMP(args->msg);
105: if (siz == args->MsgID)
106: return siz;
107: if (!args->Dup) {
108: args->Dup++;
109: continue;
110: }
111: } while (0);
1.2 misho 112:
113: end:
114: printf("Error:: Message not delivered\n");
115: return -1;
116: }
117:
118:
119: int
120: main(int argc, char **argv)
121: {
122: char ch;
123: ait_val_t val;
124: u_short port = atoi(MQTT_PORT);
1.2.2.4 misho 125: int ret = 0;
1.2 misho 126:
127: if (!(args = malloc(sizeof(struct tagArgs)))) {
128: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
129: return 1;
130: } else
131: memset(args, 0, sizeof(struct tagArgs));
132: args->free = cleanArgs;
133:
134: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
135: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
136: args->free(args);
137: free(args);
138: return 1;
139: }
140:
141: AIT_SET_STR(&args->ConnID, "");
142: AIT_SET_STR(&args->User, "");
143: AIT_SET_STR(&args->Pass, "");
144:
145: args->ka = MQTT_KEEPALIVE;
146: while ((ch = getopt(argc, argv, "T:U:P:p:q:drCW:M:m:fvh")) != -1)
147: switch (ch) {
148: case 'T':
149: args->ka = (u_short) strtol(optarg, NULL, 0);
150: break;
151: case 'M':
152: AIT_FREE_VAL(&args->Will.Msg);
153: AIT_SET_STR(&args->Will.Msg, optarg);
154: break;
155: case 'W':
156: AIT_FREE_VAL(&args->Will.Topic);
157: AIT_SET_STR(&args->Will.Topic, optarg);
158: break;
159: case 'U':
160: AIT_FREE_VAL(&args->User);
161: AIT_SET_STR(&args->User, optarg);
162: break;
163: case 'P':
164: AIT_FREE_VAL(&args->Pass);
165: AIT_SET_STR(&args->Pass, optarg);
166: break;
167: case 'm':
168: args->MsgID = (u_short) strtol(optarg, NULL, 0);
169: break;
170: case 'p':
171: port = (u_short) strtol(optarg, NULL, 0);
172: break;
173: case 'q':
174: args->QoS = (char) strtol(optarg, NULL, 0);
175: if (args->QoS > MQTT_QOS_EXACTLY) {
176: printf("Error:: invalid QoS level %d\n", args->QoS);
177: args->free(args);
178: free(args);
179: return 1;
180: }
181: break;
182: case 'd':
183: args->Dup++;
184: break;
185: case 'r':
186: args->Retain++;
187: break;
188: case 'C':
189: args->notClear++;
190: break;
191: case 'f':
192: args->isFile++;
193: break;
194: case 'v':
195: io_incDebug;
196: break;
197: case 'h':
198: default:
199: args->free(args);
200: free(args);
201: Usage();
202: return 1;
203: }
204: argc -= optind;
205: argv += optind;
206: if (argc < 4) {
207: printf("Error:: host for connect not found, connection id, topic or value not supplied!\n\n");
208: args->free(args);
209: free(args);
210: Usage();
211: return 1;
212: } else {
213: AIT_FREE_VAL(&args->ConnID);
214: AIT_SET_STR(&args->ConnID, argv[1]);
215: AIT_FREE_VAL(&args->Publish);
216: AIT_SET_STR(&args->Publish, argv[2]);
217: AIT_FREE_VAL(&args->Value);
218: AIT_SET_STR(&args->Value, argv[3]);
219: }
220: if (!io_gethostbyname(*argv, port, &args->addr)) {
221: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
222: args->free(args);
223: free(args);
224: Usage();
225: return 1;
226: }
227: if (args->QoS && !args->MsgID)
228: args->MsgID = MQTT_DEFAULT_MSGID;
1.2.2.1 misho 229: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
230: AIT_FREE_VAL(&val);
1.2 misho 231:
1.2.2.5 ! misho 232: if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
1.2 misho 233: args->free(args);
234: free(args);
235: return 2;
236: }
237:
238: if (args->isFile && !OpenFile()) {
1.2.2.4 misho 239: mqtt_cli_Close(&args->cli);
1.2 misho 240: args->free(args);
241: free(args);
242: return 3;
243: }
244:
1.2.2.4 misho 245: switch ((ret = ConnectClient(args->cli->sock))) {
1.2 misho 246: case -1:
247: printf(">> FAILED!\n");
248: break;
249: case MQTT_RETCODE_ACCEPTED:
250: printf(">> OK\n");
251: break;
252: case MQTT_RETCODE_REFUSE_VER:
253: printf(">> Incorrect version\n");
254: break;
255: case MQTT_RETCODE_REFUSE_ID:
256: printf(">> Incorrect connectID\n");
257: break;
258: case MQTT_RETCODE_REFUSE_UNAVAIL:
259: printf(">> Service unavailable\n");
260: break;
261: case MQTT_RETCODE_REFUSE_USERPASS:
262: printf(">> Refuse user/pass\n");
263: break;
264: case MQTT_RETCODE_DENIED:
265: printf(">> DENIED.\n");
266: break;
267: }
268:
269: if (ret == MQTT_RETCODE_ACCEPTED) {
1.2.2.4 misho 270: ret = !(Publish(args->cli->sock) == args->MsgID);
271: } else
1.2 misho 272: ret = 4;
1.2.2.4 misho 273:
274: mqtt_cli_Close(&args->cli);
1.2 misho 275:
276: CloseFile();
277: args->free(args);
278: free(args);
279: return ret;
280: }