File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / example / Attic / cmds.c
Revision 1.1.2.15: download - view: text, annotated - select for diffs - revision graph
Tue Dec 6 10:33:37 2011 UTC (12 years, 8 months ago) by misho
Branches: mqtt1_0
add read for subscribe message

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

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