Annotation of mqtt/src/mqtt.c, revision 1.1.1.1.2.23

1.1       misho       1: #include "global.h"
1.1.1.1.2.5  misho       2: #include "rtlm.h"
1.1.1.1.2.20  misho       3: #include "mqtt.h"
1.1.1.1.2.22  misho       4: #include "client.h"
1.1.1.1.2.5  misho       5: 
                      6: 
                      7: sl_config cfg;
1.1.1.1.2.20  misho       8: sqlite3 *acc, *pub;
                      9: FILE *logg;
                     10: extern char compiled[], compiledby[], compilehost[];
                     11: static char szCfgName[MAXPATHLEN];
                     12: int Verbose, Kill;
                     13: 
1.1.1.1.2.21  misho      14: struct tagArgs *args;
1.1.1.1.2.20  misho      15: 
                     16: 
                     17: static void
                     18: Usage(void)
                     19: {
                     20:        printf( " -= MQTT Client =- Publisher/Subscriber from ELWIX\n"
                     21:                "=== %s@%s === Compiled: %s ===\n\n"
                     22:                " Syntax: mqtt [options] <connect_to_broker[:port]> [value_for_publish]\n\n"
1.1.1.1.2.23! misho      23:                "\t-t <topic>\t\tPublish topic\n"
1.1.1.1.2.21  misho      24:                "\t-s <topic[|QoS]>\tSubscribe for this topic, if wish add different |QoS to topic\n"
1.1.1.1.2.22  misho      25:                "\t-q <QoS>\t\tQoS level (0-at most 1, 1-at least 1, 2-exactly 1)\n"
                     26:                "\t-d\t\t\tSend duplicate message\n"
                     27:                "\t-r\t\t\tRetain message from broker\n"
                     28:                "\t-c <config>\t\tService config\n"
                     29:                "\t-f\t\t\t'value_for_publish' is file instead text\n"
1.1.1.1.2.23! misho      30:                "\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
        !            31:                "\t-U <username>\t\tUsername\n"
        !            32:                "\t-P <password>\t\tPassword\n"
1.1.1.1.2.22  misho      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", 
1.1.1.1.2.20  misho      36:                compiledby, compilehost, compiled);
                     37: }
1.1       misho      38: 
1.1.1.1.2.22  misho      39: static void
                     40: cleanArgs(struct tagArgs * __restrict args)
                     41: {
1.1.1.1.2.23! misho      42:        AIT_FREE_VAL(&args->User);
        !            43:        AIT_FREE_VAL(&args->Pass);
1.1.1.1.2.22  misho      44:        AIT_FREE_VAL(&args->Publish);
                     45:        AIT_FREE_VAL(&args->Value);
                     46:        io_freeVars(&args->Subscribes);
                     47: }
                     48: 
