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