Annotation of mqtt/example/cmds.c, revision 1.1.2.19

1.1.2.1   misho       1: #include <stdio.h>
1.1.2.16  misho       2: #include <stdlib.h>
1.1.2.5   misho       3: #include <string.h>
1.1.2.1   misho       4: #include <sys/types.h>
                      5: #include <aitmqtt.h>
                      6: 
                      7: 
                      8: int
                      9: main()
                     10: {
                     11:        mqtt_msg_t *m;
1.1.2.15  misho      12:        mqtt_subscr_t s[4], *ss;
1.1.2.10  misho      13:        mqtthdr_connflgs_t flg;
1.1.2.18  misho      14:        mqtthdr_connack_t cack;
1.1.2.14  misho      15:        u_short ka, msgID;
                     16:        int i, len;
1.1.2.17  misho      17:        char cid[BUFSIZ], user[BUFSIZ], pass[BUFSIZ], topic[BUFSIZ], message[BUFSIZ], *t, *msg;
1.1.2.14  misho      18:        struct mqtthdr *hdr;
1.1.2.16  misho      19:        u_char *qoses;
1.1.2.1   misho      20: 
                     21:        m = mqtt_msgAlloc(0);
                     22:        /* conn* */
1.1.2.19! misho      23:        printf("connect=%d/%d\n", m->msg_len, mqtt_msgCONNECT(m, "MRYN", 0, "aaaaa", NULL, "bbb", NULL, 0, 0, 0));
1.1.2.1   misho      24:        for (i = 0; i < m->msg_len; i++)
                     25:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.17  misho      26:        t = msg = NULL;
1.1.2.18  misho      27:        cack = mqtt_readCONNECT(m, &ka, cid, sizeof cid, user, sizeof user, pass, sizeof pass, &t, &msg);
                     28:        flg.flags = cack.reserved;
                     29:        printf("read connect %d flags:: clean=%d will=%d qos=%d retain=%d pass=%d user=%d\n", cack.retcode, 
1.1.2.10  misho      30:                        flg.clean_sess, flg.will_flg, flg.will_qos, flg.will_retain, flg.password, flg.username);
                     31:        if (flg.reserved) {
                     32:                printf("Error:: mqtt_readCONNECT() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     33:                return 1;
                     34:        }
                     35:        printf("++> KA=%d sec, ConnID=%s User=%s Pass=%s Will_Topic=%s Will_Message=%s\n", ka, 
1.1.2.17  misho      36:                        cid, user, pass, t, msg);
                     37:        if (t)
                     38:                free(t);
                     39:        if (msg)
                     40:                free(msg);
1.1.2.1   misho      41:        printf("connack=%d/%d\n", m->msg_len, mqtt_msgCONNACK(m, 1));
                     42:        for (i = 0; i < m->msg_len; i++)
                     43:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.11  misho      44:        printf("read connack=%d\n", mqtt_readCONNACK(m));
                     45:        getchar();
1.1.2.1   misho      46: 
                     47:        /* pub* */
1.1.2.4   misho      48:        printf("publish=%d/%d\n", m->msg_len, mqtt_msgPUBLISH(m, "AAA/bbb/CCC/ddd", 7, 0, 2, 0, "OLE!!!", 7));
1.1.2.1   misho      49:        for (i = 0; i < m->msg_len; i++)
                     50:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.14  misho      51:        len = sizeof message;
                     52:        hdr = mqtt_readPUBLISH(m, topic, sizeof topic, &msgID, message, &len);
                     53:        if (!hdr) {
                     54:                printf("Error:: mqtt_readPUBLISH() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     55:                return 2;
                     56:        }
                     57:        printf("read publish: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
                     58:        printf("++> topic=%s MessageID=%d DATA=(%d)%s\n", topic, msgID, len, message);
1.1.2.3   misho      59:        printf("puback=%d/%d\n", m->msg_len, mqtt_msgPUBACK(m, 10));
                     60:        for (i = 0; i < m->msg_len; i++)
                     61:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.13  misho      62:        printf("read puback=%d\n", mqtt_readPUBACK(m));
1.1.2.3   misho      63:        printf("pubrec=%d/%d\n", m->msg_len, mqtt_msgPUBREC(m, 11));
                     64:        for (i = 0; i < m->msg_len; i++)
                     65:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.13  misho      66:        printf("read pubrec=%d\n", mqtt_readPUBREC(m));
1.1.2.3   misho      67:        printf("pubrel=%d/%d\n", m->msg_len, mqtt_msgPUBREL(m, 12));
                     68:        for (i = 0; i < m->msg_len; i++)
                     69:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.13  misho      70:        printf("read pubrel=%d\n", mqtt_readPUBREL(m));
1.1.2.3   misho      71:        printf("pubcomp=%d/%d\n", m->msg_len, mqtt_msgPUBCOMP(m, 13));
                     72:        for (i = 0; i < m->msg_len; i++)
                     73:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.13  misho      74:        printf("read pubcomp=%d\n", mqtt_readPUBCOMP(m));
1.1.2.14  misho      75:        getchar();
1.1.2.3   misho      76: 
                     77:        /* sub* */
1.1.2.5   misho      78:        memset(s, 0, sizeof s);
1.1.2.9   misho      79:        s[0].sub_topic._size = 3;
                     80:        s[0].sub_topic._base = "a/b";
                     81:        s[0].sub_ret = MQTT_QOS_ACK;
                     82:        s[1].sub_topic._size = 3;
                     83:        s[1].sub_topic._base = "c/d";
                     84:        s[1].sub_ret = MQTT_QOS_ONCE;
                     85:        s[2].sub_topic._size = 7;
                     86:        s[2].sub_topic._base = "x/y/z/Q";
                     87:        s[2].sub_ret = MQTT_QOS_EXACTLY;
1.1.2.5   misho      88:        printf("subscribe=%d/%d\n", m->msg_len, mqtt_msgSUBSCRIBE(m, s, 10, 0, 0));
                     89:        for (i = 0; i < m->msg_len; i++)
                     90:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.15  misho      91:        hdr = mqtt_readSUBSCRIBE(m, &msgID, &ss);
                     92:        if (!hdr) {
                     93:                printf("Error:: mqtt_readSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     94:                return 3;
                     95:        }
                     96:        printf("read subscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
                     97:        printf("++> MessageID=%d\n", msgID);
                     98:        for (i = 0; ss[i].sub_topic._base; i++)
                     99:                printf(" >>> QoS=%d Topic(%d)=%s\n", ss[i].sub_ret, ss[i].sub_topic._size, ss[i].sub_topic._base);
                    100:        mqtt_subFree(&ss);
1.1.2.6   misho     101:        printf("suback=%d/%d\n", m->msg_len, mqtt_msgSUBACK(m, s, 10));
                    102:        for (i = 0; i < m->msg_len; i++)
                    103:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.16  misho     104:        printf("read suback=%d\n", (len = mqtt_readSUBACK(m, &msgID, &qoses)));
                    105:        for (i = 0; i < len; i++)
                    106:                printf(" >>> QoS=%d\n", qoses[i]);
                    107:        free(qoses);
1.1.2.7   misho     108:        printf("unsubscribe=%d/%d\n", m->msg_len, mqtt_msgUNSUBSCRIBE(m, s, 10, 0, 1));
                    109:        for (i = 0; i < m->msg_len; i++)
                    110:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.16  misho     111:        hdr = mqtt_readUNSUBSCRIBE(m, &msgID, &ss);
                    112:        if (!hdr) {
                    113:                printf("Error:: mqtt_readUNSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                    114:                return 3;
                    115:        }
                    116:        printf("read unsubscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
                    117:        printf("++> MessageID=%d\n", msgID);
                    118:        for (i = 0; ss[i].sub_topic._base; i++)
                    119:                printf(" >>> Topic(%d)=%s\n", ss[i].sub_topic._size, ss[i].sub_topic._base);
                    120:        mqtt_subFree(&ss);
1.1.2.7   misho     121:        printf("unsuback=%d/%d\n", m->msg_len, mqtt_msgUNSUBACK(m, 10));
                    122:        for (i = 0; i < m->msg_len; i++)
                    123:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.13  misho     124:        printf("read unsuback=%d\n", mqtt_readUNSUBACK(m));
1.1.2.14  misho     125:        getchar();
1.1.2.1   misho     126: 
1.1.2.8   misho     127:        /* ping* */
                    128:        printf("pingreq=%d/%d\n", m->msg_len, mqtt_msgPINGREQ(m));
                    129:        for (i = 0; i < m->msg_len; i++)
                    130:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.12  misho     131:        printf("read pingreq=%d\n", mqtt_readPINGREQ(m));
1.1.2.8   misho     132:        printf("pingresp=%d/%d\n", m->msg_len, mqtt_msgPINGRESP(m));
                    133:        for (i = 0; i < m->msg_len; i++)
                    134:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.12  misho     135:        printf("read pingresp=%d\n", mqtt_readPINGRESP(m));
1.1.2.8   misho     136: 
                    137:        printf("disconnect=%d/%d\n", m->msg_len, mqtt_msgDISCONNECT(m));
                    138:        for (i = 0; i < m->msg_len; i++)
                    139:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1.2.12  misho     140:        printf("read disconnect=%d\n", mqtt_readDISCONNECT(m));
1.1.2.8   misho     141: 
1.1.2.1   misho     142:        mqtt_msgFree(&m, 42);
                    143:        return 0;
                    144: }

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