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