1.1       misho      49: 
                     50: int
                     51: main(int argc, char **argv)
                     52: {
1.1.1.1.2.20  misho      53:        char ch, batch = 1, szStr[STRSIZ] = { 0 };
1.1.1.1.2.22  misho      54:        ait_val_t *v, val;
                     55:        u_short port = atoi(MQTT_PORT);
                     56:        int sock;
1.1.1.1.2.5  misho      57: 
1.1.1.1.2.21  misho      58:        if (!(args = malloc(sizeof(struct tagArgs)))) {
                     59:                printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
                     60:                return 1;
1.1.1.1.2.22  misho      61:        } else if (!(args->Subscribes = io_allocVars(1))) {
                     62:                printf("Error:: in subscribes array #%d - %s\n", io_GetErrno(), io_GetError());
                     63:                free(args);
                     64:                return 1;
                     65:        } else
                     66:                args->free = cleanArgs;
1.1.1.1.2.21  misho      67: 
1.1.1.1.2.20  misho      68:        strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
1.1.1.1.2.23! misho      69:        while ((ch = getopt(argc, argv, "U:P:p:t:s:q:drc:fDvh")) != -1)
1.1.1.1.2.20  misho      70:                switch (ch) {
1.1.1.1.2.23! misho      71:                        case 'U':
        !            72:                                AIT_FREE_VAL(&args->User);
        !            73:                                AIT_SET_STR(&args->User, optarg);
        !            74:                                break;
1.1.1.1.2.22  misho      75:                        case 'P':
1.1.1.1.2.23! misho      76:                                AIT_FREE_VAL(&args->Pass);
        !            77:                                AIT_SET_STR(&args->Pass, optarg);
1.1.1.1.2.22  misho      78:                                break;
1.1.1.1.2.20  misho      79:                        case 'p':
1.1.1.1.2.23! misho      80:                                port = (u_short) strtol(optarg, NULL, 0);
        !            81:                                break;
        !            82:                        case 't':
1.1.1.1.2.22  misho      83:                                AIT_FREE_VAL(&args->Publish);
                     84:                                AIT_SET_STR(&args->Publish, optarg);
1.1.1.1.2.20  misho      85:                                break;
                     86:                        case 's':
1.1.1.1.2.22  misho      87:                                v = malloc(sizeof(ait_val_t));
                     88:                                if (!v) {
                     89:                                        printf("Error:: not enough memory #%d - %s\n", errno, strerror(errno));
                     90:                                        args->free(args);
                     91:                                        free(args);
                     92:                                        return 1;
                     93:                                } else
                     94:                                        AIT_SET_STR(v, optarg);
                     95:                                io_arrayElem(args->Subscribes, io_arraySize(args->Subscribes), (void**) &v);
1.1.1.1.2.20  misho      96:                                break;
                     97:                        case 'q':
1.1.1.1.2.22  misho      98:                                args->QoS = (char) strtol(optarg, NULL, 0);
                     99:                                if (args->QoS < MQTT_QOS_ONCE || args->QoS > MQTT_QOS_EXACTLY) {
                    100:                                        printf("Error:: invalid QoS level %d\n", args->QoS);
                    101:                                        args->free(args);
                    102:                                        free(args);
                    103:                                        return 1;
                    104:                                }
1.1.1.1.2.20  misho     105:                                break;
                    106:                        case 'd':
1.1.1.1.2.22  misho     107:                                args->Dup++;
1.1.1.1.2.20  misho     108:                                break;
                    109:                        case 'r':
1.1.1.1.2.22  misho     110:                                args->Retain++;
1.1.1.1.2.20  misho     111:                                break;
                    112:                        case 'f':
1.1.1.1.2.22  misho     113:                                args->isFile++;
1.1.1.1.2.20  misho     114:                                break;
                    115:                        case 'c':
                    116:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
                    117:                                break;
                    118:                        case 'D':
                    119:                                batch = 0;
                    120:                                break;
                    121:                        case 'v':
                    122:                                Verbose++;
                    123:                                break;
                    124:                        case 'h':
                    125:                        default:
1.1.1.1.2.22  misho     126:                                args->free(args);
1.1.1.1.2.21  misho     127:                                free(args);
1.1.1.1.2.20  misho     128:                                Usage();
                    129:                                return 1;
                    130:                }
                    131:        argc -= optind;
                    132:        argv += optind;
1.1.1.1.2.22  misho     133:        if (!argc) {
                    134:                printf("Error:: host for connect not found!\n");
                    135:                args->free(args);
                    136:                free(args);
                    137:                Usage();
                    138:                return 1;
                    139:        }
                    140:        if (argc > 1)
                    141:                AIT_SET_STR(&args->Value, argv[1]);
                    142:        if (!io_gethostbyname(*argv, port, &args->addr)) {
                    143:                printf("Error:: host not connect #%d - %s\n", io_GetErrno(), io_GetError());
                    144:                args->free(args);
                    145:                free(args);
                    146:                Usage();
                    147:                return 1;
                    148:        }
                    149:        VERB(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
1.1.1.1.2.5  misho     150: 
1.1.1.1.2.20  misho     151:        if (LoadConfig(szCfgName, &cfg)) {
                    152:                printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
1.1.1.1.2.22  misho     153:                args->free(args);
1.1.1.1.2.21  misho     154:                free(args);
1.1.1.1.2.20  misho     155:                return 1;
1.1.1.1.2.7  misho     156:        }
                    157: 
1.1.1.1.2.22  misho     158:        if ((sock = InitClient()) == -1) {
                    159:                args->free(args);
                    160:                free(args);
                    161:                return 2;
                    162:        }
                    163: 
                    164:        shutdown(sock, SHUT_RDWR);
                    165:        close(sock);
                    166: 
1.1.1.1.2.5  misho     167:        UnloadConfig(&cfg);
1.1.1.1.2.22  misho     168:        args->free(args);
1.1.1.1.2.21  misho     169:        free(args);
1.1       misho     170:        return 0;
                    171: }

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