Annotation of libaitmqtt/example/cmds.c, revision 1.3.12.2
1.1 misho 1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <string.h>
4: #include <sys/types.h>
1.3.12.1 misho 5: #include <limits.h>
1.3.12.2! misho 6: #include <elwix.h>
1.1 misho 7: #include <aitmqtt.h>
8:
9:
10: int
11: main()
12: {
13: mqtt_msg_t *m;
14: mqtt_subscr_t s[4], *ss;
15: mqtthdr_connflgs_t flg;
16: mqtthdr_connack_t cack;
17: u_short ka, msgID;
18: int i, len;
1.2 misho 19: char cid[BUFSIZ], user[BUFSIZ], pass[BUFSIZ], topic[BUFSIZ], *t, *msg, *pub;
1.1 misho 20: struct mqtthdr *hdr;
21: u_char *qoses;
22:
23: /* conn* */
1.3.12.2! misho 24: m = mqtt_msgCONNECT("MRYN", 4, 0, "aaaaa", NULL, "bbb", NULL, 0, 0, 0);
! 25: printf("connect=%d/%p\n", m->msg_len, m);
1.3 misho 26: // for (i = 0; i < m->msg_len; i++)
27: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 28: t = msg = NULL;
29: cack = mqtt_readCONNECT(m, &ka, cid, sizeof cid, user, sizeof user, pass, sizeof pass, &t, &msg);
30: flg.flags = cack.reserved;
31: printf("read connect %d flags:: clean=%d will=%d qos=%d retain=%d pass=%d user=%d\n", cack.retcode,
32: flg.clean_sess, flg.will_flg, flg.will_qos, flg.will_retain, flg.password, flg.username);
33: if (flg.reserved) {
34: printf("Error:: mqtt_readCONNECT() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.3.12.2! misho 35: mqtt_msgFree(&m, 0);
1.1 misho 36: return 1;
37: }
38: printf("++> KA=%d sec, ConnID=%s User=%s Pass=%s Will_Topic=%s Will_Message=%s\n", ka,
39: cid, user, pass, t, msg);
40: if (t)
1.3.12.2! misho 41: e_free(t);
1.1 misho 42: if (msg)
1.3.12.2! misho 43: e_free(msg);
! 44: mqtt_msgFree(&m, 0);
! 45:
! 46: m = mqtt_msgCONNACK(1);
! 47: printf("connack=%d/%p\n", m->msg_len, m);
1.3 misho 48: // for (i = 0; i < m->msg_len; i++)
49: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 50: printf("read connack=%d\n", mqtt_readCONNACK(m));
1.3.12.2! misho 51: mqtt_msgFree(&m, 0);
1.1 misho 52: getchar();
53:
54: /* pub* */
1.3.12.2! misho 55: m = mqtt_msgPUBLISH("AAA/bbb/CCC/ddd", 7, 0, 2, 0, "OLE!!!", 7);
! 56: printf("publish=%d/%p\n", m->msg_len, m);
1.3 misho 57: // for (i = 0; i < m->msg_len; i++)
58: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.2 misho 59: if (!(len = mqtt_readPUBLISH(m, topic, sizeof topic, &msgID, (void**) &pub))) {
1.1 misho 60: printf("Error:: mqtt_readPUBLISH() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.3.12.2! misho 61: mqtt_msgFree(&m, 0);
1.1 misho 62: return 2;
1.3 misho 63: } else
64: hdr = (struct mqtthdr*) m->msg_base;
1.1 misho 65: printf("read publish: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
1.2 misho 66: printf("++> topic=%s MessageID=%d DATA=(%d)%s\n", topic, msgID, len, pub);
1.3.12.2! misho 67: mqtt_msgFree(&m, 0);
! 68: e_free(pub);
! 69:
! 70: m = mqtt_msgPUBACK(10);
! 71: printf("puback=%d/%p\n", m->msg_len, m);
1.3 misho 72: // for (i = 0; i < m->msg_len; i++)
73: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 74: printf("read puback=%d\n", mqtt_readPUBACK(m));
1.3.12.2! misho 75: mqtt_msgFree(&m, 0);
! 76:
! 77: m = mqtt_msgPUBREC(11);
! 78: printf("pubrec=%d/%p\n", m->msg_len, m);
1.3 misho 79: // for (i = 0; i < m->msg_len; i++)
80: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 81: printf("read pubrec=%d\n", mqtt_readPUBREC(m));
1.3.12.2! misho 82: mqtt_msgFree(&m, 0);
! 83:
! 84: m = mqtt_msgPUBREL(12);
! 85: printf("pubrel=%d/%p\n", m->msg_len, m);
1.3 misho 86: // for (i = 0; i < m->msg_len; i++)
87: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 88: printf("read pubrel=%d\n", mqtt_readPUBREL(m));
1.3.12.2! misho 89: mqtt_msgFree(&m, 0);
! 90:
! 91: m = mqtt_msgPUBCOMP(13);
! 92: printf("pubcomp=%d/%p\n", m->msg_len, m);
1.3 misho 93: // for (i = 0; i < m->msg_len; i++)
94: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 95: printf("read pubcomp=%d\n", mqtt_readPUBCOMP(m));
1.3.12.2! misho 96: mqtt_msgFree(&m, 0);
1.1 misho 97: getchar();
98:
99: /* sub* */
100: memset(s, 0, sizeof s);
101: s[0].sub_topic.msg_len = 3;
102: s[0].sub_topic.msg_base = "a/b";
1.3.12.2! misho 103: s[0].sub_qos = MQTT_QOS_ACK;
1.1 misho 104: s[1].sub_topic.msg_len = 3;
105: s[1].sub_topic.msg_base = "c/d";
1.3.12.2! misho 106: s[1].sub_qos = MQTT_QOS_ONCE;
1.1 misho 107: s[2].sub_topic.msg_len = 7;
108: s[2].sub_topic.msg_base = "x/y/z/Q";
1.3.12.2! misho 109: s[2].sub_qos = MQTT_QOS_EXACTLY;
! 110: m = mqtt_msgSUBSCRIBE(s, 10);
! 111: printf("subscribe=%d/%p\n", m->msg_len, m);
! 112: /*
1.1 misho 113: for (i = 0; i < m->msg_len; i++)
114: printf("%d\n", ((u_char*) m->msg_base)[i]);
1.3.12.2! misho 115: */
1.2 misho 116: if (mqtt_readSUBSCRIBE(m, &msgID, &ss) == -1) {
1.1 misho 117: printf("Error:: mqtt_readSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.3.12.2! misho 118: mqtt_msgFree(&m, 0);
1.1 misho 119: return 3;
120: }
121: printf("read subscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
122: printf("++> MessageID=%d\n", msgID);
123: for (i = 0; ss[i].sub_topic.msg_base; i++)
1.3.12.2! misho 124: printf(" >>> QoS=%d Topic(%d)=%s\n", ss[i].sub_qos, ss[i].sub_topic.msg_len,
1.1 misho 125: (char*) ss[i].sub_topic.msg_base);
1.3.12.2! misho 126: mqtt_msgFree(&m, 0);
1.1 misho 127: mqtt_subFree(&ss);
1.3.12.2! misho 128:
! 129: m = mqtt_msgSUBACK(s, 10);
! 130: printf("suback=%d/%p\n", m->msg_len, m);
1.3 misho 131: // for (i = 0; i < m->msg_len; i++)
132: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 133: printf("read suback=%d\n", (len = mqtt_readSUBACK(m, &msgID, &qoses)));
134: for (i = 0; i < len; i++)
135: printf(" >>> QoS=%d\n", qoses[i]);
1.3.12.2! misho 136: mqtt_msgFree(&m, 0);
! 137: e_free(qoses);
! 138:
! 139: m = mqtt_msgUNSUBSCRIBE(s, 10, 0, 1);
! 140: printf("unsubscribe=%d/%p\n", m->msg_len, m);
1.3 misho 141: // for (i = 0; i < m->msg_len; i++)
142: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.2 misho 143: if (mqtt_readUNSUBSCRIBE(m, &msgID, &ss) == -1) {
1.1 misho 144: printf("Error:: mqtt_readUNSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.3.12.2! misho 145: mqtt_msgFree(&m, 0);
1.1 misho 146: return 3;
147: }
148: printf("read unsubscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
149: printf("++> MessageID=%d\n", msgID);
150: for (i = 0; ss[i].sub_topic.msg_base; i++)
151: printf(" >>> Topic(%d)=%s\n", ss[i].sub_topic.msg_len, (char*) ss[i].sub_topic.msg_base);
1.3.12.2! misho 152: mqtt_msgFree(&m, 0);
1.1 misho 153: mqtt_subFree(&ss);
1.3.12.2! misho 154:
! 155: m = mqtt_msgUNSUBACK(10);
! 156: printf("unsuback=%d/%p\n", m->msg_len, m);
1.3 misho 157: // for (i = 0; i < m->msg_len; i++)
158: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 159: printf("read unsuback=%d\n", mqtt_readUNSUBACK(m));
1.3.12.2! misho 160: mqtt_msgFree(&m, 0);
1.1 misho 161: getchar();
162:
163: /* ping* */
1.3.12.2! misho 164: m = mqtt_msgPINGREQ();
! 165: printf("pingreq=%d/%p\n", m->msg_len, m);
1.3 misho 166: // for (i = 0; i < m->msg_len; i++)
167: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 168: printf("read pingreq=%d\n", mqtt_readPINGREQ(m));
1.3.12.2! misho 169: mqtt_msgFree(&m, 0);
! 170: m = mqtt_msgPINGRESP();
! 171: printf("pingresp=%d/%p\n", m->msg_len, m);
1.3 misho 172: // for (i = 0; i < m->msg_len; i++)
173: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 174: printf("read pingresp=%d\n", mqtt_readPINGRESP(m));
1.3.12.2! misho 175: mqtt_msgFree(&m, 0);
1.1 misho 176:
1.3.12.2! misho 177: m = mqtt_msgDISCONNECT();
! 178: printf("disconnect=%d/%p\n", m->msg_len, m);
1.3 misho 179: // for (i = 0; i < m->msg_len; i++)
180: // printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1 misho 181: printf("read disconnect=%d\n", mqtt_readDISCONNECT(m));
1.3.12.2! misho 182: mqtt_msgFree(&m, 0);
1.1 misho 183: return 0;
184: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>