Annotation of mqtt/src/mqtt_subs.c, revision 1.2.2.13

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[];
1.2.2.2   misho      10: volatile intptr_t Kill;
1.2.2.12  misho      11: sched_root_task_t *root;
1.2       misho      12: 
                     13: struct tagArgs *args;
                     14: 
                     15: 
                     16: static void
                     17: Usage(void)
                     18: {
                     19:        printf( " -= MQTT Subscriber Client =- Subscriber from ELWIX\n"
                     20:                "=== %s@%s === Compiled: %s ===\n\n"
1.2.2.3   misho      21:                " Syntax: mqtt_subs [options] <connect_to_broker[:port]> <ConnectID> [exec_script <value>]\n\n"
1.2       misho      22:                "\t-l <value2file>\t\tSave received values to file\n"
1.2.2.8   misho      23:                "\t-u\t\t\tUnsubscribe given topic(s)\n"
1.2       misho      24:                "\t-s <topic[|QoS]>\tSubscribe for this topic, if wish add different |QoS to topic\n"
                     25:                "\t-d\t\t\tSend duplicate message\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\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\n"
                     33:                "\t-D\t\t\tDaemon mode\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);
1.2.2.3   misho      43:        mqtt_subFree(&args->subscr);
1.2       misho      44:        AIT_FREE_VAL(&args->Will.Msg);
                     45:        AIT_FREE_VAL(&args->Will.Topic);
                     46:        AIT_FREE_VAL(&args->User);
                     47:        AIT_FREE_VAL(&args->Pass);
                     48:        AIT_FREE_VAL(&args->Publish);
                     49:        AIT_FREE_VAL(&args->Value);
                     50:        AIT_FREE_VAL(&args->ConnID);
                     51: }
                     52: 
                     53: static int
                     54: Subscribe(int sock, FILE *lf)
                     55: {
1.2.2.6   misho      56:        u_char *qoses, *qos;
                     57:        u_short mid;
                     58:        mqtt_subscr_t *sub;
1.2.2.3   misho      59: 
1.2.2.10  misho      60: #ifdef __NetBSD__
                     61:        srandom(getpid() ^ time(NULL));
                     62: #else
1.2.2.4   misho      63:        srandomdev();
1.2.2.10  misho      64: #endif
1.2.2.6   misho      65:        mid = random() % USHRT_MAX;
1.2.2.4   misho      66: 
1.2.2.6   misho      67:        printf(" > Execute SUBSCRIBE request #%d ... ", mid);
                     68:        qoses = mqtt_cli_Subscribe(args->cli, args->subscr, mid, args->Dup, MQTT_QOS_ACK);
                     69:        if (!qoses) {
                     70:                printf("Error:: Subscribe #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2.2.3   misho      71:                return -1;
1.2.2.6   misho      72:        } else
                     73:                printf("OK\n");
1.2.2.3   misho      74: 
1.2.2.6   misho      75:        for (sub = args->subscr, qos = qoses; sub->sub_topic.msg_base; sub++, qos++)
                     76:                printf("  + Topic %s with QoS %d subscribed %s\n", (char*)
                     77:                                sub->sub_topic.msg_base, sub->sub_ret, *qos ? "done" : "failed");
                     78: 
                     79:        free(qoses);
                     80:        return 0;
                     81: }
                     82: 
                     83: static int
                     84: Unsubscribe(int sock)
                     85: {
                     86:        u_short mid;
                     87: 
1.2.2.10  misho      88: #ifdef __NetBSD__
                     89:        srandom(getpid() ^ time(NULL));
                     90: #else
1.2.2.6   misho      91:        srandomdev();
1.2.2.10  misho      92: #endif
1.2.2.6   misho      93:        mid = random() % USHRT_MAX;
                     94: 
1.2.2.7   misho      95:        printf(" > Execute UNSUBSCRIBE request #%d ... ", mid);
1.2.2.6   misho      96:        if (mqtt_cli_Unsubscribe(args->cli, args->subscr, mid, args->Dup, MQTT_QOS_ACK)) {
                     97:                printf("Error:: Unsubscribe #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2.2.3   misho      98:                return -1;
1.2.2.7   misho      99:        } else
                    100:                printf("OK\n");
1.2.2.3   misho     101: 
1.2       misho     102:        return 0;
                    103: }
                    104: 
                    105: 
1.2.2.12  misho     106: static void *
                    107: pubRX(sched_task_t *task)
                    108: {
                    109:        int siz, rlen;
1.2.2.13! misho     110:        char szTopic[STRSIZ] = { 0 };
1.2.2.12  misho     111:        void *data = NULL;
                    112:        u_short mid;
                    113: 
                    114:        rlen = RecvFrom(TASK_FD(task));
1.2.2.13! misho     115:        if (rlen == -1)
1.2.2.12  misho     116:                goto end;
1.2.2.13! misho     117:        siz = mqtt_readPUBLISH(args->msg, szTopic, sizeof szTopic, &mid, &data);
1.2.2.12  misho     118:        if (siz == -1)
                    119:                goto end;
                    120: 
1.2.2.13! misho     121:        fprintf(TASK_ARG(task), "\nMessage ID: 0x%04hu, Length: %u, Topic: %s\n", 
        !           122:                        mid, siz, szTopic);
1.2.2.12  misho     123: 
                    124:        if (data) {
                    125:                fputs((const char*) data, TASK_ARG(task));
                    126:                free(data);
                    127:        }
1.2.2.13! misho     128: 
        !           129:        fprintf(TASK_ARG(task), "\n.\n");
        !           130:        fflush(TASK_ARG(task));
1.2.2.12  misho     131: end:
                    132:        schedReadSelf(task);
                    133:        return NULL;
                    134: }
                    135: 
                    136: 
1.2       misho     137: int
                    138: main(int argc, char **argv)
                    139: {
1.2.2.6   misho     140:        char ch, un = 0, idx = 0, batch = 1;
1.2.2.3   misho     141:        ait_val_t val;
1.2       misho     142:        u_short port = atoi(MQTT_PORT);
1.2.2.3   misho     143:        mqtt_subscr_t *sub;
1.2.2.5   misho     144:        int ret = 0;
1.2.2.3   misho     145:        char *str, szStr[STRSIZ], szLogName[MAXPATHLEN] = { 0 };
1.2       misho     146:        FILE *lf;
                    147: 
1.2.2.9   misho     148:        if (!(args = io_malloc(sizeof(struct tagArgs)))) {
1.2       misho     149:                printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
                    150:                return 1;
                    151:        } else
                    152:                memset(args, 0, sizeof(struct tagArgs));
1.2.2.3   misho     153:        if (!(args->subscr = mqtt_subAlloc(idx))) {
                    154:                printf("Error:: in subscribes array #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2.2.9   misho     155:                io_free(args);
1.2       misho     156:                return 1;
                    157:        } else
                    158:                args->free = cleanArgs;
                    159: 
                    160:        if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
                    161:                printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                    162:                args->free(args);
1.2.2.9   misho     163:                io_free(args);
1.2       misho     164:                return 1;
                    165:        }
                    166: 
1.2.2.4   misho     167:        AIT_SET_STR(&args->ConnID, "");
1.2       misho     168:        AIT_SET_STR(&args->User, "");
                    169:        AIT_SET_STR(&args->Pass, "");
                    170: 
                    171:        args->ka = MQTT_KEEPALIVE;
1.2.2.6   misho     172:        while ((ch = getopt(argc, argv, "T:U:P:p:s:q:dl:W:M:CDvuh")) != -1)
1.2       misho     173:                switch (ch) {
                    174:                        case 'T':
                    175:                                args->ka = (u_short) strtol(optarg, NULL, 0);
                    176:                                break;
                    177:                        case 'M':
                    178:                                AIT_FREE_VAL(&args->Will.Msg);
                    179:                                AIT_SET_STR(&args->Will.Msg, optarg);
                    180:                                break;
                    181:                        case 'W':
                    182:                                AIT_FREE_VAL(&args->Will.Topic);
                    183:                                AIT_SET_STR(&args->Will.Topic, optarg);
                    184:                                break;
                    185:                        case 'U':
                    186:                                AIT_FREE_VAL(&args->User);
                    187:                                AIT_SET_STR(&args->User, optarg);
                    188:                                break;
                    189:                        case 'P':
                    190:                                AIT_FREE_VAL(&args->Pass);
                    191:                                AIT_SET_STR(&args->Pass, optarg);
                    192:                                break;
                    193:                        case 'p':
                    194:                                port = (u_short) strtol(optarg, NULL, 0);
                    195:                                break;
                    196:                        case 's':
1.2.2.3   misho     197:                                sub = mqtt_subRealloc(&args->subscr, idx + 1);
                    198:                                if (!sub) {
                    199:                                        printf("Error:: #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.2       misho     200:                                        args->free(args);
1.2.2.9   misho     201:                                        io_free(args);
1.2       misho     202:                                        return 1;
                    203:                                } else
1.2.2.3   misho     204:                                        sub += idx++;
                    205: 
                    206:                                strlcpy(szStr, optarg, sizeof szStr);
                    207:                                if ((str = strchr(szStr, '|'))) {
                    208:                                        *str++ = 0;
                    209:                                        *str -= 0x30;
                    210:                                        if (*str < 0 || *str > MQTT_QOS_RESERVED)
                    211:                                                sub->sub_ret = (u_char) args->QoS;
                    212:                                        else
                    213:                                                sub->sub_ret = (u_char) *str;
                    214:                                } else
                    215:                                        sub->sub_ret = (u_char) args->QoS;
1.2.2.11  misho     216:                                sub->sub_topic.msg_base = strdup(szStr);
1.2.2.3   misho     217:                                sub->sub_topic.msg_len = strlen(szStr);
1.2       misho     218:                                break;
                    219:                        case 'q':
                    220:                                args->QoS = (char) strtol(optarg, NULL, 0);
                    221:                                if (args->QoS > MQTT_QOS_EXACTLY) {
                    222:                                        printf("Error:: invalid QoS level %d\n", args->QoS);
                    223:                                        args->free(args);
1.2.2.9   misho     224:                                        io_free(args);
1.2       misho     225:                                        return 1;
                    226:                                }
                    227:                                break;
                    228:                        case 'd':
                    229:                                args->Dup++;
                    230:                                break;
                    231:                        case 'C':
                    232:                                args->notClear++;
                    233:                                break;
                    234:                        case 'l':
                    235:                                strlcpy(szLogName, optarg, sizeof szLogName);
                    236:                                break;
                    237:                        case 'D':
                    238:                                batch = 0;
                    239:                                break;
                    240:                        case 'v':
                    241:                                io_incDebug;
                    242:                                break;
1.2.2.6   misho     243:                        case 'u':
                    244:                                un = 1;
                    245:                                break;
1.2       misho     246:                        case 'h':
                    247:                        default:
                    248:                                args->free(args);
1.2.2.9   misho     249:                                io_free(args);
1.2       misho     250:                                Usage();
                    251:                                return 1;
                    252:                }
                    253:        argc -= optind;
                    254:        argv += optind;
1.2.2.3   misho     255:        if (argc < 2) {
1.2       misho     256:                printf("Error:: host for connect not found, connection id or topic not supplied!\n\n");
                    257:                args->free(args);
1.2.2.9   misho     258:                io_free(args);
1.2       misho     259:                Usage();
                    260:                return 1;
1.2.2.4   misho     261:        } else {
                    262:                AIT_FREE_VAL(&args->ConnID);
                    263:                AIT_SET_STR(&args->ConnID, argv[1]);
                    264:        }
1.2.2.3   misho     265:        if (argc > 2) {
1.2       misho     266:                AIT_FREE_VAL(&args->Value);
1.2.2.3   misho     267:                AIT_SET_STR(&args->Value, argv[2]);
1.2       misho     268:        }
                    269:        if (!io_gethostbyname(*argv, port, &args->addr)) {
                    270:                printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
                    271:                args->free(args);
1.2.2.9   misho     272:                io_free(args);
1.2       misho     273:                Usage();
                    274:                return 1;
                    275:        }
1.2.2.1   misho     276:        printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
                    277:        AIT_FREE_VAL(&val);
1.2       misho     278: 
1.2.2.6   misho     279:        if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
1.2       misho     280:                args->free(args);
1.2.2.9   misho     281:                io_free(args);
1.2       misho     282:                return 2;
                    283:        }
                    284: 
1.2.2.5   misho     285:        switch ((ret = ConnectClient(args->cli->sock))) {
1.2       misho     286:                case -1:
                    287:                        printf(">> FAILED!\n");
                    288:                        break;
                    289:                case MQTT_RETCODE_ACCEPTED:
                    290:                        printf(">> OK\n");
                    291:                        break;
                    292:                case MQTT_RETCODE_REFUSE_VER:
                    293:                        printf(">> Incorrect version\n");
                    294:                        break;
                    295:                case MQTT_RETCODE_REFUSE_ID:
                    296:                        printf(">> Incorrect connectID\n");
                    297:                        break;
                    298:                case MQTT_RETCODE_REFUSE_UNAVAIL:
                    299:                        printf(">> Service unavailable\n");
                    300:                        break;
                    301:                case MQTT_RETCODE_REFUSE_USERPASS:
                    302:                        printf(">> Refuse user/pass\n");
                    303:                        break;
                    304:                case MQTT_RETCODE_DENIED:
                    305:                        printf(">> DENIED.\n");
                    306:                        break;
                    307:        }
                    308: 
                    309:        if (ret == MQTT_RETCODE_ACCEPTED) {
                    310:                if (*szLogName)
                    311:                        lf = fopen(szLogName, "w");
                    312:                else
                    313:                        lf = stdout;
                    314:                if (lf) {
1.2.2.12  misho     315:                        root = schedBegin();
                    316: 
1.2.2.7   misho     317:                        ret = Subscribe(args->cli->sock, lf);
1.2.2.12  misho     318: 
                    319:                        schedRead(root, pubRX, lf, args->cli->sock, NULL, 0);
                    320:                        schedRun(root, &Kill);
                    321: 
1.2.2.7   misho     322:                        if (un)
                    323:                                Unsubscribe(args->cli->sock);
1.2       misho     324:                        fclose(lf);
1.2.2.12  misho     325: 
                    326:                        schedEnd(&root);
1.2       misho     327:                } else
                    328:                        printf("Error:: in subscribe file #%d - %s\n", errno, strerror(errno));
1.2.2.5   misho     329:        } else
1.2       misho     330:                ret = 3;
1.2.2.5   misho     331: 
                    332:        mqtt_cli_Close(&args->cli);
1.2       misho     333: 
                    334:        args->free(args);
1.2.2.9   misho     335:        io_free(args);
1.2       misho     336:        return ret;
                    337: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>