Annotation of mqtt/src/mqtt_subs.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: extern char compiled[], compiledby[], compilehost[];
! 10: intptr_t Kill;
! 11:
! 12: struct tagArgs *args;
! 13:
! 14:
! 15: static void
! 16: Usage(void)
! 17: {
! 18: printf( " -= MQTT Subscriber Client =- Subscriber from ELWIX\n"
! 19: "=== %s@%s === Compiled: %s ===\n\n"
! 20: " Syntax: mqtt_subs [options] <connect_to_broker[:port]> <ConnectID> <topic> [exec_script <value>]\n\n"
! 21: "\t-l <value2file>\t\tSave received values to file\n"
! 22: "\t-s <topic[|QoS]>\tSubscribe for this topic, if wish add different |QoS to topic\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\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\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\n"
! 32: "\t-D\t\t\tDaemon mode\n"
! 33: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
! 34: "\t-h\t\t\tHelp! This screen\n\n",
! 35: compiledby, compilehost, compiled);
! 36: }
! 37:
! 38: static void
! 39: cleanArgs(struct tagArgs * __restrict args)
! 40: {
! 41: mqtt_msgFree(&args->msg, 42);
! 42: AIT_FREE_VAL(&args->Will.Msg);
! 43: AIT_FREE_VAL(&args->Will.Topic);
! 44: AIT_FREE_VAL(&args->User);
! 45: AIT_FREE_VAL(&args->Pass);
! 46: AIT_FREE_VAL(&args->Publish);
! 47: AIT_FREE_VAL(&args->Value);
! 48: AIT_FREE_VAL(&args->ConnID);
! 49: io_freeVars(&args->Subscribes);
! 50: }
! 51:
! 52: static int
! 53: Subscribe(int sock, FILE *lf)
! 54: {
! 55: return 0;
! 56: }
! 57:
! 58:
! 59: int
! 60: main(int argc, char **argv)
! 61: {
! 62: char ch, batch = 1;
! 63: ait_val_t *v, val;
! 64: u_short port = atoi(MQTT_PORT);
! 65: int sock, ret = 0;
! 66: char szLogName[MAXPATHLEN] = { 0 };
! 67: FILE *lf;
! 68:
! 69: if (!(args = malloc(sizeof(struct tagArgs)))) {
! 70: printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
! 71: return 1;
! 72: } else
! 73: memset(args, 0, sizeof(struct tagArgs));
! 74: if (!(args->Subscribes = io_allocVars(1))) {
! 75: printf("Error:: in subscribes array #%d - %s\n", io_GetErrno(), io_GetError());
! 76: free(args);
! 77: return 1;
! 78: } else
! 79: args->free = cleanArgs;
! 80:
! 81: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
! 82: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
! 83: args->free(args);
! 84: free(args);
! 85: return 1;
! 86: }
! 87:
! 88: AIT_SET_STR(&args->ConnID, "");
! 89: AIT_SET_STR(&args->User, "");
! 90: AIT_SET_STR(&args->Pass, "");
! 91:
! 92: args->ka = MQTT_KEEPALIVE;
! 93: while ((ch = getopt(argc, argv, "T:U:P:p:s:q:dl:W:M:CDvh")) != -1)
! 94: switch (ch) {
! 95: case 'T':
! 96: args->ka = (u_short) strtol(optarg, NULL, 0);
! 97: break;
! 98: case 'M':
! 99: AIT_FREE_VAL(&args->Will.Msg);
! 100: AIT_SET_STR(&args->Will.Msg, optarg);
! 101: break;
! 102: case 'W':
! 103: AIT_FREE_VAL(&args->Will.Topic);
! 104: AIT_SET_STR(&args->Will.Topic, optarg);
! 105: break;
! 106: case 'U':
! 107: AIT_FREE_VAL(&args->User);
! 108: AIT_SET_STR(&args->User, optarg);
! 109: break;
! 110: case 'P':
! 111: AIT_FREE_VAL(&args->Pass);
! 112: AIT_SET_STR(&args->Pass, optarg);
! 113: break;
! 114: case 'p':
! 115: port = (u_short) strtol(optarg, NULL, 0);
! 116: break;
! 117: case 's':
! 118: v = io_allocVar();
! 119: if (!v) {
! 120: printf("Error:: not enough memory #%d - %s\n", errno, strerror(errno));
! 121: args->free(args);
! 122: free(args);
! 123: return 1;
! 124: } else
! 125: AIT_SET_STR(v, optarg);
! 126: io_arrayElem(args->Subscribes, io_arraySize(args->Subscribes), v);
! 127: break;
! 128: case 'q':
! 129: args->QoS = (char) strtol(optarg, NULL, 0);
! 130: if (args->QoS > MQTT_QOS_EXACTLY) {
! 131: printf("Error:: invalid QoS level %d\n", args->QoS);
! 132: args->free(args);
! 133: free(args);
! 134: return 1;
! 135: }
! 136: break;
! 137: case 'd':
! 138: args->Dup++;
! 139: break;
! 140: case 'C':
! 141: args->notClear++;
! 142: break;
! 143: case 'l':
! 144: strlcpy(szLogName, optarg, sizeof szLogName);
! 145: break;
! 146: case 'D':
! 147: batch = 0;
! 148: break;
! 149: case 'v':
! 150: io_incDebug;
! 151: break;
! 152: case 'h':
! 153: default:
! 154: args->free(args);
! 155: free(args);
! 156: Usage();
! 157: return 1;
! 158: }
! 159: argc -= optind;
! 160: argv += optind;
! 161: if (argc < 3) {
! 162: printf("Error:: host for connect not found, connection id or topic not supplied!\n\n");
! 163: args->free(args);
! 164: free(args);
! 165: Usage();
! 166: return 1;
! 167: } else {
! 168: AIT_FREE_VAL(&args->ConnID);
! 169: AIT_SET_STR(&args->ConnID, argv[1]);
! 170: AIT_FREE_VAL(&args->Publish);
! 171: AIT_SET_STR(&args->Publish, argv[2]);
! 172: }
! 173: if (argc > 3) {
! 174: AIT_FREE_VAL(&args->Value);
! 175: AIT_SET_STR(&args->Value, argv[3]);
! 176: }
! 177: if (!io_gethostbyname(*argv, port, &args->addr)) {
! 178: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
! 179: args->free(args);
! 180: free(args);
! 181: Usage();
! 182: return 1;
! 183: }
! 184: ioVERBOSE(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
! 185:
! 186: if ((sock = InitClient()) == -1) {
! 187: args->free(args);
! 188: free(args);
! 189: return 2;
! 190: }
! 191:
! 192: printf("Connected ... ");
! 193: switch ((ret = try2Connect(sock))) {
! 194: case -1:
! 195: printf(">> FAILED!\n");
! 196: break;
! 197: case MQTT_RETCODE_ACCEPTED:
! 198: printf(">> OK\n");
! 199: break;
! 200: case MQTT_RETCODE_REFUSE_VER:
! 201: printf(">> Incorrect version\n");
! 202: break;
! 203: case MQTT_RETCODE_REFUSE_ID:
! 204: printf(">> Incorrect connectID\n");
! 205: break;
! 206: case MQTT_RETCODE_REFUSE_UNAVAIL:
! 207: printf(">> Service unavailable\n");
! 208: break;
! 209: case MQTT_RETCODE_REFUSE_USERPASS:
! 210: printf(">> Refuse user/pass\n");
! 211: break;
! 212: case MQTT_RETCODE_DENIED:
! 213: printf(">> DENIED.\n");
! 214: break;
! 215: }
! 216:
! 217: if (ret == MQTT_RETCODE_ACCEPTED) {
! 218: if (*szLogName)
! 219: lf = fopen(szLogName, "w");
! 220: else
! 221: lf = stdout;
! 222: if (lf) {
! 223: ret = Subscribe(sock, lf);
! 224: fclose(lf);
! 225: shutdown(sock, SHUT_RDWR);
! 226: } else
! 227: printf("Error:: in subscribe file #%d - %s\n", errno, strerror(errno));
! 228: } else
! 229: ret = 3;
! 230:
! 231: close(sock);
! 232: args->free(args);
! 233: free(args);
! 234: return ret;
! 235: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>