Annotation of mqtt/src/mqtt_pub.c, revision 1.2.2.7
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-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: int siz = 0;
1.2.2.6 misho 53: u_short mid = 0;
1.2 misho 54:
1.2.2.6 misho 55: srandomdev();
56: mid = random() % USHRT_MAX;
1.2 misho 57:
1.2.2.6 misho 58: printf(" > Execute PUBLISH request #%d ... ", mid);
59: siz = mqtt_cli_Publish(args->cli, mid, args->Dup, args->QoS, args->Retain,
60: AIT_GET_STR(&args->Publish), AIT_ADDR(&args->Value), AIT_LEN(&args->Value));
61: if (siz == -1) {
62: printf("Error:: Publish #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2 misho 63: return -1;
1.2.2.6 misho 64: } else
65: printf("OK\n");
1.2 misho 66:
1.2.2.6 misho 67: return siz;
1.2 misho 68: }
69:
70:
71: int
72: main(int argc, char **argv)
73: {
74: char ch;
75: ait_val_t val;
76: u_short port = atoi(MQTT_PORT);
1.2.2.4 misho 77: int ret = 0;
1.2 misho 78:
1.2.2.7 ! misho 79: if (!(args = io_malloc(sizeof(struct tagArgs)))) {
1.2 misho 80: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
81: return 1;
82: } else
83: memset(args, 0, sizeof(struct tagArgs));
84: args->free = cleanArgs;
85:
86: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
87: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
88: args->free(args);
1.2.2.7 ! misho 89: io_free(args);
1.2 misho 90: return 1;
91: }
92:
93: AIT_SET_STR(&args->ConnID, "");
94: AIT_SET_STR(&args->User, "");
95: AIT_SET_STR(&args->Pass, "");
96:
97: args->ka = MQTT_KEEPALIVE;
1.2.2.6 misho 98: while ((ch = getopt(argc, argv, "T:U:P:p:q:drCW:M:fvh")) != -1)
1.2 misho 99: switch (ch) {
100: case 'T':
101: args->ka = (u_short) strtol(optarg, NULL, 0);
102: break;
103: case 'M':
104: AIT_FREE_VAL(&args->Will.Msg);
105: AIT_SET_STR(&args->Will.Msg, optarg);
106: break;
107: case 'W':
108: AIT_FREE_VAL(&args->Will.Topic);
109: AIT_SET_STR(&args->Will.Topic, optarg);
110: break;
111: case 'U':
112: AIT_FREE_VAL(&args->User);
113: AIT_SET_STR(&args->User, optarg);
114: break;
115: case 'P':
116: AIT_FREE_VAL(&args->Pass);
117: AIT_SET_STR(&args->Pass, optarg);
118: break;
119: case 'p':
120: port = (u_short) strtol(optarg, NULL, 0);
121: break;
122: case 'q':
123: args->QoS = (char) strtol(optarg, NULL, 0);
124: if (args->QoS > MQTT_QOS_EXACTLY) {
125: printf("Error:: invalid QoS level %d\n", args->QoS);
126: args->free(args);
1.2.2.7 ! misho 127: io_free(args);
1.2 misho 128: return 1;
129: }
130: break;
131: case 'd':
132: args->Dup++;
133: break;
134: case 'r':
135: args->Retain++;
136: break;
137: case 'C':
138: args->notClear++;
139: break;
140: case 'f':
141: args->isFile++;
142: break;
143: case 'v':
144: io_incDebug;
145: break;
146: case 'h':
147: default:
148: args->free(args);
1.2.2.7 ! misho 149: io_free(args);
1.2 misho 150: Usage();
151: return 1;
152: }
153: argc -= optind;
154: argv += optind;
155: if (argc < 4) {
156: printf("Error:: host for connect not found, connection id, topic or value not supplied!\n\n");
157: args->free(args);
1.2.2.7 ! misho 158: io_free(args);
1.2 misho 159: Usage();
160: return 1;
161: } else {
162: AIT_FREE_VAL(&args->ConnID);
163: AIT_SET_STR(&args->ConnID, argv[1]);
164: AIT_FREE_VAL(&args->Publish);
165: AIT_SET_STR(&args->Publish, argv[2]);
166: AIT_FREE_VAL(&args->Value);
167: AIT_SET_STR(&args->Value, argv[3]);
168: }
169: if (!io_gethostbyname(*argv, port, &args->addr)) {
170: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
171: args->free(args);
1.2.2.7 ! misho 172: io_free(args);
1.2 misho 173: Usage();
174: return 1;
175: }
1.2.2.1 misho 176: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
177: AIT_FREE_VAL(&val);
1.2 misho 178:
1.2.2.5 misho 179: if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
1.2 misho 180: args->free(args);
1.2.2.7 ! misho 181: io_free(args);
1.2 misho 182: return 2;
183: }
184:
185: if (args->isFile && !OpenFile()) {
1.2.2.4 misho 186: mqtt_cli_Close(&args->cli);
1.2 misho 187: args->free(args);
1.2.2.7 ! misho 188: io_free(args);
1.2 misho 189: return 3;
190: }
191:
1.2.2.4 misho 192: switch ((ret = ConnectClient(args->cli->sock))) {
1.2 misho 193: case -1:
194: printf(">> FAILED!\n");
195: break;
196: case MQTT_RETCODE_ACCEPTED:
197: printf(">> OK\n");
198: break;
199: case MQTT_RETCODE_REFUSE_VER:
200: printf(">> Incorrect version\n");
201: break;
202: case MQTT_RETCODE_REFUSE_ID:
203: printf(">> Incorrect connectID\n");
204: break;
205: case MQTT_RETCODE_REFUSE_UNAVAIL:
206: printf(">> Service unavailable\n");
207: break;
208: case MQTT_RETCODE_REFUSE_USERPASS:
209: printf(">> Refuse user/pass\n");
210: break;
211: case MQTT_RETCODE_DENIED:
212: printf(">> DENIED.\n");
213: break;
214: }
215:
216: if (ret == MQTT_RETCODE_ACCEPTED) {
1.2.2.6 misho 217: ret = (Publish(args->cli->sock) == -1);
1.2.2.4 misho 218: } else
1.2 misho 219: ret = 4;
1.2.2.4 misho 220:
221: mqtt_cli_Close(&args->cli);
1.2 misho 222:
223: CloseFile();
224: args->free(args);
1.2.2.7 ! misho 225: io_free(args);
1.2 misho 226: return ret;
227: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>