--- mqtt/example/Attic/cmds.c 2011/12/05 17:05:38 1.1.2.12 +++ mqtt/example/Attic/cmds.c 2011/12/13 15:23:43 1.1.2.19 @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -8,27 +9,35 @@ int main() { mqtt_msg_t *m; - mqtt_subscr_t s[4]; + mqtt_subscr_t s[4], *ss; mqtthdr_connflgs_t flg; - u_short ka; - int i; - char cid[BUFSIZ], user[BUFSIZ], pass[BUFSIZ], topic[BUFSIZ], message[BUFSIZ]; + mqtthdr_connack_t cack; + u_short ka, msgID; + int i, len; + char cid[BUFSIZ], user[BUFSIZ], pass[BUFSIZ], topic[BUFSIZ], message[BUFSIZ], *t, *msg; + struct mqtthdr *hdr; + u_char *qoses; m = mqtt_msgAlloc(0); /* conn* */ - printf("connect=%d/%d\n", m->msg_len, mqtt_msgCONNECT(m, "MRYN", "aaaaa", NULL, "bbb", NULL, 0, 0, 0)); + printf("connect=%d/%d\n", m->msg_len, mqtt_msgCONNECT(m, "MRYN", 0, "aaaaa", NULL, "bbb", NULL, 0, 0, 0)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); - flg = mqtt_readCONNECT(m, &ka, cid, sizeof cid, user, sizeof user, pass, sizeof pass, - topic, sizeof topic, message, sizeof message); - printf("read connect flags:: clean=%d will=%d qos=%d retain=%d pass=%d user=%d\n", + t = msg = NULL; + cack = mqtt_readCONNECT(m, &ka, cid, sizeof cid, user, sizeof user, pass, sizeof pass, &t, &msg); + flg.flags = cack.reserved; + printf("read connect %d flags:: clean=%d will=%d qos=%d retain=%d pass=%d user=%d\n", cack.retcode, flg.clean_sess, flg.will_flg, flg.will_qos, flg.will_retain, flg.password, flg.username); if (flg.reserved) { printf("Error:: mqtt_readCONNECT() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError()); return 1; } printf("++> KA=%d sec, ConnID=%s User=%s Pass=%s Will_Topic=%s Will_Message=%s\n", ka, - cid, user, pass, topic, message); + cid, user, pass, t, msg); + if (t) + free(t); + if (msg) + free(msg); printf("connack=%d/%d\n", m->msg_len, mqtt_msgCONNACK(m, 1)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); @@ -39,18 +48,31 @@ main() printf("publish=%d/%d\n", m->msg_len, mqtt_msgPUBLISH(m, "AAA/bbb/CCC/ddd", 7, 0, 2, 0, "OLE!!!", 7)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + len = sizeof message; + hdr = mqtt_readPUBLISH(m, topic, sizeof topic, &msgID, message, &len); + if (!hdr) { + printf("Error:: mqtt_readPUBLISH() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError()); + return 2; + } + printf("read publish: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); + printf("++> topic=%s MessageID=%d DATA=(%d)%s\n", topic, msgID, len, message); printf("puback=%d/%d\n", m->msg_len, mqtt_msgPUBACK(m, 10)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + printf("read puback=%d\n", mqtt_readPUBACK(m)); printf("pubrec=%d/%d\n", m->msg_len, mqtt_msgPUBREC(m, 11)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + printf("read pubrec=%d\n", mqtt_readPUBREC(m)); printf("pubrel=%d/%d\n", m->msg_len, mqtt_msgPUBREL(m, 12)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + printf("read pubrel=%d\n", mqtt_readPUBREL(m)); printf("pubcomp=%d/%d\n", m->msg_len, mqtt_msgPUBCOMP(m, 13)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + printf("read pubcomp=%d\n", mqtt_readPUBCOMP(m)); + getchar(); /* sub* */ memset(s, 0, sizeof s); @@ -66,15 +88,41 @@ main() printf("subscribe=%d/%d\n", m->msg_len, mqtt_msgSUBSCRIBE(m, s, 10, 0, 0)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + hdr = mqtt_readSUBSCRIBE(m, &msgID, &ss); + if (!hdr) { + printf("Error:: mqtt_readSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError()); + return 3; + } + printf("read subscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); + printf("++> MessageID=%d\n", msgID); + for (i = 0; ss[i].sub_topic._base; i++) + printf(" >>> QoS=%d Topic(%d)=%s\n", ss[i].sub_ret, ss[i].sub_topic._size, ss[i].sub_topic._base); + mqtt_subFree(&ss); printf("suback=%d/%d\n", m->msg_len, mqtt_msgSUBACK(m, s, 10)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + printf("read suback=%d\n", (len = mqtt_readSUBACK(m, &msgID, &qoses))); + for (i = 0; i < len; i++) + printf(" >>> QoS=%d\n", qoses[i]); + free(qoses); printf("unsubscribe=%d/%d\n", m->msg_len, mqtt_msgUNSUBSCRIBE(m, s, 10, 0, 1)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + hdr = mqtt_readUNSUBSCRIBE(m, &msgID, &ss); + if (!hdr) { + printf("Error:: mqtt_readUNSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError()); + return 3; + } + printf("read unsubscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); + printf("++> MessageID=%d\n", msgID); + for (i = 0; ss[i].sub_topic._base; i++) + printf(" >>> Topic(%d)=%s\n", ss[i].sub_topic._size, ss[i].sub_topic._base); + mqtt_subFree(&ss); printf("unsuback=%d/%d\n", m->msg_len, mqtt_msgUNSUBACK(m, 10)); for (i = 0; i < m->msg_len; i++) printf("%d\n", ((u_char*) m->msg_base)[i]); + printf("read unsuback=%d\n", mqtt_readUNSUBACK(m)); + getchar(); /* ping* */ printf("pingreq=%d/%d\n", m->msg_len, mqtt_msgPINGREQ(m));